-
Notifications
You must be signed in to change notification settings - Fork 0
/
knnelbow2.py
68 lines (58 loc) · 1.71 KB
/
knnelbow2.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
from netCDF4 import Dataset
import numpy as np
from fastdtw import fastdtw
from scipy.spatial.distance import euclidean
import matplotlib.pyplot as plt
neighbourlong=[0,1,1,1,0,-1,-1,-1]
neighbourlat =[-1,-1,0,1,1,1,0,-1]
globalcorepoints=set()
globalnoisepoints=set(range(480))
globalborderpoints=set()
globaldist=[]
Modedict={}
f = Dataset("ALDA_dataset_and_initial_code/air.2m.gauss.1979.nc", "r+", format="NETCDF4")
def testFunc():
global globaldist
timewindow=9
iter=0
temp=range(4,7)
for timeIndex in temp:
for iterlong in range(1,190):
for iterlat in range(1,92):
currtimeslice = f.variables['air'][timeIndex - timewindow/2 : timeIndex + timewindow/2 + 1, 0, iterlat, iterlong]
currdist=[]
for iter in range(8):
neighbourtimeslice = f.variables['air'][timeIndex - timewindow/2 : timeIndex + timewindow/2 + 1, 0, iterlat +neighbourlat[iter], iterlong+neighbourlong[iter]]
distance, path = fastdtw(currtimeslice, neighbourtimeslice, dist=euclidean)
#print distance
#dist.append((distance,iter))
currdist.append(distance)
currdist.sort()
globaldist+=currdist[0:2]
def main():
testFunc()
global globaldist
#print dist
globaldist.sort()
print globaldist
plt.plot(globaldist)
plt.ylabel("2nd neighbour distance")
plt.show()
#fig=plt.figure()
#ax=fig.add_subplot(111)
#y,x,_=plt.hist(dist,bins=100)
#for i in range(0,len(y)):
#if y[i]== y.max():
#print x[i]
#break
#
#fig.savefig("plot.png")
#nmax=np.max(n)
#arg_max=None
#for j,_n in enumerate(n):
#if _n==max:
#arg_max=j
#break
#print dist[arg_max]
if __name__ == '__main__':
main()