Skip to content

freqs

Classes:

  • MeanMode

    Enum of different mean for combining clips.

MeanMode

Bases: CustomEnum

Enum of different mean for combining clips.

Methods:

  • __call__

    Applies the selected mean to multiple clips.

  • expr

    Builds a mean expression using a specified mode.

  • single

    Applies the selected mean to one clip, spatially or temporally.

Attributes:

  • ARITHMETIC

    Arithmetic mean.

  • CONTRAHARMONIC

    Contraharmonic mean, implemented as a Lehmer mean withs p=2

  • GEOMETRIC

    Geometric mean, implemented as a Lehmer mean with p=0.5.

  • HARMONIC

    Harmonic mean, implemented as a Lehmer mean with p=0.

  • LEHMER

    Lehmer mean, configurable with parameter p.

  • MAXIMUM

    Maximum value across all clips

  • MEDIAN

    Median value across all clips

  • MINIMUM

    Minimum value across all clips

ARITHMETIC class-attribute instance-attribute

ARITHMETIC = 1

Arithmetic mean.

CONTRAHARMONIC class-attribute instance-attribute

CONTRAHARMONIC = 2

Contraharmonic mean, implemented as a Lehmer mean withs p=2

GEOMETRIC class-attribute instance-attribute

GEOMETRIC = 0.5

Geometric mean, implemented as a Lehmer mean with p=0.5.

HARMONIC class-attribute instance-attribute

HARMONIC = 0

Harmonic mean, implemented as a Lehmer mean with p=0.

LEHMER class-attribute instance-attribute

LEHMER = 3

Lehmer mean, configurable with parameter p.

Note: An odd number for p is preferred as it will avoid negative inputs.

MAXIMUM class-attribute instance-attribute

MAXIMUM = auto()

Maximum value across all clips

MEDIAN class-attribute instance-attribute

MEDIAN = auto()

Median value across all clips

MINIMUM class-attribute instance-attribute

MINIMUM = auto()

Minimum value across all clips

__call__

__call__(
    *_clips: VideoNodeIterableT[VideoNode],
    p: float = 3,
    planes: Planes = None,
    func: FuncExcept | None = None
) -> ConstantFormatVideoNode
__call__(
    *_clips: VideoNodeIterableT[VideoNode],
    planes: Planes = None,
    func: FuncExcept | None = None
) -> ConstantFormatVideoNode
__call__(
    *_clips: VideoNodeIterableT[VideoNode],
    planes: Planes = None,
    func: FuncExcept | None = None,
    **kwargs: Any
) -> ConstantFormatVideoNode

Applies the selected mean to multiple clips.

Parameters:

  • *_clips

    (VideoNodeIterableT[VideoNode], default: () ) –

    Input clips to combine.

  • planes

    (Planes, default: None ) –

    Which planes to process.

  • func

    (FuncExcept | None, default: None ) –

    An optional function to use for error handling.

  • **kwargs

    (Any, default: {} ) –

    Additional keyword arguments for certain modes.

    • p (float): Exponent for LEHMER mode. Defaults to 3.

Raises: CustomValueError: If there is no clip.

Returns:

  • ConstantFormatVideoNode

    A new clip containing the combined frames.

Source code in vsrgtools/freqs.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def __call__(
    self,
    *_clips: VideoNodeIterableT[vs.VideoNode],
    planes: Planes = None,
    func: FuncExcept | None = None,
    **kwargs: Any,
) -> ConstantFormatVideoNode:
    """
    Applies the selected mean to multiple clips.

    Args:
        *_clips: Input clips to combine.
        planes: Which planes to process.
        func: An optional function to use for error handling.
        **kwargs: Additional keyword arguments for certain modes.

               - p (float): Exponent for `LEHMER` mode. Defaults to 3.
    Raises:
        CustomValueError: If there is no clip.

    Returns:
        A new clip containing the combined frames.
    """

    func = func or self.__class__

    clips = flatten_vnodes(_clips)

    assert check_variable_format(clips, func)

    if not clips:
        raise CustomValueError("There is no clip to evaluate.", func)

    if (n_clips := len(clips)) < 2:
        return clips[0]

    return self.expr(n_clips, **kwargs)(clips, planes=planes, func=func)

expr

expr(
    n: (
        SupportsIndex
        | Sequence[SupportsString]
        | HoldsVideoFormat
        | VideoFormatLike
    ),
    **kwargs: Any
) -> ExprList

Builds a mean expression using a specified mode.

Parameters:

Returns:

