Esempio n. 1
0
    def get_value(self, inject_at_index, ts_id):
        """
        :param inject_at_index: the index to inject the anomaly at
        :param ts_id: the affected time series
        :return: the value to add to the current value at index 'inject_at_index'
        """

        frequency = self.df.index.inferred_freq # the granularity
        before_dt = next_earlier_dt(inject_at_index, frequency, 10)
        after_dt = next_later_dt(inject_at_index, frequency, 10)

        index_min = self.df.index.min()
        index_max = self.df.index.max()

        lower_boundary = max(index_min, before_dt)
        upper_boundary = min(after_dt, index_max)

        injection_series = self.df.loc[lower_boundary:upper_boundary, ts_id]

        # remove indexes which are already an anomaly, we won't consider them for the deviation value as it would change the outcome significantly
        df_anomaly_part = self.df_class.loc[(self.df_class.index.isin(injection_series.index)) & (self.df_class[ts_id] == True), ts_id]
        injection_series = injection_series.drop(index=df_anomaly_part.index)

        # check the remaining length
        if injection_series.index.size == 0:
            logging.debug(msg_injection_all_anomalies())
            return 0

        local_std = injection_series.std(axis=0, skipna=True, level=None, ddof=0)
        return np.random.choice([-1, 1]) * self.get_factor() * local_std
Esempio n. 2
0
    def inject(self, range):
        ts_id = self.get_time_series().id
        inject_at_index = self.next_injection_index(range)
        if inject_at_index is not None:

            upper_boundary = min(next_later_dt(inject_at_index, self.df.index.inferred_freq, self.get_factor()), self.df.index.max())
            trend_indexes = pd.date_range(inject_at_index, upper_boundary, freq=self.df.index.inferred_freq)

            # injection
            self.df_inject.loc[trend_indexes, ts_id] = 0
            self.df_inject_class.loc[trend_indexes, ts_id] = 1
Esempio n. 3
0
    def inject(self, range_index):
        ts_id = self.get_time_series().id
        inject_at_index = self.next_injection_index(range_index)
        if inject_at_index is not None:

            lower_boundary = max(next_earlier_dt(inject_at_index, self.df.index.inferred_freq, 4), self.df.index.min())
            upper_boundary = min(next_later_dt(inject_at_index, self.df.index.inferred_freq, 5), self.df.index.max())

            level_shift_indexes = pd.date_range(lower_boundary, upper_boundary, freq=self.df.index.inferred_freq)
            adjustment_value = self.get_value(inject_at_index, ts_id)
            if adjustment_value != 0:
                self.df_inject.loc[level_shift_indexes, ts_id] += adjustment_value
                self.df_inject_class.loc[level_shift_indexes, ts_id] = 1
Esempio n. 4
0
    def inject(self, range):
        ts_id = self.get_time_series().id
        inject_at_index = self.next_injection_index(range)
        if inject_at_index is not None:

            lower_boundary_before = max(next_earlier_dt(inject_at_index, self.df.index.inferred_freq, 5), self.df.index.min())

            if lower_boundary_before == self.df.index.min():
                lower_boundary = next_later_dt(lower_boundary_before, self.df.index.inferred_freq, 1)
            else:
                lower_boundary = max(next_earlier_dt(inject_at_index, self.df.index.inferred_freq, 4), self.df.index.min())

            upper_boundary = min(next_later_dt(inject_at_index, self.df.index.inferred_freq, 5), self.df.index.max())

            variance_diff_indexes = pd.date_range(lower_boundary_before, upper_boundary, freq=self.df.index.inferred_freq)
            variance_indexes = pd.date_range(lower_boundary, upper_boundary, freq=self.df.index.inferred_freq)

            difference = np.diff(self.df.loc[variance_diff_indexes, ts_id])

            # distortion
            self.df_inject.loc[variance_indexes, ts_id] += (self.get_factor() - 1) * difference
            self.df_inject_class.loc[variance_indexes, ts_id] = 1
Esempio n. 5
0
    def inject(self, range):
        ts_id = self.get_time_series().id
        inject_at_index = self.next_injection_index(range)
        if inject_at_index is not None:

            upper_boundary = min(
                next_later_dt(inject_at_index, self.df.index.inferred_freq, 9),
                self.df.index.max())
            growth_indexes = pd.date_range(inject_at_index,
                                           upper_boundary,
                                           freq=self.df.index.inferred_freq)
            slope = np.random.choice([-1, 1]) * self.get_factor() * np.arange(
                len(growth_indexes))

            # trend
            self.df_inject.loc[growth_indexes, ts_id] += slope
            self.df_inject_class.loc[growth_indexes, ts_id] = 1

            # adjust remaining
            self.df_inject.loc[self.df_inject.index > upper_boundary,
                               ts_id] += slope[-1]