-
Notifications
You must be signed in to change notification settings - Fork 5
/
NeuroPack.py
1348 lines (1122 loc) · 56.6 KB
/
NeuroPack.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PyQt5 import QtGui, QtCore, QtWidgets
import sys
import os
import numpy as np
import json
import re
import imp
import pkgutil
import time
import pyqtgraph as pg
import arc1pyqt.Globals.fonts as fonts
import arc1pyqt.Globals.styles as styles
from arc1pyqt.VirtualArC import VirtualArC
from arc1pyqt.VirtualArC import pulse as VirtualArCPulse
from arc1pyqt.VirtualArC import read as VirtualArCRead
from arc1pyqt.VirtualArC.parametric_device import ParametricDevice as memristor
from arc1pyqt.Globals import functions
from arc1pyqt import state
HW = state.hardware
APP = state.app
CB = state.crossbar
from arc1pyqt import modutils
from arc1pyqt.modutils import BaseThreadWrapper, BaseProgPanel, \
makeDeviceList, ModTag
THIS_DIR = os.path.dirname(__file__)
for ui in ['nnanalysis', 'nnvarsnaprow']:
modutils.compile_ui(os.path.join(THIS_DIR, 'uis', '%s.ui' % ui),
os.path.join(THIS_DIR, '%s.py' % ui))
from .nnanalysis import Ui_NNAnalysis
from .nnvarsnaprow import Ui_NNVarSnapRow
from . import NeuroCores
def _log(*args, **kwargs):
if bool(os.environ.get('NNDBG', False)):
print(*args, file=sys.stderr, **kwargs)
class NetworkState(object):
"""
NetworkState stores all the information for the state of the
training process. All history is available.
"""
def __init__(self, NETSIZE, DEPTH, inputNum, outputNum, epochs, epochsForTesting, temporalCoding_enable, spikeTrain, labelCounter=1):
super(NetworkState, self).__init__()
self.weight_addresses = []
#self.weights = np.array(NETSIZE*[NETSIZE*[epochs*labelCounter*[0.0]]])
self.weights = np.array((NETSIZE - outputNum) * [(NETSIZE - inputNum) * [epochs * labelCounter * [0.0]]])
#self.R = np.array((NETSIZE-outputNum) * [(NETSIZE - inputNum) * [4 * epochs * labelCounter * [0.0]]])
#self.weightsExpected =np.array(NETSIZE*[NETSIZE*[epochs*labelCounter*[0.0]]])
self.weightsExpected =np.array((NETSIZE-outputNum) * [outputNum * [epochs * labelCounter * [0.0]]])
#self.weightsError = np.array(NETSIZE*[NETSIZE*[epochs*labelCounter*[0.0]]])
self.weightsError = np.array((NETSIZE-outputNum) * [outputNum * [epochs * labelCounter * [0.0]]])
self.NeurAccum = np.zeros(shape=(epochs, (NETSIZE - inputNum)))
self.NeurAccumForTest = np.zeros(shape=(epochsForTesting, (NETSIZE - inputNum))) #New for test
self.fireCells = np.array(epochs * labelCounter * [NETSIZE * [0.0]])
self.fireCellsForTest = np.array(epochsForTesting*labelCounter*[NETSIZE*[0.0]]) #New for test
self.fireHist = np.array((DEPTH+1) * [NETSIZE * [0.0]])
self.fireHistForTest = np.array((DEPTH+1)*[NETSIZE*[0.0]]) #New for test
self.firingCells = np.array(NETSIZE * [0.0])
self.firingCellsPseudo = np.array(NETSIZE*[0.0])
self.outputFlag = 0
self.neuronFixed = 0
self.fixedNeuronID = -1
self.voltMax = np.array((NETSIZE - inputNum)*[0.0])
self.voltMaxForTest = np.array(NETSIZE*[0.0])
self.tMax = 0
self.NeurRecov = np.zeros(shape=(epochs, NETSIZE))
self.NeurRecovForTest = np.zeros(shape=(epochsForTesting, NETSIZE))
self.fixedRandomWeights = np.random.rand(NETSIZE, NETSIZE)
self.spikeTrain_cnt = 0
self.errorSteps_cnt = 0
self.errorStepsForTest_cnt = 0
self.lastSpikeTrain = 0
self.temporalCoding_enable = temporalCoding_enable
self.spikeTrain = spikeTrain
if self.temporalCoding_enable == 1:
self.errorSteps = epochs // self.spikeTrain
self.errorStepsForTest = epochsForTesting // self.spikeTrain
else:
self.errorSteps = epochs
self.errorStepsForTest = epochsForTesting
self.errorList = np.zeros(shape=(self.errorSteps, NETSIZE))
self.errorListForTest = np.zeros(shape=(self.errorStepsForTest, NETSIZE))
class Network(BaseThreadWrapper):
"""
This is the abstract represantation of the network. It includes all
information about the training process, such as potentiation and
depression potential, number of epochs, batch size, the connection matrix (mapping of
neurons to devices), the size of the network (`NETSIZE`), number of neurons in each layer,
and training mode, etc.
Network parameters are typically loaded from the network configuration file
which is a JSON file including all the necessary arguments to initialise the
network. The following arguments are always necessary, regardless whether they
are used by the core or not
NETSIZE: network size (number of neurons)
DEPTH: network depth (layers of neurons)
Check `NeuroData/NeuroBase.json` for the absolute minimal configuration of a
network. Arbitrary information can be included in the JSON file. For instance
see `NeuroData/SevenMotif.json` for a configuration file with additional data.
All JSON parameters are exposed to the core through the `Network.params` field.
The connection matrix (`conn_mat`) maps neuros to devices and marks them as
either excitatory or inhibitory. See `NeuroData/motif_connmat.txt` for an
example.
Argument `stimin` denotes forced stimulus of neurons. See file
`NeuroData/motif_stim.txt` for an example of such file. Essentially it denotes
at which timestamp certain neurons are forced to fire, regardless of their
current state. Forced and induced stimulus is typically aggregated to find out
the full stimulus status, although this is a process done by the network core.
Evolution of the training process is guided almost fully by the network
cores (which are placed under `NeuroCores`). This class does not do much
apart from calling the core functions for each timestep and saving the data
at the end of the process.
It also exposes some convenience functions such as `read` and `pulse` that
core implementers can call instead of fiddling with ArC1 internals.
List of core-accessible values and functions
* `Network.ConnMat`: The current connection matrix
* `Network.stimin`: Forced training stimulus as loaded from the stimulus file
* `Network.stiminForTesting`: Forced test stimulus as loaded from the stimulus file
* `Network.testEnable`: The signal indicating whether test is enabled
* `Network.onlineEnable`: The signal indicating whether online training is enabled
* `Network.epochs`: Total number of training iterations
* `Network.epochsForTesting`: Total number of test iterations
* `Network.NETSIZE`: Size of the network
* `Network.DEPTH`: Depth of the network
* `Network.layers`: List of numbers of neurons in each layer
* `Network.params`: This is a dict containing all JSON parameters as
picked up from the configuration file.
* `Network.state`: Complete history of weight and accumulators as well
as calculated neuron firings. History state population is responsibility
of the core.
* `Network.RTolerance` : tolerance ratio in weight updating
* `Network.pulse(w, b, A, pw)`: Function used to pulse device `(w,b)` with
a voltage pulse of amplitude `A` and pulse width `pw` (volts and seconds).
This will immediately return the new resistance of the device.
* `Network.read(w, b)`: Read-out device `(w,b)` and get its resistance.
"""
def __init__(self, conn_mat, stimin, stiminForTesting, test_enable, data, params, tsteps, testSteps, core, labelCounter=1):
super(Network, self).__init__()
self.ConnMat = conn_mat
self.stimin = stimin
self.stiminForTesting = stiminForTesting
self.testEnable = test_enable
self.Ap = data["Ap"]
self.An = data["An"]
self.a0p = data["a0p"]
self.a1p = data["a1p"]
self.a0n = data["a0n"]
self.a1n = data["a1n"]
self.tp = data["tp"]
self.tn = data["tn"]
self.epochs = data["epochs"]
self.epochsForTesting = data["epochsForTesting"]
self.filename = data["fname"]
print('Ap:%f, An:%f, a0p:%f, a0n:%f, a1p:%f, a1n:%f, tp:%f, tn:%f'%(self.Ap, self.An, self.a0p, self.a0n, self.a1p, self.a1n, self.tp, self.tn))
# pop the core parameters into distinct fields
self.NETSIZE = params.pop("NETSIZE")
self.DEPTH = params.pop("DEPTH")
self.Pattern_epoch = params.pop("PATTERN_EPOCH")
self.neuronLock_enable = params.pop('NEURONLOCK_ENABLE')
self.temporalCoding_enable = params.pop('TEMPORALCODING_ENABLE')
self.spikeTrain = params.pop('SPIKETRAIN', 1)
self.layers = params.pop('LAYER')
self.inputNum = self.layers[0]
self.outputNum = self.layers[-1]
self.prefixSum_layers = []
self.prefixSum_layers.append(self.layers[0])
for i in range(1, len(self.layers)):
self.prefixSum_layers.append(self.prefixSum_layers[i - 1] + self.layers[i])
self.dt = params.pop("dt")
self.initR = params.pop("INITRES")
self.variation = params.pop("DEVICEINITVARIATION")
self.pos_voltOfPulseList = params.pop("POSVOLTOFPULSELIST")
self.pos_pulsewidthOfPulseList = params.pop("POSPULSEWIDTHOFPULSELIST")
self.pos_pulseList = list(zip(self.pos_voltOfPulseList, self.pos_pulsewidthOfPulseList))
self.neg_voltOfPulseList = params.pop("NEGVOLTOFPULSELIST")
self.neg_pulsewidthOfPulseList = params.pop("NEGPULSEWIDTHOFPULSELIST")
self.neg_pulseList = list(zip(self.neg_voltOfPulseList, self.neg_pulsewidthOfPulseList))
# and bundle the rest under self.params
self.RTolerance = params.pop('RTOLERANCE')
self.maxSteps = params.pop('MAXUPDATESTEPS')
self.params = params
self.tsteps = tsteps
self.testSteps = testSteps
self.rawin = np.array(self.NETSIZE*[0])
self.rawin_pseudo = np.array(self.NETSIZE*[0])
self.neuronLocked = 0
self.lockedNeuronID = -1
self.Vread = HW.conf.Vread
self.state = NetworkState(self.NETSIZE, self.DEPTH, self.inputNum, self.outputNum, self.epochs, self.epochsForTesting, self.temporalCoding_enable, self.spikeTrain, labelCounter)
self.plot_counter_trigger = 100
self.plot_counter = 0
self.spikeTrainStep = 0
self.core = self.load_core(core)
def log(self, *args, **kwargs):
""" Write to stderr if CTSDBG is set"""
_log(*args, **kwargs)
def load_core(self, corename):
from pkgutil import iter_modules
import importlib
basecoremod = 'arc1pyqt.ExtPanels.NeuroPack.NeuroCores'
for (finder, name, ispkg) in iter_modules(NeuroCores.__path__):
loader = finder.find_module(name)
if name == corename:
mod = importlib.import_module('%s.%s' % (basecoremod, name))
return mod
def custom_init(self):
if not isinstance(HW.ArC, VirtualArC):
return
#HW.ArC.crossbar = [[] for x in range(100+1)]
HW.ArC.crossbar = [[] for x in range(500+1)] # to test the hidden layer
#for w in range(100+1):
for w in range(500+1):
#HW.ArC.crossbar[w].append(0)
#for b in range(100+1):
for b in range(500+1):
mx=memristor(Ap=self.Ap, An=self.An, tp=self.tp, tn=self.tn, a0p=self.a0p, a0n=self.a0n, a1p=self.a1p, a1n=self.a1n)
mx.initialise(self.initR + (np.random.rand()-0.5)*self.variation)
HW.ArC.crossbar[w].append(mx)
#functions.updateHistory(w, b, mx.Rmem, self.Vread, 0.0, 'S R')
#functions.displayUpdate.cast()
@BaseThreadWrapper.runner
def run(self):
self.disableInterface.emit(True)
self.log("Reading all devices and initialising weights")
self.custom_init()
# f = open("C:/Users/jh1d18/debug_log.txt", "a")
# For every neuron in the system.
# for postidx in range(len(self.ConnMat)):
# For every presynaptic input the neuron receives.
# for preidx in np.where(self.ConnMat[:, postidx, 0] != 0)[0]:
# w, b=self.ConnMat[preidx, postidx, 0:2]
# r = self.read(w,b)
# f.write('device RS: %f, w: %d, b: %d\n' % (r, w, b))
# f.close()
# self.state.weights[preidx, postidx - self.inputNum, 0] = 1.0/self.read(w, b)
# store device address and neuron ids for easy access in
# history_panel
# self.state.weight_addresses.append([[w,b],[preidx,postidx]])
# self.log("Done.")
print("Starting Neural Net simulator")
# Start Neural Net training
self.core.init(self)
pattern_epoch_cnt = 0
print('start training!')
for t in range(self.tsteps):
self.rawin = self.state.firingCells
self.rawinPseudo = self.state.firingCellsPseudo
if pattern_epoch_cnt == self.Pattern_epoch and self.neuronLock_enable == 1:
self.neuronLocked = 0
self.lockedNeuronID = -1
pattern_epoch_cnt = 0
else:
self.neuronLocked = self.state.neuronFixed
self.lockedNeuronID = self.state.fixedNeuronID
self.log("---> Time step neuron update in trianing: %d RAWIN: %s STIMIN: %s RAWINPSEUDO: %s" % (t, self.rawin, self.stimin[:, t], self.rawinPseudo))
self.core.neurons(self, t, phase = 'training')
if self.neuronLock_enable:
pattern_epoch_cnt += 1
self.rawin = self.state.firingCells
self.rawinPseudo = self.state.firingCellsPseudo
self.log("---> Time step synapses update in trianing: %d RAWIN: %s STIMIN: %s RAWINPSEUDO: %s" % (t, self.rawin, self.stimin[:, t], self.rawinPseudo))
self.core.plast(self, t)
self.displayData.emit()
print('testenable in Network: ', self.testEnable)
if self.testEnable == 1:
print('start testing!')
self.weightsForTest = self.state.weights[:, :, self.tsteps - 1]
for t in range(self.testSteps):
self.rawin = self.state.firingCells
self.rawinPseudo = self.state.firingCellsPseudo
self.log("---> Time step synapses update in trianing: %d RAWIN: %s STIMIN: %s RAWINPSEUDO: %s" % (t, self.rawin, self.stimin[:, t], self.rawinPseudo))
self.core.neurons(self, t, phase = 'test')
self.displayData.emit()
self.log("Final reading of all devices")
# For every neuron in the system.
for postidx in range(len(self.ConnMat)):
# For every presynaptic input the neuron receives.
for preidx in np.where(self.ConnMat[:, postidx, 0] != 0)[0]:
w,b=self.ConnMat[preidx, postidx, 0:2]
self.read(w, b)
print('fireHistForTest: ', self.state.fireCellsForTest)
# Save data if so requested
if self.filename is not None:
data = {}
# metadata; this is a numpy structured array
meta = np.array([(self.epochs, self.epochsForTesting, self.NETSIZE, self.DEPTH, self.inputNum, self.layers, self.Ap, self.An, self.tp, self.tn, self.a0p, self.a0n, self.a1p, self.a1n)],
dtype=[('trials', 'u8'), ('trialsForTesting', 'u8'), ('netsize', 'u8'), ('depth', 'u8'), ('inputNum', 'u8'), ('layers', 'O'), ('Ap', 'f4'), ('An', 'f4'),
('tp', 'f4'), ('tn', 'f4'), ('a0p', 'f4'), ('a0n', 'f4'), ('a1p', 'f4'), ('a1n', 'f4')])
data['meta'] = meta
# standard data first
# all weights
data['weights'] = self.state.weights
data['weightsExpected'] = self.state.weightsExpected
data['weightsError'] = self.state.weightsError
# calculated stimuli for each step
data['stimulus'] = self.stimin
data['stimulusForTest'] = self.stiminForTesting
# history of cells that have fired
data['fires'] = self.state.fireCells.T
data['firesForTest'] = self.state.fireCellsForTest.T
# accumulator snapshots
data['accumulator'] = self.state.NeurAccum.T
data['accumulatorForTest'] = self.state.NeurAccumForTest.T
data['membraneRecoveryVariable'] = self.state.NeurRecov.T
data['membraneRecoveryVariableForTest'] = self.state.NeurRecovForTest.T
# error between outputs and labels. No error for unsupervised learning
data['error'] = self.state.errorList
data['errorForTest'] = self.state.errorListForTest
#data['R'] = self.state.R
# and then any other arrays the core has produced
additional_data = self.core.additional_data(self)
if additional_data is not None:
for (k, v) in self.core.additional_data(self):
data[k] = v
np.savez_compressed(self.filename, **data)
self.disableInterface.emit(False)
self.finished.emit()
def read(self, w, b):
# read a device and return read value
# update interface
self.highlight.emit(w, b)
#Mnow = HW.ArC.read_one(w, b)
Mnow = VirtualArCRead(HW.ArC.crossbar, w, b)
self.sendData.emit(w, b, Mnow, self.Vread, 0, \
'S R%d V=%.1f' % (HW.conf.readmode, HW.conf.Vread))
self.updateTree.emit(w, b)
return Mnow
def pulse(self, w, b, A, pw):
# apply a pulse and return
# can instead apply any voltage series
self.highlight.emit(w,b)
#Mnow = HW.ArC.pulseread_one(w, b, A, pw)
VirtualArCPulse(HW.ArC.crossbar, w, b, A, pw, self.dt)
Mnow = VirtualArCRead(HW.ArC.crossbar, w, b)
self.sendData.emit(w, b, Mnow, A, pw, 'P')
self.updateTree.emit(w, b)
return Mnow
class NeuroPack(BaseProgPanel):
def __init__(self, short=False):
super().__init__(title="NeuroPack",
description="Flexible neural nets", short=short)
self.short = short
self.base_conf_fname = None
self.conn_matrix_fname = None
self.stim_file_fname = None
self.test_file_fname = None
self.output_file_fname = None
self.test_enable = 0
self.initUI()
fname = os.path.join(THIS_DIR, "NeuroData", "NeuroBase.json")
params = self.load_base_conf(os.path.join(THIS_DIR, "NeuroData",\
"NeuroBase.json"))
self.apply_base_conf(params, os.path.basename(fname), fname)
# def execute(self, wrapper, entrypoint=None, deferredUpdate=False, signals=True):
# """
# This function schedules a wrapper for execution taking care of the
# standard signals. The wrapped action (`wrapper`) will be passed
# along a thread which will call the `entrypoint` function of
# `wrapper`. If `entrypoint` is None the default `wrapper.run`
# entrypoint will be used. Argument `deferredUpdate` prevents the history
# tree from updating until the thread operation has finished. This can
# be useful in situations where multiple different devices are used or
# when a module uses many individual operations that would otherwise
# trigger a tree update (for instance hundreds of reads/pulses over
# ten different devices).
# """
# if (HW.ArC is None) or (self.thread is not None):
# return
# if entrypoint is None:
# entrypoint = wrapper.run
# self.threadWrapper = wrapper
# self.thread = QtCore.QThread()
# # When deferring tree updates store current point in history for the
# # whole crossbar. Once the operation is finished the history tree will
# # then be populated starting from this point in history
# if deferredUpdate:
# for (r, row) in enumerate(CB.history):
# for (c, col) in enumerate(row):
# self._deferredUpdates['%d%d' % (r, c)] = (r, c, len(col))
# self.threadWrapper.moveToThread(self.thread)
# self.thread.started.connect(entrypoint)
# self.threadWrapper.finished.connect(self.thread.quit)
# if signals:
# #self.threadWrapper.sendData.connect(functions.updateHistory)
# #self.threadWrapper.highlight.connect(functions.cbAntenna.cast)
# #self.threadWrapper.displayData.connect(functions.displayUpdate.cast)
# if not deferredUpdate:
# self.threadWrapper.updateTree.connect(\
# functions.historyTreeAntenna.updateTree.emit)
# self.threadWrapper.disableInterface.connect(functions.interfaceAntenna.cast)
# self.thread.finished.connect(partial(self._onThreadFinished, deferredUpdate, signals))
# self.thread.start()
def initUI(self):
vbox1=QtWidgets.QVBoxLayout()
titleLabel = QtWidgets.QLabel('NeuroPack')
titleLabel.setFont(fonts.font1)
descriptionLabel = QtWidgets.QLabel('Flexible neural net application module.')
descriptionLabel.setFont(fonts.font3)
descriptionLabel.setWordWrap(True)
isInt=QtGui.QIntValidator()
isFloat=QtGui.QDoubleValidator()
gridLayout = QtWidgets.QGridLayout()
gridLayout.setColumnStretch(0, 3)
gridLayout.setColumnStretch(1, 1)
gridLayout.setColumnStretch(2, 1)
gridLayout.setColumnStretch(3, 1)
#setup a line separator
lineLeft=QtWidgets.QFrame()
lineLeft.setFrameShape(QtWidgets.QFrame.VLine)
lineLeft.setFrameShadow(QtWidgets.QFrame.Raised)
lineLeft.setLineWidth(1)
gridLayout.addWidget(lineLeft, 0, 2, 2, 1)
################################################ LOAD ##############
self.push_load_base_conf = QtWidgets.QPushButton("Load Base conf.")
self.push_load_base_conf.clicked.connect(self.open_base_conf)
self.base_conf_filename = QtWidgets.QLabel("Load Base conf.")
gridLayout.addWidget(self.push_load_base_conf, 0, 1)
gridLayout.addWidget(self.base_conf_filename, 0, 0)
self.push_load_conn_matrix = QtWidgets.QPushButton("Load Conn. Matrix")
self.push_load_conn_matrix.clicked.connect(self.open_conn_matrix)
self.matrix_filename = QtWidgets.QLabel("Load Conn. Matrix")
gridLayout.addWidget(self.push_load_conn_matrix, 1, 1)
gridLayout.addWidget(self.matrix_filename, 1, 0)
self.push_load_stim_file = QtWidgets.QPushButton("Load Stim. File")
self.push_load_stim_file.clicked.connect(self.open_stim_file)
self.stim_filename = QtWidgets.QLabel("Load Stim. File")
gridLayout.addWidget(self.push_load_stim_file, 2 ,1)
gridLayout.addWidget(self.stim_filename, 2, 0)
self.check_test_file = QtWidgets.QCheckBox("Load Test File")
self.check_test_file.clicked.connect(self.check_test_file_clicked)
self.test_filename = QtWidgets.QPushButton("Load Test File")
self.test_filename.clicked.connect(self.open_test_file)
self.test_filename.setEnabled(False)
gridLayout.addWidget(self.check_test_file, 3, 0)
gridLayout.addWidget(self.test_filename, 3, 1)
self.check_save_data = QtWidgets.QCheckBox("Save to:")
self.check_save_data.clicked.connect(self.check_save_data_clicked)
self.push_save_filename = QtWidgets.QPushButton("No file selected")
self.push_save_filename.clicked.connect(self.load_output_file)
self.push_save_filename.setEnabled(False)
gridLayout.addWidget(self.check_save_data, 5, 0)
gridLayout.addWidget(self.push_save_filename, 5, 1)
self.push_show_analysis_tool = QtWidgets.QPushButton("Start analysis tool")
self.push_show_analysis_tool.clicked.connect(self.startAnalysisTool)
gridLayout.addWidget(self.push_show_analysis_tool, 9, 0, 1, 2)
####################################################################
################################################## CORES ###########
self.rulesCombo = QtWidgets.QComboBox()
for _, name, is_pkg in pkgutil.iter_modules(NeuroCores.__path__):
if not is_pkg and name.startswith("core_"):
self.rulesCombo.addItem(name.replace("core_", ""), name)
gridLayout.addWidget(QtWidgets.QLabel("Network core:"), 4, 0)
gridLayout.addWidget(self.rulesCombo, 4, 1)
####################################################################
leftLabels=['Trials for training',\
'Trials for testing']
self.leftEdits=[]
rightLabels=['Ap',\
'An',\
'a0p',\
'a0n',\
'a1p',\
'a1n',\
'tp',\
'tn'
]
self.rightEdits=[]
leftInit= ['10000',\
'10000']
rightInit = ['0.21388644421061628',\
'-0.813018367268805',\
'37086.67218413958',\
'43430.02023698205',\
'-20193.23957579438',\
'34332.85303661032',\
'1.6590989889370842',\
'1.5148294827972748'
]
#setup a line separator
lineLeft = QtWidgets.QFrame()
lineLeft.setFrameShape(QtWidgets.QFrame.VLine);
lineLeft.setFrameShadow(QtWidgets.QFrame.Raised);
lineLeft.setLineWidth(1)
gridLayout.addWidget(lineLeft, 0, 2, 11, 1)
for i in range(len(leftLabels)):
lineLabel=QtWidgets.QLabel()
lineLabel.setText(leftLabels[i])
gridLayout.addWidget(lineLabel, i+6,0)
lineEdit=QtWidgets.QLineEdit()
lineEdit.setText(leftInit[i])
lineEdit.setValidator(isFloat)
self.leftEdits.append(lineEdit)
gridLayout.addWidget(lineEdit, i+6,1)
for i in range(len(rightLabels)):
lineLabel=QtWidgets.QLabel()
lineLabel.setText(rightLabels[i])
gridLayout.addWidget(lineLabel, i,4)
lineEdit=QtWidgets.QLineEdit()
lineEdit.setText(rightInit[i])
lineEdit.setValidator(isFloat)
self.rightEdits.append(lineEdit)
gridLayout.addWidget(lineEdit, i,5)
self.Ap=float(self.rightEdits[0].text())
self.An=float(self.rightEdits[1].text())
self.a0p=float(self.rightEdits[2].text())
self.a0n=float(self.rightEdits[3].text())
self.a1p=float(self.rightEdits[4].text())
self.a1n=float(self.rightEdits[5].text())
self.tp=float(self.rightEdits[6].text())
self.tn=float(self.rightEdits[7].text())
self.epochs=int(self.leftEdits[0].text())
self.epochsForTesting=int(self.leftEdits[1].text())
################################################ LTD/LTP ###########
vbox1.addWidget(titleLabel)
vbox1.addWidget(descriptionLabel)
self.vW=QtWidgets.QWidget()
self.vW.setLayout(gridLayout)
self.vW.setContentsMargins(0,0,0,0)
scrlArea=QtWidgets.QScrollArea()
scrlArea.setWidget(self.vW)
scrlArea.setContentsMargins(0,0,0,0)
scrlArea.setWidgetResizable(False)
scrlArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scrlArea.installEventFilter(self)
vbox1.addWidget(scrlArea)
vbox1.addStretch()
if not self.short:
self.hboxProg = QtWidgets.QHBoxLayout()
push_train = QtWidgets.QPushButton('Train Network')
push_train.setStyleSheet(styles.btnStyle)
push_train.clicked.connect(self.runTrain)
self.hboxProg.addWidget(push_train)
vbox1.addLayout(self.hboxProg)
self.labelCounter=1
self.setLayout(vbox1)
self.gridLayout=gridLayout
# def update_learning_rule(self):
# pass
def gather_data(self):
if self.check_save_data.isChecked():
fname = self.output_file_fname
else:
fname = None
return { \
"Ap": float(self.rightEdits[0].text()), \
"An": float(self.rightEdits[1].text()), \
"a0p": float(self.rightEdits[2].text()),\
"a0n": float(self.rightEdits[3].text()),\
"a1p": float(self.rightEdits[4].text()),\
"a1n": float(self.rightEdits[5].text()),\
"tp": float(self.rightEdits[6].text()),\
"tn": float(self.rightEdits[7].text()),\
"epochs": int(self.leftEdits[0].text()),\
"epochsForTesting": int(self.leftEdits[1].text()),\
"fname": fname
}
def gather_params(self):
return json.load(open(self.base_conf_fname))
def runTrain(self):
def _check_output_file(fname):
if fname is None:
return False
if os.path.exists(fname) and os.stat(fname).st_size > 0:
btns = QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No
text = "File exists and is of non-zero size. Overwrite?"
reply = QtWidgets.QMessageBox.question(self, "File exists", \
text, btns)
if reply == QtWidgets.QMessageBox.Yes:
return True
return False
else:
return True
if (self.conn_matrix_fname is None) or (self.stim_file_fname is None):
errMessage = QtWidgets.QMessageBox()
errMessage.setText("No connection matrix or stimulus file")
errMessage.setIcon(QtWidgets.QMessageBox.Critical)
errMessage.setWindowTitle("Error")
errMessage.exec_()
return
data = self.gather_data()
params = self.gather_params()
if not _check_output_file(data['fname']):
data['fname'] = None
# epochs times the nr of time steps
# that are defined in the stimulus file
tsteps = data["epochs"] * self.labelCounter
testSteps = data["epochsForTesting"]
# Reload the stimulus file to account for any changes in the epochs
# Could possibly check if the field is "tainted" before loading to
# avoid accessing the file again
self.stimin = self.load_stim_file(self.stim_file_fname, \
params["NETSIZE"], data["epochs"])
self.stiminForTesting = self.load_test_file(self.test_file_fname, \
params["NETSIZE"], data["epochsForTesting"])
if HW.ArC is not None:
coreIdx = self.rulesCombo.currentIndex()
coreName = self.rulesCombo.itemData(coreIdx)
print('test_enable before calling network:', self.test_enable)
network = Network(self.ConnMat, self.stimin, self.stiminForTesting, self.test_enable, data, params, \
tsteps, testSteps, coreName, self.labelCounter)
self.execute(network, network.run, True)
def load_base_conf(self, fname):
return json.load(open(fname))
def open_base_conf(self):
path = QtCore.QFileInfo(QtWidgets.QFileDialog().getOpenFileName(self,\
'Open base configuration', THIS_DIR, filter="*.json")[0])
name = path.fileName()
try:
data = self.load_base_conf(path.filePath())
for x in ["NETSIZE", "DEPTH"]:#!!!!!!!!!!!!
if x not in data.keys():
errMessage = QtWidgets.QMessageBox()
errMessage.setText("Missing required parameter %s" % x)
errMessage.setIcon(QtWidgets.QMessageBox.Critical)
errMessage.setWindowTitle("Error")
errMessage.exec_()
return
self.apply_base_conf(data, name, path.filePath())
except Exception as exc:
_log("!!", exc)
def apply_base_conf(self, params, name, path):
self.base_conf_fname = str(path)
self.base_conf_filename.setText(name)
if self.conn_matrix_fname is not None:
data = self.gather_data()
res = self.load_conn_matrix(self.conn_matrix_fname,\
params["NETSIZE"])
self.apply_conn_matrix(res)
if self.stim_file_fname is not None:
data = self.gather_data()
stims = self.load_stim_file(self.stim_file_fname, params["NETSIZE"],\
data["epochs"])
self.apply_stim_file(stims)
if self.test_file_fname is not None:
data = self.gather_data()
stims = self.load_test_file(self.test_file_fname, params["NETSIZE"],\
data["epochsForTesting"])
self.apply_test_file(stims)
def load_stim_file(self, fname, NETSIZE, epochs):
_log("Allocating stimin")
stimin = np.array(NETSIZE*[epochs*[0]])
with open(fname, 'r') as f:
_log("File opened")
for line in f:
line = line.strip()
if (line[0] != "\n") and (line[0] != "#"):
# split into timestamp - list of neurons IDs scheduled to spike
timestamp, neuronIDs = re.split("\s*-\s*", line)
timestamp = int(timestamp)
if timestamp >= epochs:
break
_log(timestamp, neuronIDs)
# split the string into an int list of neurons
spikeNeuronID = [int(x) - 1 for x in re.split("\s*,\s*", neuronIDs.strip())]
for i, spiker in enumerate(spikeNeuronID):
stimin[spiker, timestamp] = 1
return stimin
def open_stim_file(self):
_log("Loading stimulation file...")
params = self.gather_params()
data = self.gather_data()
path = QtCore.QFileInfo(QtWidgets.QFileDialog().getOpenFileName(self,\
'Open stimulation file', THIS_DIR, filter="*.txt")[0])
name = path.fileName()
error = False
try:
res = self.load_stim_file(path.filePath(), params["NETSIZE"], \
data["epochs"])
except Exception as exc:
_log(exc)
error = True
#print(self.stimin)
if error:
self.stimin = np.array(params["NETSIZE"]*[data["epochs"]*[0]])
errMessage = QtWidgets.QMessageBox()
errMessage.setText("Invalid network stimulation file! Possible problem with syntax.")
errMessage.setIcon(QtWidgets.QMessageBox.Critical)
errMessage.setWindowTitle("Error")
errMessage.exec_()
else:
self.apply_stim_file(res, name, path.filePath())
_log("done")
def apply_stim_file(self, stim, name=None, path=None):
self.stimin = stim
if name is not None:
self.stim_filename.setText(name)
if path is not None:
self.stim_file_fname = str(path)
def load_conn_matrix(self, fname, NETSIZE):
ConnMat = np.array(NETSIZE*[NETSIZE*[3*[0]]])
with open(fname, 'r') as f:
for line in f:
if (line[0] != "\n") and (line[0] != "#"):
preid, postid, w, b, type_of=line.split(", ")
ConnMat[int(preid) - 1, int(postid) - 1] = \
[int(w), int(b), int(type_of)]
return ConnMat
def open_conn_matrix(self):
_log("Loading connectivity matrix...")
params = self.gather_params()
# self.ConnMat=np.array(params["NETSIZE"]*[params["NETSIZE"]*[3*[0]]])
path = QtCore.QFileInfo(QtWidgets.QFileDialog().getOpenFileName(self,\
'Open connectivity matrix file', THIS_DIR, filter="*.txt")[0])
name=path.fileName()
error = False
try:
res = self.load_conn_matrix(path.filePath(), params["NETSIZE"])
except Exception as exc:
error = True
_log(exc)
if error:
self.ConnMat=np.array(params["NETSIZE"]*[params["NETSIZE"]*[3*[0]]])
errMessage = QtWidgets.QMessageBox()
errMessage.setText("Invalid connectivity matrix file! Possible problem with syntax.")
errMessage.setIcon(QtWidgets.QMessageBox.Critical)
errMessage.setWindowTitle("Error")
errMessage.exec_()
else:
# self.matrix_filename.setText(name)
# self.ConnMat = res
# self.conn_matrix_fname = path.filePath()
self.apply_conn_matrix(res, name, path.filePath())
_log("done")
def apply_conn_matrix(self, matrix, name=None, path=None):
self.ConnMat = matrix
if path is not None:
self.matrix_filename.setText(name)
if name is not None:
self.conn_matrix_fname = str(path)
def check_test_file_clicked(self, checked):
self.test_filename.setEnabled(checked)
if checked == True:
self.test_enable = 1
else:
self.test_enable = 0
print('test_enable in checked_save_data_clicked:', self.test_enable)
def load_test_file(self, fname, NETSIZE, epochsForTesting):
_log("Allocating test stimin")
_log('epochsForTesting when loading test file:', epochsForTesting)
stiminForTesting = np.array(NETSIZE*[epochsForTesting*[0]])
if fname is not None:
_log(fname)
with open(fname, 'r') as f:
_log("File opened")
for line in f:
line = line.strip()
if (line[0] != "\n") and (line[0] != "#"):
# split into timestamp - list of neurons IDs scheduled to spike
timestamp, neuronIDs = re.split("\s*-\s*", line)
timestamp = int(timestamp)
if timestamp >= epochsForTesting:
break
_log(timestamp, neuronIDs)
# split the string into an int list of neurons
spikeNeuronID = [int(x) - 1 for x in re.split("\s*,\s*", neuronIDs.strip())]
for i, spiker in enumerate(spikeNeuronID):
stiminForTesting[spiker, timestamp] = 1
return stiminForTesting
def open_test_file(self):
_log("Loading test file...")
params = self.gather_params()
data = self.gather_data()
path = QtCore.QFileInfo(QtWidgets.QFileDialog().getOpenFileName(self,\
'Open test file', THIS_DIR, filter="*.txt")[0])
name = path.fileName()
error = False
try:
res = self.load_test_file(path.filePath(), params["NETSIZE"], \
data["epochsForTesting"])
except Exception as exc:
_log(exc)
error = True
#print(self.stimin)
if error:
self.stiminForTesting = np.array(params["NETSIZE"]*[data["epochsForTesting"]*[0]])
errMessage = QtWidgets.QMessageBox()
errMessage.setText("Invalid network test file! Possible problem with syntax.")
errMessage.setIcon(QtWidgets.QMessageBox.Critical)
errMessage.setWindowTitle("Error")
errMessage.exec_()
else:
self.apply_test_file(res, name, path.filePath())
_log("done")
def apply_test_file(self, stim, name=None, path=None):
self.stiminForTesting = stim
if name is not None:
self.test_filename.setText(name)
if path is not None:
self.test_file_fname = str(path)
def check_save_data_clicked(self, checked):
self.push_save_filename.setEnabled(checked)
def load_output_file(self):
if self.output_file_fname is not None:
curpath = os.path.dirname(self.output_file_fname)
else:
curpath = ''
path = QtCore.QFileInfo(QtWidgets.QFileDialog().getSaveFileName(self,\
'Save to...', curpath, "Numpy arrays (*.npz)")[0])
fname = path.fileName()
if fname is None or len(fname) == 0:
if self.output_file_fname is None:
self.check_save_data.setChecked(False)
self.push_save_filename.setEnabled(False)
return
self.output_file_fname = path.filePath()
self.push_save_filename.setText(fname)
_log("Set output file to %s..." % path.fileName())
def startAnalysisTool(self):
self._analysisWindow = QtWidgets.QMainWindow()
self._analysisWindow.setWindowTitle("NeuroPack Analysis tool")
self._analysisWindow.setCentralWidget(NeuroAnalysis())
self._analysisWindow.show()
def eventFilter(self, object, event):
if event.type()==QtCore.QEvent.Resize:
self.vW.setFixedWidth(event.size().width() - \
object.verticalScrollBar().width())
return False
def disableProgPanel(self,state):
self.hboxProg.setEnabled(not state)
class NeuroVarSnapRowWidget(Ui_NNVarSnapRow, QtWidgets.QWidget):
def __init__(self, dataset, parent=None):
super(NeuroVarSnapRowWidget, self).__init__(parent=parent)
self.dataset = None
self.selected = False
self.setupUi(self)
self.plotWidget = None