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

Add tap point for spectrograms #374

Merged
merged 1 commit into from
Mar 1, 2025
Merged
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
15 changes: 11 additions & 4 deletions aurora/pipelines/process_mth5.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
)
from loguru import logger
from mth5.helpers import close_open_files
from mth5.timeseries.spectre import Spectrogram
from typing import Optional, Union

import aurora.config.metadata.processing
Expand Down Expand Up @@ -137,7 +138,7 @@ def make_stft_objects(processing_config, i_dec_level, run_obj, run_xrds, units="

"""
stft_config = processing_config.get_decimation_level(i_dec_level)
stft_obj = run_ts_to_stft(stft_config, run_xrds)
spectrogram = run_ts_to_stft(stft_config, run_xrds)
run_id = run_obj.metadata.id
if run_obj.station_metadata.id == processing_config.stations.local.id:
scale_factors = processing_config.stations.local.run_dict[
Expand All @@ -149,11 +150,12 @@ def make_stft_objects(processing_config, i_dec_level, run_obj, run_xrds, units="
)

stft_obj = calibrate_stft_obj(
stft_obj,
spectrogram.dataset,
run_obj,
units=units,
channel_scale_factors=scale_factors,
)

return stft_obj


Expand Down Expand Up @@ -433,10 +435,11 @@ def save_fourier_coefficients(
return


def get_spectrogams(tfk: TransferFunctionKernel, i_dec_level, units="MT"):
def get_spectrograms(tfk: TransferFunctionKernel, i_dec_level, units="MT"):
"""
Given a decimation level id, loads a dictianary of all spectragrams from information in tfk.
TODO: Make this a method of TFK
TODO: Modify this to be able to yield Spectrogram objects.

Parameters
----------
Expand Down Expand Up @@ -470,6 +473,7 @@ def get_spectrogams(tfk: TransferFunctionKernel, i_dec_level, units="MT"):
run_obj = row.mth5_obj.from_reference(row.run_hdf5_reference)
if row.fc:
stft_obj = load_stft_obj_from_mth5(i_dec_level, row, run_obj)
# TODO: Cast stft_obj to a Spectrogram here
stfts = append_chunk_to_stfts(stfts, stft_obj, row.remote)
continue

Expand All @@ -485,10 +489,13 @@ def get_spectrogams(tfk: TransferFunctionKernel, i_dec_level, units="MT"):
run_xrds,
units,
)
# TODO: Cast stft_obj to a Spectrogram here or in make_stft_objects

# Pack FCs into h5
dec_level_config = tfk.config.decimations[i_dec_level]
save_fourier_coefficients(dec_level_config, row, run_obj, stft_obj)
# TODO: 1st pass, cast stft_obj to a Spectrogram here

stfts = append_chunk_to_stfts(stfts, stft_obj, row.remote)

return stfts
Expand Down Expand Up @@ -550,7 +557,7 @@ def process_mth5_legacy(
tfk.update_dataset_df(i_dec_level)
tfk.apply_clock_zero(dec_level_config)

stfts = get_spectrogams(tfk, i_dec_level, units=units)
stfts = get_spectrograms(tfk, i_dec_level, units=units)

local_merged_stft_obj, remote_merged_stft_obj = merge_stfts(stfts, tfk)

Expand Down
8 changes: 5 additions & 3 deletions aurora/pipelines/time_series_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mth5.groups import RunGroup
from mth5.timeseries.spectre.prewhitening import apply_prewhitening
from mth5.timeseries.spectre.prewhitening import apply_recoloring
import mth5.timeseries.spectre as spectre
from typing import Literal, Optional, Union


Expand Down Expand Up @@ -98,7 +99,7 @@ def nan_to_mean(xrds: xr.Dataset) -> xr.Dataset:

def run_ts_to_stft(
decimation_obj: AuroraDecimationLevel, run_xrds_orig: xr.Dataset
) -> xr.Dataset:
) -> spectre.Spectrogram:
"""
Converts a runts object into a time series of Fourier coefficients.
Similar to run_ts_to_stft_scipy, but in this implementation operations on individual
Expand All @@ -110,7 +111,7 @@ def run_ts_to_stft(
----------
decimation_obj : AuroraDecimationLevel
Information about how the decimation level is to be processed
run_ts : xarray.core.dataset.Dataset
run_xrds_orig: xarray.core.dataset.Dataset
normally extracted from mth5.RunTS

Returns
Expand Down Expand Up @@ -146,7 +147,8 @@ def run_ts_to_stft(
if decimation_obj.stft.recoloring:
stft_obj = apply_recoloring(decimation_obj.stft.prewhitening_type, stft_obj)

return stft_obj
spectrogram = spectre.Spectrogram(dataset=stft_obj)
return spectrogram


def calibrate_stft_obj(
Expand Down
8 changes: 5 additions & 3 deletions tests/synthetic/test_stft_methods_agree.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ def test_stft_methods_agree():
local_run_xrts = prototype_decimate(dec_config.decimation, local_run_xrts)

dec_config.stft.per_window_detrend_type = "constant"
local_stft_obj = run_ts_to_stft(dec_config, local_run_xrts)
local_stft_obj2 = run_ts_to_stft_scipy(dec_config, local_run_xrts)
stft_difference = local_stft_obj - local_stft_obj2
local_spectrogram = run_ts_to_stft(dec_config, local_run_xrts)
local_spectrogram2 = run_ts_to_stft_scipy(dec_config, local_run_xrts)
stft_difference = (
local_spectrogram.dataset - local_spectrogram2.dataset
) # TODO: add a "-" method to spectrogram that subtracts the datasets
stft_difference = stft_difference.to_array()

# drop dc term
Expand Down
Loading