Skip to content

Commit

Permalink
Rename WeightMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao committed Aug 23, 2024
1 parent 1d8822c commit 2e2078f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions vsadjust/tweaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'Tweak',
'tweak_clip', 'multi_tweak',

'BalanceMode', 'WeightMode', 'Override',
'BalanceMode', 'BalanceWeightMode', 'Override',

'auto_balance'
]
Expand Down Expand Up @@ -149,7 +149,7 @@ class BalanceMode(IntEnum):
DIMMING = 2


class WeightMode(IntEnum):
class BalanceWeightMode(IntEnum):
INTERPOLATE = 0
MEDIAN = 1
MEAN = 2
Expand All @@ -161,7 +161,7 @@ class WeightMode(IntEnum):
class Override(NamedTuple):
frame_range: FrameRangeN
cont: SupportsFloat
override_mode: WeightMode = WeightMode.INTERPOLATE
override_mode: BalanceWeightMode = BalanceWeightMode.INTERPOLATE


def auto_balance(
Expand All @@ -170,7 +170,7 @@ def auto_balance(
ref: vs.VideoNode | None = None, radius: int = 1, delta_thr: float = 0.4,
min_thr: float = 1.0, max_thr: float = 5.0,
min_thr_tr: float = 1.0, max_thr_tr: float = 5.0,
balance_mode: BalanceMode = BalanceMode.UNDIMMING, weight_mode: WeightMode = WeightMode.MEAN,
balance_mode: BalanceMode = BalanceMode.UNDIMMING, weight_mode: BalanceWeightMode = BalanceWeightMode.MEAN,
prop: bool = False
) -> vs.VideoNode:
import numpy as np
Expand All @@ -193,12 +193,12 @@ def auto_balance(
)
))

if weight_mode == WeightMode.NONE:
if weight_mode == BalanceWeightMode.NONE:
raise CustomValueError(auto_balance, 'Global weight mode can\'t be NONE!')

ref_stats = ref_clip.std.PlaneStats()

over_mapped = list[tuple[range, float, WeightMode]]()
over_mapped = list[tuple[range, float, BalanceWeightMode]]()

if frame_overrides:
frame_overrides = [frame_overrides] if isinstance(frame_overrides, Override) else list(frame_overrides)
Expand All @@ -220,7 +220,7 @@ def _weighted(x: float, y: float, z: float) -> float:
nobalanceclip = clip.std.SetFrameProps(AutoBalance=False) if prop else clip

def _autobalance(n: int, f: Sequence[vs.VideoFrame]) -> vs.VideoNode:
override: tuple[range, float, WeightMode] | None = next((x for x in over_mapped if n in x[0]), None)
override: tuple[range, float, BalanceWeightMode] | None = next((x for x in over_mapped if n in x[0]), None)

psvalues: Any = np.asarray([
_weighted(target, get_prop(frame.props, 'PlaneStatsMax', int), zero) for frame in f
Expand All @@ -245,10 +245,10 @@ def _autobalance(n: int, f: Sequence[vs.VideoFrame]) -> vs.VideoNode:

psvalues[(abs(psvalues - curr_value) > delta_thr)] = curr_value

def _get_cont(mode: WeightMode, frange: range) -> Any:
if mode == WeightMode.INTERPOLATE:
def _get_cont(mode: BalanceWeightMode, frange: range) -> Any:
if mode == BalanceWeightMode.INTERPOLATE:
if radius < 1:
raise CustomValueError(auto_balance, 'Radius has to be >= 1 with WeightMode.INTERPOLATE!')
raise CustomValueError(auto_balance, 'Radius has to be >= 1 with BalanceWeightMode.INTERPOLATE!')

weight = (n - (frange.start - 1)) / (frange.stop - (frange.start - 1))

Expand All @@ -257,24 +257,24 @@ def _get_cont(mode: WeightMode, frange: range) -> Any:

return weighted_prev + weighted_next

if mode == WeightMode.MEDIAN:
if mode == BalanceWeightMode.MEDIAN:
return np.median(psvalues)

if mode == WeightMode.MEAN:
if mode == BalanceWeightMode.MEAN:
return psvalues.mean()

if mode == WeightMode.MAX:
if mode == BalanceWeightMode.MAX:
return psvalues.max()

if mode == WeightMode.MIN:
if mode == BalanceWeightMode.MIN:
return psvalues.min()

return psvalues[middle_idx]

if override:
frange, cont, override_mode = override

if override_mode == WeightMode.NONE:
if override_mode == BalanceWeightMode.NONE:
return nobalanceclip

if cont is not None:
Expand Down

0 comments on commit 2e2078f

Please sign in to comment.