1
1
import copy
2
2
import unittest
3
3
import os
4
+ import itertools
4
5
5
6
import numpy
6
7
7
8
import csep
8
9
from csep .core import regions , forecasts
9
10
from csep .utils .time_utils import strptime_to_utc_epoch , strptime_to_utc_datetime
10
11
from csep .core .catalogs import CSEPCatalog
12
+ from csep .core .regions import CartesianGrid2D , Polygon , compute_vertices
11
13
12
14
def comcat_path ():
13
15
root_dir = os .path .dirname (os .path .abspath (__file__ ))
@@ -18,6 +20,18 @@ def comcat_path():
18
20
class CatalogFiltering (unittest .TestCase ):
19
21
def setUp (self ):
20
22
23
+ # create some arbitrary grid
24
+ self .nx = 8
25
+ self .ny = 10
26
+ self .dh = 1
27
+ x_points = numpy .arange (1.5 , self .nx ) * self .dh
28
+ y_points = numpy .arange (1.5 , self .ny ) * self .dh
29
+
30
+ # spatial grid starts at (1.5, 1.5); so the event at (1, 1) should be removed.
31
+ self .origins = list (itertools .product (x_points , y_points ))
32
+ self .num_nodes = len (self .origins )
33
+ self .cart_grid = CartesianGrid2D ([Polygon (bbox ) for bbox in compute_vertices (self .origins , self .dh )], self .dh )
34
+
21
35
# define dummy cat
22
36
date1 = strptime_to_utc_epoch ('2009-01-01 00:00:00.0000' )
23
37
date2 = strptime_to_utc_epoch ('2010-01-01 00:00:00.0000' )
@@ -72,6 +86,21 @@ def test_filter_with_datetime(self):
72
86
filtered_test_cat = test_cat .filter (filters , in_place = False )
73
87
numpy .testing .assert_equal (numpy .array ([b'1' , b'2' ], dtype = 'S256' ).T , filtered_test_cat .get_event_ids ())
74
88
89
+ def test_filter_spatial (self ):
90
+
91
+ test_cat = copy .deepcopy (self .test_cat1 )
92
+ filtered_test_cat = test_cat .filter_spatial (region = self .cart_grid )
93
+ numpy .testing .assert_equal (numpy .array ([b'2' , b'3' ], dtype = 'S256' ).T , filtered_test_cat .get_event_ids ())
94
+
95
+
96
+ def test_filter_spatial_in_place_return (self ):
97
+ test_cat = copy .deepcopy (self .test_cat1 )
98
+ filtered_test_cat = test_cat .filter_spatial (region = self .cart_grid , in_place = False )
99
+ numpy .testing .assert_array_equal (filtered_test_cat .region .midpoints (), test_cat .region .midpoints ())
100
+ numpy .testing .assert_array_equal (filtered_test_cat .region .origins (), test_cat .region .origins ())
101
+ numpy .testing .assert_array_equal (filtered_test_cat .region .bbox_mask , test_cat .region .bbox_mask )
102
+ numpy .testing .assert_array_equal (filtered_test_cat .region .idx_map , test_cat .region .idx_map )
103
+
75
104
def test_catalog_binning_and_filtering_acceptance (self ):
76
105
# create space-magnitude region
77
106
region = regions .create_space_magnitude_region (
@@ -93,7 +122,7 @@ def test_catalog_binning_and_filtering_acceptance(self):
93
122
# catalog filtered cumulative
94
123
c = comcat .filter ([f'magnitude >= { m_min } ' ], in_place = False )
95
124
# catalog filtered incrementally
96
- c_int = comcat .filter ([f'magnitude >= { m_min } ' , f'magnitude < { m_min + 0.09999999 } ' ], in_place = False )
125
+ c_int = comcat .filter ([f'magnitude >= { m_min } ' , f'magnitude < { m_min + 0.1 } ' ], in_place = False )
97
126
# sum from overall data set
98
127
gs = d .data [:, idm :].sum ()
99
128
# incremental counts
0 commit comments