Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace interpolate with resample #313

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
776 changes: 240 additions & 536 deletions src/noisepy/monitoring/monitoring_methods.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/noisepy/seis/noise_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def preprocess_raw(
# make downsampling if needed
if abs(samp_freq - sps) > 1e-4:
# downsampling here
st.interpolate(samp_freq, method="weighted_average_slopes")
# st.interpolate(samp_freq, method="weighted_average_slopes")
st.resample(samp_freq)
delta = st[0].stats.delta

# when starttimes are between sampling points
Expand Down
53 changes: 10 additions & 43 deletions tests/test_monitoring_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
dtw_dvv,
mwcs_dvv,
stretching,
stretching_vect,
wtdtw_allfreq,
wcc_dvv,
wtdtw_dvv,
wts_allfreq,
wts_dvv,
wxs_dvv,
)
Expand All @@ -17,15 +15,15 @@
(
np.sin(np.arange(100) / 10),
np.sin(np.arange(100) / 10.1),
{"twin": [0, 100], "freq": [0.01, 0.49], "dt": 1},
{"twin": [0, 100], "t": np.arange(101), "freq": [0.01, 0.49], "dt": 1},
)
]

data2 = [
(
np.sin(np.arange(100) / 10),
np.sin(np.arange(100) / 10.1),
{"twin": [0, 99], "t": np.array([0, 100]), "freq": [0.01, 0.49], "dt": 1},
{"twin": [0, 99], "t": np.array(np.arange(100)), "freq": [0.01, 0.49], "dt": 1},
)
]

Expand All @@ -41,13 +39,11 @@ def test_stretching(d1: np.ndarray, d2: np.ndarray, param: dict):


@pytest.mark.parametrize("d1,d2,param", data)
def test_stretching_vect(d1: np.ndarray, d2: np.ndarray, param: dict):
dv_range = 0.05
nbtrial = 50
dv, error, cc, _ = stretching_vect(d1, d2, dv_range, nbtrial, param)
assert np.isclose(dv, -1, rtol=0.2)
assert np.isclose(error, 0.0, atol=1e-2)
assert np.isclose(cc, 1.0, atol=1e-1)
def test_wcc_dvv(d1: np.ndarray, d2: np.ndarray, param: dict):
moving_window_length = 10.0
window_slide_step = 1.0
dv, error = wcc_dvv(d1, d2, moving_window_length, window_slide_step, param)
assert dv < 0


@pytest.mark.parametrize("d1,d2,param", data)
Expand All @@ -67,7 +63,7 @@ def test_mwcs_dvv(d1: np.ndarray, d2: np.ndarray, param: dict):
assert dv < 0


@pytest.mark.parametrize("d1,d2,param", data)
@pytest.mark.parametrize("d1,d2,param", data2)
@pytest.mark.parametrize("allfreq", [True, False])
def test_wxs_dvv(d1: np.ndarray, d2: np.ndarray, param: dict, allfreq: bool):
if allfreq:
Expand All @@ -78,7 +74,7 @@ def test_wxs_dvv(d1: np.ndarray, d2: np.ndarray, param: dict, allfreq: bool):
assert np.all(dvv < 0)


@pytest.mark.parametrize("d1,d2,param", data)
@pytest.mark.parametrize("d1,d2,param", data2)
@pytest.mark.parametrize("allfreq", [True, False])
@pytest.mark.parametrize("normalize", [True, False])
def test_wts_dvv(d1: np.ndarray, d2: np.ndarray, param: dict, allfreq: bool, normalize: bool):
Expand All @@ -92,35 +88,6 @@ def test_wts_dvv(d1: np.ndarray, d2: np.ndarray, param: dict, allfreq: bool, nor
assert np.all(dvv < 0)


@pytest.mark.parametrize("d1,d2,param", data)
@pytest.mark.parametrize("allfreq", [True, False])
@pytest.mark.parametrize("normalize", [True, False])
def test_wts_allfreq(d1: np.ndarray, d2: np.ndarray, param: dict, allfreq: bool, normalize: bool):
dv_range = 0.05
nbtrial = 50
if allfreq:
_, dvv, err = wts_allfreq(d1, d2, allfreq, param, dv_range, nbtrial, normalize=normalize)
else:
dvv, err = wts_allfreq(d1, d2, allfreq, param, dv_range, nbtrial, normalize=normalize)
assert np.all(np.isclose(err, 0, atol=1e-1))
assert np.all(dvv < 0)


@pytest.mark.parametrize("d1,d2,param", data)
@pytest.mark.parametrize("allfreq", [True, False])
@pytest.mark.parametrize("normalize", [True, False])
def test_wtdtw_allfreq(d1: np.ndarray, d2: np.ndarray, param: dict, allfreq: bool, normalize: bool):
maxlag = 5
b = 10
direction = 1
if allfreq:
_, dvv, err = wtdtw_allfreq(d1, d2, allfreq, param, maxlag, b, direction, normalize=normalize)
else:
dvv, err = wtdtw_allfreq(d1, d2, allfreq, param, maxlag, b, direction, normalize=normalize)
assert np.all(np.isclose(err, 0, atol=2e-1))
assert np.all(np.isclose(dvv, 0, atol=2e0))


@pytest.mark.parametrize("d1,d2,param", data2)
@pytest.mark.parametrize("allfreq", [True, False])
@pytest.mark.parametrize("normalize", [True, False])
Expand Down
35 changes: 3 additions & 32 deletions tests/test_stretching.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
import pytest
from obspy.signal.invsim import cosine_taper

from noisepy.monitoring.monitoring_methods import (
mwcs_dvv,
stretching,
stretching_vect,
wcc_dvv,
)
from noisepy.monitoring.monitoring_methods import mwcs_dvv, stretching, wcc_dvv

# This short script is intended as a test for the stretching routine
# it takes a generic sine curve with known stretching factor and ensures
Expand All @@ -26,7 +21,9 @@ def test_stretching():
t_stretch = np.linspace(0.0, 10.0, 2500)
stretched_signal = np.interp(t, t_stretch, original_signal)

# {"twin": [0, 100], "t": np.arange(101), "freq": [0.01, 0.49], "dt": 1},
para = {}
para["t"] = np.arange(2501)
para["dt"] = 1.0 / 250.0
para["twin"] = [0.0, 10.0]
para["freq"] = [9.9, 10.1]
Expand All @@ -37,26 +34,6 @@ def test_stretching():
assert dvv + 0.5 < para["dt"] # assert result is -0.5%


def test_stretching_vect():
t = np.linspace(0.0, 9.95, 2500) # 0.5 % perturbation
original_signal = np.sin(t * 10.0) * cosine_taper(2500, p=0.75)

t_stretch = np.linspace(0.0, 10.0, 2500)
stretched_signal = np.interp(t, t_stretch, original_signal)

para = {}
para["dt"] = 1.0 / 250.0
para["twin"] = [0.0, 10.0]
para["freq"] = [9.9, 10.1]

dvv, error, cc, cdp = stretching_vect(
ref=original_signal, cur=stretched_signal, dv_range=0.05, nbtrial=100, para=para
)

assert pytest.approx(cc) == 1.0
assert dvv + 0.5 < para["dt"] # assert result is -0.5%


def test_wcc_dvv():
t = np.linspace(0.0, 9.95, 2500) # 0.5 % perturbation
original_signal = np.sin(t * 10.0) * cosine_taper(2500, p=0.75)
Expand Down Expand Up @@ -121,12 +98,6 @@ def test_mwcs_dvv():
test_stretching()
print("Done stretching, no errors, %4.2fs." % (time.time() - t))

print("Running stretching using numpy...")
t = time.time()
for i in range(100):
test_stretching_vect()
print("Done stretching, no errors, %4.2fs." % (time.time() - t))

t = time.time()
for i in range(100):
test_mwcs_dvv()
Expand Down
2 changes: 2 additions & 0 deletions tutorials/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ logo: ../docs_old/figures/logo.png
# See https://jupyterbook.org/content/execute.html
execute:
execute_notebooks: force
exclude_patterns:
- noisepy_aws_batch.ipynb
timeout: 360

only_build_toc_files: true
Expand Down
6 changes: 2 additions & 4 deletions tutorials/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
# Learn more at https://jupyterbook.org/customize/toc.html

format: jb-book
root: noise_configuration.md
root: noisepy_configuration.md
chapters:
- file: get_started.ipynb
- file: noisepy_datastore.ipynb
- file: noisepy_scedc_tutorial.ipynb
- file: noisepy_ncedc_tutorial.ipynb
- file: noisepy_compositestore_tutorial.ipynb
- file: CLI.md
- file: cloud/checklist.md
- file: cloud/aws-ec2.md
- file: cloud/aws-batch.md
- file: cloud/noisepy_aws_batch.ipynb
28 changes: 28 additions & 0 deletions tutorials/cloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Running NoisePy with AWS

## EC2 and Jupyter Lab
Please refer to [SCOPED HPS Book](https://seisscoped.org/HPS-book/chapters/cloud/AWS_101.html) for full detailed instruction on launching an AWS EC2 instance and/or running the notebooks within a containerized environment.

## Submit Batch Job
For large job load, please refer to the [notebook tutorial](./noisepy_aws_batch.ipynb) for more instruction.

## Command Line Interface
You may create or edit the [config.yml](../config.yml) file with appropriate parameters. The cross-correlation function is written to the `ccf_path`.

```bash
noisepy cross_correlate --format numpy --raw_data_path s3://scedc-pds/continuous_waveforms/ \
--xml_path s3://scedc-pds/FDSNstationXML/CI/ \
--ccf_path s3://<S3_BUCKET>/<CC_PATH> \
--stations=SBC,RIO,DEV \
--start=2022-02-02 \
--end=2022-02-03
```

This toy problem gathers the all the cross-correlations calculated and stack them into the NumPy format on the S3 bucket, specificed by the `stack_path`.

```bash
noisepy stack \
--format numpy \
--ccf_path s3://<S3_BUCKET>/<CC_PATH> \
--stack_path s3://<S3_BUCKET>/<STACK_PATH> \
```
63 changes: 0 additions & 63 deletions tutorials/cloud/aws-batch.md

This file was deleted.

100 changes: 0 additions & 100 deletions tutorials/cloud/aws-ec2.md

This file was deleted.

Loading
Loading