Source code in vsrgtools/freqs.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def expr(
    self, n: SupportsIndex | Sequence[SupportsString] | HoldsVideoFormat | VideoFormatLike, **kwargs: Any
) -> ExprList:
    """
    Builds a mean expression using a specified mode.

    Args:
        n: Object from which to infer the variables.
        **kwargs: Additional keyword arguments for certain modes.

               - p (float): Exponent for `LEHMER` mode. Defaults to 3.

    Returns:
        Mean expression.
    """
    evars = n if isinstance(n, Sequence) else ExprVars(n)

    if (n_len := len(evars)) < 2:
        return ExprList([evars[0]])

    match self:
        case MeanMode.LEHMER:
            p = kwargs.pop("p", self.value)

            expr = ExprList((f"{v} neutral - D{i}!" for i, v in enumerate(evars)))

            for x in range(2):
                expr.extend([[f"D{i}@ {p - x} pow" for i in range(n_len)], ExprOp.ADD * (n_len - 1), f"P{x}!"])

            expr.append("P1@ 0 = 0 P0@ P1@ / ? neutral +")

            return expr
        case MeanMode.HARMONIC | MeanMode.GEOMETRIC | MeanMode.CONTRAHARMONIC:
            return MeanMode.LEHMER.expr(n, p=self.value)
        case MeanMode.MEDIAN:
            n_op = (n_len - 1) // 2
            mean = "" if n_len % 2 else "+ 2 /"

            return ExprList(
                [
                    f"{' '.join(str(v) for v in evars)}",
                    f"sort{n_len}",
                    f"drop{n_op}",
                    f"{mean}",
                    f"swap{n_op}",
                    f"drop{n_op}",
                ]
            )
        case MeanMode.ARITHMETIC:
            op = ExprOp.ADD
            kwargs.update(expr_suffix=f"{n_len} /")
        case MeanMode.MINIMUM:
            op = ExprOp.MIN
        case MeanMode.MAXIMUM:
            op = ExprOp.MAX

    return combine_expr(evars, op, **kwargs)

single

single(
    clip: VideoNode,
    radius: int | Sequence[int] = 1,
    mode: ConvMode = SQUARE,
    exclude: Iterable[tuple[int, int]] | None = None,
    include: Iterable[tuple[int, int]] | None = None,
    planes: Planes = None,
    func: FuncExcept | None = None,
    **kwargs: Any
) -> ConstantFormatVideoNode

Applies the selected mean to one clip, spatially or temporally.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • radius

    (int | Sequence[int], default: 1 ) –

    The radius per plane (Sequence) or uniform radius (int). Only int is allowed in temporal mode.

  • mode

    (ConvMode, default: SQUARE ) –

    The convolution mode. Defaults to ConvMode.SQUARE.

  • exclude

    (Iterable[tuple[int, int]] | None, default: None ) –

    Optional set of (x, y) coordinates to exclude from the matrix.

  • include

    (Iterable[tuple[int, int]] | None, default: None ) –

    Optional set of (x, y) coordinates to include in the matrix.

  • planes

    (Planes, default: None ) –

    Which planes to process.

  • func

    (FuncExcept | None, default: None ) –

    An optional function to use for error handling.

  • **kwargs

    (Any, default: {} ) –

    Additional keyword arguments for certain modes.

    • p (float): Exponent for LEHMER mode. Defaults to 3.

Raises:

  • CustomValueError

    If a list is passed for radius in temporal mode, which is unsupported.

Returns:

  • ConstantFormatVideoNode

    A new clip with the mean applied.

Source code in vsrgtools/freqs.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
def single(
    self,
    clip: vs.VideoNode,
    radius: int | Sequence[int] = 1,
    mode: ConvMode = ConvMode.SQUARE,
    exclude: Iterable[tuple[int, int]] | None = None,
    include: Iterable[tuple[int, int]] | None = None,
    planes: Planes = None,
    func: FuncExcept | None = None,
    **kwargs: Any,
) -> ConstantFormatVideoNode:
    """
    Applies the selected mean to one clip, spatially or temporally.

    Args:
        clip: Input clip.
        radius: The radius per plane (Sequence) or uniform radius (int). Only int is allowed in temporal mode.
        mode: The convolution mode. Defaults to ConvMode.SQUARE.
        exclude: Optional set of (x, y) coordinates to exclude from the matrix.
        include: Optional set of (x, y) coordinates to include in the matrix.
        planes: Which planes to process.
        func: An optional function to use for error handling.
        **kwargs: Additional keyword arguments for certain modes.

               - p (float): Exponent for `LEHMER` mode. Defaults to 3.

    Raises:
        CustomValueError: If a list is passed for radius in temporal mode, which is unsupported.

    Returns:
        A new clip with the mean applied.
    """
    func = func or f"{self!s}.single"

    assert check_variable_format(clip, func)

    if mode == ConvMode.TEMPORAL:
        if not isinstance(radius, int):
            raise CustomValueError("A list of radius isn't supported for ConvMode.TEMPORAL!", func, radius)

        clips = shift_clip_multi(clip, (-radius, radius))

        (ops,) = ExprOp.matrix(ExprVars(len(clips)), radius, mode, exclude, include)

        return self.expr(ops, **kwargs)(*clips, planes=planes, func=func)

    radius = normalize_seq(radius, clip.format.num_planes)
    expr_plane = list[list[ExprList]]()

    for r in radius:
        expr_passes = list[ExprList]()

        for mat in ExprOp.matrix("x", r, mode, exclude, include):
            expr_passes.append(self.expr(mat, **kwargs))

        expr_plane.append(expr_passes)

    for e in zip(*expr_plane):
        clip = norm_expr(clip, e, planes, func=func)

    return clip