-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOM.py
executable file
·181 lines (131 loc) · 4.69 KB
/
SOM.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 29 12:00:36 2019
@author: jishnu
"""
#%%
import glob
import pickle
import numpy as np
import seaborn as sns
from minisom import MiniSom
import matplotlib.pyplot as plt
#%%
class SOM:
'''
Class to easily train, use, save and load the SOM clustering/classifier
packages used;
Pickle #to save and load files
Minisom #minimal implementation of Self Organizing maps
'''
def __init__(self,files=None,data=None,som=None,network_h=None,
network_w=None,coords=None,x=None,y=None,fnames=None):
'''initializing all the necessary values
Parameters:
-----------
files : list of files, which contain the 1D phase binned data
data : An array of arrays,each element is a 1D phase binned LC
som : self organising map NN with hxw neurons
network_h : height of the network
network_w : width of the network
coords : An array which contains som.winner for all LCs in data
x,y : x,y coords from coords
'''
self.files = files
self.fnames = fnames
self.data = data
self.som = som
self.network_h = 50
self.network_w = 50
self.coords = coords
self.x = x
self.y = y
def set_files(self,path):
'''
takes path to the files as arg; returns list of files in the path
'''
self.files = glob.glob(path+'*')
def get_arr(self,file):
'''
Get data from a file as an np array:
reject files which has nan values in them
nan can break the SOM classifier
'''
data = np.loadtxt(file)
if np.isnan(data).any() == True:
return np.nan
else:
return data
def set_data(self):
'''
opens each file in the folder and reads the data
into an array, and appends that to the data array
if it doesnt contain any nan values
'''
self.fnames,self.data,err_f = [],[],[]
for f in self.files:
arr = self.get_arr(f)
if arr is not np.nan:
self.fnames.append(f)
self.data.append(arr)
else:
err_f.append(f)
def set_som(self,sigma,learning_rate):
'''
initializes the network:
by default 50x50 with 0.1 sigma and 1.5 lr is initialized
'''
self.som = MiniSom(x = self.network_h,y = self.network_w,
input_len = 32, sigma = sigma,
learning_rate = learning_rate)
self.som.random_weights_init(self.data)
#initialize random weights to the network
def train_som(self,number):
'''
tains the network with 'number' iterations by randomly taking
'number' of elements from the data array
'''
self.som.train_random(self.data, number)
def save_model(self,outfile):
'''
Save the trained model
'''
with open(outfile+'.p', 'wb') as outfile:
pickle.dump(self.som, outfile)
def load_model(self,som_file):
'''
Load the saved model
'''
with open(som_file, 'rb') as infile:
self.som = pickle.load(infile)
def get_coords(self):
'''
Runs each of the elements of the dataset through the SOM
and gets the winner and appends it to the coords array
'''
self.coords = []
err = []
self.x = []
self.y = []
for d in self.data:
try:
coord = np.array(self.som.winner(d))
self.coords.append(coord)
self.x.append(coord[0])
self.y.append(coord[1])
except:
#print("err with ",str(d))
err.append(d)
#getting x,y points
# self.x = [i[0] for i in self.coords]
# self.y = [i[1] for i in self.coords]
return self.x,self.y
def plot_winners(self):
x,y = self.x,self.y
plt.style.use('seaborn')
plt.figure(figsize=(9,9))
plt.plot(x,y,'.',alpha=0.15)
sns.kdeplot(x,y,cmap='Blues',shade=True,bw=1.5,shade_lowest=False, alpha=0.8)
plt.show()
plt.close()