Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ features \ weights \ threshold_weight_kernel.py: 100%
7 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-10 00:11 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-10 00:11 -0800
1# =====================================================
2# Imports
3# =====================================================
4from typing import Annotated
6from pydantic import Field
8from mt_metadata.features.weights.taper_monotonic_weight_kernel import (
9 TaperMonotonicWeightKernel,
10)
13# =====================================================
16class ThresholdWeightKernel(TaperMonotonicWeightKernel):
17 """
18 ThresholdWeightKernel
20 A special case of MonotonicWeightKernel where the transition region is a single value,
21 resulting in a hard threshold (step function). This kernel outputs 0 or 1 depending on
22 whether the input is above or below the threshold, according to the threshold_type.
24 Parameters
25 ----------
26 threshold : float
27 The threshold value.
28 threshold_type : str, optional
29 "low cut" (default) or "high cut". Determines which side is downweighted.
30 **kwargs :
31 Additional keyword arguments passed to MonotonicWeightKernel.
32 """
34 threshold_type: Annotated[
35 str,
36 Field(
37 default="low cut",
38 description="Which side of a threshold should be downweighted.",
39 alias=None,
40 json_schema_extra={
41 "units": None,
42 "required": True,
43 "examples": ["low cut"],
44 },
45 ),
46 ]
48 # TODO: Uncomment if needed for testing or future use
49 def _normalize(self, values):
50 """
51 Normalize input values to the [0, 1] interval, supporting infinite bounds for activation and taper kernels.
52 Handles all combinations of finite and infinite transition bounds:
53 - Both bounds finite: linear normalization.
54 - Lower bound -inf, upper bound finite: exponential normalization.
55 - Lower bound finite, upper bound +inf: exponential normalization.
56 - Both bounds infinite: all values map to 0.5.
57 """
59 raise NotImplementedError(
60 "Normalization not implemented for ThresholdWeightKernel."
61 )
62 # lb = float(self.transition_lower_bound)
63 # ub = float(self.transition_upper_bound)
64 # values = np.asarray(values)
65 # # Both bounds finite
66 # if np.isfinite(lb) and np.isfinite(ub):
67 # return np.clip((values - lb) / (ub - lb), 0, 1)
68 # # Lower bound -inf, upper bound finite
69 # elif not np.isfinite(lb) and np.isfinite(ub):
70 # scale = np.std(values) if np.std(values) > 0 else 1.0
71 # x = 1 - np.exp(-(ub - values) / scale)
72 # return np.clip(x, 0, 1)
73 # # Lower bound finite, upper bound +inf
74 # elif np.isfinite(lb) and not np.isfinite(ub):
75 # scale = np.std(values) if np.std(values) > 0 else 1.0
76 # x = 1 - np.exp(-(values - lb) / scale)
77 # return np.clip(x, 0, 1)
78 # # Both bounds infinite
79 # else:
80 # return np.full_like(values, 0.5)