-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05Temp_Precip_2D.py
259 lines (209 loc) · 11.4 KB
/
05Temp_Precip_2D.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# %%
from utils import read_data, apply_climate, climate_shift, kriging_regression, kriging_predict
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score, mean_squared_error
def get_failure_rate(precip_bins, precip_data, temp_bins, temp_data, break_record, pipe_record, year, step, age_thres):
considered_breaks = break_record[
(break_record['used_time'].dt.year >= year) & (break_record['used_time'].dt.year < year + step) & (
break_record['break_age'] >= age_thres) & (break_record['break_age'] < age_thres + 25)]
considered_breaks['Temp'] = pd.cut(
considered_breaks['Temp'], temp_bins, labels=temp_bins[:-1])
considered_breaks['Precip'] = pd.cut(
considered_breaks['Precip'], precip_bins, labels=precip_bins[:-1])
considered_breaks.dropna(subset=['Temp', 'Precip'], inplace=True)
comsidered_temp = temp_data[(temp_data.index.year >= year) & (
temp_data.index.year < year + step)]
comsidered_pr = precip_data[(precip_data.index.year >= year) & (
precip_data.index.year < year + step)]
climate_days = pd.DataFrame()
climate_days.set_index = comsidered_temp.index
climate_days['aligned_temp'] = pd.cut(
comsidered_temp['used_value'], temp_bins, labels=temp_bins[:-1])
climate_days['aligned_precip'] = pd.cut(
comsidered_pr['used_value'], precip_bins, labels=precip_bins[:-1])
pipe_record.loc[:, 'pipe_age'] = year - pipe_record['INSTALLDATE'].dt.year
pipe_record = pipe_record[(
pipe_record['pipe_age'] < age_thres + 25) & (pipe_record['pipe_age'] >= age_thres)]
pipe_length = pipe_record['ASBUILTLENGTH'].sum()
if pipe_length < 5280:
pipe_length = np.nan
return considered_breaks, climate_days, pipe_length
def plot_single_year(days_data, break_data, failure_rate, precip_low, precip_up, temp_low, temp_up, year, step):
with plt.style.context(['science', 'no-latex']):
plt.imshow(days_data, origin='lower',
extent=[precip_low, precip_up, temp_low, temp_up], aspect='auto')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Days')
plt.xlabel('Mean precipitation')
plt.ylabel('Mean temperature')
plt.title(
"{}-{}Number of Days".format(year, year + step))
plt.tight_layout()
# plt.savefig(
# '../results/MonthlyPrediction/ExampleNumberDays_{}.tiff'.format(year), dpi=300, bbox_inches='tight')
plt.show()
plt.cla()
plt.clf()
plt.imshow(break_data, origin='lower',
extent=[precip_low, precip_up, temp_low, temp_up], aspect='auto')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Breaks')
plt.xlabel('Mean precipitation')
plt.ylabel('Mean temperature')
plt.title(
"{}-{}Number of Breaks".format(year, year + step))
plt.tight_layout()
plt.savefig(
'../results/MonthlyPrediction/ExampleNumberBreaks_{}.tiff'.format(year), dpi=300, bbox_inches='tight')
plt.show()
plt.cla()
plt.clf()
failure_rate[failure_rate == 0] = np.nan
plt.imshow(failure_rate, origin='lower',
extent=[precip_low, precip_up, temp_low, temp_up], aspect='auto')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Failure rates')
plt.xlabel('Mean precipitation')
plt.ylabel('Mean temperature')
plt.title(
"{}-{}failure_rate".format(year, year + step))
plt.tight_layout()
# plt.savefig(
# '../results/MonthlyPrediction/ExampleFailureRate_{}.tiff'.format(year), dpi=300, bbox_inches='tight')
plt.show()
plt.cla()
plt.clf()
if __name__ == '__main__':
variable = 'Mean'
break_record, min_temp, precip, pipe_record = read_data()
break_record = break_record[break_record.used_time.dt.year >= 1990]
shift_time = 29
min_temp = climate_shift(
min_temp, shift_day=shift_time + 1, variable='Temp', variation='Mean')
precip = climate_shift(
precip, shift_day=shift_time + 1, variable='Pr', variation='Mean')
for material_name in ['Cast Iron', 'Ductile Iron', 'Unknown']:
material_break_record = break_record[break_record['MATERIAL']
== material_name]
material_pipe_record = pipe_record[pipe_record['MATERIAL']
== material_name]
material_break_record = apply_climate(
material_break_record, climate_data=min_temp, climate_name='Temp')
material_break_record = apply_climate(
material_break_record, climate_data=precip, climate_name='Precip')
# get temp-precip grid
temp_low = math.floor(
material_break_record['Temp'].quantile(0.05) * 100) / 100
temp_up = math.ceil(
material_break_record['Temp'].quantile(0.95) * 100) / 100
precip_low = math.floor(
material_break_record['Precip'].quantile(0.05) * 100) / 100
precip_up = math.ceil(
material_break_record['Precip'].quantile(0.95) * 100) / 100
bins = 11
temp_bins = np.linspace(temp_low, temp_up, bins)
bins = 11
precip_bins = np.linspace(precip_low, precip_up, bins)
for age_thres in [0, 25, 50, 75]:
if material_name == 'Ductile Iron' and age_thres > 30:
break
step = 1
yearly_failure_rate = []
weights = []
example = False
for year in range(1990, 2020, step):
break_counts, climate_days, pipe_length = get_failure_rate(precip_bins, precip, temp_bins, min_temp, material_break_record,
material_pipe_record, year, step, age_thres)
yearlfaR = np.array(len(break_counts)) / \
np.array(pipe_length) * 528000
break_data = np.zeros((len(temp_bins)-1, len(precip_bins)-1))
days_data = np.zeros((len(temp_bins)-1, len(precip_bins)-1))
for row_index in range(break_data.shape[0]):
for col_index in range(break_data.shape[1]):
break_data[row_index, col_index] = len(break_counts[(break_counts['Temp'] == temp_bins[row_index]) & (
break_counts['Precip'] == precip_bins[col_index])])
days_data[row_index, col_index] = len(climate_days[
(climate_days['aligned_temp'] == temp_bins[row_index]) & (
climate_days['aligned_precip'] == precip_bins[
col_index])])
# days_data[days_data < 2] = 0
failure_rate = break_data / days_data / pipe_length * 528000
failure_rate[failure_rate == np.inf] = np.nan
if (np.sum((failure_rate > 0).any(axis=1)) < 5) or (np.sum((failure_rate > 0).any(axis=0)) < 5):
if example:
plot_single_year(days_data, break_data, failure_rate,
precip_low, precip_up, temp_low, temp_up, year, step)
failure_rate[:] = np.nan
weights.append(days_data * np.nan)
else:
weights.append(days_data * pipe_length)
# failure_rate[failure_rate > 0.21] = np.nan
yearly_failure_rate.append(failure_rate)
mask = np.isnan(np.array(yearly_failure_rate))
masked_FR = np.ma.MaskedArray(
np.array(yearly_failure_rate), mask=mask)
weighted_sum = False
if weighted_sum:
ma_weights = np.ma.MaskedArray(
np.array(weights), mask=mask)
ma_weights = ma_weights / np.sum(ma_weights, axis=0)
average_failure = np.multiply(masked_FR, ma_weights)
average_failure = np.sum(average_failure, axis=0)
else:
average_failure = np.ma.average(masked_FR, axis=0)
# average_failure[average_failure > 0.25] = np.nan
upper_quartile = np.percentile(average_failure, 90)
lower_quartile = np.percentile(average_failure, 0)
average_failure[average_failure > upper_quartile] = np.nan
average_failure[average_failure < lower_quartile] = np.nan
print(
f"material: {material_name}, age: {age_thres}, mean FR {np.mean(average_failure)}")
with plt.style.context(['science', 'no-latex']):
plt.imshow(average_failure, origin='lower', extent=[precip_low, precip_up, temp_low, temp_up],
aspect='auto', vmax=0.125, vmin=0)
cbar = plt.colorbar()
cbar.ax.set_ylabel('Failure rates')
plt.xlabel('Mean precipitation')
plt.ylabel('Mean temperature')
plt.title("Average failure_rate")
plt.tight_layout()
plt.savefig('../results/MonthlyPrediction/test/2DFailure rate_{}_{}.tiff'.format(
material_name, age_thres), dpi=300, bbox_inches='tight')
plt.show()
if np.sum(np.isnan(average_failure)) < 50:
x_pos, y_pos = np.nonzero(~np.isnan(average_failure))
z_values = average_failure[x_pos, y_pos]
X_1 = np.array([temp_bins[i] for i in x_pos])
X_2 = np.array([precip_bins[i] for i in y_pos])
label = r'../results/MonthlyPrediction/test/2DKriging{}{}'.format(
material_name, age_thres)
NSE, parameter, best_score = kriging_regression(
X_2, X_1, z_values, label)
print(
f"The failure rate model of {material_name} and {age_thres}")
print(f"The best score is {best_score}")
print(f"The RMSE value is {NSE}")
new_precip = np.linspace(0.9*precip_low, precip_up, 50)
new_temp = np.linspace(0.9*temp_low, temp_up, 50)
high_res, ss = kriging_predict(
new_precip, new_temp, label, style='grid')
# high_res = predict_value(new_precip, new_temp, label)
with plt.style.context(['science', 'no-latex']):
plt.imshow(high_res, origin='lower', extent=[
precip_low, precip_up, temp_low, temp_up], aspect='auto', vmin=0, vmax=0.08)
cbar = plt.colorbar()
cbar.ax.set_ylabel('Failure rate')
plt.title('Failure rate with kriging')
plt.xlabel('Mean precipitation')
plt.ylabel('Mean temperature')
plt.tight_layout()
plt.savefig(
'../results/MonthlyPrediction/test/2DFailure rate_Kridged_{}_{}.tiff'.format(
material_name, age_thres),
dpi=300, bbox_inches='tight')
plt.show()
# %%