• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rafaelpadilla / 3W / 24912462866

24 Apr 2026 09:21PM UTC coverage: 76.362% (-3.1%) from 79.464%
24912462866

push

github

web-flow
Merge pull request #73 from rafaelpadilla/eduardo/refactor_data_operations

Refactor of data operations, trainers and models.

244 of 339 branches covered (71.98%)

Branch coverage included in aggregate %.

1317 of 1706 new or added lines in 50 files covered. (77.2%)

28 existing lines in 5 files now uncovered.

2124 of 2762 relevant lines covered (76.9%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/toolkit/ThreeWToolkit/preprocessing/remap.py
1
from pydantic import Field, PrivateAttr
1✔
2
from ..core.base_dataset import BaseDataset
1✔
3
from ..core.base_preprocessing import BasePreprocessing, BasePreprocessingConfig
1✔
4
from ..core.dataset_outputs import DatasetOutputs
1✔
5

6

7
class RemapClassConfig(BasePreprocessingConfig):
1✔
8
    """Configuration for remapping class labels to integers starting from 0."""
9

10
    class_map: dict | None = Field(
1✔
11
        default=None,
12
        description="Optional mapping from original class labels to new labels. Auto-generated from data if None.",
13
    )
14
    _target: type = PrivateAttr(default_factory=lambda: RemapClass)
1✔
15

16

17
class RemapClass(BasePreprocessing):
1✔
18
    """
19
    Preprocessing step to remap class/label values according to a provided mapping.
20
    If class_map is not provided, it will be generated in fit() by collecting all
21
    unique classes across events. Stores the original mapping for convenience.
22
    """
23

24
    def __init__(self, config: RemapClassConfig):
1✔
25
        """Initialize the RemapClass preprocessing step with the given configuration.
26

27
        Args:
28
            config: RemapClassConfig object containing the configuration for this preprocessing step.
29
        """
30
        self.config: RemapClassConfig = config
1✔
31

32
    def fit(self, data: BaseDataset) -> None:
1✔
33
        """
34
        Collect all unique classes from the label Series if class_map is not provided.
35
        If class_map is provided, this method will check that all classes in the data are present in the class_map.
36

37
        Args:
38
            data: BaseDataset object from which to collect unique class labels if class_map is not provided.
39
        """
40
        if self.config.class_map is not None:
1✔
41
            self.class_map = self.config.class_map
1✔
42

43
            for event in data:
1✔
44
                _ = self.transform(event)  # check that all labels can be mapped
1✔
45
            return
1✔
46

47
        unique_classes: set[int] = set()
1✔
48
        # collect unique classes across all events
49
        for event in data:
1✔
50
            if event.label is not None:
1✔
51
                unique_classes.update(event.label.dropna().unique())
1✔
52
        self.class_map = {c: i for i, c in enumerate(sorted(unique_classes))}
1✔
53

54
    def transform(self, data: DatasetOutputs) -> DatasetOutputs:
1✔
55
        """
56
        Remap class labels according to the class_map.
57

58
        Args:
59
            data: DatasetOutputs object with label field
60

61
        Returns:
62
            DatasetOutputs: Transformed data with remapped labels
63
        """
64
        if self.class_map is None:
1✔
NEW
65
            raise ValueError("RemapClass: class_map is not set. Call fit first.")
×
66

67
        mapped_label = data.label
1✔
68
        if data.label is not None:
1✔
69
            mapped_label = data.label.map(self.class_map)
1✔
70
            if mapped_label.isna().any():
1✔
71
                # If there are any labels that were not in the class_map, they will be NaN after mapping.
72
                raise ValueError("Some labels were not in the class_map.")
1✔
73

74
        return DatasetOutputs(
1✔
75
            signal=data.signal,
76
            label=mapped_label,
77
            metadata=data.metadata.copy(),
78
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc