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

refactor: change freq_norm from string to enum #248

Merged
merged 17 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 7 additions & 6 deletions script/test_whiten.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import scipy
from scipy.fftpack import next_fast_len

from noisepy.seis.noise_module import moving_ave, whiten
from src.noisepy.seis.datatypes import FreqNorm
from src.noisepy.seis.noise_module import moving_ave, whiten
anujsinha3 marked this conversation as resolved.
Show resolved Hide resolved


def whiten_original(data, fft_para):
Expand Down Expand Up @@ -62,9 +63,9 @@ def whiten_original(data, fft_para):
1j * np.angle(FFTRawSign[:, low:left])
)
# Pass band:
if freq_norm == "phase_only":
if freq_norm == FreqNorm.PHASE_ONLY:
FFTRawSign[:, left:right] = np.exp(1j * np.angle(FFTRawSign[:, left:right]))
elif freq_norm == "rma":
elif freq_norm == FreqNorm.RMA:
for ii in range(data.shape[0]):
tave = moving_ave(np.abs(FFTRawSign[ii, left:right]), smooth_N)
FFTRawSign[ii, left:right] = FFTRawSign[ii, left:right] / tave
Expand All @@ -82,9 +83,9 @@ def whiten_original(data, fft_para):
1j * np.angle(FFTRawSign[low:left])
)
# Pass band:
if freq_norm == "phase_only":
if freq_norm == FreqNorm.PHASE_ONLY:
FFTRawSign[left:right] = np.exp(1j * np.angle(FFTRawSign[left:right]))
elif freq_norm == "rma":
elif freq_norm == FreqNorm.RMA:
tave = moving_ave(np.abs(FFTRawSign[left:right]), smooth_N)
FFTRawSign[left:right] = FFTRawSign[left:right] / tave
# Right tapering:
Expand All @@ -109,7 +110,7 @@ def whiten_original(data, fft_para):
"freqmin": 0.01,
"freqmax": 0.2,
"smooth_N": 1,
"freq_norm": "phase_only",
"freq_norm": FreqNorm.PHASE_ONLY,
}

# 1 D case
Expand Down
8 changes: 7 additions & 1 deletion src/noisepy/seis/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ class StackMethod(Enum):
ALL = "all"


class FreqNorm(Enum):
RMA = "rma"
NO = "no"
PHASE_ONLY = "phase_only"


class ConfigParameters(BaseModel):
model_config = ConfigDict(validate_default=True)

Expand Down Expand Up @@ -139,7 +145,7 @@ class ConfigParameters(BaseModel):
freqmin: float = Field(default=0.05)
freqmax: float = Field(default=2.0)
freq_norm: str = Field(
anujsinha3 marked this conversation as resolved.
Show resolved Hide resolved
default="rma", description="choose between 'rma' for a soft whitenning or 'no' for no whitening"
default=FreqNorm.RMA.value, description="choose between 'rma' for a soft whitenning or 'no' for no whitening"
)
# TODO: change "no"for "None", and add "one_bit"as an option
# TODO: change time_norm option from "no"to "None"
Expand Down
4 changes: 2 additions & 2 deletions src/noisepy/seis/noise_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from scipy.fftpack import next_fast_len
from scipy.signal import hilbert

from .datatypes import ChannelData, ConfigParameters, StackMethod
from .datatypes import ChannelData, ConfigParameters, FreqNorm, StackMethod

logger = logging.getLogger(__name__)
"""
Expand Down Expand Up @@ -559,7 +559,7 @@ def noise_processing(fft_para: ConfigParameters, dataS):
white = dataS

# -----to whiten or not------
if fft_para.freq_norm != "no":
if fft_para.freq_norm != FreqNorm.NO:
source_white = whiten(white, fft_para) # whiten and return FFT
else:
Nfft = int(next_fast_len(int(dataS.shape[1])))
Expand Down
11 changes: 6 additions & 5 deletions tests/test_whiten.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from scipy.fftpack import next_fast_len

from noisepy.seis.correlate import ConfigParameters
from noisepy.seis.datatypes import FreqNorm
from noisepy.seis.noise_module import moving_ave, whiten


Expand Down Expand Up @@ -56,9 +57,9 @@ def whiten_original(data, fft_para: ConfigParameters):
1j * np.angle(FFTRawSign[:, low:left])
)
# Pass band:
if fft_para.freq_norm == "phase_only":
if fft_para.freq_norm == FreqNorm.PHASE_ONLY:
FFTRawSign[:, left:right] = np.exp(1j * np.angle(FFTRawSign[:, left:right]))
elif fft_para.freq_norm == "rma":
elif fft_para.freq_norm == FreqNorm.RMA:
for ii in range(data.shape[0]):
tave = moving_ave(np.abs(FFTRawSign[ii, left:right]), fft_para.smooth_N)
FFTRawSign[ii, left:right] = FFTRawSign[ii, left:right] / tave
Expand All @@ -76,9 +77,9 @@ def whiten_original(data, fft_para: ConfigParameters):
1j * np.angle(FFTRawSign[low:left])
)
# Pass band:
if fft_para.freq_norm == "phase_only":
if fft_para.freq_norm == FreqNorm.PHASE_ONLY:
FFTRawSign[left:right] = np.exp(1j * np.angle(FFTRawSign[left:right]))
elif fft_para.freq_norm == "rma":
elif fft_para.freq_norm == FreqNorm.RMA:
tave = moving_ave(np.abs(FFTRawSign[left:right]), fft_para.smooth_N)
FFTRawSign[left:right] = FFTRawSign[left:right] / tave
# Right tapering:
Expand All @@ -103,7 +104,7 @@ def whiten_original(data, fft_para: ConfigParameters):
fft_para.freqmin = 0.01
fft_para.freqmax = 0.2
fft_para.smooth_N = 1
fft_para.freq_norm = "phase_only"
fft_para.freq_norm = FreqNorm.PHASE_ONLY


def whiten1d():
Expand Down
6 changes: 3 additions & 3 deletions tutorials/get_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"source": [
"from noisepy.seis import download, cross_correlate, stack, plotting_modules, __version__\n",
"from noisepy.seis.asdfstore import ASDFRawDataStore, ASDFCCStore, ASDFStackStore\n",
"from noisepy.seis.datatypes import ConfigParameters\n",
"from noisepy.seis.datatypes import ConfigParameters, FreqNorm\n",
"from dateutil.parser import isoparse\n",
"import os\n",
"print(f\"Using NoisePy version {__version__}\")\n",
Expand Down Expand Up @@ -119,7 +119,7 @@
"config.max_over_std = 10 # threshold to remove window of bad signals: set it to 10*9 if prefer not to remove them\n",
"\n",
"# TEMPORAL and SPECTRAL NORMALISATION\n",
"config.freq_norm= \"rma\" # choose between \"rma\" for a soft whitenning or \"no\" for no whitening. Pure whitening is not implemented correctly at this point.\n",
"config.freq_norm= FreqNorm.RMA # choose between \"rma\" for a soft whitenning or \"no\" for no whitening. Pure whitening is not implemented correctly at this point.\n",
"config.smoothspect_N = 10 # moving window length to smooth spectrum amplitude (points)\n",
" # here, choose smoothspect_N for the case of a strict whitening (e.g., phase_only)\n",
"\n",
Expand Down Expand Up @@ -248,7 +248,7 @@
},
"outputs": [],
"source": [
"config.freq_norm = \"rma\"\n",
"config.freq_norm = FreqNorm.RMA\n",
"raw_store = ASDFRawDataStore(raw_data_path) # Store for reading raw data\n",
"cc_store = ASDFCCStore(cc_data_path) # Store for writing CC data\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions tutorials/noisepy_scedc_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"from noisepy.seis import cross_correlate, stack, plotting_modules, __version__ # noisepy core functions\n",
"from noisepy.seis.asdfstore import ASDFCCStore, ASDFStackStore # Object to store ASDF data within noisepy\n",
"from noisepy.seis.scedc_s3store import SCEDCS3DataStore, channel_filter # Object to query SCEDC data from on S3\n",
"from noisepy.seis.datatypes import ConfigParameters, StackMethod # Main configuration object\n",
"from noisepy.seis.datatypes import ConfigParameters, FreqNorm, StackMethod # Main configuration object\n",
"from noisepy.seis.channelcatalog import XMLStationChannelCatalog # Required stationXML handling object\n",
"import os\n",
"from datetime import datetime\n",
Expand Down Expand Up @@ -201,7 +201,7 @@
"config.max_over_std = 10 # threshold to remove window of bad signals: set it to 10*9 if prefer not to remove them\n",
"\n",
"################### SPECTRAL NORMALIZATION ############\n",
"config.freq_norm= \"rma\" # choose between \"rma\" for a soft whitening or \"no\" for no whitening. Pure whitening is not implemented correctly at this point.\n",
"config.freq_norm= FreqNorm.RMA # choose between \"rma\" for a soft whitening or \"no\" for no whitening. Pure whitening is not implemented correctly at this point.\n",
"config.smoothspect_N = 10 # moving window length to smooth spectrum amplitude (points)\n",
" # here, choose smoothspect_N for the case of a strict whitening (e.g., phase_only)\n",
"\n",
Expand Down
Loading