コード例 #1
0
 def test_catalog_creation(self):
     ''' Test the pSysmon Event class.
     '''
     # Create an event with valid time limits.
     catalog = detect.Catalog(name = 'test_name')
     self.assertIsInstance(catalog, detect.Catalog)
     self.assertEqual(catalog.name, 'test_name')
     self.assertIsNone(catalog.db_id)
     self.assertIsNone(catalog.description)
     self.assertIsNone(catalog.agency_uri)
     self.assertIsNone(catalog.author_uri)
     self.assertIsNotNone(catalog.creation_time)
     self.assertListEqual(catalog.detections, [])
コード例 #2
0
    def test_write_to_database_with_detections(self):
        ''' Test the writing to the database of a catalog with detections.
        '''
        creation_time = UTCDateTime()
        catalog = detect.Catalog(name = 'test',
                          description = 'A test description.',
                          agency_uri = 'uot',
                          author_uri = 'tester',
                          creation_time = creation_time)

        # Create a detection and add it to the catalog.
        start_time = '2000-01-01T00:00:00'
        end_time = '2000-01-01T01:00:00'
        creation_time = UTCDateTime()
        det = detect.Detection(start_time = start_time,
                                 end_time = end_time,
                                 creation_time = creation_time)

        catalog.add_detections([det,])


        catalog.write_to_database(self.project)

        db_catalog_orm = self.project.dbTables['detection_catalog']
        db_session = self.project.getDbSession()
        result = db_session.query(db_catalog_orm).all()
        db_session.close()
        self.assertEqual(len(result), 1)
        tmp = result[0]
        self.assertEqual(len(tmp.detections), 1)
        self.assertEqual(tmp.detections[0].catalog_id, catalog.db_id)

        # Add a second event.
        start_time = '2000-01-02T00:00:00'
        end_time = '2000-01-02T01:00:00'
        creation_time = UTCDateTime()
        det = detect.Detection(start_time = start_time,
                      end_time = end_time,
                      creation_time = creation_time)
        catalog.add_detections([det,])
        catalog.write_to_database(self.project)

        db_session = self.project.getDbSession()
        result = db_session.query(db_catalog_orm).all()
        db_session.close()
        self.assertEqual(len(result), 1)
        tmp = result[0]
        self.assertEqual(len(tmp.detections), 2)
        self.assertEqual(tmp.detections[0].catalog_id, catalog.db_id)
        self.assertEqual(tmp.detections[1].catalog_id, catalog.db_id)
コード例 #3
0
    def test_add_detections(self):
        ''' Test the add_events method.
        '''
        catalog = detect.Catalog(name = 'test')

        # Create a detection.
        start_time = '2000-01-01T00:00:00'
        end_time = '2000-01-01T01:00:00'
        creation_time = UTCDateTime()
        det = detect.Detection(start_time = start_time,
                                 end_time = end_time,
                                 creation_time = creation_time)

        catalog.add_detections([det,])

        self.assertEqual(len(catalog.detections), 1)
        self.assertEqual(catalog.detections[0], det)
        self.assertEqual(det.parent, catalog)
コード例 #4
0
    def test_write_to_database(self):
        ''' Test the write_to_database method.
        '''
        creation_time = UTCDateTime()
        catalog = detect.Catalog(name = 'test',
                          description = 'A test description.',
                          agency_uri = 'uot',
                          author_uri = 'tester',
                          creation_time = creation_time)
        catalog.write_to_database(self.project)

        db_catalog_orm = self.project.dbTables['detection_catalog']
        db_session = self.project.getDbSession()
        result = db_session.query(db_catalog_orm).all()
        db_session.close()
        self.assertEqual(len(result), 1)
        tmp = result[0]
        self.assertEqual(tmp.name, 'test')
        self.assertEqual(tmp.description, 'A test description.')
        self.assertEqual(tmp.agency_uri, 'uot')
        self.assertEqual(tmp.author_uri, 'tester')
        self.assertEqual(tmp.creation_time, creation_time.isoformat())
