Repository navigation
Expand file tree
/
Copy pathsolutionspace.py
More file actions
374 lines (344 loc) · 14.9 KB
/
Copy pathsolutionspace.py
File metadata and controls
374 lines (344 loc) · 14.9 KB
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
# Solution Space Class
from typing import Tuple
from mne_features.univariate import get_univariate_funcs
from mne_features.bivariate import get_bivariate_funcs
from GeFSalg_BS.dna import Gene, Genotype
from GeFSalg_BS.utils import Utils
from inspect import getfullargspec
import numpy as np
class SolutionSpace:
_rng = np.random.default_rng()
def __init__(self, utils: Utils) -> None:
self._gene = dict()
self._genotype = None
self._params = utils.params
self.genotype_len = range(utils.genotype_len)
self._un_funcs = get_univariate_funcs(utils.epochs.info['sfreq'])
self._bi_funcs = get_bivariate_funcs(utils.epochs.info['sfreq'])
def build_population(self, size: int) -> list:
new_population = list()
for _ in range(size):
self.build_genotype()
new_population.append(self._genotype)
return new_population
def build_genotype(self) -> Genotype:
genes = []
for _ in self.genotype_len:
self.build_gene()
genes.append(self._gene)
self._genotype = Genotype(tuple(genes))
return self._genotype
def build_gene(self) -> Gene:
self._gene = dict()
# Parameters for the function
self._gene["params"] = dict()
# Index for special functions
self._gene["index"] = -1
# Channels to be used
self._gene["source"] = None
self.choose_feature()
# Selected Function
func = self._gene["selected_funcs"]
# Specific parameters obtention
if func in self._un_funcs.keys():
self.args = set(getfullargspec(self._un_funcs[func])[0])
elif func in self._bi_funcs.keys():
self.args = set(getfullargspec(self._bi_funcs[func])[0])
else:
raise Exception
self.args.discard('data')
#self._set_gene()
self.choose_source()
self.choose_params()
self._gene = Gene(
self._gene['selected_funcs'],
tuple(self._gene['params'].items()),
self._gene['source'],
self._gene['index']
)
return self._gene
def reset_gene(self,gene):
self._gene = dict()
# Parameters for the function
self._gene["params"] = dict()
# Index for special functions
self._gene["index"] = -1
# Channels to be used
self._gene["source"] = None
self._gene['selected_funcs'] = gene.feature
# Selected Function
func = self._gene["selected_funcs"]
# Specific parameters obtention
if func in self._un_funcs:
self.args = set(getfullargspec(self._un_funcs[func])[0])
elif func in self._bi_funcs:
self.args = set(getfullargspec(self._bi_funcs[func])[0])
else:
raise Exception
self.args.discard('data')
#self._set_gene()
self.choose_source()
self.choose_params()
self._gene = Gene(
self._gene['selected_funcs'],
tuple(self._gene['params'].items()),
self._gene['source'],
self._gene['index']
)
return self._gene
def choose_feature(self) -> None:
self._gene['selected_funcs'] = self._rng.choice(self._params['funcs'])
def _get_band(self, case: int) -> Tuple:
key = 'freq_bands'
if case == 1:
selection = self._rng.choice(len(self._params[key]), size = case, replace = False)
else:
selection = self._rng.choice(len(self._params[key]), size = case, replace = False)
band1,band2 = (self._params[key][i] for i in selection)
while max(0, min(band1[1], band2[1]) - max(band1[0], band2[0])) != 0:
selection = self._rng.choice(len(self._params[key]), size = case, replace = False)
band1,band2 = (self._params[key][i] for i in selection)
return selection
def choose_source(self) -> None:
func = self._gene["selected_funcs"]
# 1 canal y 2 bandas
# 2 canales y 1 banda
# 1 canal 1 banda
if 'freq_bands' in self.args:
key = 'freq_bands'
case = self._rng.integers(1,3)
selection = self._get_band(case)
value = {f'band {i}': self._params[key][i] for i in selection}
self._gene["params"].update({key: tuple(value.items())})
self.args.discard(key)
# Special channel selection for univariate functions
# with self contained frequency settings
key = 'original_channels'
if case == 1:
case = self._rng.integers(1,3)
else:
case = 1
selection = self._rng.choice(self._params[key], size = case, replace = False)
self._gene["source"] = tuple(selection)
elif {'fmin','fmax'}.issubset(self.args):
key = 'freq_bands'
key1 = 'fmin'
key2 = 'fmax'
selection = self._rng.choice(len(self._params[key]))
fmin, fmax = self._params[key][selection]
self._gene["params"].update({key1: fmin})
self._gene["params"].update({key2: fmax})
self.args.discard(key1)
self.args.discard(key2)
# Special channel selection for univariate functions
# with self contained frequency settings
key = 'original_channels'
case = self._rng.integers(1,3)
selection = self._rng.choice(self._params[key], size = case, replace = False)
self._gene["source"] = tuple(selection)
# Channel selection for bivariate functions
# with no self contained frequency settings
elif func in self._bi_funcs.keys():
key = 'original_channels'
ch_selection = self._rng.choice(self._params[key], size = 2, replace = False)
key = 'freq_bands'
index = self._rng.choice(len(self._params[key]))
fb_selection = self._params[key][index]
src = [f'{ch}{fb_selection}' for ch in ch_selection]
self._gene["source"] = tuple(src)
# Channel selection for univariate functions
# with no self contained frequency settings
else:
key = 'original_channels'
case = self._rng.integers(1,3)
ch_selection = self._rng.choice(self._params[key], size = case, replace = False)
key = 'freq_bands'
if case == 1:
case = self._rng.integers(1,3)
else:
case = 1
indexes = self._get_band(case)
fb_selection = [self._params[key][i] for i in indexes]
if len(ch_selection) == 1 and len(fb_selection) == 1:
self._gene["source"] = f'{ch_selection[0]}{fb_selection[0]}'
elif len(ch_selection) == 1 and len(fb_selection) == 2:
src = [f'{ch_selection[0]}{fb}' for fb in fb_selection]
self._gene["source"] = tuple(src)
elif len(ch_selection) == 2 and len(fb_selection) == 1:
src = [f'{ch}{fb_selection[0]}' for ch in ch_selection]
self._gene["source"] = tuple(src)
def choose_params(self) -> None:
# Set default parameters
# All parameters that are predefined
# and should stay always equal are default
# Default settings, not chosen randomly
if 'normalize' in self.args:
key = 'normalize'
self._gene["params"].update({key: False})
self.args.discard(key)
if 'ratios' in self.args:
key = 'ratios'
self._gene["params"].update({key: None})
self.args.discard(key)
if 'include_diag' in self.args:
key = 'include_diag'
self._gene["params"].update({key: False})
self.args.discard(key)
if 'with_intercept' in self.args:
key = 'with_intercept'
self._gene["params"].update({key: True})
self.args.discard(key)
# Index selection for special function
i = self._rng.integers(4)
self._gene["index"] = int(i)
if 'with_eigenvalues' in self.args:
key = 'with_eigenvalues'
self._gene["params"].update({key: False})
self.args.discard(key)
# Index selection for special function
i = self._rng.integers(3)
self._gene["index"] = int(i)
# Set simple parameters
# All parameters which setting does
# not need calculations are simple
for arg in self.args:
try:
val = self._rng.choice(self._params[arg])
self._gene["params"].update({arg: val})
except KeyError:
pass
# Set all Gene object attributes
def _set_gene(self) -> None:
# Selected Function
func = self._gene["selected_funcs"]
# Parameters for the function
self._gene["params"] = dict()
# Channels to be used
self._gene["source"] = None
# Index for special functions
self._gene["index"] = -1
# Specific parameters obtention
if func in self._un_funcs.keys():
args = set(getfullargspec(self._un_funcs[func])[0])
elif func in self._bi_funcs.keys():
args = set(getfullargspec(self._bi_funcs[func])[0])
else:
raise Exception
args.discard('data')
# Set default parameters
# All parameters that are predefined
# and should stay always equal are default
# Default settings, not chosen randomly
if 'normalize' in args:
key = 'normalize'
self._gene["params"].update({key: False})
args.discard(key)
if 'ratios' in args:
key = 'ratios'
self._gene["params"].update({key: None})
args.discard(key)
if 'include_diag' in args:
key = 'include_diag'
self._gene["params"].update({key: False})
args.discard(key)
if 'with_intercept' in args:
key = 'with_intercept'
self._gene["params"].update({key: True})
args.discard(key)
# Index selection for special function
i = self._rng.integers(4)
self._gene["index"] = int(i)
if 'with_eigenvalues' in args:
key = 'with_eigenvalues'
self._gene["params"].update({key: False})
args.discard(key)
# Index selection for special function
i = self._rng.integers(3)
self._gene["index"] = int(i)
# Set complex parameters
# All parameters which setting
# involves calculations are complex
if 'q' in args:
key = 'q'
case = self._rng.integers(1,3)
value = self._rng.choice(self._params[key], size = case, replace = False)
self._gene["params"].update({key: tuple(value)})
args.discard(key)
if 'edge' in args:
key = 'edge'
case = self._rng.integers(1,3)
value = self._rng.choice(self._params[key], size = case, replace = False)
self._gene["params"].update({key: tuple(value)})
args.discard(key)
# Frequency settings
if 'freq_bands' in args:
key = 'freq_bands'
case = self._rng.integers(1,3)
selection = self._rng.choice(len(self._params[key]), size = case, replace = False)
value = {f'band {i}': self._params[key][i] for i in selection}
self._gene["params"].update({key: tuple(value.items())})
args.discard(key)
# Special channel selection for univariate functions
# with self contained frequency settings
key = 'original_channels'
case = self._rng.integers(1,3)
selection = self._rng.choice(self._params[key], size = case, replace = False)
self._gene["source"] = tuple(selection)
elif {'fmin','fmax'}.issubset(args):
key = 'freq_bands'
key1 = 'fmin'
key2 = 'fmax'
selection = self._rng.choice(len(self._params[key]))
fmin, fmax = self._params[key][selection]
self._gene["params"].update({key1: fmin})
self._gene["params"].update({key2: fmax})
args.discard(key1)
args.discard(key2)
# Special channel selection for univariate functions
# with self contained frequency settings
key = 'original_channels'
case = self._rng.integers(1,3)
selection = self._rng.choice(self._params[key], size = case, replace = False)
self._gene["source"] = tuple(selection)
# Channel selection for bivariate functions
# with no self contained frequency settings
elif func in self._bi_funcs.keys():
key = 'original_channels'
ch_selection = self._rng.choice(self._params[key], size = 2, replace = False)
key = 'freq_bands'
indexes = self._rng.choice(len(self._params[key]), size = 2)
fb_selection = [self._params[key][i] for i in indexes]
src = [f'{ch}{fb}' for ch, fb in zip(ch_selection, fb_selection)]
self._gene["source"] = tuple(src)
# Channel selection for univariate functions
# with no self contained frequency settings
else:
key = 'original_channels'
case = self._rng.integers(1,3)
ch_selection = self._rng.choice(self._params[key], size = case, replace = False)
key = 'freq_bands'
case = self._rng.integers(1,3)
indexes = self._rng.choice(len(self._params[key]), size = case, replace = False)
fb_selection = [self._params[key][i] for i in indexes]
if len(ch_selection) == 1 and len(fb_selection) == 1:
self._gene["source"] = f'{ch_selection[0]}{fb_selection[0]}'
elif len(ch_selection) == 1 and len(fb_selection) == 2:
src = [f'{ch_selection[0]}{fb}' for fb in fb_selection]
self._gene["source"] = tuple(src)
elif len(ch_selection) == 2 and len(fb_selection) == 1:
src = [f'{ch}{fb_selection[0]}' for ch in ch_selection]
self._gene["source"] = tuple(src)
elif len(ch_selection) == 2 and len(fb_selection) == 2:
src1 = [f'{ch_selection[0]}{fb}' for fb in fb_selection]
src2 = [f'{ch_selection[1]}{fb}' for fb in fb_selection]
src = src1 + src2
self._gene["source"] = tuple(src)
# Set simple parameters
# All parameters which setting does
# not need calculations are simple
for arg in args:
try:
val = self._rng.choice(self._params[arg])
self._gene["params"].update({arg: val})
except KeyError:
pass