Ejemplo n.º 1
0
def test_custom_weight_function():
    """
    Test the custom weight function. Set the weight of every window with a
    CC of smaller then 95 to 0.0.
    """
    def weight_function(win):
        if win.max_cc_value < 0.95:
            return 0.0
        else:
            return 10.0

    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.80,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0,
        window_weight_fct=weight_function)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert np.all(np.array([_i.max_cc_value for _i in windows]) >= 0.95)

    # Not setting it will result in the default value.
    config.window_weight_fct = None
    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert bool(np.all(np.array([_i.max_cc_value for _i in windows]) >=
                       0.95)) is False
Ejemplo n.º 2
0
def test_window_plotting(tmpdir):
    reset_matplotlib()

    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.80,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0)

    pyflex.select_windows(OBS_DATA, SYNTH_DATA, config, plot=True)
    images_are_identical("picked_windows", str(tmpdir))
Ejemplo n.º 3
0
def process(this_station_group, other_station_group):
    # Make sure everything thats required is there.
    if not hasattr(this_station_group, "StationXML") or \
            not hasattr(this_station_group, "preprocessed_27s_to_60s") or \
            not hasattr(other_station_group,
                        "preprocessed_synthetic_27s_to_60s"):
        return

    stationxml = this_station_group.StationXML
    observed = this_station_group.preprocessed_27s_to_60s
    synthetic = other_station_group.preprocessed_synthetic_27s_to_60s

    all_windows = []

    for component in ["Z", "R", "T"]:
        obs = observed.select(component=component)
        syn = synthetic.select(component=component)
        if not obs or not syn:
            continue

        windows = pyflex.select_windows(obs,
                                        syn,
                                        config,
                                        event=event,
                                        station=stationxml)
        print("Station %s.%s component %s picked %i windows" %
              (stationxml[0].code, stationxml[0][0].code, component,
               len(windows)))
        if not windows:
            continue
        all_windows.append(windows)
    return all_windows
Ejemplo n.º 4
0
def test_settings_arrays_as_config_values():
    """
    Tests that arrays can be set as config values.
    """
    npts = OBS_DATA[0].stats.npts
    stalta_waterlevel = 0.08 * np.ones(npts)
    tshift_acceptance_level = 15.0 * np.ones(npts)
    dlna_acceptance_level = 1.0 * np.ones(npts)
    cc_acceptance_level = 0.80 * np.ones(npts)
    s2n_limit = 1.5 * np.ones(npts)
    config = pyflex.Config(min_period=50.0,
                           max_period=150.0,
                           stalta_waterlevel=stalta_waterlevel,
                           tshift_acceptance_level=tshift_acceptance_level,
                           dlna_acceptance_level=dlna_acceptance_level,
                           cc_acceptance_level=cc_acceptance_level,
                           s2n_limit=s2n_limit,
                           c_0=0.7,
                           c_1=4.0,
                           c_2=0.0,
                           c_3a=1.0,
                           c_3b=2.0,
                           c_4a=3.0,
                           c_4b=10.0)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert len(windows) == 9
Ejemplo n.º 5
0
def compute_windows_pyflex(event, pyflex_conf, conf):
    obsds = utils.SUReader(conf, [event], "obs")
    synts = utils.SUReader(conf, [event], "syn")
    windows = {}

    event_loc = conf.get_event_loc(event)
    for station, loc in conf.get_stations(event,
                                          add_event_label=True).iteritems():
        dist = get_min_dist(conf, event_loc, loc)
        t = dist / conf.window.max_vel
        ti = int(t * obsds[station].stats.sampling_rate)
        pyflex_conf.noise_start_index = 0
        pyflex_conf.noise_end_index = ti
        pyflex_conf.signal_start_index = ti + 1
        pyflex_conf.signal_end_index = conf.simulation.nstep
        sta_wins = pyflex.select_windows(obsds[station],
                                         synts[station],
                                         config=pyflex_conf,
                                         plot=False)
        sta = ".".join(station.split(".")[:3])
        windows[sta] = [(w.left * w.dt, w.right * w.dt) for w in sta_wins]

    filename = conf.get_windows_filename(event)
    io.makedir_for(filename)
    utils.write_json(filename, windows)
    return {}
