3
3
import pandas as pd
4
4
import rasterio
5
5
from beartype import beartype
6
- from beartype .typing import Union
6
+ from beartype .typing import Dict , Union
7
7
from statsmodels .stats import stattools
8
8
from statsmodels .stats .weightstats import DescrStatsW
9
9
10
- from eis_toolkit .exceptions import InvalidColumnException
10
+ from eis_toolkit .exceptions import InvalidColumnException , InvalidRasterBandException
11
11
12
12
13
- def _descriptive_statistics (data : Union [rasterio .io .DatasetReader , pd .DataFrame , gpd .GeoDataFrame ]) -> dict :
13
+ def _descriptive_statistics (data : Union [rasterio .io .DatasetReader , pd .DataFrame , gpd .GeoDataFrame ]) -> Dict [ str , float ] :
14
14
statistics = DescrStatsW (data )
15
15
min = np .min (data )
16
16
max = np .max (data )
@@ -38,14 +38,25 @@ def _descriptive_statistics(data: Union[rasterio.io.DatasetReader, pd.DataFrame,
38
38
39
39
40
40
@beartype
41
- def descriptive_statistics_dataframe (input_data : Union [pd .DataFrame , gpd .GeoDataFrame ], column : str ) -> dict :
42
- """Generate descriptive statistics from vector data.
41
+ def descriptive_statistics_dataframe (
42
+ input_data : Union [pd .DataFrame , gpd .GeoDataFrame ], column : str
43
+ ) -> Dict [str , float ]:
44
+ """Compute descriptive statistics from vector data.
43
45
44
- Generates min, max, mean, quantiles(25%, 50% and 75%), standard deviation, relative standard deviation and skewness.
46
+ Computes the following statistics:
47
+ - min
48
+ - max
49
+ - mean
50
+ - quantiles 25%
51
+ - quantile 50% (median)
52
+ - quantile 75%
53
+ - standard deviation
54
+ - relative standard deviation
55
+ - skewness
45
56
46
57
Args:
47
- input_data: Data to generate descriptive statistics from .
48
- column: Specify the column to generate descriptive statistics from.
58
+ input_data: Input vector data .
59
+ column: Column in vector data to compute descriptive statistics from.
49
60
50
61
Returns:
51
62
The descriptive statistics in previously described order.
@@ -58,19 +69,33 @@ def descriptive_statistics_dataframe(input_data: Union[pd.DataFrame, gpd.GeoData
58
69
59
70
60
71
@beartype
61
- def descriptive_statistics_raster (input_data : rasterio .io .DatasetReader ) -> dict :
62
- """Generate descriptive statistics from raster data.
72
+ def descriptive_statistics_raster (input_data : rasterio .io .DatasetReader , band : int = 1 ) -> Dict [str , float ]:
73
+ """Compute descriptive statistics from raster data.
74
+
75
+ Computes the following statistics:
76
+ - min
77
+ - max
78
+ - mean
79
+ - quantiles 25%
80
+ - quantile 50% (median)
81
+ - quantile 75%
82
+ - standard deviation
83
+ - relative standard deviation
84
+ - skewness
63
85
64
- Generates min, max, mean, quantiles(25%, 50% and 75%), standard deviation, relative standard deviation and skewness.
65
86
Nodata values are removed from the data before the statistics are computed.
66
87
67
88
Args:
68
- input_data: Data to generate descriptive statistics from.
89
+ input_data: Input raster data.
90
+ band: Raster band to compute descriptive statistics from.
69
91
70
92
Returns:
71
93
The descriptive statistics in previously described order.
72
94
"""
73
- data = input_data .read ().flatten ()
95
+ if band not in range (1 , input_data .count + 1 ):
96
+ raise InvalidRasterBandException (f"Input raster does not contain the selected band: { band } ." )
97
+
98
+ data = input_data .read (band )
74
99
nodata_value = input_data .nodata
75
100
data = data [data != nodata_value ]
76
101
statistics = _descriptive_statistics (data )
0 commit comments