Skip to content

Commit ed5fe61

Browse files
committed
review Valentine
1 parent 0631a0d commit ed5fe61

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎doc/source/stats.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ GeoUtils provides support for grouped statistics, allowing statistics to be comp
9999
defined by one or more grouping bins. This is particularly useful when analyzing how statistical properties vary
100100
across classes, bins, or segmentation derived from the data itself.
101101

102-
Example of grouped statistics according elevation values
102+
103+
### Example with altitude intervals
104+
In this example, we will create different altitude classes from a chosen interval [400, 1000, 2000, 3000, >3000].
105+
Once these bins are created, we reapply them and compute the mean, minimum, and maximum values of the same raster
106+
for each sub-interval. It is also possible to use a reference other than the raster itself for the group_by.
107+
103108
```{code-cell} ipython3
104109
from geoutils.stats import grouped_stats
105110
106111
group_by = {"rast": rast}
107-
bins = {"rast": [100, 1000, 2000, 3000, np.inf]}
112+
bins = {"rast": [400, 1000, 2000, 3000, np.inf]}
108113
to_aggregate = {"rast": rast}
109114
statistics = ["mean", "min", "max"]
110115
@@ -117,7 +122,13 @@ Bins can be presented in different ways. It is possible to integrate an interval
117122
segmentation map in raster format.
118123
```
119124

120-
Example of grouped statistics according elevation values as mask
125+
126+
### Example with altitude masks
127+
128+
In this example, we will create a mask such as altitude is more than 2000 meters.
129+
Once these masks are created as a raster, we reapply them and compute the mean, minimum, and maximum values of the same raster
130+
for masks = True. It is also possible to use a reference other than the raster itself for the group_by.
131+
121132
```{code-cell} ipython3
122133
from geoutils.stats import grouped_stats
123134
@@ -127,7 +138,7 @@ bins = {"rast": elev_mask}
127138
to_aggregate = {"rast": rast}
128139
statistics = ["mean", "min", "max"]
129140
130-
df = grouped_stats.grouped_stats(group_by, bins, to_aggregate, statistics)
141+
df = grouped_stats.grouped_stats(group_by, bins, to_aggregate, statistics, save_masks="")
131142
df
132143
```
133144

‎geoutils/stats/grouped_stats.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def grouped_stats(
129129

130130
# is interval
131131
if is_interval(bins_array):
132-
cut = pd.cut(df[groupby_key], bins=bins_array, include_lowest=True)
132+
cut = pd.cut(df[groupby_key], bins=bins_array)
133133
df[group_col] = cut
134134

135135
if save_masks and not isinstance(bins[groupby_key], str):
@@ -175,7 +175,7 @@ def grouped_stats(
175175
groupby_keys.append(group_col)
176176

177177
# Use aggregation directly on categorical columns (fast)
178-
result = df.groupby(groupby_keys, observed=True, dropna=False)[list(aggregated_vars.keys())].agg(statistics)
178+
result = df.groupby(groupby_keys)[list(aggregated_vars.keys())].agg(statistics)
179179

180180
if save_csv:
181181
csv_file = Path(save_csv) / "grouped_stats.csv"

‎tests/test_stats/test_grouped_stats.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_savings_csv_and_tiff(
195195
group_by_test, bins_test, aggregated_vars, statistics, save_csv=tmp_path, save_masks=tmp_path
196196
)
197197

198-
for interval in ["-0.001_5.0", "10.0_inf", "5.0_10.0"]:
198+
for interval in ["0_5.0", "10.0_inf", "5.0_10.0"]:
199199
assert tmp_path / "groupbyslope1" / f"slope1_{interval}.tif"
200200
for seg in [1, 2, 3]:
201201
assert tmp_path / "groupbyslope2" / f"slope2_{seg}.tif"

0 commit comments

Comments
 (0)