def test_bin_spatial_counts(self): """ this will test both good and bad points within the region. 1) 2 inside the domain 2) outside the bbox but not in domain 3) completely outside the domain we will check that only 1 event is placed in the grid and ensure its location is correct. """ # create some arbitrary grid nx = 8 ny = 10 dh = 0.1 x_points = numpy.arange(nx) * dh y_points = numpy.arange(ny) * dh origins = list(itertools.product(x_points, y_points)) # grid is missing first and last block origins.pop(0) origins.pop(-1) cart_grid = CartesianGrid2D( [Polygon(bbox) for bbox in compute_vertices(origins, dh)], dh) catalog = MockCatalog() catalog.region = cart_grid test_result = catalog.spatial_counts() # we have tested 2 inside the domain self.assertEqual(numpy.sum(test_result), 2) # we know that (0.05, 0.15) corresponds to index 0 self.assertEqual(test_result[0], 1) # we know that (0.15, 0.05) corresponds too index 9 self.assertEqual(test_result[9], 1)
def test_polygon_mask_all_masked(self): polygons = [Polygon(bbox) for bbox in compute_vertices(self.origins, self.dh)] n_poly = len(polygons) # this should mask every polygon for easy checking test_grid = CartesianGrid2D(polygons, self.dh, mask=numpy.zeros(n_poly)) self.assertEqual(n_poly, test_grid.num_nodes) numpy.testing.assert_array_equal(test_grid.bbox_mask, 1) numpy.testing.assert_array_equal(test_grid.poly_mask, 0)
class TestPolygon(unittest.TestCase): def setUp(self): dh = 1 origin = (0,0) self.polygon = Polygon(compute_vertex(origin, dh)) def test_object_creation(self): self.assertTupleEqual(self.polygon.origin,(0,0)) numpy.testing.assert_allclose(self.polygon.points,[(0,0),(0,1),(1,1),(1,0)]) def test_contains_inside(self): test_point = (0.5, 0.5) self.assertTrue(self.polygon.contains(test_point)) def test_contains_outside(self): test_point = (-0.5, -0.5) self.assertFalse(self.polygon.contains(test_point)) def test_compute_centroid(self): expected = (0.5, 0.5) numpy.testing.assert_almost_equal(self.polygon.centroid(), expected)
def setUp(self): # create some arbitrary grid self.nx = 8 self.ny = 10 self.dh = 0.1 x_points = numpy.arange(self.nx)*self.dh y_points = numpy.arange(self.ny)*self.dh self.origins = list(itertools.product(x_points, y_points)) # grid is missing first and last block self.origins.pop(0) self.origins.pop(-1) self.num_nodes = len(self.origins) # this is kinda ugly, maybe we want to create this in a different way, class method? self.cart_grid = CartesianGrid2D([Polygon(bbox) for bbox in compute_vertices(self.origins, self.dh)], self.dh)
def test_bin_probability(self): # we have tested 2 inside the domain nx = 8 ny = 10 dh = 0.1 x_points = numpy.arange(nx) * dh y_points = numpy.arange(ny) * dh origins = list(itertools.product(x_points, y_points)) # grid is missing first and last block origins.pop(0) origins.pop(-1) cart_grid = CartesianGrid2D( [Polygon(bbox) for bbox in compute_vertices(origins, dh)], dh) catalog = MockCatalog() catalog.region = cart_grid test_result = catalog.spatial_event_probability() self.assertEqual(numpy.sum(test_result), 2) # we know that (0.05, 0.15) corresponds to index 0 from the above test self.assertEqual(test_result[0], 1) self.assertEqual(test_result[9], 1)
def setUp(self): # create some arbitrary grid self.nx = 8 self.ny = 10 self.dh = 1 x_points = numpy.arange(1.5, self.nx) * self.dh y_points = numpy.arange(1.5, self.ny) * self.dh # spatial grid starts at (1.5, 1.5); so the event at (1, 1) should be removed. self.origins = list(itertools.product(x_points, y_points)) self.num_nodes = len(self.origins) self.cart_grid = CartesianGrid2D([ Polygon(bbox) for bbox in compute_vertices(self.origins, self.dh) ], self.dh) # define dummy cat date1 = strptime_to_utc_epoch('2009-01-01 00:00:00.0000') date2 = strptime_to_utc_epoch('2010-01-01 00:00:00.0000') date3 = strptime_to_utc_epoch('2011-01-01 00:00:00.0000') catalog = [(b'1', date1, 1.0, 1.0, 1.0, 1.0), (b'2', date2, 2.0, 2.0, 2.0, 2.0), (b'3', date3, 3.0, 3.0, 3.0, 3.0)] self.test_cat1 = CSEPCatalog(data=catalog)
def setUp(self): dh = 1 origin = (0,0) self.polygon = Polygon(compute_vertex(origin, dh))
def load_ascii(cls, ascii_fname, start_date=None, end_date=None, name=None): """ Reads Forecast file from CSEP1 ascii format. The ascii format from CSEP1 testing centers. The ASCII format does not contain headers. The format is listed here: Lon_0, Lon_1, Lat_0, Lat_1, z_0, z_1, Mag_0, Mag_1, Rate, Flag For the purposes of defining region objects and magnitude bins use the Lat_0 and Lon_0 values along with Mag_0. We can assume that the magnitude bins are regularly spaced to allow us to compute Deltas. The file is row-ordered so that magnitude bins are fastest then followed by space. Args: ascii_fname: file name of csep forecast in .dat format """ # Load data data = numpy.loadtxt(ascii_fname) # this is very ugly, but since unique returns a sorted list, we want to get the index, sort that and then return # from the original array. same for magnitudes below. all_polys = data[:, :4] all_poly_mask = data[:, -1] sorted_idx = numpy.sort(numpy.unique(all_polys, return_index=True, axis=0)[1], kind='stable') unique_poly = all_polys[sorted_idx] # gives the flag for a spatial cell in the order it was presented in the file poly_mask = all_poly_mask[sorted_idx] # create magnitudes bins using Mag_0, ignoring Mag_1 bc they are regular until last bin. we dont want binary search for this all_mws = data[:, -4] sorted_idx = numpy.sort(numpy.unique(all_mws, return_index=True)[1], kind='stable') mws = all_mws[sorted_idx] # csep1 stores the lat lons as min values and not (x,y) tuples bboxes = [ tuple(itertools.product(bbox[:2], bbox[2:])) for bbox in unique_poly ] # the spatial cells are arranged fast in latitude, so this only works for the specific csep1 file format dh = float(unique_poly[0, 3] - unique_poly[0, 2]) # create CarteisanGrid of points region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, mask=poly_mask) # get dims of 2d np.array n_mag_bins = len(mws) n_poly = region.num_nodes # reshape rates into correct 2d format rates = data[:, -2].reshape(n_poly, n_mag_bins) # create / return class if name is None: name = os.path.basename(ascii_fname[:-4]) gds = cls(start_date, end_date, magnitudes=mws, name=name, region=region, data=rates) return gds