Skip to content

Commit 8acdaf3

Browse files
added the evaluation module within FIMserv
1 parent 14fd6d7 commit 8acdaf3

5 files changed

Lines changed: 139 additions & 3 deletions

File tree

src/fimserve/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
# evaluation of FIM
3939
from .fimevaluation.fims_setup import FIMService, fim_lookup
40+
from .fimevaluation.run_fimeval import run_evaluation
4041

4142

4243
__all__ = [
@@ -57,4 +58,5 @@
5758
"getIntersectedHUC8ID",
5859
"FIMService",
5960
"fim_lookup",
61+
"run_evaluation",
6062
]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .fims_setup import FIMService, fim_lookup
2+
from .run_fimeval import run_evaluation
23

3-
__all__ = ["FIMService", "fim_lookup"]
4+
__all__ = ["FIMService", "fim_lookup", "run_evaluation"]

src/fimserve/fimevaluation/fims_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ def fim_lookup(
523523
date_input: Optional[str] = None,
524524
file_name: Optional[str] = None,
525525
run_handfim: bool = False,
526-
out_dir: Optional[str] = None,
526+
out_dir: Optional[str] = None, # Directory to place downloaded/generated files
527527
start_date: Optional[str] = None,
528528
end_date: Optional[str] = None,
529529
) -> str:
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+

tests/test_evalutionhandfim.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,27 @@ def test_bm_fimlookup():
77
out = fm.fim_lookup(
88
HUCID="10170203",
99
date_input="2019-09-19 16:00:00", # If user is more specific then they can pass date (with hour if known) along with HUC8
10-
# run_handfim=True, #It will look for the owp hand fim for the mentioned HUC8 and date, if not found it will download and generate the owp hand fim
10+
run_handfim=True, #It will look for the owp hand fim for the mentioned HUC8 and date, if not found it will download and generate the owp hand fim
1111
# file_name="PSS_1_0m_20190919T165541_963659W424518N_BM.tif", #If user pass the specific filename, it will download that and assume that this is the right benchmark, else based on exact match of date it will look for the benchmark
12+
# out_dir = None, # If user want to save the benchmark fim in specific directory
1213
# start_date="2024-06-20", #If user is not sure of the exact date then they can pass a range of dates
1314
# end_date="2024-06-25",
1415
)
1516
print(out)
17+
18+
#After finalizing the benchmark FIM data user can run evaluation
19+
def test_run_fimeval():
20+
fm.run_evaluation(
21+
Main_dir=None, #If user use their own input directory to where FIM outputs; basically out_dir in fim_lookup us Main_dir here
22+
output_dir= None, #Folder where evaluation results will be saved
23+
shapefile_path= None, #AOI shapefile or vector file used to clip data during evaluation. Internally used the geopackage within folder.
24+
PWB_dir= None, #Directory containing the Permanent Water Bodies.
25+
building_footprint= None, #Local building footprint dataset (GeoJSON/Shapefile) for building-level exposure evaluation.
26+
target_crs= None, #CRS to reproject FIM rasters to (e.g., "EPSG:3857").
27+
target_resolution= None, #Output raster resolution (units depend on CRS).
28+
method_name= None, #By default it will use 'AOI' which is downloaded but incase user want to explore different method they can pass here
29+
countryISO= None, #ISO-3 country code used only when downloading footprints from GEE.
30+
geeprojectID= None, #Google Earth Engine project ID for footprint download (if no local file provided).
31+
print_graphs= False, #If True, generates and saves contingency maps and evaluation metric plots.
32+
Evalwith_BF= False, #If user want to run evaluation with building footprint
33+
)

0 commit comments

Comments
 (0)