def test_parameters(self):
     event_book = PandasEventBook('test')
     self.assertEqual(0, event_book.time_distance)
     self.assertEqual(0, event_book.count_distance)
     self.assertEqual(0, event_book.events_log_distance)
     event_book = PandasEventBook('test',
                                  time_distance=1,
                                  count_distance=2,
                                  events_log_distance=3)
     self.assertEqual(1, event_book.time_distance)
     self.assertEqual(2, event_book.count_distance)
     self.assertEqual(3, event_book.events_log_distance)
     event_book.set_time_distance(distance=11)
     self.assertEqual(11, event_book.time_distance)
     event_book.set_count_distance(distance=12)
     self.assertEqual(12, event_book.count_distance)
     event_book.set_events_log_distance(distance=13)
     self.assertEqual(13, event_book.events_log_distance)
 def test_events(self):
     event_book = PandasEventBook('test')
     selection = list("ABCD")
     master = pd.DataFrame(columns=selection)
     event_book.add_event(event=master)
     self.assertEqual((0, 4), event_book.current_state.shape)
     # add event
     event = pd.DataFrame({'A': [1, 1, 1], 'E': [1, 1, 1]})
     event_book.add_event(event=event)
     self.assertEqual((3, 5), event_book.current_state.shape)
     event = pd.DataFrame({'A': [1, 0, 1]})
     event_book.increment_event(event=event)
     control = pd.Series([2, 1, 2])
     result = event_book.current_state['A']
     self.assertCountEqual(control, result)
     event_book.decrement_event(event=event)
     control = pd.Series([1, 1, 1])
     result = event_book.current_state['A']
     self.assertCountEqual(control, result)
 def add_event_book(self, book_name: str, reset: bool = None):
     """Returns the event book instance for the given reference name"""
     reset = reset if isinstance(reset, bool) else False
     if not self.is_event_book(book_name=book_name):
         self.__book_catalog.update(
             {book_name: PandasEventBook(book_name=book_name)})
     elif reset:
         self.__book_catalog.get(book_name).reset_state()
     else:
         raise ValueError(
             f"The book name '{book_name}' already exists in the catalog and does not need to be added"
         )
     return
 def test_persist(self):
     state_uri = os.path.join(os.environ['HADRON_PM_PATH'], 'state.pickle')
     events_uri = os.path.join(os.environ['HADRON_PM_PATH'],
                               'events_log.pickle')
     state_connector = ConnectorContract(uri=state_uri,
                                         module_name=self.MODULE,
                                         handler=self.HANDLER)
     events_connector = ConnectorContract(uri=events_uri,
                                          module_name=self.MODULE,
                                          handler=self.HANDLER)
     engine = PandasEventBook('test',
                              state_connector=state_connector,
                              events_log_connector=events_connector)
     self.assertEqual(False, os.path.exists(state_uri))
     self.assertEqual(False, os.path.exists(events_uri))
     for i in range(10):
         engine.increment_event(event=pd.DataFrame(
             data={'A': [i, i * 2, i * 3]}))
     self.assertEqual(0, len(engine._current_events_log.keys()), "loop run")
     self.assertEqual(False, os.path.exists(state_uri))
     self.assertEqual(False, os.path.exists(events_uri))
     engine.set_count_distance(3)
     engine.set_events_log_distance(2)
     # add one
     engine.increment_event(event=pd.DataFrame(data={'A': [1, 1, 1]}))
     self.assertEqual(False, os.path.exists(state_uri))
     self.assertEqual(False, os.path.exists(events_uri))
     self.assertEqual(1, len(engine._current_events_log.keys()), "loop One")
     # add two
     engine.increment_event(event=pd.DataFrame(data={'A': [1, 1, 1]}))
     self.assertEqual(False, os.path.exists(state_uri))
     self.assertEqual(True, os.path.exists(events_uri))
     self.assertEqual(0, len(engine._current_events_log.keys()), "loop Two")
     # add three
     engine.increment_event(event=pd.DataFrame(data={'A': [1, 1, 1]}))
     self.assertEqual(True, os.path.exists(state_uri))
     self.assertEqual(True, os.path.exists(events_uri))
     self.assertEqual(0, len(engine._current_events_log.keys()),
                      "loop Three")
     # add four
     engine.increment_event(event=pd.DataFrame(data={'A': [1, 1, 1]}))
     self.assertEqual(1, len(engine._current_events_log.keys()),
                      "loop Four")
 def test_runs(self):
     """Basic smoke test"""
     PandasEventBook(book_name='test')
 def test_fillna(self):
     eb = PandasEventBook('test')
     event = pd.DataFrame({'A': [1, 1, 1], 'E': [1.1, 1.5, 2.6]})
     eb.add_event(event)
     eb.add_event(pd.DataFrame({'B': ['A', np.nan, 'C', 'D', np.nan]}))
     eb.add_event(
         pd.DataFrame(
             {'C': [True, False, np.nan, np.nan, np.nan, False, True]},
             dtype=bool))
     eb.add_event(
         pd.DataFrame({'D': ['M', 'F', np.nan, np.nan, 'F', 'F']},
                      dtype='category'))
     print(eb.current_state(fillna=True))
    def add_event_book(self,
                       book_name: str,
                       module_name: str = None,
                       event_book_cls: str = None,
                       start_book: bool = None,
                       save_intent: bool = None,
                       intent_order: int = None,
                       replace_intent: bool = None,
                       remove_duplicates: bool = None,
                       **kwargs):
        """ Adds an Event Book to the intent portfolio. Not that if multiple Event Books are referenced from a single
        Event Intent, use book_name to uniquely identify each event_book within the event intent.

        :param book_name: The unique reference name for the Event Book.
        :param module_name: (optional) if passing connectors, The module name where the Event Book class can be found
        :param event_book_cls: (optional) if passing connectors. The name of the Event Book class to instantiate
        :param start_book: (optional) if the event book should be created and returned.
        :param save_intent: (optional) if the intent contract should be saved to the property manager
        :param book_name: if the event has more than one book, this uniquely references the event book in the intent
        :param intent_order: (optional) the order in which each intent should run.
                        If None: default's to -1
                        if -1: added to a level above any current instance of the intent section, level 0 if not found
                        if int: added to the level specified, overwriting any that already exist
        :param replace_intent: (optional) if the intent method exists at the level, or default level
                        True - replaces the current intent method with the new
                        False - leaves it untouched, disregarding the new intent
        :param remove_duplicates: (optional) removes any duplicate intent in any level that is identical
        :return:
        """
        # resolve intent persist options
        self._set_intend_signature(self._intent_builder(
            method=inspect.currentframe().f_code.co_name, params=locals()),
                                   intent_level=book_name,
                                   intent_order=intent_order,
                                   replace_intent=replace_intent,
                                   remove_duplicates=remove_duplicates,
                                   save_intent=save_intent)
        # create the event book
        if isinstance(start_book, bool) and start_book:
            if not isinstance(module_name, str) or not isinstance(
                    event_book_cls, str):
                state_connector = kwargs.pop('state_connector', None)
                if isinstance(state_connector, str) and self._pm.has_connector(
                        connector_name=state_connector):
                    state_connector = self._pm.get_connector_contract(
                        connector_name=state_connector)
                events_log_connector = kwargs.pop('events_log_connector', None)
                if self._pm.has_connector(connector_name=events_log_connector):
                    events_log_connector = self._pm.get_connector_contract(
                        connector_name=events_log_connector)
                time_distance = kwargs.pop('time_distance', 0)
                count_distance = kwargs.pop('count_distance', 0)
                events_log_distance = kwargs.pop('events_log_distance', 0)
                return PandasEventBook(
                    book_name=book_name,
                    time_distance=time_distance,
                    count_distance=count_distance,
                    events_log_distance=events_log_distance,
                    state_connector=state_connector,
                    events_log_connector=events_log_connector)
            else:
                event_book_contract = EventBookContract(
                    book_name=book_name,
                    module_name=module_name,
                    event_book_cls=event_book_cls,
                    **kwargs)
                return EventBookFactory.instantiate(
                    event_book_contract=event_book_contract)
        return