-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions_Extreme_WTs.py
executable file
·1111 lines (939 loc) · 50.2 KB
/
Functions_Extreme_WTs.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
'''File name: Functions_Extreme-WTs.py
Author: Andreas Prein
E-mail: [email protected]
Date created: 21.07.2019
Date last modified: 21.07.2019
##############################################################
Purpos:
Contains functions that are called for the Weather Typing
of extreme precipiation events
'''
from dateutil import rrule
import datetime
import glob
from netCDF4 import Dataset
import sys, traceback
import dateutil.parser as dparser
import string
from pdb import set_trace as stop
import numpy as np
import numpy.ma as ma
import os
from mpl_toolkits import basemap
# import ESMF
import pickle
import subprocess
import pandas as pd
from scipy import stats
import copy
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib as mpl
import pylab as plt
import random
import scipy.ndimage as ndimage
import scipy
import shapefile
import matplotlib.path as mplPath
from matplotlib.patches import Polygon as Polygon2
# Cluster specific modules
from scipy.cluster.hierarchy import dendrogram, linkage
from scipy.cluster.hierarchy import cophenet
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import fcluster
from scipy.cluster.vq import kmeans2, vq, whiten
from scipy.ndimage import gaussian_filter
import seaborn as sns
# import metpy.calc as mpcalc
import matplotlib.gridspec as gridspec
from collections import OrderedDict
def fancy_dendrogram(*args, **kwargs):
max_d = kwargs.pop('max_d', None)
if max_d and 'color_threshold' not in kwargs:
kwargs['color_threshold'] = max_d
annotate_above = kwargs.pop('annotate_above', 0)
ddata = dendrogram(*args, **kwargs)
if not kwargs.get('no_plot', False):
plt.title('Hierarchical Clustering Dendrogram (truncated)')
plt.xlabel('sample index or (cluster size)')
plt.ylabel('distance')
for i, d, c in zip(ddata['icoord'], ddata['dcoord'], ddata['color_list']):
x = 0.5 * sum(i[1:3])
y = d[1]
if y > annotate_above:
plt.plot(x, y, 'o', c=c)
plt.annotate("%.3g" % y, (x, y), xytext=(0, -5),
textcoords='offset points',
va='top', ha='center')
if max_d:
plt.axhline(y=max_d, c='k')
return ddata
def add_subplot_axes(ax,rect,axisbg='w'):
fig = plt.gcf()
box = ax.get_position()
width = box.width
height = box.height
inax_position = ax.transAxes.transform(rect[0:2])
transFigure = fig.transFigure.inverted()
infig_position = transFigure.transform(inax_position)
x = infig_position[0]
y = infig_position[1]
width *= rect[2]
height *= rect[3] # <= Typo was here
subax = fig.add_axes([x, y, width, height]) #,axisbg=axisbg)
x_labelsize = subax.get_xticklabels()[0].get_size()
y_labelsize = subax.get_yticklabels()[0].get_size()
x_labelsize *= rect[2]**0.5
y_labelsize *= rect[3]**0.5
subax.xaxis.set_tick_params(labelsize=x_labelsize)
subax.yaxis.set_tick_params(labelsize=y_labelsize)
return subax
def Scatter_ED_PR(EuclDist,
ClosestWT,
PRall,
NrExtremes,
PlotLoc='./',
PlotName='Scatter.pdf'):
import matplotlib.gridspec as gridspec
from matplotlib import pyplot
# plots a scatter diagram comparing eucledian distacnes from
# EWT centroids with daily precipitation volumes in the target region
plt.rcParams.update({'font.size': 18})
fig = plt.figure(figsize=(15, 15), constrained_layout=True)
widths = [3, 1]
heights = [1, 3]
gs1 = gridspec.GridSpec(ncols=2, nrows=2, width_ratios=widths, height_ratios=heights)
# plot scatter first
ax = fig.add_subplot(gs1[1, 0])
ax.scatter(EuclDist, PRall, color="k", s=2)
# highlight the top extreme events
ExtremePR=PRall[np.argsort(PRall)][::-1][:NrExtremes]
ExtrPR_ED=EuclDist[np.argsort(PRall)][::-1][:NrExtremes]
# lable the WTs of the most extreme days in color
WTcolors=['#1f78b4', '#33a02c', '#e31a1c', '#ff7f00', '#a6cee3', '#b2df8a', '#fb9a99', '#fdbf6f', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928', '#1f78b4', '#33a02c', '#e31a1c', '#ff7f00', '#a6cee3', '#b2df8a', '#fb9a99', '#fdbf6f', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928']
XWTs=ClosestWT[np.argsort(PRall)][::-1][:NrExtremes]
XWTunique=np.unique(XWTs)
for xwt in range(len(XWTunique)):
iAct=(XWTs == XWTunique[xwt])
ax.scatter(ExtrPR_ED[iAct], ExtremePR[iAct], alpha=1, color=WTcolors[XWTunique[xwt]], label='XWT-'+str(XWTunique[xwt]+1), s=30)
# ax.scatter(ExtrPR_ED,ExtremePR,color='r', s=15)
Q75_Extr=np.percentile(ExtrPR_ED, 75)
plt.axvline(x=Q75_Extr, c='r', ls='--', lw=2.5)
plt.xlabel('Eucledian Distance []')
plt.ylabel('SCS count [events per day]')
Xrange=ax.get_xlim()
Yrange=ax.get_ylim()
ax.legend(loc='upper right')
# plot histogram for EDs
ax = fig.add_subplot(gs1[0, 0])
bins = np.linspace(Xrange[0], Xrange[1], 50)
pyplot.hist(EuclDist, bins, alpha=0.5, color='k', label='all events', density=True)
pyplot.hist(ExtrPR_ED, bins, alpha=0.5, color='r', label='extremes', density=True)
pyplot.legend(loc='upper right')
plt.ylabel('Probability []')
plt.xlim([Xrange[0], Xrange[1]])
# plot histogram for PR
ax = fig.add_subplot(gs1[1, 1])
bins = np.linspace(Yrange[0], Yrange[1], 50)
pyplot.hist(PRall, bins, alpha=0.5, color='k', label='all events', density=True, orientation="horizontal")
pyplot.hist(ExtremePR, bins, alpha=0.5, color='r', label='extremes', density=True, orientation="horizontal")
plt.xlabel('Probability []')
plt.ylim([Yrange[0], Yrange[1]])
# Calculate the skill scores
# from Functions_Extreme_WTs import ExtremeDays
# rgiExtrEval=ExtremeDays(PRall,NrExtremes,7)
rgiExtrEval=np.argsort(PRall)[-NrExtremes:]
from Functions_Extreme_WTs import MRR, MRD, perkins_skill
# Perkins Skill Score
rPSS=perkins_skill(EuclDist, EuclDist[rgiExtrEval], 0.5)
# Mean relative difference
rMRD=MRD(EuclDist, PRall, rgiExtrEval)
# Mean Rank Ratio
rMRR=MRR(EuclDist, rgiExtrEval)
# % of days excluded
Excluded=(1-np.sum(EuclDist < np.nanpercentile(EuclDist[rgiExtrEval], 75))/float(len(EuclDist)))*100.
# Write skill-scores in top right corner of plot
plt.text(0.70, 0.85, 'Perkins Skill Score: '+str("%.2f" % rPSS), fontsize=17, transform=plt.gcf().transFigure)
plt.text(0.70, 0.81, 'Mean relative difference: '+str("%.2f" % rMRD), fontsize=17, transform=plt.gcf().transFigure)
plt.text(0.70, 0.77, 'Mean Rank Ratio: '+str("%.2f" % rMRR), fontsize=17, transform=plt.gcf().transFigure)
plt.text(0.70, 0.73, 'Excluded days: '+str("%.2f" % Excluded)+' %', fontsize=17, transform=plt.gcf().transFigure)
print((' plot: '+PlotLoc+PlotName))
fig.savefig(PlotLoc+PlotName)
def ReadPRISM(rgiYears, # array containing the years that should be read
iNrOfExtremes, # number of extreme events
rgiSeasonWT, # months that should be processed
iMonths, # array of months that should be read in
grPRregion): # shapefile that contains target region
ncid=Dataset('/glade/work/prein/observations/PRISM/data/PR/PRISM_daily_ppt_2014.nc', mode='r') # open the netcdf file
rgrLatPR=np.squeeze(ncid.variables['lat'][:])
rgrLonPR=np.squeeze(ncid.variables['lon'][:])
ncid.close()
rgrGridCells=[(rgrLonPR.ravel()[ii], rgrLatPR.ravel()[ii]) for ii in range(len(rgrLonPR.ravel()))]
rgrSRact=np.array(grPRregion.contains_points(rgrGridCells)); rgrSRact=np.reshape(rgrSRact, (rgrLatPR.shape[0], rgrLatPR.shape[1]))
rgiSrPR=np.array(np.where(rgrSRact == True))
iLatMax=rgiSrPR[0,:].max()
iLatMin=rgiSrPR[0,:].min()
iLonMax=rgiSrPR[1,:].max()
iLonMin=rgiSrPR[1,:].min()
rgrPRdata=np.zeros((sum(rgiSeasonWT), iLatMax-iLatMin, iLonMax-iLonMin))
jj=0
for yy in range(len(rgiYears)):
rgdTimeYY = pd.date_range(datetime.datetime(rgiYears[0]+yy, 1, 1, 0), end=datetime.datetime(rgiYears[0]+yy, 12, 31, 23), freq='d')
rgiDD=np.where(((rgdTimeYY.year == rgiYears[0]+yy) & (np.isin(rgdTimeYY.month, iMonths))))[0]
ncid=Dataset('/glade/work/prein/observations/PRISM/data/PR/PRISM_daily_ppt_'+str(rgiYears[0]+yy)+'.nc', mode='r') # open the netcdf file
rgrPRdata[jj:jj+len(rgiDD),:,:]=np.squeeze(ncid.variables['PR'][rgiDD, iLatMin:iLatMax, iLonMin:iLonMax])
ncid.close()
jj=jj+len(rgiDD)
rgrPRdata[rgrPRdata < 0]=np.nan
rgiSRgridcells=rgrSRact[iLatMin:iLatMax, iLonMin:iLonMax]
rgrPRrecords=np.nanmean(rgrPRdata[:, rgiSRgridcells], axis=(1))
SortedDates=np.argsort(rgrPRrecords)[:][::-1]
MinDistDD=7 # two extremes should be at least 7 days appart
rgiExtremePR=np.zeros((iNrOfExtremes)); rgiExtremePR[:]=np.nan
ii=1
jj=1
rgiExtremePR[0]=SortedDates[0]
while ii < iNrOfExtremes:
if np.nanmin(np.abs(rgiExtremePR - SortedDates[jj])) < MinDistDD:
jj=jj+1
else:
rgiExtremePR[ii]=SortedDates[jj]
jj=jj+1
ii=ii+1
rgiExtremePR=rgiExtremePR.astype('int')
return rgrPRrecords, rgiExtremePR
# ###################################################
def ReadERAI(grWTregion,
rgdTime,
iMonths, # list of months that should be considered
rgsWTfolders,
rgsWTvars):
rgiYears=np.unique(rgdTime.year)
ncid=Dataset('/glade/scratch/prein/ERA-Interim/PSL/fin_PSL-sfc_ERA-Interim_12-0_2014.nc', mode='r') # open the netcdf file
rgrLatWT1D=np.squeeze(ncid.variables['lat'][:])
rgrLonWT1D=np.squeeze(ncid.variables['lon'][:])
ncid.close()
rgrLonWT=np.asarray(([rgrLonWT1D,]*rgrLatWT1D.shape[0]))
rgrLonWT[rgrLonWT > 180]=rgrLonWT[rgrLonWT > 180]-360
rgrLatWT=np.asarray(([rgrLatWT1D,]*rgrLonWT1D.shape[0])).transpose()
rgrGridCells=[(rgrLatWT.ravel()[ii], rgrLonWT.ravel()[ii]) for ii in range(len(rgrLonWT.ravel()))]
rgrSRact=np.array(grWTregion.contains_points(rgrGridCells)); rgrSRact=np.reshape(rgrSRact, (rgrLatWT.shape[0], rgrLatWT.shape[1]))
rgiSrWT=np.array(np.where(rgrSRact == True))
iLatMax=rgiSrWT[0,:].max()
iLatMin=rgiSrWT[0,:].min()
iLonMax=rgiSrWT[1,:].max()
iLonMin=rgiSrWT[1,:].min()
DailyVars=np.zeros((len(rgdTime), iLatMax-iLatMin, iLonMax-iLonMin, len(rgsWTvars))); DailyVars[:]=np.nan
for yy in range(len(rgiYears)):
print((' Read ERA-I year: '+str(rgiYears[yy])))
DaysYY = pd.date_range(datetime.datetime(rgiYears[yy], 1, 1, 0), end=datetime.datetime(rgiYears[yy], 12, 31, 23), freq='d')
DD=((rgdTime.year == rgiYears[yy]) & np.isin(rgdTime.month, iMonths))
DDactYYYY=np.isin(DaysYY.month, iMonths)
# DDactYYYY=((DaysYY.month >= iStartMon) & (DaysYY.month <= iStopMon))
for va in range(len(rgsWTvars)):
ncid=Dataset(rgsWTfolders[va]+str(rgiYears[yy])+'.nc', mode='r')
DailyVars[DD,:,:, va]=np.squeeze(np.squeeze(ncid.variables[rgsWTvars[va]])[:, iLatMin:iLatMax, iLonMin:iLonMax])[DDactYYYY,:]
ncid.close()
return DailyVars, rgrLonWT[iLatMin:iLatMax, iLonMin:iLonMax], rgrLatWT[iLatMin:iLatMax, iLonMin:iLonMax]
# ###################################################
def PreprocessWTdata(DailyVars, # WT data [time,lat,lon,var]
RelAnnom=1, # calculate relative anomalies [1-yes; 0-no]
SmoothSigma=0, # Smoothing stddev (Gaussian smoothing)
RemoveAnnualCycl=1, # remove annual cycle [1-yes; 0-no]
NormalizeData=1, # normalize data [1-yes; 0-no]
ReferencePer=None): # period for normalizing the data
# Calculate relative anomaly
if RelAnnom == 1:
# we have to work with absolute values for this since we risk to divide by zero values in the climatology
DailyVars=np.abs(DailyVars)
if ReferencePer is None:
DailyVars=(DailyVars-np.mean(DailyVars, axis=0)[None,:])/np.mean(DailyVars, axis=0)[None,:]
else:
DailyVars=(DailyVars-np.mean(DailyVars[ReferencePer], axis=0)[None,:])/np.mean(DailyVars[ReferencePer], axis=0)[None,:]
# Spatially smooth the data
DailyVars=gaussian_filter(DailyVars[:,:,:,:], sigma=(0, SmoothSigma, SmoothSigma, 0))
# Remove the annual cycle
if RemoveAnnualCycl == 1:
SpatialMeanData=pd.DataFrame(np.nanmean(DailyVars, axis=(1, 2)))
Averaged=np.roll(np.array(SpatialMeanData.rolling(window=21).mean()), -10, axis=0)
Averaged[:10,:]=Averaged[11,:][None,:]; Averaged[-10:,:]=Averaged[-11,:][None,:]
DailyVars=DailyVars-Averaged[:, None, None,:]
# Normalize the data
if NormalizeData == 1:
DailyVars=(DailyVars-np.mean(DailyVars, axis=(1, 2))[:, None, None,:])/np.std(DailyVars, axis=(1, 2))[:, None, None,:]
# if ReferencePer is None:
# DailyVars=(DailyVars-np.mean(DailyVars, axis=(0,1,2))[None,None,None,:])/np.std(DailyVars, axis=(0,1,2))[None,None,None,:]
# else:
# DailyVars=(DailyVars-np.mean(DailyVars[ReferencePer], axis=(0,1,2))[None,None,None,:])/np.std(DailyVars[ReferencePer], axis=(0,1,2))[None,None,None,:]
DailyVars[np.isnan(DailyVars)]=0
return DailyVars
# ===================================================================
def GetExtremeDays(DailyVars,
rgdTime,
rgiExtremeDays):
# Grab the extreme days from the full data
rgrWTdata=np.zeros((len(rgiExtremeDays), DailyVars.shape[1], DailyVars.shape[2], DailyVars.shape[3]))
for dd in range(len(rgiExtremeDays)):
rgdTimeYY = pd.date_range(datetime.datetime(rgiExtremeDays[dd].year, 1, 1, 0), end=datetime.datetime(rgiExtremeDays[dd].year, 12, 31, 23), freq='d')
rgiDD=np.where(((rgdTime.year == rgiExtremeDays[dd].year) & (rgdTime.month ==rgiExtremeDays[dd].month ) & (rgdTime.day == rgiExtremeDays[dd].day)))[0]
rgrWTdata[dd,:,:,:]=DailyVars[rgiDD[0],:]
return rgrWTdata
# ===================================================================
def ClusterAnalysis(rgrWTdata,
sPlotDir,
iNrOfExtremes,
YYYY_stamp,
ClusterMeth, # current options are ['HandK','hdbscan']
Plot=0, # 0 - no plots; 1 - plots will be saved to sPlotDir
ClusterBreakup=0):
if ClusterMeth == 'hdbscan':
# #--------------------------------------------------
# # HDBSCAN -- https://hdbscan.readthedocs.io/en/latest/basic_hdbscan.html
import hdbscan
rgrDataCluster=np.reshape(rgrWTdata, (rgrWTdata.shape[0], rgrWTdata.shape[1]*rgrWTdata.shape[2]*rgrWTdata.shape[3]))
# clusterer = hdbscan.HDBSCAN(min_cluster_size=2).fit(rgrDataCluster)
Epsilon = int(np.std(np.sum(np.abs(rgrDataCluster), axis=1)))
MinClusterSize = 4
clusterer = hdbscan.HDBSCAN(min_cluster_size=MinClusterSize, min_samples=1, cluster_selection_epsilon=Epsilon).fit(rgrDataCluster)
Clusters=clusterer.labels_
# Recluster the outlier cases seperately
Outlier = (Clusters == -1)
if np.sum(Outlier) == len(Clusters):
Clusters[:]=0
if np.sum(Outlier) > 2:
ClustersO = hdbscan.HDBSCAN(min_cluster_size=2, min_samples=1, cluster_selection_epsilon=1).fit(rgrDataCluster[Outlier,:]).labels_
ClustersO = ClustersO + Clusters.max()+1
Clusters[Outlier] = ClustersO
if ClusterBreakup == 1:
# split a cluster if it contains unproportionally many cases
ClusterSizes = np.array([np.sum(Clusters == ii) for ii in range(np.max(Clusters))])
if len(ClusterSizes) == 0:
ClusterSizes = [len(Clusters)]
try:
MAX = max(ClusterSizes)
except:
stop()
if (max(ClusterSizes)/len(Clusters) > 0.5) & (len(Clusters) > 10) == True:
iClusterL = (Clusters == np.argmax(ClusterSizes))
ClusterL = hdbscan.HDBSCAN(min_cluster_size=3, min_samples=1, cluster_selection_epsilon=1).fit(rgrDataCluster[iClusterL,:]).labels_
# Recluster the outlier cases seperately
Outlier = (ClusterL == -1)
if np.sum(Outlier) > 2:
ClustersO = hdbscan.HDBSCAN(min_cluster_size=2, min_samples=1, cluster_selection_epsilon=Epsilon).fit(rgrDataCluster[iClusterL,:][Outlier,:]).labels_
ClustersO = ClustersO + ClusterL.max()+1
ClusterL[Outlier] = ClustersO
ClusterL = ClusterL + Clusters.max()+1
Clusters[iClusterL] = ClusterL
Clusters[Clusters >= np.argmax(ClusterSizes)] = Clusters[Clusters >= np.argmax(ClusterSizes)]-1
UniqueClus=np.where(Clusters == -1)[0]
if len(UniqueClus) > 0:
MaxClust=Clusters.max()
for uc in range(len(UniqueClus)):
Clusters[UniqueClus[uc]]=MaxClust+1+uc
# check if cluster count starts with zero
if np.min(Clusters) != 0:
Clusters=Clusters-np.min(Clusters)
# calculate centroids
rgrWTcentroids=np.zeros((Clusters.max()+1, rgrWTdata.shape[1], rgrWTdata.shape[2], rgrWTdata.shape[3])); rgrWTcentroids[:]=np.nan
for cc in range(Clusters.max()+1):
rgiClAct=(Clusters == (cc))
rgrWTcentroids[cc,:]=np.mean(rgrWTdata[rgiClAct,:,:,:], axis=0)
rgrWTcentroids=np.reshape(rgrWTcentroids, (rgrWTcentroids.shape[0], rgrWTcentroids.shape[1]*rgrWTcentroids.shape[2]*rgrWTcentroids.shape[3]))
rgrClustersFin=(rgrWTcentroids, Clusters)
if ClusterMeth == 'HandK':
# --------------------------------------------------
# # PERFORM HIRACHICAL AND K-MEANS CLUSTER ANALYSIS
# see excample: https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/
rgrDataCluster=np.reshape(rgrWTdata, (rgrWTdata.shape[0], rgrWTdata.shape[1]*rgrWTdata.shape[2]*rgrWTdata.shape[3]))
rgrCluster = linkage(rgrDataCluster, 'ward')
cc, coph_dists = cophenet(rgrCluster, pdist(rgrDataCluster))
# last = rgrCluster[-25:, 2]
last = rgrCluster[-6:, 2]
last_rev = last[::-1]
idxs = np.arange(1, len(last) + 1)
acceleration = np.diff(last, 2) # 2nd derivative of the distances
acceleration_rev = acceleration[::-1]
if Plot == 1:
# PLOT DENDROGRAM
fig = plt.figure(figsize=(10, 6))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('sample index')
plt.ylabel('distance')
from Functions_Extreme_WTs import fancy_dendrogram
fancy_dendrogram(rgrCluster,
truncate_mode='lastp', # show only the last p merged clusters
p=12, # show only the last p merged clusters
leaf_rotation=90., # rotates the x axis labels
leaf_font_size=12., # font size for the x axis labels
show_contracted=True, # to get a distribution impression in truncated branches
annotate_above=10,
max_d=36,
)
sPlotFile=sPlotDir
sPlotName= 'BottomUp-Hirarch-Cluster_Dendrogram_'+str("%03d" % iNrOfExtremes)+'_'+YYYY_stamp+'.pdf'
if os.path.isdir(sPlotFile) != 1:
subprocess.call(["mkdir", "-p", sPlotFile])
print((' Plot map to: '+sPlotFile+sPlotName))
fig.savefig(sPlotFile+sPlotName)
# PLOT DISTANCE ACCELERATION
fig = plt.figure(figsize=(7, 6))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Nr. of weather types')
plt.ylabel('distance')
plt.plot(idxs, last_rev, c='k')
plt.plot(idxs[:-2] + 1, acceleration_rev, c='r')
sPlotFile=sPlotDir
sPlotName= 'Accelaration-Of-Distance-Growth_'+str("%03d" % iNrOfExtremes)+'_'+YYYY_stamp+'.pdf'
if os.path.isdir(sPlotFile) != 1:
subprocess.call(["mkdir", "-p", sPlotFile])
print((' Plot map to: '+sPlotFile+sPlotName))
fig.savefig(sPlotFile+sPlotName)
iClusters1=0
try:
while acceleration_rev[iClusters1] > 0: iClusters1=iClusters1+1
iClusters2=np.where(max(acceleration_rev) == acceleration_rev)[0][0]+1
except:
iClusters2=iClusters1
iClusters=np.max([iClusters1, iClusters2])
rThreshold=last[-iClusters]
rgiClusterMembers=fcluster(rgrCluster, rThreshold, criterion='distance')
rgrClusters=np.zeros((rgiClusterMembers.max(), rgrDataCluster.shape[1])); rgrClusters[:]=np.nan
for cc in range(rgiClusterMembers.max()):
rgiClAct=(rgiClusterMembers == (cc+1))
rgrClusters[cc,:]=np.mean(rgrDataCluster[rgiClAct,:], axis=0)
# use this as initial seed for the k-means clustering
rgrClustersFin=kmeans2(rgrDataCluster, rgrClusters)
# stop()
return rgrClustersFin
# ===================================================================
def EucledianDistance(DailyVars,
rgrClustersFin):
SHAPE=DailyVars.shape
Data_flatten=np.reshape(DailyVars, (SHAPE[0], SHAPE[1]*SHAPE[2]*SHAPE[3]))
EucledianDist=np.zeros((SHAPE[0], rgrClustersFin[0].shape[0])); EucledianDist[:]=np.nan
Correlation=np.copy(EucledianDist)
for dd in range(SHAPE[0]):
EucledianDist[dd,:] = np.array([np.linalg.norm(rgrClustersFin[0][wt,:]-Data_flatten[dd,:]) for wt in range(rgrClustersFin[0].shape[0])])
Correlation[dd,:] = np.array([np.corrcoef(rgrClustersFin[0][wt,:], Data_flatten[dd,:])[0][1] for wt in range(rgrClustersFin[0].shape[0])])
return EucledianDist, Correlation
# Perkins Skill Score
# overlap between two PDFs | zero is best
def perkins_skill(data1, data2, Binsize):
Min=np.nanmin([np.nanmin(data1), np.nanmin(data2)])
Max=np.nanmax([np.nanmax(data1), np.nanmax(data2)])
try:
hist, bin_edges = np.histogram(data1[~np.isnan(data1)], bins=np.arange(Min, Max, Binsize), density=True)
except:
stop()
pdf1 = hist*np.diff(bin_edges)
try:
histEx, bin_edgesEx = np.histogram(data2, bins=np.arange(Min, Max, Binsize), density=True)
except:
stop()
pdf2 = histEx*np.diff(bin_edgesEx)
mins = np.minimum(pdf1, pdf2)
ss = np.nansum(mins)
return ss
# Mean relative difference
# Mean relative difference between extreme PR cases and cases with low
# Eucledian Distances | zero is best
def MRD(Distance, Precipitation, iExtremes):
Extreme75P=np.nanpercentile(Distance[iExtremes], 75)
MeanExtreme=np.nanmean(Precipitation[iExtremes][Distance[iExtremes] <= Extreme75P])
MeanAll75P=np.nanmean(Precipitation[Distance <= Extreme75P])
RelDiff=((MeanAll75P-MeanExtreme)/MeanExtreme)*-1
return RelDiff
# Mean Rank Ratio
# Difference between ranks of extreme cases according to their Eucledian Distances
# and average rank in dataset | zero is best; one is no scill; 2 is perfect negative skill
def MRR(Distance, iExtremes):
RankedDistances=np.argsort(Distance)
ExtremeRanks=np.mean(np.array([np.where(RankedDistances == iExtremes[ii])[0][0] for ii in range(len(iExtremes))]))-(len(iExtremes)/2.-0.5)
WorstRanks=len(Distance)-1
MeanRankRatio=(ExtremeRanks/(WorstRanks-len(iExtremes)+1))*2
return MeanRankRatio
# Get extreme PR days with minimum days appart
def ExtremeDays(Record, ExtremeNr, DistanceDD):
SortedDates=np.argsort(Record)[:][::-1]
rgiExtremePR=np.zeros((ExtremeNr)); rgiExtremePR[:]=np.nan
ii=1
jj=1
rgiExtremePR[0]=SortedDates[0]
while ii < ExtremeNr:
if np.nanmin(np.abs(rgiExtremePR - SortedDates[jj])) < DistanceDD:
jj=jj+1
else:
rgiExtremePR[ii]=SortedDates[jj]
jj=jj+1
ii=ii+1
return rgiExtremePR.astype('int')
# ===================================================================
def read_shapefile(sf):
"""
Read a shapefile into a Pandas dataframe with a 'coords'
column holding the geometry information. This uses the pyshp
package
"""
fields = [x[0] for x in sf.fields][1:]
records = sf.records()
shps = [s.points for s in sf.shapes()]
df = pd.DataFrame(columns=fields, data=records)
df = df.assign(coords=shps)
return df
# ===================================================================
def ReadCESMday(DaySel,
Exp,
iWest,
iEast,
iSouth,
iNort,
rgrTimeCESMFull):
"""
Read in a single day within a region from one
CESM large ensemble simulation
All variables nescessary for a synopic mapplot are read in
"""
rgsWTvars=['Z500', 'U850', 'V850', 'TMQ',]
VarsFullName= ['Z500', 'U850', 'V850', 'PW']
rgsWTfolders=['/glade/collections/cdg/data/cesmLE/CESM-CAM5-BGC-LE/atm/proc/tseries/daily/Z500/',\
'/glade/collections/cdg/data/cesmLE/CESM-CAM5-BGC-LE/atm/proc/tseries/daily/U850/',\
'/glade/collections/cdg/data/cesmLE/CESM-CAM5-BGC-LE/atm/proc/tseries/daily/V850/',\
'/glade/collections/cdg/data/cesmLE/CESM-CAM5-BGC-LE/atm/proc/tseries/daily/TMQ/']
s20Cname='b.e11.B20TRC5CNBDRD.f09_g16.'
s21Cname='b.e11.BRCP85C5CNBDRD.f09_g16.'
# start reading in the CESM data
iRegionPlus=20 # grid cell added around shape rectangle
ncid=Dataset('/glade/collections/cdg/data/cesmLE/CESM-CAM5-BGC-LE/atm/proc/tseries/daily/PSL/b.e11.B20TRC5CNBDRD.f09_g16.001.cam.h1.PSL.18500101-20051231.nc', mode='r')
rgrLonWT1D=np.squeeze(ncid.variables['lon'][:])
rgrLatWT1D=np.squeeze(ncid.variables['lat'][:])
ncid.close()
rgrLonS=rgrLonWT1D[iWest-iRegionPlus:iEast+iRegionPlus]
rgrLatS=rgrLatWT1D[iSouth-iRegionPlus:iNort+iRegionPlus]
# Read the variables
DataAll=np.zeros((len(rgrLatS), len(rgrLonS), len(rgsWTvars))); DataAll[:]=np.nan
for va in range(len(rgsWTvars)):
if DaySel.year < 2006:
if Exp == '001':
rgrTimeCESM=pd.date_range(datetime.date(1850, 1, 1), end=datetime.date(2005, 12, 31), freq='d')
else:
rgrTimeCESM=pd.date_range(datetime.date(1920, 1, 1), end=datetime.date(2005, 12, 31), freq='d')
Cfiles=glob.glob(rgsWTfolders[va]+'/'+s20Cname+Exp+'*'+rgsWTvars[va]+'*')[0]
if DaySel.year >= 2006:
if int(Exp) >= 34:
rgrTimeCESM=pd.date_range(datetime.date(2006, 1, 1), end=datetime.date(2100, 12, 31), freq='d')
Cfiles=glob.glob(rgsWTfolders[va]+'/'+s21Cname+Exp+'*'+rgsWTvars[va]+'*')[0]
elif DaySel.year < 2080:
rgrTimeCESM=pd.date_range(datetime.date(2006, 1, 1), end=datetime.date(2080, 12, 31), freq='d')
try:
Cfiles=np.sort(glob.glob(rgsWTfolders[va]+'/'+s21Cname+Exp+'*'+rgsWTvars[va]+'*'))[0]
except:
stop()
elif DaySel.year > 2081:
rgrTimeCESM=pd.date_range(datetime.date(2081, 1, 1), end=datetime.date(2100, 12, 31), freq='d')
Cfiles=np.sort(glob.glob(rgsWTfolders[va]+'/'+s21Cname+Exp+'*'+rgsWTvars[va]+'*'))[1]
rgiNonLeap=np.where((rgrTimeCESM.month != 2) | (rgrTimeCESM.day != 29))[0]
rgrTimeCESM=rgrTimeCESM[rgiNonLeap]
iDDselect=np.where(rgrTimeCESM == DaySel)[0][0]
try:
ncid=Dataset(Cfiles, mode='r')
DataAll[:,:, va]=np.squeeze(ncid.variables[rgsWTvars[va]][iDDselect, iSouth-iRegionPlus:iNort+iRegionPlus, iWest-iRegionPlus:iEast+iRegionPlus])
ncid.close()
except:
stop()
return DataAll, rgrLonS, rgrLatS
# ===================================================================
def DetrentData(DATA,
TIME,
YYYY_WINDOW):
# remove YYYY_WINDOW anomaly from each year in the time series
# This removes the thermodynamic effects while maintaining the dynamics effects
rgrDataDetrended=np.copy(DATA)
iYearsFull=np.unique(TIME.year)
Y_half=int(YYYY_WINDOW)
for yy in range(len(iYearsFull)):
if yy < Y_half:
yyStart=iYearsFull[0]
else:
yyStart=iYearsFull[yy-Y_half]
if yy > len(iYearsFull)-(Y_half+1):
yyStop=iYearsFull[len(iYearsFull)-1]
else:
yyStop=iYearsFull[yy+Y_half]
iTimePeriodAct=((TIME.year >= yyStart) & (TIME.year <= yyStop))
rgrDataDetrended[iTimePeriodAct,:,:,:]=DATA[iTimePeriodAct,:,:,:]-np.mean(DATA[iTimePeriodAct,:,:,:], axis=(0, 1, 2))[None, None, None,:]
rgrDataDetrended=rgrDataDetrended+np.mean(DATA, axis=0)[None,:,:,:]
return rgrDataDetrended
# ===================================================================
def SearchOptimum_XWT(PlotFile,
sPlotDir,
SkillScores_All,
GlobalMinimum1,
GlobalMinimum2,
Optimum,
VariableIndices,
Dimensions,
Metrics,
VarsFullName,
ss,
rgrNrOfExtremes,
WT_Domains,
Annual_Cycle,
SpatialSmoothing):
# provide visual guidance for how large the impacts of changing
# setup variables are to find an optimal configuration
fig = plt.figure(figsize=(18, 13))
plt.rcParams.update({'font.size': 14})
rgsLableABC=list(string.ascii_lowercase)+list(string.ascii_uppercase)
YY=[0, 0, 0, 1, 1]
XX=[0, 1, 2, 0, 1]
Shape=SkillScores_All.shape
SkillScoreColors=['#1f78b4', '#e31a1c', '#ff7f00']
# ---------------------------
# Plot showing the time that extremes occured
gs1 = gridspec.GridSpec(ncols=3, nrows=2, figure=fig)
gs1.update(left=0.08, right=0.98,
bottom=0.10, top=0.96,
wspace=0.25, hspace=0.25)
for se in range(len(Dimensions)-2):
ax = fig.add_subplot(gs1[YY[se], XX[se]])
if Dimensions[se] == 'Variables':
SS1_Data=SkillScores_All[:, GlobalMinimum1[1][0], GlobalMinimum1[2][0], GlobalMinimum1[3][0], GlobalMinimum1[4][0], 0,:]
Xaxis1=[VarsFullName[int(VariableIndices[va, GlobalMinimum1[1][0], GlobalMinimum1[2][0], GlobalMinimum1[3][0], GlobalMinimum1[4][0], ss])] for va in range(Shape[0])]
SS2_Data=SkillScores_All[:, GlobalMinimum2[1][0], GlobalMinimum2[2][0], GlobalMinimum2[3][0], GlobalMinimum2[4][0], 1,:]
Xaxis2=[VarsFullName[int(VariableIndices[va, GlobalMinimum2[1][0], GlobalMinimum2[2][0], GlobalMinimum2[3][0], GlobalMinimum2[4][0], ss])] for va in range(Shape[0])]
Xlabel='variable'
Xaxis=[Xaxis1[ii]+'/'+Xaxis2[ii] for ii in range(len(Xaxis1))]
XOptimum=Optimum[0][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
if Dimensions[se] == 'Extreme Nr.':
SS1_Data=SkillScores_All[GlobalMinimum1[0][0],:, GlobalMinimum1[2][0], GlobalMinimum1[3][0], GlobalMinimum1[4][0], 0,:]
SS2_Data=SkillScores_All[GlobalMinimum2[0][0],:, GlobalMinimum2[2][0], GlobalMinimum2[3][0], GlobalMinimum2[4][0], 1,:]
Xlabel='Nr. of extreme days'
XOptimum=Optimum[1][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
Xaxis=rgrNrOfExtremes
if Dimensions[se] == 'Domain Size':
SS1_Data=SkillScores_All[GlobalMinimum1[0][0], GlobalMinimum1[1][0],:, GlobalMinimum1[3][0], GlobalMinimum1[4][0], 0,:]
SS2_Data=SkillScores_All[GlobalMinimum2[0][0], GlobalMinimum2[1][0],:, GlobalMinimum2[3][0], GlobalMinimum2[4][0], 1,:]
Xlabel='Domain size'
XOptimum=Optimum[2][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
Xaxis=WT_Domains
if Dimensions[se] == 'Annual Cycle':
SS1_Data=SkillScores_All[GlobalMinimum1[0][0], GlobalMinimum1[1][0], GlobalMinimum1[2][0],:, GlobalMinimum1[4][0], 0,:]
SS2_Data=SkillScores_All[GlobalMinimum2[0][0], GlobalMinimum2[1][0], GlobalMinimum2[2][0],:, GlobalMinimum2[4][0], 1,:]
Xlabel='Annual cycle removed'
XOptimum=Optimum[3][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
Xaxis=Annual_Cycle
if Dimensions[se] == 'Smoothing':
SS1_Data=SkillScores_All[GlobalMinimum1[0][0], GlobalMinimum1[1][0], GlobalMinimum1[2][0], GlobalMinimum1[3][0],:, 0,:]
SS2_Data=SkillScores_All[GlobalMinimum2[0][0], GlobalMinimum2[1][0], GlobalMinimum2[2][0], GlobalMinimum2[3][0],:, 1,:]
Xlabel='Spatial smoothing'
XOptimum=Optimum[4][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
Xaxis=SpatialSmoothing
# Start plotting the scill scores dependency on setup
for sc in range(len(Metrics)):
plt.plot(list(range(len(Xaxis))), SS1_Data[:, sc], c=SkillScoreColors[sc], label=Metrics[sc], ls='-')
plt.plot(list(range(len(Xaxis))), SS2_Data[:, sc], c=SkillScoreColors[sc], label=Metrics[sc], ls='--')
plt.plot(XOptimum, YOptimum[sc], marker='o', c=SkillScoreColors[sc], label='Optimum', markersize=10)
plt.plot(list(range(len(Xaxis))), np.mean(SS1_Data[:,:], axis=1), c='k', label='Mean', ls='-', lw=3)
plt.plot(list(range(len(Xaxis))), np.mean(SS2_Data[:,:], axis=1), c='k', label='Mean', ls='--', lw=3)
plt.plot(XOptimum, np.mean(YOptimum), marker='o', c='k', label='Optimum', markersize=10)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_ylabel('Skill Score []')
ax.set_xlabel(Xlabel)
plt.xticks(np.arange(0, len(Xaxis), 1.0))
ax.set_xticklabels(Xaxis, rotation=45)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(list(zip(labels, handles)))
plt.legend(list(by_label.values()), list(by_label.keys()))
sPlotFile=sPlotDir
# sPlotName= 'BottomUp-'+str(rgrClustersFin[1].max()+1)+'WT_precipitation.pdf'
sPlotName= PlotFile
if os.path.isdir(sPlotFile) != 1:
subprocess.call(["mkdir", "-p", sPlotFile])
print(' Plot map to: '+sPlotFile+sPlotName)
fig.savefig(sPlotFile+sPlotName)
# ===================================================================
def PlotOptimum_XWT(PlotFile,
sPlotDir,
SkillScores_All,
GlobalMinimum1,
GlobalMinimum2,
Optimum,
VariableIndices,
Dimensions,
Metrics,
VarsFullName,
ss,
rgrNrOfExtremes,
WT_Domains,
Annual_Cycle,
SpatialSmoothing):
# provide visual guidance for how large the impacts of changing
# setup variables are to find an optimal configuration
fig = plt.figure(figsize=(18, 13))
plt.rcParams.update({'font.size': 14})
rgsLableABC=list(string.ascii_lowercase)+list(string.ascii_uppercase)
YY=[0, 0, 0, 1, 1]
XX=[0, 1, 2, 0, 1]
Shape=SkillScores_All.shape
SkillScoreColors=['#1f78b4', '#e31a1c', '#ff7f00', '#418600', '#8465de']
# ---------------------------
# Plot showing the time that extremes occured
gs1 = gridspec.GridSpec(ncols=3, nrows=2, figure=fig)
gs1.update(left=0.08, right=0.98,
bottom=0.10, top=0.96,
wspace=0.25, hspace=0.25)
for se in range(len(Dimensions)-2):
ax = fig.add_subplot(gs1[YY[se], XX[se]])
if Dimensions[se] == 'Variables':
SS1_Data=SkillScores_All[:, GlobalMinimum1[1][0], GlobalMinimum1[2][0], GlobalMinimum1[3][0], GlobalMinimum1[4][0], 0,:]
Xaxis1=[VarsFullName[int(VariableIndices[va, GlobalMinimum1[1][0], GlobalMinimum1[2][0], GlobalMinimum1[3][0], GlobalMinimum1[4][0], ss])] for va in range(Shape[0])]
SS2_Data=SkillScores_All[:, GlobalMinimum2[1][0], GlobalMinimum2[2][0], GlobalMinimum2[3][0], GlobalMinimum2[4][0], 1,:]
Xaxis2=[VarsFullName[int(VariableIndices[va, GlobalMinimum2[1][0], GlobalMinimum2[2][0], GlobalMinimum2[3][0], GlobalMinimum2[4][0], ss])] for va in range(Shape[0])]
Xlabel='variable'
Xaxis=[Xaxis1[ii]+'/'+Xaxis2[ii] for ii in range(len(Xaxis1))]
XOptimum=Optimum[0][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
if Dimensions[se] == 'Extreme Nr.':
SS1_Data=SkillScores_All[GlobalMinimum1[0][0],:, GlobalMinimum1[2][0], GlobalMinimum1[3][0], GlobalMinimum1[4][0], 0,:]
SS2_Data=SkillScores_All[GlobalMinimum2[0][0],:, GlobalMinimum2[2][0], GlobalMinimum2[3][0], GlobalMinimum2[4][0], 1,:]
Xlabel='Nr. of extreme days'
XOptimum=Optimum[1][0]
YOptimum=SkillScores_All[Optimum[0][0], Optimum[1][0], Optimum[2][0], Optimum[3][0], Optimum[4][0], ss,:]
Xaxis=rgrNrOfExtremes
# Start plotting the scill scores dependency on setup
for sc in range(len(Metrics)):
plt.plot(list(range(len(Xaxis))), SS1_Data[:, sc], c=SkillScoreColors[sc], label=Metrics[sc], ls='-')
plt.plot(list(range(len(Xaxis))), SS2_Data[:, sc], c=SkillScoreColors[sc], label=Metrics[sc], ls='--')
plt.plot(XOptimum, YOptimum[sc], marker='o', c=SkillScoreColors[sc], label='Optimum', markersize=10)
plt.plot(list(range(len(Xaxis))), np.mean(SS1_Data[:,:], axis=1), c='k', label='Mean', ls='-', lw=3)
plt.plot(list(range(len(Xaxis))), np.mean(SS2_Data[:,:], axis=1), c='k', label='Mean', ls='--', lw=3)
plt.plot(XOptimum, np.mean(YOptimum), marker='o', c='k', label='Optimum', markersize=10)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_ylabel('Skill Score []')
ax.set_xlabel(Xlabel)
plt.xticks(np.arange(0, len(Xaxis), 1.0))
ax.set_xticklabels(Xaxis, rotation=45)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(list(zip(labels, handles)))
plt.legend(list(by_label.values()), list(by_label.keys()))
sPlotFile=sPlotDir
# sPlotName= 'BottomUp-'+str(rgrClustersFin[1].max()+1)+'WT_precipitation.pdf'
sPlotName= PlotFile
if os.path.isdir(sPlotFile) != 1:
subprocess.call(["mkdir", "-p", sPlotFile])
print(' Plot map to: '+sPlotFile+sPlotName)
fig.savefig(sPlotFile+sPlotName)
# ===================================================================
def XWT(training_predictors, # predictor variables that are used to train the model
testing_predictors, # predictor variables that are used to evaluate the model
training_predictant, # predictent variable that is uesed to train the model
testing_predictant, # predictent variable that is uesed to evaluate the model
training_time, # daily time vector for the training dataset
testing_time, # daily time vector for the testing datast
extreme_nr, # Nr. of extreme events considered
smoothing_radius, # smoothing radius applied to predictor fields
ClusterMeth='HandK', # current options are ['HandK','hdbscan']
ClusterBreakup=0): # break up clusters that have more than 50% of events and if extreme Nr. > 10
# OPTIONAL INPUTS
MinDistDD=7 # min. distance between two extremes in days
#MinDistDD=0 # KRF: try zero days, min. distance between two extremes in days
RelAnnom=1 # 1 means that the clustering is performed based on reltaive anomalies
RemoveAnnualCycl=1 # 1 means that the annual cycle will be removed before the clustering
NormalizeData=1 # 1 means that the each record will be normalized according to its spatial characteristics. This automatically removes the annual cycle.
sPlotDir=None
YYYY_stamp=None
from Functions_Extreme_WTs import ExtremeDays
rgiExtrTrain=ExtremeDays(training_predictant,extreme_nr,MinDistDD)
ExtrTrainDays=training_time[rgiExtrTrain]
rgiExtrEval=np.argsort(testing_predictant)[-extreme_nr:]
# rgiExtrEval=ExtremeDays(testing_predictant,extreme_nr,MinDistDD)
ExtrEvalDays=testing_time[rgiExtrEval]
from Functions_Extreme_WTs import PreprocessWTdata
training_predictors=PreprocessWTdata(training_predictors, # WT data [time,lat,lon,var]
RelAnnom=RelAnnom, # calculate relative anomalies [1-yes; 0-no]
SmoothSigma=smoothing_radius, # Smoothing stddev (Gaussian smoothing)
RemoveAnnualCycl=RemoveAnnualCycl, # remove annual cycle [1-yes; 0-no]
NormalizeData=NormalizeData) # normalize data [1-yes; 0-no]
from Functions_Extreme_WTs import GetExtremeDays
rgrWTdata=GetExtremeDays(training_predictors,
training_time,
ExtrTrainDays)
# ################################################
# #### Run Hirarchical clustering
from Functions_Extreme_WTs import ClusterAnalysis
rgrClustersFin=ClusterAnalysis(rgrWTdata,
sPlotDir,
extreme_nr,
YYYY_stamp,
Plot=0,
ClusterMeth=ClusterMeth,
ClusterBreakup=ClusterBreakup)
# ################################################
# #### Prepare evaluation data
DailyVarsEvalNorm=PreprocessWTdata(testing_predictors, # WT data [time,lat,lon,var]
RelAnnom=RelAnnom, # calculate relative anomalies [1-yes; 0-no]
SmoothSigma=smoothing_radius, # Smoothing stddev (Gaussian smoothing)
RemoveAnnualCycl=RemoveAnnualCycl, # remove annual cycle [1-yes; 0-no]
NormalizeData=NormalizeData) # normalize data [1-yes; 0-no]
# ################################################
# ###### EUCLEDIAN DISTANCES
from Functions_Extreme_WTs import EucledianDistance
EucledianDist, Correlation =EucledianDistance(DailyVarsEvalNorm,
rgrClustersFin)
from Functions_Extreme_WTs import Scatter_ED_PR
MinDistance=np.min(EucledianDist, axis=1)
ClosestWT=np.argmin(EucledianDist, axis=1)
MaxCorr=np.max(Correlation, axis=1)
# Scatter_ED_PR(MinDistance,
# ClosestWT,
# Peval,
# rgrNrOfExtremes,
# PlotLoc=sPlotDir,
# PlotName='Scatter_'+sRegion+'_NrExt-'+str(rgrNrOfExtremes)+'_Smooth-'+str(SpatialSmoothing)+'_AnnCy-'+Annual_Cycle+'_'+VarsJoint+'_'+sMonths+'_'+Samples[ss]+'.pdf')
# Calculate the skill scores
from Functions_Extreme_WTs import MRR, MRD, perkins_skill
# Perkins Skill Score
try:
grPSS=perkins_skill(MinDistance,MinDistance[rgiExtrEval], 0.5)
except:
stop()
# Mean relative difference
grMRD=MRD(MinDistance,testing_predictant,rgiExtrEval)
# Mean Rank Ratio
grMRR=MRR(MinDistance,rgiExtrEval)
# % of days excluded
grExluded=(1-np.sum(MinDistance < np.nanpercentile(MinDistance[rgiExtrEval],75))/float(len(MinDistance)))*100.
# calculate the AUC
from sklearn.metrics import roc_auc_score
testy=(testing_predictant >= np.sort(testing_predictant)[-extreme_nr])
probs=(MinDistance-np.min(MinDistance)); probs=np.abs((probs/probs.max())-1)
try:
auc = roc_auc_score(testy, probs)
except:
auc=np.nan
# Calculate the Average precision-recall score
from sklearn.metrics import average_precision_score
from sklearn import svm, datasets
try:
average_precision = average_precision_score(testy, probs)
except:
average_precision = np.nan
# print("--- Summary of performance ---")
# print(" PSS: "+str(np.round(grPSS,2)))
# print(" MRD: "+str(np.round(grMRD,2)))
# print(" MRR: "+str(np.round(grMRR,2)))
# print(" Excluded: "+str(np.round(grExluded,2)))
# print(" AUC: "+str(np.round(auc,2)))
# print(" APR: "+str(np.round(average_precision,2)))
# print("------------------------------")
XWT_output={'grClustersFin':rgrClustersFin,
'grEucledianDist':MinDistance,
'EucledianDistAllWTs':EucledianDist,
'grCorrelatio':MaxCorr,
'grCorrelatioAllWTs':Correlation,
'grPSS':grPSS,
'grMRD':grMRD,
'grMRR':grMRR,
'APR':average_precision,
'AUC':auc,
'PEX':grExluded,
'grExluded':grExluded}
return XWT_output