예제 #1
0
 def test_registerRtProcess(self):
     """
     Testing register_rt_process method.
     """
     tr = RtTrace()
     # 1 - function call
     tr.register_rt_process(np.abs)
     self.assertEqual(tr.processing, [(np.abs, {}, None)])
     # 2 - predefined RT processing algorithm
     tr.register_rt_process('integrate', test=1, muh='maeh')
     self.assertEqual(tr.processing[1][0], 'integrate')
     self.assertEqual(tr.processing[1][1], {'test': 1, 'muh': 'maeh'})
     self.assertTrue(isinstance(tr.processing[1][2][0], RtMemory))
     # 3 - contained name of predefined RT processing algorithm
     tr.register_rt_process('in')
     self.assertEqual(tr.processing[2][0], 'integrate')
     tr.register_rt_process('integ')
     self.assertEqual(tr.processing[3][0], 'integrate')
     tr.register_rt_process('integr')
     self.assertEqual(tr.processing[4][0], 'integrate')
     # 4 - unknown functions
     self.assertRaises(NotImplementedError,
                       tr.register_rt_process, 'integrate2')
     self.assertRaises(NotImplementedError, tr.register_rt_process, 'xyz')
     # 5 - module instead of function
     self.assertRaises(NotImplementedError, tr.register_rt_process, np)
     # check number off all processing steps within RtTrace
     self.assertEqual(len(tr.processing), 5)
     # check tr.stats.processing
     self.assertEqual(len(tr.stats.processing), 5)
     self.assertTrue(tr.stats.processing[0].startswith("realtime_process"))
     self.assertIn('absolute', tr.stats.processing[0])
     for i in range(1, 5):
         self.assertIn('integrate', tr.stats.processing[i])
     # check kwargs
     self.assertIn("maeh", tr.stats.processing[1])
예제 #2
0
 def test_register_rt_process(self):
     """
     Testing register_rt_process method.
     """
     tr = RtTrace()
     # 1 - function call
     tr.register_rt_process(np.abs)
     self.assertEqual(tr.processing, [(np.abs, {}, None)])
     # 2 - predefined RT processing algorithm
     tr.register_rt_process('integrate', test=1, muh='maeh')
     self.assertEqual(tr.processing[1][0], 'integrate')
     self.assertEqual(tr.processing[1][1], {'test': 1, 'muh': 'maeh'})
     self.assertTrue(isinstance(tr.processing[1][2][0], RtMemory))
     # 3 - contained name of predefined RT processing algorithm
     tr.register_rt_process('in')
     self.assertEqual(tr.processing[2][0], 'integrate')
     tr.register_rt_process('integ')
     self.assertEqual(tr.processing[3][0], 'integrate')
     tr.register_rt_process('integr')
     self.assertEqual(tr.processing[4][0], 'integrate')
     # 4 - unknown functions
     self.assertRaises(NotImplementedError, tr.register_rt_process,
                       'integrate2')
     self.assertRaises(NotImplementedError, tr.register_rt_process, 'xyz')
     # 5 - module instead of function
     self.assertRaises(NotImplementedError, tr.register_rt_process, np)
     # check number off all processing steps within RtTrace
     self.assertEqual(len(tr.processing), 5)
     # check tr.stats.processing
     self.assertEqual(len(tr.stats.processing), 5)
     self.assertTrue(tr.stats.processing[0].startswith("realtime_process"))
     self.assertIn('absolute', tr.stats.processing[0])
     for i in range(1, 5):
         self.assertIn('integrate', tr.stats.processing[i])
     # check kwargs
     self.assertIn("maeh", tr.stats.processing[1])
예제 #3
0
 def test_copy(self):
     """
     Testing copy of RtTrace object.
     """
     rtr = RtTrace()
     rtr.copy()
     # register predefined function
     rtr.register_rt_process('integrate', test=1, muh='maeh')
     rtr.copy()
     # register ObsPy function call
     rtr.register_rt_process(signal.filter.bandpass, freqmin=0, freqmax=1,
                             df=0.1)
     rtr.copy()
     # register NumPy function call
     rtr.register_rt_process(np.square)
     rtr.copy()
