forked from Radhakrishnanlab-UPenn/Parameter_Sweep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunsweep.py
132 lines (109 loc) · 4.04 KB
/
runsweep.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
#!/usr/bin/env python
import sys, copy
import tempfile as tf
import os, csv, json
import subprocess
import sweep
change_ext = lambda filename, ext : os.path.splitext(filename)[0] + ext
def Odesolve(sweeper):
"""
Solves a single instance of a sweeper
gets the data and performs cleanup
"""
sweeper.Change_Parameters()
sweeper.Createtempfile()
Outfilename = sweeper.change_report()
sweeper.headers()
sweeper.Writefile()
sweeper.OdeRun()
data = sweeper.Get_Data()
status = sweeper.Cleanup()
class RunSweep:
"""
Sets up, runs, collects data and
cleans up particular instances of a
parameter sweep
"""
def __init__(self, inputfile, parameter, conv = 1.0):
self.root = sweep.Generate_Xml(inputfile)
self.parameter = parameter
self.conv = conv
self.status = "not started"
def Change_Parameters(self):
"""
Changes the sweep parameters
"""
for param in self.parameter:
if param["Paramtype"] == "Initial Species Values" :
conv = self.conv
else:
conv = 1
oldval, newval = sweep.SetCpsValues(self.root, param, conv = conv)
param["value_change"] = (float(oldval)/conv, float(newval)/conv)
def Createtempfile(self):
fp = tf.NamedTemporaryFile(delete=False)
self.tmpfile = fp.name
def change_report(self, XPathstr = ".//*[@type='timeCourse']"):
Task_Time_Course = sweep.Get_Elem(self.root, XPathstr)[0]
Outfilename = change_ext(self.tmpfile, ".csv")
Task_Time_Course = sweep.Set_Elem(Task_Time_Course, "target", Outfilename)
self.output = Outfilename
return Outfilename
def headers(self):
self.header = sweep.Get_Headers(self.root, ".//Table")
def Writefile(self):
sweep.Write_File(self.root, self.tmpfile)
self.status = "ready"
def OdeRun(self, copasipath='CopasiSE'):
out = err = ''
Copasi_Command = copasipath + ' --verbose ' + self.tmpfile
p = subprocess.Popen(Copasi_Command,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for lines in iter(p.stdout.readline,''):
out += lines.rstrip()
for lines in iter(p.stderr.readline,''):
err += lines.rstrip()
streams = p.communicate()
self.status = "completed"
return out, err
def Get_Data(self):
"""
Extracts data for a particular run and appends to
the input dictionary
"""
time_course_data = { key : [] for key in self.header }
if self.status == "completed":
with open(self.output) as fp:
reader = csv.DictReader(fp, fieldnames = self.header)
for ii, row in enumerate(reader):
if ii == 0:
continue
else:
for key in row:
time_course_data[key].append(float(row[key]))
self.data = time_course_data
else:
print "Run not completed for %s" % self.tmpfile
return time_course_data
def Cleanup(self):
"""
Cleans up the temporary files
"""
cpstatus = os.remove(self.tmpfile)
csvstatus = os.remove(self.output)
return cpstatus, csvstatus
if __name__ == '__main__':
#Copasifile = "test/ErbB4-JAK2-STAT5_Yamda_Combined_Exec.cps"
Inputfile = sys.argv[1]
with open(Inputfile) as fp:
Input_Data = json.load(fp)
Sweep_Space_Full = sweep.Generate_Sweeps(Input_Data["sweep_info"])
control_input = Input_Data["sweep_info"]["Nrg"]
control_input["value"] = control_input["control"]
del control_input["control"]
control_sweep = RunSweep(Input_Data["Copasifile"], [control_input], conv = Input_Data["conv"])
Odesolve(control_sweep)
sweepers = []
for elem in Sweep_Space_Full:
sweeper = RunSweep(Input_Data["Copasifile"], elem, conv = Input_Data["conv"])
Odesolve(sweeper)
sweepers.append(sweeper)