Repository navigation
Expand file tree
/
Copy pathmodel_base.py
More file actions
219 lines (186 loc) · 7.69 KB
/
Copy pathmodel_base.py
File metadata and controls
219 lines (186 loc) · 7.69 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
import pickle, re
class Node:
def __init__(self,
name=None,
pattern=('stride', (1, 1)),
activation='relu',
bias=3.5,
bias_fixed = False,
time_constant=None,
time_constant_fixed=False):
self.name = name
self.pattern = pattern
self.activation = activation
self.bias = bias
self.bias_fixed = bias_fixed
self.time_constant = time_constant
self.time_constant_fixed = time_constant_fixed
# Tuple-like accessors for backward compatibility
def __delitem__(self, key):
if isinstance(key, int):
key = self.decode_index(key)
self.__delattr__(key)
def __getitem__(self, key):
if isinstance(key, int):
key = self.decode_index(key)
return self.__getattribute__(key)
def __setitem__(self, key, value):
if isinstance(key, int):
key = self.decode_index(key)
self.__setattr__(key, value)
def decode_index(self, index):
return ['name', 'pattern', 'activation', 'bias', 'time_constant'][index]
def from_dict(self, dict_src):
for key in dict_src.key():
if key in self.__dict__.keys():
self[key] = dict_src[key]
return self
def from_tuple(self, tpl_src):
for idx in range(0, len(tpl_src)):
self[idx] = tpl_src[idx]
return self
class Edge:
def __init__(self,
src=None,
tar=None,
offsets=[],
alpha=1.0,
alpha_fixed=False,
alpha_references=[],
time_constant=None,
time_constant_fixed=False,
lambda_mult=None,
edge_type='chem'):
self.src = src
self.tar = tar
self.offsets = offsets
self.alpha = alpha
self.alpha_fixed = alpha_fixed
self.alpha_references = alpha_references
self.time_constant = time_constant
self.time_constant_fixed = time_constant_fixed
self.lambda_mult = lambda_mult
self.edge_type = edge_type
# Tuple-like accessors for backward compatibility
def __delitem__(self, key):
if isinstance(key, int):
key = self.decode_index(key)
self.__delattr__(key)
def __getitem__(self, key):
if isinstance(key, int):
key = self.decode_index(key)
return self.__getattribute__(key)
def __setitem__(self, key, value):
if isinstance(key, int):
key = self.decode_index(key)
self.__setattr__(key, value)
def decode_index(self, index):
return ['src', 'tar', 'offsets', 'alpha', 'lambda_mult', 'time_constant'][index]
def from_dict(self, dict_src):
for key in dict_src.key():
if key in self.__dict__.keys():
self[key] = dict_src[key]
return self
def from_tuple(self, tpl_src):
for idx in range(0, len(tpl_src)):
self[idx] = tpl_src[idx]
return self
class Receptor():
def __init__(self, name=None, time_constant=None, time_constant_fixed=False):
self.name = name
self.time_constant = time_constant
self.time_constant_fixed = time_constant_fixed
# Tuple-like accessors for backward compatibility
def __delitem__(self, key):
if isinstance(key, int):
key = self.decode_index(key)
self.__delattr__(key)
def __getitem__(self, key):
if isinstance(key, int):
key = self.decode_index(key)
return self.__getattribute__(key)
def __setitem__(self, key, value):
if isinstance(key, int):
key = self.decode_index(key)
self.__setattr__(key, value)
def decode_index(self, index):
return ['name', 'time_constant'][index]
def from_dict(self, dict_src):
for key in dict_src.key():
if key in self.__dict__.keys():
self[key] = dict_src[key]
return self
def from_tuple(self, tpl_src):
for idx in range(0, len(tpl_src)):
self[idx] = tpl_src[idx]
return self
def to_string_val(val):
if isinstance(val, str):
return '\''+val+'\''
else:
return str(val)
def serialize(output_name, receptors=[], nodes=[], edges=[], input_units=[], output_units=[], output_pickle=False):
if output_pickle:
# Pickle serialize
data = dict()
data['nodes'] = nodes
data['edges'] = edges
data['receptors'] = receptors
data['input_units'] = input_units
data['output_units'] = output_units
pickle.dump(data, open(output_name, 'wb'), pickle.HIGHEST_PROTOCOL)
else:
# Text serialize to active python code
file = open(output_name, 'w')
text = '# Python active code serialized model\n'
text = text + 'import model_base\n'
text = text + '################################################################################\n'
text = text + '# Input units\n'
text = text + 'input_units = ['
for i in range(0, len(input_units)):
text = text + '\'' + input_units[i] + '\''
if i < len(input_units)-1:
text = text + ', '
text = text + ']\n'
text = text + '################################################################################\n'
text = text + '# Output units\n'
text = text +'output_units = ['
for i in range(0, len(output_units)):
text = text + '\'' + output_units[i] + '\''
if i < len(output_units)-1:
text = text + ', '
text = text + ']\n'
text = text + '################################################################################\n'
text = text + '# Nodes\n'
text = text + 'nodes = []\n'
for node in nodes:
node_serial = ''
for key in node.__dict__.keys():
node_serial += key + '=' + to_string_val(node.__dict__[key]) + ', '
text = text + 'nodes.append(model_base.Node(' + node_serial + '))\n'
text = text + '################################################################################\n'
text = text + '# Edges\n'
text = text + 'edges = []\n'
for edge in edges:
edge_serial = ''
for key in edge.__dict__.keys():
edge_serial += key + '=' + to_string_val(edge.__dict__[key]) + ', '
text = text + 'edges.append(model_base.Edge(' + edge_serial + '))\n'
text = text + '################################################################################\n'
text = text + '# Receptors\n'
text = text + 'receptors = []\n'
for receptor in receptors:
receptor_serial = ''
for key in receptor.__dict__.keys():
receptor_serial += key + '=' + to_string_val(receptor.__dict__[key]) + ', '
text = text + 'receptors.append(model_base.Receptor(' + receptor_serial + '))\n'
file.write(text)
file.close()
def node_natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key.name) ]
return sorted(l, key = alphanum_key)
def edge_natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key.src+key.tar) ]
return sorted(l, key = alphanum_key)