/
recursetest.py
397 lines (360 loc) · 17.9 KB
/
recursetest.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
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 11 17:36:03 2014
@author: s4493222
"""
#!/usr/bin/env python
#support for the
import numpy as np
import os
import sys
import bivariate_multi as bivariate
import lowess
from window40test_lowess import dataDict
import regress
import matplotlib.pyplot as plt
SVNRevision="$Revision: 297 $"
bcp85keys=['CCSM4r4i1p1', 'FGOALS-s2r2i1p1', 'Had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r2i1p1', 'CESM1-CAM5r3i1p1', 'FGOALS-g2r1i1p1', 'CCSM4r1i1p1', 'bcc-csm1-1-mr1i1p1', 'CNRM-CM5r6i1p1', 'IPSL-CM5A-LRr1i1p1', 'CanESM2r1i1p1', 'CSIRO-Mk3-6-0r5i1p1', 'IPSL-CM5A-MRr1i1p1', 'MPI-ESM-MRr1i1p1', 'CNRM-CM5r10i1p1', 'CESM1-CAM5r1i1p1', 'CSIRO-Mk3-6-0r7i1p1', 'CSIRO-Mk3-6-0r10i1p1', 'CanESM2r4i1p1', 'CanESM2r3i1p1', 'HadGEM2-ESr3i1p1', 'IPSL-CM5A-LRr4i1p1', 'CanESM2r2i1p1', 'IPSL-CM5A-LRr2i1p1', 'CCSM4r2i1p1', 'NorESM1-MEr1i1p1', 'CMCC-CMr1i1p1', 'BNU-ESMr1i1p1']
class recurseTestException(Exception):
def __str__(self):
return "recurseTestException "+self.message
class recurse(object):
def __init__(self, ys, xs, years, model, pr=0.01, smooth=False, anom=False, onethreshold=True, trim=1, debug=False, ConstSxy=True, withshifts=False):
try:
#print "Pr size", pr, size
self.__size=len(xs)
#print self.__size, type(self.__size)
self.__threshold=bivariate.critTi(pr, self.__size)
self.__breakpoints=np.zeros((self.__size,))
self.__breakyears = {}
except Exception as e:
print bivariate.ExceptionInfo()
print "__init__",str(e)
raise recurseTestException("__init__:"+str(e))
if smooth:
if anom:
#print "window smooth anom"
txs, tys, td1, td2 = lowess.R().lowess(np.array(xs),np.array(ys), f=1./4., iter=1)
self.__ys = np.array(ys)-np.array(tys)
self.__xs=np.copy(xs)
else:
#print "window smooth not anom"
txs, tys, td1, td2 = lowess.R().lowess(np.array(xs),np.array(ys), f=1./4., iter=1)
self.__ys = np.array(ys)
self.__xs=np.array(tys)+np.array(xs)
else:
#print "window not smooth"
self.__ys=np.array(ys)
self.__xs=np.copy(xs)
#print "window call bivariate"
try:
#self.__bv=bivariate.bivariate(self.__ys, self.__xs, critical=self.__threshold, anomalise=False,constantsxy=ConstSxy)
self.__bv=bivariate.bivariate(self.__ys, self.__xs, critical=None, pr=pr, anomalise=False,constantsxy=ConstSxy)
if withshifts:
ap_MaxTis, ap_MaxIndexes, (ap_lows, ap_highs), ap_shifts = self.__bv.allPoints(pr, withshifts=True)
else:
ap_MaxTis, ap_MaxIndexes, (ap_lows, ap_highs)= self.__bv.allPoints(pr, withshifts=False)
#print "BV.BIVARIATE -> ", len(ap_MaxTis), len(ap_MaxIndexes), len(ap_lows), len(ap_highs)
except Exception as e:
print bivariate.ExceptionInfo()
raise recurseTestException("recurse.__init__ call to bivariate:"+str(e))
#print ap_MaxTis, ap_MaxIndexes, (ap_lows, ap_highs)
if trim == 1:
#==============================================================================
## This mode implements a trimming mode whereby breakpoints tested by
## bracketing them and accepting them if there ids a breakpoint in the interval
## The alternative (mode 2) is to replace them with the new breakpoint
#==============================================================================
try:
stepn = 0
if debug: print "trimming"
trimmed = True
while trimmed:
trimmed = False
for mi in range(len(ap_MaxIndexes)):
if debug: print "considering mi"
if not trimmed:
stepn = 1
self.__bv.reinit(ap_lows[int(mi)], ap_highs[int(mi)]+1, pr)
stepn=2
#print self.__bv.maxnTi() , ap_lows[int(mi)], ap_MaxIndexes[int(mi)], ap_highs[int(mi)], bivariate.critTi(pr, 1 + ap_highs[int(mi)]-ap_lows[int(mi)])
stepn = 3
if self.__bv.maxnTi() < bivariate.critTi(pr, 1 + ap_highs[int(mi)]-ap_lows[int(mi)]):
stepn = 4
trimmed = True
if debug: print "trimmer removed ", mi, ap_MaxIndexes[int(mi)], " between ", ap_lows[int(mi)], ap_highs[int(mi)]
stepn = 5
#print mi, len(ap_MaxTis), len(ap_MaxIndexes), len(ap_lows), len(ap_highs)
#print ap_MaxTis, " becomes ",
pop1 = ap_MaxTis.pop(int(mi))
#print ap_MaxTis
pop2 = ap_MaxIndexes.pop(int(mi))
pop3 = ap_lows.pop(int(mi))
pop4 = ap_highs.pop(int(mi))
with open("removals.txt","a") as rmf:
rmf.write("from %s trimmer removed element %s (Ti:%s ) %s between %s and %s crit=%s \n" % (str(model), str(mi), str(pop1), str(pop2), str(pop3), str(pop4), str( bivariate.critTi(pr, 1 + ap_highs[min(len(ap_highs)-1,mi)]-ap_lows[min(len(ap_lows)-1,mi)]))))
except Exception as e:
raise recurseTestException("at trim state "+str(stepn)+" in __init__:"+str(e))
if trim == 2:
#==============================================================================
## This mode implements a trimming mode whereby breakpoints tested by
## bracketing them and accepting them if there ids a breakpoint in the interval
## The alternative (mode 2) is to replace them with the new breakpoint
#==============================================================================
try:
stepn = 0
if debug: print "trimming mode 2"
trimmed = True
while trimmed:
trimmed = False
for mi in range(len(ap_MaxIndexes)):
if debug: print "considering mi"
if not trimmed:
stepn = 1
self.__bv.reinit(ap_lows[int(mi)], ap_highs[int(mi)]+1, pr)
stepn=2
print self.__bv.maxnTi() , ap_lows[int(mi)], ap_MaxIndexes[int(mi)], ap_highs[int(mi)], bivariate.critTi(pr, 1 + ap_highs[int(mi)]-ap_lows[int(mi)])
if self.__bv.maxnTi() < bivariate.critTi(pr, 1 + ap_highs[int(mi)]-ap_lows[int(mi)]):
trimmed = True
if debug: print "trimmer (mode 2) removed ", mi, ap_MaxIndexes[int(mi)], " between ", ap_lows[int(mi)], ap_highs[int(mi)]
pop1 = ap_MaxTis.pop(int(mi))
pop2 = ap_MaxIndexes.pop(int(mi))
pop3 = ap_lows.pop(int(mi))
pop4 = ap_highs.pop(int(mi))
with open("removals.txt","a") as rmf:
rmf.write("from %s trimmer (mode 2) removed element %s (Ti:%s ) %s between %s and %s crit=%s \n" % (str(model), str(mi), str(pop1), str(pop2), str(pop3), str(pop4), str( bivariate.critTi(pr, 1 + ap_highs[min(len(ap_highs)-1,mi)]-ap_lows[min(len(ap_lows)-1,mi)]))))
elif self.__bv.maxIndexnTi() != ap_MaxIndexes[int(mi)]:
trimmed = True
pop1 = ap_MaxTis[int(mi)]
pop2 = ap_MaxIndexes[int(mi)]
pop3 = ap_lows[int(mi)]
pop4 = ap_highs[int(mi)]
ap_MaxTis[int(mi)] = self.__bv.maxnTi()
ap_MaxIndexes[int(mi)] = self.__bv.maxIndexnTi()
with open("removals.txt","a") as rmf:
rmf.write("from %s trimmer (mode 2) substituted element %s (Ti:%s ) %s with %s between %s and %s crit=%s \n" % (str(model), str(mi), str(ap_MaxIndexes[int(mi)]), str(pop1), str(pop2), str(pop3), str(pop4), str( bivariate.critTi(pr, 1 + ap_highs[int(mi)]-ap_lows[int(mi)]))))
except Exception as e:
raise recurseTestException("at mode 2 trim state "+str(stepn)+" in __init__:"+str(e))
try:
stepn = 0
for mi in range(len(ap_MaxIndexes)):
#print mi
stepn = 1
if ((onethreshold and (ap_MaxTis[int(mi)] >= self.__threshold)) or
(not onethreshold and (ap_MaxTis[int(mi)] >=
bivariate.critTi(pr, 1 + ap_highs[int(mi)]-ap_lows[int(mi)])))):
self.__breakpoints[int(ap_MaxIndexes[int(mi)])] += 1
stepn = 2
self.__breakyears[years[int(ap_MaxIndexes[int(mi)])]] = None
stepn = 3
if withshifts:
self.__breakyears[years[int(ap_MaxIndexes[mi])]] = (ap_MaxTis[mi], years[int(ap_lows[mi])], years[int(ap_highs[mi])-1], ap_shifts[mi])
else:
self.__breakyears[years[int(ap_MaxIndexes[mi])]] = (ap_MaxTis[mi], years[int(ap_lows[mi])], years[int(ap_highs[mi])-1])
except Exception as e:
print "Exception -----------------------------------------------------------"
try:
print "mi",mi
#print "years",years
print "ap_MaxIndexes",ap_MaxIndexes
print "ap_MaxTis",ap_MaxTis
print "ap_lows",ap_lows
print "ap_highs",ap_highs
print "self.__breakyears",self.__breakyears
print "years[int(ap_MaxIndexes[int(mi)])]",years[int(ap_MaxIndexes[int(mi)])]
except:
pass
print bivariate.ExceptionInfo()
raise recurseTestException("Reporting loop of __init__: "+str(stepn)+" "+str(e))
def breakpoints(self):
return self.__breakpoints
def breakyears(self):
return self.__breakyears
def plot(self, x, y, years, title=0.0, save=True):
ys = self.__ys
xs = self.__xs
print x, y, years
#print self.__breakyears.keys()
breaks=[years[0]]
breaks.extend(np.sort(self.__breakyears.keys()))
breaks.extend([years[-1]])
#print breaks
fig, ax = plt.subplots()
if title != 0.0:
ax.set_title(sys.argv[2]+ " "+title)
pl = []
#print "LOWESS",lowess(xs,ys)
#sys.exit()
#print "HERE"
pl.append(ax.plot(years, y))
for i in range(len(breaks)-1):
try:
pt = 0
sYs = np.array(y[breaks[i]-breaks[0]:breaks[i+1]+1-breaks[0]])
sXs = np.array(years[breaks[i]-breaks[0]:breaks[i+1]+1-breaks[0]])
#print breaks[i]-breaks[0], breaks[i+1]+1-breaks[0], sXs, sYs
#print "JIM", len(xs), len(ys)
pt = 1
stats=regress.analysed_regress(sYs,sXs)
pt = 2
yhat, resid=regress.residuals(sYs, sXs, stats)
pt = 3
pl.append(ax.plot(sXs, yhat, '-'))
except Exception as e:
print "Exception at "+str(e),pt
if not save:
plt.show()
else:
plt.savefig(sys.argv[2]+ " "+title+'.png')
return breaks
#JHR 7-5-2014 support for selection of breakpoints given an accelerating series, to avoid over reaching
#def validslice(self, MaxIndexes):
def process(dataD, gcm, filename, smooth=True, anom=True):
ys=np.array(dataD[gcm], dtype=np.float32)
if os.path.exists(filename):
os.remove(filename)
for j in range(100):
xs=np.array([np.random.randn() for i in range(len(ys))], np.float32)
#print "process window"
bp=recurse(ys, xs, gcm, smooth=smooth, anom=anom).breakpoints()
#print "process segment"
with open(filename, "a") as outf:
for line in bp:
outf.write(str(line)+"\n")
if __name__ == "__main__":
#itewrative
tasD={}
if len(sys.argv) <=1:
sys.argv.append(os.environ["HOMEPATH"]+"\\Documents\\abrupt\\CMIP5_breakpoints\\historical_rcp85qccceGW.txt")
#sys.argv.append( "MIROC5r1i1p1")# "ACCESS1-0r1i1p1")
fn = sys.argv[1]
all85=dataDict(fn)
#print all85.years()
sys.argv.append("MIROC5r3i1p1")
if len(sys.argv) <=2:
for model in all85.models():
if not os.path.exists(model+'.summary.txt'):
sys.argv.append(model)
#sys.argv.append( "MIROC5r3i1p1")# "ACCESS1-0r1i1p1")
#sys.argv.append( "MIROC5r2i1p1")# "ACCESS1-0r1i1p1")
#sys.argv.append( "MIROC5r1i1p1")# "ACCESS1-0r1i1p1")
#sys.argv.append( "ACCESS1-0r1i1p1")
years = all85.years()[sys.argv[2]]
print "YEARS",sys.argv[1], sys.argv[2], years
tasD = all85[sys.argv[2]]
model = sys.argv[2]
ys=np.array(tasD, dtype=np.float32)
byssummary = []
if os.path.exists(model+'.years.txt'): os.remove(model+'.years.txt')
if os.path.exists(model+'.bp.txt'): os.remove(model+'.bp.txt')
for j in range(100): ###############################################################
xs=np.array([np.random.randn() for i in range(len(ys))], np.float32)
try:
stepn=0
bpv=recurse(ys, xs, years, "%s_run_%d" % (model, j), 0.01, smooth=False, anom=False, debug=True, trim=1)
bp = bpv.breakpoints()
bys = bpv.breakyears()
stepn=1
byfn=model+'.years.txt'
bpfn=model+'.bp.txt'
with open(byfn, "a") as byf:
for k in np.sort(bys.keys()):
print k, bys[k]
byf.write("%s %s\n" % (str(k), str(bys[k])))
with open(bpfn, "a") as bpf:
for p in bp:
bpf.write("%s\n" % (str(p),))
byssummary.append(bpv.plot(xs, ys, years, str(j)))
except Exception as e:
print "MAIN Exception Step ", stepn, str(e)
pass
#print byssummary
byssummarycounts={} #we will count how many actual different predictions we get
plt.show()
for i in range(len(byssummary)):
ax1=plt.plot(byssummary[i], np.ones(np.shape(byssummary[i]))*i, "bo")
bskey=str(byssummary[i])
if not (bskey in byssummarycounts):
byssummarycounts[bskey] = 0
byssummarycounts[bskey] += 1
#plot the variance, skew and kurtosis all normalised between 0 and 100
import runningstats
s=runningstats.runningMoments(ys)
sdelta=runningstats.runningMoments(ys[1:]-ys[:-1])
(mean, variance, sigma, skew, kurtosis) = s.moments()
#print (mean, variance, sigma, skew, kurtosis)
vararray= [variance]
skewarray= [skew]
kurtarray= [ kurtosis]
while s.step():
(mean, variance, sigma, skew, kurtosis) = s.moments()
#print (mean, variance, sigma, skew, kurtosis)
vararray.append(variance)
skewarray.append(skew)
kurtarray.append(kurtosis)
vararray = np.array(vararray)
skewarray = np.array(skewarray)
kurtarray = np.array(kurtarray)
vararray = 50 + 50 * (np.array(vararray)-np.mean(vararray))/(np.max(vararray)-np.min(vararray))
skewarray = 50 + 50.* (np.array(skewarray)-np.mean(skewarray))/(np.max(skewarray)-np.min(skewarray))
kurtarray = 50 + 50.* (np.array(kurtarray)-np.mean(kurtarray))/(np.max(kurtarray)-np.min(kurtarray))
ax3=plt.plot(years[15:15+len(vararray)], vararray,"g--")
ax4=plt.plot(years[15:15+len(vararray)], skewarray,"r--")
ax5=plt.plot(years[15:15+len(vararray)], kurtarray,"k--")
(mean, variance, sigma, skew, kurtosis) = sdelta.moments()
#print (mean, variance, sigma, skew, kurtosis)
vararray= [variance]
skewarray= [skew]
kurtarray= [ kurtosis]
while sdelta.step():
(mean, variance, sigma, skew, kurtosis) = sdelta.moments()
#print (mean, variance, sigma, skew, kurtosis)
vararray.append(variance)
skewarray.append(skew)
kurtarray.append(kurtosis)
vararray = np.array(vararray)
skewarray = np.array(skewarray)
kurtarray = np.array(kurtarray)
vararray = 50 + 50 * (np.array(vararray)-np.mean(vararray))/(np.max(vararray)-np.min(vararray))
skewarray = 50 + 50.* (np.array(skewarray)-np.mean(skewarray))/(np.max(skewarray)-np.min(skewarray))
kurtarray = 50 + 50.* (np.array(kurtarray)-np.mean(kurtarray))/(np.max(kurtarray)-np.min(kurtarray))
ax3=plt.plot(years[15:15+len(vararray)], vararray,"g-")
ax4=plt.plot(years[15:15+len(vararray)], skewarray,"r-")
ax5=plt.plot(years[15:15+len(vararray)], kurtarray,"k-")
rescaled = np.array(100.0*(ys-np.min(ys))/(np.max(ys)-np.min(ys)))
ax6=plt.plot(years, rescaled,"b-")
plt.title= sys.argv[2]
plt.savefig(sys.argv[2]+ "_Summary.png")
maxcount = 0
maxlist = []
with open(model+'.summary.txt', "w") as bysf:
for k in np.sort(byssummarycounts.keys()):
if byssummarycounts[k] == maxcount:
maxlist.append(k)
elif byssummarycounts[k] > maxcount:
maxlist = [k]
maxcount = byssummarycounts[k]
print k, byssummarycounts[k]
bysf.write("%s %s\n" % (str(k), str(byssummarycounts[k])))
pl = []
for breakk in maxlist:
breaks=[int(b) for b in breakk[1:-1].split(',')]
#print "BREAKS",breaks
for i in range(1,len(breaks)):
try:
pt = 0
#print "RANGE",years[0],breaks[i-1],years[0],breaks[i], range(years[0]-breaks[i-1],years[0]-breaks[i])
sYs = np.array(rescaled[breaks[i-1]-years[0]:breaks[i] - years[0]])
sXs = np.array(range(breaks[i-1],breaks[i]))
# print "SXY",sYs, sXs
#print breaks[i]-breaks[0], breaks[i+1]+1-breaks[0], sXs, sYs
#print "JIM", len(xs), len(ys)
pt = 1
stats=regress.analysed_regress(sYs,sXs)
pt = 2
yhat, resid=regress.residuals(sYs, sXs, stats)
pt = 3
pl.append(plt.plot(sXs, yhat, '-'))
except Exception as e:
print bivariate.ExceptionInfo()
print "Exception at ",str(e),pt