forked from yang-song/score_sde_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a CLI for filtering samples by time period
- Loading branch information
1 parent
684b018
commit e36cd31
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import logging | ||
import os | ||
from pathlib import Path | ||
import typer | ||
import xarray as xr | ||
|
||
from mlde_utils import samples_path, samples_glob, TIME_PERIODS | ||
|
||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(levelname)s - %(filename)s - %(asctime)s - %(message)s", | ||
) | ||
logger = logging.getLogger() | ||
logger.setLevel("INFO") | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.callback() | ||
def callback(): | ||
pass | ||
|
||
|
||
@app.command() | ||
def filter( | ||
workdir: Path, | ||
dataset: str = typer.Option(...), | ||
time_period: str = typer.Option(...), | ||
checkpoint: str = typer.Option(...), | ||
input_xfm: str = "stan", | ||
split: str = "val", | ||
ensemble_member: str = typer.Option(...), | ||
): | ||
"""Filter a set of samples based on time period.""" | ||
|
||
new_dataset = f"{dataset}-{time_period}" | ||
filtered_samples_dirpath = samples_path( | ||
workdir, | ||
checkpoint=checkpoint, | ||
input_xfm=input_xfm, | ||
dataset=new_dataset, | ||
split=split, | ||
ensemble_member=ensemble_member, | ||
) | ||
os.makedirs(filtered_samples_dirpath, exist_ok=False) | ||
|
||
for sample_filepath in samples_glob( | ||
samples_path( | ||
workdir, | ||
checkpoint=checkpoint, | ||
input_xfm=input_xfm, | ||
dataset=dataset, | ||
split=split, | ||
ensemble_member=ensemble_member, | ||
) | ||
): | ||
samples_ds = xr.open_dataset(sample_filepath) | ||
|
||
filtered_samples_filepath = filtered_samples_dirpath / sample_filepath.name | ||
|
||
samples_ds.sel(time=slice(*TIME_PERIODS[time_period])).to_netcdf( | ||
filtered_samples_filepath | ||
) |