-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.BasicPlots.py
148 lines (114 loc) · 5.84 KB
/
1.BasicPlots.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
# %%
from utils import read_data
import matplotlib.pyplot as plt
from calendar import monthrange
from datetime import datetime
import pytz
plt.style.use(['science', 'no-latex', 'grid'])
plt.rcParams.update({
'figure.figsize': (6, 4), # specify font family here
"font.size": 8})
def pipe_monthly_failure_rate(row, grouped_pipe, material):
current_month = datetime(int(row.year), int(row.month), monthrange(int(row.year), int(row.month))[1], 0, 0, 0, 0,
pytz.UTC)
pipe_month = grouped_pipe[grouped_pipe.INSTALLDATE <= current_month]
# pipe_month = grouped_pipe[((
# current_month - grouped_pipe.INSTALLDATE).dt.days/365 <= 100) & (grouped_pipe.INSTALLDATE <= current_month)]
pipe_length = pipe_month['ASBUILTLENGTH'].sum()
row[f'{material}Length'] = pipe_length / 528000
row[f'{material}FR'] = row.FailureNumber / pipe_length / \
monthrange(int(row.year), int(row.month))[1] * 528000 * 365
pipe_month['age'] = current_month - pipe_month.INSTALLDATE
row[f'{material}MeanAge'] = pipe_month['age'].mean().days / 365
row[f'{material}LowerAge'] = pipe_month['age'].quantile(0.1).days / 365
row[f'{material}UpperAge'] = pipe_month['age'].quantile(0.9).days / 365
return row
def failure_rate_plot(material, break_record, pipe_record):
if material:
break_record = break_record[break_record['MATERIAL'] == material]
pipe_record = pipe_record[pipe_record['MATERIAL'] == material]
failure_number = break_record.groupby(
[break_record.used_time.dt.year, break_record.used_time.dt.month]).count().reindex(monthly_temp_sum.index,
fill_value=0)[['Age_label']]
failure_number.rename(columns={'Age_label': 'FailureNumber'}, inplace=True)
pipe_installation = pipe_record.groupby(
[pipe_record.INSTALLDATE.dt.year, pipe_record.INSTALLDATE.dt.month]).count()[['soil_ID']]
pipe_installation.rename(columns={'soil_ID': "PipeNumber"}, inplace=True)
cache_index = failure_number.index
failure_number.index = failure_number.index.set_names(['year', 'month'])
failure_number = failure_number.reset_index()
failure_number = failure_number.apply(
pipe_monthly_failure_rate, args=(pipe_record, material), axis=1)
failure_number.index = cache_index
return failure_number, pipe_installation
def get_monthly_static(name, data):
monthly_data = data.groupby([data.index.year, data.index.month])
sum_value = monthly_data.mean().rename(columns={'value': f'{name}Mean'})
std_value = monthly_data.std().rename(columns={'value': f'{name}Var'})
accum_difference = data.diff().abs().groupby([data.index.year, data.index.month]).mean().rename(
columns={'value': f'{name}Diff'})
return accum_difference, sum_value, std_value
def plot_monthly_climate(data):
name = data.columns[0]
data.plot()
plt.xlabel('Date')
plt.ylabel(f'Monthly {name}')
plt.title(f'{name}')
plt.tight_layout()
plt.savefig(
f'../results/MonthlyPrediction/{name}.tiff', dpi=300, bbox_inches='tight')
plt.show()
# %%
if __name__ == '__main__':
break_record, min_temp, precip, pipe_record = read_data()
pipe_record = pipe_record[(pipe_record['MATERIAL'] == 'Cast Iron') | (
pipe_record['MATERIAL'] == 'Ductile Iron') | (pipe_record['MATERIAL'] == 'Unknown')]
break_record = break_record[(break_record['MATERIAL'] == 'Cast Iron') | (
break_record['MATERIAL'] == 'Ductile Iron') | (break_record['MATERIAL'] == 'Unknown')]
# break_record = break_record[break_record['break_age'] <= 100]
# %%
min_temp = min_temp[(min_temp.index.year < 2020) &
(min_temp.index.year >= 1990)]
precip = precip[(precip.index.year < 2020) & (precip.index.year >= 1990)]
temp_difference, monthly_temp_sum, monthly_temp_var = get_monthly_static(
'Temp', min_temp)
precip_difference, monthly_precip_sum, monthly_precip_var = get_monthly_static(
'Pr', precip)
# %%
all_failure, all_pipe = failure_rate_plot(
material=False, break_record=break_record, pipe_record=pipe_record)
fig, ax = plt.subplots(figsize=(4, 3))
all_failure.groupby('year').sum().plot(y='FailureNumber', ax=ax)
plt.xlabel('Date')
plt.ylabel('Failure number')
plt.show()
# %%
# Plot the Monthly failure rates by Failures/100 miles/day
all_failure, all_pipe = failure_rate_plot(
'Unknown', break_record, pipe_record)
cast_failure, cast_pipe = failure_rate_plot(
'Cast Iron', break_record, pipe_record)
ductile_failure, ductile_pipe = failure_rate_plot(
'Ductile Iron', break_record, pipe_record)
fig, ax = plt.subplots(figsize=(4, 3))
all_failure.groupby('year').mean().plot(y=f'UnknownFR', ax=ax)
cast_failure.groupby('year').mean().plot(y=f'Cast IronFR', ax=ax)
ductile_failure.groupby('year').mean().plot(y=f'Ductile IronFR', ax=ax)
plt.legend(['Unknown', 'Cast Iron', 'Ductile Iron'])
plt.xlabel('Date')
plt.ylabel('FailureRate(No./year/100miles)')
plt.savefig(f'../results/MonthlyPrediction/Pipe AnnualFR.tiff',
dpi=300, bbox_inches='tight')
plt.show()
# %%
fig, ax = plt.subplots(figsize=(4, 3))
all_failure.groupby('year').mean().plot(y=f'UnknownMeanAge', ax=ax)
cast_failure.groupby('year').mean().plot(y=f'Cast IronMeanAge', ax=ax)
ductile_failure.groupby('year').mean().plot(
y=f'Ductile IronMeanAge', ax=ax)
plt.legend(['Unknown', 'Cast Iron', 'Ductile Iron'])
plt.xlabel('Date')
plt.ylabel('Pipe age (years)')
plt.savefig(f'../results/MonthlyPrediction/Pipe MeanAge.tiff',
dpi=300, bbox_inches='tight')
plt.show()