예제 #4
0
 def test_copy(self):
     """
     Testing copy of RtTrace object.
     """
     rtr = RtTrace()
     rtr.copy()
     # register predefined function
     rtr.register_rt_process('integrate', test=1, muh='maeh')
     rtr.copy()
     # register ObsPy function call
     rtr.register_rt_process(obspy.signal.filter.bandpass,
                             freqmin=0,
                             freqmax=1,
                             df=0.1)
     rtr.copy()
     # register NumPy function call
     rtr.register_rt_process(np.square)
     rtr.copy()
예제 #5
0
 def test_missingOrWrongArgumentInRtProcess(self):
     """
     Tests handling of missing/wrong arguments.
     """
     trace = Trace(np.arange(100))
     # 1- function scale needs no additional arguments
     rt_trace = RtTrace()
     rt_trace.register_rt_process('scale')
     rt_trace.append(trace)
     # adding arbitrary arguments should fail
     rt_trace = RtTrace()
     rt_trace.register_rt_process('scale', muh='maeh')
     self.assertRaises(TypeError, rt_trace.append, trace)
     # 2- function tauc has one required argument
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc', width=10)
     rt_trace.append(trace)
     # wrong argument should fail
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc', xyz='xyz')
     self.assertRaises(TypeError, rt_trace.append, trace)
     # missing argument width should raise an exception
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc')
     self.assertRaises(TypeError, rt_trace.append, trace)
     # adding arbitrary arguments should fail
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc', width=20, notexistingoption=True)
     self.assertRaises(TypeError, rt_trace.append, trace)