コード例 #5
0
    def test_load_detections(self):
        ''' Test the loading of detections from the database.
        '''
        creation_time = UTCDateTime()
        catalog = detect.Catalog(name = 'test',
                          description = 'A test description.',
                          agency_uri = 'uot',
                          author_uri = 'tester',
                          creation_time = creation_time)

        # Create detections and add it to the catalog.
        det_start_times = ['2000-01-01T00:00:00',
                           '2000-01-01T01:00:00',
                           '2000-01-01T02:00:00']

        for cur_start_time in det_start_times:
            start_time = UTCDateTime(cur_start_time)
            end_time = start_time + 10
            creation_time = UTCDateTime()
            det = detect.Detection(start_time = start_time,
                                     end_time = end_time,
                                     creation_time = creation_time)
            catalog.add_detections([det,])
        catalog.write_to_database(self.project)

        db_catalog_orm = self.project.dbTables['detection_catalog']
        db_session = self.project.getDbSession()
        query = db_session.query(db_catalog_orm).filter(db_catalog_orm.name.in_(['test',]))
        if db_session.query(query.exists()):
            cur_db_catalog = query.first()
            loaded_catalog = detect.Catalog.from_db_catalog(cur_db_catalog, load_detections = False)
        db_session.close()

        self.assertEqual(loaded_catalog.detections, [])
        loaded_catalog.load_detections(self.project)
        self.assertEqual(len(loaded_catalog.detections), len(det_start_times))
コード例 #6
0
    def test_bind(self):
        ''' Test the binding of the detections.
        '''
        # Create the test detections.
        catalog = detect.Catalog(name='test',
                                 description='A test description.',
                                 agency_uri='uot',
                                 author_uri='tester',
                                 creation_time=utcdatetime.UTCDateTime())

        channels = [('GILA', 'HHZ', 'ALPAACT', '00'),
                    ('GUWA', 'HHZ', 'ALPAACT', '00'),
                    ('G_NAWA', 'HHZ', 'ALPAACT', '00'),
                    ('SITA', 'HHZ', 'ALPAACT', '00')]
        events = {}
        events['2016-01-01T00:00:00'] = [0, 1, 2, 3]
        events['2016-01-01T01:00:00'] = [1, 0, 1, 2]
        events['2016-01-01T03:00:00'] = [2, 1, 0, 1]
        events['2016-01-01T04:00:00'] = [3, 2, 1, 0]

        for cur_start, cur_delay_list in events.items():
            for k, cur_delay in enumerate(cur_delay_list):
                cur_scnl = channels[k]
                cur_channel = self.project.geometry_inventory.get_channel(
                    station=cur_scnl[0],
                    name=cur_scnl[1],
                    network=cur_scnl[2],
                    location=cur_scnl[3])
                cur_channel = cur_channel[0]

                # Create a detection and add it to the catalog.
                start_time = utcdatetime.UTCDateTime(cur_start) + cur_delay
                end_time = start_time + 3
                cur_rec_stream = cur_channel.get_stream(start_time=start_time,
                                                        end_time=end_time)
                cur_rec_stream = cur_rec_stream[0]
                det = detect.Detection(start_time=start_time,
                                       end_time=end_time,
                                       creation_time=utcdatetime.UTCDateTime(),
                                       rec_stream_id=cur_rec_stream.id)
                catalog.add_detections([
                    det,
                ])
        catalog.write_to_database(self.project)

        # Get the channels for the detections.
        catalog.assign_channel(self.project.geometry_inventory)

        # Create an event catalog where to store the events.
        event_catalog = ev_core.Catalog(
            name='event_bind_test',
            description='A test description.',
            agency_uri='uot',
            author_uri='tester',
            creation_time=utcdatetime.UTCDateTime())

        # Bind the detections to events.
        binder = binding.EventBinder(event_catalog=event_catalog,
                                     author_uri='tester',
                                     agency_uri='uot')
        binder.compute_search_windows(
            self.project.geometry_inventory.get_station())
        binder.bind(catalog, channels)

        # Save the event catalog to the database.
        event_catalog.write_to_database(self.project)

        # Load the events from the database and check for the correctly
        # associated detections.
        db_catalog_orm = self.project.dbTables['event_catalog']
        db_session = self.project.getDbSession()
        try:
            result = db_session.query(db_catalog_orm).filter(
                db_catalog_orm.name == 'event_bind_test').all()
            loaded_catalog = ev_core.Catalog.from_db_catalog(result[0],
                                                             load_events=True)
            for cur_event_start, cur_delay in events.items():
                cur_event_start = utcdatetime.UTCDateTime(cur_event_start)
                selected_event = loaded_catalog.get_events(
                    start_time=cur_event_start, end_time=cur_event_start + 10)
                self.assertEqual(len(selected_event), 1)
                selected_event = selected_event[0]
                self.assertEqual(len(selected_event.detections), 4)
                detections = sorted(selected_event.detections,
                                    key=op.attrgetter('start_time'))
                cur_det_starts = [
                    cur_event_start + x for x in sorted(cur_delay)
                ]
                self.assertEqual(detections[0].start_time, cur_det_starts[0])
                self.assertEqual(detections[1].start_time, cur_det_starts[1])
                self.assertEqual(detections[2].start_time, cur_det_starts[2])
                self.assertEqual(detections[3].start_time, cur_det_starts[3])

        finally:
            db_session.close()