Esempio n. 1
0
 def test_compute_sta_lta(self):
     ''' Test the computation of the threshold function.
     '''
     seismo = psysmon.core.test_util.compute_synthetic_seismogram(
         length=5, sps=1000, wavelet_offset=2)
     detector = detect.StaLtaDetector(data=seismo)
     detector.compute_cf()
     detector.compute_sta_lta()
Esempio n. 2
0
    def test_set_data(self):
        ''' Test the setting of the data.
        '''
        data = np.arange(10)
        detector = detect.StaLtaDetector()
        detector.set_data(data)

        np_test.assert_array_equal(detector.data, data)
        np_test.assert_array_equal(detector.cf, np.empty((0, 0)))
        np_test.assert_array_equal(detector.thrf, np.empty((0, 0)))
        np_test.assert_array_equal(detector.sta, np.empty((0, 0)))
        np_test.assert_array_equal(detector.lta, np.empty((0, 0)))
Esempio n. 3
0
    def test_sta_lta_detector_creation(self):
        ''' Test the creation of the StaLtaDetector instance.
        '''
        detector = detect.StaLtaDetector()
        np_test.assert_array_equal(detector.cf, np.empty((0, 0)))
        np_test.assert_array_equal(detector.thrf, np.empty((0, 0)))
        np_test.assert_array_equal(detector.sta, np.empty((0, 0)))
        np_test.assert_array_equal(detector.lta, np.empty((0, 0)))

        np_test.assert_raises(ValueError,
                              detect.StaLtaDetector,
                              cf_type='not_valid')
Esempio n. 4
0
    def test_compute_cf(self):
        ''' Test the computation of the characteristic function.
        '''
        data = np.arange(10)
        detector = detect.StaLtaDetector()
        detector.compute_cf()
        np_test.assert_array_equal(detector.data, np.empty((0, 0)))
        np_test.assert_array_equal(detector.cf, np.empty((0, 0)))

        detector.set_data(data=data)
        detector.cf_type = 'abs'
        detector.compute_cf()
        np_test.assert_array_equal(detector.data, data)
        np_test.assert_array_equal(detector.cf, np.abs(data))

        detector.cf_type = 'square'
        detector.compute_cf()
        np_test.assert_array_equal(detector.data, data)
        np_test.assert_array_equal(detector.cf, data**2)
