|
| 1 | +""" |
| 2 | +FIM evaluation workflow module within FIMserv. |
| 3 | +Author: Supath Dhital |
| 4 | +Date: 18 Nov, 2025 |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | +from typing import Optional |
| 10 | +import fimeval as fe # type: ignore |
| 11 | + |
| 12 | +class run_evaluation: |
| 13 | + """ |
| 14 | + Runs the full Flood Inundation Mapping (FIM) evaluation workflow. |
| 15 | +
|
| 16 | + This class loads benchmark FIM data (from FIMbench or user-provided folders), |
| 17 | + performs raster-based evaluation, optionally prints summary plots, and can also |
| 18 | + run building-footprint-based exposure evaluation. |
| 19 | +
|
| 20 | + The below mostly used internally, incase user want to run evaluation with modifications of data inputs they can pass different parameters |
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + Main_dir : Optional[str]; Directory containing benchmark FIM inputs. Defaults to ./FIM_Evaluation/FIM_inputs if not provided. |
| 24 | + output_dir : Optional[str]; Folder where evaluation results and graphs will be saved. |
| 25 | + shapefile_path : Optional[str]; AOI shapefile or vector file used to clip data during evaluation. Internally used the geopackage within folder. |
| 26 | + PWB_dir : Optional[str]; Directory containing the Permanent Water Bodies. |
| 27 | + building_footprint : Optional[str]; Local building footprint dataset (GeoJSON/Shapefile/Parquet) for building-level exposure evaluation. |
| 28 | + target_crs : Optional[str]; CRS to reproject FIM rasters to (e.g., "EPSG:3857"). |
| 29 | + target_resolution : Optional[float]; Output raster resolution (units depend on CRS). |
| 30 | + method_name : Optional[str]; Name of the evaluation method to evaluate. Defaults to "AOI". |
| 31 | + countryISO : Optional[str]; ISO-3 country code used only when downloading footprints from GEE. |
| 32 | + geeprojectID : Optional[str]; Google Earth Engine project ID for footprint download (if no local file provided). |
| 33 | + print_graphs : bool; If True, generates and saves contingency maps and evaluation metric plots. |
| 34 | + Evalwith_BF : bool; If True, performs building-footprint-based exposure evaluation. |
| 35 | + """ |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + Main_dir: Optional[str] = None, #If user use their own input directory to save benchmark FIM outputs |
| 39 | + output_dir: Optional[str] = None, |
| 40 | + shapefile_path: Optional[str] = None, |
| 41 | + PWB_dir: Optional[str] = None, |
| 42 | + building_footprint: Optional[str] = None, |
| 43 | + target_crs: Optional[str] = None, |
| 44 | + target_resolution: Optional[float] = None, |
| 45 | + method_name: Optional[str] = None, #By default it will use 'AOI' which is downloaded but incase user want to explore different method they can pass here |
| 46 | + countryISO: Optional[str] = None, |
| 47 | + geeprojectID: Optional[str] = None, |
| 48 | + print_graphs: bool = False, |
| 49 | + Evalwith_BF: bool = False, #If user want to run evaluation with building footprint |
| 50 | + ): |
| 51 | + if Main_dir is None: |
| 52 | + self.Main_dir = os.path.join(os.getcwd(), "FIM_Evaluation", "FIM_inputs") |
| 53 | + else: |
| 54 | + self.Main_dir = Main_dir |
| 55 | + |
| 56 | + self.shapefile_path = shapefile_path |
| 57 | + |
| 58 | + if output_dir is None: |
| 59 | + self.output_dir = os.path.join(os.getcwd(), "FIM_Evaluation", "Evaluation_Results") |
| 60 | + else: |
| 61 | + self.output_dir = output_dir |
| 62 | + |
| 63 | + self.PWB_dir = PWB_dir |
| 64 | + self.building_footprint = building_footprint |
| 65 | + self.target_crs = target_crs |
| 66 | + self.target_resolution = target_resolution |
| 67 | + if method_name is None: |
| 68 | + self.method_name = 'AOI' |
| 69 | + else: |
| 70 | + self.method_name = method_name |
| 71 | + self.countryISO = countryISO |
| 72 | + self.geeprojectID = geeprojectID |
| 73 | + self.Evalwith_BF = Evalwith_BF |
| 74 | + self.print_graphs = print_graphs |
| 75 | + |
| 76 | + #run the process |
| 77 | + self.run_eval() |
| 78 | + |
| 79 | + def run_eval(self): |
| 80 | + fe.EvaluateFIM( |
| 81 | + main_dir=self.Main_dir, |
| 82 | + method_name=self.method_name, |
| 83 | + output_dir=self.output_dir, |
| 84 | + PWB_dir=self.PWB_dir, |
| 85 | + shapefile_dir=self.shapefile_path, |
| 86 | + target_crs=self.target_crs, |
| 87 | + target_resolution=self.target_resolution, |
| 88 | + ) |
| 89 | + |
| 90 | + if self.print_graphs: |
| 91 | + fe.PrintContingencyMap( |
| 92 | + main_dir=self.Main_dir, |
| 93 | + method_name=self.method_name, |
| 94 | + output_dir=self.output_dir, |
| 95 | + ) |
| 96 | + fe.PlotEvaluationMetrics( |
| 97 | + main_dir=self.Main_dir, |
| 98 | + method_name=self.method_name, |
| 99 | + output_dir=self.output_dir, |
| 100 | + ) |
| 101 | + |
| 102 | + if self.Evalwith_BF: |
| 103 | + try: |
| 104 | + fe.EvaluationWithBuildingFootprint( |
| 105 | + main_dir=self.Main_dir, |
| 106 | + method_name=self.method_name, |
| 107 | + output_dir=self.output_dir, |
| 108 | + countryISO=self.countryISO, |
| 109 | + geeprojectID=self.geeprojectID, |
| 110 | + building_footprint=self.building_footprint, |
| 111 | + ) |
| 112 | + except Exception as e: |
| 113 | + print("Skipping evaluation with building footprint due to error:", e) |
| 114 | + |
| 115 | + |
0 commit comments