-
Notifications
You must be signed in to change notification settings - Fork 7
/
NL_population.cpp
1227 lines (1039 loc) · 43.6 KB
/
NL_population.cpp
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
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** 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/. **
** **
****************************************************************************
** Author: Alex Cope **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "NL_population.h"
#include "EL_experiment.h"
#include "SC_projectobject.h"
#include <sstream>
#include <iomanip>
#ifdef Q_OS_WIN
#define RETINA_SUPPORT 1.75
#else
#define RETINA_SUPPORT 1
#endif
population::population(float x, float y, float size, float aspect_ratio, QString name)
{
this->x = x;
this->y = y;
this->targx = x;
this->targy = y;
#ifdef Q_OS_MAC
this->animspeed = 0.2f;//0.1;
#else
this->animspeed = 0.2f;//0.1;
#endif
this->size = size;
this->aspect_ratio = aspect_ratio;
this->left = this->x-this->size/(2.0)*this->aspect_ratio;
this->right = this->x+this->size/(2.0)*this->aspect_ratio;
this->top = this->y+this->size/2.0;
this->bottom = this->y-this->size/2.0;
if (name.isEmpty()) {
name = "New population";
}
this->name = name;
this->neuronTypeName = "none";
this->numNeurons = 1;
this->colour = QColor(0,0,0,255);
this->type = populationObject;
this->isVisualised = false;
loc3.x = 0;
loc3.y = 0;
loc3.z = 0;
isSpikeSource = false;
}
population::population(QSharedPointer <population> data, QSharedPointer<population> thisSharedPointer)
{
this->x = data->x;
this->y = data->y;
this->targx = data->targx;
this->targy = data->targy;
this->animspeed = data->animspeed;
this->size = data->size;
this->aspect_ratio = data->aspect_ratio;
this->left = data->left;
this->right = data->right;
this->top = data->top;
this->bottom = data->bottom;
this->name = data->name;
this->neuronTypeName = data->neuronTypeName;
this->numNeurons = data->numNeurons;
this->colour = data->colour;
this->type = populationObject;
this->isVisualised = false;
loc3.x = data->loc3.x;
loc3.y = data->loc3.y;
loc3.z = data->loc3.z;
this->neuronType = QSharedPointer<ComponentInstance>(new ComponentInstance(data->neuronType));
// fix owner
this->neuronType->owner = thisSharedPointer;
isSpikeSource = false;
}
void
population::readFromXML (QDomElement &e, QDomDocument *, QDomDocument * meta,
projectObject * data, QSharedPointer<population> thisSharedPointer)
{
// defaults
this->x = 0;
this->y = 0;
this->targx = 0;
this->targy = 0;
#ifdef Q_OS_MAC
this->animspeed = 0.2f;//0.1;
#else
this->animspeed = 0.2f;//0.1;
#endif
this->size = 1.0f;
this->aspect_ratio = 3.0/4.0;
this->left = this->x-this->size/(2.0)*this->aspect_ratio;
this->right = this->x+this->size/(2.0)*this->aspect_ratio;
this->top = this->y+this->size/2.0;
this->bottom = this->y-this->size/2.0;
//this->name = name; // Don't self-assign name
this->neuronTypeName = "none";
this->numNeurons = 1;
this->colour = QColor(0,0,0,255);
this->type = populationObject;
this->isVisualised = false;
loc3.x = 0;
loc3.y = 0;
loc3.z = 0;
// ////////////////// fetch in the model data /////////////////////
// make sure we don't get all crashy if layout not specified
this->layoutType = QSharedPointer<NineMLLayoutData> (new NineMLLayoutData(data->catalogLAY[0]));
//this->layoutType->component = data->catalogLAY[0];
this->type = populationObject;
QDomNode n = e.firstChild();
QDomNode metaData;
while(!n.isNull()) {
if (n.isComment()) {
n = n.nextSibling();
continue;
}
if (n.toElement().tagName() == "LL:Annotation") {
QDomNodeList scAnns = n.toElement().elementsByTagName("SpineCreator");
if (scAnns.length() == 1) {
metaData = scAnns.at(0).cloneNode();
n.removeChild(scAnns.at(0));
}
QTextStream temp(&this->annotation);
n.save(temp,1);
} else if (n.toElement().tagName() == "LL:Neuron") {
// get attributes
this->name = n.toElement().attribute("name");
if (this->name == "") {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML error: missing Neuron attribute 'name'");
settings.endArray();
}
this->numNeurons = n.toElement().attribute("size").toInt();
if (this->numNeurons == 0) {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML error: missing Neuron attribute 'size', or 'size' is zero");
settings.endArray();
}
this->neuronTypeName = n.toElement().attribute("url");
QString real_url = this->neuronTypeName;
if (this->neuronTypeName == "") {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML error: missing Neuron attribute 'url'");
settings.endArray();
}
QStringList tempName = this->neuronTypeName.split('.');
// first section will hold the name
if (tempName.size() > 0) {
this->neuronTypeName = tempName[0];
}
this->neuronTypeName.replace("_", " ");
// do we have errors - if so abort here
{
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
if (num_errs > 0) {
DBG() << "Aborting on errors";
return;
}
}
///////////// FIND AND LOAD NEURON
this->neuronType.clear();
if (this->neuronTypeName == "SpikeSource") {
// make a spikes source
makeSpikeSource(thisSharedPointer);
} else {
this->isSpikeSource = false;
}
// if not found then match
if (!this->isSpikeSource) {
for (int i = 0; i < data->catalogNB.size(); ++i) {
if (neuronTypeName == data->catalogNB[i]->name) {
this->neuronType = QSharedPointer<ComponentInstance>(new ComponentInstance((QSharedPointer<Component>) data->catalogNB[i]));
this->neuronType->owner = thisSharedPointer;
this->neuronType->import_parameters_from_xml(n);
}
}
}
// if still missing then we have a problem
if (this->neuronType == NULL) {
this->neuronType = QSharedPointer<ComponentInstance>(new ComponentInstance(data->catalogNB[0]));
this->neuronType->owner = thisSharedPointer;
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("warning");
settings.setArrayIndex(num_errs + 1);
settings.setValue("warnText", "Network references component '" + this->neuronTypeName + "' which is not found");
settings.endArray();
}
} else if (n.toElement().tagName() == "LL:Projection") {
// handled elsewhere
} else if (n.toElement().tagName() == "Layout") {
this->layoutName = n.toElement().attribute("url");
QString real_url = this->layoutName;
if (this->layoutName == "") {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML error: missing Layout attribute 'url'");
settings.endArray();
}
this->layoutName.chop(4);
//this->layoutName.replace('_', ' ');
///////////// FIND AND LOAD LAYOUT
bool layFound = false;
// DO FETCH OF LAYOUT NODE HERE - IF WE HAVE IT
for (int i = 0; i < data->catalogLAY.size(); ++i) {
if (this->layoutName == data->catalogLAY[i]->name) {
this->layoutType.clear();
this->layoutType = QSharedPointer<NineMLLayoutData> (new NineMLLayoutData(data->catalogLAY[i]));
this->layoutType->component = data->catalogLAY[i];
this->layoutType->import_parameters_from_xml(n);
layFound = true;
break;
}
}
if (!layFound) {
this->layoutType.clear();
this->layoutType = QSharedPointer<NineMLLayoutData> (new NineMLLayoutData(data->catalogLAY[0]));
QSettings settings;
int num_errs = settings.beginReadArray("warnings");
settings.endArray();
settings.beginWriteArray("warnings");
settings.setArrayIndex(num_errs + 1);
settings.setValue("warnText", "Network references missing Layout '" + layoutName + "'");
settings.endArray();
}
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML error: misplaced or unknown tag '" + n.toElement().tagName() + "'");
settings.endArray();
}
n = n.nextSibling();
}
// ////////////////// fetch in the metadata ///////////////////////
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
if (meta != NULL) {
QDomNode findN = meta->documentElement().firstChild();
QDomElement metaE;
// locate the matching metadata node
while( !findN.isNull() ) {
metaE = findN.toElement();
if (metaE.tagName() == "population") {
if (metaE.attribute("name","") == this->name) {
break;
}
}
findN = findN.nextSibling();
}
n = metaE.firstChild();
}
#endif
if (!metaData.isNull()) {
n = metaData.firstChild();
}
while( !n.isNull() )
{
if (n.isComment()) {
n = n.nextSibling();
continue;
}
QDomElement e2 = n.toElement();
if( e2.tagName() == "xPos" ) {
this->x = e2.attribute("value", "").toFloat() + data->getCursorPos().x;
this->targx = this->x;
}
if( e2.tagName() == "yPos" ) {
this->y = e2.attribute("value", "").toFloat() + data->getCursorPos().y;
this->targy = this->y;
}
if( e2.tagName() == "animSpeed" ) {
this->animspeed = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "aspectRatio" ) {
this->aspect_ratio = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "colour" ) {
this->colour.setRed(e2.attribute("red", "").toUInt());
this->colour.setGreen(e2.attribute("green", "").toUInt());
this->colour.setBlue(e2.attribute("blue", "").toInt());
}
if( e2.tagName() == "size" ) {
this->size = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "tag" ) {
this->tag = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "x3D" ) {
this->loc3.x = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "y3D" ) {
this->loc3.y = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "z3D" ) {
this->loc3.z = e2.attribute("value", "").toFloat();
}
if( e2.tagName() == "is_visualised" ) {
this->isVisualised = e2.attribute("value", "").toFloat();
}
n = n.nextSibling();
}
// calculate some values:
this->left = this->x-this->size/(2.0)*this->aspect_ratio;
this->right = this->x+this->size/(2.0)*this->aspect_ratio;
this->top = this->y+this->size/2.0;
this->bottom = this->y-this->size/2.0;
}
void population::setupBounds()
{
this->left = this->x-this->size/(2.0)*this->aspect_ratio;
this->right = this->x+this->size/(2.0)*this->aspect_ratio;
this->top = this->y+this->size/2.0;
this->bottom = this->y-this->size/2.0;
}
#define DBGRI() qDebug() << __FUNCTION__ << ": "
void population::read_inputs_from_xml(QDomElement &e, QDomDocument * meta, projectObject * data)
{
//DBGBRK();
// e starts out as a LL:Population
//DBGRI() << "population::read_inputs_from_xml called for element " << e.tagName();
QDomNodeList nListNRN = e.elementsByTagName("LL:Neuron");
//DBGRI() << "LL:Neuron list has size " << nListNRN.size();
if (nListNRN.size() == 1) {
QDomElement e1 = nListNRN.item(0).toElement();
QString popname = e1.attribute("name", "unknown");
// Population name and ComponentInstance name should be the same.
//DBGRI() << "Population name: " << popname << " neuronType (ComponentInstance) name:" << this->neuronType->getXMLName();
QDomNodeList nList = e1.elementsByTagName("LL:Input");
//DBGRI() << "LL:Input list has size " << nList.size();
for (int nrni = 0; nrni < (int) nList.size(); ++nrni) {
QDomElement e2 = nList.item(nrni).toElement();
QSharedPointer<genericInput> newInput = QSharedPointer<genericInput>(new genericInput);
newInput->srcCmpt.clear();
newInput->dstCmpt.clear();
//DBGRI() << "Setting destination to be neuronType->owner, with name: " << this->neuronType->owner->getName();
newInput->destination = this->neuronType->owner;
newInput->projInput = false;
// read in src from XML and locate src in existing projectobject data:
QString srcName = e2.attribute("src");
//DBGRI() << "srcName: " << srcName;
for (int i = 0; i < data->network.size(); ++i) {
//DBGRI() << "XML element type: " << data->network[i]->neuronType->getXMLName();
if (data->network[i]->neuronType->getXMLName() == srcName) {
//DBGRI() << "Set newInput->src to the thing with name " << data->network[i]->neuronType->getXMLName();
newInput->srcCmpt = data->network[i]->neuronType;
newInput->source = data->network[i];
}
for (int j = 0; j < data->network[i]->projections.size(); ++j) {
//DBGRI() << " Projection name: " << data->network[i]->projections[j]->getName();
for (int k = 0; k < data->network[i]->projections[j]->synapses.size(); ++k) {
//DBGRI() << " Synapse: " << data->network[i]->projections[j]->synapses[k]->getName();
if (data->network[i]->projections[j]->synapses[k]->weightUpdateCmpt->getXMLName() == srcName) {
//DBGRI() << " WU: " << data->network[i]->projections[j]->synapses[k]->weightUpdateType->getXMLName();
newInput->srcCmpt = data->network[i]->projections[j]->synapses[k]->weightUpdateCmpt;
//DBGRI() << "(From WU) Set newInput->src to the thing with name " << newInput->srcCmpt->getXMLName();
newInput->source = data->network[i]->projections[j];
}
if (data->network[i]->projections[j]->synapses[k]->postSynapseCmpt->getXMLName() == srcName) {
//DBGRI() << " PS: " << data->network[i]->projections[j]->synapses[k]->postsynapseType->getXMLName();
newInput->srcCmpt = data->network[i]->projections[j]->synapses[k]->postSynapseCmpt;
//DBGRI() << "(From PS) Set newInput->src to the thing with name " << newInput->srcCmpt->getXMLName();
newInput->source = data->network[i]->projections[j];
}
}
}
}
// read in port names
newInput->srcPort = e2.attribute("src_port");
newInput->dstPort = e2.attribute("dst_port");
// get connectivity
QDomNodeList type = e2.elementsByTagName("AllToAllConnection");
if (type.count() == 1) {
delete newInput->conn;
newInput->conn = new alltoAll_connection;
QDomNode cNode = type.item(0);
newInput->conn->setParent (newInput);
newInput->conn->import_parameters_from_xml(cNode);
}
type = e2.elementsByTagName("OneToOneConnection");
if (type.count() == 1) {
delete newInput->conn;
newInput->conn = new onetoOne_connection;
newInput->conn->setParent(newInput);
QDomNode cNode = type.item(0);
newInput->conn->setParent (newInput);
newInput->conn->import_parameters_from_xml(cNode);
}
type = e2.elementsByTagName("FixedProbabilityConnection");
if (type.count() == 1) {
delete newInput->conn;
newInput->conn = new fixedProb_connection;
QDomNode cNode = type.item(0);
newInput->conn->setParent (newInput);
newInput->conn->import_parameters_from_xml(cNode);
}
type = e2.elementsByTagName("ConnectionList");
if (type.count() == 1) {
delete newInput->conn;
newInput->conn = new csv_connection;
QDomNode cNode = type.item(0);
newInput->conn->srcPop = qSharedPointerDynamicCast <population> (newInput->source);
newInput->conn->dstPop = qSharedPointerDynamicCast <population> (newInput->destination);
newInput->conn->setParent (newInput);
newInput->conn->import_parameters_from_xml(cNode);
}
if (newInput->srcCmpt != (QSharedPointer <ComponentInstance>)0) {
// neuronType is a ComponentInstance, owner is a systemObject. newInput->dst is a ComponentInstance.
newInput->dstCmpt = this->neuronType; // FIXME. This doesn't seem to be the right thing.
//DBGRI() << "For population with name " << this->name << ", set newInput->dst to the thing with name " << newInput->dstCmpt->getXMLName();
//DBGRI() << "Pushing back the newInput!";
this->neuronType->inputs.push_back(newInput);
newInput->srcCmpt->outputs.push_back(newInput);
} else {
// Error
}
// get annotations
QDomNode annInst = e2.firstChild();
while (!(annInst.toElement().tagName() == "LL:Annotation") && !(annInst.isNull())) {
annInst = annInst.nextSibling();
}
if (annInst.toElement().tagName() == "LL:Annotation") {
QDomNode n = annInst;
this->neuronType->inputs.back()->read_meta_data(n, data->getCursorPos());
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
} else {
this->neuronType->inputs.back()->read_meta_data(meta, data->getCursorPos());
#endif
}
}
}
#ifdef __GONE__
// read metadata. This should add the curves to the generic inputs.
for (int i = 0; i < this->neuronType->inputs.size(); ++i) {
this->neuronType->inputs[i]->read_meta_data(meta, data->getCursorPos());
}
#endif
this->neuronType->matchPorts();
//DBGRI() << "population::read_inputs_from_xml returning";
}
void population::delAll(nl_rootdata * data)
{
// remove the projections (they'll take themselves off the vector)
while ( this->projections.size()) {
this->projections[0]->delAll(data);
}
// remove the reverse projections (they'll take themselves off the vector)
while (this->reverseProjections.size()) {
this->reverseProjections[0]->delAll(data);
}
neuronType->removeReferences();
}
void population::delAll(projectObject * data)
{
// remove the projections (they'll take themselves off the vector)
while ( this->projections.size()) {
this->projections[0]->delAll(data);
}
// remove the reverse projections (they'll take themselves off the vector)
while (this->reverseProjections.size()) {
this->reverseProjections[0]->delAll(data);
}
neuronType->removeReferences();
}
population::~population()
{
if (isSpikeSource) {
this->neuronType->component.clear();
}
neuronType.clear();
}
QString population::getName()
{
return this->name;
}
bool population::within_bounds(float x, float y)
{
// cerr("v = %f %f %f %f %f %f", x, y, top, bottom, left, right);
if (x > this->left && x < this->right && y > this->bottom && y < this->top) {
return 1;
} else {
return 0;
}
}
bool population::is_clicked(float x, float y, float)
{
if (this->within_bounds(x, y)) {
return 1;
} else {
return 0;
}
}
void population::animate(QSharedPointer<population>thisSharedPointer)
{
// do animation:
//DBG() << "stuff " << float(this->targx) << " " << float(this->targy) << endl;
float delta[2];
delta[HORIZ] = this->animspeed*(this->targx - this->x);
delta[VERT] = this->animspeed*(this->targy - this->y);
this->x = this->x + delta[HORIZ];
this->y = this->y + delta[VERT];
this->left = this->x-this->size/(2.0)*this->aspect_ratio;
this->right = this->x+this->size/(2.0)*this->aspect_ratio;
this->top = this->y+this->size/2.0;
this->bottom = this->y-this->size/2.0;
// update projections:
for (int i = 0; i < this->projections.size(); ++i) {
this->projections[i]->animate(thisSharedPointer, QPointF(delta[HORIZ], delta[VERT]), this->projections[i]);
}
// update reverse projections
for (int i = 0; i < this->reverseProjections.size(); ++i) {
this->reverseProjections[i]->animate(thisSharedPointer, QPointF(delta[HORIZ], delta[VERT]), this->reverseProjections[i]);
}
// update inputs
for (int i = 0; i < this->neuronType->inputs.size(); ++i) {
this->neuronType->inputs[i]->animate(thisSharedPointer, QPointF(delta[HORIZ], delta[VERT]));
}
// update outputs
for (int i = 0; i < this->neuronType->outputs.size(); ++i) {
this->neuronType->outputs[i]->animate(thisSharedPointer, QPointF(delta[HORIZ], delta[VERT]));
}
}
void population::setupTrans(float GLscale, float viewX, float viewY, int width, int height)
{
this->tempTrans.GLscale = GLscale;
this->tempTrans.viewX = viewX;
this->tempTrans.viewY = viewY;
this->tempTrans.width = float(width);
this->tempTrans.height = float(height);
}
QPointF population::transformPoint(QPointF point)
{
point.setX(((point.x()+this->tempTrans.viewX)*this->tempTrans.GLscale+this->tempTrans.width)/2);
point.setY(((-point.y()+this->tempTrans.viewY)*this->tempTrans.GLscale+this->tempTrans.height)/2);
return point;
}
void population::draw(QPainter *painter, float GLscale, float viewX, float viewY, int width, int height, QImage image, drawStyle style)
{
float scale = GLscale/(200.0*RETINA_SUPPORT);
this->setupTrans(GLscale, viewX, viewY, width, height);
if (this->isSpikeSource) {
style = spikeSourceDrawStyle;
}
switch (style) {
case microcircuitDrawStyle:
{
// draw circle
QPen oldPen = painter->pen();
QPen pen = painter->pen();
pen.setWidthF((pen.widthF()+1.0)*2*scale);
painter->setPen(pen);
painter->drawEllipse(transformPoint(QPointF(this->x, this->y)),0.5*GLscale/2.0,0.5*GLscale/2.0);
painter->setPen(oldPen);
QFont oldFont = painter->font();
QFont font = painter->font();
font.setPointSizeF(GLscale/10.0);
painter->setFont(font);
// print label
QStringList text = this->name.split(" ");
if (text.size()>0) {
QString title = text.at(0);
if (title.size() > 5)
title.resize(5);
painter->drawText(QRectF(transformPoint(QPointF(this->x-0.5, this->y-0.2)),transformPoint(QPointF(this->x+0.5, this->y+0.2))), Qt::AlignCenter, title);
painter->setFont(oldFont);
}
return;
}
case layersDrawStyle:
{
return;
}
case spikeSourceDrawStyle:
{
// draw circle
QPen oldPen = painter->pen();
QPen pen = painter->pen();
pen.setWidthF((pen.widthF()+1.0));//*GLscale/100.0
pen.setColor(QColor(200,200,200,0));
painter->setPen(pen);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
QColor col(this->colour);
col.setAlpha(100);
brush.setColor(col);
QBrush oldBrush = painter->brush();
painter->setBrush(brush);
painter->drawEllipse(transformPoint(QPointF(this->x, this->y)),0.5*GLscale/2.0,0.5*GLscale/2.0);
QFont oldFont = painter->font();
QFont font = painter->font();
font.setPointSizeF(GLscale/10.0);
painter->setFont(font);
// print label
pen.setColor(QColor(0,0,0,255));
painter->setPen(pen);
//painter->drawText(QRectF(transformPoint(QPointF(this->x-0.5, this->y-0.2)),transformPoint(QPointF(this->x+0.5, this->y+0.2))), Qt::AlignCenter, "SS");
painter->setFont(oldFont);
painter->setBrush(oldBrush);
painter->setPen(oldPen);
QImage ssimage(":/images/ssBig.png");
QRectF imRect(transformPoint(QPointF(this->x, this->y))-QPointF(0.4*GLscale/2.0,0.4*GLscale/2.0),QSizeF(0.4*GLscale,0.4*GLscale));
painter->drawImage(imRect, ssimage);
return;
break;
}
case standardDrawStyle:
case standardDrawStyleExcitatory:
case saveNetworkImageDrawStyle:
default:
// do nothing here, break out into the code below.
break;
}
// transform the co-ordinates manually (using the qt transformation leads to blurry fonts!)
float left = ((this->left+viewX)*GLscale+float(width))/2;
float right = ((this->right+viewX)*GLscale+float(width))/2;
float top = ((-this->top+viewY)*GLscale+float(height))/2;
float bottom = ((-this->bottom+viewY)*GLscale+float(height))/2;
QRectF rectangle(left, top, right-left, bottom-top);
QRectF rectangleInner(left+2*scale, top+2*scale, right-left-8*scale, bottom-top-4*scale);
QColor col(this->colour);
col.setAlpha(100);
QPainterPath path;
path.addRoundedRect(rectangle,0.05*GLscale,0.05*GLscale);
painter->fillPath(path, col);
painter->drawImage(rectangle, image);
// Draw a dark grey border around the population
painter->setPen(QColor(200,200,200,255));
painter->drawRoundedRect(rectangle,0.05*GLscale,0.05*GLscale);
painter->setPen(QColor(0,0,0,255));
QString displayed_name = this->name;
if (displayed_name.size() > 13) {
displayed_name.resize(10);
displayed_name = displayed_name + "...";
}
QString displayed_comp_name = this->neuronType->component->name;
if (displayed_comp_name.size() > 14) {
displayed_comp_name.resize(11);
displayed_comp_name = displayed_comp_name + "...";
}
QFont oldFont = painter->font();
QFont font = painter->font();
QString text = displayed_name + "\n" + QString::number(this->numNeurons);// + "\n" + displayed_comp_name;
font.setPointSizeF(1.5*GLscale/(20.0*RETINA_SUPPORT));
painter->setFont(font);
painter->drawText(rectangleInner, Qt::AlignRight|Qt::AlignTop, text);
font.setPointSizeF(1.3*GLscale/(20.0*RETINA_SUPPORT));
painter->setFont(font);
painter->setPen(QColor(60,60,60,255));
painter->drawText(rectangleInner, Qt::AlignRight|Qt::AlignBottom, displayed_comp_name);
painter->setFont(oldFont);
}
void population::drawSynapses(QPainter *painter, float GLscale, float viewX, float viewY, int width, int height, drawStyle style)
{
QPen oldPen = painter->pen();
QPen pen(QColor(0,0,255,255));
pen.setWidthF(1.5);
#ifdef Q_OS_MAC
pen.setWidthF(0.75);
#endif
painter->setPen(pen);
// so we could have an inherited class with this function
QImage ignored;
// draw projections
for (int i = 0; i < this->projections.size(); ++i) {
this->projections[i]->draw(painter, GLscale, viewX, viewY, width, height, ignored, style);
}
painter->setPen(oldPen);
}
void population::drawInputs(QPainter *painter, float GLscale, float viewX, float viewY, int width, int height, drawStyle style)
{
painter->setPen(QColor(0,210,0,255));
// so we could have an inherited class with this function
QImage ignored;
// draw neuron inputs
for (int i = 0; i < this->neuronType->inputs.size(); ++i) {
this->neuronType->inputs[i]->draw(painter, GLscale, viewX, viewY, width, height, ignored, style);
}
// draw projection inputs
for (int i = 0; i < this->projections.size(); ++i) {
this->projections[i]->drawInputs(painter, GLscale, viewX, viewY, width, height, ignored, style);
}
painter->setPen(QColor(0,0,0,255));
}
QPainterPath * population::addToPath(QPainterPath * path)
{
path->addRect(this->getLeft(), this->getBottom(), this->size*this->aspect_ratio, this->size);
return path;
}
float population::leftBound(float pos)
{
return this->left + (pos-this->x);
}
float population::rightBound(float pos)
{
return this->right + (pos-this->x);
}
float population::topBound(float pos)
{
return this->top + (pos-this->y);
}
float population::bottomBound(float pos)
{
return this->bottom + (pos-this->y);
}
float population::getLeft()
{
return this->left;
}
float population::getRight()
{
return this->right;
}
float population::getTop()
{
return this->top;
}
float population::getBottom()
{
return this->bottom;
}
float population::getSide(int dir, int which)
{
if (dir == HORIZ && which == LOWER) {
return this->left;
}
if (dir == HORIZ && which == UPPER) {
return this->right;
}
if (dir == VERT && which == LOWER) {
return this->bottom;
}
if (dir == VERT && which == UPPER) {
return this->top;
}
return -10000.0;
}
bool population::connectsTo(QSharedPointer <population> pop)
{
for (int i = 0; i < this->reverseProjections.size(); ++i) {
if (this->reverseProjections[i]->source->name == pop->name) {
return true;
}
}
return false;
}
QPointF population::currentLocation()
{
return QPointF(this->targx, this->targy);
}
void population::move(float x, float y)
{
this->targx = x + this->locationOffset.x();
this->targy = y + this->locationOffset.y();
}
void population::write_population_xml(QXmlStreamWriter &xmlOut)
{
// population tag
xmlOut.writeStartElement("LL:Population");
// Population annotations
xmlOut.writeStartElement("LL:Annotation");
// old annotations
this->annotation.replace("\n", "");
this->annotation.replace("<LL:Annotation>", "");
this->annotation.replace("</LL:Annotation>", "");
QXmlStreamReader reader(this->annotation);
while (!reader.atEnd()) {
if (reader.tokenType() != QXmlStreamReader::StartDocument
&& reader.tokenType() != QXmlStreamReader::EndDocument) {
xmlOut.writeCurrentToken(reader);
}
reader.readNext();
}
// new annotations
xmlOut.writeStartElement("SpineCreator");
// add tags for each bit of metadata
// x position
xmlOut.writeEmptyElement("xPos");
// To avoid metaData changing arbitrarily, impose a
// granularity limit on float x.
stringstream xx;
xx << std::setprecision(METADATA_FLOAT_PRECISION) << this->x;
xmlOut.writeAttribute("value", xx.str().c_str());
// y position
xmlOut.writeEmptyElement("yPos");
// To avoid metaData changing arbitrarily, impose a
// granularity limit on float y.
stringstream yy;
yy << std::setprecision(METADATA_FLOAT_PRECISION) << this->y;
xmlOut.writeAttribute("value", yy.str().c_str());
// this->animspeed;
xmlOut.writeEmptyElement("animSpeed");
xmlOut.writeAttribute("value", QString::number(this->animspeed));
// this->aspect_ratio;
xmlOut.writeEmptyElement("aspectRatio");
xmlOut.writeAttribute("value", QString::number(this->aspect_ratio));
// this->colour;
xmlOut.writeEmptyElement("colour");
xmlOut.writeAttribute("red", QString::number(this->colour.red()));
xmlOut.writeAttribute("green", QString::number(this->colour.green()));
xmlOut.writeAttribute("blue", QString::number(this->colour.blue()));
// this->size;
xmlOut.writeEmptyElement("size");
xmlOut.writeAttribute("value", QString::number(this->size));
// this->tag;
xmlOut.writeEmptyElement("tag");
xmlOut.writeAttribute("value", QString::number(this->tag));
// 3d x position
xmlOut.writeEmptyElement("x3D");
xmlOut.writeAttribute("value", QString::number(this->loc3.x));
// 3d y position
xmlOut.writeEmptyElement("y3D");
xmlOut.writeAttribute("value", QString::number(this->loc3.y));
// 3d z position
xmlOut.writeEmptyElement("z3D");
xmlOut.writeAttribute("value", QString::number(this->loc3.z));
// isViz?
xmlOut.writeEmptyElement("is_visualised");
xmlOut.writeAttribute("value", QString::number(this->isVisualised));
xmlOut.writeEndElement(); // SpineCreator
// end annotations
xmlOut.writeEndElement(); // annotation
// NEURON /////////////////
xmlOut.writeStartElement("LL:Neuron");