Skip to content

Commit eb10890

Browse files
authored
fix: Added magnitudes as init attribute for CartesianGrid2D class. Defaults to none. Changed default CSEP regions instantiation to account for this, instead of hardcoding magnitudes. (#273)
tests: added magnitude instantiation in class unit tests.
1 parent 97a1c88 commit eb10890

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

Diff for: csep/core/regions.py

+21-31
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ def california_relm_collection_region(dh_scale=1, magnitudes=None, name="relm-ca
5252

5353
# turn points into polygons and make region object
5454
bboxes = compute_vertices(origins, dh)
55-
relm_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, name=name)
56-
57-
if magnitudes is not None:
58-
relm_region.magnitudes = magnitudes
55+
relm_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, magnitudes=magnitudes, name=name)
5956

6057
return relm_region
6158

@@ -97,10 +94,8 @@ def california_relm_region(dh_scale=1, magnitudes=None, name="relm-california",
9794

9895
# turn points into polygons and make region object
9996
bboxes = compute_vertices(origins, dh)
100-
relm_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, name=name)
101-
102-
if magnitudes is not None:
103-
relm_region.magnitudes = magnitudes
97+
relm_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh,magnitudes=magnitudes,
98+
name=name)
10499

105100
return relm_region
106101

@@ -145,10 +140,8 @@ def italy_csep_region(dh_scale=1, magnitudes=None, name="csep-italy", use_midpoi
145140

146141
# turn points into polygons and make region object
147142
bboxes = compute_vertices(origins, dh)
148-
italy_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, name=name)
149-
150-
if magnitudes is not None:
151-
italy_region.magnitudes = magnitudes
143+
italy_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh,
144+
magnitudes=magnitudes, name=name)
152145

153146
return italy_region
154147

@@ -187,10 +180,8 @@ def italy_csep_collection_region(dh_scale=1, magnitudes=None, name="csep-italy-c
187180

188181
# turn points into polygons and make region object
189182
bboxes = compute_vertices(origins, dh)
190-
relm_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, name=name)
191-
192-
if magnitudes is not None:
193-
relm_region.magnitudes = magnitudes
183+
relm_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, magnitudes=magnitudes,
184+
name=name)
194185

195186
return relm_region
196187

@@ -229,10 +220,8 @@ def nz_csep_region(dh_scale=1, magnitudes=None, name="csep-nz", use_midpoint=Tru
229220

230221
# turn points into polygons and make region object
231222
bboxes = compute_vertices(origins, dh)
232-
nz_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, name=name)
233-
234-
if magnitudes is not None:
235-
nz_region.magnitudes = magnitudes
223+
nz_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, magnitudes=magnitudes,
224+
name=name)
236225

237226
return nz_region
238227

@@ -271,10 +260,8 @@ def nz_csep_collection_region(dh_scale=1, magnitudes=None, name="csep-nz-collect
271260

272261
# turn points into polygons and make region object
273262
bboxes = compute_vertices(origins, dh)
274-
nz_collection_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh, name=name)
275-
276-
if magnitudes is not None:
277-
nz_collection_region.magnitudes = magnitudes
263+
nz_collection_region = CartesianGrid2D([Polygon(bbox) for bbox in bboxes], dh,
264+
magnitudes=magnitudes, name=name)
278265

279266
return nz_collection_region
280267

@@ -294,9 +281,9 @@ def global_region(dh=0.1, name="global", magnitudes=None):
294281
lons = cleaner_range(-180.0, 179.9, dh)
295282
lats = cleaner_range(-90, 89.9, dh)
296283
coords = itertools.product(lons,lats)
297-
region = CartesianGrid2D([Polygon(bbox) for bbox in compute_vertices(coords, dh)], dh, name=name)
298-
if magnitudes is not None:
299-
region.magnitudes = magnitudes
284+
region = CartesianGrid2D([Polygon(bbox) for bbox in compute_vertices(coords, dh)], dh,
285+
magnitudes=magnitudes, name=name)
286+
300287
return region
301288

302289
def magnitude_bins(start_magnitude, end_magnitude, dmw):
@@ -585,7 +572,7 @@ class CartesianGrid2D:
585572
Custom regions can be easily created by using the from_polygon classmethod. This function will accept an arbitrary closed
586573
polygon and return a CartesianGrid class with only points inside the polygon to be valid.
587574
"""
588-
def __init__(self, polygons, dh, name='cartesian2d', mask=None):
575+
def __init__(self, polygons, dh, name='cartesian2d', mask=None, magnitudes=None):
589576
self.polygons = polygons
590577
self.poly_mask = mask
591578
self.dh = dh
@@ -601,6 +588,7 @@ def __init__(self, polygons, dh, name='cartesian2d', mask=None):
601588
# Bounds [origin, top_right]
602589
orgs = self.origins()
603590
self.bounds = numpy.column_stack((orgs, orgs + dh))
591+
self.magnitudes = magnitudes
604592

605593
def __eq__(self, other):
606594
return self.to_dict() == other.to_dict()
@@ -758,9 +746,11 @@ def from_origins(cls, origins, dh=None, magnitudes=None, name=None):
758746
dh1 = numpy.abs(lats[1]-lats[0])
759747
dh = numpy.max([dh1, dh2])
760748

761-
region = CartesianGrid2D([Polygon(bbox) for bbox in compute_vertices(origins, dh)], dh, name=name)
762-
if magnitudes is not None:
763-
region.magnitudes = magnitudes
749+
region = CartesianGrid2D(polygons=[Polygon(bbox) for bbox in compute_vertices(origins, dh)],
750+
dh=dh,
751+
magnitudes=magnitudes,
752+
name=name)
753+
764754
return region
765755

766756
def _build_bitmask_vec(self):

Diff for: tests/test_spatial.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def setUp(self):
5757
self.origins.pop(0)
5858
self.origins.pop(-1)
5959
self.num_nodes = len(self.origins)
60+
self.magnitudes = numpy.array([4, 5])
6061
# this is kinda ugly, maybe we want to create this in a different way, class method?
61-
self.cart_grid = CartesianGrid2D([Polygon(bbox) for bbox in compute_vertices(self.origins, self.dh)], self.dh)
62+
self.cart_grid = CartesianGrid2D([Polygon(bbox) for bbox in compute_vertices(self.origins, self.dh)], self.dh, magnitudes=self.magnitudes)
6263

6364
def test_polygon_mask_all_masked(self):
6465
polygons = [Polygon(bbox) for bbox in compute_vertices(self.origins, self.dh)]

0 commit comments

Comments
 (0)