-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxdm.py
229 lines (160 loc) · 6.61 KB
/
maxdm.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
"""MAXDM - Maxim's Species Distribution Models
SDM estimators to predict patterns based on environmental variable similarity to occurence sites.
This module implements the following estimators:
* Geometric Median Similarity (GMS)
* nearest neighbours similarity (KNNS).
These similarity methods are applicable to presence-only data.
"""
import warnings
import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator
from sklearn.compose import ColumnTransformer
from sklearn.metrics import pairwise_distances
from sklearn.preprocessing import MinMaxScaler
__author__ = 'Maxim Jaffe'
# TODO code repetition between GMS and NNS, maybe create mixin class?
class GMS(BaseEstimator):
"""GMS - Geometric Median Similarity estimator
Estimates species distribution based on similarity to a geometric median of occurences based on scaled distance.
Parameters
----------
dist: str, default='l1'
Distance metric. See scikit-learn.
Notes
-----
Geometric median of all occurences is calculated based on scaled distance between occurence sites, where each variable is min-max scaled to a [0, 1] range.
Similarity is calculated as:
1 - (scaled distance / number of variables)
This model is should be similar to BIOCLIM model, but uses a geoemtric median instead of an centroid (mean).
Warning
-------
When predicting assumes variable range is within that of the fitted model variable range.
"""
_minmax_scaler = MinMaxScaler()
def __init__(self, metric='l1'):
self.metric = metric
def fit(self, X, Y):
"""Fit species distribution model
Note that X and Y need to match in len
Parameters
----------
X: pandas.Dataframe
Variables
Y: pandas.Dataframe
Occurrences
"""
assert len(X) == len(Y)
self.columns = X.columns
# Fit column min-max scaler
self.minmax_col_scaler = ColumnTransformer(
transformers=[('mm', self._minmax_scaler , self.columns)])
self.minmax_col_scaler.fit(X)
# Scale variables
X_scaled = self.minmax_col_scaler.transform(X)
# Select rows with ocurrences
X_occ = X_scaled[Y == 1]
# Find geometric median
X_occ_dist_matrix = pairwise_distances(X_occ, metric=self.metric)
X_occ_dist_sum = X_occ_dist_matrix.sum(axis=0)
self.geom_median = X_occ[[X_occ_dist_sum.argmin()],:]
def predict(self, X):
"""Predict species distribution
Parameters
----------
X: pandas.Dataframe
Variables
Returns
-------
pandas.Dataframe
Similarity
"""
# Check data has the same variables
assert (X.columns == self.columns).all()
# Scale variables
X_scaled = self.minmax_col_scaler.transform(X)
# Check if any value is not within [0,1] range
# TODO need to test this check
if np.logical_or(X_scaled < 0, X_scaled > 1).any():
warnings.warn('Some values are beyond fitted model ranges.')
# Calculate distance to geometric median
dist = pairwise_distances(
X_scaled, self.geom_median, metric=self.metric)
# Calculate similarity
sim = 1 - (dist / len(self.columns))
# Convert similarity output to dataframe while keeping input indexes
df = pd.DataFrame(index=X.index)
df['sim'] = sim
return df
class KNNS(BaseEstimator):
"""KNNS - K Neareast Neighbour Similarity estimator
Estimates species distribution based on similarity to nearest neighbour in occurences based on scaled distance.
Parameters
----------
dist: str, default='l1'
Distance metric. See scikit-learn.
Notes
-----
Caculated based on scaled distances, where each variable is min-max scaled to a [0, 1] range. Mean scaled distance to k nearest neighbours (knn) is used to calculate similarity.
Similarity is calculated as:
1 - (mean(knn scaled distances) / number of variables)
This model is should be similar to DOMAIN model when k = 1.
Warning
-------
When predicting assumes variable range is within that of the fitted model variable range.
"""
_minmax_scaler = MinMaxScaler()
def __init__(self, k=1, metric='l1'):
self.k = k
self.metric = metric
def fit(self, X, Y):
"""Fit species distribution model
Note that X and Y need to match in len
Parameters
----------
X: pandas.Dataframe
Variables
Y: pandas.Dataframe
Occurrences
"""
assert len(X) == len(Y)
self.columns = X.columns
# Fit column min-max scaler
self.minmax_col_scaler = ColumnTransformer(
transformers=[('mm', self._minmax_scaler , self.columns)])
self.minmax_col_scaler.fit(X)
# Scale variables
X_scaled = self.minmax_col_scaler.transform(X)
# Select rows with ocurrences
self.occ_scaled = X_scaled[Y == 1]
def predict(self, X):
"""Predict species distribution
Parameters
----------
X: pandas.Dataframe
Variables
Returns
-------
pandas.Dataframe
Similarity
"""
# Check data has the same variables
assert (X.columns == self.columns).all()
# Scale variables
X_scaled = self.minmax_col_scaler.transform(X)
# Check if any value is not within [0,1] range
# TODO need to test this check
if np.logical_or(X_scaled < 0, X_scaled > 1).any():
warnings.warn('Some values are beyond fitted model ranges.')
# Calculate distances to occurences
dist = pairwise_distances(X_scaled, self.occ_scaled, metric=self.metric)
# Select K nearest neighbours
knn = np.sort(dist, axis=1)[:,:self.k]
# Calculate mean of K nearest neighbours
mean_dist = knn.mean(axis=1)
# Calculate similarity
sim = 1 - (mean_dist / len(self.columns))
# Convert similarity output to dataframe while keeping input indexes
df = pd.DataFrame(index=X.index)
df['sim'] = sim
return df