-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintfsw.mod
2084 lines (1749 loc) · 55.5 KB
/
intfsw.mod
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
: $Id: intfsw.mod,v 1.50 2009/02/26 18:24:34 samn Exp $
:* COMMENT
COMMENT
this file contains functions/utilities for computing the network/graph-theoretic
properties of INTF and other networks represented as adjacency lists
:** clustering coefficient functions
FUNCTION GetCCR(adj,outvec,[startid,endid,subsamp]) gets the clustering coefficient on a range
of cells
FUNCTION GetCC -- gets clustering coefficient
FUNCTION GetCCSubPop -- get the clustering coefficient between 'sub-populations' of vertices
:** path length related functions
FUNCTION GetPathR -- gets path length on a range of cells at a time
FUNCTION GetWPath -- gets weighted path length , which may be weighted by synaptic weights &
delays
FUNCTION GetPairDist -- computes distances between all pairs of vertices, self->self distance==
distance of shortest loop
FUNCTION GetPathSubPop -- computes path lengths between sub-populations
FUNCTION GetLoopLength -- computes distance to loop back to each node
FUNCTION GetPathEV -- gets path length
FUNCTION CountNeighborsR -- counts the # of neighbors/outputs of a specified degree on a range
of cells
:** miscellaneous functions
FUNCTION GetRecurCount -- counts # of recurrent connections
FUNCTION Factorial -- computes factorial, if input is too large uses approximation
FUNCTION perm - count # of permutations from set of N elements with R selections
ENDCOMMENT
:* NEURON blocks
NEURON {
SUFFIX intfsw
GLOBAL INSTALLED
GLOBAL verbose
GLOBAL edgefuncid : edge-weight-function for GetWPath,0=weightdelaydist,1=weightdist,2=delaydist
}
PARAMETER {
INSTALLED=0
verbose=0
edgefuncid=0
}
VERBATIM
#include "misc.h"
typedef struct {
int isz;
int imaxsz;
double* p;
} myvec;
myvec* allocmyvec (int maxsz){
myvec* pv = (myvec*)malloc(sizeof(myvec));
if(!pv) return 0x0;
pv->isz=0;
pv->imaxsz=maxsz;
pv->p=(double*)malloc(sizeof(double)*maxsz);
if(!pv->p) { free(pv); return 0x0; }
return pv;
}
int freemyvec (myvec** pps) {
if(!pps || !pps[0]) return 0;
myvec* ps = pps[0];
if(ps->p)free(ps->p);
free(ps);
pps[0]=0x0;
return 1;
}
double popmyvec (myvec* pv) {
if(pv->isz<1) {
printf("popmyvec ERRA: can't pop empty stack!\n");
return 0.0;
}
double d = pv->p[pv->isz-1]; pv->isz--;
return d;
}
void popallmyvec (myvec* pv) {
pv->isz=0;
}
double pushmyvec (myvec* ps,double d) {
if(ps->isz==ps->imaxsz) {
printf("pushmyvec realloc\n");
ps->imaxsz*=2;
ps->p=(double*)realloc(ps->p,sizeof(double)*ps->imaxsz);
if(!ps->p){ printf("pushmyvec ERRA: myvec out of memory %d!!\n",ps->imaxsz); return 0.0; }
}
ps->p[ps->isz++]=d;
return 1.0;
}
double appendmyvec (myvec* ps,double d) {
return pushmyvec(ps,d);
}
typedef struct myqnode_ {
struct myqnode_* pnext;
struct myqnode_* pprev;
int dd;
} myqnode;
myqnode* allocmyqnode() {
myqnode* p = (myqnode*)malloc(sizeof(myqnode));
p->pnext=0x0;
p->pprev=0x0;
return p;
}
typedef struct {
myqnode* pfront;
myqnode* pback;
} myq;
myq* allocmyq() {
myq* pq = (myq*)malloc(sizeof(myq));
pq->pfront = pq->pback = 0x0;
return pq;
}
int freemyq(myq** ppq) {
myq* pq = *ppq;
myqnode* ptmp=pq->pback;
while(pq->pback){
if(pq->pback->pprev==0x0){
free(pq->pback);
pq->pback=0x0;
pq->pfront=0x0;
break;
} else {
ptmp=pq->pback->pprev;
free(pq->pback);
}
}
free(pq);
ppq[0]=0;
return 1;
}
int printfrontmyq (myq* pq) {
if(pq && pq->pfront) {
printf("front=%d ",pq->pfront->dd);
return 1;
}
printf("printfrontmyq ERRA: empty front!\n");
return 0;
}
int printbackmyq (myq* pq) {
if(pq && pq->pback) {
printf("back=%d ",pq->pback->dd);
return 1;
}
printf("printbackmyq ERRA: empty back!\n");
return 0;
}
int printmyq (myq* pq, int backwards) {
if(pq){
int i=0;
if(backwards){
myqnode* pnode = pq->pback;
while(pnode){
printf("val %d from back = %d\n",i++,pnode->dd);
pnode = pnode->pprev;
}
} else {
myqnode* pnode = pq->pfront;
while(pnode){
printf("val %d from front = %d\n",i++,pnode->dd);
pnode = pnode->pnext;
}
}
return 1;
}
printf("printmyq ERRA: null pointer!\n");
return 0;
}
int enqmyq (myq* pq,int d) {
if(pq->pfront==pq->pback) {
if(!pq->pfront){
pq->pfront = allocmyqnode();
pq->pback = pq->pfront;
pq->pfront->dd=d;
} else {
pq->pback = allocmyqnode();
pq->pback->dd=d;
pq->pback->pprev = pq->pfront;
pq->pfront->pnext = pq->pback;
}
} else {
myqnode* pnew = allocmyqnode();
pnew->dd = d;
pq->pback->pnext = pnew;
pnew->pprev = pq->pback;
pq->pback = pnew;
}
return 1;
}
int emptymyq (myq* pq) {
if(pq->pfront==0x0) return 1;
return 0;
}
int deqmyq (myq* pq) {
if(pq->pfront == pq->pback){
if(!pq->pfront){
printf("deqmyq ERRA: can't deq empty q!\n");
return -1.0;
} else {
int d = pq->pfront->dd;
free(pq->pfront);
pq->pfront=pq->pback=0x0;
return d;
}
} else {
myqnode* tmp = pq->pfront;
int d = tmp->dd;
pq->pfront = pq->pfront->pnext;
pq->pfront->pprev = 0x0;
free(tmp);
return d;
}
}
ENDVERBATIM
FUNCTION testmystack () {
VERBATIM
myvec* pv = allocmyvec(10);
printf("created stack with sz %d\n",pv->imaxsz);
int i;
for(i=0;i<pv->imaxsz;i++) {
double d = 41.0 * (i%32) + rand()%100;
printf("pushing %g onto stack of sz %d\n",d,pv->isz);
pushmyvec(pv,d);
}
printf("test stack realloc by pushing 123.0\n");
pushmyvec(pv,123.0);
printf("stack now has %d elements, %d maxsz. contents:\n",pv->isz,pv->imaxsz);
for(i=0;i<pv->isz;i++)printf("s[%d]=%g\n",i,pv->p[i]);
printf("popping %d elements. contents:\n",pv->isz);
while(pv->isz){
double d = popmyvec(pv);
printf("popped %g, new sz = %d\n",d,pv->isz);
}
printf("can't pop stack now, empty test: ");
popmyvec(pv);
freemyvec(&pv);
printf("freed stack\n");
return 1.0;
ENDVERBATIM
}
FUNCTION testmyq () {
VERBATIM
myq* pq = allocmyq();
printf("created q, empty = %d\n",emptymyq(pq));
printf("enqueing 10 values:\n");
int i;
for(i=0;i<10;i++){
int d = 41 * (i%32) + rand()%252;
printf("enqueuing %d...",d);
enqmyq(pq,d);
printfrontmyq(pq);
printbackmyq(pq); printf("\n");
}
printf("printing q in forwards order:\n");
printmyq(pq,0);
printf("printing q in backwards order:\n");
printmyq(pq,1);
printf("testing deq:\n");
while(!emptymyq(pq)){
printf("b4 deq: ");
printfrontmyq(pq);
printbackmyq(pq); printf("\n");
int d = deqmyq(pq);
printf("dequeued %d\n",d);
printf("after deq: ");
printfrontmyq(pq);
printbackmyq(pq); printf("\n");
}
freemyq(&pq);
printf("freed myq\n");
return 1.0;
ENDVERBATIM
}
:* utility functions: copynz(), nnmeandbl(), gzmeandbl(), gzmean(), nnmean()
VERBATIM
//copy values in valarray who's corresponding entry in binarray != 0 into this vector
//copynz(valvec,binvec)
static double copynz (void* vv) {
double* pV;
int n = vector_instance_px(vv,&pV) , iCount = 0 , idx=0;
int iStartIDx = 0, iEndIDx = n - 1;
if(ifarg(2)){
iStartIDx = (int)*getarg(1);
iEndIDx = (int) *getarg(2);
}
if(iEndIDx < iStartIDx || iStartIDx >= n || iEndIDx >= n
|| iStartIDx<0 || iEndIDx < 0){
printf("copynz ERRA: invalid indices start=%d end=%d size=%d\n",iStartIDx,iEndIDx,n);
return -1.0;
}
double* pVal,*pBin;
if(vector_arg_px(1,&pVal)!=n || vector_arg_px(2,&pBin)!=n){
printf("copynz ERRB: vec args must have size %d!",n);
return 0.0;
}
int iOutSz = 0;
for(idx=iStartIDx;idx<=iEndIDx;idx++){
if(pBin[idx]){
pV[iOutSz++]=pVal[idx];
}
}
vector_resize((IvocVect*)pV,iOutSz);
return (double)iOutSz;
}
//** nnmeandbl()
static double nnmeandbl (double* p,int iStartIDX,int iEndIDX) {
int iCount=0,idx=0;
double dSum = 0.0;
for(idx=iStartIDX;idx<=iEndIDX;idx++){
if(p[idx]>=0.0){
dSum+=p[idx];
iCount++;
}
}
if(iCount>0) return dSum / iCount;
return -1.0;
}
//** gzmeandbl()
static double gzmeandbl (double* p,int iStartIDX,int iEndIDX) {
int iCount=0,idx=0;
double dSum = 0.0;
for(idx=iStartIDX;idx<=iEndIDX;idx++){
if(p[idx]>0.0){
dSum+=p[idx];
iCount++;
}
}
if(iCount>0) return dSum / iCount;
return -1.0;
}
//** gzmean() mean for elements in Vector > 0.0
static double gzmean (void* vv) {
double* pV;
int n = vector_instance_px(vv,&pV) , iCount = 0 , idx=0;
int iStartIDx = 0, iEndIDx = n - 1;
if(ifarg(2)){
iStartIDx = (int)*getarg(1);
iEndIDx = (int) *getarg(2);
}
if(iEndIDx < iStartIDx || iStartIDx >= n || iEndIDx >= n
|| iStartIDx<0 || iEndIDx < 0){
printf("gzmean ERRA: invalid indices start=%d end=%d size=%d\n",iStartIDx,iEndIDx,n);
return -1.0;
}
return gzmeandbl(pV,iStartIDx,iEndIDx);
}
//** nnmean() mean for elements in Vector >= 0.0
static double nnmean (void* vv) {
double* pV;
int n = vector_instance_px(vv,&pV) , iCount = 0 , idx=0;
int iStartIDx = 0, iEndIDx = n - 1;
if(ifarg(2)){
iStartIDx = (int)*getarg(1);
iEndIDx = (int) *getarg(2);
}
if(iEndIDx < iStartIDx || iStartIDx >= n || iEndIDx >= n
|| iStartIDx<0 || iEndIDx < 0){
printf("nnmean ERRA: invalid indices start=%d end=%d size=%d\n",iStartIDx,iEndIDx,n);
return -1.0;
}
return nnmeandbl(pV,iStartIDx,iEndIDx);
}
ENDVERBATIM
:* GetCCR(adj,outvec,[startid,endid,subsamp])
FUNCTION GetCCR () {
VERBATIM
ListVec* pList = AllocListVec(*hoc_objgetarg(1));
if(!pList){
printf("GetCC ERRA: problem initializing first arg!\n");
return 0.0;
}
int iCells = pList->isz;
if(iCells<2){
printf("GetCC ERRB: size of List < 2 !\n");
FreeListVec(&pList);
return 0.0;
}
double** pLV = pList->pv;
unsigned int* pLen = pList->plen;
//init vector of distances to each cell , 0 == no path found
int* pNeighbors = (int*)calloc(iCells,sizeof(int));
int i = 0, iNeighbors = 0;
if(!pNeighbors){
printf("GetCCR ERRE: out of memory!\n");
FreeListVec(&pList);
return 0.0;
}
//init vector of avg distances to each cell , 0 == no path found
double* pCC;
int iVecSz = vector_arg_px(2,&pCC);
if(!pCC || iVecSz < iCells){
printf("GetCCR ERRE: arg 2 must be a Vector with size %d\n",iCells);
FreeListVec(&pList);
return 0.0;
}
memset(pCC,0,sizeof(double)*iVecSz);//init to 0
//start/end id of cells to find path to
int iStartID = ifarg(3) ? (int)*getarg(3) : 0,
iEndID = ifarg(4) ? (int)*getarg(4) : iCells - 1;
if(iStartID < 0 || iStartID >= iCells ||
iEndID < 0 || iEndID >= iCells ||
iStartID >= iEndID){
printf("GetCCR ERRH: invalid ids start=%d end=%d numcells=%d\n",iStartID,iEndID,iCells);
FreeListVec(&pList);
free(pNeighbors);
return 0.0;
}
double dSubsamp = ifarg(5)?*getarg(5):1.0;
if(dSubsamp<0.0 || dSubsamp>1.0){
printf("GetCCR ERRH: invalid subsamp = %g , must be btwn 0 and 1\n",dSubsamp);
FreeListVec(&pList);
free(pNeighbors);
return 0.0;
}
unsigned int iSeed = ifarg(7)?(unsigned int)*getarg(7):INT_MAX-109754;
double* pUse = 0;
if(dSubsamp<1.0){ //if using only a fraction of the cells
pUse = (double*)malloc(iCells*sizeof(double));
mcell_ran4(&iSeed, pUse, iCells, 1.0);
}
//get id of cell to find paths from
int myID;
int* pNeighborID = (int*)calloc(iCells,sizeof(int));
if( verbose > 0 ) printf("searching from id: ");
for(myID=0;myID<iCells;myID++) pCC[myID]=-1.0; //set invalid
for(myID=iStartID;myID<=iEndID;myID++){
if(verbose > 0 && myID%1000==0)printf("%d ",myID);
//only use dSubSamp fraction of cells, skip rest
if(pUse && pUse[myID]>=dSubsamp) continue;
int idx = 0, youID = 0, youKidID=0 , iNeighbors = 0;
//mark neighbors of distance == 1
for(idx=0;idx<pLen[myID];idx++){
youID = pLV[myID][idx];
if(youID>=iStartID && youID<=iEndID){
pNeighbors[youID]=1;
pNeighborID[iNeighbors++]=youID;
}
}
if(iNeighbors < 2){
for(i=0;i<iNeighbors;i++)pNeighbors[pNeighborID[i]]=0;
continue;
}
int iConns = 0 ;
//this checks # of connections between neighbors of node
for(i=0;i<iNeighbors;i++){
if(!pNeighbors[pNeighborID[i]])continue;
youID=pNeighborID[i];
for(idx=0;idx<pLen[youID];idx++){
youKidID=pLV[youID][idx];
if(youKidID >= iStartID && youKidID <= iEndID && pNeighbors[youKidID]){
iConns++;
}
}
}
pCC[myID]=(double)iConns/((double)iNeighbors*(iNeighbors-1));
for(i=0;i<iNeighbors;i++)pNeighbors[pNeighborID[i]]=0;
}
free(pNeighborID);
free(pNeighbors);
FreeListVec(&pList);
if(pUse)free(pUse);
if( verbose > 0 ) printf("\n");
return 1.0;
ENDVERBATIM
}
:* usage GetCentrality(adjlist,outvec)
: based on code from http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf
: and python networkx centrality.py implementation (brandes betweenness centrality)
FUNCTION GetCentrality () {
VERBATIM
ListVec* pList = AllocListVec(*hoc_objgetarg(1));
if(!pList){
printf("GetCentrality ERRA: problem initializing first arg!\n");
return 0.0;
}
int iCells = pList->isz;
if(iCells<2){
printf("GetCentrality ERRB: size of List < 2 !\n");
FreeListVec(&pList);
return 0.0;
}
double** pLV = pList->pv;
unsigned int* pLen = pList->plen;
//init vector of avg distances to each cell , 0 == no path found
double* pCE;
int iVecSz = vector_arg_px(2,&pCE);
if(!pCE || iVecSz < iCells){
printf("GetCCR ERRE: arg 2 must be a Vector with size %d\n",iCells);
FreeListVec(&pList);
return 0.0;
}
memset(pCE,0,sizeof(double)*iVecSz);//init to 0
double dSubsamp = ifarg(3)?*getarg(3):1.0;
if(dSubsamp<0.0 || dSubsamp>1.0){
printf("GetCCR ERRH: invalid subsamp = %g , must be btwn 0 and 1\n",dSubsamp);
FreeListVec(&pList);
return 0.0;
}
unsigned int iSeed = ifarg(4)?(unsigned int)*getarg(4):INT_MAX-109754;
double* pUse = 0;
if(dSubsamp<1.0){ //if using only a fraction of the cells
pUse = (double*)malloc(iCells*sizeof(double));
mcell_ran4(&iSeed, pUse, iCells, 1.0);
}
int s,w,T,v,idx;
myvec* S = allocmyvec(iCells*2);
myvec** P = (myvec**)malloc(sizeof(myvec*)*iCells);
myvec* d = allocmyvec(iCells);
myvec* sigma = allocmyvec(iCells);
myvec* di = allocmyvec(iCells);
for(w=0;w<iCells;w++) P[w]=allocmyvec(iCells);
for(s=0;s<iCells;s++){
if(verbose && s%100==0) printf("s=%d\n",s);
S->isz=0;//empty stack
for(w=0;w<iCells;w++) P[w]->isz=0;//empty list
for(T=0;T<iCells;T++) sigma->p[T]=0; sigma->p[s]=1;
for(T=0;T<iCells;T++) d->p[T]=-1; d->p[s]=0;
myq* Q = allocmyq();
enqmyq(Q,s);
while(!emptymyq(Q)){
v = deqmyq(Q);
pushmyvec(S,v);
for(idx=0;idx<pLen[v];idx++){
w = (int) pLV[v][idx];
if(d->p[w]<0){
enqmyq(Q,w);
d->p[w] = d->p[v] + 1;
}
if(d->p[w] == d->p[v] + 1){
sigma->p[w] = sigma->p[w] + sigma->p[v];
appendmyvec(P[w],v);
}
}
}
freemyq(&Q);
for(v=0;v<iCells;v++) di->p[v]=0;
while(S->isz){
w = popmyvec(S);
for(idx=0;idx<P[w]->isz;idx++){
v=P[w]->p[idx];
di->p[v] = di->p[v] + (sigma->p[v]/sigma->p[w])*(1.0+di->p[w]);
}
if(w!=s) pCE[w] = pCE[w] + di->p[w];
}
}
int N = 0;
for(s=0;s<iCells;s++) if(pLen[s]) N++;
if(N>2){
double scale = 1.0/( (N-1.0)*(N-2.0) );
for(v=0;v<iCells;v++) if(pLen[v]) pCE[v] *= scale;
}
CEFREE:
freemyvec(&S);
for(w=0;w<iCells;w++) freemyvec(&P[w]);
free(P);
freemyvec(&d);
freemyvec(&sigma);
freemyvec(&di);
if(pUse)free(pUse);
return 1.0;
ENDVERBATIM
}
:* usage GetCC(adjlist,myid,[startid,endid])
: adjlist == list of vectors specifying connectivity - adjacency list : from row -> to entry in column
: myid == id of cell to get clustering coefficient for
: startid == min id of cells search can terminate on or go through
: endid == max ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
FUNCTION GetCC () {
VERBATIM
ListVec* pList = AllocListVec(*hoc_objgetarg(1));
if(!pList){
printf("GetCC ERRA: problem initializing first arg!\n");
return -1.0;
}
int iCells = pList->isz;
if(iCells<2){
printf("GetCC ERRB: size of List < 2 !\n");
FreeListVec(&pList);
return -1.0;
}
double** pLV = pList->pv;
unsigned int* pLen = pList->plen;
//init vector of distances to each cell , 0 == no path found
int* pNeighbors = (int*)calloc(iCells,sizeof(int));
int i = 0, iNeighbors = 0;
if(!pNeighbors){
printf("GetCC ERRE: out of memory!\n");
FreeListVec(&pList);
return -1.0;
}
//get id of cell to find paths from
int myID = (int) *getarg(2);
if(myID < 0 || myID >= iCells){
printf("GetCC ERRF: invalid id = %d\n",myID);
FreeListVec(&pList);
free(pNeighbors);
return -1.0;
}
//start/end id of cells to find path to
int iStartID = ifarg(3) ? (int)*getarg(3) : 0,
iEndID = ifarg(4) ? (int)*getarg(4) : iCells - 1;
if(iStartID < 0 || iStartID >= iCells ||
iEndID < 0 || iEndID >= iCells ||
iStartID >= iEndID){
printf("GetCC ERRH: invalid ids start=%d end=%d numcells=%d\n",iStartID,iEndID,iCells);
FreeListVec(&pList);
free(pNeighbors);
return -1.0;
}
int idx = 0, iDist = 1 , youID = 0, youKidID=0;
int* pNeighborID = (int*)calloc(iCells,sizeof(int));
//mark neighbors of distance == 1
for(idx=0;idx<pLen[myID];idx++){
youID = pLV[myID][idx];
if(youID>=iStartID && youID<=iEndID){
pNeighbors[youID]=1;
pNeighborID[iNeighbors++]=youID;
}
}
if(iNeighbors < 2){
FreeListVec(&pList);
free(pNeighbors);
return -1.0;
}
int iConns = 0;
//this checks # of connections between neighbors of node starting from
for(i=0;i<iNeighbors;i++){
if(!pNeighbors[pNeighborID[i]])continue;
youID=pNeighborID[i];
for(idx=0;idx<pLen[youID];idx++){
youKidID=pLV[youID][idx];
if(youKidID >= iStartID && youKidID <= iEndID && pNeighbors[youKidID]){
iConns++;
}
}
}
free(pNeighborID);
free(pNeighbors);
FreeListVec(&pList);
return (double)iConns/((double)iNeighbors*(iNeighbors-1));
ENDVERBATIM
}
:* usage CountNeighborsR(adjlist,outvec,startid,endid,degree,subsamp])
: adjlist == list of vectors specifying connectivity - adjacency list : from row -> to entry in column
: outvec == vector of distances
: startid == min id of cells search can terminate on or go through
: endid == max ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
: degree == distance of neighbors -- counts # of neighbors of EXACT distance specified ONLY
: subsamp == specifies fraction btwn 0 and 1 of starting nodes to search
FUNCTION CountNeighborsR () {
VERBATIM
ListVec* pList = AllocListVec(*hoc_objgetarg(1));
if(!pList){
printf("CountNeighborsR ERRA: problem initializing first arg!\n");
return 0.0;
}
int iCells = pList->isz;
if(iCells < 2){
printf("CountNeighborsR ERRB: size of List < 2 !\n");
FreeListVec(&pList);
return 0.0;
}
double** pLV = pList->pv;
unsigned int* pLen = pList->plen;
//init vector of avg distances to each cell , 0 == no path found
double* pVD;
int iVecSz = vector_arg_px(2,&pVD) , i = 0;
if(!pVD || iVecSz < iCells){
printf("CountNeighborsR ERRE: arg 2 must be a Vector with size %d\n",iCells);
FreeListVec(&pList);
return 0.0;
}
memset(pVD,0,sizeof(double)*iVecSz);//init to 0
//get id of cell to find paths from
int myID = (int) *getarg(3);
if(myID < 0 || myID >= iCells){
printf("CountNeighborsR ERRF: invalid id = %d\n",myID);
FreeListVec(&pList);
return 0.0;
}
//start/end id of cells to search for neighbors of degree iDist
int iStartID = (int)*getarg(3),
iEndID = (int)*getarg(4),
iSearchDegree = (int)*getarg(5);
double dSubsamp = ifarg(6)?*getarg(6):1.0;
unsigned int iSeed = ifarg(7)?(unsigned int)*getarg(7):INT_MAX-109754;
if(iStartID < 0 || iStartID >= iCells ||
iEndID < 0 || iEndID >= iCells ||
iStartID >= iEndID){
printf("CountNeighborsR ERRH: invalid ids start=%d end=%d numcells=%d\n",iStartID,iEndID,iCells);
FreeListVec(&pList);
return 0.0;
}
//check search degree
if(iSearchDegree<=0){
printf("CountNeighborsR ERRI: invalid searchdegree=%d\n",iSearchDegree);
FreeListVec(&pList);
return 0.0;
}
//init array of cells/neighbors to check
int* pCheck = (int*)malloc(sizeof(int)*iCells);
if(!pCheck){
printf("CountNeighborsR ERRG: out of memory!\n");
FreeListVec(&pList);
return 0.0;
}
int iCheckSz = 0, idx = 0, iDist = 1 , youID = 0, youKidID=0, iTmpSz = 0, jdx = 0, iMatches = 0;
double* pVDTmp = 0, dgzt = 0.0;
int* pTmp = 0;
double* pUse = 0;
if(dSubsamp<1.0){ //if using only a fraction of the cells
pUse = (double*)malloc(iCells*sizeof(double));
mcell_ran4(&iSeed, pUse, iCells, 1.0);
}
if( verbose > 0 ) printf("searching from id: ");
pVDTmp = (double*)calloc(iCells,sizeof(double));
pTmp = (int*)calloc(iCells,sizeof(int));
for(myID=iStartID;myID<=iEndID;myID++){
if(verbose > 0 && myID%1000==0)printf("%d ",myID);
//only use dSubSamp fraction of cells, skip rest
if(pUse && pUse[myID]>=dSubsamp) continue;
iMatches = 0;
iCheckSz = 0; idx = 0; iDist = 1; youID = 0; youKidID = 0;
//mark neighbors of distance == 1
for(idx=0;idx<pLen[myID];idx++){
youID = pLV[myID][idx];
if(youID>=iStartID && youID<=iEndID && !pVDTmp[youID]){
pVDTmp[youID]=(double)iDist;
pCheck[iCheckSz++]=youID;
}
}
if(iSearchDegree == iDist){
pVD[myID] = iCheckSz;
for(idx=0;idx<iCheckSz;idx++) pVDTmp[pCheck[idx]]=0; //reset for next cell
continue;
}
pVDTmp[myID]=1;
iTmpSz = 0; jdx=0;
iDist++;
//this does a breadth-first search but avoids recursion
while(iCheckSz>0 && iDist<=iSearchDegree){
iTmpSz = 0;
for(idx=0;idx<iCheckSz;idx++){
youID=pCheck[idx];
for(jdx=0;jdx<pLen[youID];jdx++){
youKidID=pLV[youID][jdx];
if(youKidID >= iStartID && youKidID <=iEndID && !pVDTmp[youKidID]){
pTmp[iTmpSz++] = youKidID; //save id of cell to search it's kids on next iteration
pVDTmp[youKidID]=(double)iDist; //this cell is at iDist away, even if it is also @ a shorter distance
}
}
}
iCheckSz = iTmpSz;
if(iSearchDegree == iDist){
pVD[myID] = iCheckSz;
memset(pVDTmp,0,sizeof(double)*iCells); //reset to 0 for next cell
break;
}
if(iCheckSz) memcpy(pCheck,pTmp,sizeof(int)*iCheckSz);
iDist++;
}
}
if(pUse) free(pUse);
free(pCheck);
FreeListVec(&pList);
free(pVDTmp); free(pTmp);
if( verbose > 0 ) printf("\n");
return 1.0;
ENDVERBATIM
}
:* utility functions: maxval(), weightdelaydist(), weightdist(), delaydist(), printedgefunc()
VERBATIM
double maxval(double* p,int sz)
{
double dmax = p[0];
int i = 1;
for(;i<sz;i++) if(p[i]>dmax) dmax = p[i];
return dmax;
}
double weightdelaydist(double w,double d)
{
if(w < 0)
return -w/d;
if(w > 0)
return d/w;
return DBL_MAX; // no connection means infinite distance
}
double weightdist(double w,double d)
{
if(w < 0)
return -w;
if(w > 0)
return 1/w;
return DBL_MAX; // no connection means infinite distance
}
double delaydist(double w,double d)
{
return d;
}
void printedgefunc(int id)
{
switch(id){
case 0:
printf("weightdelaydist\n");
break;
case 1:
printf("weightdist\n");
break;
case 2:
printf("delaydist\n");
break;
default:
printf("unknown!\n");
break;
}
}
ENDVERBATIM
:* FUNCTION predgefunc()
FUNCTION predgefunc () {
VERBATIM
int i;
if(ifarg(1)){ printf("%d=",(int)*getarg(1)); printedgefunc((int)*getarg(1)); printf("\n"); }
else for(i=0;i<3;i++){ printf("%d=",i); printedgefunc(i); printf("\n"); }
return 0.0;
ENDVERBATIM
}
:* usage GetWPath(preid,poid,weights,delays,outvec,[subsamp])
: preid == list of presynaptic IDs
: poid == list of postsynaptic IDs
: weights == list of weights, excit > 0 , inhib < 0
: delays == list of delays
: outvec == vector of distances
: subsamp == only use specified fraction of synapses , optional
FUNCTION GetWPath () {
VERBATIM
double* ppre = 0, *ppo = 0, *pwght = 0, *pdel = 0, *pout = 0;
int iSz,iTmp,i,j,k,l;
IvocVect* voi;
iSz = vector_arg_px(1,&ppre);
if(iSz < 1)
{ printf("GetWPath ERRO: invalid size for presynaptic ID Vector (arg 1) %d!\n",iSz);
return -666.666;
}
if( (iTmp=vector_arg_px(2,&ppo)) != iSz)
{ printf("GetWPath ERRA: incorrectly sized postsynaptic ID Vector (arg 2) %d %d!",iSz,iTmp);
return -666.666;
}
if( (iTmp=vector_arg_px(3,&pwght)) != iSz)
{ printf("GetWPath ERRB: incorrectly sized weight Vector (arg 3) %d %d!\n",iSz,iTmp);
return -666.666;
}
if( (iTmp=vector_arg_px(4,&pdel)) != iSz)
{ printf("GetWPath ERRC: incorrectly sized delay Vector (arg 4) %d %d!\n",iSz,iTmp);
return -666.666;
}
int maxid = maxval(ppre,iSz);
iTmp = maxval(ppo,iSz);
if(iTmp > maxid) maxid=iTmp;
voi = vector_arg(5);
if( (iTmp=vector_arg_px(5,&pout))!= maxid+1 && 0)
{ printf("GetWPath ERRD: incorrectly sized output Vector (arg 5) %d %d!\n",maxid+1,iTmp);
return -666.666;
}