Utils Module🔗
calculate_temporal_stats(df)
🔗
Calculate temporal statistics for a given DataFrame.
Source code in src/water_timeseries/utils/data.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
create_tile_layers()
🔗
Create tile layers for folium maps.
Returns:
| Type | Description |
|---|---|
|
List of tile layer names that can be added to folium.Map |
Source code in src/water_timeseries/utils/map_styling.py
149 150 151 152 153 154 155 | |
format_tooltip_columns(valid_gdf, id_column, tooltip_columns=None)
🔗
Format columns for tooltip display to avoid JSON serialization issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
valid_gdf
|
GeoDataFrame to format |
required | |
id_column
|
str
|
Name of the ID column (always shown first) |
required |
tooltip_columns
|
List of tuples (original_col, display_alias, format_string, unit) If None, uses default NetChange columns |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
formatted_gdf |
GeoDataFrame with display columns added |
|
fields_to_show |
List of field names for tooltip |
|
aliases_to_show |
List of field aliases for tooltip |
Source code in src/water_timeseries/utils/map_styling.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
get_colored_style_function(color_column='NetChange_perc', vmin=-40, vmax=40, colormap=None, default_color='#cccccc', fill_opacity=0.6, edge_color='#dddddd', edge_weight=1)
🔗
Create a style function for folium polygons based on a numeric column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_column
|
str
|
Column name to use for coloring |
'NetChange_perc'
|
vmin
|
float
|
Minimum value for normalization |
-40
|
vmax
|
float
|
Maximum value for normalization |
40
|
colormap
|
Matplotlib colormap (defaults to RdBu_r) |
None
|
|
default_color
|
str
|
Color for missing/null values |
'#cccccc'
|
fill_opacity
|
float
|
Opacity of polygon fill (0-1) |
0.6
|
edge_color
|
str
|
Color of polygon edges |
'#dddddd'
|
edge_weight
|
float
|
Width of polygon edges |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
style_function |
Function that can be passed to folium.GeoJson style_function parameter |
Source code in src/water_timeseries/utils/map_styling.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
get_default_style_function(fill_color='blue', edge_color='#dddddd', edge_weight=1, fill_opacity=0.5)
🔗
Create a default style function for folium polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_color
|
str
|
Fill color for all polygons |
'blue'
|
edge_color
|
str
|
Color of polygon edges |
'#dddddd'
|
edge_weight
|
float
|
Width of polygon edges |
1
|
fill_opacity
|
float
|
Opacity of polygon fill (0-1) |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
style_function |
Function that can be passed to folium.GeoJson style_function parameter |
Source code in src/water_timeseries/utils/map_styling.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
get_water_dataset_type(input_ds)
🔗
Determine the water dataset type based on the presence of specific variables in the dataset.
Source code in src/water_timeseries/utils/data.py
21 22 23 24 25 26 27 28 29 30 | |
load_vector_dataset(file_path, logger=None)
🔗
Load a vector dataset from file based on file extension.
Supports GeoPackage, Shapefile, GeoJSON, and Parquet formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to the vector dataset file. |
required |
logger
|
Optional[logger]
|
Optional logger instance for logging messages. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[GeoDataFrame]
|
GeoDataFrame if successful, None otherwise. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/water_timeseries/utils/io.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
load_xarray_dataset(path, format=None)
🔗
Load xarray dataset from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to the dataset file. |
required |
format
|
Optional[str]
|
Format of the file ('zarr' or 'netcdf'). If None, auto-detected from extension. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
xr.Dataset: The loaded dataset. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file format is not supported. |
Source code in src/water_timeseries/utils/io.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
save_xarray_dataset(ds, save_path, output_dir=None, logger=None)
🔗
Save xarray dataset to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
The xarray dataset to save. |
required |
save_path
|
Union[str, Path]
|
Path to save the file. Format is determined by extension: - '.zarr' for Zarr format - '.nc' for NetCDF format If a relative path is provided and output_dir is specified, the file will be saved in that directory. |
required |
output_dir
|
Optional[Union[str, Path]]
|
Directory for relative paths. If None and save_path is relative, the current working directory is used. |
None
|
logger
|
Logger for logging progress. If None, print statements are used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved path where the dataset was saved. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file extension is not supported. |
Source code in src/water_timeseries/utils/io.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |