/
Plot_ER_Data_Figure_8a.py
684 lines (544 loc) · 27.3 KB
/
Plot_ER_Data_Figure_8a.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 14:01:05 2015
@author: SWDG
"""
import matplotlib.pyplot as plt
from matplotlib import rcParams
from matplotlib import container
from matplotlib import collections
import numpy as np
import scipy.optimize as optimize
import bin_data as Bin
from scipy.stats import gaussian_kde
from scipy.stats import sem
from uncertainties import unumpy as unp
def LoadData(Path,Prefix,Order):
"""
Loads topogrpahic data from the 3 datafiles generated by E_R_STAR.cpp.
Path is the path where the files are stored, with a trailing slash.
Prefix is the filename prefix used by E_R_STAR.cpp to denote a landscape.
Order is the integer basin order used to extract basin average data in E_R_STAR.cpp.
Returns 2D arrays of topographic data from the raw, patch average and basin average datasets.
"""
#load the data from the raw file
#not using genfromtext as I want access to individual elements
#for debugging, may change in future
with open(Path+Prefix+'_E_R_Star_Raw_Data.csv','r') as raw:
no_of_cols = len(raw.readline().split(','))
rawdata = raw.readlines()
#want the data in a 2d array to make moving the values about simpler
#dimensions will be 6Xlen(rawdata) no_of_cols = 6
#and the row order will follow the header format in the input file:
#i j LH CHT Relief Slope
no_of_lines = len(rawdata)
RawData = np.zeros((no_of_cols,no_of_lines),dtype='float64')
for i,r in enumerate(rawdata):
split = r.split(',')
for a in range(no_of_cols):
RawData[a][i] = split[a]
#now we have a transformed 2d array of our raw data
#Next, repeat the process for the patch data
with open(Path+Prefix+'_E_R_Star_Patch_Data.csv','r') as patch:
no_of_cols = len(patch.readline().split(','))
patchdata = patch.readlines()
#dimensions will be 18Xlen(patchdata) no_of_cols = 18
#and the row order will follow the header format in the input file:
#Final_ID lh_means lh_medians lh_std_devs lh_std_errs cht_means cht_medians cht_std_devs cht_std_errs r_means r_medians r_std_devs r_std_errs s_means s_medians s_std_devs s_std_errs patch_size
no_of_lines = len(patchdata)
PatchData = np.zeros((no_of_cols,no_of_lines),dtype='float64')
for i,p in enumerate(patchdata):
split = p.split(',')
for a in range(no_of_cols):
PatchData[a][i] = split[a]
#Next, repeat the process for the Basin data
with open(Path+Prefix+'_E_R_Star_Basin_'+str(Order)+'_Data.csv','r') as basin:
no_of_cols = len(basin.readline().split(','))
basindata = basin.readlines()
#dimensions will be 11Xlen(basindata) no_of_cols = 19
#and the row order will follow the header format in the input file:
#Basin_ID,LH_mean,CHT_mean,Relief_mean,Slope_mean,LH_median,CHT_median,Relief_median,Slope_median,LH_StdDev,CHT_StdDev,Relief_StdDev,Slope_StdDev,LH_StdErr,CHT_StdErr,Relief_StdErr,Slope_StdErr,Area,Count
no_of_lines = len(basindata)
BasinData = np.zeros((no_of_cols,no_of_lines),dtype='float64')
for i,d in enumerate(basindata):
split = d.split(',')
for a in range(no_of_cols):
BasinData[a][i] = split[a]
return RawData,PatchData,BasinData
def PropagateErrors(PatchData,BasinData):
"""
Load the hillslope, Relief and hilltop curavture data from the basin and
patch data files into the uncertainties package, so that we can propagate
errors through our calculations.
"""
#median, sem
patchLH = unp.uarray(PatchData[2],PatchData[4])
patchR = unp.uarray(PatchData[10],PatchData[12])
patchCHT = unp.uarray(PatchData[6],PatchData[8])
basinLH = unp.uarray(BasinData[5],BasinData[13])
basinR = unp.uarray(BasinData[7],BasinData[15])
basinCHT = unp.uarray(BasinData[6],BasinData[14])
return (patchLH,patchR,patchCHT),(basinLH,basinR,basinCHT)
def SetUpPlot():
"""
Configure the plotting environment.
"""
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['arial']
rcParams['font.size'] = 15
ax = plt.gca()
ax.set_yscale('log', nonposy='clip')
ax.set_xscale('log', nonposx='clip')
plt.xlabel('Dimensionless Erosion Rate, E*')
plt.ylabel('Dimensionless Relief, R*')
plt.ylim(0.05,1.1)
plt.xlim(0.1,1000)
return ax
def PlotRaw(Sc,RawData):
"""
Plot the raw E*R* data as small grey points.
"""
plt.plot(E_Star(Sc,RawData[3],RawData[2]),R_Star(Sc,RawData[4],RawData[2]),
'k.',alpha=0.2,label='Raw Data')
def PlotRawDensity(Sc,RawData,Thin):
"""
Plot the raw E*R* data as a density plot, computing the PDF as a gaussian
and including a colorbar.
Built around code from: http://stackoverflow.com/a/20107592/1627162
"""
x = E_Star(Sc,RawData[3],RawData[2])
y = R_Star(Sc,RawData[4],RawData[2])
if Thin:
x = x[::Thin]
y = y[::Thin]
xy = np.vstack([x,y])
density = gaussian_kde(xy)(xy)
#order the points by density so highest density is on top in the plot
idx = density.argsort()
x, y, density = x[idx], y[idx], density[idx]
plt.scatter(x,y,c=density,edgecolor='',cmap=plt.get_cmap("autumn_r"))
cbar = plt.colorbar()
cbar.set_label('Probability Distribution Function')
def PlotRawBins(Sc,RawData,NumBins,MinimumBinSize=100,ErrorBars=True):
"""
Plot E*R* data binned from the raw data.
"""
E_s = E_Star(Sc, RawData[3], RawData[2])
R_s = R_Star(Sc, RawData[4], RawData[2])
bin_x, bin_std_x, bin_y, bin_std_y, std_err_x, std_err_y, count = Bin.bin_data_log10(E_s,R_s,NumBins)
#filter bins based on the number of data points used in their calculation
bin_x = np.ma.masked_where(count<MinimumBinSize, bin_x)
bin_y = np.ma.masked_where(count<MinimumBinSize, bin_y)
#these lines produce a meaningless warning - don't know how to solve it yet.
if ErrorBars:
#only plot errorbars for y as std dev of x is just the bin width == meaningless
plt.scatter(bin_x,bin_y,c=count,s=50,edgecolor='',cmap=plt.get_cmap("autumn_r"),label='Binned Raw Data', zorder=100)
plt.errorbar(bin_x, bin_y, yerr=std_err_y, fmt=None,ecolor='k', zorder=0)
cbar = plt.colorbar()
cbar.set_label('Number of values per bin')
else:
plt.errorbar(bin_x, bin_y, fmt='bo',label='Binned Raw Data')
def PlotPatchBins(Sc,PatchData,NumBins,color,MinimumBinSize=7,ErrorBars=True):
"""
Plot E*R* data binned from the hilltop pacth data.
"""
E_s = E_Star(Sc,PatchData[6],PatchData[2])
R_s = R_Star(Sc,PatchData[10],PatchData[2])
bin_x, bin_std_x, bin_y, bin_std_y, std_err_x, std_err_y, count = Bin.bin_data_log10(E_s,R_s,NumBins)
#filter bins based on the number of data points used in their calculation
bin_x = np.ma.masked_where(count<MinimumBinSize, bin_x)
bin_y = np.ma.masked_where(count<MinimumBinSize, bin_y)
#these lines produce a meaningless warning - don't know how to solve it yet.
if ErrorBars:
#only plot errorbars for y as std dev of x is just the bin width == meaningless
plt.scatter(bin_x,bin_y,c=count,s=50,edgecolor='',cmap=plt.get_cmap("autumn_r"),label='Binned Patch Data', zorder=100)
plt.errorbar(bin_x, bin_y, yerr=std_err_y, fmt=None,ecolor='k',elinewidth=2,capsize=3,zorder=0)
cbar = plt.colorbar()
cbar.set_label('Number of values per bin')
else:
plt.errorbar(bin_x, bin_y, fmt='o',color=color,label='No. of Bins = '+str(NumBins))
def PlotPatches(Sc,PatchData,ErrorBars):
"""
Plot E*R* data binned from hilltop patches.
"""
e_star = E_Star(Sc,PatchData[2],PatchData[0])
r_star = R_Star(Sc,PatchData[1],PatchData[0])
if ErrorBars:
plt.errorbar(unp.nominal_values(e_star),unp.nominal_values(r_star),yerr=unp.std_devs(r_star),xerr=unp.std_devs(e_star),
fmt='ro',label='Hilltop Patch Data')
else:
plt.errorbar(unp.nominal_values(e_star),unp.nominal_values(r_star),
fmt='ro',label='Hilltop Patch Data')
def PlotPatchesArea(Sc,PatchData,thresh,alpha):
"""
Plot patch average E*R* data filtered by a user defined patch area value.
"""
e_star = E_Star(Sc,PatchData[6],PatchData[2])
r_star = R_Star(Sc,PatchData[10],PatchData[2])
area = PatchData[17]
x = []
y = []
for a,b,s in zip(e_star,r_star,area):
if s > thresh:
x.append(a)
y.append(b)
plt.plot(x,y,color='k',alpha=alpha,marker='o',linestyle='',label='Min. Patch Area = '+str(thresh))
def PlotBasins(Sc,BasinData,ErrorBars):
"""
Plot basin average E*R* data.
"""
e_star = E_Star(Sc,BasinData[2],BasinData[0])
r_star = R_Star(Sc,BasinData[1],BasinData[0])
if ErrorBars:
plt.errorbar(unp.nominal_values(e_star),unp.nominal_values(r_star),yerr=unp.std_devs(r_star),xerr=unp.std_devs(e_star),
fmt='go',label='Basin Data')
else:
plt.errorbar(unp.nominal_values(e_star),unp.nominal_values(r_star),
fmt='go',label='Basin Data')
def PlotBasinsArea(Sc,BasinData,thresh,alpha):
"""
Plot basin average E*R* data filtered by a user defined basin area value.
"""
e_star = E_Star(Sc,BasinData[6],BasinData[5])
r_star = R_Star(Sc,BasinData[7],BasinData[5])
area = BasinData[18]
x = []
y = []
for a,b,s in zip(e_star,r_star,area):
if s > thresh:
x.append(a)
y.append(b)
plt.plot(x,y,color='k',alpha=alpha,marker='o',linestyle='',label='Min. Basin Data Points = '+str(thresh))
def PlotLandscapeAverage(Sc,RawData,ErrorBars):
"""
Plot a landscape median data point, calculated using the raw data, and generating
errorbars using the standard error.
"""
E_Star_temp = E_Star(Sc,RawData[3],RawData[2])
R_Star_temp = R_Star(Sc,RawData[4],RawData[2])
E_Star_avg = np.median(E_Star_temp)
R_Star_avg = np.median(R_Star_temp)
if ErrorBars:
E_Star_std = np.std(E_Star_temp)
R_Star_std = np.std(R_Star_temp)
E_Star_serr = sem(E_Star_temp)
R_Star_serr = sem(R_Star_temp)
plt.errorbar(E_Star_avg,R_Star_avg,yerr=R_Star_serr,xerr=E_Star_serr,
fmt='ko',label='Landscape Average')
else:
plt.errorbar(E_Star_avg,R_Star_avg, fmt='ko',label='Landscape Average')
def R_Star_Model(x):
"""
Return the predicted R* value for a given value of E* using eq 10 in Roering
et al. (2007) http://www.sciencedirect.com/science/article/pii/S0012821X07006061
"""
return (1./x) * (np.sqrt(1.+(x*x)) - np.log(0.5*(1. + np.sqrt(1.+(x*x)))) - 1.)
def E_Star(Sc,CHT,LH):
"""
Calculate the E* value from topographic data after Roering et al. (2007)
http://www.sciencedirect.com/science/article/pii/S0012821X07006061
"""
if type(LH[0]) == np.float64:
return (2.*np.fabs(CHT)*LH)/Sc
else:
return (2.*unp.fabs(CHT)*LH)/Sc
def R_Star(Sc, R, LH):
"""
Calculate the R* value from topographic data after Roering et al. (2007)
http://www.sciencedirect.com/science/article/pii/S0012821X07006061
"""
return R/(LH*Sc)
def Residuals(Sc, R, LH, CHT):
"""
Calculate the residuals between the R* value computed using eq 10 in Roering
et al. (2007) (http://www.sciencedirect.com/science/article/pii/S0012821X07006061)
and the R* calculated from topographic data.
"""
return R_Star_Model(E_Star(Sc,CHT,LH)) - R_Star(Sc, R, LH)
def reduced_chi_square(Residuals,Sc,DataErrs=None):
"""
Compute a reduced chi square value for the best fit Sc value.
"""
#if we are fitting from patches or basins, get the std err and include in the chi squared
if DataErrs:
r_star = R_Star(Sc,DataErrs[1],DataErrs[0])
#get rid of any divide by zero errors
temp = ((Residuals/unp.std_devs(r_star))**2)
temp[np.isinf(temp)] = 0
chi_square = np.sum(temp)
else:
chi_square = np.sum(Residuals**2)
# degrees of freedom, as we have 1 free parameter, Sc
d_o_f = Residuals.size-2
return chi_square/d_o_f
def r_squared(Sc, R, LH, CHT, infodict):
"""
Calculate the R squared value of the best fit Sc value, using the residuals
from the infodict generated by the optimize package.
"""
measured = R_Star(Sc, R, LH)
mean_measured = np.mean(measured)
sqr_err_w_line = np.square(infodict['fvec'])
sqr_err_mean = np.square((measured - mean_measured))
return 1.-(np.sum(sqr_err_w_line)/np.sum(sqr_err_mean))
def DrawCurve():
"""
Plot the steady state curve in E*R* space.
"""
#plot the e* r* curve from roering 2007
x = np.arange(0.01, 1000, 0.1)
plt.plot(x, R_Star_Model(x), 'k-', linewidth=2, label='Equation 5')
def GetBestFitSc(Method, Data, DataErrs=None):
"""
Compute the best fit Sc value to the data using the scipy optimize.leassq
package. Also returns the reduced chi squared as a measure of the goodness
of fit.
"""
ScInit = 0.8 # Need to have an initial for the optimizer, any valid Sc value can be used - will not impact the final value
Fit_Sc = [] #Need to initialize this in case Method is incorrectly defined. Need some error handling!
if Method == 'raw':
Fit_Sc,_,infodict,_,_ = optimize.leastsq(Residuals, ScInit, args=(Data[4], Data[2], Data[3]),full_output=True)
chi = reduced_chi_square(infodict['fvec'],Fit_Sc[0])
elif Method == 'patches':
Fit_Sc,_,infodict,_,_ = optimize.leastsq(Residuals, ScInit, args=(Data[10], Data[2], Data[6]),full_output=True)
chi = reduced_chi_square(infodict['fvec'],Fit_Sc[0],DataErrs)
elif Method == 'basins':
Fit_Sc,_,infodict,_,_ = optimize.leastsq(Residuals, ScInit, args=(Data[7], Data[5], Data[6]),full_output=True)
chi = reduced_chi_square(infodict['fvec'],Fit_Sc[0],DataErrs)
return Fit_Sc[0],chi
def BootstrapSc(Method, Data, n=10000):
"""
Bootstrap the calculation of the best fit Sc value n times to get the 95%
confidence interval for the best fit Sc.
Values of n larger than 10000 will take a long time to run.
"""
tmp = []
#need to convert the LH,R,CHT data into a serial 1D array before bootstrapping
if Method == 'raw':
for i in range(len(Data[0])):
tmp.append(SerializeData(Data[2][i],Data[4][i],Data[3][i]))
if Method == 'patches':
for i in range(len(Data[0])):
tmp.append(SerializeData(Data[2][i],Data[10][i],Data[6][i]))
if Method == 'basins':
for i in range(len(Data[0])):
tmp.append(SerializeData(Data[5][i],Data[7][i],Data[6][i]))
ToSample = np.array(tmp)
Scs = []
i=0
while i < n:
print i
sample = np.random.choice(ToSample,len(ToSample),replace=True)
LH,R,CHT = UnserializeList(sample)
sc,_,_,_,_ = optimize.leastsq(Residuals, 0.8, args=(R, LH, CHT),full_output=True)
if sc < 2.0:
Scs.append(sc[0])
i += 1
# mean upper bound lower bound
return np.mean(Scs),np.percentile(Scs,97.5)-np.mean(Scs), np.mean(Scs)-np.percentile(Scs,2.5)
def SerializeData(LH, R, CHT):
"""
Convert the hillslope length, relief, and hilltop curvature data into a string,
to facilitate the sampling by replacement of the data in the bootstrapping
method.
"""
return str([LH, R, CHT])[1:-1]
def UnserializeList(serial_list):
"""
Convenicence function to unserialize a list of serialized data and return
arrays of hillslope length, relief, and hilltop curvature.
"""
LH=[]
R=[]
CHT=[]
for s in serial_list:
lh,r,cht = UnserializeData(s)
LH.append(lh)
R.append(r)
CHT.append(cht)
return np.array(LH),np.array(R),np.array(CHT)
def UnserializeData(serial):
"""
Unpack the data serialized by SerializeData, returning the original values
in their original format.
"""
split = [float(s) for s in serial.split(',')]
return split[0],split[1],split[2]
def Labels(Sc,Method,ax):
"""
Method to handle the labelling of axes, generation of a legend and creation
of a plot title.
"""
#remove errorbars from the legend
handles, labels = ax.get_legend_handles_labels()
handles = [h[0] if isinstance(h, container.ErrorbarContainer) else h for h in handles]
#color scatterplot symbols like colormap
for h in handles:
if isinstance(h, collections.PathCollection):
h.set_color('r')
h.set_edgecolor('')
ax.legend(handles, labels, loc=4, numpoints=1,scatterpoints=1)
#in case Method is invalid
fit_description = ' = '
if Method == 'raw':
fit_description = ' from raw data = '
elif Method == 'patches':
fit_description = ' from hilltop patches = '
elif Method == 'basins':
fit_description = ' from basin average data = '
if isinstance(Method,int) or isinstance(Method,float):
plt.title('$\mathregular{S_c}$ = ' + str(round(Sc,2)), y = 1.02)
else:
plt.title('Best fit $\mathregular{S_c}$'+fit_description+str(round(Sc,2)), y = 1.02, fontsize=15)
def SavePlot(Path,Prefix,Format):
"""
Wrapper function around the matplotlib savefig method, which save a high resolution
copy of the final figure into the user supplied path with the user suppied
file format.
"""
plt.savefig(Path+'a'+Prefix+'_E_R_Star.'+Format,dpi=500)
plt.clf()
def CRHurst():
"""
Plots the E*R* data generated for the Cascade Ridge by Hurst et al. (2012)
(http://onlinelibrary.wiley.com/doi/10.1029/2011JF002057/full) seen in
figure 14.
"""
x = [1.15541793184, 2.96599962747, 5.06753455114, 6.87537359947, 8.86462081724, 10.9425778888, 12.9426702489, 14.9866553641, 16.9785507349, 19.0034609662, 20.9560856862, 22.8577931724, 24.6085876779, 27.3044634219, 28.3873092441, 31.1978149101, 32.8625186998, 35.2335006909, 37.2282499959, 43.8911646306, 45.5936728215]
y = [0.379133283693, 0.435531356239, 0.547479389809, 0.588874111323, 0.652649344881, 0.696659574468, 0.824275084903, 0.733856012658, 0.783243670886, 0.836195147679, 0.920291139241, 0.862545710267, 0.953440506329, 0.851824367089, 0.97046835443, 0.909219409283, 0.964772151899, 1.08295780591, 0.904050632911, 1.13525316456, 0.934139240506]
yStdErr = [0.12913016, 0.03901928, 0.04112731, 0.02724568, 0.04694418,
0.04026138, 0.03122017, 0.02083737, 0.01752255, 0.02029758,
0.02403573, 0.02065953, 0.02382283, 0.021544, 0.0216427,
0.02044206, 0.02158805, 0.02227467, 0.03237965, 0.04332041,
0.09519842]
plt.errorbar(x, y, yerr=yStdErr, fmt='k^', label='Hurst et al. (2012)',
elinewidth=2, capsize=3, markersize=6)
def GMRoering():
"""
Plots the E*R* data for the Gabilan Mesa generated by Roering et al. (2007)
http://www.sciencedirect.com/science/article/pii/S0012821X07006061
"""
x = [1.68]*2
y = [0.34,0.43]
xerr = [0.7]*2
yerr = [0.17,0.2]
plt.errorbar(x,y,yerr,xerr,'k^',label='Roering et al. (2007)',elinewidth=2,capsize=3,markersize=12,zorder=1000)
def OCRRoering():
"""
Plots the E*R* data for the Oregon Coast Range generated by Roering et al.
(2007) http://www.sciencedirect.com/science/article/pii/S0012821X07006061
"""
x = [6.3]*2
y = [0.57,0.64]
xerr = [2.1]*2
yerr = [0.23,0.18]
plt.errorbar(x,y,yerr,xerr,'k^',label='Roering et al. (2007)',elinewidth=2,capsize=3,markersize=12,zorder=1000)
def MakeThePlot(Path,Prefix,Sc_Method,RawFlag,DensityFlag,BinFlag,NumBins,PatchFlag,BasinFlag,LandscapeFlag,Order,ErrorBarFlag=True,Format='png',ComparisonData=(False,False,False),NumBootsraps=10000):
"""
Method which controls the generation of E*R* data. Does not need to be
interfaced with directly. Is called by the IngestSettings method using
parameters in the Settings.py file.
"""
RawData,PatchData,BasinData = LoadData(Path,Prefix,Order)
PatchDataErrs, BasinDataErrs = PropagateErrors(PatchData,BasinData)
ax = SetUpPlot()
if isinstance(Sc_Method,int) or isinstance(Sc_Method,float):
Sc = Sc_Method
else:
if Sc_Method == 'raw':
Sc,upper,lower = BootstrapSc(Sc_Method, RawData, NumBootsraps)
if Sc_Method == 'patches':
Sc,upper,lower = BootstrapSc(Sc_Method, PatchData, NumBootsraps)
if Sc_Method == 'basins':
Sc,upper,lower = BootstrapSc(Sc_Method, BasinData, NumBootsraps)
if RawFlag:
PlotRaw(Sc,RawData)
if DensityFlag:
PlotRawDensity(Sc,RawData,DensityFlag)
if PatchFlag:
PlotPatches(Sc,PatchDataErrs,ErrorBarFlag)
if BinFlag == 'patches':
if NumBins == 50:
DrawCurve()
PlotPatchBins(Sc,PatchData,NumBins,'gold',ErrorBars=ErrorBarFlag)
if NumBins == 20: PlotPatchBins(Sc,PatchData,NumBins,'darkorange',ErrorBars=ErrorBarFlag)
if NumBins == 10: PlotPatchBins(Sc,PatchData,NumBins,'r',ErrorBars=ErrorBarFlag)
if NumBins == 5: PlotPatchBins(Sc,PatchData,NumBins,'darkred',ErrorBars=ErrorBarFlag)
elif BinFlag == 'raw':
PlotRawBins(Sc,RawData,NumBins,ErrorBars=ErrorBarFlag)
if BasinFlag:
PlotBasins(Sc,BasinDataErrs,ErrorBarFlag)
if LandscapeFlag:
PlotLandscapeAverage(Sc,RawData,ErrorBarFlag)
if ComparisonData[0]:
GMRoering()
if ComparisonData[1]:
OCRRoering()
if ComparisonData[2]:
CRHurst()
Labels(Sc,Sc_Method,ax)
#plt.show()
#SavePlot(Path,Prefix+`Sc`,Format)
def IngestSettings(nb):
"""
Load the parameters from the Settings.py file, perform type and sanity checking
and run the main code to generate E*R* data.
"""
import Settings
import sys
#typecheck inputs
if not isinstance(Settings.Path, str):
sys.exit('Path=%s \nThis is not a valid string and so cannot be used as a path.\nExiting...' % Settings.Path)
if not isinstance(Settings.Prefix, str):
sys.exit('Prefix=%s \nThis is not a valid string and so cannot be used as a filename prefix.\nExiting...' % Settings.Prefix)
if not isinstance(Settings.Sc_Method, str) and not isinstance(Settings.Sc_Method, float):
sys.exit('Sc_Method=%s \nThis is not a valid string or floating point value.\nExiting...' % Settings.Sc_Method)
else:
if isinstance(Settings.Sc_Method, str) and (Settings.Sc_Method != 'raw' and Settings.Sc_Method != 'patches' and Settings.Sc_Method != 'basins'):
sys.exit('Sc_Method=%s \nThis is not a valid method to fit a critical gradient. Valid options are \'raw\',\'patches\', or \'basins\'.\nExiting...' % Settings.Sc_Method)
if isinstance(Settings.Sc_Method, float) and (Settings.Sc_Method <= 0 or Settings.Sc_Method > 3):
sys.exit('Sc_Method=%s \nThis critical gradient not within a expected range of 0 to 3.\nExiting...' % Settings.Sc_Method)
if not isinstance(Settings.RawFlag, int):
sys.exit('RawFlag should be set to 1 to plot the raw data or 0 to exclude the raw data. You have entered %s\nExiting...' % Settings.RawFlag)
if not isinstance(Settings.DensityFlag, int):
sys.exit('DensityFlag should be set to 1 to produce a density plot or 0 to not plot a density plot. Integer values greater than 1 will thin the data before plotting. You have entered %s\nExiting...' % Settings.DensityFlag)
if not isinstance(Settings.BinFlag, str):
sys.exit('BinFlag=%s \nThis is not a valid string to select the binning method. If not performing binning, enter a blank string: \'\'.\nExiting...' % Settings.BinFlag)
else:
if Settings.BinFlag:
if Settings.BinFlag.lower() != 'raw' and Settings.BinFlag.lower() != 'patches':
sys.exit('BinFlag=%s \nSelect either \'raw\' or \'patches\' as the binning method. Enter a blank string: \'\' if no binning is required.\nExiting...' % Settings.BinFlag)
if not isinstance(Settings.NumBins, int):
sys.exit('NumBins should be set to the number of bins to be generated when binning the data. If no binning is to be performed, set the value to 0. You have entered %s\nExiting...' % Settings.NumBins)
if not isinstance(Settings.PatchFlag, int):
sys.exit('PatchFlag should be set to 1 to plot the patch data or 0 to exclude the patch data. You have entered %s\nExiting...' % Settings.PatchFlag)
if not isinstance(Settings.BasinFlag, int):
sys.exit('BasinFlag should be set to 1 to plot the basin data or 0 to exclude the basin data. You have entered %s\nExiting...' % Settings.BasinFlag)
if not isinstance(Settings.LandscapeFlag, int):
sys.exit('LandscapeFlag should be set to 1 to plot the landscape average data or 0 to exclude the landscape average data. You have entered %s\nExiting...' % Settings.LandscapeFlag)
if not isinstance(Settings.Order, int):
sys.exit('Order should be set to an integer (eg 1,2,3, etc) to load the basin average data generated for that order of basin. You have entered %s, of %s\nExiting...' % (Settings.Order, type(Settings.Order)))
if not isinstance(Settings.ErrorBarFlag, bool):
sys.exit('ErrorBarFlag should be set to either True or False. True will generate plots with errorbars, False will exclude them. You have entered %s\nExiting...' % Settings.ErrorBarFlag)
ValidFormats = ['png', 'pdf','ps', 'eps','svg']
if not isinstance(Settings.Format, str):
sys.exit('Format=%s \nFile format must be a valid string.\nExiting...' % Settings.Format)
if Settings.Format.lower() not in ValidFormats:
sys.exit('Format=%s \nFile format must be one of: png, pdf, ps, eps or svg.\nExiting...' % Settings.Format)
if not isinstance(Settings.GabilanMesa, bool):
sys.exit('GabilanMesa should be set to either True or False. True will plot data from Roering et al. (2007), False will not. You have entered %s\nExiting...' % Settings.GabilanMesa)
if not isinstance(Settings.OregonCoastRange, bool):
sys.exit('OregonCoastRange should be set to either True or False. True will plot data from Roering et al. (2007), False will not. You have entered %s\nExiting...' % Settings.OregonCoastRange)
if not isinstance(Settings.SierraNevada, bool):
sys.exit('SierraNevada should be set to either True or False. True will plot data from Roering et al. (2007), False will not. You have entered %s\nExiting...' % Settings.SierraNevada)
if not isinstance(Settings.NumBootsraps, int):
sys.exit('NumBootsraps should be set to an integer (eg 10000) to select the number of iterations in the bootstrapping calculation.\n\nThis value is ignored if a value of Sc is supplied. Using a value > 10000 will take a long time on big datasets. You have entered %s, of %s\nExiting...' % (Settings.NumBootsraps, type(Settings.NumBootsraps)))
MakeThePlot(Settings.Path,Settings.Prefix,Settings.Sc_Method,Settings.RawFlag,Settings.DensityFlag,Settings.BinFlag,nb,Settings.PatchFlag,Settings.BasinFlag,Settings.LandscapeFlag,Settings.Order,Settings.ErrorBarFlag,Settings.Format,(Settings.GabilanMesa,Settings.OregonCoastRange,Settings.SierraNevada),Settings.NumBootsraps)
for i in [50,20,10,5]:
IngestSettings(i)
SavePlot('C:\\Users\\Stuart\\Dropbox\\data\\final\\','CR','png')
#MakeThePlot('GM','patches',0,0,'',20,0,1,0,2,ErrorBarFlag=False,Format='png')
#for l in ['GM','OR','NC','CR']:
# for m in ['raw','patches','basins']:
# print l,m
# MakeThePlot('C:\\Users\\Stuart\\Dropbox\\data\\final\\',l,m,0,0,'',20,0,1,0,2,ErrorBarFlag=False,Format='png')