-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelAnalyzer_cnoise.py
More file actions
executable file
·591 lines (499 loc) · 19.9 KB
/
modelAnalyzer_cnoise.py
File metadata and controls
executable file
·591 lines (499 loc) · 19.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#!/usr/bin/env python
'''
This module has the following methods:
'''
import sys
import os
import numpy as np
from collections import OrderedDict
from collections import defaultdict
from os.path import basename
import copy
import time
import ctypes
np.random.seed(1)
clib=ctypes.cdll.LoadLibrary('./simulation_clib.so')
#clib.randu.argtypes=(ctypes.c_double,ctypes.c_double)
#clib.randu.restype=ctypes.c_double
SLOW_EDGE_TYPES=[1,2,3,4]
FAST_EDGE_TYPES=[5,6]
EXCITATION_TYPES=[1,4,5] #4: degrdation inhibition => activation of degradation
INHIBITION_TYPES=[2,3,6] #3: dedgration activation => inhibition of degradation
#-----------------------------------------------------------------------------#
def print_dict_log2(d):
for k,v in sorted(d.items()):
print(k, ': ', np.log2(v))
print('\n')
return None
#-----------------------------------------------------------------------------#
def print_dict(d):
for k,v in d.items():
print(k, ': ', v)
print('\n')
return None
#-----------------------------------------------------------------------------#
def print_nested_defaultdict(node_dict,d):
for k in node_dict.keys():
v= d[k]
print(k,':')
for idx,e in v.items():
print(' ',idx,': ', e)
print('\n')
return None
#-----------------------------------------------------------------------------#
def print_nested_defaultdict_log2(d):
#print(d)
#sys.exit(0)
for idx,v in d.items():
print(idx,':')
for X,e in v.items():
print(' ',X,': ', np.log2(e))
print('\n')
return None
#-----------------------------------------------------------------------------#
def save_parameters(fh_params,model_no,nodeParam_dict,
master_dict,solution_dict):
#model no + number of states:
outstr=str(model_no)+'\t'+str(len(solution_dict.keys()))
#skipping node type information nodeParam_dict[X][0]
#save MPRs:
for X in nodeParam_dict.keys():
#outstr+='\t'+str('%10.6f'%nodeParam_dict[X][0])
outstr+='\t'+str('%10.6f'%nodeParam_dict[X][1])
outstr+='\n'
fh_params.write(outstr)
#save all DNRs:
outstr=str(model_no)+'\t'+str(len(solution_dict.keys()))
for X in nodeParam_dict.keys():
#outstr+='\t'+str('%10.6f'%nodeParam_dict[X][1])
outstr+='\t'+str('%10.6f'%nodeParam_dict[X][2])
outstr+='\n'
fh_params.write(outstr)
#save all TSHs:
outstr=str(model_no)+'\t'+str(len(solution_dict.keys()))
for idx in master_dict.keys():
outstr+='\t'+str('%10.6f'%master_dict[idx][3])
outstr+='\n'
fh_params.write(outstr)
#save all HCOs:
outstr=str(model_no)+'\t'+str(len(solution_dict.keys()))
for idx in master_dict.keys():
outstr+='\t'+str(master_dict[idx][4])
outstr+='\n'
fh_params.write(outstr)
#save all FCHs:
outstr=str(model_no)+'\t'+str(len(solution_dict.keys()))
for idx in master_dict.keys():
outstr+='\t'+str('%10.6f'%master_dict[idx][5])
outstr+='\n'
fh_params.write(outstr)
fh_params.flush()
return None
def save_parameters_old(fh_params,model_no,nodeParam_dict,
master_dict,solution_dict):
#model no + number of states:
outstr=str(model_no)+'\t'+str(len(solution_dict.keys()))
#skipping node type information nodeParam_dict[X][0]
#write MPR to outstr:
for X in nodeParam_dict.keys():
#outstr+='\t'+str('%10.6f'%nodeParam_dict[X][0])
outstr+='\t'+str('%10.6f'%nodeParam_dict[X][1])
#write DNR to outstr:
for X in nodeParam_dict.keys():
#outstr+='\t'+str('%10.6f'%nodeParam_dict[X][1])
outstr+='\t'+str('%10.6f'%nodeParam_dict[X][2])
#write edge parameters (TSH,HCO,FCH) to outstr:
for idx in master_dict.keys():
outstr+='\t'+str('%10.6f'%master_dict[idx][3])
outstr+='\t'+str(master_dict[idx][4])
outstr+='\t'+str('%10.6f'%master_dict[idx][5])
outstr+='\n'
fh_params.write(outstr)
fh_params.flush()
return None
#----------------------------------------------------------------------#
def read_params(fh_params,network,MODEL_TO_INSPECT):
'''
This method loads the network topology and places the topology
information into the specific data structures.
fh_tpo: file handle to a network topology file
It updates the following data structures:
node_dict: a dictionary of genes/nodes in the network
target_dict:
source_dict:
master_dict:
'''
#
node_id_dict=network.get_node_id_dict()
edge_source_dict=network.get_edge_source_dict()
NUM_NODES=len(node_id_dict.keys()) #size of node_id_arr
NUM_EDGES=len(edge_source_dict.keys()) #size of edge_id_arr
MPR_arr=np.zeros(NUM_NODES,dtype=np.double)
DNR_arr=np.zeros(NUM_NODES,dtype=np.double)
NODE_TYPE_arr=np.zeros(NUM_NODES,dtype=np.intc)
TSH_arr=np.zeros(NUM_EDGES,dtype=np.double)
HCO_arr=np.zeros(NUM_EDGES,dtype=np.intc)
FCH_arr=np.zeros(NUM_EDGES,dtype=np.double)
#import node and edge information:
node_dict=network.get_node_dict()
master_dict=network.get_master_dict()
node_count=len(node_dict.keys())
edge_count=len(master_dict.keys())
for line in fh_params:
if not line.strip():
continue
#read line 1:MPR
#fields[0]:model index,fields[1]:number of states
#fields[2] ~ fields[.]:node_count number of MPRs
fields=line.strip().split()
if (int(fields[0]) is not int(MODEL_TO_INSPECT)):
#for the current model, skip all subsequent lines:
next(fh_params) #skip DNR line
next(fh_params) #skip TSH line
next(fh_params) #skip HCO line
next(fh_params) #skip FCH line
continue
for i in range(2,len(fields)):
MPR_arr[i-2]=fields[i]
if(node_count is not len(fields[2:])):
print('mismatch in node count and MPR count')
print('program exiting ...')
sys.exit(0)
#read line 2:DNR
line2=fh_params.readline()
fields=line2.strip().split()
for i in range(2,len(fields)):
DNR_arr[i-2]=fields[i]
#read line 3:TSH
line3=fh_params.readline()
fields=line3.strip().split()
for i in range(2,len(fields)):
TSH_arr[i-2]=fields[i]
if(edge_count is not len(fields[2:])):
print('mismatch in edge count and TSH count')
print('program exiting ...')
sys.exit(0)
#read line 4:HCO
line4=fh_params.readline()
fields=line4.strip().split()
for i in range(2,len(fields)):
HCO_arr[i-2]=fields[i]
#read line 5:FCH
line5=fh_params.readline()
fields=line5.strip().split()
for i in range(2,len(fields)):
FCH_arr[i-2]=fields[i]
return MPR_arr,DNR_arr,TSH_arr,HCO_arr,FCH_arr
#----------------------------------------------------------------------#
def import_model_params(fh_params, network, MODEL_LIST):
'''
This method imports the parameters for each model in
the MODEL_LIST and places them in a dictionary model_params_dict
model_params_dict:
key: model no
values: a list of five lists for each model:
MPR list: a list of MPR parameters
DNR list: a list of DNR parameters
TSH list: a list of TSH parameters
HCO list: a list of HCO parameters
FCH list: a list of FCH parameters
'''
# define a dictionary to store the parameters for the model set:
model_params_dict = OrderedDict()
# convert the list elements to int if they are not in int format already:
MODEL_LIST = list(map(int, MODEL_LIST))
node_id_dict=network.get_node_id_dict()
edge_source_dict=network.get_edge_source_dict()
NUM_NODES=len(node_id_dict.keys()) #size of node_id_arr
NUM_EDGES=len(edge_source_dict.keys()) #size of edge_id_arr
#MPR_arr=np.zeros(NUM_NODES,dtype=np.double)
#DNR_arr=np.zeros(NUM_NODES,dtype=np.double)
#NODE_TYPE_arr=np.zeros(NUM_NODES,dtype=np.intc)
#TSH_arr=np.zeros(NUM_EDGES,dtype=np.double)
#HCO_arr=np.zeros(NUM_EDGES,dtype=np.intc)
#FCH_arr=np.zeros(NUM_EDGES,dtype=np.double)
#import node and edge information:
#need this information for verification of number of fields in different lines:
node_dict=network.get_node_dict()
master_dict=network.get_master_dict()
node_count=len(node_dict.keys())
edge_count=len(master_dict.keys())
for line in fh_params:
if not line.strip():
continue
#read line 1:MPR
fields=line.strip().split()
#fields[0]:model index,fields[1]:number of states
#fields[2] ~ fields[.]:node_count number of MPRs
if (int(fields[0]) not in MODEL_LIST):
#for the current model, skip all subsequent lines:
next(fh_params) #skip DNR line
next(fh_params) #skip TSH line
next(fh_params) #skip HCO line
next(fh_params) #skip FCH line
continue
# initialize the lists for all parameter types:
MPR_arr=np.zeros(NUM_NODES,dtype=np.double)
DNR_arr=np.zeros(NUM_NODES,dtype=np.double)
NODE_TYPE_arr=np.zeros(NUM_NODES,dtype=np.intc)
TSH_arr=np.zeros(NUM_EDGES,dtype=np.double)
HCO_arr=np.zeros(NUM_EDGES,dtype=np.intc)
FCH_arr=np.zeros(NUM_EDGES,dtype=np.double)
for i in range(2,len(fields)):
MPR_arr[i-2]=fields[i]
if(node_count is not len(fields[2:])):
print('mismatch in node count and MPR count')
print('program exiting ...')
sys.exit(0)
#read line 2:DNR
line2=fh_params.readline()
fields=line2.strip().split()
for i in range(2,len(fields)):
DNR_arr[i-2]=fields[i]
#read line 3:TSH
line3=fh_params.readline()
fields=line3.strip().split()
for i in range(2,len(fields)):
TSH_arr[i-2]=fields[i]
if(edge_count is not len(fields[2:])):
print('mismatch in edge count and TSH count')
print('program exiting ...')
sys.exit(0)
#read line 4:HCO
line4=fh_params.readline()
fields=line4.strip().split()
for i in range(2,len(fields)):
HCO_arr[i-2]=fields[i]
#read line 5:FCH
line5=fh_params.readline()
fields=line5.strip().split()
for i in range(2,len(fields)):
FCH_arr[i-2]=fields[i]
# insert the params in the dictionary:
model_params_dict[int(fields[0])] = [MPR_arr, DNR_arr, TSH_arr, HCO_arr, FCH_arr]
return model_params_dict
#----------------------------------------------------------------------#
def emulate_a_model(network, model_no, params_list):
'''
This method generates RACIPE models.
'''
global clib
import numpy
import numpy as np
import ctypes
import sys
import modelManager as mm
#sys.exit(0)
# add appropriate noise:
NOISE=1.0
#NOISE=10.0 # 30.0
NOISE_SHOT=0
node_dict = network.get_node_dict()
node_id_dict=network.get_node_id_dict()
edge_source_dict=network.get_edge_source_dict()
edge_target_dict=network.get_edge_target_dict()
edge_type_dict=network.get_edge_type_dict()
NUM_NODES=len(node_id_dict.keys()) #size of node_id_arr
NUM_EDGES=len(edge_source_dict.keys()) #size of edge_id_arr
#create arrays for storing noise level for each node:
NOISE_strength=np.zeros(NUM_NODES,dtype=np.double)
NOISE_strength_shot=np.zeros(NUM_NODES,dtype=np.double)
#create array for storing the source nodes of the edges:
edge_source_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_source_dict.keys():
edge_source_arr[i]=edge_source_dict[idx]
i+=1
#create array for storing the target nodes of the edges:
edge_target_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_target_dict.keys():
edge_target_arr[i]=edge_target_dict[idx]
i+=1
#create character array for work directory string:
WORK_DIR=(network.get_work_dir()).encode('utf-8')
#create character arrays for file names to be used in C:
fname_dict_simu = network.get_fname_dict_simu()
FNAME_STATES = fname_dict_simu['FNAME_STATES'].encode('utf-8')
FNAME_LIMITCYCLES = fname_dict_simu['FNAME_LIMITCYCLES'].encode('utf-8')
FNAME_SUMMARY = fname_dict_simu['FNAME_SUMMARY'].encode('utf-8')
edge_source_dict=network.get_edge_source_dict()
edge_target_dict=network.get_edge_target_dict()
edge_type_dict=network.get_edge_type_dict()
#create array for storing the source nodes of the edges:
edge_source_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_source_dict.keys():
edge_source_arr[i]=edge_source_dict[idx]
i+=1
#create array for storing the target nodes of the edges:
edge_target_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_target_dict.keys():
edge_target_arr[i]=edge_target_dict[idx]
i+=1
#create array for storing the types of the edges:
edge_type_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_type_dict.keys():
edge_type_arr[i]=edge_type_dict[idx]
i+=1
# create arrays for storing the parameters:
MPR_arr = params_list[0]
DNR_arr = params_list[1]
TSH_arr = params_list[2]
HCO_arr = params_list[3]
FCH_arr = params_list[4]
NODE_TYPE_arr=np.zeros(NUM_NODES,dtype=np.intc)
node_dict=network.get_node_dict()
i=0
for node in node_dict.keys():
(node_type,mpr_range,dnr_range)=node_dict[node]
NODE_TYPE_arr[i]=node_type
NOISE_strength[i]=1
NOISE_strength_shot[i]=1
i=i+1
#import config_dict:
config_dict=network.get_config_dict()
EXP_dict_arr=np.zeros(NUM_NODES*int(config_dict['NUM_RANDOM_ICS']),
dtype=np.double)
clib.find_solutions_stochastic(\
ctypes.create_string_buffer(WORK_DIR),
#ctypes.create_string_buffer(FNAME_STATES),
#ctypes.create_string_buffer(FNAME_LIMITCYCLES),
#ctypes.create_string_buffer(FNAME_SUMMARY),
ctypes.c_int(model_no),
ctypes.c_int(NUM_NODES),
ctypes.c_int(NUM_EDGES),
ctypes.c_int(int(config_dict['NUM_RANDOM_ICS'])),
ctypes.c_int(int(config_dict['ITER_FOR_ODE'])),
ctypes.c_double(float(config_dict['EULER_SIM_TIME'])),
ctypes.c_double(float(config_dict['EULER_SIM_STEP_SIZE'])),
ctypes.c_longdouble(float(config_dict['CONVERGENCE_PROXIMITY'])),
ctypes.c_double(float(config_dict['TRANS_RATE_FACTOR'])),
ctypes.c_void_p(MPR_arr.ctypes.data),
ctypes.c_void_p(DNR_arr.ctypes.data),
ctypes.c_void_p(NODE_TYPE_arr.ctypes.data),
ctypes.c_void_p(edge_source_arr.ctypes.data),
ctypes.c_void_p(edge_target_arr.ctypes.data),
ctypes.c_void_p(edge_type_arr.ctypes.data),
ctypes.c_void_p(TSH_arr.ctypes.data),
ctypes.c_void_p(HCO_arr.ctypes.data),
ctypes.c_void_p(FCH_arr.ctypes.data),
ctypes.c_void_p(EXP_dict_arr.ctypes.data),
ctypes.c_void_p(NOISE_strength.ctypes.data),
ctypes.c_void_p(NOISE_strength_shot.ctypes.data),
ctypes.c_double(NOISE),
ctypes.c_double(NOISE_SHOT)
)
return None
#----------------------------------------------------------------------#
def simulate_network_cnoise(network, model_no, params_list):
'''
This method generates RACIPE models.
'''
global clib
import numpy
import numpy as np
import ctypes
import sys
import modelManager as mm
node_dict = network.get_node_dict()
node_id_dict=network.get_node_id_dict()
edge_source_dict=network.get_edge_source_dict()
edge_target_dict=network.get_edge_target_dict()
edge_type_dict=network.get_edge_type_dict()
NUM_NODES=len(node_id_dict.keys()) #size of node_id_arr
NUM_EDGES=len(edge_source_dict.keys()) #size of edge_id_arr
#create arrays for storing noise level for each node:
NOISE_strength=np.zeros(NUM_NODES,dtype=np.double)
NOISE_strength_shot=np.zeros(NUM_NODES,dtype=np.double)
#create array for storing the source nodes of the edges:
edge_source_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_source_dict.keys():
edge_source_arr[i]=edge_source_dict[idx]
i+=1
#create array for storing the target nodes of the edges:
edge_target_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_target_dict.keys():
edge_target_arr[i]=edge_target_dict[idx]
i+=1
#create character array for work directory string:
WORK_DIR=(network.get_work_dir()).encode('utf-8')
#create character arrays for file names to be used in C:
fname_dict_simu = network.get_fname_dict_simu()
#FNAME_STATES = fname_dict_simu['FNAME_STATES'].encode('utf-8')
FNAME_STATES = fname_dict_simu['FNAME_STATES_CNOISE'].encode('utf-8')
FNAME_LIMITCYCLES = fname_dict_simu['FNAME_LIMITCYCLES'].encode('utf-8')
FNAME_SUMMARY = fname_dict_simu['FNAME_SUMMARY'].encode('utf-8')
edge_source_dict=network.get_edge_source_dict()
edge_target_dict=network.get_edge_target_dict()
edge_type_dict=network.get_edge_type_dict()
#create array for storing the source nodes of the edges:
edge_source_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_source_dict.keys():
edge_source_arr[i]=edge_source_dict[idx]
i+=1
#create array for storing the target nodes of the edges:
edge_target_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_target_dict.keys():
edge_target_arr[i]=edge_target_dict[idx]
i+=1
#create array for storing the types of the edges:
edge_type_arr=np.zeros(NUM_EDGES,dtype=np.intc)
i=0
for idx in edge_type_dict.keys():
edge_type_arr[i]=edge_type_dict[idx]
i+=1
# create arrays for storing the parameters:
MPR_arr = params_list[0]
DNR_arr = params_list[1]
TSH_arr = params_list[2]
HCO_arr = params_list[3]
FCH_arr = params_list[4]
NODE_TYPE_arr=np.zeros(NUM_NODES,dtype=np.intc)
node_dict=network.get_node_dict()
i=0
for node in node_dict.keys():
(node_type,mpr_range,dnr_range)=node_dict[node]
NODE_TYPE_arr[i]=node_type
i=i+1
#import config_dict:
config_dict=network.get_config_dict()
EXP_dict_arr=np.zeros(NUM_NODES*int(config_dict['NUM_RANDOM_ICS']),
dtype=np.double)
clib.simulate_network_cnoise_gnw(\
ctypes.create_string_buffer(WORK_DIR),
ctypes.create_string_buffer(FNAME_STATES),
ctypes.create_string_buffer(FNAME_LIMITCYCLES),
ctypes.create_string_buffer(FNAME_SUMMARY),
ctypes.c_int(model_no),
ctypes.c_int(NUM_NODES),
ctypes.c_int(NUM_EDGES),
ctypes.c_int(int(config_dict['NUM_RANDOM_ICS'])),
ctypes.c_int(int(config_dict['ITER_FOR_ODE'])),
ctypes.c_double(float(config_dict['EULER_SIM_TIME'])),
ctypes.c_double(float(config_dict['EULER_SIM_STEP_SIZE'])),
ctypes.c_longdouble(float(config_dict['CONVERGENCE_PROXIMITY'])),
ctypes.c_double(float(config_dict['TRANS_RATE_FACTOR'])),
ctypes.c_void_p(MPR_arr.ctypes.data),
ctypes.c_void_p(DNR_arr.ctypes.data),
ctypes.c_void_p(NODE_TYPE_arr.ctypes.data),
ctypes.c_void_p(edge_source_arr.ctypes.data),
ctypes.c_void_p(edge_target_arr.ctypes.data),
ctypes.c_void_p(edge_type_arr.ctypes.data),
ctypes.c_void_p(TSH_arr.ctypes.data),
ctypes.c_void_p(HCO_arr.ctypes.data),
ctypes.c_void_p(FCH_arr.ctypes.data),
ctypes.c_void_p(EXP_dict_arr.ctypes.data)
)
return None
#-----------------------------------------------------------------------------#
if __name__ == '__main__':
print (sys.argv[0] + ':')
print(__doc__)
sys.exit(0)