Ejemplo n.º 1
0
 def effect3(history: History) -> float:
     value = history.get_event(2)
     if not history.get_event(2, delay=1):
         value *= 1.5
     elif not history.get_event(2, delay=2):
         value *= 3
     elif not history.get_event(2, delay=3):
         value *= 6
     return value
    def promo_delay(history: History) -> float:
        value = 0
        if history.get_event(4):
            value = Uniform(40, 80).generate()
        elif history.get_event(4, delay=1):
            value = Uniform(30, 50).generate()
        elif history.get_event(4, delay=2):
            value = Uniform(0, 20).generate()

        return value
Ejemplo n.º 3
0
    def server_down(h: History) -> float:
        try:
            load_log = h.get_range(position=1, time_range='1m', null_value=0)
            high_load = max(load_log) > 90 if len(load_log) != 0 else None

            error_log = h.get_range(position=2, time_range='2m')
            critical = any(error_log == 'critical')
            warning = any(error_log == 'warning')

            return 'server down' if critical or (warning
                                                 and high_load) else None
        except Exception as err:
            print('error:', err)
 def function(h: History):
     # Place value in a [-10; 10] range, from a [0, 9] input range.
     value = (h.e(4) - 0) / (9 - 0) * 20 - 10
     # Apply a sigmoid probability distribution.
     probability = 1 / (1 + math.exp(-value))
     weights = (1 - probability, probability)
     return Discrete(weights=weights).generate()
Ejemplo n.º 5
0
    def test_simple_time_range(self):
        events = [
            Time('2020-02-10', step='5m'),
            Linear(start=0, step=1),
            Linear(start=0, step=-1),
        ]
        history = History()
        history.start(events_count=len(events))
        for _ in range(10):
            for position, event in enumerate(events):
                event.position = position
                history.set_event(event, event.generate())

        self.assertEqual([10, 9, 8], list(history.get_range(1, '15m')))
        self.assertEqual([-10, -9, -8], list(history.get_range(2, '15m')))
        self.assertEqual([10, 9, 8], list(history.get_range(1, '720s')))
Ejemplo n.º 6
0
        def effect_function(history: History) -> float:
            value = history.get_event()
            if history.get_event(201, delay=200):
                value *= 1.5
            elif history.get_event(position=301, delay=300, null_value=None):
                value *= 3
            elif history.get_event(delay=500):
                value *= 3
            elif history.get_event(delay=600, position=601):
                value *= 3
            value = history.get_event(401) + 4

            return value
 def __init__(self, sequential: bool = False):
     self.__events = []
     self.__sequential = sequential
     self.history = History()
     self.__add_event(Time(shadow=True))
