-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraymaker.py
300 lines (233 loc) · 9.04 KB
/
arraymaker.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
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pylab
import time
import pandas as pd
import statistics
import math
from scipy.interpolate import UnivariateSpline
from random import randrange
import seaborn as sns
sns.set()
class grid:
def __init__(self, x, y, params):
#x = grid x
#y = grid y
#params number of facies
self.x = x
self.y = y
self.params = params
self.array = []
self.cmap = []
self.norm = []
img = []
def input(self, inputdata, params):
self.array = inputdata
self.x = inputdata[1]
self.y = inputdata[0]
self.params = params
def update(self):
return plt.imshow(self.array,interpolation='nearest',
cmap = self.cmap,norm=self.norm
)
def genrandomgrid(self):
self.array = np.random.randint(self.params, size=(self.x, self.y))
def show2dgrid(self):
global img
bounds=[]
for i in range (0,self.params+2):
bounds.append(i)
self.cmap = plt.cm.tab10 # define the colormap replace if needed
# extract all colors from the .jet map
cmaplist = [self.cmap(i) for i in range(self.cmap.N)]
# create the new map
self.cmap = mpl.colors.LinearSegmentedColormap.from_list(
'Custom cmap', cmaplist, self.cmap.N)
self.norm = mpl.colors.BoundaryNorm(bounds, self.cmap.N)
# tell imshow about color map so that only set colors are used
img = self.update()
cmapbounds = bounds
del cmapbounds[-1]
#define ticks
cblegend = cmapbounds
#define colour bar remove blink colour
cb = plt.colorbar(img,boundaries=cmapbounds,ticks=cblegend)
labels = np.arange(0, int(cblegend[-1]), 1)
loc = labels + .5
cb.set_ticklabels(labels)
cb.set_ticks(loc)
cb.ax.tick_params(width=0)
cb.set_label('Facies Value', rotation=90)
self.update()
return plt.show(block=False)
def gridblink(self, x, y):
global img
reset = self.array[x, y]
self.array[x, y] = self.params+1 #blinks with next colour
self.update()
time.sleep(1)
self.array[x, y] = reset #blinks with next colour
self.update()
def randomreturn(self, n, iterations, valuemode):
#arraystructure = [[x1,y1,v1],[x2,y2,v2]]
cords = self.array
shape = list(cords.shape)
self.randarry = []
self.graphdata = []
data = []
for i in range(0,iterations):
randomseq = []
for i in range(n):
x = randrange(shape[1])
y = randrange(shape[0])
if valuemode == True:
randomseq.append([self.array[x,y]])
#only returns grid values and not x,y
else:
randomseq.append(x,y,self.array[x,y])
#returns coords and value in array
data.append(randomseq)
self.graphdata = data
return self.graphdata
def mean(self): #returns mean value of array
self.mean = np.mean(self.array)
return self.mean
def plot(self, type):
#variables
iterations = len(self.graphdata[0])
numruns = len(self.graphdata)
#creates moving adverages of data
def movavg():
def mov_avg(x, w):
for m in range(len(x)-(w-1)):
yield sum(np.ones(w) * x[m:m+w]) / w
rawdata = self.graphdata
data = {'y':[]}#dataframe
df = pd.DataFrame(data)
#df = df.transpose()
x = []
for n in range(0,iterations):
x.append(n+1)
self.iterations = x #x value for graph
for run in range(0,numruns):
y = [val for sublist in self.graphdata[run] for val in sublist]
df['y'] = y
df['SMA' + str(run)] = df.y.rolling(iterations, min_periods=1).mean()
del df['y']
return df
df = movavg()
#sphaghetti plot
def sphagetti():
#plot moving avergdge data from dataframe
for run in range(0,numruns):
plt.plot(self.iterations,df['SMA' + str(run)])
plt.xlim([1,iterations])
plt.ylim([0,self.params-1])
plt.xlabel('Iterations')
plt.ylabel('Measured property')
print('Input parameters:')
print('Iterations:', iterations)
print('Number of sample runs:', numruns)
print('Mean value:', self.mean())
return plt.show(block=False)
#minmax plot
def minmax():
#checks to see if minmax values have been generated, if not creates them.
is_local = "minmaxdf" in locals()
if is_local == True:
return
else:
#creates new dataframe with max and min variables in.
minmaxdf = pd.DataFrame()
minmaxdf['iteration'] = self.iterations
del self.iterations
minmaxdf['Max'] = df.max(axis=1)
minmaxdf['Min'] = df.min(axis=1)
self.iterations = minmaxdf['iteration']
max1 = minmaxdf['Max']
min1 = minmaxdf['Min']
x = minmaxdf['iteration']
ymax = max1.values.tolist()
ymin = min1.values.tolist()
#curve is y = 1/x
smax = UnivariateSpline(x, ymax, k=5, s=iterations)
xsmax = np.linspace(0, iterations , 100)
ysmax = smax(xsmax)
smin = UnivariateSpline(x, ymin, k=5, s=iterations)
xsmin = np.linspace(0, iterations , 100)
ysmin = smin(xsmin)
#plt.plot(xsmax, ysmax,color='red')
#plt.plot(xsmin, ysmin,color='blue')
plt.plot(x, max1, 'r+', alpha=0.3)
plt.plot(x, min1, 'b+', alpha=0.3)
mean = self.mean #call mean
plt.axhline(mean, color='r', linestyle=':')#mean line
plt.figtext(1, 0, mean, wrap=True, horizontalalignment='right', fontsize=12)#mean text
plt.xlim([1,iterations])
plt.ylim([0,self.params-1])
plt.xlabel('Iterations')
plt.ylabel('Measured property')
return plt.show(block=False)
#max min difference
def minmax_curve():
#checks to see if minmax values have been generated, if not creates them.
is_local = "minmaxdf" in locals()
if is_local == True:
return
else:
#creates new dataframe with max and min variables in.
minmaxdf = pd.DataFrame()
minmaxdf['iteration'] = self.iterations
minmaxdf['Max'] = df.max(axis=1)
minmaxdf['Min'] = df.min(axis=1)
#set x axis
x = minmaxdf['iteration']
plt.plot(x, minmaxdf['Max'] - minmaxdf['Min'])
plt.xlim([1,iterations])
plt.ylim([0,self.params-1])
plt.xlabel('Iterations')
plt.ylabel('Max/min iter. difference')
return plt.show(block=False)
#standard deviation of each iteration
def stdev():
#checks to see if minmax values have been generated, if not creates them.
is_local = "minmaxdf" in locals()
if is_local == True:
return
else:
#creates new dataframe with max and min variables in.
minmaxdf = pd.DataFrame()
minmaxdf['iteration'] = self.iterations
minmaxdf['Max'] = df.max(axis=1)
minmaxdf['Min'] = df.min(axis=1)
e = []
y = []
epos = []
eneg = []
for row in range(0,len(self.iterations)):
values = df.loc[row]
values = list(values)
dev = statistics.stdev(values,self.mean)
e.append(dev/2)
y.append(statistics.mean(list(values))) #mean
epos.append(statistics.mean(list(values))+(dev/2)) #for graph
eneg.append(statistics.mean(list(values))-(dev/2))
x = self.iterations
plt.plot(x, y)
#plt.plot(x, epos)
#plt.plot(x, eneg)
plt.fill_between(x, epos, eneg,color='gray', alpha=0.2)
plt.xlim(1, len(x))
plt.xlabel('Iterations')
plt.ylabel('Standard Deviation')
if type == 'sphagetti':
return sphagetti()
elif type == 'minmax': #sphagetti must be plotted before minmax
return minmax()
elif type == 'minmax_curve':
return minmax_curve()
elif type == 'stdev': #sphagetti must be plotted before stdev
return stdev()
else:
return ('Error: no valid type selected')