def to_run_ts(self, fn=None, e_channels=["e1", "e2"]): """ Return a RunTS object from the data :param fn: DESCRIPTION, defaults to None :type fn: TYPE, optional :return: DESCRIPTION :rtype: TYPE """ ch_list = [] for comp in (["bx", "by", "bz"] + e_channels + ["temperature_e", "temperature_h"]): if comp[0] in ["h", "b"]: ch = ChannelTS("magnetic") elif comp[0] in ["e"]: ch = ChannelTS("electric") else: ch = ChannelTS("auxiliary") ch.sample_rate = self.sample_rate ch.start = self.start ch.ts = self._df[comp].values ch.component = comp ch_list.append(ch) return RunTS( array_list=ch_list, station_metadata=self.station_metadata, run_metadata=self.run_metadata, )
def test_channel_mtts(self): meta_dict = { "electric": { "component": "Ex", "dipole_length": 49.0, "measurement_azimuth": 12.0, "type": "electric", "units": "millivolts", "time_period.start": "2020-01-01T12:00:00", "sample_rate": 1, } } channel_ts = ChannelTS( channel_type="electric", data=np.random.rand(4096), channel_metadata=meta_dict, ) station = self.mth5_obj.add_station("MT002", survey="test") run = station.add_run("MT002a") ex = run.add_channel("Ex", "electric", None) ex.from_channel_ts(channel_ts) new_ts = ex.to_channel_ts() with self.subTest(name="start times"): self.assertEqual(channel_ts.start, new_ts.start) with self.subTest(name="metadata"): self.assertDictEqual(channel_ts._ts.time.to_dict(), new_ts._ts.time.to_dict())
def setUpClass(self): # pole zero filter pz = PoleZeroFilter(units_in="volts", units_out="nanotesla", name="instrument_response") pz.poles = [ (-6.283185 + 10.882477j), (-6.283185 - 10.882477j), (-12.566371 + 0j), ] pz.zeros = [] pz.normalization_factor = 18244400 # channel properties self.channel = ChannelTS() self.channel.channel_metadata.filter.applied = [False] self.channel.channel_metadata.filter.name = ["instrument_response"] self.channel.channel_metadata.component = "hx" self.channel.channel_response_filter.filters_list.append(pz) self.channel.sample_rate = 1 n_samples = 4096 self.t = np.arange(n_samples) * self.channel.sample_interval # make a random signal self.example_ts = np.sum( [ np.cos(2 * np.pi * w * self.t + phi) for w, phi in zip( np.logspace(-4, self.channel.sample_rate / 2, 20), np.random.rand(20), ) ], axis=0, ) # multiply by filter response f = np.fft.rfftfreq(self.t.size, self.channel.sample_interval) response_ts = np.fft.irfft( np.fft.rfft(self.example_ts) * pz.complex_response(f)[::-1]) # add in a linear trend self.channel.ts = (0.3 * self.t) + response_ts self.calibrated_ts = self.channel.remove_instrument_response()
def test_sr_fail(self): self.hz = ChannelTS( "magnetic", data=np.random.rand(self.npts), channel_metadata={ "magnetic": { "component": "hz", "sample_rate": 1, "time_period.start": self.start, } }, ) self.assertRaises( MTTSError, self.run.set_dataset, [self.ex, self.ey, self.hx, self.hy, self.hz], )
def test_from_run_ts(self): ts_list = [] for comp in ["ex", "ey", "hx", "hy", "hz"]: if comp[0] in ["e"]: ch_type = "electric" elif comp[1] in ["h", "b"]: ch_type = "magnetic" else: ch_type = "auxiliary" meta_dict = { ch_type: { "component": comp, "dipole_length": 49.0, "measurement_azimuth": 12.0, "type": ch_type, "units": "counts", "time_period.start": "2020-01-01T12:00:00", "sample_rate": 1, } } channel_ts = ChannelTS(ch_type, data=np.random.rand(4096), channel_metadata=meta_dict) ts_list.append(channel_ts) run_ts = RunTS(ts_list, {"id": "MT002a"}) station = self.mth5_obj.add_station("MT002", survey="test") run = station.add_run("MT002a") channel_groups = run.from_runts(run_ts) self.assertListEqual(["ex", "ey", "hx", "hy", "hz"], run.groups_list) # check to make sure the metadata was transfered for cg in channel_groups: with self.subTest(name=cg.metadata.component): self.assertEqual(MTime("2020-01-01T12:00:00"), cg.start) self.assertEqual(1, cg.sample_rate) self.assertEqual(4096, cg.n_samples) # slicing with self.subTest("get slice"): r_slice = run.to_runts(start="2020-01-01T12:00:00", n_samples=256) self.assertEqual(r_slice.end, "2020-01-01T12:04:16+00:00")
class TestRemoveResponse(unittest.TestCase): """ Test remove response, make a fake signal add some trends, """ @classmethod def setUpClass(self): # pole zero filter pz = PoleZeroFilter(units_in="volts", units_out="nanotesla", name="instrument_response") pz.poles = [ (-6.283185 + 10.882477j), (-6.283185 - 10.882477j), (-12.566371 + 0j), ] pz.zeros = [] pz.normalization_factor = 18244400 # channel properties self.channel = ChannelTS() self.channel.channel_metadata.filter.applied = [False] self.channel.channel_metadata.filter.name = ["instrument_response"] self.channel.channel_metadata.component = "hx" self.channel.channel_response_filter.filters_list.append(pz) self.channel.sample_rate = 1 n_samples = 4096 self.t = np.arange(n_samples) * self.channel.sample_interval # make a random signal self.example_ts = np.sum( [ np.cos(2 * np.pi * w * self.t + phi) for w, phi in zip( np.logspace(-4, self.channel.sample_rate / 2, 20), np.random.rand(20), ) ], axis=0, ) # multiply by filter response f = np.fft.rfftfreq(self.t.size, self.channel.sample_interval) response_ts = np.fft.irfft( np.fft.rfft(self.example_ts) * pz.complex_response(f)[::-1]) # add in a linear trend self.channel.ts = (0.3 * self.t) + response_ts self.calibrated_ts = self.channel.remove_instrument_response() def test_return_type(self): self.assertIsInstance(self.calibrated_ts, ChannelTS) def test_applied(self): self.assertTrue((np.array( self.calibrated_ts.channel_metadata.filter.applied) == True).all()) def test_returned_metadata(self): with self.subTest("component"): self.assertEqual(self.channel.component, self.calibrated_ts.component) with self.subTest("sample rate"): self.assertEqual(self.channel.sample_rate, self.calibrated_ts.sample_rate) def test_calibration(self): original_ts = self.example_ts - self.example_ts.mean() std = (self.calibrated_ts.ts - original_ts).std() / original_ts.max() self.assertLessEqual(std, 0.075)
def setUp(self): self.run = RunTS() self.maxDiff = None self.start = "2015-01-08T19:49:18+00:00" self.end = "2015-01-08T19:57:49.875000" self.sample_rate = 8 self.npts = 4096 self.ex = ChannelTS( "electric", data=np.random.rand(self.npts), channel_metadata={ "electric": { "component": "Ex", "sample_rate": self.sample_rate, "time_period.start": self.start, } }, ) self.ey = ChannelTS( "electric", data=np.random.rand(self.npts), channel_metadata={ "electric": { "component": "Ey", "sample_rate": self.sample_rate, "time_period.start": self.start, } }, ) self.hx = ChannelTS( "magnetic", data=np.random.rand(self.npts), channel_metadata={ "magnetic": { "component": "hx", "sample_rate": self.sample_rate, "time_period.start": self.start, } }, ) self.hy = ChannelTS( "magnetic", data=np.random.rand(self.npts), channel_metadata={ "magnetic": { "component": "hy", "sample_rate": self.sample_rate, "time_period.start": self.start, } }, ) self.hz = ChannelTS( "magnetic", data=np.random.rand(self.npts), channel_metadata={ "magnetic": { "component": "hz", "sample_rate": self.sample_rate, "time_period.start": self.start, } }, ) self.run.set_dataset([self.ex, self.ey, self.hx, self.hy, self.hz])