forked from BioMachineLearning/EPLNetworkImamCleland2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiOdorTest_extended.py
99 lines (81 loc) · 3.01 KB
/
multiOdorTest_extended.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
# Original Authors:
# Imam, Nabil; Cleland, Thomas [tac29 at cornell.edu];
# 2020
# https://senselab.med.yale.edu/ModelDB/showmodel.cshtml?model=261864#tabs-1
#
# Modified by Nik Dennler, 2022, [email protected]
#
# ATTENTION: Run with Python 2.7!
import time
import numpy as np
import re
import os
from lib.epl import EPL
from lib.OSN import OSN_encoding
from lib.readout import readout
from lib import plots
import pickle
pickle_files = './pickle_files'
print(os.path.isdir(pickle_files))
# Iterate over pickle_files with training/testing data for all experiments
for file in os.listdir(pickle_files):
if file[0]=='.':
continue
dst = pickle_files + "/" + file
print(dst)
results_dir = "./results/" + file[:-3] + "/"
print(results_dir)
if not os.path.exists(results_dir):
os.mkdir(results_dir)
name = dst.split('/')[-1].split('.')[0]
print(name)
# Load training and testing arrays
rf = open(dst, "rb")
trainingOdors = np.array(pickle.load(rf))
testOdors = np.array(pickle.load(rf))
rf.close()
nOdors = len(trainingOdors)
nTestPerOdor = len(testOdors)/nOdors
print("Number of odors to train = " + str(len(trainingOdors)))
print("Number of odors to test = " + str(len(testOdors)))
#Network initialization
nMCs = len(trainingOdors[0])
GCsPerNeurogenesis = 5
nGCs = nMCs*GCsPerNeurogenesis*nOdors #every MC has 5 GCs per odor
epl = EPL(nMCs, nGCs, GCsPerNeurogenesis)
#Sniff
def sniff(odor, learn_flag=0, nGammaPerOdor=5, gPeriod=40):
sensorInput = OSN_encoding(odor)
for j in range(0, nGammaPerOdor):
for k in range(0, gPeriod):
epl.update(sensorInput, learn_flag=learn_flag)
pass
epl.reset()
#Training
t1 = time.time()
for i in range(0, len(trainingOdors)):
print("Training odor " + str(i+1))
sniff(trainingOdors[i], learn_flag=1)
epl.GClayer.invokeNeurogenesis()
sniff(trainingOdors[i], learn_flag=0)
#Testing
for i in range(0, len(testOdors)):
sniff(testOdors[i], learn_flag=0)
if(i%10==0 and i!=0):
print(str(i) + " odors tested")
t2 = time.time()
print("Simulation Duration = " + str(t2-t1) + "s")
#Readout
sMatrix, odorClassification, netClassification = readout(epl.gammaCode, nOdors, nTestPerOdor)
# Save outputs
np.save(results_dir + "sMatrix.npy", np.array(sMatrix))
np.save(results_dir + "odorClassification.npy", np.array(odorClassification))
np.save(results_dir + "netClassification.npy", np.array(netClassification))
np.save(results_dir + "gammaCode.npy", np.array(epl.gammaCode))
#Plots
plots.plotFigure4a(epl.gammaCode, results_dir)
plots.plotFigure4b(sMatrix, results_dir)
plots.plotFigure4d(epl.gammaCode, sMatrix, results_dir)
print("done")
print(odorClassification)
print(netClassification)