-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathranky.py
44 lines (31 loc) · 1.48 KB
/
ranky.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
#function ranks an obs dataset against an ensemble. ranks overwhich values are tied and randomly generated
import numpy as np
from scipy.stats import rankdata
def rankz(obs,ensemble,mask):
''' Parameters
----------
obs : array of observations
ensemble : array of ensemble, with the first dimension being the
ensemble member and the remaining dimensions being identical to obs
mask : boolean mask of shape of obs, with zero/false being where grid cells are masked.
Returns
-------
histogram data for ensemble.shape[0] + 1 bins.
The first dimension of this array is the height of
each histogram bar, the second dimension is the histogram bins.
'''
mask=np.bool_(mask)
obs=obs[mask]
ensemble=ensemble[:,mask]
combined=np.vstack((obs[np.newaxis],ensemble))
# print('computing ranks')
ranks=np.apply_along_axis(lambda x: rankdata(x,method='min'),0,combined)
# print('computing ties')
ties=np.sum(ranks[0]==ranks[1:], axis=0)
ranks=ranks[0]
tie=np.unique(ties)
for i in range(1,len(tie)):
index=ranks[ties==tie[i]]
# print('randomizing tied ranks for ' + str(len(index)) + ' instances where there is ' + str(tie[i]) + ' tie/s. ' + str(len(tie)-i-1) + ' more to go')
ranks[ties==tie[i]]=[np.random.randint(index[j],index[j]+tie[i]+1,tie[i])[0] for j in range(len(index))]
return np.histogram(ranks, bins=np.linspace(0.5, combined.shape[0]+0.5, combined.shape[0]+1))