This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlive_mapper.py
63 lines (49 loc) · 2.14 KB
/
live_mapper.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
import standardizer
import MultiNEAT as NEAT
import pickle
import effect
class LiveMapper(object):
def __init__(self, parameter_data):
self.parameter_data = parameter_data
genotype = pickle.loads(self.parameter_data['genotype_pickled'])
self.net = NEAT.NeuralNetwork()
genotype.BuildPhenotype(self.net)
self.standardizer = standardizer.Standardizer([])
self.standardizer.feature_statistics = self.parameter_data['feature_statistics']
self.effect = effect.get_effect_instance(self.parameter_data['args']['effect_names'])
self.effect_parameters = None
def activate(self, *analysis_vector):
neural_input = self.standardize(analysis_vector)
self.net.Flush()
self.net.Input(neural_input)
self.net.Activate()
output = self.net.Output()
neural_output = [min(1.0, max(0.0, x)) for x in output]
self.scale_output(neural_output)
def standardize(self, analysis_vector):
neural_input = []
for i, feature in enumerate(self.parameter_data['experiment_settings']['neural_input_channels']):
neural_input.append(
self.standardizer.get_standardized_value(feature, analysis_vector[i])
)
neural_input.append(1.0) # bias
return neural_input
def scale_output(self, neural_output):
self.effect_parameters = []
# map neural output to the appropriate ranges of the effect parameters
for i in range(self.effect.num_parameters):
mapping = self.effect.parameters[i]['mapping']
min_value = mapping['min_value']
max_value = mapping['max_value']
skew_factor = mapping['skew_factor']
self.effect_parameters.append(
standardizer.Standardizer.get_mapped_value(
normalized_value=neural_output[i],
min_value=min_value,
max_value=max_value,
skew_factor=skew_factor
)
)
return self.effect_parameters
def get_effect_parameter_value(self, i):
return self.effect_parameters[int(i)]