class Generator:
    EMPTY_VALUE = None

    def __init__(self, sequential: bool = False):
        self.__events = []
        self.__sequential = sequential
        self.history = History()
        self.__add_event(Time(shadow=True))

    def generate(self, samples: int = 3) -> pd.DataFrame:
        data = pd.DataFrame()
        self.history.start(events_count=len(self.__events))
        default_sample = {
            event.label: self.EMPTY_VALUE
            for event in self.get_events()
        }

        events = self.get_events()
        position = 1

        while len(data) < samples:
            sample = default_sample.copy()

            if self.__sequential:
                # Process only one event at a time, but always process time.
                events = [self.get_events()[0], self.get_events()[position]]
                position = position % (len(self.__events) - 1) + 1

            for event in events:
                # Allow event to be executed regarding its probability,
                if not event.draw():
                    continue

                value = event.generate()
                if value is not None:
                    self.history.set_event(event, value)
                    sample[event.label] = value

            # Only add sample if it is not empty.
            if not all(value is None for value in list(sample.values())[1:]):
                data = data.append(sample, ignore_index=True)

        # Convert columns to specified data type.
        for event in events:
            data[event.label] = data[event.label].astype(event.data_type)

        # Remove shadow causes from generator.
        columns = [event.label for event in events if event.shadow]
        data = data.drop(columns, axis=1)

        # Remove line breaks in column names.
        data.columns = data.columns.str.replace("\n", " ")

        return data

    def get_events(self, include_shadow: bool = True) -> List[EventInterface]:
        return self.__events if include_shadow else [
            event for event in self.__events if not event.shadow
        ]

    def __add_event(self, event: EventInterface) -> Generator:
        event.setup(self.__events)
        self.__events.append(event)
        return self

    def add_function(self, event_function: Callable[[History], float],
                     **kwargs) -> Generator:
        return self.__add_event(
            Function(event_function, self.history, **kwargs))

    def add_uniform(self, min: int = 0, max: int = 10, **kwargs) -> Generator:
        return self.__add_event(Uniform(min, max, **kwargs))

    def add_gaussian(self,
                     mu: float = 0,
                     sigma: float = 1,
                     **kwargs) -> Generator:
        return self.__add_event(Gaussian(mu, sigma, **kwargs))

    def add_discrete(self,
                     samples: int = 2,
                     weights: tuple = None,
                     data_type=int,
                     **kwargs) -> Generator:
        return self.__add_event(
            Discrete(samples, weights, data_type=data_type, **kwargs))

    def add_linear(self,
                   start: float = 0,
                   step: float = 1,
                   **kwargs) -> Generator:
        return self.__add_event(Linear(start, step, **kwargs))

    def add_constant(self, value: float = 1, **kwargs) -> Generator:
        return self.__add_event(Constant(value, **kwargs))

    def add_categorical(self,
                        values: tuple = (0, 1),
                        weights: tuple = None,
                        data_type=str,
                        **kwargs) -> Generator:
        return self.__add_event(
            Categorical(values, weights, data_type=data_type, **kwargs))

    def set_time(self,
                 start_date: str = None,
                 step: str = '1m',
                 precision: str = None,
                 **kwargs) -> Generator:
        event = Time(start_date, step, precision, **kwargs)
        event.setup(events=[])
        self.__events[0] = event
        return self

    def get_time(self) -> Time:
        return self.__events[0]

    def build_relations(self, include_shadow=True) -> List[Relation]:
        return RelationFactory.build_relations(self.get_events(),
                                               include_shadow)

    def plot_relations(self,
                       fig_size=(4, 3),
                       node_size=20,
                       include_shadow=True,
                       dpi=None):
        RelationPlot.show(self.get_events(),
                          self.build_relations(include_shadow),
                          fig_size,
                          node_size * 100,
                          dpi=dpi)
Ejemplo n.º 9
0
 def function2(h: History) -> float:
     return h.get_timestamp()
 def year_week(history: History) -> float:
     week = history.get_datetime().isocalendar()[1]
     ratio = week - history.get_datetime(-1).isocalendar()[1]
     return 1 + ratio / 5
    def pathology(history: History) -> float:
        gene1 = history.get_event(1) > 0.8
        gene2 = history.get_event(2) > 0.7
        gene5 = history.get_event(5) > 0.6

        return gene1 and gene2 or gene5
Ejemplo n.º 12
0
 def effect_function1(h: History) -> float:
     return h.get_event()
Ejemplo n.º 13
0
 def func3(history: History) -> float:
     return history.get_timestamp(delay=2)
Ejemplo n.º 14
0
 def func2(history: History) -> float:
     return history.get_datetime(1).isocalendar()[1]
Ejemplo n.º 15
0
 def func1(history: History) -> float:
     return history.get_datetime().weekday()
 def week_day(history: History) -> float:
     day = history.get_datetime().weekday()
     ratios = [20, 20, 15, 5, 0, -20, -30]
     return 1 + ratios[day] / 100
 def sales(history: History) -> float:
     return history.get_event(1) * history.get_event(2)
Ejemplo n.º 18
0
 def effect1(history: History) -> float:
     day = history.get_datetime().weekday()
     # [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
     ratios = [20, 20, 15, 10, 0, -30, -50]
     return history.get_event(1) * (1 + ratios[day] / 100)
Ejemplo n.º 19
0
 def effect2(history: History) -> float:
     week = history.get_datetime().isocalendar()[1]
     ratio = week / 52
     return history.get_event(1) * (1 + ratio / 20)
Ejemplo n.º 20
0
 def effect_function(history: History) -> float:
     return history.get_event()