-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposition_plot.py
executable file
·237 lines (205 loc) · 6.26 KB
/
composition_plot.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
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
#!/usr/bin/env python
import sys
import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm
from cycler import cycler
from loadmodules import *
import gadget_snap
from const import rsol, msol
import loaders
def composition_plot(
file=os.path.join("output", "composition.txt"),
save="plots",
nucleid="ni56",
eosspecies="species55.txt",
filetype="png",
dpi=600,
maxtime=None,
mintime=None,
scale="linear",
):
if maxtime is not None and mintime is not None:
if maxtime < mintime:
raise ValueError("maxtime is larger than mintime")
ls = [
("solid", (0, ())),
("dotted", (0, (1, 1))),
("dashed", (0, (5, 5))),
("dashdotted", (0, (3, 5, 1, 5))),
("dashdotdotted", (0, (3, 5, 1, 5, 1, 5))),
("loosely dotted", (0, (1, 10))),
("loosely dashed", (0, (5, 10))),
("loosely dashdotted", (0, (3, 10, 1, 10))),
("loosely dashdotdotted", (0, (3, 10, 1, 10, 1, 10))),
("densely dotted", (0, (1, 1))),
("densely dashed", (0, (5, 1))),
("densely dashdotted", (0, (3, 1, 1, 1))),
("densely dashdotdotted", (0, (3, 1, 1, 1, 1, 1))),
]
cmaps = [
"Blues_r",
"Oranges_r",
"Greens_r",
"Reds_r",
"Purples_r",
"Greys_r",
]
sp = loaders.load_species(eosspecies)
fig, ax = plt.subplots(1, 1, figsize=[6.4, 4.8])
if isinstance(file, list):
len_f = len(file)
else:
len_f = 1
file = np.array([file])
if isinstance(nucleid, list):
len_n = len(nucleid)
else:
len_n = 1
nucleid = np.array([nucleid])
color_count = np.arange(len_f)
norm = mpl.colors.Normalize(min(color_count), max(color_count))
colors = []
for k in range(len_n):
vals = np.array(norm(color_count)) * 0.5
colors.append(cm.get_cmap(cmaps[k % len(cmaps)])(vals))
for i, f in enumerate(file):
comp = np.genfromtxt(f)
if len_f > 1:
runname = os.path.split(
os.path.split(os.path.split(os.path.abspath(f))[0])[0]
)[1]
runname = " - %s" % runname
else:
runname = ""
n_species = comp.shape[1] - 1
assert n_species == sp["count"], "Species file does not fit composition file"
time = comp[:, 0]
if maxtime is None:
maxtime = max(time)
if mintime is None:
mintime = min(time)
mask = np.logical_and(time >= mintime, time <= maxtime)
for j, n in enumerate(nucleid):
sp_i = np.where(np.array(sp["names"]) == n)[0][0]
sp_data = comp[:, sp_i + 1]
if scale == "log":
ax.semilogy(
time[mask],
sp_data[mask],
label="{:s}{:s}".format(n, runname),
color=colors[j][i],
linestyle=ls[i % len(ls)][1],
)
elif scale == "linear":
ax.plot(
time[mask],
sp_data[mask],
label="{:s}{:s}".format(n, runname),
color=colors[j][i],
linestyle=ls[i % len(ls)][1],
)
else:
raise ValueError("Invalid scale")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Abundance fraction")
ax.grid()
fig.tight_layout()
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc="upper left", bbox_to_anchor=(1.05, 1.05))
if not os.path.exists(save):
print("Creating save directory...")
os.mkdir(save)
savefile = os.path.join(save, "composition_evolution.%s" % filetype)
saved = False
tryed = 0
while not saved:
if os.path.exists(savefile):
tryed += 1
savefile = os.path.join(
save,
"composition_evolution-(%d).%s" % (tryed, filetype),
)
else:
fig.savefig(
savefile,
bbox_inches="tight",
bbox_extra_artists=(lgd,),
dpi=dpi,
)
saved = True
plt.close()
return savefile
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--file",
help="Filename of composition file. If multiple files are provided, all will be plotted inthe same plot. Default: output/composition.txt",
default=os.path.join("output", "composition.txt"),
nargs="+",
)
parser.add_argument(
"-s",
"--save",
help="Path to directory where plots are saved to. Default: plots",
default="plots",
)
parser.add_argument(
"-n",
"--nucleid",
help="List of elements to be plotted. Needs to be listed in species file. Default: ni56",
default="ni56",
nargs="+",
)
parser.add_argument(
"-e",
"--eosspecies",
help="Species file including all the species used in the production of the composition file. Default: species55.txt",
default="species55.txt",
)
parser.add_argument(
"-t",
"--filetype",
help="Fileformat of saved figure. Default: png",
default="png",
)
parser.add_argument(
"-d",
"--dpi",
help="DPI of saved figure. Default: 600",
type=int,
default=600,
)
parser.add_argument(
"--maxtime",
help="Upper timelimit for composition plot in s.",
type=float,
)
parser.add_argument(
"--mintime",
help="Lower timelimit for composition plot in s.",
type=float,
)
parser.add_argument(
"--scale",
help="Scale of plot. Either linear or log. Default: linear",
default="linear",
choices=["linear", "log"],
)
args = parser.parse_args()
s = composition_plot(
file=args.file,
save=args.save,
nucleid=args.nucleid,
eosspecies=args.eosspecies,
filetype=args.filetype,
dpi=args.dpi,
maxtime=args.maxtime,
mintime=args.mintime,
scale=args.scale,
)
print("Finished plotting %s" % s)