Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import pandas as pd
from settings import *
from utilities.initialise_log import save_log
class check_general_settings():
def __init__(self):
if training_dataset or map_generation:
if os.path.isdir(path_train):
save_path = path_train + '/check_user_input.log'
save_path = os.path.dirname(path_train) + '/check_user_input.log'
if os.path.isdir(path_pred):
save_path = path_pred + '/check_user_input.log'
save_path = os.path.dirname(path_pred) + '/check_user_input.log'
save_path = '/check_user_input.log'
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
if os.path.exists(save_path):
os.remove(save_path)
self.logger = save_log(save_path)
self.logger.info("Start checking user input")
self.error = False
self.set_up_dic()
self.check_bools()
self.check_list()
self.check_int()
self.check_int_float()
self.check_string()
self.check_path()
self.check_bb()
self.check_if_right_params_are_set()
self.check_extension()
self.check_path_extension_geosummary()
for handler in self.logger.handlers:
handler.close()
self.logger.removeHandler(handler)
def check_if_right_params_are_set(self):
if training_dataset is None and prediction_dataset is None and map_generation is None:
self.logger.error('Specify a purpose of the run! Set either training_dataset, prediction_dataset and/or map_generation')
self.error = True
if None in [crs, no_value, random_seed, resolution]:
self.logger.error('Set the general settings crs, no_value, random_seed and resolution!')
self.error = True
if training_dataset:
if train_from_scratch is None and train_delete is None:
self.logger.error('Speciy whether you want to generate training dataset from scratch or add/remove feature(s)')
self.error = True
else:
if None in [preprocessing, data_summary_path, key_to_include_path, path_train, path_landslide_database, ID, landslide_database_x, landslide_database_y, path_nonls_locations, num_nonls, nonls_database_x, nonls_database_y]:
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
self.logger.error('Speciy all necessary parameters for training dataset generation!')
self.error = True
if prediction_dataset:
if pred_from_scratch is None and pred_delete is None:
self.logger.error('Speciy whether you want to generate prediction dataset from scratch or add/remove feature(s)')
self.error = True
else:
if None in [data_summary_path, key_to_include_path, bounding_box, path_pred]:
self.logger.error('Speciy all necessary parameters for prediction dataset generation!')
self.error = True
if map_generation:
if None in [path_ml, size, not_included_pred_data, not_included_train_data, num_trees, criterion, depth, model_to_save, model_to_load, model_database_dir, parallel]:
self.logger.error('Speciy all necessary parameters for map generation!')
self.error = True
def set_up_dic(self):
self.dic = {}
self.dic['bool'] = [training_dataset, train_from_scratch, train_delete, prediction_dataset, pred_from_scratch, pred_delete, map_generation, parallel]
self.dic['path'] = [path_ml, data_summary_path, key_to_include_path, path_train, path_landslide_database, path_nonls_locations, path_pred, model_database_dir]
self.dic['str'] = [crs, ID, landslide_database_x, landslide_database_y, nonls_database_x, nonls_database_y, criterion, model_to_save, model_to_load]
self.dic['int'] = [resolution, random_seed, num_nonls, num_trees, depth]
self.dic['int_float'] = [size, no_value]
self.dic['list'] = [bounding_box, not_included_pred_data, not_included_train_data]
self.dic_steps = {}
self.dic_steps['general'] = []
self.dic_steps['run_purpose'] = [training_dataset, prediction_dataset, map_generation]
def check_extension(self):
for path in [data_summary_path, key_to_include_path, path_landslide_database, path_train]:
if path is not None:
if '.' in path:
if len(path.split('.')) != 2:
self.logger.error(path + ': Paths must not contain full stops!')
else:
if path.split('.')[1] != 'csv':
self.logger.error(path + ': wrong file format! Needs to be csv')
self.error = True
else:
self.logger.warning(path + ': Directory is given. Generic file name will be used')
for path in [path_pred, path_nonls_locations]:
if path is not None:
if '.' in path:
if len(path.split('.')) != 2:
self.logger.error(path + ': Paths must not contain full stops!')
else:
if path.split('.')[1] != 'nc':
self.logger.error(path + ': wrong file format! Needs to be nc')
self.error = True
else:
self.logger.warning(path + ': Directory is given. Generic file name will be used')
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
def check_bools(self):
self.logger.info("Start checking bools")
for key in self.dic['bool']:
if key is not None:
if type(key) is not bool:
self.logger.info(key + ': not a bool')
self.error = True
def check_list(self):
self.logger.info("Start checking list")
for key in self.dic['list']:
if key is not None:
if type(key) is not list:
self.logger.info(key + ': not a list')
self.error = True
def check_int(self):
self.logger.info("Start checking integers")
for key in self.dic['int']:
if key is not None:
if type(key) is not int:
self.logger.info(key + ': not an integer')
self.error = True
def check_int_float(self):
self.logger.info("Start checking integers and floats")
for key in self.dic['int_float']:
if key is not None:
if type(key) is not int and type(key) is not float:
self.logger.info(key + ': not an integer or float')
self.error = True
def check_string(self):
self.logger.info("Start checking strings")
for key in self.dic['str']:
if key is not None:
if type(key) is not str:
self.logger.info(key + ': not a string')
self.error = True
def check_path(self):
self.logger.info("Start checking paths")
for key in self.dic['path']:
if key is not None:
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
if type(key) is not str:
self.logger.info(key + ': path is not a string')
self.error = True
else:
if key == path_train and training_dataset is True:
pass
elif key == path_pred and prediction_dataset is True:
pass
else:
if not os.path.exists(key):
self.logger.error(key + ': path could not be found!')
self.error = True
def check_bb(self):
if bounding_box is not None:
if bounding_box[1] >= bounding_box[0]:
self.logger.error('Careful! South coordinate north of north coordinate!')
self.error = True
if bounding_box[2] >= bounding_box[3]:
if (((bounding_box[2] < 0 and bounding_box[2] > -10) and (bounding_box[3] > 0 and bounding_box[3] < 10))
or ((bounding_box[2] > 0 and bounding_box[2] > 170) and (bounding_box[3] < 0 and bounding_box[3] < -170))):
self.logger.warning('Careful! Please check east and west coordinates!')
else:
self.logger.error('Careful! West coordinate east of east coordinate!')
def check_path_extension_geosummary(self):
self.logger.info('Start checking paths in geospatial data summary')
if data_summary_path is not None and key_to_include_path is not None:
if os.path.exists(data_summary_path) and os.path.exists(key_to_include_path):
if data_summary_path.split('.')[1] != 'csv' and key_to_include_path.split('.')[1] != 'csv':
summary = pd.read_csv(data_summary_path)
keys_to_include = pd.read_csv(key_to_include_path)
for key in list(keys_to_include['keys_to_include']):
idx = list(summary['keys']).index(key)
if summary.at[idx, 'path'].split('.')[1] not in ['nc', 'tif', 'tiff']:
self.logger.error(key + ': Wrong file format!')
self.error = True
if not os.path.exists(summary.at[idx, 'path']):
self.logger.error(key + ': File cannot be found!')
self.error = True