def test_set_velocity_no_picks(self): """ make sure it is not possible to set velocities if no picks have been provided """ with pytest.raises(ValueError, match="No phases have been added"): misutil.broadcast_param(pd.DataFrame(), self.velocity_dict, "source_velocity", "phase_hint")
def test_broadcast_from_dict(self, broadcast_df): """ verify that it is possible to specify source velocities from a dict""" out = misutil.broadcast_param( broadcast_df, self.velocity_dict, "source_velocity", "phase_hint" ) for phase, vel in self.velocity_dict.items(): assert (out.xs(phase, level="phase_hint")["source_velocity"] == vel).all()
def _add_free_surface_coefficient( self, df: pd.DataFrame, free_surface_coefficient: Optional[BroadcastableFloatType] = None, na_only: bool = True, ): """ Add the coefficient which corrects for free surface. By default just uses 1/2 if the depth of the instrument is 0, else 1. """ if free_surface_coefficient is None: station_depths = self.event_station_df.reset_index()[ ["seed_id", "station_depth"] ].drop_duplicates(subset="seed_id") station_depths["seed_id_less"] = station_depths["seed_id"].str[:-1] free_surface_map = station_depths.set_index("seed_id_less") free_surface_map["free_surface_coefficient"] = free_surface_map[ "station_depth" ].apply(lambda x: 2.0 if np.isclose(x, 0.0) else 1.0) free_surface_coefficient = free_surface_map[ "free_surface_coefficient" ].to_dict() return broadcast_param( df=df, param=free_surface_coefficient, col_name="free_surface_coefficient", broadcast_by="seed_id_less", na_only=na_only, )
def test_set_velocity_overwrite(self, simple_velocity): """ make sure overwriting issues a warning """ with pytest.warns(UserWarning, match="Overwriting"): out = misutil.broadcast_param( simple_velocity, self.velocity_dict, "source_velocity", "phase_hint", na_only=False, ) assert not (out["source_velocity"] == self.velocity).any()
def _add_source_velocity( self, df: pd.DataFrame, velocity: Optional[BroadcastableFloatType] = None, na_only: bool = True, ): """ Add the velocity to meta dataframe """ # Determine what the appropriate value should be if velocity is None: velocity = dict(S=get_default_param("s_velocity"), P=get_default_param("p_velocity")) return broadcast_param( df=df, param=velocity, col_name="source_velocity", broadcast_by="phase_hint", na_only=na_only, )
def _add_shear_modulus( self, df: pd.DataFrame, shear_modulus: Optional[BroadcastableFloatType] = None, na_only: bool = True, ): """ Add the shear modulus to the meta dataframe """ if shear_modulus is None: shear_modulus = get_default_param("shear_modulus") # Note, "broadcast_by" is a useless parameter for "shear_modulus" return broadcast_param( df=df, param=shear_modulus, col_name="shear_modulus", broadcast_by="phase_hint", na_only=na_only, )
def _add_density( self, df: pd.DataFrame, density: Optional[BroadcastableFloatType] = None, na_only: bool = True, ): """ Add density to the meta dataframe. If None, use defaults. """ if density is None: density = get_default_param("density") # Note, "broadcast_by" is a useless parameter for "density" return broadcast_param( df=df, param=density, col_name="density", broadcast_by="phase_hint", na_only=na_only, )
def _add_quality_factor( self, df: pd.DataFrame, quality_factor: Optional[BroadcastableFloatType] = None, na_only: bool = True, ): """Add the quality factor""" if quality_factor is None: quality_factor = dict( S=get_default_param("s_quality_factor"), P=get_default_param("p_quality_factor"), Noise=get_default_param("noise_quality_factor"), ) return broadcast_param( df=df, param=quality_factor, col_name="quality_factor", broadcast_by=("phase_hint", "seed_id_less"), na_only=na_only, )
def _add_radiation_coeficient( self, df: pd.DataFrame, radiation_coefficient: Optional[BroadcastableFloatType] = None, na_only=True, ): """Add the factor used to correct for radiation pattern.""" if radiation_coefficient is None: radiation_coefficient = dict( S=get_default_param("s_radiation_coefficient"), P=get_default_param("p_radiation_coefficient"), Noise=get_default_param("noise_radiation_coefficient"), ) return broadcast_param( df=df, param=radiation_coefficient, col_name="radiation_coefficient", broadcast_by="phase_hint", na_only=na_only, )
def test_broadcast_from_dict_not_possible(self, broadcast_df): """Ensure broadcasting from dict raises.""" with pytest.raises(TypeError, match="not supported"): misutil.broadcast_param( broadcast_df, self.velocity_dict, "source_velocity", None )
def simple_velocity(self, broadcast_df) -> pd.DataFrame: """ Returns a DataFrame with a uniform velocity applied""" return misutil.broadcast_param( broadcast_df, self.velocity, "source_velocity", "phase_hint" )
def test_set_velocity_bogus(self, broadcast_df): """ verify that a bogus velocity fails predictably""" with pytest.raises(TypeError): misutil.broadcast_param( broadcast_df, "bogus", "source_velocity", "phase_hint" )
def test_broadcast_from_series(self, broadcast_df, mapped_source_velocities): """Ensure broadcasting from series works.""" out = misutil.broadcast_param( broadcast_df, mapped_source_velocities, "source_velocity", "phase_hint" ) assert out["source_velocity"].equals(mapped_source_velocities)