forked from AZMP-NL/python-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azmp_bottomT_scorecards.py
358 lines (331 loc) · 14.9 KB
/
azmp_bottomT_scorecards.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
'''
To generate AZMP score cards for bottom temperature
Uses pickled object generated by azmp_bottom_stats.py
'''
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
import unicodedata
clim_year = [1981, 2010]
def is_number(s):
#https://www.pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
try:
float(s)
return True
except ValueError:
pass
try:
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#### ------------- For fall ---------------- ####
# 1.
infile = 'stats_2J_fall.pkl'
df = pd.read_pickle(infile)
year_list = df.index # save former index
year_list = [i[2:4] for i in year_list] # 2-digit year
df.index = pd.to_datetime(df.index) # update index to datetime
df['area_colder0'] = df['area_colder0']/1000 # In 1000km
df['area_colder1'] = df['area_colder1']/1000 # In 1000km
df['area_warmer2'] = df['area_warmer2']/1000
df_clim = df[(df.index.year>=clim_year[0]) & (df.index.year<=clim_year[1])]
std_anom = (df-df_clim.mean(axis=0))/df_clim.std(axis=0)
std_anom = std_anom.T
std_anom['MEAN'] = df_clim.mean(axis=0)
std_anom['SD'] = df_clim.std(axis=0)
std_anom = std_anom.reindex(['Tmean', 'Tmean_sha200', 'area_warmer2', 'area_colder1'])
std_anom = std_anom.rename({'Tmean': r'$\rm T_{bot}$', 'Tmean_sha200': r'$\rm T_{bot_{<200m}}$', 'area_warmer2': r'$\rm Area_{>2^{\circ}C}$', 'area_colder1': r'$\rm Area_{<1^{\circ}C}$'})
std_anom.rename(columns={'MEAN': r'$\rm \overline{x}$', 'SD': r'sd'}, inplace=True)
# Get text values + cell color
year_list.append(r'$\rm \overline{x}$') # add 2 extra columns
year_list.append(r'sd')
vals = np.around(std_anom.values,1)
vals[vals==-0.] = 0.
vals_color = vals.copy()
vals_color[-1,] = vals_color[-1,]*-1 # Reverse last row colorscale
vals_color[:,-1] = 0 # No color to last two columns (mean and STD)
vals_color[:,-2] = 0
#vals_color[(vals_color<0.5) & (vals_color>-.5)] = 0.
normal = plt.Normalize(-4.49, 4.49)
cmap = plt.cm.get_cmap('seismic', 9)
#cmap = plt.cm.get_cmap('seismic', 15)
nrows, ncols = std_anom.index.size+1, std_anom.columns.size
hcell, wcell = 0.5, 0.5
hpad, wpad = 0, 0
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
header = ax.table(cellText=[['']],
colLabels=['-- NAFO divison 2J --'],
loc='center'
)
header.set_fontsize(13)
#the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=std_anom.columns,
the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=year_list,
loc='center', cellColours=cmap(normal(vals_color)), cellLoc='center',
bbox=[0, 0, 1, 0.5]
)
# change font color to white where needed:
the_table.auto_set_font_size(False)
the_table.set_fontsize(12.5)
table_props = the_table.properties()
table_cells = table_props['child_artists']
last_columns = np.arange(vals.shape[1]-2, vals.shape[1]) # last columns
for key, cell in the_table.get_celld().items():
cell_text = cell.get_text().get_text()
if is_number(cell_text) == False:
pass
elif key[0] == 0: #year's row = no color
pass
elif key[1] in last_columns:
cell._text.set_color('darkslategray')
elif (np.float(cell_text) <= -1.5) | (np.float(cell_text) >= 1.5) :
cell._text.set_color('white')
plt.savefig("scorecards_fall_2J.png", dpi=300)
os.system('convert -trim scorecards_fall_2J.png scorecards_fall_2J.png')
# 2.
infile = 'stats_3K_fall.pkl'
df = pd.read_pickle(infile)
df.index = pd.to_datetime(df.index) # update index to datetime
df['area_colder0'] = df['area_colder0']/1000 # In 1000km
df['area_colder1'] = df['area_colder1']/1000 # In 1000km
df['area_warmer2'] = df['area_warmer2']/1000
df_clim = df[(df.index.year>=clim_year[0]) & (df.index.year<=clim_year[1])]
std_anom = (df-df_clim.mean(axis=0))/df_clim.std(axis=0)
std_anom = std_anom.T
std_anom['MEAN'] = df_clim.mean(axis=0)
std_anom['SD'] = df_clim.std(axis=0)
std_anom = std_anom.reindex(['Tmean', 'Tmean_sha200', 'area_warmer2', 'area_colder1'])
std_anom = std_anom.rename({'Tmean': r'$\rm T_{bot}$', 'Tmean_sha200': r'$\rm T_{bot_{<200m}}$', 'area_warmer2': r'$\rm Area_{>2^{\circ}C}$', 'area_colder1': r'$\rm Area_{<1^{\circ}C}$'})
std_anom.rename(columns={'MEAN': r'$\rm \overline{x}$', 'SD': r'sd'}, inplace=True)
## df = pd.read_pickle(infile)
## # Pass in 2 digit-index:
## new_index = [i[2:4] for i in df.index]
## df = df.set_index(pd.Series(new_index))
## df['area_colder0'] = df['area_colder0']/1000 # In 1000km
## df['area_colder1'] = df['area_colder1']/1000 # In 1000km
## df['area_warmer2'] = df['area_warmer2']/1000
## std_anom = (df-df.mean(axis=0))/df.std(axis=0)
## std_anom = std_anom.T
## std_anom['MEAN'] = df.mean(axis=0)
## std_anom['SD'] = df.std(axis=0)
## std_anom = std_anom.reindex(['Tmean', 'Tmean_sha300', 'area_warmer2', 'area_colder1'])
## std_anom = std_anom.rename({'Tmean': r'$\rm T_{bot}$', 'Tmean_sha300': r'$\rm T_{bot_{<300m}}$', 'area_warmer2': r'$\rm Area_{>2^{\circ}C}$', 'area_colder1': r'$\rm Area_{<1^{\circ}C}$'})
## std_anom.rename(columns={'MEAN': r'$\rm \overline{x}$', 'SD': r'sd'}, inplace=True)
vals = np.around(std_anom.values,1)
vals[vals==-0.] = 0.
vals_color = vals.copy()
vals_color[-1,] = vals_color[-1,]*-1
vals_color[:,-1] = 0 # No color to last two columns (mean and STD)
vals_color[:,-2] = 0
normal = plt.Normalize(-4.49, 4.49)
cmap = plt.cm.get_cmap('seismic', 9)
nrows, ncols = std_anom.index.size+1, std_anom.columns.size
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
header = ax.table(cellText=[['']],
colLabels=['-- NAFO divison 3K --'],
loc='center'
)
#the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=std_anom.columns,
header.set_fontsize(12.5)
the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=None,
loc='center', cellColours=cmap(normal(vals_color)), cellLoc='center',
bbox=[0, 0, 1.0, 0.50]
)
the_table.auto_set_font_size(False)
the_table.set_fontsize(12.5)
# change font color to white where needed:
table_props = the_table.properties()
table_cells = table_props['child_artists']
last_columns = np.arange(vals.shape[1]-2, vals.shape[1]) # last columns
for key, cell in the_table.get_celld().items():
cell_text = cell.get_text().get_text()
if is_number(cell_text) == False:
pass
#elif key[0] == 0:# <--- remove when no years
# pass
elif key[1] in last_columns:
cell._text.set_color('darkslategray')
elif (np.float(cell_text) <= -1.5) | (np.float(cell_text) >= 1.5) :
cell._text.set_color('white')
plt.savefig("scorecards_fall_3K.png", dpi=300)
os.system('convert -trim scorecards_fall_3K.png scorecards_fall_3K.png')
# 3.
infile = 'stats_3LNO_fall.pkl'
df = pd.read_pickle(infile)
df.index = pd.to_datetime(df.index) # update index to datetime
df['area_colder0'] = df['area_colder0']/1000 # In 1000km
df['area_colder1'] = df['area_colder1']/1000 # In 1000km
df['area_warmer2'] = df['area_warmer2']/1000
df_clim = df[(df.index.year>=clim_year[0]) & (df.index.year<=clim_year[1])]
std_anom = (df-df_clim.mean(axis=0))/df_clim.std(axis=0)
std_anom = std_anom.T
std_anom['MEAN'] = df_clim.mean(axis=0)
std_anom['SD'] = df_clim.std(axis=0)
std_anom = std_anom.reindex(['Tmean', 'Tmean_sha200', 'area_warmer2', 'area_colder1'])
std_anom = std_anom.rename({'Tmean': r'$\rm T_{bot}$', 'Tmean_sha200': r'$\rm T_{bot_{<200m}}$', 'area_warmer2': r'$\rm Area_{>2^{\circ}C}$', 'area_colder1': r'$\rm Area_{<1^{\circ}C}$'})
std_anom.rename(columns={'MEAN': r'$\rm \overline{x}$', 'SD': r'sd'}, inplace=True)
vals = np.around(std_anom.values,1)
vals[vals==-0.] = 0.
vals_color = vals.copy()
vals_color[-1,] = vals_color[-1,]*-1
vals_color[:,-1] = 0 # No color to last two columns (mean and STD)
vals_color[:,-2] = 0
normal = plt.Normalize(-4.49, 4.49)
cmap = plt.cm.get_cmap('seismic', 9)
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
header = ax.table(cellText=[['']],
colLabels=['-- NAFO divison 3LNO --'],
loc='center'
)
header.set_fontsize(12.5)
the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=None,
loc='center', cellColours=cmap(normal(vals_color)), cellLoc='center',
bbox=[0, 0, 1.0, 0.50]
)
# change font color to white where needed:
the_table.auto_set_font_size(False)
the_table.set_fontsize(12.5)
table_props = the_table.properties()
table_cells = table_props['child_artists']
last_columns = np.arange(vals.shape[1]-2, vals.shape[1]) # last columns
for key, cell in the_table.get_celld().items():
cell_text = cell.get_text().get_text()
if is_number(cell_text) == False:
pass
#elif key[0] == 0:# <--- remove when no years
# pass
elif key[1] in last_columns:
cell._text.set_color('darkslategray')
elif (np.float(cell_text) <= -1.5) | (np.float(cell_text) >= 1.5) :
cell._text.set_color('white')
plt.savefig("scorecards_fall_3LNO.png", dpi=300)
os.system('convert -trim scorecards_fall_3LNO.png scorecards_fall_3LNO.png')
os.system('montage scorecards_fall_2J.png scorecards_fall_3K.png scorecards_fall_3LNO.png -tile 1x3 -geometry +1+1 -background white scorecards_botT_fall.png')
#### ------------- For Spring ---------------- ####
# 1.
infile = 'stats_3LNO_spring.pkl'
df = pd.read_pickle(infile)
year_list = df.index # save former index
year_list = [i[2:4] for i in year_list] # 2-digit year
df.index = pd.to_datetime(df.index) # update index to datetime
df['area_colder0'] = df['area_colder0']/1000 # In 1000km
df['area_colder1'] = df['area_colder1']/1000 # In 1000km
df['area_warmer2'] = df['area_warmer2']/1000
df_clim = df[(df.index.year>=clim_year[0]) & (df.index.year<=clim_year[1])]
std_anom = (df-df_clim.mean(axis=0))/df_clim.std(axis=0)
std_anom = std_anom.T
std_anom['MEAN'] = df_clim.mean(axis=0)
std_anom['SD'] = df_clim.std(axis=0)
std_anom = std_anom.reindex(['Tmean', 'Tmean_sha200', 'area_warmer2', 'area_colder1'])
std_anom = std_anom.rename({'Tmean': r'$\rm T_{bot}$', 'Tmean_sha200': r'$\rm T_{bot_{<200m}}$', 'area_warmer2': r'$\rm Area_{>2^{\circ}C}$', 'area_colder1': r'$\rm Area_{<1^{\circ}C}$'})
std_anom.rename(columns={'MEAN': r'$\rm \overline{x}$', 'SD': r'sd'}, inplace=True)
year_list.append(r'$\rm \overline{x}$') # add 2 extra columns
year_list.append(r'sd')
vals = np.around(std_anom.values,1)
vals[vals==-0.] = 0.
vals_color = vals.copy()
vals_color[-1,] = vals_color[-1,]*-1
vals_color[:,-1] = 0 # No color to last two columns (mean and STD)
vals_color[:,-2] = 0
normal = plt.Normalize(-4.49, 4.49)
cmap = plt.cm.get_cmap('seismic', 9)
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
header = ax.table(cellText=[['']],
colLabels=['-- NAFO divison 3LNO --'],
loc='center'
)
header.set_fontsize(12.5)
#the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=std_anom.columns,
the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=year_list,
loc='center', cellColours=cmap(normal(vals_color)), cellLoc='center',
bbox=[0, 0, 1.0, 0.50]
)
# change font color to white where needed:
the_table.auto_set_font_size(False)
the_table.set_fontsize(12.5)
table_props = the_table.properties()
table_cells = table_props['child_artists']
last_columns = np.arange(vals.shape[1]-2, vals.shape[1]) # last columns
for key, cell in the_table.get_celld().items():
cell_text = cell.get_text().get_text()
if is_number(cell_text) == False:
pass
elif key[0] == 0:
pass
elif key[1] in last_columns:
cell._text.set_color('darkslategray')
elif (np.float(cell_text) <= -1.5) | (np.float(cell_text) >= 1.5) :
cell._text.set_color('white')
plt.savefig("scorecards_spring_3LNO.png", dpi=300)
os.system('convert -trim scorecards_spring_3LNO.png scorecards_spring_3LNO.png')
# 2.
infile = 'stats_3Ps_spring.pkl'
df = pd.read_pickle(infile)
df.index = pd.to_datetime(df.index) # update index to datetime
df['area_colder0'] = df['area_colder0']/1000 # In 1000km
df['area_colder1'] = df['area_colder1']/1000 # In 1000km
df['area_warmer2'] = df['area_warmer2']/1000
df_clim = df[(df.index.year>=clim_year[0]) & (df.index.year<=clim_year[1])]
std_anom = (df-df_clim.mean(axis=0))/df_clim.std(axis=0)
std_anom = std_anom.T
std_anom['MEAN'] = df_clim.mean(axis=0)
std_anom['SD'] = df_clim.std(axis=0)
std_anom = std_anom.reindex(['Tmean', 'Tmean_sha200', 'area_warmer2', 'area_colder1'])
std_anom = std_anom.rename({'Tmean': r'$\rm T_{bot}$', 'Tmean_sha200': r'$\rm T_{bot_{<200m}}$', 'area_warmer2': r'$\rm Area_{>2^{\circ}C}$', 'area_colder1': r'$\rm Area_{<1^{\circ}C}$'})
std_anom.rename(columns={'MEAN': r'$\rm \overline{x}$', 'SD': r'sd'}, inplace=True)
vals = np.around(std_anom.values,1)
vals[vals==-0.] = 0.
vals_color = vals.copy()
vals_color[-1,] = vals_color[-1,]*-1
vals_color[:,-1] = 0 # No color to last two columns (mean and STD)
vals_color[:,-2] = 0
normal = plt.Normalize(-4.49, 4.49)
cmap = plt.cm.get_cmap('seismic', 9)
nrows, ncols = std_anom.index.size+1, std_anom.columns.size
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
header = ax.table(cellText=[['']],
colLabels=['-- NAFO divison 3Ps --'],
loc='center'
)
header.set_fontsize(12.5)
#the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=std_anom.columns,
the_table=ax.table(cellText=vals, rowLabels=std_anom.index, colLabels=None,
loc='center', cellColours=cmap(normal(vals_color)), cellLoc='center',
bbox=[0, 0, 1.0, 0.50]
)
# change font color to white where needed:
the_table.auto_set_font_size(False)
the_table.set_fontsize(12.5)
table_props = the_table.properties()
table_cells = table_props['child_artists']
last_columns = np.arange(vals.shape[1]-2, vals.shape[1]) # last columns
for key, cell in the_table.get_celld().items():
cell_text = cell.get_text().get_text()
if is_number(cell_text) == False:
pass
#elif key[0] == 0: # <--- remove when no years
# pass
elif key[1] in last_columns:
cell._text.set_color('darkslategray')
elif (np.float(cell_text) <= -1.5) | (np.float(cell_text) >= 1.5) :
cell._text.set_color('white')
plt.savefig("scorecards_spring_3Ps.png", dpi=300)
os.system('convert -trim scorecards_spring_3Ps.png scorecards_spring_3Ps.png')
os.system('montage scorecards_spring_3LNO.png scorecards_spring_3Ps.png -tile 1x3 -geometry +1+1 -background white scorecards_botT_spring.png')