forked from drdrhatch/IFS_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genescanplot.py
executable file
·209 lines (186 loc) · 7.58 KB
/
genescanplot.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
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
import os
import sys
import glob
import numpy as npy
import matplotlib.pyplot as plt
import matplotlib.backends.backend_pdf as pdfh
def read_parameters(paramfpath):
#Developed by Ehab Hassan on 2019-02-05
if "parameters" not in paramfpath:
if paramfpath[-1] == "/": paramfpath+="parametrers"
else: paramfpath+="/parameters"
if os.path.isfile(paramfpath):
print(paramfpath+' FILE FOUND ...')
else:
print(paramfpath+' FILE NOT FOUND. Exit!'); sys.exit()
ofh = open(paramfpath,'r')
lines = ofh.readlines()
ofh.close()
geneparam = {'filepath':paramfpath}
nspecs = 0
for line in lines:
if line[0] == '&':
ckey = line[1:].strip()
if ckey=="species":
nspecs += 1
ckey += "_"+str(nspecs)
geneparam[ckey] = {}
elif line[0] == '/' or line[0] == '!':
continue
elif line.strip() == '':
continue
else:
items = line.split('=')
values = items[1].split()
if "!scanlist" in items[1]:
cvalues = []
for item in values[2:]:
if item == '!': break
cvalues.append(float(item[0:-1]))
cvalues = npy.array(cvalues)
else:
if values[0].isdigit(): cvalues = float(values[0].strip())
else: cvalues = values[0].strip()
geneparam[ckey][items[0].strip()] = cvalues
return geneparam
def read_scanfile(scanfpath):
#Developed by Ehab Hassan on 2019-02-01
if "scan.log" not in scanfpath:
if scanfpath[-1] == "/": scanfpath+="scan.log"
else: scanfpath+="/scan.log"
if os.path.isfile(scanfpath):
print(scanfpath+' FILE FOUND ...')
else:
print(scanfpath+' FILE NOT FOUND. Exit!'); sys.exit()
ofh = open(scanfpath,'r')
lines = ofh.readlines()
ofh.close()
scandata = {'filepath':scanfpath}
hlist = lines[0].split('|')[1:]
vlist = []
for ihead in hlist:
vlist.append(ihead.split()[0])
nrecs = npy.size(lines)-1
vscan = npy.zeros((len(vlist),nrecs))
gamma = npy.zeros(nrecs)
omega = npy.zeros(nrecs)
for irec in range(nrecs):
line = lines[irec+1].split('|')
for ivar in range(1,len(vlist)+1):
vscan[ivar-1,irec] = float(line[ivar])
gamma[irec] = float(line[-1].split()[0])
omega[irec] = float(line[-1].split()[1])
scandata['gamma'] = gamma
scandata['omega'] = omega
for ivar in range(len(vlist)):
scandata[vlist[ivar]] = vscan[ivar,:]
return scandata
def plot_scandata(scandata):
#Developed by Ehab Hassan on 2019-02-05
vlist = list(set(scandata.keys())-{'omega','gamma','filepath'})
if len(vlist) == 1:
if 'kymin' in vlist:
plabel = scandata['filepath'][0:-9]
else:
params = read_parameters(scandata['filepath'][0:-9]+'/parameters')
plabel = 'kymin = '+params['box']['kymin']
gammafig = [plt.figure(1)]
omegafig = [plt.figure(2)]
axhand01 = gammafig[0].add_subplot(1,1,1)
axhand02 = omegafig[0].add_subplot(1,1,1)
axhand01.plot(scandata[vlist[0]],scandata['gamma'],label=plabel)
axhand02.plot(scandata[vlist[0]],scandata['omega'],label=plabel)
axhand01.set_title(vlist[0]+' vs $\gamma$')
axhand02.set_title(vlist[0]+' vs $\omega$')
axhand01.set_xlabel(vlist[0])
axhand02.set_xlabel(vlist[0])
axhand01.set_ylabel('$\gamma$')
axhand02.set_ylabel('$\omega$')
axhand01.legend()
axhand02.legend()
elif len(vlist) == 2:
if 'kymin' not in vlist:
params = read_parameters(scandata['filepath'][0:-9]+'/parameters')
iscan = 'kymin = '+params['box']['kymin']+': '
else:
iscan = ''
gammafig = []
omegafig = []
maxrepeat = 1
for var in vlist:
for irepeat in range(1,len(scandata[var])):
if scandata[var][irepeat] == scandata[var][0]: break
if irepeat >= maxrepeat: maxrepeatvar = var
maxrepeat = max(maxrepeat,irepeat)
gammafig.append(plt.figure(1))
omegafig.append(plt.figure(2))
axhand01 = gammafig[0].add_subplot(1,1,1)
axhand02 = omegafig[0].add_subplot(1,1,1)
for iplot in range(npy.size(scandata[vlist[0]])/maxrepeat):
sindex = iplot*maxrepeat
eindex = (iplot+1)*maxrepeat
for var in list(set(vlist)-{maxrepeatvar}):
plabel = iscan+var+'='+str(scandata[var][sindex:eindex][0])+' '
axhand01.plot(scandata[maxrepeatvar][sindex:eindex],scandata['gamma'][sindex:eindex],label=plabel)
axhand02.plot(scandata[maxrepeatvar][sindex:eindex],scandata['omega'][sindex:eindex],label=plabel)
axhand01.set_title(maxrepeatvar+' vs $\gamma$')
axhand02.set_title(maxrepeatvar+' vs $\omega$')
axhand01.set_xlabel(maxrepeatvar)
axhand02.set_xlabel(maxrepeatvar)
axhand01.set_ylabel('$\gamma$')
axhand02.set_ylabel('$\omega$')
axhand01.legend()
axhand02.legend()
gammafig.append(plt.figure(3))
omegafig.append(plt.figure(4))
pvarname = list(set(vlist)-{maxrepeatvar})
axhand01 = gammafig[1].add_subplot(1,1,1)
axhand02 = omegafig[1].add_subplot(1,1,1)
for iplot in range(maxrepeat):
sindex = iplot
stprng = npy.size(scandata[pvarname[0]])/maxrepeat
plabel = iscan+maxrepeatvar+'='+str(scandata[maxrepeatvar][sindex:eindex][0])+' '
axhand01.plot(scandata[pvarname[0]][sindex::stprng],scandata['gamma'][sindex::stprng],label=plabel)
axhand02.plot(scandata[pvarname[0]][sindex::stprng],scandata['omega'][sindex::stprng],label=plabel)
axhand01.set_title(pvarname[0]+' vs $\gamma$')
axhand02.set_title(pvarname[0]+' vs $\omega$')
axhand01.set_xlabel(pvarname[0])
axhand02.set_xlabel(pvarname[0])
axhand01.set_ylabel('$\gamma$')
axhand02.set_ylabel('$\omega$')
axhand01.legend()
axhand02.legend()
return gammafig,omegafig
def plot_scans(scanfiles):
#Developed by Ehab Hassan on 2019-02-07
if type(scanfiles)==list:
for iscan in scanfiles:
scanvals = read_scanfile(iscan)
gammafig,omegafig = plot_scandata(scanvals)
else:
scanvals = read_scanfile(scanfiles)
gammafig,omegafig = plot_scandata(scanvals)
plt.show(gammafig)
plt.show(omegafig)
if raw_input('Do you want to save these figures [Yes/No]? ').lower() in ['yes','y']:
pdfpages = pdfh.PdfPages('scanfigs.pdf')
for item in range(len(gammafig)):
gammafig[item].savefig('gamma%02d.png' % (item+1))
omegafig[item].savefig('omega%02d.png' % (item+1))
pdfpages.savefig(gammafig[item])
pdfpages.savefig(omegafig[item])
pdfpages.close()
return 1
scanfpath = []
if len(sys.argv[1:]) >= 1:
for iarg in sys.argv[1:]:
scanfpath.append(iarg)
else:
if raw_input('Do you want to compare several scans in several files? (yes/no)').lower() in ['yes','y']:
while True:
infpath = raw_input('Path to scan file: ')
if len(infpath) > 0: scanfpath.append(infpath)
else: break
else:
scanfpath.append(raw_input('Path to the scan file: '))
plot_scans(scanfpath)