def process(this_station_group, other_station_group):
    # Make sure everything thats required is there.
    if not hasattr(this_station_group, "StationXML") or \
            not hasattr(this_station_group, "preprocessed_27s_to_60s") or \
            not hasattr(other_station_group,
                        "preprocessed_synthetic_27s_to_60s"):
        return

    stationxml = this_station_group.StationXML
    observed = this_station_group.preprocessed_27s_to_60s
    synthetic = other_station_group.preprocessed_synthetic_27s_to_60s

    all_windows = []

    for component in ["Z", "R", "T"]:
        obs = observed.select(component=component)
        syn = synthetic.select(component=component)
        if not obs or not syn:
            continue

        windows = pyflex.select_windows(
            obs, syn, config, event=event, station=stationxml)
        print("Station %s.%s component %s picked %i windows" % (
            stationxml[0].code, stationxml[0][0].code, component,
            len(windows)))
        if not windows:
            continue
        all_windows.append(windows)
    return all_windows
Ejemplo n.º 7
0
def test_window_selection():
    """
    This WILL need to be adjusted if any part of the algorithm changes!

    The settings for this test are more or less the same as for the test
    data example in the original FLEXWIN package.
    """
    config = pyflex.Config(min_period=50.0,
                           max_period=150.0,
                           stalta_waterlevel=0.08,
                           tshift_acceptance_level=15.0,
                           dlna_acceptance_level=1.0,
                           cc_acceptance_level=0.80,
                           c_0=0.7,
                           c_1=4.0,
                           c_2=0.0,
                           c_3a=1.0,
                           c_3b=2.0,
                           c_4a=3.0,
                           c_4b=10.0)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert len(windows) == 9

    lefties = np.array([_i.left for _i in windows])
    righties = np.array([_i.right for _i in windows])

    np.testing.assert_allclose(
        lefties,
        np.array([1551, 2221, 2709, 2960, 3353, 3609, 3983, 4715, 4962]),
        atol=3)
    np.testing.assert_allclose(
        righties,
        np.array([1985, 2709, 2960, 3172, 3609, 3920, 4442, 4962, 5207]),
        atol=3)

    np.testing.assert_allclose(np.array([_i.max_cc_value for _i in windows]),
                               np.array([
                                   0.95740629, 0.96646804, 0.96335716,
                                   0.98249547, 0.96838754, 0.88501979,
                                   0.82529382, 0.92953344, 0.92880873
                               ]),
                               rtol=1E-2)

    assert [_i.cc_shift for _i in windows] == [-3, 0, -5, -5, -6, 4, -9, -1, 7]
    np.testing.assert_allclose(np.array([_i.dlnA for _i in windows]),
                               np.array([
                                   0.07469, 0.12808, -0.19277, 0.185563,
                                   0.093674, -0.118859, -0.638657, 0.25942,
                                   0.106571
                               ]),
                               rtol=1E-2)

    # Assert the phases of the first window.
    assert sorted([_i["phase_name"] for _i in windows[0].phase_arrivals]) == \
        ['PKIKP', 'PKIKS', 'PKiKP', 'PP', 'SKIKP', 'SKiKP', 'pPKIKP', 'pPKiKP',
         'sPKIKP', 'sPKiKP']
Ejemplo n.º 8
0
def test_cc_config_setting():
    """
    Make sure setting the CC threshold does something.
    """
    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.95,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert np.all(np.array([_i.max_cc_value for _i in windows]) >= 0.95)
Ejemplo n.º 9
0
def test_window_merging_strategy():
    """
    Pyflex can also merge windows.
    """
    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.80,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0,
        resolution_strategy="merge")

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert len(windows) == 4
Ejemplo n.º 10
0
def test_run_with_data_quality_checks():
    """
    Run with data quality checks.
    """
    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.80,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0,
        check_global_data_quality=True)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    # The data in this case is so good that nothing should have changed.
    assert len(windows) == 9
