Repository navigation
Expand file tree
/
Copy pathconstructs.h
More file actions
769 lines (565 loc) · 15.7 KB
/
Copy pathconstructs.h
File metadata and controls
769 lines (565 loc) · 15.7 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
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
//
// Version: $Id: constructs.h 170 2014-01-29 14:00:04Z gk $
//
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CONSTRUCTS_H__
#define __CONSTRUCTS_H__
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include "mtrand.h"
#include <algorithm>
#include <vector>
#include <fstream>
#include <map>
using namespace std;
enum PATTERN_MODE {
PATTERN_SEQ=0,
PATTERN_SIM=1
};
#define TYPE_CS 1 /* Conditional Stimulus */
#define TYPE_US 2
#define TYPE_NOISE 3
#define param_g_L 30.0
#define param_C 281.0
#define param_default_syn_weight 0.999
#define param_tau_wadapt_in 40.0
#define param_tau_wadapt_pyr 200.0
#define param_delta_t 0.2
#define param_thresh 20.0
#define param_E_L 0.0
#define param_V_rest param_E_L
#define param_beta (0.4)
#define param_alpha 0.2
#define param_tau_actvar 144.0 //msec
#define param_tau_branch 6.0
#define param_tau_soma 20.0
#define param_tau_calcium_syn 300.0 // msec
#define param_tau_tagrise 1000.0 // msec
#define param_tau_stag 300.0 // time constant for window during which eltp expression can be cancelled (sec)
#define param_tau_eltprise 300.0 // time required to express eltp in activated synapses
#define param_tau_iltprise 1800.0
#define param_tau_eltp (2.0*3600.)
#define param_tau_iltp (24.*3600.)
#define param_tau_trigger (15.*60.)
#define param_tau_proteinrate (0.5*3600.0) // ProteinRate tau
#define param_tau_iltpconsolidation (3600.0*3.0)
#define param_tau_proteinprod (.5*3600.0) // protein production
#define param_tau_protein_branch (2000.0) // degradation
#define param_tau_protein_neuron (2000.0) // degradation
#define param_tau_scaling (2.0*3600.0)
#define param_tau_branch_scaling (6.0*3600.0)
enum {
RUN_TRAINING = 1,
RUN_PRE = 2,
RUN_TEST = 3,
};
struct LANeuron;
struct LABranch;
const float GPROD_CUTOFF = 18.0;
const float BPROD_CUTOFF = 1.8;
inline double rgen()
{
return double(std::rand())/double(RAND_MAX);
}
/* Tracks independent synapses , synaptic tags and synaptic calcium concentration etc. */
struct LASynapse
{
int sid;
bool isPlastic;
float weight, iltp, eltp, pos, calcium, stag, trigger, ltag, stdpTag ;
int locks; // Lame
vector<float> weightHistory;
vector<float> tagHistory;
int tagTime;
float tagMax;
LANeuron* source_nrn;
LANeuron* target_nrn;
LABranch* target_branch;
LASynapse()
{
Reset();
locks =0;
}
~LASynapse()
{
Reset();
}
void Reset()
{
source_nrn = target_nrn =0;
target_branch = 0;
weight = param_default_syn_weight;
isPlastic = false;
iltp = ltag = stag = eltp = calcium =0.0;
pos = rgen();
sid = -1;
stdpTag =0;
tagTime =-1;
}
};
/* Tracks local branch depolarization, protein production, branch strength */
struct LABranch
{
int bid, branch_spikes ;
LANeuron* neuron;
float passiveWeight;
vector<LASynapse*> synapses;
float depol, depol2, bcalcium;
float protein, proteinRate, strength, dreset, strengthTag, dspike, totcalc;
int dspikeT;
float protein1, protein2;
vector<pair<float,float> > prpTransients;
vector<float> branchStrengthHistory; // For graphs
vector<float> branchProteinHistory; // For graphs
vector<float> branchVoltageHistory; // For graphs
vector<float> branchSpikesHistory; // For graphs
vector<float> branchCalciumHistory;
LABranch()
{
Reset();
}
~LABranch()
{
Reset();
}
void Reset()
{
bcalcium = depol =0.0;
bid = -1;
passiveWeight = 1.0;
bid = -1;
proteinRate = protein = 0.;
strength = 1.0;
strengthTag =0.0;
branch_spikes = 0;
dspike = dreset =0.0;
depol = depol2 =0.0;
protein1 = protein2 = 0.0;
totcalc =0.0;
dspikeT =0;
}
};
struct LANetwork;
/* Tracks somatic potential, somatic protein availability, spiking and backpropagating */
struct LANeuron
{
int nid, input_id, input_type;
char type;
float pos_x,pos_y,pos_z;
float glx, gly;
float V, w, crebLevel, protein, proteinRate, synScaling, totcalc;
int crebLevelT;
bool isSpiking;
float wadapt, actvar, vreset, vspike;
float stdp_x, branch_scaling;
float synapticWeightsInitialSum;
int lastSpikeT;
float exc_cur;
float inh_cur;
LANetwork* network;
vector<pair<float,float> > prpTransients;
vector<float> voltageHistory;
vector<int> spikeTimings;
vector<float> proteinHistory;
vector<float> crebHistory;
int total_spikes, dend_spikes;
vector<LABranch*> branches;
vector<LASynapse*> outgoing;
vector<LASynapse*> incoming;
LANeuron()
{
Reset();
}
~LANeuron()
{
Reset();
}
void Reset()
{
type = ' ';
w = crebLevel = protein = wadapt = actvar = vreset= 0.0;
nid = -1;
V = param_V_rest;
crebLevelT = -1;
pos_x = rgen();
pos_y = rgen();
pos_z = rgen();
stdp_x=0.0;
lastSpikeT =0;
protein = proteinRate = 0.0;
totcalc =0.0;
synScaling = total_spikes =0;
branch_scaling =0;
input_id = -1;
vspike =0.;
isSpiking = false;
dend_spikes =0;
exc_cur =inh_cur= 0;
}
};
/* Artificial spike generators for external inputs */
struct LAInput: public LANeuron
{
int* spikeTimes;
int curSpike;
int nextSpikeT;
int totalSpikes;
int groupIdx; // id of this neuron in the group representing a memory
LAInput()
{
LANeuron();
spikeTimes = 0;
groupIdx =-1;
}
~LAInput()
{
Reset();
}
void Reset()
{
curSpike = -1;
delete[] spikeTimes;
spikeTimes = 0;
totalSpikes =0;
nextSpikeT = -1;
}
int Program(int tstart, int duration, float freq, float randomness)
{
Reset();
int total = round((float(duration) * freq)/1000.0);
//printf("Programming %d spikes dur = %d freq=%f\n", total, duration, freq);
if (!total) return 0;
float period = 1000.0/freq;
spikeTimes = new int[total];
for (int i =0; i < total; i++)
{
spikeTimes[i] = tstart + period*i + rgen()*randomness*period;
}
this->curSpike =0;
this->nextSpikeT = this->spikeTimes[this->curSpike];
this->totalSpikes = total;
//cout << "Total spikes " << totalSpikes << endl;
return total;
}
int CopyShuffled(LAInput &other, float randomness)
{
Reset();
this->spikeTimes = new int[other.totalSpikes];
for (int i=0; i < other.totalSpikes; i++)
{
this->spikeTimes[i] = other.spikeTimes[i] += (rgen() - 0.5)*randomness;
}
this->totalSpikes = other.totalSpikes;
return this->totalSpikes;
}
};
/* This is used by wxWidgets */
struct Arr2D {
float* data;
int nx, ny;
Arr2D(int nx, int ny)
{
this->data = new float[nx*ny];
this->nx = nx;
this->ny = ny;
}
float& at(int x, int y)
{
return data[x*nx+y];
}
};
/* used by wxWidgets */
class LAWindow;
/* iterator shortcuts */
typedef vector<LANeuron*> nrn_list;
typedef vector<vector<LANeuron*> >::iterator input_iter;
typedef vector<LANeuron*>::iterator nrn_iter;
typedef vector<LABranch*>::iterator branch_iter;
typedef vector<LASynapse*>::iterator syn_iter;
typedef vector< pair<float, float> >::iterator pair_iter;
/* Global structure to hold network configuration */
class LANetwork
{
public:
vector<LASynapse*> synapses; /* List of all synapses */
vector<LANeuron*> neurons;
vector<LABranch*> branches;
vector<LANeuron*> pyr_list;
vector<LANeuron*> in_list;
vector<LANeuron*> bg_list;
vector<LANeuron*> noise_inputs;
vector< vector<LANeuron*> > inputs_cs; /* List of input stimuli (each stimulus is a set of neurons) */
vector< vector<LANeuron*> > inputs_us; /* List of input stimuli (each stimulus is a set of neurons) */
vector< vector<int> > spikesPerPattern;
//vector< LANeuron*> us_inputs;
vector<float> dbgNeuron;
vector< vector<int> > spikeTimings; // stores time of spikes during stimulation only!
vector< vector<int> > spikesPerStim; // stores time of spikes during stimulation only!
vector< vector<float> > nrnVoltages; // ditto for voltages
map< pair<int, int>, double> distances; // holds euclidean between neurons if needed
static int RSEED;
int runningMode, runningPatternNo;
float localPRPThresh, globalPRPThresh;
float homeostasisTimeParam; // Time that it takes for synaptic scaling to be applied
float BSPTimeParam; // Time that it takes for BSP to be applied
float CREBTimeParam; // Time that it takes for CREB to fall
float connectivityParam, inhibitionParam, stimDurationParam; // Multipliers for doing sensitivity analysis
int weakMemId;
vector<int> isWeakMem;
pthread_mutex_t synapses_mutex;
ofstream mfile, vfile, sumweightsFile;
vector< vector<int> > patterns;
float homeostasisTime;
int synapsesCounter;
LAWindow* wx_window;
FILE* spikesFile; // Save spiking info 'ere
Arr2D* voltageData;
int n_neurons,
n_branches_per_neuron,
n_inputs,
n_neurons_per_input,
Tstimulation, // Total stimulated time
T; // Simulation clock time
bool enablePlasticity; /* Is plasticity enabled? */
bool isInterstim;
bool disableCreb;
bool debugMode;
float synapseMult ;
bool isRecall;
bool localProteins, repeatedLearning, pretraining, altConnectivity, globalProteins;
char* conditionsString;
string datadir ;
float branchOverlap;
float initWeight, maxWeight, dendSpikeThresh;
bool enablePruning, isPruningSynapses;
LANetwork()
{
enablePruning = isPruningSynapses = false;
synapsesCounter =0;
T = Tstimulation =0;
isRecall = false;
wx_window = NULL;
n_neurons = n_branches_per_neuron = n_inputs = n_neurons_per_input =0;
enablePlasticity = true;
isInterstim = false;
spikesFile = NULL;
runningMode = RUN_TRAINING;
runningPatternNo = 0;
weakMemId = -1;
repeatedLearning = globalProteins = localProteins = false;
pthread_mutex_init(&this->synapses_mutex, NULL);
pretraining = false;
debugMode = false;
altConnectivity=false;
conditionsString = NULL;
datadir = "./";
branchOverlap = -1.0;
disableCreb = false;
homeostasisTime = 24.0;
synapseMult = 1.0;
localPRPThresh=1.0;
globalPRPThresh=1.0;
homeostasisTimeParam = 1.0;
BSPTimeParam = 1.0;
CREBTimeParam = 1.0;
inhibitionParam = 1.0;
connectivityParam = 1.0;
stimDurationParam = 1.0;
dendSpikeThresh = 1.0;
initWeight = 0.3;
maxWeight = 1.0;
}
static void SetRandomSeed( int seed)
{
LANetwork::RSEED = seed+80; // XXX
std::srand(seed+80);
}
~LANetwork()
{
Cleanup();
}
/* Set up the network, neurons and connections */
void CreateFearNet(int, int , int, int);
void Cleanup(void)
{
ResetSpikeCounters();
for (nrn_iter ni = this->neurons.begin(); ni != this->neurons.end(); ni++)
delete (*ni);
for (branch_iter ni = this->branches.begin(); ni != this->branches.end(); ni++)
delete (*ni);
for (syn_iter ni = this->synapses.begin(); ni != this->synapses.end(); ni++)
delete (*ni);
this->neurons.clear();
this->branches.clear();
this->synapses.clear();
if (this->spikesFile)
fclose(this->spikesFile);
}
/* Connect two sets of neurons , can specify minimum / maximum allowed distances between pairs of neurons */
int ConnectByDistance(vector<LANeuron*> fromList, vector<LANeuron*> toList, float fromDistance, float toDistance, int nNeuronPairs, int nSynapsesPerNeuron,float, bool, bool, float );
int PurgeInputSynapses( int totalToRemove, float);
int CreateInputSynapses( int totalToAdd);
/* Create a set of neurons and append them to specified list */
void CreateNeurons(int number, int branches_per_neuron, char type, vector<LANeuron*>* appendTo, int, bool);
void CalculateDistances();
void Stimulate(int, int);
void Stimulate2(int, int);
void RunPatternTest();
/* Simulate stimulus-dynamics (1msec per step) */
void StimDynamics(int duration);
/* Simulate inter-stimulus dynamics (protein / creb level / synapse weights changes only */
void Interstim(int duration);
void Begin(void);
void CreateTags(void);
void MemoryStats(int);
void ResetSpikeCounters(void)
{
for (nrn_iter i = neurons.begin(); i != neurons.end(); i++)
{
(*i)->total_spikes =0;
(*i)->dend_spikes =0;
for (branch_iter b = (*i)->branches.begin(); b != (*i)->branches.end(); ++b)
{
(*b)->branch_spikes = 0;
}
}
}
void ResetCrebLevels()
{
for (nrn_iter i = neurons.begin(); i != neurons.end(); i++)
(*i)->crebLevel =0.0;
}
void RunStoreTest(int np, int app, int dm, int test=0, int a=-1);
void RunStore2(int np, int app, int dm, int test=0, int a=-1);
void RunPattern(vector<int>&, float, float, int , int );
void StoreDataFiles( bool);
void RecordInitialWeightSums()
{
for (nrn_iter i = neurons.begin(); i != neurons.end(); i++)
{
LANeuron* nrn = *i;
nrn->synapticWeightsInitialSum =0.;
for (branch_iter b = (*i)->branches.begin(); b != (*i)->branches.end(); ++b)
for (syn_iter si = (*b)->synapses.begin(); si != (*b)->synapses.end(); ++si)
{
nrn->synapticWeightsInitialSum += (*si)->weight;
}
}
}
void SaveSpikeCounters(ofstream& ratesdat)
{
for (nrn_iter na = neurons.begin(); na != neurons.end(); ++na)
{
LANeuron* nrn = *na;
ratesdat << nrn->total_spikes << " ";
}
ratesdat << endl;
}
void SaveSnapshot(char*);
bool HasCondition(char* cond)
{
if (this->conditionsString && strstr(this->conditionsString, cond))
return true;
return false;
}
bool SaveSynapseState(char* filename)
{
ofstream synstatedat(filename);
for (syn_iter si =this->synapses.begin(); si != this->synapses.end(); ++si)
{
LASynapse* s = *si;
if (s->isPlastic)
{
synstatedat << s->sid<<" "
<< s->target_branch->bid<<" "
<< s->target_nrn->nid << " "
<< s->source_nrn->nid <<" "
<< s->source_nrn->input_id<< " "
<< s->target_branch->strength << " "
<< s->weight << " "
<<endl;
}
}
return true;
}
void ReportSumWeights()
{
float consolidated[10] = {0.0};
if (!this->sumweightsFile.is_open())
{
this->sumweightsFile.open((this->datadir + "/sum-weights.txt").c_str(), std::ofstream::out );
}
for (syn_iter si = this->synapses.begin(); si != this->synapses.end(); ++si)
{
LASynapse* s =*si;
if ( s->source_nrn->input_id >=0 && s->target_nrn->type == 'P')
{
consolidated[s->source_nrn->input_id] += s->weight;
}
}
for (int i=0; i < 10; i++)
{
//cout << " ["<< i <<"]-> "<< consolidated[i]<< endl;
this->sumweightsFile << consolidated[i] << " " ;
cout << consolidated[i] << " " ;
}
this->sumweightsFile << endl;
cout << endl;
}
void SetDataDir(string dir)
{
this->datadir = dir;
mkdir(this->datadir.c_str(), 0755);
}
void PrintSynapsesSnapshot(string outfile)
{
ofstream fout(outfile.c_str());
for (nrn_iter ni = this->pyr_list.begin(); ni != this->pyr_list.end(); ni++)
{
LANeuron* nrn = *ni;
for (branch_iter bi = nrn->branches.begin(); bi != nrn->branches.end(); ++bi)
{
LABranch* b = *bi;
for (syn_iter si = b->synapses.begin(); si != b->synapses.end(); ++si)
{
LASynapse* s =*si;
LAInput* src = (LAInput*)s->source_nrn;
if (src->input_id >=0)
{
fout << src->input_id << " " << src->groupIdx << " " << s->target_branch->bid << " " << s->target_nrn->nid << " " << s->weight << " " << endl;
}
}
}
}
}
void SaveCalcs()
{
ofstream ooo("./data/calc.dat");
for (syn_iter si = this->synapses.begin(); si!= this->synapses.end(); si++)
{
LASynapse* s = *si;
if (s->source_nrn->input_id ==0)
{
ooo << s->calcium << endl;
}
}
ooo.close();
}
};
#endif