Skip to content

Commit ab150b0

Browse files
committedFeb 24, 2025
add range var max and reindex to compute_MVBS parameters and drop flox maximum version
1 parent 73972ac commit ab150b0

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed
 

‎echopype/commongrid/api.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import logging
6-
from typing import Literal
6+
from typing import Literal, Union
77

88
import numpy as np
99
import pandas as pd
@@ -32,9 +32,11 @@ def compute_MVBS(
3232
range_var: Literal["echo_range", "depth"] = "echo_range",
3333
range_bin: str = "20m",
3434
ping_time_bin: str = "20s",
35-
method="map-reduce",
36-
skipna=True,
35+
method: str = "map-reduce",
36+
reindex: bool = False,
37+
skipna: bool = True,
3738
closed: Literal["left", "right"] = "left",
39+
range_var_max: Union[int, float] = None,
3840
**flox_kwargs,
3941
):
4042
"""
@@ -63,11 +65,21 @@ def compute_MVBS(
6365
The flox strategy for reduction of dask arrays only.
6466
See flox `documentation <https://flox.readthedocs.io/en/latest/implementation.html>`_
6567
for more details.
68+
reindex: bool, default False
69+
If False, reindex after the blockwise stage. If True, reindex at the blockwise stage.
70+
Generally, `reindex=False` results in less memory at the cost of computation speed.
71+
Can only be used when method='map-reduce'.
72+
See flox `documentation <https://flox.readthedocs.io/en/latest/implementation.html>`_
73+
for more details.
6674
skipna: bool, default True
6775
If true, the mean operation skips NaN values.
6876
Else, the mean operation includes NaN values.
6977
closed: {'left', 'right'}, default 'left'
7078
Which side of bin interval is closed.
79+
range_var_max: Union[int, float], default None
80+
Range variable maximum. Can be true range variable maximum or the maximum depth the
81+
user wishes to regrid to. If known, users can pass in range variable maximum to
82+
ensure that `compute_MVBS` can lazily run without any computation.
7183
**flox_kwargs
7284
Additional keyword arguments to be passed
7385
to flox reduction function.
@@ -76,6 +88,8 @@ def compute_MVBS(
7688
-------
7789
A dataset containing bin-averaged Sv
7890
"""
91+
if method != "map-reduce" and reindex is not None:
92+
raise ValueError(f"Passing in reindex={reindex} is only allowed when method='map_reduce'.")
7993

8094
# Setup and validate
8195
# * Sv dataset must contain specified range_var
@@ -86,10 +100,11 @@ def compute_MVBS(
86100
if not isinstance(ping_time_bin, str):
87101
raise TypeError("ping_time_bin must be a string")
88102

89-
# create bin information for echo_range
90-
# this computes the echo range max since there might NaNs in the data
91-
echo_range_max = ds_Sv[range_var].max()
92-
range_interval = np.arange(0, echo_range_max + range_bin, range_bin)
103+
# Create bin information for the range variable
104+
if range_var_max is None:
105+
# This computes the range variable max since there might NaNs in the data
106+
range_var_max = ds_Sv[range_var].max(skipna=True)
107+
range_interval = np.arange(0, range_var_max + range_bin, range_bin)
93108

94109
# create bin information needed for ping_time
95110
d_index = (
@@ -109,6 +124,7 @@ def compute_MVBS(
109124
ping_interval,
110125
range_var=range_var,
111126
method=method,
127+
reindex=reindex,
112128
skipna=skipna,
113129
**flox_kwargs,
114130
)

‎echopype/commongrid/utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def compute_raw_MVBS(
2020
ping_interval: Union[pd.IntervalIndex, np.ndarray],
2121
range_var: Literal["echo_range", "depth"] = "echo_range",
2222
method="map-reduce",
23+
reindex=False,
2324
skipna=True,
2425
**flox_kwargs,
2526
):
@@ -46,6 +47,12 @@ def compute_raw_MVBS(
4647
The flox strategy for reduction of dask arrays only.
4748
See flox `documentation <https://flox.readthedocs.io/en/latest/implementation.html>`_
4849
for more details.
50+
reindex: bool, default False
51+
If False, reindex after the blockwise stage. If True, reindex at the blockwise stage.
52+
Generally, `reindex=False` results in less memory at the cost of computation speed.
53+
Can only be used when method='map-reduce'.
54+
See flox `documentation <https://flox.readthedocs.io/en/latest/implementation.html>`_
55+
for more details.
4956
skipna: bool, default True
5057
If true, the mean operation skips NaN values.
5158
Else, the mean operation includes NaN values.
@@ -69,6 +76,7 @@ def compute_raw_MVBS(
6976
x_var=x_var,
7077
range_var=range_var,
7178
method=method,
79+
reindex=reindex,
7280
func="nanmean" if skipna else "mean",
7381
skipna=skipna,
7482
**flox_kwargs,
@@ -495,6 +503,7 @@ def _groupby_x_along_channels(
495503
x_var: Literal["ping_time", "distance_nmi"] = "ping_time",
496504
range_var: Literal["echo_range", "depth"] = "echo_range",
497505
method: str = "map-reduce",
506+
reindex: bool = False,
498507
func: str = "nanmean",
499508
skipna: bool = True,
500509
**flox_kwargs,
@@ -534,6 +543,12 @@ def _groupby_x_along_channels(
534543
The flox strategy for reduction of dask arrays only.
535544
See flox `documentation <https://flox.readthedocs.io/en/latest/implementation.html>`_
536545
for more details.
546+
reindex: bool, default False
547+
If False, reindex after the blockwise stage. If True, reindex at the blockwise stage.
548+
Generally, `reindex=False` results in less memory at the cost of computation speed.
549+
Can only be used when method='map-reduce'.
550+
See flox `documentation <https://flox.readthedocs.io/en/latest/implementation.html>`_
551+
for more details.
537552
func: str, default 'nanmean'
538553
The aggregation function used for reducing the data array.
539554
By default, 'nanmean' is used. Other options can be found in the flox `documentation
@@ -593,6 +608,7 @@ def _groupby_x_along_channels(
593608
expected_groups=(None, x_interval, range_interval),
594609
isbin=[False, True, True],
595610
method=method,
611+
reindex=reindex,
596612
func=func,
597613
skipna=skipna,
598614
**flox_kwargs,

‎requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ aiohttp
22
bottleneck
33
dask[array,distributed]
44
dask-image
5-
flox>=0.7.2,<1.0.0
5+
flox>=0.7.2
66
fsspec
77
geopy
88
jinja2

0 commit comments

Comments
 (0)