예제 #6
0
class RealTimeSignalTestCase(unittest.TestCase):
    """
    The obspy.realtime.signal test suite.
    """
    @classmethod
    def setUpClass(cls):
        # read test data as float64
        cls.orig_trace = read(os.path.join(os.path.dirname(__file__), 'data',
                                           'II.TLY.BHZ.SAC'),
                              dtype=np.float64)[0]
        # make really sure test data is float64
        cls.orig_trace.data = np.require(cls.orig_trace.data, np.float64)
        cls.orig_trace_chunks = cls.orig_trace / NUM_PACKETS

    def setUp(self):
        # clear results
        self.filt_trace_data = None
        self.rt_trace = None
        self.rt_appended_traces = []

    def tearDown(self):
        # use results for debug plots if enabled
        if PLOT_TRACES and self.filt_trace_data is not None and \
           self.rt_trace is not None and self.rt_appended_traces:
            self._plot_results()

    def test_square(self):
        """
        Testing np.square function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = np.square(trace)
        # filtering real time
        process_list = [(np.square, {})]
        self._run_rt_process(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_integrate(self):
        """
        Testing integrate function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = signal.integrate(trace)
        # filtering real time
        process_list = [('integrate', {})]
        self._run_rt_process(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_differentiate(self):
        """
        Testing differentiate function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = signal.differentiate(trace)
        # filtering real time
        process_list = [('differentiate', {})]
        self._run_rt_process(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_boxcar(self):
        """
        Testing boxcar function.
        """
        trace = self.orig_trace.copy()
        options = {'width': 500}
        # filtering manual
        self.filt_trace_data = signal.boxcar(trace, **options)
        # filtering real time
        process_list = [('boxcar', options)]
        self._run_rt_process(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertAlmostEqual(peak, 566974.214, 3)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_scale(self):
        """
        Testing scale function.
        """
        trace = self.orig_trace.copy()
        options = {'factor': 1000}
        # filtering manual
        self.filt_trace_data = signal.scale(trace, **options)
        # filtering real time
        process_list = [('scale', options)]
        self._run_rt_process(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertEqual(peak, 1045237000.0)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_offset(self):
        """
        Testing offset function.
        """
        trace = self.orig_trace.copy()
        options = {'offset': 500}
        # filtering manual
        self.filt_trace_data = signal.offset(trace, **options)
        # filtering real time
        process_list = [('offset', options)]
        self._run_rt_process(process_list)
        # check results
        diff = self.rt_trace.data - self.orig_trace.data
        self.assertEqual(np.mean(diff), 500)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_kurtosis(self):
        """
        Testing kurtosis function.
        """
        trace = self.orig_trace.copy()
        options = {'win': 5}
        # filtering manual
        self.filt_trace_data = signal.kurtosis(trace, **options)
        # filtering real time
        process_list = [('kurtosis', options)]
        self._run_rt_process(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_abs(self):
        """
        Testing np.abs function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = np.abs(trace)
        # filtering real time
        process_list = [(np.abs, {})]
        self._run_rt_process(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertEqual(peak, 1045237)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_tauc(self):
        """
        Testing tauc function.
        """
        trace = self.orig_trace.copy()
        options = {'width': 60}
        # filtering manual
        self.filt_trace_data = signal.tauc(trace, **options)
        # filtering real time
        process_list = [('tauc', options)]
        self._run_rt_process(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertAlmostEqual(peak, 114.302, 3)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_mwp_integral(self):
        """
        Testing mwpintegral functions.
        """
        trace = self.orig_trace.copy()
        options = {
            'mem_time': 240,
            'ref_time': trace.stats.starttime + 301.506,
            'max_time': 120,
            'gain': 1.610210e+09
        }
        # filtering manual
        self.filt_trace_data = signal.mwpintegral(self.orig_trace.copy(),
                                                  **options)
        # filtering real time
        process_list = [('mwpintegral', options)]
        self._run_rt_process(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_mwp(self):
        """
        Testing Mwp calculation using two processing functions.
        """
        trace = self.orig_trace.copy()
        epicentral_distance = 30.0855
        options = {
            'mem_time': 240,
            'ref_time': trace.stats.starttime + 301.506,
            'max_time': 120,
            'gain': 1.610210e+09
        }
        # filtering manual
        trace.data = signal.integrate(trace)
        self.filt_trace_data = signal.mwpintegral(trace, **options)
        # filtering real time
        process_list = [('integrate', {}), ('mwpintegral', options)]
        self._run_rt_process(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        mwp = signal.calculate_mwp_mag(peak, epicentral_distance)
        self.assertAlmostEqual(mwp, 8.78902911791, 5)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_combined(self):
        """
        Testing combining integrate and differentiate functions.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        trace.data = signal.integrate(trace)
        self.filt_trace_data = signal.differentiate(trace)
        # filtering real time
        process_list = [('int', {}), ('diff', {})]
        self._run_rt_process(process_list)
        # check results
        trace = self.orig_trace.copy()
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)
        np.testing.assert_almost_equal(trace.data[1:], self.rt_trace.data[1:])
        np.testing.assert_almost_equal(trace.data[1:],
                                       self.filt_trace_data[1:])

    def _run_rt_process(self, process_list, max_length=None):
        """
        Helper function to create a RtTrace, register all given process
        functions and run the real time processing.
        """
        # assemble real time trace
        self.rt_trace = RtTrace(max_length=max_length)

        for (process, options) in process_list:
            self.rt_trace.register_rt_process(process, **options)

        # append packet data to RtTrace
        self.rt_appended_traces = []
        for trace in self.orig_trace_chunks:
            # process single trace
            result = self.rt_trace.append(trace, gap_overlap_check=True)
            # add to list of appended traces
            self.rt_appended_traces.append(result)

    def _plot_results(self):
        """
        Plots original, filtered original and real time processed traces into
        a single plot.
        """
        # plot only if test is started manually
        if __name__ != '__main__':
            return
        # create empty stream
        st = Stream()
        st.label = self._testMethodName
        # original trace
        self.orig_trace.label = "Original Trace"
        st += self.orig_trace
        # use header information of original trace with filtered trace data
        tr = self.orig_trace.copy()
        tr.data = self.filt_trace_data
        tr.label = "Filtered original Trace"
        st += tr
        # real processed chunks
        for i, tr in enumerate(self.rt_appended_traces):
            tr.label = "RT Chunk %02d" % (i + 1)
            st += tr
        # real time processed trace
        self.rt_trace.label = "RT Trace"
        st += self.rt_trace
        st.plot(automerge=False, color='blue', equal_scale=False)
예제 #7
0
 def test_missing_or_wrong_argument_in_rt_process(self):
     """
     Tests handling of missing/wrong arguments.
     """
     trace = Trace(np.arange(100))
     # 1- function scale needs no additional arguments
     rt_trace = RtTrace()
     rt_trace.register_rt_process('scale')
     rt_trace.append(trace)
     # adding arbitrary arguments should fail
     rt_trace = RtTrace()
     rt_trace.register_rt_process('scale', muh='maeh')
     self.assertRaises(TypeError, rt_trace.append, trace)
     # 2- function tauc has one required argument
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc', width=10)
     rt_trace.append(trace)
     # wrong argument should fail
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc', xyz='xyz')
     self.assertRaises(TypeError, rt_trace.append, trace)
     # missing argument width should raise an exception
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc')
     self.assertRaises(TypeError, rt_trace.append, trace)
     # adding arbitrary arguments should fail
     rt_trace = RtTrace()
     rt_trace.register_rt_process('tauc', width=20, notexistingoption=True)
     self.assertRaises(TypeError, rt_trace.append, trace)
예제 #8
0
class RealTimeSignalTestCase(unittest.TestCase):
    """
    The obspy.realtime.signal test suite.
    """
    def __init__(self, *args, **kwargs):
        super(RealTimeSignalTestCase, self).__init__(*args, **kwargs)
        # read test data as float64
        self.orig_trace = read(os.path.join(os.path.dirname(__file__), 'data',
                                            'II.TLY.BHZ.SAC'),
                               dtype=np.float64)[0]
        # make really sure test data is float64
        self.orig_trace.data = np.require(self.orig_trace.data, np.float64)
        self.orig_trace_chunks = self.orig_trace / NUM_PACKETS

    def setUp(self):
        # clear results
        self.filt_trace_data = None
        self.rt_trace = None
        self.rt_appended_traces = []

    def tearDown(self):
        # use results for debug plots if enabled
        if PLOT_TRACES and self.filt_trace_data is not None and \
           self.rt_trace is not None and self.rt_appended_traces:
            self._plotResults()

    def test_square(self):
        """
        Testing np.square function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = np.square(trace)
        # filtering real time
        process_list = [(np.square, {})]
        self._runRtProcess(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_integrate(self):
        """
        Testing integrate function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = signal.integrate(trace)
        # filtering real time
        process_list = [('integrate', {})]
        self._runRtProcess(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_differentiate(self):
        """
        Testing differentiate function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = signal.differentiate(trace)
        # filtering real time
        process_list = [('differentiate', {})]
        self._runRtProcess(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_boxcar(self):
        """
        Testing boxcar function.
        """
        trace = self.orig_trace.copy()
        options = {'width': 500}
        # filtering manual
        self.filt_trace_data = signal.boxcar(trace, **options)
        # filtering real time
        process_list = [('boxcar', options)]
        self._runRtProcess(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertAlmostEqual(peak, 566974.214, 3)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_scale(self):
        """
        Testing scale function.
        """
        trace = self.orig_trace.copy()
        options = {'factor': 1000}
        # filtering manual
        self.filt_trace_data = signal.scale(trace, **options)
        # filtering real time
        process_list = [('scale', options)]
        self._runRtProcess(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertEqual(peak, 1045237000.0)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_offset(self):
        """
        Testing offset function.
        """
        trace = self.orig_trace.copy()
        options = {'offset': 500}
        # filtering manual
        self.filt_trace_data = signal.offset(trace, **options)
        # filtering real time
        process_list = [('offset', options)]
        self._runRtProcess(process_list)
        # check results
        diff = self.rt_trace.data - self.orig_trace.data
        self.assertEqual(np.mean(diff), 500)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_kurtosis(self):
        """
        Testing kurtosis function.
        """
        trace = self.orig_trace.copy()
        options = {'win': 5}
        # filtering manual
        self.filt_trace_data = signal.kurtosis(trace, **options)
        # filtering real time
        process_list = [('kurtosis', options)]
        self._runRtProcess(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_abs(self):
        """
        Testing np.abs function.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        self.filt_trace_data = np.abs(trace)
        # filtering real time
        process_list = [(np.abs, {})]
        self._runRtProcess(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertEqual(peak, 1045237)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_tauc(self):
        """
        Testing tauc function.
        """
        trace = self.orig_trace.copy()
        options = {'width': 60}
        # filtering manual
        self.filt_trace_data = signal.tauc(trace, **options)
        # filtering real time
        process_list = [('tauc', options)]
        self._runRtProcess(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        self.assertAlmostEqual(peak, 114.302, 3)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_mwpIntegral(self):
        """
        Testing mwpIntegral functions.
        """
        trace = self.orig_trace.copy()
        options = {'mem_time': 240,
                   'ref_time': trace.stats.starttime + 301.506,
                   'max_time': 120,
                   'gain': 1.610210e+09}
        # filtering manual
        self.filt_trace_data = signal.mwpIntegral(self.orig_trace.copy(),
                                                  **options)
        # filtering real time
        process_list = [('mwpIntegral', options)]
        self._runRtProcess(process_list)
        # check results
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_mwp(self):
        """
        Testing Mwp calculation using two processing functions.
        """
        trace = self.orig_trace.copy()
        epicentral_distance = 30.0855
        options = {'mem_time': 240,
                   'ref_time': trace.stats.starttime + 301.506,
                   'max_time': 120,
                   'gain': 1.610210e+09}
        # filtering manual
        trace.data = signal.integrate(trace)
        self.filt_trace_data = signal.mwpIntegral(trace, **options)
        # filtering real time
        process_list = [('integrate', {}), ('mwpIntegral', options)]
        self._runRtProcess(process_list)
        # check results
        peak = np.amax(np.abs(self.rt_trace.data))
        mwp = signal.calculateMwpMag(peak, epicentral_distance)
        self.assertAlmostEqual(mwp, 8.78902911791, 5)
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)

    def test_combined(self):
        """
        Testing combining integrate and differentiate functions.
        """
        trace = self.orig_trace.copy()
        # filtering manual
        trace.data = signal.integrate(trace)
        self.filt_trace_data = signal.differentiate(trace)
        # filtering real time
        process_list = [('int', {}), ('diff', {})]
        self._runRtProcess(process_list)
        # check results
        trace = self.orig_trace.copy()
        np.testing.assert_almost_equal(self.filt_trace_data,
                                       self.rt_trace.data)
        np.testing.assert_almost_equal(trace.data[1:], self.rt_trace.data[1:])
        np.testing.assert_almost_equal(trace.data[1:],
                                       self.filt_trace_data[1:])

    def _runRtProcess(self, process_list, max_length=None):
        """
        Helper function to create a RtTrace, register all given process
        functions and run the real time processing.
        """
        # assemble real time trace
        self.rt_trace = RtTrace(max_length=max_length)

        for (process, options) in process_list:
            self.rt_trace.register_rt_process(process, **options)

        # append packet data to RtTrace
        self.rt_appended_traces = []
        for trace in self.orig_trace_chunks:
            # process single trace
            result = self.rt_trace.append(trace, gap_overlap_check=True)
            # add to list of appended traces
            self.rt_appended_traces.append(result)

    def _plotResults(self):
        """
        Plots original, filtered original and real time processed traces into
        a single plot.
        """
        # plot only if test is started manually
        if __name__ != '__main__':
            return
        # create empty stream
        st = Stream()
        st.label = self._testMethodName
        # original trace
        self.orig_trace.label = "Original Trace"
        st += self.orig_trace
        # use header information of original trace with filtered trace data
        tr = self.orig_trace.copy()
        tr.data = self.filt_trace_data
        tr.label = "Filtered original Trace"
        st += tr
        # real processed chunks
        for i, tr in enumerate(self.rt_appended_traces):
            tr.label = "RT Chunk %02d" % (i + 1)
            st += tr
        # real time processed trace
        self.rt_trace.label = "RT Trace"
        st += self.rt_trace
        st.plot(automerge=False, color='blue', equal_scale=False)