def test_select_within_fault_distance(self): ''' Tests the selection of events within a distance from the fault ''' # Set up catalouge self.catalogue = Catalogue() self.catalogue.data['longitude'] = np.arange(0., 5.5, 0.5) self.catalogue.data['latitude'] = np.arange(0., 5.5, 0.5) self.catalogue.data['depth'] = np.zeros(11, dtype=float) self.catalogue.data['eventID'] = np.arange(0, 11, 1) self.fault_source = mtkSimpleFaultSource('101', 'A simple fault') trace_as_line = line.Line( [point.Point(2.0, 3.0), point.Point(3.0, 2.0)]) self.fault_source.create_geometry(trace_as_line, 30., 0., 30.) selector0 = CatalogueSelector(self.catalogue) # Test 1 - simple case Joyner-Boore distance self.fault_source.select_catalogue(selector0, 40.) np.testing.assert_array_almost_equal( np.array([2., 2.5]), self.fault_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2., 2.5]), self.fault_source.catalogue.data['latitude']) # Test 2 - simple case Rupture distance self.fault_source.catalogue = None self.fault_source.select_catalogue(selector0, 40., 'rupture') np.testing.assert_array_almost_equal( np.array([2.5]), self.fault_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2.5]), self.fault_source.catalogue.data['latitude']) # Test 3 - for vertical fault ensure that Joyner-Boore distance # behaviour is the same as for rupture distance fault1 = mtkSimpleFaultSource('102', 'A vertical fault') fault1.create_geometry(trace_as_line, 90., 0., 30.) self.fault_source.create_geometry(trace_as_line, 90., 0., 30.) # Joyner-Boore self.fault_source.select_catalogue(selector0, 40.) # Rupture fault1.select_catalogue(selector0, 40., 'rupture') np.testing.assert_array_almost_equal( self.fault_source.catalogue.data['longitude'], fault1.catalogue.data['longitude']) np.testing.assert_array_almost_equal( self.fault_source.catalogue.data['latitude'], fault1.catalogue.data['latitude']) # The usual test to ensure error is raised when no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.fault_source.select_catalogue(selector0, 40.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def test_select_catalogue(self): ''' Tests the select_catalogue function - essentially a wrapper to the two selection functions ''' self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100.) self.assertEqual(ver.exception.message, 'No events found in catalogue!') # Create a catalogue self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # To ensure that square function is called - compare against direct instance # First implementation - compare select within distance self.point_source.select_catalogue_within_distance(selector0, 100., 'epicentral') expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 100., 'circle') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Second implementation - compare select within cell expected_catalogue = None self.point_source.select_catalogue_within_cell(selector0, 150.) expected_catalogue = deepcopy(self.point_source.catalogue) self.point_source.catalogue = None # Reset catalogue self.point_source.select_catalogue(selector0, 150., 'square') np.testing.assert_array_equal( self.point_source.catalogue.data['eventID'], expected_catalogue.data['eventID']) # Finally ensure error is raised when input is neither 'circle' nor 'square' with self.assertRaises(ValueError) as ver: self.point_source.select_catalogue(selector0, 100., 'bad input') self.assertEqual(ver.exception.message, 'Unrecognised selection type for point source!')
def setUp(self): cat1 = Catalogue() cat1.end_year = 2000 cat1.start_year = 1900 cat1.data['eventID'] = [1.0, 2.0, 3.0] cat1.data['magnitude'] = np.array([1.0, 2.0, 3.0]) cat2 = Catalogue() cat2.end_year = 1990 cat2.start_year = 1910 cat2.data['eventID'] = [1.0, 2.0, 3.0] cat2.data['magnitude'] = np.array([1.0, 2.0, 3.0]) self.cat1 = cat1 self.cat2 = cat2
def test_select_within_distance(self): ''' Tests the selection of earthquakes within distance of fault ''' # Create fault self.fault_source = mtkComplexFaultSource('101', 'A complex fault') # Test case when input as list of nhlib.geo.line.Line self.fault_source.create_geometry(self.trace_line, mesh_spacing=2.0) self.assertIsInstance(self.fault_source.geometry, ComplexFaultSurface) # Create simple catalogue self.catalogue.data['longitude'] = np.arange(0., 4.1, 0.1) self.catalogue.data['latitude'] = np.arange(0., 4.1, 0.1) self.catalogue.data['depth'] = np.ones(41, dtype=float) self.catalogue.data['eventID'] = np.arange(0, 41, 1) selector0 = CatalogueSelector(self.catalogue) # Test when considering Joyner-Boore distance self.fault_source.select_catalogue(selector0, 50.) np.testing.assert_array_equal( self.fault_source.catalogue.data['eventID'], np.arange(2, 14, 1)) # Test when considering rupture distance self.fault_source.select_catalogue(selector0, 50., 'rupture') np.testing.assert_array_equal( self.fault_source.catalogue.data['eventID'], np.arange(2, 12, 1)) # The usual test to ensure error is raised when no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.fault_source.select_catalogue(selector0, 40.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def test_analysis_Frankel_comparison(self): ''' To test the run_analysis function we compare test results with those from Frankel's fortran implementation, under the same conditions ''' self.grid_limits = [-128., -113.0, 0.2, 30., 43.0, 0.2, 0., 100., 100.] comp_table = np.array([[1933., 4.0], [1900., 5.0], [1850., 6.0], [1850., 7.0]]) config = {'Length_Limit': 3., 'BandWidth': 50., 'increment': 0.1} self.model = SmoothedSeismicity(self.grid_limits, bvalue=0.8) self.catalogue = Catalogue() frankel_catalogue = np.genfromtxt( os.path.join(BASE_PATH, FRANKEL_TEST_CATALOGUE)) self.catalogue.data['magnitude'] = frankel_catalogue[:, 0] self.catalogue.data['longitude'] = frankel_catalogue[:, 1] self.catalogue.data['latitude'] = frankel_catalogue[:, 2] self.catalogue.data['depth'] = frankel_catalogue[:, 3] self.catalogue.data['year'] = frankel_catalogue[:, 4] self.catalogue.end_year = 2006 frankel_results = np.genfromtxt( os.path.join(BASE_PATH, FRANKEL_OUTPUT_FILE)) # Run analysis output_data = self.model.run_analysis( self.catalogue, config, completeness_table=comp_table, smoothing_kernel=IsotropicGaussian()) self.assertTrue( fabs(np.sum(output_data[:, -1]) - np.sum(output_data[:, -2])) < 1.0) self.assertTrue(fabs(np.sum(output_data[:, -1]) - 390.) < 1.0)
def test_select_events_within_cell(self): ''' Tests the selection of events within a cell centred on the point ''' self.point_source = mtkPointSource('101', 'A Point Source') simple_point = Point(4.5, 4.5) self.point_source.create_geometry(simple_point, 0., 30.) self.catalogue = Catalogue() self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['latitude'] = np.arange(4.0, 7.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) selector0 = CatalogueSelector(self.catalogue) # Simple case - 200 km by 200 km cell centred on point self.point_source.select_catalogue_within_cell(selector0, 100.) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([4., 4.5, 5.]), self.point_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.point_source.catalogue.data['depth'])
def test_select_catalogue_rrup(self): """ Tests catalogue selection with Joyner-Boore distance """ self.fault = mtkActiveFault( '001', 'A Fault', self.simple_fault, [(5., 0.5), (7., 0.5)], 0., None, msr_sigma=[(-1.5, 0.15), (0., 0.7), (1.5, 0.15)]) cat1 = Catalogue() cat1.data = {"eventID": ["001", "002", "003", "004"], "longitude": np.array([30.1, 30.1, 30.5, 31.5]), "latitude": np.array([30.0, 30.25, 30.4, 30.5]), "depth": np.array([5.0, 250.0, 10.0, 10.0])} selector = CatalogueSelector(cat1) # Select within 50 km of the fault self.fault.select_catalogue(selector, 50.0, distance_metric="rupture") np.testing.assert_array_almost_equal( self.fault.catalogue.data["longitude"], np.array([30.1, 30.5])) np.testing.assert_array_almost_equal( self.fault.catalogue.data["latitude"], np.array([30.0, 30.4])) np.testing.assert_array_almost_equal( self.fault.catalogue.data["depth"], np.array([5.0, 10.0]))
def test_select_within_magnitude_range(self): ''' Tests the function to select within the magnitude range ''' # Setup function self.catalogue = Catalogue() self.catalogue.data['magnitude'] = np.array([4., 5., 6., 7., 8.]) selector0 = CatalogueSelector(self.catalogue) # Test case 1: No limits specified - all catalogue valid test_cat_1 = selector0.within_magnitude_range() np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], self.catalogue.data['magnitude']) # Test case 2: Lower depth limit specfied only test_cat_1 = selector0.within_magnitude_range(lower_mag=5.5) np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], np.array([6., 7., 8.])) # Test case 3: Upper depth limit specified only test_cat_1 = selector0.within_magnitude_range(upper_mag=5.5) np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], np.array([4., 5.])) # Test case 4: Both depth limits specified test_cat_1 = selector0.within_magnitude_range(upper_mag=7.5, lower_mag=5.5) np.testing.assert_array_almost_equal(test_cat_1.data['magnitude'], np.array([6., 7.]))
def test_select_within_depth_range(self): ''' Tests the function to select within the depth range ''' # Setup function self.catalogue = Catalogue() self.catalogue.data['depth'] = np.array([5., 15., 25., 35., 45.]) selector0 = CatalogueSelector(self.catalogue) # Test case 1: No limits specified - all catalogue valid test_cat_1 = selector0.within_depth_range() np.testing.assert_array_almost_equal(test_cat_1.data['depth'], self.catalogue.data['depth']) # Test case 2: Lower depth limit specfied only test_cat_1 = selector0.within_depth_range(lower_depth=30.) np.testing.assert_array_almost_equal(test_cat_1.data['depth'], np.array([5., 15., 25.])) # Test case 3: Upper depth limit specified only test_cat_1 = selector0.within_depth_range(upper_depth=20.) np.testing.assert_array_almost_equal(test_cat_1.data['depth'], np.array([25., 35., 45.])) # Test case 4: Both depth limits specified test_cat_1 = selector0.within_depth_range(upper_depth=20., lower_depth=40.) np.testing.assert_array_almost_equal(test_cat_1.data['depth'], np.array([25., 35.]))
def select_catalogue(self, valid_id): ''' Method to post-process the catalogue based on the selection options :param numpy.ndarray valid_id: Boolean vector indicating whether each event is selected (True) or not (False) :returns: Catalogue of selected events as instance of hmtk.seismicity.catalogue.Catalogue class ''' if not np.any(valid_id): # No events selected - create clean instance of class output = Catalogue() output.processes = self.catalogue.processes output elif np.all(valid_id): if self.copycat: output = deepcopy(self.catalogue) else: output = self.catalogue else: if self.copycat: output = deepcopy(self.catalogue) else: output = self.catalogue output.purge_catalogue(valid_id) return output
def test_load_to_array(self): """ Tests the creation of a catalogue from an array and a key list """ cat = Catalogue() cat.load_from_array(['year', 'magnitude'], self.data_array) data = cat.load_to_array(['year', 'magnitude']) self.assertTrue(np.allclose(data, self.data_array))
def setUp(self): """ """ self.catalogue = Catalogue() x, y = np.meshgrid(np.arange(5., 50., 10.), np.arange(5.5, 9.0, 1.)) nx, ny = np.shape(x) self.catalogue.data['depth'] = (x.reshape([nx * ny, 1])).flatten() self.catalogue.data['magnitude'] = (y.reshape([nx * ny, 1])).flatten()
def test_load_from_array(self): # Tests the creation of a catalogue from an array and a key list cat = Catalogue() cat.load_from_array(['year', 'magnitude'], self.data_array) np.testing.assert_allclose(cat.data['magnitude'], self.data_array[:, 1]) np.testing.assert_allclose(cat.data['year'], self.data_array[:, 0].astype(int))
def test_hypocentres_as_mesh(self): # Tests the function to render the hypocentres to a # hazardlib.geo.mesh.Mesh object. cat = Catalogue() cat.data['longitude'] = np.array([2., 3.]) cat.data['latitude'] = np.array([2., 3.]) cat.data['depth'] = np.array([2., 3.]) self.assertTrue(isinstance(cat.hypocentres_as_mesh(), Mesh))
def test_load_from_array(self): """ Tests the creation of a catalogue from an array and a key list """ cat = Catalogue() cat.load_from_array(['year', 'magnitude'], self.data_array) self.assertTrue( np.allclose(cat.data['magnitude'], self.data_array[:, 1])) self.assertTrue( np.allclose(cat.data['year'], self.data_array[:, 0].astype(int)))
def test_catalogue_mt_filter(self): # Tests the catalogue magnitude-time filter cat = Catalogue() cat.load_from_array(['year', 'magnitude'], self.data_array) cat.data['eventID'] = np.arange(0, 7) cat.catalogue_mt_filter(self.mt_table) mag = np.array([7.0, 5.5, 5.01, 6.99]) yea = np.array([1920, 1970, 1960, 1960]) np.testing.assert_allclose(cat.data['magnitude'], mag) np.testing.assert_allclose(cat.data['year'], yea)
def test_update_start_end_year(self): # Tests the correct usage of the update start year cat1 = Catalogue() cat1.data['year'] = np.array([1900, 1950, 2000]) # Update start year cat1.update_start_year() self.assertEqual(cat1.start_year, 1900) # Update end-year cat1.update_end_year() self.assertEqual(cat1.end_year, 2000)
def test_get_bounding_box(self): """ Tests the method to return the bounding box of a catalogue """ cat1 = Catalogue() cat1.data["longitude"] = np.array([10.0, 20.0]) cat1.data["latitude"] = np.array([40.0, 50.0]) bbox = cat1.get_bounding_box() self.assertAlmostEqual(bbox[0], 10.0) self.assertAlmostEqual(bbox[1], 20.0) self.assertAlmostEqual(bbox[2], 40.0) self.assertAlmostEqual(bbox[3], 50.0)
def test_catalogue_mt_filter_no_flag(self): """ Tests the catalogue magnitude-time filter """ cat = Catalogue() cat.load_from_array(['year','magnitude'], self.data_array) cat.data['eventID'] = np.arange(0, len(cat.data['magnitude']), 1) cat.catalogue_mt_filter(self.mt_table) mag = np.array([7.0, 5.5, 5.01, 6.99]) yea = np.array([1920, 1970, 1960, 1960]) self.assertTrue(np.allclose(cat.data['magnitude'],mag)) self.assertTrue(np.allclose(cat.data['year'],yea))
def test_select_events_in_source(self): ''' Basic test of method to select events from catalogue in polygon ''' self.area_source = mtkAreaSource('101', 'A Source') simple_polygon = polygon.Polygon([ point.Point(2.0, 3.0), point.Point(3.0, 3.0), point.Point(3.0, 2.0), point.Point(2.0, 2.0) ]) self.catalogue.data['eventID'] = np.arange(0, 7, 1) self.catalogue.data['longitude'] = np.arange(1.0, 4.5, 0.5) self.catalogue.data['latitude'] = np.arange(1.0, 4.5, 0.5) self.catalogue.data['depth'] = np.ones(7, dtype=float) # Simple Case - No buffer selector0 = CatalogueSelector(self.catalogue) self.area_source.create_geometry(simple_polygon, 0., 30.) self.area_source.select_catalogue(selector0, 0.) np.testing.assert_array_almost_equal( np.array([2., 2.5, 3.]), self.area_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([2., 2.5, 3.]), self.area_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.array([1., 1., 1.]), self.area_source.catalogue.data['depth']) # Simple case - dilated by 200 km (selects all) self.area_source.select_catalogue(selector0, 200.) np.testing.assert_array_almost_equal( np.array([1., 1.5, 2., 2.5, 3., 3.5, 4.0]), self.area_source.catalogue.data['longitude']) np.testing.assert_array_almost_equal( np.array([1., 1.5, 2., 2.5, 3., 3.5, 4.0]), self.area_source.catalogue.data['latitude']) np.testing.assert_array_almost_equal( np.ones(7, dtype=float), self.area_source.catalogue.data['depth']) # Bad case - no events in catalogue self.catalogue = Catalogue() selector0 = CatalogueSelector(self.catalogue) with self.assertRaises(ValueError) as ver: self.area_source.select_catalogue(selector0, 0.0) self.assertEqual(ver.exception.message, 'No events found in catalogue!')
def setUp(self): warnings.simplefilter("ignore") self.catalogue = Catalogue() self.fault_source = None self.trace_line = [line.Line([point.Point(1.0, 0.0, 1.0), point.Point(0.0, 1.0, 0.9)])] self.trace_line.append(line.Line([point.Point(1.2, 0.0, 40.), point.Point(1.0, 1.0, 45.), point.Point(0.0, 1.3, 42.)])) self.trace_array = [np.array([[1.0, 0.0, 1.0], [0.0, 1.0, 0.9]])] self.trace_array.append(np.array([[1.2, 0.0, 40.], [1.0, 1.0, 45.], [0.0, 1.3, 42.]]))
def setUp(self): self.catalogue = Catalogue() x, y = np.meshgrid(np.arange(1915., 2010., 10.), np.arange(5.5, 9.0, 1.0)) nx, ny = np.shape(x) self.catalogue.data['magnitude'] = (y.reshape([nx * ny, 1])).flatten() x = (x.reshape([nx * ny, 1])).flatten() self.catalogue.data['year'] = x.astype(int) self.catalogue.data['month'] = np.ones_like(x, dtype=int) self.catalogue.data['day'] = np.ones_like(x, dtype=int) self.catalogue.data['hour'] = np.ones_like(x, dtype=int) self.catalogue.data['minute'] = np.ones_like(x, dtype=int) self.catalogue.data['second'] = np.ones_like(x, dtype=float)
def test_purge_catalogue(self): # Tests the function to purge the catalogue of invalid events cat1 = Catalogue() cat1.data['eventID'] = np.array([100, 101, 102], dtype=int) cat1.data['magnitude'] = np.array([4., 5., 6.], dtype=float) cat1.data['Agency'] = ['XXX', 'YYY', 'ZZZ'] flag_vector = np.array([False, True, False]) cat1.purge_catalogue(flag_vector) np.testing.assert_array_almost_equal(cat1.data['magnitude'], np.array([5.])) np.testing.assert_array_equal(cat1.data['eventID'], np.array([101])) self.assertListEqual(cat1.data['Agency'], ['YYY'])
def test_catalogue_mt_filter_with_flag(self): ''' Tests the catalogue magnitude-time filter when an input boolean vector is also defined ''' cat = Catalogue() cat.load_from_array(['year','magnitude'], self.data_array) cat.data['eventID'] = np.arange(0, len(cat.data['magnitude']), 1) flag = np.array([1, 1, 1, 1, 1, 0, 1], dtype=bool) cat.catalogue_mt_filter(self.mt_table, flag) mag = np.array([7.0, 5.5, 6.99]) yea = np.array([1920, 1970, 1960]) self.assertTrue(np.allclose(cat.data['magnitude'],mag)) self.assertTrue(np.allclose(cat.data['year'],yea))
def test_hypocentres_to_cartesian(self): # Tests the function to render the hypocentres to a cartesian array. # The invoked function nhlib.geo.utils.spherical_to_cartesian is # tested as part of the nhlib suite. The test here is included for # coverage cat = Catalogue() cat.data['longitude'] = np.array([2., 3.]) cat.data['latitude'] = np.array([2., 3.]) cat.data['depth'] = np.array([2., 3.]) expected_data = spherical_to_cartesian(cat.data['longitude'], cat.data['latitude'], cat.data['depth']) model_output = cat.hypocentres_to_cartesian() np.testing.assert_array_almost_equal(expected_data, model_output)
def setUp(self): ''' ''' self.output_filename = os.path.join(os.path.dirname(__file__), 'TEST_OUTPUT_CATALOGUE.csv') print(self.output_filename) self.catalogue = Catalogue() self.catalogue.data['eventID'] = ['1', '2', '3', '4', '5'] self.catalogue.data['magnitude'] = np.array([5.6, 5.4, 4.8, 4.3, 5.]) self.catalogue.data['year'] = np.array([1960, 1965, 1970, 1980, 1990]) self.catalogue.data['ErrorStrike'] = np.array( [np.nan, np.nan, np.nan, np.nan, np.nan]) self.magnitude_table = np.array([[1990., 4.5], [1970., 5.5]]) self.flag = np.array([1, 1, 1, 1, 0], dtype=bool)
def test_get_3d_grid(self): ''' Tests the module to count the events in a 3D grid ''' comp_table = np.array([[1960., 4.0]]) self.catalogue = Catalogue() self.catalogue.data['longitude'] = np.hstack( [np.arange(35., 41.0, 1.0), np.arange(35., 41.0, 1.0)]) self.catalogue.data['latitude'] = np.hstack( [np.arange(40., 46.0, 1.0), np.arange(40., 46.0, 1.0)]) self.catalogue.data['depth'] = np.hstack( [10.0 * np.ones(6), 30.0 * np.ones(6)]) self.catalogue.data['magnitude'] = 4.5 * np.ones(12) self.catalogue.data['year'] = 1990. * np.ones(12) # Case 1 - one depth layer self.grid_limits = Grid.make_from_list( [35.0, 40., 0.5, 40.0, 45., 0.5, 0., 40., 40.]) self.model = SmoothedSeismicity(self.grid_limits, bvalue=1.0) [gx, gy] = np.meshgrid(np.arange(35.25, 40., 0.5), np.arange(40.25, 45., 0.5)) ngp = np.shape(gx)[0] * np.shape(gy)[1] gx = np.reshape(gx, [ngp, 1]) gy = np.reshape(gy, [ngp, 1]) gz = 20. * np.ones(ngp) expected_count = np.zeros(ngp, dtype=float) expected_count[[9, 28, 46, 64, 82, 90]] = 2.0 expected_result = np.column_stack( [gx, np.flipud(gy), gz, expected_count]) self.model.create_3D_grid(self.catalogue, comp_table) np.testing.assert_array_almost_equal(expected_result, self.model.data) # Case 2 - multiple depth layers self.grid_limits = Grid.make_from_list( [35.0, 40., 0.5, 40., 45., 0.5, 0., 40., 20.]) self.model = SmoothedSeismicity(self.grid_limits, bvalue=1.0) expected_result = np.vstack([expected_result, expected_result]) expected_count = np.zeros(200) expected_count[[9, 28, 46, 64, 82, 90, 109, 128, 146, 164, 182, 190]] = 1.0 expected_result[:, -1] = expected_count expected_result[:, 2] = np.hstack( [10. * np.ones(100), 30. * np.ones(100)]) self.model.create_3D_grid(self.catalogue, comp_table) np.testing.assert_array_almost_equal(expected_result, self.model.data)
def test_catalogue_writer_only_mag_table_purging(self): ''' Tests the writer only purging according to the magnitude table ''' # Write to file writer = CsvCatalogueWriter(self.output_filename) writer.write_file(self.catalogue, magnitude_table=self.magnitude_table) parser = CsvCatalogueParser(self.output_filename) cat2 = parser.read_file() expected_catalogue = Catalogue() expected_catalogue.data['eventID'] = ['1', '3', '5'] expected_catalogue.data['magnitude'] = np.array([5.6, 4.8, 5.0]) expected_catalogue.data['year'] = np.array([1960, 1970, 1990]) expected_catalogue.data['ErrorStrike'] = np.array( [np.nan, np.nan, np.nan]) self.check_catalogues_are_equal(expected_catalogue, cat2)
def test_catalogue_writer_only_flag_purging(self): ''' Tests the writer only purging according to the flag ''' # Write to file writer = CsvCatalogueWriter(self.output_filename) writer.write_file(self.catalogue, flag_vector=self.flag) parser = CsvCatalogueParser(self.output_filename) cat2 = parser.read_file() expected_catalogue = Catalogue() expected_catalogue.data['eventID'] = ['1', '2', '3', '4'] expected_catalogue.data['magnitude'] = np.array([5.6, 5.4, 4.8, 4.3]) expected_catalogue.data['year'] = np.array([1960, 1965, 1970, 1980]) expected_catalogue.data['ErrorStrike'] = np.array( [np.nan, np.nan, np.nan, np.nan]) self.check_catalogues_are_equal(expected_catalogue, cat2)
def test_catalogue_writer_both_purging(self): ''' Tests the writer only purging according to the magnitude table and the flag vector ''' # Write to file writer = CsvCatalogueWriter(self.output_filename) writer.write_file(self.catalogue, flag_vector=self.flag, magnitude_table=self.magnitude_table) parser = CsvCatalogueParser(self.output_filename) cat2 = parser.read_file() expected_catalogue = Catalogue() expected_catalogue.data['eventID'] = np.array([1, 3]) expected_catalogue.data['magnitude'] = np.array([5.6, 4.8]) expected_catalogue.data['year'] = np.array([1960, 1970]) expected_catalogue.data['ErrorStrike'] = np.array([np.nan, np.nan]) self.check_catalogues_are_equal(expected_catalogue, cat2)