def test_exceptions(): ddir = os.path.join("data", "testdata", "geonet") homedir = pkg_resources.resource_filename("gmprocess", ddir) datafile_v2 = os.path.join(homedir, "us1000778i", "20161113_110259_WTMC_20.V2A") stream_v2 = read_geonet(datafile_v2)[0] # Check for origin Error passed = True try: m = MetricsController("pga", "radial_transverse", stream_v2, config=config) except PGMException as e: passed = False assert passed == False # -------- Horizontal Channel Errors ----------- # Check for horizontal passthrough gm st2 = stream_v2.select(component="[N1]") st3 = stream_v2.select(component="Z") st1 = StationStream([st2[0], st3[0]]) passed = True m = MetricsController("pga", "geometric_mean", st1, config=config) pgm = m.pgms result = pgm["Result"].tolist()[0] assert np.isnan(result) # Check for horizontal passthrough rotd50 m = MetricsController("pga", "rotd50", st1, config=config) pgm = m.pgms result = pgm["Result"].tolist()[0] assert np.isnan(result) # Check for horizontal passthrough gmrotd50 m = MetricsController("pga", "gmrotd50", st1, config=config) pgm = m.pgms result = pgm["Result"].tolist()[0] assert np.isnan(result) # No horizontal channels try: m = MetricsController("sa3.0", "channels", st3, config=config) except PGMException as e: passed = False assert passed == False
def test_exceptions(): ddir = os.path.join('data', 'testdata', 'geonet') homedir = pkg_resources.resource_filename('gmprocess', ddir) datafile_v2 = os.path.join(homedir, 'us1000778i', '20161113_110259_WTMC_20.V2A') stream_v2 = read_geonet(datafile_v2)[0] # Check for origin Error passed = True try: m = MetricsController('pga', 'radial_transverse', stream_v2, config=config) except PGMException as e: passed = False assert passed == False # -------- Horizontal Channel Errors ----------- # Check for horizontal passthrough gm st2 = stream_v2.select(component='[N1]') st3 = stream_v2.select(component='Z') st1 = StationStream([st2[0], st3[0]]) passed = True m = MetricsController('pga', 'geometric_mean', st1, config=config) pgm = m.pgms result = pgm['Result'].tolist()[0] assert np.isnan(result) # Check for horizontal passthrough rotd50 m = MetricsController('pga', 'rotd50', st1, config=config) pgm = m.pgms result = pgm['Result'].tolist()[0] assert np.isnan(result) # Check for horizontal passthrough gmrotd50 m = MetricsController('pga', 'gmrotd50', st1, config=config) pgm = m.pgms result = pgm['Result'].tolist()[0] assert np.isnan(result) # No horizontal channels try: m = MetricsController('sa3.0', 'channels', st3, config=config) except PGMException as e: passed = False assert passed == False
def test_end_to_end(): datafiles, _ = read_data_dir('geonet', 'us1000778i', '20161113_110259_WTMC_20.V2A') datafile = datafiles[0] target_imcs = np.sort( np.asarray([ 'GREATER_OF_TWO_HORIZONTALS', 'H1', 'H2', 'Z', 'ROTD50.0', 'ROTD100.0' ])) target_imts = np.sort(np.asarray(['SA(1.0)', 'PGA', 'PGV'])) stream = read_geonet(datafile)[0] input_imcs = [ 'greater_of_two_horizontals', 'channels', 'rotd50', 'rotd100', 'invalid' ] input_imts = ['sa1.0', 'PGA', 'pgv', 'invalid'] m = MetricsController(input_imts, input_imcs, stream) test_pgms = [('PGV', 'ROTD(100.0)', 114.24894584734818), ('PGV', 'ROTD(50.0)', 81.55436750525355), ('PGV', 'Z', 37.47740000000001), ('PGV', 'H1', 100.81460000000004), ('PGV', 'H2', 68.4354), ('PGV', 'GREATER_OF_TWO_HORIZONTALS', 100.81460000000004), ('PGA', 'ROTD(100.0)', 100.73875535385548), ('PGA', 'ROTD(50.0)', 91.40178541935455), ('PGA', 'Z', 183.7722361866693), ('PGA', 'H1', 99.24999872535474), ('PGA', 'H2', 81.23467239067368), ('PGA', 'GREATER_OF_TWO_HORIZONTALS', 99.24999872535474), ('SA(1.0)', 'ROTD(100.0)', 146.9023350124098), ('SA(1.0)', 'ROTD(50.0)', 106.03202302692158), ('SA(1.0)', 'Z', 27.74118995438756), ('SA(1.0)', 'H1', 136.25041187387063), ('SA(1.0)', 'H2', 84.69296738413021), ('SA(1.0)', 'GREATER_OF_TWO_HORIZONTALS', 136.25041187387063)] pgms = m.pgms assert len(pgms['IMT'].tolist()) == len(test_pgms) for target in test_pgms: target_imt = target[0] target_imc = target[1] value = target[2] sub_imt = pgms.loc[pgms['IMT'] == target_imt] df = sub_imt.loc[sub_imt['IMC'] == target_imc] assert len(df['IMT'].tolist()) == 1 np.testing.assert_array_almost_equal(df['Result'].tolist()[0], value, decimal=10)
def from_config(cls, stream, config=None, event=None): """ Args: stream (obspy.core.stream.Stream): Strong motion timeseries for one station. event (ScalarEvent): Object containing latitude, longitude, depth, and magnitude. config (dictionary): Configuration dictionary. Note: Assumes a processed stream with units of gal (1 cm/s^2). No processing is done by this class. """ if config is None: config = get_config() station = cls() damping = config['metrics']['sa']['damping'] smoothing = config['metrics']['fas']['smoothing'] bandwidth = config['metrics']['fas']['bandwidth'] station._damping = damping station._smoothing = smoothing station._bandwidth = bandwidth station._stream = stream station.event = event station.set_metadata() metrics = MetricsController.from_config(stream, config=config, event=event) pgms = metrics.pgms if pgms is None: station._components = metrics.imcs station._imts = metrics.imts station.pgms = pd.DataFrame.from_dict({ 'IMT': [], 'IMC': [], 'Result': [] }) else: station._components = set(pgms['IMC'].tolist()) station._imts = set(pgms['IMT'].tolist()) station.pgms = pgms station._summary = station.get_summary() return station
def test_end_to_end(): datafiles, _ = read_data_dir("geonet", "us1000778i", "20161113_110259_WTMC_20.V2A") datafile = datafiles[0] stream = read_geonet(datafile)[0] input_imcs = [ "greater_of_two_horizontals", "channels", "rotd50", "rotd100", "invalid", ] input_imts = ["sa1.0", "PGA", "pgv", "invalid"] m = MetricsController(input_imts, input_imcs, stream, config=config) test_pgms = [ ("PGV", "ROTD(100.0)", 114.24894584734818), ("PGV", "ROTD(50.0)", 81.55436750525355), ("PGV", "Z", 37.47740000000001), ("PGV", "H1", 100.81460000000004), ("PGV", "H2", 68.4354), ("PGV", "GREATER_OF_TWO_HORIZONTALS", 100.81460000000004), ("PGA", "ROTD(100.0)", 100.73875535385548), ("PGA", "ROTD(50.0)", 91.40178541935455), ("PGA", "Z", 183.7722361866693), ("PGA", "H1", 99.24999872535474), ("PGA", "H2", 81.23467239067368), ("PGA", "GREATER_OF_TWO_HORIZONTALS", 99.24999872535474), ("SA(1.000)", "ROTD(100.0)", 146.9023350124098), ("SA(1.000)", "ROTD(50.0)", 106.03202302692158), ("SA(1.000)", "Z", 27.74118995438756), ("SA(1.000)", "H1", 136.25041187387063), ("SA(1.000)", "H2", 84.69296738413021), ("SA(1.000)", "GREATER_OF_TWO_HORIZONTALS", 136.25041187387063), ] pgms = m.pgms assert len(pgms) == len(test_pgms) for target in test_pgms: target_imt = target[0] target_imc = target[1] value = target[2] df = pgms.loc[target_imt, target_imc] assert len(df) == 1 np.testing.assert_array_almost_equal(df["Result"], value, decimal=10)
def from_stream(cls, stream, components, imts, event=None, damping=None, smoothing=None, bandwidth=None, config=None): """ Args: stream (obspy.core.stream.Stream): Strong motion timeseries for one station. components (list): List of requested components (str). imts (list): List of requested imts (str). event (ScalarEvent): Origin/magnitude for the event containing time, latitude, longitude, depth, and magnitude. damping (float): Damping of oscillator. Default is None. smoothing (float): Smoothing method. Default is None. bandwidth (float): Bandwidth of smoothing. Default is None. config (dictionary): Configuration dictionary. Note: Assumes a processed stream with units of gal (1 cm/s^2). No processing is done by this class. """ if config is None: config = get_config() station = cls() imts = np.sort(imts) components = np.sort(components) if damping is None: damping = config['metrics']['sa']['damping'] if smoothing is None: smoothing = config['metrics']['fas']['smoothing'] if bandwidth is None: bandwidth = config['metrics']['fas']['bandwidth'] station._damping = damping station._smoothing = smoothing station._bandwidth = bandwidth station._stream = stream station.event = event station.set_metadata() metrics = MetricsController(imts, components, stream, bandwidth=bandwidth, damping=damping, event=event, smooth_type=smoothing) pgms = metrics.pgms if pgms is None: station._components = metrics.imcs station._imts = metrics.imts station.pgms = pd.DataFrame.from_dict({ 'IMT': [], 'IMC': [], 'Result': [] }) else: station._components = set(pgms['IMC'].tolist()) station._imts = set(pgms['IMT'].tolist()) station.pgms = pgms station._summary = station.get_summary() return station
def test_controller(): datafiles, event = read_data_dir('geonet', 'us1000778i', '20161113_110259_WTMC_20.V2A') datafile = datafiles[0] input_imts = [ 'pgv', 'pga', 'sa2', 'sa1.0', 'sa0.3', 'fas2', 'fas1.0', 'fas0.3', 'arias', 'invalid' ] input_imcs = [ 'rotd50', 'rotd100.0', 'gmrotd50', 'gmrotd100.0', 'radial_transverse', 'geometric_mean', 'arithmetic_mean', 'channels', 'greater_of_two_horizontals', 'invalid', 'quadratic_mean' ] stream_v2 = read_geonet(datafile)[0] # Testing for acceleration -------------------------- m1 = MetricsController(input_imts, input_imcs, stream_v2, event=event) pgms = m1.pgms # testing for pga, pgv, sa target_imcs = [ 'ROTD(50.0)', 'ROTD(100.0)', 'GMROTD(50.0)', 'GMROTD(100.0)', 'HNR', 'HNT', 'GEOMETRIC_MEAN', 'ARITHMETIC_MEAN', 'H1', 'H2', 'Z', 'GREATER_OF_TWO_HORIZONTALS', 'QUADRATIC_MEAN' ] for col in ['PGA', 'PGV', 'SA(1.000)', 'SA(2.000)', 'SA(0.300)']: imt = pgms.loc[pgms['IMT'] == col] imcs = imt['IMC'].tolist() assert len(imcs) == len(target_imcs) np.testing.assert_array_equal(np.sort(imcs), np.sort(target_imcs)) # testing for fas for col in ['FAS(1.000)', 'FAS(2.000)', 'FAS(0.300)']: imt = pgms.loc[pgms['IMT'] == col] imcs = imt['IMC'].tolist() assert len(imcs) == 3 np.testing.assert_array_equal( np.sort(imcs), ['ARITHMETIC_MEAN', 'GEOMETRIC_MEAN', 'QUADRATIC_MEAN']) # testing for arias imt = pgms.loc[pgms['IMT'] == 'ARIAS'] imcs = imt['IMC'].tolist() assert len(imcs) == 1 np.testing.assert_array_equal(np.sort(imcs), ['ARITHMETIC_MEAN']) _validate_steps(m1.step_sets, 'acc') # Testing for Velocity -------------------------- for trace in stream_v2: trace.stats.standard.units = 'vel' m = MetricsController(input_imts, input_imcs, stream_v2, event=event) pgms = m.pgms # testing for pga, pgv, sa target_imcs = [ 'ROTD(50.0)', 'ROTD(100.0)', 'GMROTD(50.0)', 'GMROTD(100.0)', 'HNR', 'HNT', 'GEOMETRIC_MEAN', 'ARITHMETIC_MEAN', 'QUADRATIC_MEAN', 'H1', 'H2', 'Z', 'GREATER_OF_TWO_HORIZONTALS' ] for col in ['PGA', 'PGV', 'SA(1.000)', 'SA(2.000)', 'SA(0.300)']: imt = pgms.loc[pgms['IMT'] == col] imcs = imt['IMC'].tolist() assert len(imcs) == len(target_imcs) np.testing.assert_array_equal(np.sort(imcs), np.sort(target_imcs)) # testing for fas for col in ['FAS(1.000)', 'FAS(2.000)', 'FAS(0.300)']: imt = pgms.loc[pgms['IMT'] == col] imcs = imt['IMC'].tolist() assert len(imcs) == 3 np.testing.assert_array_equal( np.sort(imcs), ['ARITHMETIC_MEAN', 'GEOMETRIC_MEAN', 'QUADRATIC_MEAN']) # testing for arias imt = pgms.loc[pgms['IMT'] == 'ARIAS'] imcs = imt['IMC'].tolist() assert len(imcs) == 1 np.testing.assert_array_equal(np.sort(imcs), ['ARITHMETIC_MEAN']) _validate_steps(m.step_sets, 'vel')
def from_stream(cls, stream, components, imts, event=None, damping=None, smoothing=None, bandwidth=None, allow_nans=None, config=None, calc_waveform_metrics=True, calc_station_metrics=True, rupture=None, vs30_grids=None): """ Args: stream (obspy.core.stream.Stream): Strong motion timeseries for one station. components (list): List of requested components (str). imts (list): List of requested imts (str). event (ScalarEvent): Origin/magnitude for the event containing time, latitude, longitude, depth, and magnitude. damping (float): Damping of oscillator. Default is None. smoothing (float): Smoothing method. Default is None. bandwidth (float): Bandwidth of smoothing. Default is None. config (dictionary): Configuration dictionary. calc_waveform_metrics (bool): Whether to calculate waveform metrics. Default is True. calc_station_metrics (bool): Whether to calculate station metrics. Default is True. rupture (PointRupture or QuadRupture): impactutils rupture object. Default is None. vs30_grids (dict): A dictionary containing the vs30 grid files, names, and descriptions (see config). Note: Assumes a processed stream with units of gal (1 cm/s^2). No processing is done by this class. """ if config is None: config = get_config() station = cls() imts = np.sort(imts) components = np.sort(components) if damping is None: damping = config['metrics']['sa']['damping'] if smoothing is None: smoothing = config['metrics']['fas']['smoothing'] if bandwidth is None: bandwidth = config['metrics']['fas']['bandwidth'] if allow_nans is None: allow_nans = config['metrics']['fas']['allow_nans'] station._damping = damping station._smoothing = smoothing station._bandwidth = bandwidth station._stream = stream station.event = event station.set_metadata() if stream.passed and calc_waveform_metrics: metrics = MetricsController(imts, components, stream, bandwidth=bandwidth, allow_nans=allow_nans, damping=damping, event=event, smooth_type=smoothing) station.channel_dict = metrics.channel_dict.copy() pgms = metrics.pgms if pgms.empty: station._components = metrics.imcs station._imts = metrics.imts station.pgms = pd.DataFrame.from_dict({ 'IMT': [], 'IMC': [], 'Result': [] }) else: station._components = set(pgms.index.get_level_values('IMC')) station._imts = set(pgms.index.get_level_values('IMT')) station.pgms = pgms if calc_station_metrics: station.compute_station_metrics(rupture, vs30_grids) return station
def from_config(cls, stream, config=None, event=None, calc_waveform_metrics=True, calc_station_metrics=True, rupture=None, vs30_grids=None): """ Args: stream (obspy.core.stream.Stream): Strong motion timeseries for one station. config (dictionary): Configuration dictionary. event (ScalarEvent): Object containing latitude, longitude, depth, and magnitude. calc_waveform_metrics (bool): Whether to calculate waveform metrics. Default is True. calc_station_metrics (bool): Whether to calculate station metrics. Default is True. rupture (PointRupture or QuadRupture): impactutils rupture object. Default is None. vs30_grids (dict): A dictionary containing the vs30 grid files, names, and descriptions (see config). Note: Assumes a processed stream with units of gal (1 cm/s^2). No processing is done by this class. """ if config is None: config = get_config() station = cls() damping = config['metrics']['sa']['damping'] smoothing = config['metrics']['fas']['smoothing'] bandwidth = config['metrics']['fas']['bandwidth'] station._damping = damping station._smoothing = smoothing station._bandwidth = bandwidth station._stream = stream station.event = event station.set_metadata() if stream.passed and calc_waveform_metrics: metrics = MetricsController.from_config(stream, config=config, event=event) station.channel_dict = metrics.channel_dict.copy() pgms = metrics.pgms if pgms is None: station._components = metrics.imcs station._imts = metrics.imts station.pgms = pd.DataFrame.from_dict({ 'IMT': [], 'IMC': [], 'Result': [] }) else: station._components = set(pgms.index.get_level_values('IMC')) station._imts = set(pgms.index.get_level_values('IMT')) station.pgms = pgms if calc_station_metrics: station.compute_station_metrics(rupture, vs30_grids) return station
def test_controller(): datafiles, event = read_data_dir("geonet", "us1000778i", "20161113_110259_WTMC_20.V2A") datafile = datafiles[0] input_imts = [ "pgv", "pga", "sa2", "sa1.0", "sa0.3", "fas2", "fas1.0", "fas0.3", "arias", "invalid", ] input_imcs = [ "rotd50", "rotd100.0", "gmrotd50", "gmrotd100.0", "radial_transverse", "geometric_mean", "arithmetic_mean", "channels", "greater_of_two_horizontals", "invalid", "quadratic_mean", ] stream_v2 = read_geonet(datafile)[0] # Testing for acceleration -------------------------- m1 = MetricsController(input_imts, input_imcs, stream_v2, event=event, config=config) pgms = m1.pgms # testing for pga, pgv, sa target_imcs = [ "ROTD(50.0)", "ROTD(100.0)", "GMROTD(50.0)", "GMROTD(100.0)", "HNR", "HNT", "GEOMETRIC_MEAN", "ARITHMETIC_MEAN", "H1", "H2", "Z", "GREATER_OF_TWO_HORIZONTALS", "QUADRATIC_MEAN", ] for col in ["PGA", "PGV", "SA(1.000)", "SA(2.000)", "SA(0.300)"]: imcs = pgms.loc[col].index.tolist() assert len(imcs) == len(target_imcs) np.testing.assert_array_equal(np.sort(imcs), np.sort(target_imcs)) # testing for fas for col in ["FAS(1.000)", "FAS(2.000)", "FAS(0.300)"]: imcs = pgms.loc[col].index.tolist() assert len(imcs) == 9 np.testing.assert_array_equal( np.sort(imcs), [ "ARITHMETIC_MEAN", "GEOMETRIC_MEAN", "GREATER_OF_TWO_HORIZONTALS", "H1", "H2", "HNR", "HNT", "QUADRATIC_MEAN", "Z", ], ) # testing for arias imcs = pgms.loc["ARIAS"].index.tolist() assert len(imcs) == 9 np.testing.assert_array_equal( np.sort(imcs), [ "ARITHMETIC_MEAN", "GEOMETRIC_MEAN", "GREATER_OF_TWO_HORIZONTALS", "H1", "H2", "HNR", "HNT", "QUADRATIC_MEAN", "Z", ], ) _validate_steps(m1.step_sets, "acc") # Testing for Velocity -------------------------- for trace in stream_v2: trace.stats.standard.units = "vel" m = MetricsController(input_imts, input_imcs, stream_v2, event=event, config=config) pgms = m.pgms # testing for pga, pgv, sa target_imcs = [ "ROTD(50.0)", "ROTD(100.0)", "GMROTD(50.0)", "GMROTD(100.0)", "HNR", "HNT", "GEOMETRIC_MEAN", "ARITHMETIC_MEAN", "QUADRATIC_MEAN", "H1", "H2", "Z", "GREATER_OF_TWO_HORIZONTALS", ] for col in ["PGA", "PGV", "SA(1.000)", "SA(2.000)", "SA(0.300)"]: imcs = pgms.loc[col].index.tolist() assert len(imcs) == len(target_imcs) np.testing.assert_array_equal(np.sort(imcs), np.sort(target_imcs)) # testing for fas for col in ["FAS(1.000)", "FAS(2.000)", "FAS(0.300)"]: imcs = pgms.loc[col].index.tolist() assert len(imcs) == 9 np.testing.assert_array_equal( np.sort(imcs), [ "ARITHMETIC_MEAN", "GEOMETRIC_MEAN", "GREATER_OF_TWO_HORIZONTALS", "H1", "H2", "HNR", "HNT", "QUADRATIC_MEAN", "Z", ], ) # testing for arias imcs = pgms.loc["ARIAS"].index.tolist() assert len(imcs) == 9 np.testing.assert_array_equal( np.sort(imcs), [ "ARITHMETIC_MEAN", "GEOMETRIC_MEAN", "GREATER_OF_TWO_HORIZONTALS", "H1", "H2", "HNR", "HNT", "QUADRATIC_MEAN", "Z", ], ) _validate_steps(m.step_sets, "vel")