Esempio n. 5
0
    def execute(self, stream, process_limits = None, origin_resource = None, **kwargs):
        ''' Execute the stack node.

        Parameters
        ----------
        stream : :class:`obspy.core.Stream`
            The data to process.
        '''
        self.logger.debug("stream: %s", str(stream))
        self.logger.debug("process_limits: %s", process_limits)

        # Get the detection parameters.
        sta_len = self.pref_manager.get_value('sta_length')
        lta_len = self.pref_manager.get_value('lta_length')
        thr = self.pref_manager.get_value('thr')
        fine_thr = self.pref_manager.get_value('fine_thr')
        turn_limit = self.pref_manager.get_value('turn_limit')
        cf_type = self.pref_manager.get_value('cf_type')
        stop_delay = self.pref_manager.get_value('stop_delay')
        stop_growth = self.pref_manager.get_value('stop_growth')
        stop_growth_exp = self.pref_manager.get_value('stop_growth_exp')
        stop_growth_inc = self.pref_manager.get_value('stop_growth_inc')
        stop_growth_inc_begin = self.pref_manager.get_value('stop_growth_inc_begin')
        reject_length = self.pref_manager.get_value('reject_length')

        # Get the selected catalog id.
        catalog_name = self.pref_manager.get_value('detection_catalog')
        self.load_catalogs()
        selected_catalog = [x for x in self.catalogs if x.name == catalog_name]
        if len(selected_catalog) == 0:
            raise RuntimeError("No detection catalog found with name %s in the database.", catalog_name)
        elif len(selected_catalog) > 1:
            raise RuntimeError("Multiple detection catalogs found with name %s: %s.", catalog_name, selected_catalog)
        else:
            selected_catalog = selected_catalog[0]

        # Initialize the detector.
        detector = detect.StaLtaDetector(thr = thr, cf_type = cf_type, fine_thr = fine_thr,
                                         turn_limit = turn_limit, stop_growth = stop_growth,
                                         stop_growth_exp = stop_growth_exp,
                                         stop_growth_inc = stop_growth_inc)

        # The list to store the data to be inserted into the database.
        db_data = []

        # Detect the events using the STA/LTA detector.
        for cur_trace in stream:
            # Check for open_end events and slice the data to the stored event
            # start. If no open_end event is found. slice the trace to the
            # original pre_stream_length.
            if cur_trace.id in iter(self.open_end_start.keys()):
                cur_oe_start = self.open_end_start.pop(cur_trace.id)
                cur_trace = cur_trace.slice(starttime = cur_oe_start)
                self.logger.debug("Sliced the cur_trace to the open_end start. cur_trace: %s.", cur_trace)
            else:
                cur_trace = cur_trace.slice(starttime = process_limits[0] - lta_len)
                self.logger.debug("Sliced the cur_trace to the original pre_stream_length. cur_trace: %s.", cur_trace)

            time_array = np.arange(0, cur_trace.stats.npts)
            time_array = old_div(time_array * 1,cur_trace.stats.sampling_rate)
            time_array = time_array + cur_trace.stats.starttime.timestamp

            # Check if the data is a ma.maskedarray
            if np.ma.count_masked(cur_trace.data):
                try:
                    time_array = np.ma.array(time_array, mask=cur_trace.data.mask)
                except:
                    time_array = np.ma.array(time_array[:-1], mask=cur_trace.data.mask)

            n_sta = int(sta_len * cur_trace.stats.sampling_rate)
            n_lta = int(lta_len * cur_trace.stats.sampling_rate)
            stop_delay_smp = int(stop_delay * cur_trace.stats.sampling_rate)

            detector.n_sta = n_sta
            detector.n_lta = n_lta
            detector.stop_growth_inc_begin = int(stop_growth_inc_begin * cur_trace.stats.sampling_rate)
            detector.reject_length = reject_length * cur_trace.stats.sampling_rate

            detector.set_data(cur_trace.data)
            detector.compute_cf()
            detector.compute_sta_lta()
            detection_markers = detector.compute_event_limits(stop_delay = stop_delay_smp)
            self.logger.debug("detection_markers: %s", detection_markers)

            # Check if the last event has no end. Change the pre_stream_length
            # to request more data for the next time window loop.
            last_marker = []
            if len(detection_markers) > 0 and np.isnan(detection_markers[-1][1]):
                # Handle a detected event with no end.
                last_marker = detection_markers.pop(-1)
                slice_marker = last_marker[0] - detector.n_lta - detector.n_sta - 1
                if slice_marker < 0:
                    slice_marker = 0
                cur_pre_length = (len(detector.data) - slice_marker) * cur_trace.stats.delta

                self.open_end_start[cur_trace.id] = utcdatetime.UTCDateTime(time_array[slice_marker])

                if self._pre_stream_length is None:
                    self._pre_stream_length = cur_pre_length
                elif cur_pre_length > self._pre_stream_length:
                    self._pre_stream_length = cur_pre_length

            # Get the database recorder stream id.
            try:
                cur_channel = self.project.geometry_inventory.get_channel(station = cur_trace.stats.station,
                                                                          name = cur_trace.stats.channel,
                                                                          network = cur_trace.stats.network,
                                                                          location = cur_trace.stats.location)[0]
                cur_timebox = cur_channel.get_stream(start_time = process_limits[0], end_time = process_limits[0])[0]
                cur_stream_id = cur_timebox.item.id
            except:
                cur_stream_id = None

            # Write the detections to the database.
            detection_table = self.project.dbTables['detection']
            for det_start_ind, det_end_ind in detection_markers:
                det_start_time = time_array[det_start_ind]
                det_end_time = time_array[det_end_ind]
                cur_orm = detection_table(catalog_id = selected_catalog.id,
                                          rec_stream_id = cur_stream_id,
                                          start_time = float(det_start_time),
                                          end_time = float(det_end_time),
                                          method = self.name_slug,
                                          agency_uri = self.project.activeUser.agency_uri,
                                          author_uri = self.project.activeUser.author_uri,
                                          creation_time = utcdatetime.UTCDateTime().isoformat())
                db_data.append(cur_orm)

        if db_data:
            try:
                db_session = self.project.getDbSession()
                db_session.add_all(db_data)
                db_session.commit()
            finally:
                db_session.close()

        if len(self.open_end_start) == 0:
            self._pre_stream_length = None
    def plot(self, stream, sta_len, lta_len, thr, fine_thr, turn_limit,
             cf_type, stop_delay, stop_growth, stop_growth_exp,
             stop_growth_inc, stop_growth_inc_begin, reject_length):
        ''' Plot the STA/LTA features.
        '''
        plot_detection_marker = True
        plot_lta_replace_marker = False
        #plot_features = ['sta', 'lta * thr']
        plot_features = ['sta', 'lta * thr', 'lta_orig * thr', 'stop_crit']
        #plot_features = ['thrf']

        detector = detect.StaLtaDetector(thr=thr,
                                         cf_type=cf_type,
                                         fine_thr=fine_thr,
                                         turn_limit=turn_limit,
                                         stop_growth=stop_growth,
                                         stop_growth_exp=stop_growth_exp,
                                         stop_growth_inc=stop_growth_inc)

        for cur_trace in stream:
            time_array = np.arange(0, cur_trace.stats.npts)
            time_array = old_div(time_array * 1, cur_trace.stats.sampling_rate)
            time_array = time_array + cur_trace.stats.starttime.timestamp

            # Check if the data is a ma.maskedarray
            if np.ma.count_masked(cur_trace.data):
                time_array = np.ma.array(time_array[:-1],
                                         mask=cur_trace.data.mask)

            n_sta = int(sta_len * cur_trace.stats.sampling_rate)
            n_lta = int(lta_len * cur_trace.stats.sampling_rate)
            stop_delay_smp = int(stop_delay * cur_trace.stats.sampling_rate)

            detector.n_sta = n_sta
            detector.n_lta = n_lta
            detector.stop_growth_inc_begin = int(stop_growth_inc_begin *
                                                 cur_trace.stats.sampling_rate)
            detector.reject_length = reject_length * cur_trace.stats.sampling_rate
            detector.set_data(cur_trace.data)
            detector.compute_cf()
            detector.compute_sta_lta()
            detection_markers = detector.compute_event_limits(
                stop_delay=stop_delay_smp)

            y_lim_min = []
            y_lim_max = []
            for cur_feature in plot_features:
                cur_line = self.lines[cur_feature]
                #if cur_feature == 'cf':
                cur_time = time_array
                #else:
                #    cur_time = time_array[detector.n_lta:-detector.n_sta]

                if cur_feature == 'lta * thr':
                    cur_data = detector.lta * detector.thr
                elif cur_feature == 'lta_orig * thr':
                    cur_data = detector.lta_orig * detector.thr
                elif cur_feature == 'thrf':
                    cur_data = old_div(detector.sta, detector.lta)
                else:
                    cur_data = getattr(detector, cur_feature)

                if cur_feature != 'stop_crit':
                    cur_min_data = np.min(cur_data[detector.valid_ind:])
                    cur_max_data = np.max(cur_data[detector.valid_ind:])

                y_lim_min.append(cur_min_data)
                y_lim_max.append(cur_max_data)

                if not cur_line:
                    if cur_feature == 'lta * thr':
                        cur_data = detector.lta * detector.thr
                        artists = self.axes.plot(cur_time, cur_data)
                    else:
                        artists = self.axes.plot(cur_time, cur_data)
                    self.lines[cur_feature] = artists[0]
                else:
                    cur_line.set_xdata(cur_time)
                    cur_line.set_ydata(cur_data)

            y_lim = [np.min(y_lim_min), np.max(y_lim_max)]
            print(y_lim)
            self.axes.set_ylim(bottom=y_lim[0], top=y_lim[1])
            #self.axes.set_ylim(bottom = 0, top = detector.thr)
            self.axes.set_yscale('log')

            # Clear the marker lines.
            for cur_line in self.marker_lines:
                self.axes.lines.remove(cur_line)
            self.marker_lines = []

            if plot_detection_marker:
                for det_start_ind, det_end_ind in detection_markers:
                    det_start_time = cur_trace.stats.starttime + old_div(
                        det_start_ind, cur_trace.stats.sampling_rate)
                    cur_line = self.axes.axvline(x=det_start_time.timestamp,
                                                 color='r')
                    self.marker_lines.append(cur_line)

                    if not np.isnan(det_end_ind):
                        det_end_time = det_start_time + old_div(
                            (det_end_ind - det_start_ind),
                            cur_trace.stats.sampling_rate)
                        cur_line = self.axes.axvline(x=det_end_time.timestamp,
                                                     color='b')
                        self.marker_lines.append(cur_line)

            if plot_lta_replace_marker:
                for det_start_ind, det_end_ind in detector.replace_limits:
                    #det_start_time = cur_trace.stats.starttime + (n_lta - 1 + det_start_ind) / cur_trace.stats.sampling_rate
                    det_start_time = cur_trace.stats.starttime + old_div(
                        det_start_ind, cur_trace.stats.sampling_rate)
                    det_end_time = det_start_time + old_div(
                        (det_end_ind - det_start_ind),
                        cur_trace.stats.sampling_rate)

                    cur_line = self.axes.axvline(x=det_start_time.timestamp,
                                                 color='y')
                    self.marker_lines.append(cur_line)
                    cur_line = self.axes.axvline(x=det_end_time.timestamp,
                                                 color='m')
                    self.marker_lines.append(cur_line)