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

--name_suffix added to WB2/compute_quantiles.py #236

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion scripts/compute_quantiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
default=[],
help='Comma delimited list of dimensions to reduce over.',
)
NAME_SUFFIX = flags.DEFINE_string(
'name_suffix',
'',
help=(
'Suffix to add to variable names. If you add "_quantile", then the'
' output can be used as climatology for Weatherbench 2 thresholded'
' scores.'
),
)
SKIPNA = flags.DEFINE_boolean(
'skipna',
False,
Expand Down Expand Up @@ -170,7 +179,8 @@ def _evaluate_chunk_core(chunk: xr.Dataset) -> xr.Dataset:
raise ValueError(
f'Expected all quantiles to be in [0, 1]. Found {quantiles=}'
)
return chunk.quantile(quantiles, dim=DIM.value, skipna=SKIPNA.value)
values = chunk.quantile(quantiles, dim=DIM.value, skipna=SKIPNA.value)
return values.rename_vars({v: v + NAME_SUFFIX.value for v in values})


def main(argv: list[str]) -> None:
Expand Down
10 changes: 8 additions & 2 deletions scripts/compute_quantiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ def _dict_to_str(self, a_dict):
output_chunks={'time': 2},
),
dict(
testcase_name='SpecifyInputOutputChunks2',
testcase_name='SpecifyInputOutputChunks2_NameSuffix',
input_chunks={'timedelta': 2},
output_chunks={'time': -1},
name_suffix='_quantile',
),
)
def test_basic_dataset(
self,
input_chunks=None,
output_chunks=None,
working_chunks=None,
name_suffix='',
):
input_chunks = input_chunks or {}
output_chunks = output_chunks or {}
Expand Down Expand Up @@ -107,6 +109,7 @@ def test_basic_dataset(
time_start='2023-01-01',
time_stop='2023-01-03',
quantiles=','.join(str(q) for q in quantiles),
name_suffix=name_suffix,
runner='DirectRunner',
):
compute_quantiles.main([])
Expand All @@ -128,7 +131,10 @@ def test_basic_dataset(
self.assertEqual(expected_output_chunks, actual_output_chunks)

xr.testing.assert_equal(
input_ds[['precip']].isel(time=slice(3)).quantile(quantiles, dim='lat'),
input_ds[['precip']]
.isel(time=slice(3))
.quantile(quantiles, dim='lat')
.rename_vars({'precip': 'precip' + name_suffix}),
output,
)

Expand Down
Loading