-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_traditional_sdm.py
376 lines (294 loc) · 15.6 KB
/
train_traditional_sdm.py
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
'''
evaluation on custom dataset.
1) generate pseudo-absence samples based on environmental rasters
2) train SDMs with presence-only(PO) and pseudo-absence samples
3) evaluate them with presence-absence(PA) samples
'''
import os
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import joblib
import statsmodels.api as sm
from statsmodels.api import GLM # GLM
from statsmodels.stats.outliers_influence import variance_inflation_factor
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis # LDA (Linear Discriminant Analysis) or FDA (Fisher Discriminant Analysis)
from sklearn.neural_network import MLPClassifier # ANN
from sklearn.tree import DecisionTreeClassifier # CTA
from sklearn.ensemble import RandomForestClassifier # RF
from sklearn.ensemble import GradientBoostingClassifier # GBM
from xgboost import XGBClassifier # XGB
from lightgbm import LGBMClassifier # LGBM
from pygam import LogisticGAM # GAM
import elapid
from elapid import MaxentModel # MaxEnt
from elapid import NicheEnvelopeModel # SRE
from lib.dataset import EnvironmentalDataset
from lib.raster import PatchExtractor
from lib.raster_metadata import raster_metadata
from lib.evaluation import evaluate
from lib.metrics import ValidationMetricsForBinaryClassification
from lib.utils import make_labels, set_reproducibility
# For reproducibility
random_seed = 42
set_reproducibility(random_seed=random_seed)
# SETTINGS
# files
DATASET_PATH = './data/GBIF_Lithobates_catesbeianus.csv'
RASTER_PATH = './data/rasters_KR/'
# exclusion buffer
EXCLUSION_DIST = 10000 # exclusion distance
EXCLUSION_DIST2 = 10000
LOCAL_CRS = 5181 # KR
# csv columns
#ID = 'id'
#LABEL = 'Label'
LATITUDE = 'decimalLatitude'
LONGITUDE = 'decimalLongitude'
# dataset construction
TEST_SIZE = 0.3
TRAIN_SIZE = 0.7 # integer or None
# environmental patches
PATCH_SIZE = 1
# sdm models
MODEL_LIST = ['SRE', 'GLM', 'GAM', 'ANN', 'RF', 'GBM', 'XGB', 'LGBM', 'MaxEnt']
SAVE_MODEL_DIR = None
if SAVE_MODEL_DIR:
os.makedirs(SAVE_MODEL_DIR, exist_ok=True)
# evaluation
METRICS = (ValidationMetricsForBinaryClassification(verbose=True),)
USE_VIF = False
def remove_multicollinearity(X_train, X_test):
tempX_train = np.copy(X_train)
tempX_test = np.copy(X_test)
while True:
vif = [variance_inflation_factor(tempX_train, j) for j in range(tempX_train.shape[1])]
if np.max(vif) < 10 or len(vif) <= 2:
break
max_j = np.argmax(vif)
tempX_train = np.delete(tempX_train, max_j, axis=1)
tempX_test = np.delete(tempX_test, max_j, axis=1)
print(f'removed multicollinearity results by VIF: {vif}')
return tempX_train, tempX_test
if __name__ == '__main__':
# create patch extractor and add all default rasters
extractor = PatchExtractor(RASTER_PATH, raster_metadata=raster_metadata['default'], size=PATCH_SIZE, verbose=True)
extractor.add_all(normalized=True, transform=None, ignore=[])
# READ DATASET
df = pd.read_csv(DATASET_PATH, header='infer', sep=',', low_memory=False)
# presence positions
p_pos = df[[LATITUDE, LONGITUDE]].to_numpy()
# remove redundant data
p_pos = extractor.remove_redundant_positions(raster_name='globcover', pos=p_pos)
# presence labels
p_labels = make_labels(len(p_pos), is_presence=True)
train_p_pos, test_p_pos, train_p_labels, test_p_labels \
= train_test_split(p_pos, p_labels, test_size=TEST_SIZE, train_size=TRAIN_SIZE, random_state=random_seed)
# To train presence/absence model, we samples pseudo-absence points from valid positions
# Valid positions are determined by a raster (study area) and presence positions
train_pa_pos = extractor.get_valid_positions(raster_name='bioclim_1', invalid_pos=train_p_pos, buffer_pos=train_p_pos,
sample_size=8000, drop_nodata=True,
exclusion_dist=EXCLUSION_DIST, local_crs=LOCAL_CRS)
# # NOTE: sre-based pseudo-absence sampling
# train_p_X_samples = np.stack([extractor[(lat, long)] for (lat, long) in train_p_pos], axis=0)
# train_pa_X_samples = np.stack([extractor[(lat, long)] for (lat, long) in train_pa_pos], axis=0)
# # 존재 지역의 환경 변수들을 기준으로 70% 이상 적합한 지역은 의사 부재 위치로 적합하지 않음
# nem = NicheEnvelopeModel(percentile_range=[2.5, 97.5], overlay='average')
# nem.fit(train_p_X_samples, train_p_labels, categorical=None)
# in_range = nem.predict(train_pa_X_samples) > 0.7
# print('"in_range == False" count: ', sum(in_range == False))
# train_pa_pos = train_pa_pos[in_range == False, :]
# under sampling to balance presence/absence samples
train_pa_pos = train_pa_pos[:len(train_p_pos)]
ex_pos = np.concatenate((train_p_pos, train_pa_pos, test_p_pos), axis=0)
bf_pos = np.concatenate((train_p_pos, test_p_pos), axis=0)
test_pa_pos = extractor.get_valid_positions(raster_name='bioclim_1', invalid_pos=ex_pos, buffer_pos=bf_pos,
sample_size=8000, drop_nodata=True,
exclusion_dist=EXCLUSION_DIST2, local_crs=LOCAL_CRS)
# # NOTE: sre-based pseudo-absence sampling
# test_pa_X_samples = np.stack([extractor[(lat, long)] for (lat, long) in test_pa_pos], axis=0)
# in_range = nem.predict(test_pa_X_samples) > 0.7
# print('"in_range == False" count: ', sum(in_range == False))
# test_pa_pos = test_pa_pos[in_range == False, :]
# under sampling to balance presence/absence samples
test_pa_pos = test_pa_pos[:len(test_p_pos)]
# pseudo-absence pos, labels
train_pa_pos = train_pa_pos
train_pa_labels = make_labels(len(train_pa_pos), is_presence=False)
test_pa_pos = test_pa_pos
test_pa_labels = make_labels(len(test_pa_pos), is_presence=False)
# merge presences and pseudo-absences
train_pos = np.concatenate((train_p_pos, train_pa_pos), axis=0)
train_labels = np.concatenate((train_p_labels, train_pa_labels), axis=0)
train_ids = np.arange(len(train_pos))
test_pos = np.concatenate((test_p_pos, test_pa_pos), axis=0)
test_labels = np.concatenate((test_p_labels, test_pa_labels), axis=0)
test_ids = np.arange(len(test_pos))
# constructing pytorch dataset
train_set = EnvironmentalDataset(train_labels, train_pos, train_ids, patch_extractor=extractor)
test_set = EnvironmentalDataset(test_labels, test_pos, test_ids, patch_extractor=extractor)
# print sampled dataset
print('train_set presences : ', len(train_set.labels[train_set.labels == 1]))
print('train_set pseudo-absences : ', len(train_set.labels[train_set.labels == 0]))
print('test_set presences : ', len(test_set.labels[test_set.labels == 1]))
print('test_set pseudo-absences : ', len(test_set.labels[test_set.labels == 0]))
X_train, y_train = train_set.numpy()
X_test, y_test = test_set.numpy()
# Remove Multicollinearity
if USE_VIF:
X_train, X_test = remove_multicollinearity(X_train, X_test)
for model_name in MODEL_LIST:
if model_name == 'GLM':
print(f'Training {model_name}...')
model = GLM(y_train, X_train, family=sm.families.Binomial())
model_results = model.fit()
print('Test: ')
predictions = model_results.predict(X_test)
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
model_results.save(SAVE_MODEL_DIR + "sdm_glm.pkl")
elif model_name == 'GAM':
print(f'Training {model_name}...')
# model = LogisticGAM().fit(X_train, y_train)
model = LogisticGAM().gridsearch(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
model_results.save(SAVE_MODEL_DIR + "sdm_gam.pkl")
elif model_name == 'LDA':
print(f'Training {model_name}...')
model = LinearDiscriminantAnalysis(n_components=1, solver="svd", store_covariance=True)
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
joblib.dump(model, SAVE_MODEL_DIR + 'sdm_lda.pkl')
elif model_name == 'ANN':
print(f'Training {model_name}...')
# model = MLPClassifier(hidden_layer_sizes=(100), solver='adam')
model = MLPClassifier(hidden_layer_sizes=(100), solver='adam',
verbose=True, learning_rate_init=0.001,
n_iter_no_change=10, max_iter=200, random_state=random_seed) # default adam, l2 panelty
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
joblib.dump(model, SAVE_MODEL_DIR + 'sdm_ann.pkl')
elif model_name == 'CTA':
print(f'Training {model_name}...')
model = DecisionTreeClassifier(random_state=random_seed)
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
joblib.dump(model, SAVE_MODEL_DIR + 'sdm_cta.pkl')
elif model_name == 'RF':
print(f'Training {model_name}...')
model = RandomForestClassifier(n_estimators=100, max_depth=17, n_jobs=16, random_state=random_seed)
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1] # Nx2(probs of absences, probs of presences)
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
joblib.dump(model, SAVE_MODEL_DIR + 'sdm_rf.pkl')
elif model_name == 'GBM':
print(f'Training {model_name}...')
model = GradientBoostingClassifier(learning_rate=0.01, n_estimators=100, random_state=random_seed)
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
joblib.dump(model, SAVE_MODEL_DIR + 'sdm_gbm.pkl')
elif model_name == 'XGB':
print(f'Training {model_name}...')
#model = XGBClassifier(n_estimators=500, learning_rate=0.01, max_depth=10, use_label_encoder=False)
model = XGBClassifier(eval_metric='logloss', random_state=random_seed)
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
model.save_model(SAVE_MODEL_DIR + 'sdm_xgb.pkl')
elif model_name == 'LGBM':
print(f'Training {model_name}...')
model = LGBMClassifier(random_state=random_seed, max_depth=4)
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
joblib.dump(model, SAVE_MODEL_DIR + 'sdm_lgbm.pkl')
elif model_name == 'MaxEnt':
print(f'Training {model_name}...')
# model = MaxentModel(
# feature_types = ['linear', 'hinge', 'product'], # the feature transformations
# tau = 0.5, # prevalence scaler
# clamp = True, # set covariate min/max based on range of training data
# scorer = 'roc_auc', # metric to optimize (from sklearn.metrics.SCORERS)
# beta_multiplier = 1.0, # regularization scaler (high values drop more features)
# beta_lqp = 1.0, # linear, quadratic, product regularization scaler
# beta_hinge = 1.0, # hinge regularization scaler
# beta_threshold = 1.0, # threshold regularization scaler
# beta_categorical = 1.0, # categorical regularization scaler
# n_hinge_features = 10, # number of hinge features to compute
# n_threshold_features = 10, # number of threshold features to compute
# convergence_tolerance = 1e-07, # model fit convergence threshold
# use_lambdas = 'best', # set to 'best' (least overfit), 'last' (highest score)
# n_cpus = 4, # number of cpu cores to use
# )
model = MaxentModel()
# MaxEnt uses background samples
b_pos = extractor.get_valid_positions(raster_name='bioclim_1', sample_size=10000)
b_labels = make_labels(len(b_pos), is_presence=False)
merged_pos = np.concatenate((train_p_pos, b_pos), axis=0)
merged_labels = np.concatenate((train_p_labels, b_labels), axis=0)
merged_ids = np.arange(len(merged_pos))
maxent_train_set = EnvironmentalDataset(merged_labels, merged_pos, merged_ids, patch_extractor=extractor)
maxent_X_train, maxent_y_train = maxent_train_set.numpy()
model.fit(maxent_X_train, maxent_y_train)
print('Test: ')
predictions = model.predict(X_test)
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'Saving {model_name}...\n')
elapid.save_object(model, SAVE_MODEL_DIR + 'sdm_maxent.ela')
elif model_name == 'SRE':
print(f'Training {model_name}...')
model = NicheEnvelopeModel(percentile_range=[2.5, 97.5], overlay='intersection')
model.fit(X_train, y_train)
print('Test: ')
predictions = model.predict_proba(X_test)[:, 1]
print(evaluate(predictions, y_test, METRICS, final=True))
if SAVE_MODEL_DIR:
print(f'No Saving option for SRE...\n')
elif model_name == 'Ensemble':
print(f'Training {model_name}...')
models = []
models.append(RandomForestClassifier(random_state=random_seed))
models.append(GradientBoostingClassifier(random_state=random_seed))
models.append(XGBClassifier(eval_metric='logloss', random_state=random_seed))
models.append(LGBMClassifier(random_state=random_seed))
models.append(MLPClassifier(hidden_layer_sizes=(100, 100), solver='adam'))
outputs = []
for mdl in models:
mdl.fit(X_train, y_train)
predictions = mdl.predict_proba(X_test)[:, 1] # Nx2(probs of absences, probs of presences)
outputs.append(predictions)
outputs = np.mean(outputs, axis=0)
print(evaluate(outputs, y_test, METRICS, final=True))
else:
print('Wrong model name.')