-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgenetic_encoders.py
206 lines (158 loc) · 6.66 KB
/
genetic_encoders.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
"""
Code from https://github.com/EpistasisLab/autoqtl
This file contains the class definition for all the genetic encoders.
All the genetic encoder classes inherit the Scikit learn BaseEstimator and TransformerMixin classes to follow the Scikit-learn paradigm.
"""
import numpy as np
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.utils import check_array
class DominantEncoder(TransformerMixin, BaseEstimator ):
"""This class contains the function definition for encoding the input features as a Dominant genetic model.
The encoding used is AA(0)->1, Aa(1)->1, aa(2)->0. """
def fit(self, X, y=None):
"""Do nothing and return the estimator unchanged.
Dummy function to fit in with the sklearn API and hence work in pipelines.
Parameters
----------
X : array-like
"""
return self
def transform(self, X, y=None):
"""Transform the data by applying the Dominant encoding.
Parameters
----------
X : numpy ndarray, {n_samples, n_components}
New data, where n_samples is the number of samples (number of individuals)
and n_components is the number of components (number of features).
y : None
Unused
Returns
-------
X_transformed: numpy ndarray, {n_samples, n_components}
The encoded feature set
"""
X = check_array(X)
map = {0: 1, 1: 1, 2: 0}
mapping_function = np.vectorize(lambda i: map[i] if i in map else i)
X_transformed = mapping_function(X)
return X_transformed
class RecessiveEncoder(TransformerMixin, BaseEstimator ):
"""This class contains the function definition for encoding the input features as a Recessive genetic model.
The encoding used is AA(0)->0, Aa(1)->1, aa(2)->1. """
def fit(self, X, y=None):
"""Do nothing and return the estimator unchanged.
Dummy function to fit in with the sklearn API and hence work in pipelines.
Parameters
----------
X : array-like
"""
return self
def transform(self, X, y=None):
"""Transform the data by applying the Recessive encoding.
Parameters
----------
X : numpy ndarray, {n_samples, n_components}
New data, where n_samples is the number of samples (number of individuals)
and n_components is the number of components (number of features).
y : None
Unused
Returns
-------
X_transformed: numpy ndarray, {n_samples, n_components}
The encoded feature set
"""
X = check_array(X)
map = {0: 0, 1: 1, 2: 1}
mapping_function = np.vectorize(lambda i: map[i] if i in map else i)
X_transformed = mapping_function(X)
return X_transformed
class HeterosisEncoder(TransformerMixin, BaseEstimator ):
"""This class contains the function definition for encoding the input features as a Heterozygote Advantage genetic model.
The encoding used is AA(0)->0, Aa(1)->1, aa(2)->0. """
def fit(self, X, y=None):
"""Do nothing and return the estimator unchanged.
Dummy function to fit in with the sklearn API and hence work in pipelines.
Parameters
----------
X : array-like
"""
return self
def transform(self, X, y=None):
"""Transform the data by applying the Heterosis encoding.
Parameters
----------
X : numpy ndarray, {n_samples, n_components}
New data, where n_samples is the number of samples (number of individuals)
and n_components is the number of components (number of features).
y : None
Unused
Returns
-------
X_transformed: numpy ndarray, {n_samples, n_components}
The encoded feature set
"""
X = check_array(X)
map = {0: 0, 1: 1, 2: 0}
mapping_function = np.vectorize(lambda i: map[i] if i in map else i)
X_transformed = mapping_function(X)
return X_transformed
class UnderDominanceEncoder(TransformerMixin, BaseEstimator ):
"""This class contains the function definition for encoding the input features as a Under Dominance genetic model.
The encoding used is AA(0)->2, Aa(1)->0, aa(2)->1. """
def fit(self, X, y=None):
"""Do nothing and return the estimator unchanged.
Dummy function to fit in with the sklearn API and hence work in pipelines.
Parameters
----------
X : array-like
"""
return self
def transform(self, X, y=None):
"""Transform the data by applying the Heterosis encoding.
Parameters
----------
X : numpy ndarray, {n_samples, n_components}
New data, where n_samples is the number of samples (number of individuals)
and n_components is the number of components (number of features).
y : None
Unused
Returns
-------
X_transformed: numpy ndarray, {n_samples, n_components}
The encoded feature set
"""
X = check_array(X)
map = {0: 2, 1: 0, 2: 1}
mapping_function = np.vectorize(lambda i: map[i] if i in map else i)
X_transformed = mapping_function(X)
return X_transformed
class OverDominanceEncoder(TransformerMixin, BaseEstimator ):
"""This class contains the function definition for encoding the input features as a Over Dominance genetic model.
The encoding used is AA(0)->1, Aa(1)->2, aa(2)->0. """
def fit(self, X, y=None):
"""Do nothing and return the estimator unchanged.
Dummy function to fit in with the sklearn API and hence work in pipelines.
Parameters
----------
X : array-like
"""
return self
def transform(self, X, y=None):
"""Transform the data by applying the Heterosis encoding.
Parameters
----------
X : numpy ndarray, {n_samples, n_components}
New data, where n_samples is the number of samples (number of individuals)
and n_components is the number of components (number of features).
y : None
Unused
Returns
-------
X_transformed: numpy ndarray, {n_samples, n_components}
The encoded feature set
"""
X = check_array(X)
map = {0: 1, 1: 2, 2: 0}
mapping_function = np.vectorize(lambda i: map[i] if i in map else i)
X_transformed = mapping_function(X)
return X_transformed