Skip to content

ts_shape.features.cycles.cycles_extractor ¤

Classes:

  • CycleExtractor

    Class for processing cycles based on different criteria.

CycleExtractor ¤

CycleExtractor(dataframe: DataFrame, start_uuid: str, end_uuid: Optional[str] = None)

Bases: Base

Class for processing cycles based on different criteria.

Methods:

Source code in src/ts_shape/features/cycles/cycles_extractor.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def __init__(self, dataframe: pd.DataFrame, start_uuid: str, end_uuid: Optional[str] = None):
    """Initializes the class with the data and the UUIDs for cycle start and end."""
    super().__init__(dataframe)

    # Validate input types
    if not isinstance(dataframe, pd.DataFrame):
        raise ValueError("dataframe must be a pandas DataFrame")
    if not isinstance(start_uuid, str):
        raise ValueError("start_uuid must be a string")

    self.df = dataframe  # Use the provided DataFrame directly
    self.start_uuid = start_uuid
    self.end_uuid = end_uuid if end_uuid else start_uuid
    logging.info(f"CycleExtractor initialized with start_uuid: {self.start_uuid} and end_uuid: {self.end_uuid}")

get_dataframe ¤

get_dataframe() -> DataFrame

Returns the processed DataFrame.

Source code in src/ts_shape/utils/base.py
34
35
36
def get_dataframe(self) -> pd.DataFrame:
    """Returns the processed DataFrame."""
    return self.dataframe

process_persistent_cycle ¤

process_persistent_cycle() -> DataFrame

Processes cycles where the value of the variable stays true during the cycle.

Source code in src/ts_shape/features/cycles/cycles_extractor.py
25
26
27
28
29
30
31
def process_persistent_cycle(self) -> pd.DataFrame:
    """Processes cycles where the value of the variable stays true during the cycle."""
    # Assuming dataframe is pre-filtered
    cycle_starts = self.df[self.df['value_bool'] == True]
    cycle_ends = self.df[self.df['value_bool'] == False]

    return self._generate_cycle_dataframe(cycle_starts, cycle_ends)

process_separate_start_end_cycle ¤

process_separate_start_end_cycle() -> DataFrame

Processes cycles where different variables indicate cycle start and end.

Source code in src/ts_shape/features/cycles/cycles_extractor.py
41
42
43
44
45
46
47
def process_separate_start_end_cycle(self) -> pd.DataFrame:
    """Processes cycles where different variables indicate cycle start and end."""
    # Assuming dataframe is pre-filtered for both start_uuid and end_uuid
    cycle_starts = self.df[self.df['value_bool'] == True]
    cycle_ends = self.df[self.df['value_bool'] == True]

    return self._generate_cycle_dataframe(cycle_starts, cycle_ends)

process_state_change_cycle ¤

process_state_change_cycle() -> DataFrame

Processes cycles where the start of a new cycle is the end of the previous cycle.

Source code in src/ts_shape/features/cycles/cycles_extractor.py
57
58
59
60
61
62
63
def process_state_change_cycle(self) -> pd.DataFrame:
    """Processes cycles where the start of a new cycle is the end of the previous cycle."""
    # Assuming dataframe is pre-filtered
    cycle_starts = self.df.copy()
    cycle_ends = self.df.shift(-1)

    return self._generate_cycle_dataframe(cycle_starts, cycle_ends)

process_step_sequence ¤

process_step_sequence(start_step: int, end_step: int) -> DataFrame

Processes cycles based on a step sequence, where specific integer values denote cycle start and end.

Source code in src/ts_shape/features/cycles/cycles_extractor.py
49
50
51
52
53
54
55
def process_step_sequence(self, start_step: int, end_step: int) -> pd.DataFrame:
    """Processes cycles based on a step sequence, where specific integer values denote cycle start and end."""
    # Assuming dataframe is pre-filtered
    cycle_starts = self.df[self.df['value_integer'] == start_step]
    cycle_ends = self.df[self.df['value_integer'] == end_step]

    return self._generate_cycle_dataframe(cycle_starts, cycle_ends)

process_trigger_cycle ¤

process_trigger_cycle() -> DataFrame

Processes cycles where the value of the variable goes from true to false during the cycle.

Source code in src/ts_shape/features/cycles/cycles_extractor.py
33
34
35
36
37
38
39
def process_trigger_cycle(self) -> pd.DataFrame:
    """Processes cycles where the value of the variable goes from true to false during the cycle."""
    # Assuming dataframe is pre-filtered
    cycle_starts = self.df[self.df['value_bool'] == True]
    cycle_ends = self.df[self.df['value_bool'] == False].shift(-1)

    return self._generate_cycle_dataframe(cycle_starts, cycle_ends)

process_value_change_cycle ¤

process_value_change_cycle() -> DataFrame

Processes cycles where a change in the value indicates a new cycle.

Source code in src/ts_shape/features/cycles/cycles_extractor.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def process_value_change_cycle(self) -> pd.DataFrame:
    """Processes cycles where a change in the value indicates a new cycle."""
    # Assuming dataframe is pre-filtered

    # Fill NaN or None values with appropriate defaults for diff() to work
    self.df['value_double'] = self.df['value_double'].fillna(0)  # Assuming numeric column
    self.df['value_bool'] = self.df['value_bool'].fillna(False)  # Assuming boolean column
    self.df['value_string'] = self.df['value_string'].fillna('')  # Assuming string column
    self.df['value_integer'] = self.df['value_integer'].fillna(0)  # Assuming integer column

    # Detect changes across the relevant columns using diff()
    self.df['value_change'] = (
        (self.df['value_double'].diff().ne(0)) |
        (self.df['value_bool'].diff().ne(0)) |
        (self.df['value_string'].shift().ne(self.df['value_string'])) |
        (self.df['value_integer'].diff().ne(0))
    )

    # Define cycle starts and ends based on changes
    cycle_starts = self.df[self.df['value_change'] == True]
    cycle_ends = self.df[self.df['value_change'] == True].shift(-1)

    return self._generate_cycle_dataframe(cycle_starts, cycle_ends)