Skip to content

Source range selector

neureptrace.decoding.source_range_selector implements source-only feature selection by source feature range.

The protocol is Category 1 / strict source-only. Feature ranges and the selected feature mask are estimated from source rows only. Evaluation rows are transformed with the fitted mask but are not used to fit it.

neureptrace.decoding.source_range_selector

Source-only range selector.

SourceRangeSelectorResult dataclass

Source code in src/neureptrace/decoding/source_range_selector.py
15
16
17
18
19
20
21
@dataclass(frozen=True, slots=True)
class SourceRangeSelectorResult:
    train_features: np.ndarray
    test_features: np.ndarray
    selected_indices: np.ndarray
    ranges: np.ndarray
    metadata: dict[str, Any] = field(default_factory=dict)

fit_source_range_selector(*, source_features, test_features, min_range=0.0, top_k=None)

Source code in src/neureptrace/decoding/source_range_selector.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def fit_source_range_selector(*, source_features, test_features, min_range: float = 0.0, top_k: int | None = None) -> SourceRangeSelectorResult:
    source = _matrix(source_features, name="source_features")
    test = _matrix(test_features, name="test_features")
    if source.shape[1] != test.shape[1]:
        raise ValueError("feature widths differ")
    min_range_value = _nonnegative_float(min_range, name="min_range")
    top_k_value = None if top_k is None else _positive_int(top_k, name="top_k")
    ranges = _stable_column_ranges(source)
    selected = select_source_range_features(ranges, min_range=min_range_value, top_k=top_k_value)
    return SourceRangeSelectorResult(
        train_features=_compact_float32(source[:, selected]),
        test_features=_compact_float32(test[:, selected]),
        selected_indices=selected.astype(int, copy=False),
        ranges=_compact_float32(ranges),
        metadata={
            "source_range_selector": True,
            "source_range_selector_protocol": SOURCE_RANGE_SELECTOR_PROTOCOL,
            "source_range_selector_protocol_category": SOURCE_RANGE_SELECTOR_CATEGORY,
            "source_range_selector_uses_source_features": True,
            "source_range_selector_uses_test_features_for_fitting": False,
            "source_range_selector_valid_for_strict_source_only": True,
            "source_range_selector_input_dim": int(source.shape[1]),
            "source_range_selector_output_dim": int(selected.shape[0]),
            "source_range_selector_min_range": min_range_value,
            "source_range_selector_top_k": "" if top_k_value is None else top_k_value,
        },
    )

select_source_range_features(ranges, *, min_range=0.0, top_k=None)

Source code in src/neureptrace/decoding/source_range_selector.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def select_source_range_features(ranges, *, min_range: float = 0.0, top_k: int | None = None) -> np.ndarray:
    materialized = _materialize_one_pass_iterables(ranges)
    if _contains_boolean_value(materialized):
        raise ValueError("ranges must contain finite non-negative numeric, non-boolean values.")
    values = np.asarray(materialized, dtype=float)
    if values.ndim != 1:
        raise ValueError("ranges must be one-dimensional.")
    if values.size == 0 or not np.all(np.isfinite(values)) or np.any(values < 0.0):
        raise ValueError("bad ranges")
    threshold = _nonnegative_float(min_range, name="min_range")
    selected = np.flatnonzero(values > threshold)
    if top_k is not None:
        k = _positive_int(top_k, name="top_k")
        ranked = np.argsort(values, kind="mergesort")[-min(k, values.size):]
        selected = np.intersect1d(selected, ranked, assume_unique=False)
    if selected.size == 0:
        selected = np.asarray([int(np.argmax(values))], dtype=int)
    return np.sort(selected).astype(int, copy=False)