-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinear_decoder.py
More file actions
executable file
·194 lines (144 loc) · 8.66 KB
/
linear_decoder.py
File metadata and controls
executable file
·194 lines (144 loc) · 8.66 KB
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
import os
import numpy as np
import h5py
from time import time
from dataset import ds_x_activities_y_original
## Import from constants file
data_dir = 'FakeData/' #'temp/'
cell_type_inds_path = 'temp/' # optional, only if you ever want to split cell types
linear_decoder_save_path = 'FakeModels/'
immean = 0.5
total_block_size = 10000
class LinearDecoder():
def __init__(self):
self.data = []
def runLD(activity_suffix, LD_directory_suffix, im_width=128, cell_type_list=None, create_decoded_images=True):
'''
Inputs:
activitiy_suffix: string that follows activities you want, for example 'spatialSim' for activitiies in folder 'activities_spatialSim'
LD_directory_suffix: the suffix for the folder in which to save linear decoded images, aka 'LD_images'
im_width: the size of one side of the images (code only handles square images for now but this could be easily modified)
cell_type_list: numpy array of index of specific cell types you want decoded (corresponding to cell_type_inds.npy), for example np.asarray(0,1) if you want to grab all cell types identified as 0 or 1
create_decoded_images: True if you want to create all training/validation/testing decoded images automatically, false to just create and save linear decoder weights
Outputs:
Computes and saves linear decoder weights
Saves decoded images for training/validation/testing if create_decoded_images is True
'''
# -------------------------------------
# CREATE MODEL SAVE DIRECTORY/LOG FILE
# -------------------------------------
if cell_type_list is not None: # if using specific cell types
cell_type_string = str(cell_type_list).replace(' ','').replace('.','').replace('[','').replace(']','') # make cell type list concatenated string
directory_name = linear_decoder_save_path+'/linear_decoder_'+activity_suffix+'_'+cell_type_string+''
else:
directory_name = linear_decoder_save_path+'/linear_decoder_'+activity_suffix+''
if not os.path.exists(directory_name):
os.makedirs(directory_name)
os.makedirs(directory_name+'/model')
os.makedirs(directory_name+'/logs')
log_file_name = directory_name + '/logs/LD.txt'
with open(log_file_name, "a") as log_file:
log_file.write('training starting \n')
# -----------------------------
# LOAD TRAINING DATA GENERATOR
# -----------------------------
data_type='training'
activities_dir_path = data_dir+data_type+'/activities_'+activity_suffix+'/'
original_dir_path = data_dir+data_type+'/images/'
data_generator = ds_x_activities_y_original(batch_size=total_block_size, im_width=128, activities_dir_path=activities_dir_path, original_dir_path=original_dir_path, image_mean=immean, normalize_scale=True, shuffle=False)
# -----------------
# PARSE CELL TYPES
# -----------------
if cell_type_list is not None: # use specific cell types instead of all the data
cell_type_inds = np.load(cell_type_inds_path+'/cell_type_inds.npy')
which_n = np.in1d(cell_type_inds, cell_type_list) # specify a specific cell type
# ---------------------------------------
# CALCULATE SAMPLE XTX AND XTY STATISTICS
# ---------------------------------------
# Loop through all training blocks of data
stats_created=0
while True:
try:
# Generate activities (X)/ images(Y)
X, Y, block_num = next(data_generator)
Y = Y.reshape((-1,im_width*im_width))
# Prepare activities
if cell_type_list is not None:
X = X[:,which_n]
X = np.insert(X, 0, 1.0, axis=1) # insert column of bias terms
# Add this blocks statistics to overall ones
if stats_created == 0:
XTX = X.T.dot(X)
XTY = X.T.dot(Y)
stats_created = 1
else:
XTX += X.T.dot(X)
XTY += X.T.dot(Y)
with open(log_file_name, "a") as log_file:
log_file.write('Block number ' + str(block_num) + ' XTX/XTY created \n')
except StopIteration: # stop when generator has gone through all training data
break
# -------------------------------
# COMPUTE LINEAR DECODER WEIGHTS
# -------------------------------
XTX_inv = np.linalg.inv(XTX)
W = np.dot(XTX_inv, XTY)
# ----------------------------
# SAVE LINEAR DECODER WEIGHTS
# ----------------------------
if not cell_type_list: # if using all cells
model_path = directory_name+'/model/lineardecoder_W_'+activity_suffix+'.h5'
else:
cell_type_string = str(cell_type_list).replace(' ','').replace('.','').replace('[','').replace(']','') # make cell type list concatenated string
model_path = directory_name+'/model/lineardecoder_W_'+activity_suffix+'_CellType_'+cell_type_string+'.h5'
h5_temp = h5py.File(model_path, 'w')
h5_temp.create_dataset('data',
data=W)
h5_temp.close()
# ------------------------------------------------------
# CREATE DECODED IMAGES FOR TRAINING/VALIDATION/TESTING
# ------------------------------------------------------
if create_decoded_images:
data_type_vec = ['training','validation','testing']
# Loop over data types
for i_data_type in range(len(data_type_vec)):
data_type = data_type_vec[i_data_type]
# ---------------------
# LOAD DATA GENERATOR
# ---------------------
activities_dir_path = data_dir+data_type+'/activities_'+activity_suffix+'/'
original_dir_path = data_dir+data_type+'/images/'
data_generator = ds_x_activities_y_original(batch_size=total_block_size, im_width=128, activities_dir_path=activities_dir_path, original_dir_path=original_dir_path, image_mean=immean, normalize_scale=True, shuffle=False)
# --------------------------------
# MAKE DIRECTORY IF DOESN'T EXIST
# --------------------------------
if cell_type_list is not None: # if using specific cell types
cell_type_string = str(cell_type_list).replace(' ','').replace('.','').replace('[','').replace(']','') # make cell type list concatenated string
decoded_images_directory_name = ''+data_dir+data_type+'/decodedimages_cellType_'+cell_type_list+'_'+LD_directory_suffix+'/'
else:
decoded_images_directory_name = ''+data_dir+data_type+'/decodedimages_'+LD_directory_suffix+'/'
if not os.path.exists(decoded_images_directory_name):
os.makedirs(decoded_images_directory_name)
while True:
try:
# Generate activities (X)/ images(Y)
X, Y, block_num = next(data_generator)
# Prepare activities
if cell_type_list is not None:
X = X[:,which_n]
X = np.insert(X, 0, 1.0, axis=1) # insert column of bias terms
# Create decoded images
decoded_images = np.dot(X,W)
# Save decoded images
if not cell_type_list: # if using all cells
save_path =decoded_images_directory_name+str(block_num)+'_'+LD_directory_suffix+'.h5'
else:
cell_type_string = str(cell_type_list).replace(' ','').replace('.','').replace('[','').replace(']','') # make cell type list concatenated string
save_path =decoded_images_directory_name+str(block_num)+'_'+LD_directory_suffix+'_CellType_'+cell_type_list+'.h5'
h5_temp = h5py.File(save_path, 'w')
h5_temp.create_dataset('data',data=decoded_images)
h5_temp.close()
with open(log_file_name, "a") as log_file:
log_file.write('Block number ' + str(block_num) + ' '+data_type+' decoded images created \n')
except StopIteration: # stop when generator has gone through all training data
break