Ejemplo n.º 11
0
def test_runs_without_event_information(recwarn):
    """
    Make sure it runs without event information. Some things will not work
    but it will at least not crash.
    """
    config = pyflex.Config(min_period=50.0,
                           max_period=150.0,
                           stalta_waterlevel=0.08,
                           tshift_acceptance_level=15.0,
                           dlna_acceptance_level=1.0,
                           cc_acceptance_level=0.80,
                           c_0=0.7,
                           c_1=4.0,
                           c_2=0.0,
                           c_3a=1.0,
                           c_3b=2.0,
                           c_4a=3.0,
                           c_4b=10.0)

    obs = OBS_DATA[0].copy()
    syn = SYNTH_DATA[0].copy()

    # Remove the sac header information.
    del obs.stats.sac
    del syn.stats.sac

    recwarn.clear()
    windows = pyflex.select_windows(obs, syn, config)

    # This will actually result in a bunch more windows as before. So it
    # is always a good idea to specify the event and station information!
    assert len(windows) == 12

    assert len(recwarn.list) == 1
    w = recwarn.list[0]
    assert w.category == pyflex.PyflexWarning
    assert "Event and/or station information is not available".lower() in \
        str(w.message).lower()

    # No phases should be attached as they cannot be calculated.
    phases = []
    for win in windows:
        phases.extend(win.phase_arrivals)

    assert phases == []
Ejemplo n.º 12
0
def test_window_selection():
    """
    This WILL need to be adjusted if any part of the algorithm changes!

    The settings for this test are more or less the same as for the test
    data example in the original FLEXWIN package.
    """
    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.80,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert len(windows) == 9

    lefties = np.array([_i.left for _i in windows])
    righties = np.array([_i.right for _i in windows])

    np.testing.assert_allclose(
        lefties,
        np.array([1551, 2221, 2709, 2960, 3353, 3609, 3983, 4715, 4962]),
        atol=3)
    np.testing.assert_allclose(
        righties,
        np.array([1985, 2709, 2960, 3172, 3609, 3920, 4442, 4962, 5207]),
        atol=3)

    np.testing.assert_allclose(
        np.array([_i.max_cc_value for _i in windows]),
        np.array([0.95740629, 0.96646804, 0.96335716, 0.98249547, 0.96838754,
                  0.88501979, 0.82529382, 0.92953344, 0.92880873]), rtol=1E-2)

    assert [_i.cc_shift for _i in windows] == [-3, 0, -5, -5, -6, 4, -9, -1, 7]
    np.testing.assert_allclose(
        np.array([_i.dlnA for _i in windows]),
        np.array([0.07469, 0.12808, -0.19277, 0.185563, 0.093674, -0.118859,
                  -0.638657, 0.25942, 0.106571]), rtol=1E-2)

    # Assert the phases of the first window.
    assert sorted([_i["name"] for _i in windows[0].phase_arrivals]) == \
        ['PKIKP', 'PKIKS', 'PKiKP', 'PP', 'SKIKP', 'SKiKP', 'pPKIKP', 'pPKiKP',
         'sPKIKP', 'sPKiKP']
Ejemplo n.º 13
0
def test_settings_arrays_as_config_values():
    """
    Tests that arrays can be set as config values.
    """
    npts = OBS_DATA[0].stats.npts
    stalta_waterlevel = 0.08 * np.ones(npts)
    tshift_acceptance_level = 15.0 * np.ones(npts)
    dlna_acceptance_level = 1.0 * np.ones(npts)
    cc_acceptance_level = 0.80 * np.ones(npts)
    s2n_limit = 1.5 * np.ones(npts)
    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=stalta_waterlevel,
        tshift_acceptance_level=tshift_acceptance_level,
        dlna_acceptance_level=dlna_acceptance_level,
        cc_acceptance_level=cc_acceptance_level, s2n_limit=s2n_limit,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0)

    windows = pyflex.select_windows(OBS_DATA, SYNTH_DATA, config)
    assert len(windows) == 9
