-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_isimipFL_module_load.R
178 lines (168 loc) · 8.99 KB
/
validation_isimipFL_module_load.R
1
2
3
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
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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# validation_isimipFL_module_load.R
# set of functions to load output of ISIMIP FLOOD CLIMADA
### ---------------------------------------------------------------------------
### ---------------------------------------------------------------------------
### LOADING FUNCTIONS
### ---------------------------------------------------------------------------
### ---------------------------------------------------------------------------
# source("validation_isimipFL_module_load.R")
require(tidyverse)
load_JRC_eval <- function(RegionID,
years_range = c(1971,2010),
hazard_protection="flopros",
subtract_matsiro=1,
entity_year=0) {
# load damages computed with the JRC damage functions
# test <- load_JRC_eval("NAM")
filename_head <- paste0('damages_JRC_', RegionID, '_', years_range[1], '-', years_range[2])
filename_haz = paste0('Haz-Prot', hazard_protection, '-subMATSIRO', subtract_matsiro)
filename_ent = paste0('Entity-Year', entity_year)
output_eval_filename = paste0(file.path(DATA_PATH_FLDCAL, 'damages_JRCdamFun/'), filename_head, '_', filename_haz, '_', filename_ent, '_eval.csv')
dat <- read_csv(output_eval_filename, col_types = cols()) %>% mutate(damage=round(damage))
return(dat)
}
get_all_calib_filenames <- function(regionID, calib_method_list = get_calib_methods_tibble()) {
fn <- sapply(1:nrow(calib_method_list), function(i) {
get_calib_filename(regionID,
cost_function = calib_method_list$cost_function[i],
exclude_years_0totals = calib_method_list$exclude_years_0totals[i],
which_file = "mat")
})
calib_method_list %>% mutate(filename=fn)
}
get_calib_filename <- function(regionID,
calib_method="patternsearch",
cost_function,
which_file,
years_range=c(1992,2010),
calib_options=NULL,
n_per_dim=20,underestimation_factor=2,
hazard_protection='flopros',subtract_matsiro=1,entity_year=0,keep_countries_0emdat=2,
remove_years_0emdat=0,remove_years_0YDS=list(do=0),exclude_years_0totals=0,
MM_how='MMM',pars_range=list(c(0.0001,1),c(0.0001,5))) {
# function to retrieve an output file name from the calibration
# if calib_options is not passed, set default values
if (is.null(calib_options)) {
if (calib_method == 'regular_sampling') {
calib_options <- list(n_per_dim=20)
} else if (calib_method == 'patternsearch') {
calib_options <- list(random=0,
nstart=3,
InitialMeshSize=0.25,
step_tolerance=0.001)
}
}
if (calib_method == 'regular_sampling') {
filename_calib_method <- paste0(calib_method,'-',n_per_dim)
} else if (calib_method == 'patternsearch') {
filename_calib_method <- paste0(calib_method,
c('-reg','-rand')[calib_options$random+1], calib_options$nstart,
'-mesh', calib_options$InitialMeshSize,
'-step', calib_options$step_tolerance)
} else {
stop("input parameter calib_method should be one of 'regular_sampling' or 'patternsearch'")
}
filename_calib <- paste0('calib_',regionID,'_',years_range[1], '-', years_range[2], '_',
cost_function, '-uf', underestimation_factor, '-',
MM_how, '-', filename_calib_method)
filename_haz <- paste0('Haz-Prot', hazard_protection, '-subMATSIRO', subtract_matsiro)
filename_ent <- paste0('Entity-Year', entity_year)
filename_filter <- paste0('Filters-Country', keep_countries_0emdat,
'-emdat', remove_years_0emdat,
'-YDS', remove_years_0YDS$do)
if (remove_years_0YDS$do) {
filename_filter <- paste0(filename_filter, '-t', remove_years_0YDS$threshold,
'-w', remove_years_0YDS$what,
'-m', remove_years_0YDS$min_val)
}
if (exclude_years_0totals) {
filename_filter <- paste0(filename_filter, '_Filter-RegYears0')
}
filename_pars <- paste0('pars',
format(pars_range[[1]][1], scientific = F), '-',
format(pars_range[[1]][2], scientific = F), '-',
format(pars_range[[2]][1], scientific = F), '-',
format(pars_range[[2]][2], scientific = F))
which_file_list <- list(mat=c('calibration_opt_params/','.mat'),
steps=c('calibration_steps/','_steps.dat'),
eval=c('calibration_eval/','_eval.csv'))
if (!(which_file %in% names(which_file_list))) {
stop(paste("input parameter 'which_file' should have one of the following value : ", paste(names(which_file_list), collapse=", ")))
}
path_in <- DATA_PATH_FLDCAL
filename <- paste0(path_in, "/", which_file_list[[which_file]][1], filename_calib, '_',
filename_haz, '_', filename_ent, '_', filename_filter, '_',
filename_pars, which_file_list[[which_file]][2])
return(filename)
}
load_calib_steps <- function(...) {
filename <- get_calib_filename(which_file='steps',...)
dat <- read_delim(filename, delim=" ")
return(dat)
}
load_calib_params <- function(...) {
library(h5)
library(hdf5r)
filename <- get_calib_filename(which_file='mat',...)
file.h5 <- H5File$new(filename, mode = "r")
optimal_pars <- file.h5[["optimal_pars"]]$read()
return(optimal_pars)
}
load_calib_eval <- function(...) {
filename <- get_calib_filename(which_file='eval',...)
dat <- read_csv(filename, col_types = cols()) %>% mutate(damage=round(damage),
used_in_calibration=as.logical(used_in_calibration))
return(dat)
}
load_all_evals_one_region <- function(regionID, regSum_only=FALSE, calib_only=FALSE) {
calib_methods <- get_calib_methods_tibble()
for (i in 1:nrow(calib_methods)) {
if (i==1) {
data_calib <- call_fun_by_calibMethod(calib_methods$calibration_method_names[i], load_calib_eval, regionID=regionID) %>%
mutate(damage_source=ifelse(dataset=="EM-DAT", "EM-DAT",calib_methods$calibration_method_names[i]))
} else {
data_calib <- data_calib %>% bind_rows(call_fun_by_calibMethod(calib_methods$calibration_method_names[i], load_calib_eval, regionID=regionID) %>%
mutate(damage_source=ifelse(dataset=="EM-DAT", "EM-DAT",calib_methods$calibration_method_names[i])))
}
}
if (calib_only) {
data_calib <- data_calib %>% filter(used_in_calibration==T)
}
if (any(is.na(data_calib$damage) & data_calib$dataset!='EM-DAT')) {
warning(paste0("** WARNING ** some damages are missing (calib_eval file for regionID ",regionID,") *****"))
}
data_JRC <- load_JRC_eval(regionID) %>%
filter(dataset!='EM-DAT') %>%
mutate(damage_source="JRC")
if (calib_only) {
data_JRC <- data_JRC %>% right_join(data_calib %>% select(country,year) %>% unique())
}
if (any(is.na(data_JRC$damage))) {
warning(paste0("** WARNING ** some damages are missing (JRC file for regionID ",regionID,") *****"))
}
data <- bind_rows(data_calib, data_JRC) %>%
unique() %>%
mutate(damage_source=factor(damage_source, levels=c("EM-DAT","JRC",calib_methods$calibration_method_names)))
# lines below not necessary: if any country has 'NA', the whole region will have NA for that year, dataset and damage_source.
# Also, NAs should will only appear in EM-DAT, by definition
# # filter out NAs to keep only the same set of countries for the regional sum
# if (common_country_years_only) {
# data %>% spread(damage_source,damage) %>% group_by(country, year) %>% summarise(nobs=sum(!is.na(`EM-DAT`)),nJRC=)
# data %>% group_by(country,damage_source) %>% summarise(mean_damage=mean(damage)) %>% filter(is.na(mean_damage)) %>% ungroup() %>%select(country) %>% unique()
# }
# add regional sum data
data_sum <- data %>%
group_by(year, dataset,damage_source) %>%
summarise(damage=sum(damage)) %>%
ungroup() %>%
right_join(data %>% select(year,dataset,damage_source) %>% unique() %>% mutate(used_in_calibration=year>=1992)) %>%
mutate(country=paste0("ALL (",regionID,")"))
data_sum <- data_sum[,names(data)]
if (!regSum_only) {
data_sum <- data %>% bind_rows(data_sum)
}
data_sum <- data_sum %>%
add_column(region=regionID) %>%
add_MMM()
return(data_sum)
}