def train(self, dataframe: pd.DataFrame, payload: Union[list, dict], cache: Optional[ModelCache]) -> ModelCache: cache = AnomalyCache.from_json(payload) cache.time_step = utils.find_interval(dataframe) segments = cache.segments if len(segments) > 0: seasonality = cache.seasonality prepared_segments = [] for segment in segments: segment_len = (int(segment.to_timestamp) - int(segment.from_timestamp)) assert segment_len <= seasonality, \ f'seasonality {seasonality} must be greater than segment length {segment_len}' from_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment.from_timestamp, unit='ms')) to_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment.to_timestamp, unit='ms')) segment_data = dataframe[from_index : to_index] prepared_segments.append( AnomalyDetectorSegment( segment.from_timestamp, segment.to_timestamp, segment_data.value.tolist() ) ) cache.set_segments(prepared_segments) return { 'cache': cache.to_json() }
def train(self, dataframe: pd.DataFrame, threshold: dict, cache: Optional[ModelCache]) -> ModelCache: time_step = utils.find_interval(dataframe) return { 'cache': { 'value': threshold['value'], 'condition': threshold['condition'], 'timeStep': time_step } }
def train(self, dataframe: pd.DataFrame, segments: List[Segment], cache: Optional[ModelCache]) -> ModelCache: # TODO: pass only part of dataframe that has segments if self.contains_labeled_segments(segments) == False: msg = f'{self.analytic_unit_id} has no positive labeled segments. Pattern detector needs at least 1 positive labeled segment' logger.error(msg) raise ValueError(msg) self.model.state: models.ModelState = self.model.get_state(cache) new_cache: models.ModelState = self.model.fit(dataframe, segments, self.analytic_unit_id) # time step is optional if len(dataframe) > 1: new_cache.time_step = utils.find_interval(dataframe) new_cache = new_cache.to_json() if len(new_cache) == 0: logging.warning( 'new_cache is empty with data: {}, segments: {}, cache: {}, analytic unit: {}' .format(dataframe, segments, cache, self.analytic_unit_id)) return {'cache': new_cache}