Ejemplo n.º 14
0
def test_runs_without_event_information(recwarn):
    """
    Make sure it runs without event information. Some things will not work
    but it will at least not crash.
    """
    config = pyflex.Config(
        min_period=50.0, max_period=150.0,
        stalta_waterlevel=0.08, tshift_acceptance_level=15.0,
        dlna_acceptance_level=1.0, cc_acceptance_level=0.80,
        c_0=0.7, c_1=4.0, c_2=0.0, c_3a=1.0, c_3b=2.0, c_4a=3.0, c_4b=10.0)

    obs = OBS_DATA[0].copy()
    syn = SYNTH_DATA[0].copy()

    # Remove the sac header information.
    del obs.stats.sac
    del syn.stats.sac

    recwarn.clear()
    windows = pyflex.select_windows(obs, syn, config)

    # This will actually result in a bunch more windows as before. So it
    # is always a good idea to specify the event and station information!
    assert len(windows) == 12

    assert len(recwarn.list) == 1
    w = recwarn.list[0]
    assert w.category == pyflex.PyflexWarning
    assert "Event and/or station information is not available".lower() in \
        str(w.message).lower()

    # No phases should be attached as they cannot be calculated.
    phases = []
    for win in windows:
        phases.extend(win.phase_arrivals)

    assert phases == []
Ejemplo n.º 15
0
        sta_info = ds_synt.waveforms[sta].StationXML
        synt_tr = ds_synt.waveforms[sta][synt_tag].select(
            channel="*"+channel)[0]
        obsd_tr = ds_obsd.waveforms[sta][obsd_tag].select(
            channel="*" + channel)[0]

        # Filter both traces
        synt_tr.detrend('demean').detrend('linear').taper(0.05).filter(
            type='bandpass', freqmin=1/80, freqmax=1/40, zerophase=True)
        obsd_tr.detrend('demean').detrend('linear').taper(0.05).filter(
            type='bandpass', freqmin=1/80, freqmax=1/40, zerophase=True)

        new_conf = user_func(conf, sta_info, origin, obsd_tr, synt_tr)
        try:
            wins = pyflex.select_windows(obsd_tr, synt_tr, new_conf,
                                         event=event, station=sta_info,
                                         plot=False)

            if wins:
                num_channels += 1
                text.append(obsd_tr.id)
                text.append(synt_tr.id)
                text.append(len(wins))
                for win in wins:
                    text.append('%.2f %.2f' % (win.left * obsd_tr.stats.delta,
                                               win.right*obsd_tr.stats.delta))
        except Exception:
            continue

text.insert(0, num_channels)
with open('windows.txt', 'w') as f:
Ejemplo n.º 16
0
            # preprocessing
            obs_st.detrend("linear")
            obs_st.taper(max_percentage=0.05, type="hann")
            obs_st.filter("lowpass", freq=0.25, corners=2, zerophase=True)

            syn_st.detrend("linear")
            syn_st.taper(max_percentage=0.05, type="hann")
            syn_st.filter("lowpass", freq=0.25, corners=2, zerophase=True)

            # user defined configuration
            config = pyflex.Config(min_period=1.0,
                                   max_period=100.0,
                                   stalta_waterlevel=0.08,
                                   tshift_acceptance_level=15.0,
                                   dlna_acceptance_level=1.0,
                                   cc_acceptance_level=0.50,
                                   c_0=0.7,
                                   c_1=4.0,
                                   c_2=0.0,
                                   c_3a=1.0,
                                   c_3b=2.0,
                                   c_4a=3.0,
                                   c_4b=10.0)

            windows = pyflex.select_windows(obs_st, syn_st, config, plot=True)

            pprint.pprint(windows)
#win = windows[0]
#   print("Relative times in seconds: %s - %s" % (win.relative_starttime,
#                                         win.relative_endtime))