-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.py
138 lines (107 loc) · 4.43 KB
/
chart.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
import matplotlib.pyplot as plt
import seaborn as sns
from pandas import read_csv as rcsv
from os import chdir
import pandas as pd
import numpy as np
class Charting:
def __init__(self, res_path, t_crit, rset, probs):
chdir(res_path)
self.results = rcsv('stoch_rest.csv', sep=',')
self.t_crit = t_crit
self.rset = rset
self.p_coll = probs[0]
self.p_evac = probs[1]
# charts used for risk analysis
def cdf(self, data, x_crit, y_crit, label, crit_lab):
sns_plot = sns.distplot(data, hist_kws={'cumulative': True},
kde_kws={'cumulative': True, 'label': 'CDF'}, bins=20, axlabel=label)
plt.axvline(x=x_crit, color='r')
plt.axhline(y=y_crit, color='r')
plt.text(x_crit - 0.05 * (plt.axis()[1]-plt.axis()[0]), 0.2, crit_lab, rotation=90)
def pdf(self, data, x_crit, label, crit_lab):
sns_plot = sns.distplot(data, kde_kws={'label': 'PDF'}, axlabel=label)
plt.axvline(x=x_crit, color='r')
plt.text(x_crit - 0.05 * (plt.axis()[1] - plt.axis()[0]), 0.2, crit_lab, rotation=90)
def dist(self, type='cdf'):
try:
plt.figure(figsize=(12, 4))
plt.subplot(121)
if type == 'cdf':
self.cdf(self.results.t_max, self.t_crit, self.p_coll, 'Temperature [°C]', r'$\theta_{a,cr}$')
elif type == 'pdf':
self.pdf(self.results.t_max, self.t_crit, 'Temperature [°C]', r'$\theta_{a,cr}$')
plt.subplot(122)
if type == 'cdf':
self.cdf(self.results.time_crit[self.results.time_crit > 0], self.rset, self.p_evac, 'Time [s]', 'RSET')
elif type == 'pdf':
self.pdf(self.results.time_crit[self.results.time_crit > 0], self.rset, 'Time [s]', 'RSET')
except:
plt.figure(figsize=(6, 4))
if type == 'cdf':
self.cdf(self.results.t_max, self.t_crit, self.p_coll, 'Temperature [°C]', r'$\theta_{a,cr}$')
elif type == 'pdf':
self.pdf(self.results.t_max, self.t_crit, 'Temperature [°C]', r'$\theta_{a,cr}$')
if type == 'cdf':
plt.savefig('dist_p.png')
elif type == 'pdf':
plt.savefig('dist_d.png')
def draw(self):
print(self.results)
self.dist(type='cdf')
self.dist(type='pdf')
return 0
'''DrawOZone allows to create set of charts from OZone data
use .PRI or .STT output file as an argument'''
class DrawOZone:
def __init__(self, file_paths):
# if file_paths[-3:] != ('pri' or 'stt'):
# 'Use PRI or STT file as an argument, please!'
self.file_paths = file_paths
def read(self, pth):
with open(pth) as file:
raw = file.readlines()
data = []
for line in raw[2:]:
df_line = [] # add time to line
for i in range(0, 140, 10):
try:
df_line.append(float(line[max(0, i-2):(8+i)].split()[-1])) # add values to line
except IndexError:
df_line.append(0)
data.append(df_line) # add line to table
df = pd.DataFrame(np.array(data), columns=[raw[0].split()]) # create dataframe
return df
def read_stt(self, pth):
with open(pth) as file:
raw = file.readlines()
data = []
for line in raw[2:]:
df_line = [] # add time to line
for i in range(0, 30, 10):
try:
df_line.append(float(line[max(0, i-2):(8+i)].split()[-1])) # add values to line
except IndexError:
df_line.append(0)
data.append(df_line) # add line to table
df = pd.DataFrame(np.array(data), columns=[raw[1].split()]) # create dataframe
return df
def chart(self, name, dfs):
fig, ax = plt.subplots(1, 1)
labs = ['50MW', '100MW', '300MW']
i = 0
for df in dfs:
print(list(df))
print(list(df).index((name,)))
df.plot(x=0, y=list(df).index((name,)), ax=ax, label=labs[i])
i += 1
plt.ylabel = name
plt.xlabel = 'time'
plt.savefig(name)
return fig
def draw(self):
dfs = []
for file in self.file_paths:
dfs.append(self.read_stt(file))
for column in list(dfs[0])[1:]:
self.chart(column[0], dfs)