bakaano.neuralnet.simulate¶
Simulation and inference utilities for streamflow prediction.
Role: Prepare simulation inputs and run trained model inference.
- bakaano.neuralnet.simulate._load_pysheds_grid()[source]¶
Import pysheds lazily to avoid import-time backend failures.
- bakaano.neuralnet.simulate._open_dataset_with_fallback(nc_path)[source]¶
Open NetCDF with backend fallback for Colab/Drive compatibility.
- bakaano.neuralnet.simulate.split_predictions_by_station(flat_predictions, station_window_counts)[source]¶
Split flat model outputs into station-aligned chunks.
- bakaano.neuralnet.simulate._preview_items(items, limit=8)[source]¶
Return a compact preview string for notebook progress messages.
- bakaano.neuralnet.simulate.coerce_prediction_array(predicted_streamflow)[source]¶
Convert model outputs to a NumPy float array safe for NumPy ops.
- bakaano.neuralnet.simulate.clip_negative_predictions(predicted_streamflow)[source]¶
Clamp negative predictions to zero after normalizing dtype.
- bakaano.neuralnet.simulate.convert_area_normalized_flow(predicted_streamflow, catch_area)[source]¶
Convert area-normalized depth (mm/day) back to discharge (m3/s).
- bakaano.neuralnet.simulate.inverse_log1p_predictions(predicted_streamflow)[source]¶
Convert model outputs from log1p target space back to linear values.
- bakaano.neuralnet.simulate.build_prediction_frame(predicted_streamflow, sim_start)[source]¶
Return a dated prediction frame after the 365-day model lookback.
- bakaano.neuralnet.simulate._plot_grdc_streamflow(observed_streamflow, predicted_streamflow, val_start)[source]¶
Plot observed vs predicted streamflow for one interactive evaluation run.
- bakaano.neuralnet.simulate.evaluate_streamflow_model_interactively(working_dir, study_area, model_path, val_start, val_end, grdc_netcdf=None, routing_method='mfd', catchment_size_threshold=1000, area_normalize=True, log_transform=True, csv_dir=None, lookup_csv=None, id_col='id', lat_col='latitude', lon_col='longitude', date_col='date', discharge_col='discharge', file_pattern='{id}.csv', runoff_output_dir=None)[source]¶
Interactively evaluate a trained model against one station.
- bakaano.neuralnet.simulate.simulate_streamflow(working_dir, study_area, model_path, sim_start, sim_end, latlist, lonlist, routing_method='mfd', area_normalize=True, log_transform=True, runoff_output_dir=None)[source]¶
Simulate streamflow at arbitrary latitude/longitude points.
- bakaano.neuralnet.simulate.simulate_grdc_csv_stations(working_dir, study_area, model_path, sim_start, sim_end, grdc_netcdf=None, routing_method='mfd', csv_dir=None, lookup_csv=None, id_col='id', lat_col='latitude', lon_col='longitude', date_col='date', discharge_col='discharge', file_pattern='{id}.csv', area_normalize=True, log_transform=True, runoff_output_dir=None)[source]¶
Simulate streamflow for GRDC or CSV-defined station sets.
- class bakaano.neuralnet.simulate.PredictDataPreprocessor(working_dir, study_area, sim_start, sim_end, routing_method, grdc_streamflow_nc_file=None, catchment_size_threshold=None, runoff_output_dir=None)[source]¶
Bases:
object- _load_runoff_entries_for_period()[source]¶
Load routed runoff entries and require exact daily coverage for simulation.
- _load_optional_rainfall_entries_for_period()[source]¶
Load routed rainfall entries if available and fully aligned.
- _extract_station_rowcol(lat, lon)[source]¶
Extract the row and column indices for a given latitude and longitude from given raster file.
- Parameters:
lat (float) – The latitude of the station.
lon (float) – The longitude of the station.
- Returns:
row (int) – The row index corresponding to the given latitude and longitude.
col (int) – The column index corresponding to the given latitude and longitude.
- _snap_coordinates(lat, lon)[source]¶
Snap the given latitude and longitude to the nearest river segment based on a river grid.
- Parameters:
lat (float) – The latitude to be snapped.
lon (float) – The longitude to be snapped.
- Returns:
snapped_lat (float) – The latitude of the nearest river segment.
snapped_lon (float) – The longitude of the nearest river segment.
- _check_point_in_region(olat, olon)[source]¶
Check whether a single (olat, olon) point lies within a study-area shapefile.
If NOT inside: raise SystemExit with a formatted, user-facing message
If inside: print confirmation and do nothing
- load_observed_streamflow(grdc_streamflow_nc_file)[source]¶
Load and filter observed GRDC streamflow data in a schema-robust way. Works for single- and multi-station NetCDFs.
- Parameters:
grdc_streamflow_nc_file (str) – Path to GRDC NetCDF file.
- Returns:
Filtered GRDC subset for the study area.
- Return type:
xarray.Dataset
- load_observed_streamflow_from_csv_dir(csv_dir, lookup_csv, id_col='id', lat_col='latitude', lon_col='longitude', date_col='date', discharge_col='discharge', file_pattern='{id}.csv')[source]¶
Load observed streamflow from per-station CSV files using a lookup table.
The lookup table must include station identifiers and coordinates. The method filters stations to the study area, then loads per-station CSVs by ID.
- Parameters:
csv_dir (str) – Directory containing per-station CSV files.
lookup_csv (str) – CSV file with station ids and coordinates.
id_col (str) – Station id column in lookup CSV.
lat_col (str) – Latitude column in lookup CSV.
lon_col (str) – Longitude column in lookup CSV.
date_col (str) – Date column in station CSVs.
discharge_col (str) – Discharge column in station CSVs.
file_pattern (str) – Pattern for station CSV filenames (e.g.,
"{id}.csv").
- Returns:
Mapping of station_id to observed discharge DataFrame.
- Return type:
dict
- get_data()[source]¶
Extract and preprocess predictor and response variables for each station based on its coordinates.
- Returns:
A list containing two elements: - self.data_list: A list of tuples, each containing predictors (DataFrame) and response (DataFrame). - self.catchment: A list of tuples, each containing catchment data (accumulation and slope values).
- Return type:
list
- class bakaano.neuralnet.simulate.PredictStreamflow(working_dir, area_normalize=True, log_transform=True)[source]¶
Bases:
object- predict_station_series(batch_size=None, area_normalize=None)[source]¶
Run inference and return one prediction array per valid station/point.
- load_model_config(model_path)[source]¶
Load saved model-scale options before preparing inference tensors.
- print_prediction_summary(point_count=None)[source]¶
Print a compact summary of prepared simulation tensors.
- load_model(path)[source]¶
Load a trained regional model from disk.
- Parameters:
path (str) – Path to the saved Keras model.
- Returns:
Loaded model instance.
- Return type:
tensorflow.keras.Model