Skip to content

Commit cce5b99

Browse files
committed
remove optional reduction application from regroup
1 parent b6f4ee6 commit cce5b99

File tree

2 files changed

+44
-80
lines changed

2 files changed

+44
-80
lines changed

enacts/calc.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ def replace_intervals_with_points(
910910
Returns
911911
-------
912912
point_data : xr.DataArray or xr.Dataset
913-
of whicn interval dimension has been replaced by point dimension
913+
of which interval dimension has been replaced by point dimension
914914
915915
See Also
916916
--------
@@ -1051,9 +1051,8 @@ def resample_interval_to_daily(time_series, is_intensive=None, time_dim="T_bins"
10511051
return time_series
10521052

10531053

1054-
def regroup(time_series, group="1D", method=None, method_kwargs={}, time_dim="T"):
1055-
""" Regroup any type of interval-based time series to another,
1056-
according to `method`
1054+
def regroup(time_series, group="1D", time_dim="T"):
1055+
""" Regroup any type of interval-based time series to another
10571056
10581057
Parameters
10591058
----------
@@ -1065,21 +1064,13 @@ def regroup(time_series, group="1D", method=None, method_kwargs={}, time_dim="T"
10651064
or d Mmm - d Mmm. See Notes for details.
10661065
As integer or array-like[pandas.DatetimeIndex],
10671066
see xr.DataArray.groupby_bins' `bins` Parameter
1068-
method: str, optional
1069-
name of xr.core.groupby.DataArrayGroupBy's Method to apply to form the new
1070-
intervals. Default is None in which case no final reduction is applied
1071-
and returned `time_series` is a xr.core.groupby.DataArrayGroupBy object of
1072-
days grouped according to `group` .
1073-
method_kwargs: dict, optional
1074-
keyword arguments of `method` . Default is an empty dict in which case
1075-
default keywords of `method` will be applied.
10761067
time_dim : str, optional
10771068
name of interval time dimenstion, default is "T"
10781069
10791070
Returns
10801071
-------
1081-
regrouped : xr.DataArray or xr.Dataset
1082-
`time_series` regrouped to specified time intervals according to `method`
1072+
regrouped : xr.core.groupby.DataArray/DatasetGroupBy
1073+
`time_series` grouped to specified time intervals groups
10831074
10841075
See Also
10851076
--------
@@ -1232,10 +1223,7 @@ def regroup(time_series, group="1D", method=None, method_kwargs={}, time_dim="T"
12321223
assert (bins.size > 1), (
12331224
"data must span at least one full group (need 2 edges to form 1 bin)"
12341225
)
1235-
regrouped = time_series.groupby_bins(time_series[time_dim], bins, right=False)
1236-
if method is not None :
1237-
regrouped = getattr(regrouped, method)(**method_kwargs)
1238-
return regrouped
1226+
return time_series.groupby_bins(time_series[time_dim], bins, right=False)
12391227

12401228

12411229
def strftimeb2int(strftimeb):

enacts/tests/test_calc.py

Lines changed: 38 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_regroup_daily_to_7D():
105105
t = pd.date_range(start="2000-01-01", end="2000-01-28", freq="1D")
106106
values = 1 + np.arange(t.size)
107107
precip = xr.DataArray(values, coords={"T": t})
108-
precip_int = calc.regroup(precip, group="7D", method="sum")
108+
precip_int = calc.regroup(precip, group="7D").sum()
109109

110110
np.testing.assert_array_equal(
111111
precip_int, precip.resample(T="7D").sum(skipna=True, min_count=7).dropna("T")
@@ -121,8 +121,8 @@ def test_regroup_daily_to_pentad():
121121
values_leap = 1 + np.arange(t_leap.size)
122122
precip = xr.DataArray(values, coords={"T": t})
123123
precip_leap = xr.DataArray(values_leap, coords={"T": t_leap})
124-
precip_pentad = calc.regroup(precip, group="pentad", method="sum")
125-
precip_pentad_leap = calc.regroup(precip_leap, group="pentad", method="sum")
124+
precip_pentad = calc.regroup(precip, group="pentad").sum()
125+
precip_pentad_leap = calc.regroup(precip_leap, group="pentad").sum()
126126

127127
np.testing.assert_array_equal(precip_pentad.data, [
128128
15., 40., 65., 90., 115., 140., 165., 190., 215., 240., 265.,
@@ -141,7 +141,7 @@ def test_regroup_daily_to_8day():
141141
t = pd.date_range(start="2020-11-01T120000", end="2021-02-01T120000", freq="1D")
142142
values = 1 + np.arange(t.size)
143143
precip = xr.DataArray(values, coords={"T": t})
144-
precip_8day = calc.regroup(precip, group="8day", method="sum")
144+
precip_8day = calc.regroup(precip, group="8day").sum()
145145

146146
np.testing.assert_array_equal(precip_8day.data, [
147147
92., 156., 220., 284., 348., 412., 351., 524., 588., 652., 716.
@@ -152,7 +152,7 @@ def test_regroup_daily_to_dekad():
152152
t = pd.date_range(start="2020-01-01T120000", end="2020-03-09T120000", freq="1D")
153153
values = 1 + np.arange(t.size)
154154
precip = xr.DataArray(values, coords={"T": t})
155-
precip_dekad = calc.regroup(precip, group="dekad", method="sum")
155+
precip_dekad = calc.regroup(precip, group="dekad").sum()
156156

157157
np.testing.assert_array_equal(
158158
precip_dekad.data, [55., 155., 286., 365., 465., 504.]
@@ -163,7 +163,7 @@ def test_regroup_daily_to_16day():
163163
t = pd.date_range(start="2000-11-01T120000", end="2001-02-01T120000", freq="1D")
164164
values = 1 + np.arange(t.size)
165165
precip = xr.DataArray(values, coords={"T": t})
166-
precip_16day = calc.regroup(precip, group="16day", method="sum")
166+
precip_16day = calc.regroup(precip, group="16day").sum()
167167

168168
np.testing.assert_array_equal(
169169
precip_16day.data, [376., 632., 763., 1112., 1368.]
@@ -174,7 +174,7 @@ def test_regroup_daily_to_1M():
174174
t = pd.date_range(start="2000-11-01T120000", end="2001-02-01T120000", freq="1D")
175175
values = 1 + np.arange(t.size)
176176
precip = xr.DataArray(values, coords={"T": t})
177-
precip_month = calc.regroup(precip, group="1M", method="sum")
177+
precip_month = calc.regroup(precip, group="1M").sum()
178178

179179
np.testing.assert_array_equal(
180180
precip_month.data,
@@ -186,7 +186,7 @@ def test_regroup_daily_to_5M():
186186
t = pd.date_range(start="2000-01-01", end="2001-12-31", freq="1D")
187187
values = 1 + np.arange(t.size)
188188
precip = xr.DataArray(values, coords={"T": t})
189-
precip_month = calc.regroup(precip, group="5M", method="sum")
189+
precip_month = calc.regroup(precip, group="5M").sum()
190190

191191
xr.testing.assert_equal(
192192
precip_month.isel(T_bins=0, drop=True),
@@ -202,7 +202,7 @@ def test_regroup_daily_to_season1():
202202
t = pd.date_range(start="2000-01-01", end="2002-12-31", freq="1D")
203203
values = 1 + np.arange(t.size)
204204
precip = xr.DataArray(values, coords={"T": t})
205-
precip_seas = calc.regroup(precip, group="14 Dec - 29 Mar", method="sum")
205+
precip_seas = calc.regroup(precip, group="14 Dec - 29 Mar").sum()
206206

207207
xr.testing.assert_equal(
208208
precip_seas.isel(T_bins=0, drop=True),
@@ -230,7 +230,7 @@ def test_regroup_daily_to_season2():
230230
t = pd.date_range(start="2000-01-01", end="2001-12-31", freq="1D")
231231
values = 1 + np.arange(t.size)
232232
precip = xr.DataArray(values, coords={"T": t})
233-
precip_seas = calc.regroup(precip, group="19-29 Feb", method="sum")
233+
precip_seas = calc.regroup(precip, group="19-29 Feb").sum()
234234

235235
xr.testing.assert_equal(
236236
precip_seas.isel(T_bins=0, drop=True),
@@ -246,7 +246,7 @@ def test_regroup_daily_to_season3():
246246
t = pd.date_range(start="2000-01-01", end="2001-12-31", freq="1D")
247247
values = 1 + np.arange(t.size)
248248
precip = xr.DataArray(values, coords={"T": t})
249-
precip_seas = calc.regroup(precip, group="29 Feb - 29 Mar", method="sum")
249+
precip_seas = calc.regroup(precip, group="29 Feb - 29 Mar").sum()
250250

251251
xr.testing.assert_equal(
252252
precip_seas.isel(T_bins=0, drop=True),
@@ -262,7 +262,7 @@ def test_regroup_daily_to_int():
262262
t = pd.date_range(start="2000-01-01", end="2000-01-28", freq="1D")
263263
values = 1 + np.arange(t.size)
264264
precip = xr.DataArray(values, coords={"T": t})
265-
precip_int = calc.regroup(precip, group=4, method="sum")
265+
precip_int = calc.regroup(precip, group=4).sum()
266266

267267
np.testing.assert_array_equal(
268268
precip_int,
@@ -274,7 +274,7 @@ def test_resample_interval_to_daily():
274274
t = pd.date_range(start="2000-01-01", end="2000-01-28", freq="1D")
275275
values = 1 + np.arange(t.size)
276276
precip = xr.DataArray(values, coords={"T": t})
277-
precip_pentad = calc.regroup(precip, group="pentad", method="sum")
277+
precip_pentad = calc.regroup(precip, group="pentad").sum()
278278
precip_daily = calc.resample_interval_to_daily(precip_pentad)
279279

280280
np.testing.assert_array_equal(precip_daily, [
@@ -287,7 +287,7 @@ def test_resample_interval_to_daily_intensive():
287287
t = pd.date_range(start="2000-01-01", end="2000-01-28", freq="1D")
288288
values = 1 + np.arange(t.size)
289289
precip = xr.DataArray(values, coords={"T": t}, attrs={"units": "mm/day"})
290-
precip_pentad = calc.regroup(precip, group="pentad", method="mean")
290+
precip_pentad = calc.regroup(precip, group="pentad").mean()
291291
precip_daily = calc.resample_interval_to_daily(precip_pentad)
292292

293293
np.testing.assert_array_equal(precip_daily, [
@@ -633,19 +633,15 @@ def test_seasonal_onset_date_keeps_returning_same_outputs():
633633
def test_seasonal_onset_date_keeps_returning_same_outputs_with_regroup():
634634
precip = data_test_calc.multi_year_data_sample()
635635
onsetsds = calc.regroup(
636-
time_series=precip,
637-
group="1 Mar - 20 Jun",
638-
method="map",
639-
method_kwargs={
640-
"func": calc.onset_date,
641-
"wet_thresh": 1,
642-
"wet_spell_length": 3,
643-
"wet_spell_thresh": 20,
644-
"min_wet_days": 1,
645-
"dry_spell_length": 7,
646-
"dry_spell_search": 21,
647-
},
648-
)
636+
time_series=precip, group="1 Mar - 20 Jun"
637+
).map(calc.onset_date, **{
638+
"wet_thresh": 1,
639+
"wet_spell_length": 3,
640+
"wet_spell_thresh": 20,
641+
"min_wet_days": 1,
642+
"dry_spell_length": 7,
643+
"dry_spell_search": 21,
644+
})
649645
# That part could be included in regroup for this specific group case
650646
onsetsds = onsetsds.isel(T_bins=np.arange(0, onsetsds.size, 2), drop=True)
651647
# Note that onset_date is written and such a manner that is also outputs T, while
@@ -709,15 +705,11 @@ def test_seasonal_cess_date_keeps_returning_same_outputs_with_regroup():
709705
time_dim="T"
710706
).to_array(name="soil moisture").squeeze("variable", drop=True)
711707
cessds = calc.regroup(
712-
time_series=wb,
713-
group="1 Sep - 30 Nov",
714-
method="map",
715-
method_kwargs={
716-
"func": calc.cess_date_from_sm,
708+
time_series=wb, group="1 Sep - 30 Nov"
709+
).map(calc.cess_date_from_sm, **{
717710
"dry_thresh": 5,
718711
"dry_spell_length_thresh": 3,
719-
},
720-
)
712+
})
721713
# Not sure what happened to T_bins here
722714
cessds = cessds.isel(T=np.arange(0, cessds.size, 2), drop=True)
723715
cess = (cessds + cessds["T"]).squeeze()
@@ -758,18 +750,14 @@ def test_seasonal_cess_date_from_rain_keeps_returning_same_outputs_with_regroup(
758750

759751
precip = data_test_calc.multi_year_data_sample()
760752
cessds = calc.regroup(
761-
time_series=precip,
762-
group="1 Sep - 30 Nov",
763-
method="map",
764-
method_kwargs={
765-
"func": calc.cess_date_from_rain,
753+
time_series=precip, group="1 Sep - 30 Nov"
754+
).map(calc.cess_date_from_rain, **{
766755
"dry_thresh": 5,
767756
"dry_spell_length_thresh": 3,
768757
"et": 5,
769758
"taw": 60,
770759
"sminit": 33.57026932, # from previous test sm output on 8/31/2000
771-
}
772-
)
760+
})
773761
cessds = cessds.isel(T=np.arange(0, cessds.size, 2), drop=True)
774762
cess = (cessds + cessds["T"]).squeeze()
775763

@@ -857,19 +845,15 @@ def test_seasonal_onset_date_with_regroup():
857845
).rename("synthetic_precip")
858846

859847
onsetsds = calc.regroup(
860-
time_series=synthetic_precip,
861-
group="1 Mar - 20 Jun",
862-
method="map",
863-
method_kwargs={
864-
"func": calc.onset_date,
848+
time_series=synthetic_precip, group="1 Mar - 20 Jun"
849+
).map(calc.onset_date, **{
865850
"wet_thresh": 1,
866851
"wet_spell_length": 3,
867852
"wet_spell_thresh": 20,
868853
"min_wet_days": 1,
869854
"dry_spell_length": 7,
870855
"dry_spell_search": 21,
871-
},
872-
)
856+
})
873857
onsetsds = onsetsds.isel(T_bins=np.arange(0, onsetsds.size, 2), drop=True)
874858
onsets = (onsetsds + onsetsds["T"]).drop_vars("T")
875859

@@ -978,15 +962,11 @@ def test_seasonal_cess_date_with_regroup():
978962
time_dim="T"
979963
).to_array(name="soil moisture")
980964
cessds = calc.regroup(
981-
time_series=wb,
982-
group="1 Sep - 30 Nov",
983-
method="map",
984-
method_kwargs={
985-
"func": calc.cess_date_from_sm,
965+
time_series=wb, group="1 Sep - 30 Nov",
966+
).map(calc.cess_date_from_sm, **{
986967
"dry_thresh": 5,
987968
"dry_spell_length_thresh": 3,
988-
},
989-
)
969+
})
990970
cessds = cessds.isel(T=np.arange(0, cessds.size, 2), drop=True)
991971
cess = (cessds + cessds["T"]).squeeze(drop=True)
992972

@@ -1081,18 +1061,14 @@ def test_seasonal_cess_date_from_rain_with_regroup():
10811061
synthetic_precip,
10821062
).rename("synthetic_precip")
10831063
cessds = calc.regroup(
1084-
time_series=synthetic_precip,
1085-
group="1 Sep - 30 Nov",
1086-
method="map",
1087-
method_kwargs={
1088-
"func": calc.cess_date_from_rain,
1064+
time_series=synthetic_precip, group="1 Sep - 30 Nov"
1065+
).map(calc.cess_date_from_rain, **{
10891066
"dry_thresh": 5,
10901067
"dry_spell_length_thresh": 3,
10911068
"et": 5,
10921069
"taw": 60,
10931070
"sminit": 0,
1094-
},
1095-
)
1071+
})
10961072
cessds = cessds.isel(T=np.arange(0, cessds.size, 2), drop=True)
10971073
cess = (cessds + cessds["T"]).squeeze()
10981074

0 commit comments

Comments
 (0)