Skip to content

Commit

Permalink
Update parallel code
Browse files Browse the repository at this point in the history
Update variable and file names, remove unused stuff
  • Loading branch information
matamadio committed Sep 18, 2023
1 parent 322e7a2 commit 276a57b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
31 changes: 17 additions & 14 deletions Top-down/parallelization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ Input data layers must be named and placed according to some rules, as follows:

- Create a working directory and set it as DATA_DIR (e.g. ./data) in `common.py`.
Inside the workdir, the data folders must follow this structure:

```
DATA_DIR/ADM: Administrative boundaries as '.gpkg' (multiple levels)
DATA_DIR/HZD: Hazard layers as '.tif' using CRS 4326
DATA_DIR/EXP: Exposure layers as '.tif' using CRS 4326
```
- To name datasets, use [`ISO3166_a3`](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) country code followed by specific data identifier - which is not fixed, you need to edit it in `runAnalysis.py`.

```
CCDR_tools/ADM/SEN_ADM.gpkg
CCDR_tools/HZD/SEN_FL_RP10.tif
Expand All @@ -73,29 +75,30 @@ Input data layers must be named and placed according to some rules, as follows:
## Setting parameters

Edit the `main.py` file to specify:
- **country (`country_dd`)**: [`ISO3166_a3`](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) country code
- **hazard type (`haz_cat_dd`)**: `'FL'` for floods; `'HS'` for heat stress; `'DR'` for drought; `'LS'` for landslide
- **country (`country`)**: [`ISO3166_a3`](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) country code
- **hazard type (`haz_cat`)**: `'FL'` for floods; `'HS'` for heat stress; `'DR'` for drought; `'LS'` for landslide
- **return periods (`return_periods`)**: list of return period scenarios as in the data, e.g. `[5, 10, 20, 50, 75, 100, 200, 250, 500, 1000]`
- **exposure categories (`exp_cat_dd_list`)**: list of exposure categories: `['pop', 'builtup', 'agri']`
- exposure categories file name (`exp_cat_dd_list`): list of same length of `exp_cat_dd_list` with file names for exposure categories: `['WPOP20', 'WSF19', 'ESA20_agri']`
If 'None', the default `['WPOP20', 'WSF19', 'ESA20_agri']` applies
- **analysis approach (`analysis_app_dd`)**: `['Classes', 'Function']`
- **exposure categories (`exp_cat_list`)**: list of exposure categories: `['POP', 'BU', 'AGR']`
- exposure categories file name (`exp_cat_list`): list of same length of `exp_cat_list` with file names for exposure categories, e.g.: `['GHS', 'WSF19', 'ESA20']`
If 'None', the default `['POP', 'BU', 'AGR']` applies
- **analysis approach (`analysis_app`)**: `['Classes', 'Function']`
- If `'Function'`, you can set minimum hazard threshold value (`min_haz_slider`). Hazard value below this threshold will be ignored
- If `'Classes'`, you can set the number and value of thresholds to consider to split hazard intensity values into bins (`class_edges`)
- **admin level (`adm_dd`)**: specify which boundary level to use for results summary (must exist in the `ISOa3`_ADM.gpkg file)
- **admin level (`adm`)**: specify which boundary level to use for results summary (must exist in the `ISOa3`_ADM.gpkg file)
- **save check (`save_check_raster`)**: specify if you want to export intermediate rasters (increases processing time) `[True, False]`

Example of `main.py` running flood analysis (`haz_cat_dd`) over Cambodia (`country_dd`) for 10 return periods (`return_periods`) over three exposure categories (`exp_cat_dd_list`) using hazard classes according to thresholds (`class_edges`); results summarised at ADM3 level (`adm_dd`). Do not save intermediate rasters (`save_check_raster`).
Example of `main.py` running flood analysis (`haz_cat`) over Cambodia (`country`) for 10 return periods (`return_periods`) over three exposure categories (`exp_cat_list`) using hazard classes according to thresholds (`class_edges`); results summarised at ADM3 level (`adm`). Do not save intermediate rasters (`save_check_raster`).

```
# Defining the initial parameters
country_dd = 'KHM'
haz_cat_dd = 'FL'
country = 'KHM'
haz_cat = 'FL'
return_periods = [5, 10, 20, 50, 75, 100, 200, 250, 500, 1000]
min_haz_slider = 0.05
exp_cat_dd_list = ['pop', 'builtup', 'agri']
exp_nam_dd_list = ['WPOP20', 'WSF19', 'ESA20_agri']
adm_dd = 'ADM3'
analysis_app_dd = 'Classes'
exp_cat_list = ['POP', 'BU', 'AGR']
exp_nam_list = ['GHS', 'WSF19', 'ESA20']
adm = 'ADM3'
analysis_app = 'Classes'
class_edges = [0.05, 0.25, 0.50, 1.00, 2.00]
save_check_raster = False
```
28 changes: 14 additions & 14 deletions Top-down/parallelization/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
# Defining the main function
def main():
# Defining the initial parameters
country_dd = 'KHM'
haz_cat_dd = 'FL' #'FL' for floods; 'HS' for heat stress; 'DR' for drought; 'LS' for landslide
country = 'BGD' # ISO3166-a3 code
haz_cat = 'FL' #'FL' for floods; 'HS' for heat stress; 'DR' for drought; 'LS' for landslide
return_periods = [5, 10, 20, 50, 75, 100, 200, 250, 500, 1000] # FL [5, 10, 20, 50, 75, 100, 200, 250, 500, 1000] # TC [10, 20, 50, 100, 200, 500] # add here as will
min_haz_slider = 0.05 # FL 0.05 # TC 25.0 # ASI 0.01
exp_cat_dd_list = ['pop', 'builtup', 'agri'] # ['pop', 'builtup', 'agri']
exp_nam_dd_list = ['WPOP20', 'WSF19', 'ESA20_agri']
# if None, the default applies: 'pop':'WPOP20', 'builtup':'WSF19', 'agri':'ESA20_agri', 'cstk'.'CSTK19' - If not None, expect a list of same length of exp_cat_dd_list
adm_dd = 'ADM3' #['ADM1', 'ADM2', 'ADM3']
analysis_app_dd = 'Classes' #['Classes', 'Function']
min_haz_slider = 0.05 # FL 0.05 # TC 25.0 # ASI 0.01
exp_cat_list = ['pop', 'BU', 'AGR'] # ['pop', 'builtup', 'agri']
exp_nam_list = ['POP', 'BU', 'AGR'] # if None, the default applies: 'Population':'POP', 'Built-up':'BU', 'Agricultural land':'AGR' - If not None, expect a list of same length of exp_cat_list
adm = 'ADM3' #['ADM1', 'ADM2', 'ADM3']
analysis_app = 'Function' #['Classes', 'Function']
class_edges = [0.05, 0.25, 0.50, 1.00, 2.00] # FL [0.05, 0.25, 0.50, 1.00, 2.00] # TC [17.0, 32.0, 42.0, 49.0, 58.0, 70.0] # DR_ASI [0.01, 0.10, 0.25, 0.40, 0.55, 0.70, 0.85]
save_check_raster = False

import time
start_time = time.time()

# Running the analysis
if exp_nam_dd_list is not None and len(exp_nam_dd_list) != len(exp_cat_dd_list): sys.exit("ERROR: Parameter 'exp_nam_dd_list' should either be 'None' or have the same length as 'exp_cat_dd_list'")
# For every exp_cat_dd
for i in range(len(exp_cat_dd_list)):
if exp_nam_list is not None and len(exp_nam_list) != len(exp_cat_list): sys.exit("ERROR: Parameter 'exp_nam_list' should either be 'None' or have the same length as 'exp_cat_list'")
# For every exp_cat
for i in range(len(exp_cat_list)):
# Defining the list variable to pass to run_analysis
exp_cat_dd = exp_cat_dd_list[i]
exp_nam_dd = exp_nam_dd_list[i]
run_analysis(country_dd, haz_cat_dd, return_periods, min_haz_slider, exp_cat_dd, exp_nam_dd, adm_dd, analysis_app_dd, class_edges, save_check_raster)
exp_cat = exp_cat_list[i]
exp_nam = exp_nam_list[i]
run_analysis(country, haz_cat, return_periods, min_haz_slider,
exp_cat, exp_nam, adm, analysis_app, class_edges, save_check_raster)

print("--- %s seconds ---" % (time.time() - start_time))

Expand Down
19 changes: 7 additions & 12 deletions Top-down/parallelization/runAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def zonal_stats_parallel(args):
# Defining the main function to run the analysis
def run_analysis(country: str, haz_cat: str, valid_RPs: list[int],
min_haz_threshold: float, exp_cat: str, exp_nam: str, adm_name: str,
time_horizon: list[int], rcp_scenario: list[str],
analysis_type: str, class_edges: list[float],
save_check_raster: bool):
"""
Expand Down Expand Up @@ -73,28 +72,24 @@ def run_analysis(country: str, haz_cat: str, valid_RPs: list[int],

# Checking which kind of exposed category is being considered...
# If the exposed category is population...
if exp_cat == 'pop':
if exp_cat == 'POP':
damage_factor = mortality_factor
exp_ras = f"{exp_folder}/{country}_WPOP20.tif"
exp_ras = f"{exp_folder}/{country}_POP.tif"
# If the exposed category is builtup area...
elif exp_cat == 'builtup':
elif exp_cat == 'BU':
damage_factor = damage_factor_builtup
exp_ras = f"{exp_folder}/{country}_WSF19.tif"
exp_ras = f"{exp_folder}/{country}_BU.tif"
# If the exposed category is agriculture...
elif exp_cat == 'agri':
elif exp_cat == 'AGR':
damage_factor = damage_factor_agri
exp_ras = f"{exp_folder}/{country}_ESA20_agri.tif"
# If the exposed category is capital stock...
elif exp_cat == 'cstk':
damage_factor = damage_factor_builtup
exp_ras = f"{exp_folder}/{country}_CSTK19.tif"
exp_ras = f"{exp_folder}/{country}_AGR.tif"
# If the exposed category is missing, then give an error
else:
exp_ras = None
ValueError(f"Missing or unknown data layer {exp_cat}")
# If user-specified exposure file name is passed, then
if exp_nam is not None:
exp_cat = str(exp_cat+'_'+exp_nam)
exp_cat = str(exp_cat)
exp_ras = f"{exp_folder}/{country}_{exp_nam}.tif"

# Running the analysis
Expand Down

0 comments on commit 276a57b

Please sign in to comment.