-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.h
3487 lines (2973 loc) · 97.9 KB
/
neuron.h
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
#ifndef neuron_h
#define neuron_h
#include "neuron_types.h"
#include <cstring>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <hdf5.h>
#include <hdf5_hl.h>
//#include <tbb/tbb.h>
#include <tbb/task_scheduler_init.h>
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
#include <tbb/queuing_mutex.h>
#include <tbb/task_group.h>
#include <vtkType.h>
#include <vtkSOADataArrayTemplate.h>
#include <vtkAOSDataArrayTemplate.h>
#include <vtkPointData.h>
#include <vtkCellData.h>
#include <vtkIdTypeArray.h>
#include <vtkPolyData.h>
#include <vtkImageData.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkPolyData.h>
#include <vtkImageData.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkXMLMultiBlockDataWriter.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyDataWriter.h>
/*#include <vtkXMLMultiBlockDataWriter.h>
#include <vtkPolyDataWriter.h>*/
// make memory aligned to cahce lines/avx register size
#define ALIGN_TO __BIGGEST_ALIGNMENT__
#define MALLOC(_nbytes) \
aligned_alloc(ALIGN_TO, _nbytes)
#define SWAP(_name) \
auto _name ## Tmp = other._name; \
other._name = _name; \
_name = _name ## Tmp;
#define SWAP_VEC(_name, _len) \
for (int i = 0; i < _len; ++i) \
{ \
typename std::remove_extent<decltype(_name)>::type \
_name ## Tmp = other._name[i]; \
other._name[i] = _name[i]; \
_name[i] = _name ## Tmp; \
}
namespace neuron
{
neuron::e_type_map e_types;
neuron::m_type_map m_types;
// defining MEM_CHECK will result in a walk of the dataset
// every point of every cell is sent to stderr stream used
// in conjunction with valgrind this validates the cells
//#define MEM_CHECK
// force the removal of the first simple cell. this is
// assumed to be the root of the neuron. if that is not
// the case then unsetting this will lead to assert fail
// in the packaging code which is written to factor the
// root and associated data into a separate dataset.
#define FORCE_CLEAN_ROOT
// this is meant to prevent multiple concurrent calls into HDF5
// and it is also used to serialize debug output when MEM_CHECK
// is defined
extern tbb::queuing_mutex ioMutex;
// --------------------------------------------------------------------------
#define ERROR(_msg) \
{ \
std::cerr << "Error: [" \
<< __FILE__ << ":" << __LINE__ << "] " _msg << std::endl; \
}
// --------------------------------------------------------------------------
#define EYESNO(_er) (_er == 0 ? "(yes)" : "(no)")
// locate data files
int imFileExists(const char *dirName);
int neuronFileExists(const char *dirName, int nid);
int scanForNeurons(const char *dirName, int &lastId);
// --------------------------------------------------------------------------
int readNumDimensions(hid_t fh, const char *dsname, int &nDims)
{
// get number of dimensions
if (H5LTget_dataset_ndims(fh, dsname, &nDims) < 0)
return -1;
return 0;
}
// --------------------------------------------------------------------------
int readDimensions(hid_t fh, const char *dsname, hsize_t *dims,
int nDimsReq=1, hsize_t *dimsReq=nullptr)
{
// get size of buffers for time series
int nDims = 0;
if (H5LTget_dataset_ndims(fh, dsname, &nDims) < 0)
{
ERROR("Failed to get number of \"" << dsname << "\" dimensions")
return -1;
}
// verify that this dataset has the expected dimensionality
if (nDims != nDimsReq)
{
ERROR("\"" << dsname << "\" has " << nDims << " but we require " << nDimsReq)
return -1;
}
H5T_class_t elemClass;
size_t elemSize = 0;
if (H5LTget_dataset_info(fh, dsname, dims, &elemClass, &elemSize) < 0)
{
ERROR("Failed to get dataset info for \"" << dsname << "\"")
return -1;
}
// user passed expected dimensions verify the dimensions
if (dimsReq)
{
for (int i =0; i < nDimsReq; ++i)
{
if (dims[i] != dimsReq[i])
{
ERROR("In \"" << dsname << "\" expected "
<< dimsReq[i] << " but found " << dims[i]
<< " at dimension " << i)
return -1;
}
}
}
return 0;
}
// --------------------------------------------------------------------------
int readDimensions(hid_t fh, const char *dsname, hsize_t *dims,
int nDimsReq, hsize_t dimsReq)
{
return readDimensions(fh, dsname, dims, nDimsReq, &dimsReq);
}
// --------------------------------------------------------------------------
template<typename cppT>
struct cppH5Tt {};
#define declareCppH5Tt(_cppT, _h5Type) \
template<> struct cppH5Tt<_cppT> \
{ \
static \
hid_t typeCode() { return _h5Type; } \
\
static \
int readDataset(hid_t fh, const char *dsn, _cppT *buf) \
{ \
herr_t ierr = H5LTread_dataset(fh, dsn, _h5Type, buf); \
if (ierr < 0) \
return -1; \
return 0; \
} \
\
static \
int readAttribute(hid_t fh, const char *dsn, \
const char *atn, _cppT *buf) \
{ \
herr_t ierr = H5LTget_attribute(fh, dsn, atn, _h5Type, buf); \
if (ierr < 0) \
return -1; \
return 0; \
} \
};
declareCppH5Tt(char, H5T_NATIVE_CHAR)
declareCppH5Tt(signed char, H5T_NATIVE_SCHAR)
declareCppH5Tt(unsigned char, H5T_NATIVE_UCHAR)
declareCppH5Tt(short, H5T_NATIVE_SHORT)
declareCppH5Tt(unsigned short, H5T_NATIVE_USHORT)
declareCppH5Tt(int, H5T_NATIVE_INT)
declareCppH5Tt(unsigned, H5T_NATIVE_UINT)
declareCppH5Tt(long, H5T_NATIVE_LONG)
declareCppH5Tt(unsigned long, H5T_NATIVE_ULONG)
declareCppH5Tt(long long, H5T_NATIVE_LLONG)
declareCppH5Tt(unsigned long long, H5T_NATIVE_ULLONG)
declareCppH5Tt(float, H5T_NATIVE_FLOAT)
declareCppH5Tt(double, H5T_NATIVE_DOUBLE)
declareCppH5Tt(long double, H5T_NATIVE_LDOUBLE)
//declareCppH5Tt(hsize_t, H5T_NATIVE_HSIZE)
//declareCppH5Tt(hssize_t, H5T_NATIVE_HSSIZE)
// specialization for reading strings
template<> struct cppH5Tt<char **>
{
static
int readDataset(hid_t fh, const char *dsn, char *&str, size_t maxlen=64)
{
hid_t dset = H5Dopen(fh, dsn, H5P_DEFAULT);
if (dset < 0)
{
ERROR("Failed to open \"" << dsn << "\"")
return -1;
}
hid_t ft = H5Dget_type(dset);
hid_t dsp = H5Dget_space(dset);
int ndims = H5Sget_simple_extent_ndims(dsp);
if (ndims != 0)
{
ERROR("\"" << dsn << "\" not a scalar")
return -1;
}
hid_t mt = H5Tcopy(ft);
char *pstr = nullptr;
hid_t ierr = H5Dread(dset, mt, H5S_ALL, H5S_ALL, H5P_DEFAULT, &pstr);
if (ierr < 0)
{
ERROR("Failed to read \"" << dsn << "\"")
return -1;
}
str = strndup(pstr, maxlen);
H5Dvlen_reclaim(mt, dsp, H5P_DEFAULT, &pstr);
H5Dclose(dset);
H5Sclose(dsp);
H5Tclose(ft);
H5Tclose(mt);
return 0;
}
static
int readAttribute(hid_t dset, const char *atn,
char *&str, size_t maxlen=64)
{
hid_t att = H5Aopen(dset, atn, H5P_DEFAULT);
if (att < 0)
{
ERROR("Failed to open \"" << atn << "\"")
return -1;
}
hid_t ft = H5Aget_type(att);
hid_t mt = H5Tcopy(ft);
char *pstr = nullptr;
hid_t ierr = H5Aread(att, mt, &pstr);
if (ierr < 0)
{
ERROR("Failed to read \"" << atn << "\"")
return -1;
}
str = strndup(pstr, maxlen);
H5free_memory(pstr);
H5Tclose(ft);
H5Tclose(mt);
H5Aclose(att);
return 0;
}
static
int readAttribute(hid_t fh, const char *dsn, const char *atn,
char *&str, size_t maxlen=64)
{
hid_t dset = H5Dopen(fh, dsn, H5P_DEFAULT);
if (dset < 0)
{
ERROR("Failed to open \"" << dsn << "\"")
return -1;
}
int ierr = readAttribute(dset, atn, str, maxlen);
H5Dclose(dset);
return ierr;
}
};
// --------------------------------------------------------------------------
template <typename index_t, typename data_t>
int readTimeStep(hid_t fh, index_t nSteps, index_t stepSize, int nScalars,
index_t step, data_t *data[2])
{
if (fh < 0)
{
ERROR("Invalid file handle")
return -1;
}
if (step >= nSteps)
{
H5Fclose(fh);
fh = -1;
ERROR("Invalid step " << step << " file has " << nSteps << " steps")
return -1;
}
// deal with the tomfoolery coming out of BMTK output format
const char *dsetName[2];
if (nScalars == 1)
{
dsetName[0] = "/data";
dsetName[1] = nullptr;
}
else if (nScalars == 2)
{
dsetName[0] = "/im/data";
dsetName[1] = "/v/data";
}
else
{
H5Fclose(fh);
fh = -1;
ERROR("Only at most 2 scalars are allowed")
return -1;
}
for (int i = 0; i < nScalars; ++i)
{
hid_t dset = H5Dopen(fh, dsetName[i], H5P_DEFAULT);
if (dset < 0)
{
H5Fclose(fh);
fh = -1;
ERROR("Failed to ope3n the dataset " << dsetName[i])
return -1;
}
// select the time step
hsize_t foffs[2] = {hsize_t(step), 0};
hsize_t fsize[2] = {1, hsize_t(stepSize)};
hid_t fdsp = H5Dget_space(dset);
if (H5Sselect_hyperslab(fdsp, H5S_SELECT_SET, foffs, nullptr, fsize, nullptr) < 0)
{
H5Fclose(fh);
fh = -1;
ERROR("Failed to select time step " << step << " for " << dsetName[i])
return -1;
}
// allocate a buffer of the right side if needed
if (data[i] == nullptr)
data[i] = (data_t*)MALLOC(stepSize*sizeof(data_t));
// read it in
hid_t mdsp = H5Screate_simple(1, &fsize[1], nullptr);
if (H5Dread(dset, cppH5Tt<data_t>::typeCode(), mdsp, fdsp, H5P_DEFAULT, data[i]) < 0)
{
free(data[i]);
data[i] = nullptr;
H5Sclose(mdsp);
H5Sclose(fdsp);
H5Dclose(dset);
H5Fclose(fh);
fh = -1;
ERROR("Failed to read time step " << step << " for " << dsetName[i])
return -1;
}
H5Sclose(mdsp);
H5Sclose(fdsp);
H5Dclose(dset);
//H5Fclose(fh);
}
return 0;
}
// --------------------------------------------------------------------------
template <typename index_t, typename data_t>
int readTimeSeriesMetadata(const std::string &baseDir, hid_t &fh,
index_t &nNeurons, index_t &nSteps, index_t &stepSize, int &nScalars,
char *name[2], data_t &t0, data_t &t1, data_t &dt, index_t *&neuronIds,
index_t *&neuronOffs, index_t *&neuronSize)
{
tbb::queuing_mutex::scoped_lock lock(ioMutex);
if (fh < 0)
{
char fn[256];
snprintf(fn, 256, "%s/im.h5", baseDir.c_str());
fh = H5Fopen(fn, H5F_ACC_RDONLY, H5P_DEFAULT);
if (fh < 0)
{
ERROR("Failed to open " << fn)
return -1;
}
}
// read time metadata
hsize_t dims[2] = {0};
data_t tmd[3] = {data_t(0)};
if (readDimensions(fh, "/mapping/time", dims, 1, 3) ||
cppH5Tt<double>::readDataset(fh, "/mapping/time", tmd))
{
ERROR("Failed to read time metadata")
H5Fclose(fh);
fh = -1;
return -1;
}
t0 = tmd[0];
t1 = tmd[1];
dt = tmd[2];
// attempt to detect the file format. Vyassa's using some broken bmtk code
// and we need to attempt to work through a number of cases depending on
// what that code has done.
nScalars = 0;
int nDims = 0;
const char *dsetName = nullptr;
if (readNumDimensions(fh, "/data", nDims) == 0)
{
nScalars = 1;
name[0] = strdup("im");
name[1] = nullptr;
dsetName = "/data";
}
else if ((readNumDimensions(fh, "/im/data", nDims) == 0) &&
(readNumDimensions(fh, "/v/data", nDims) == 0))
{
nScalars = 2;
name[0] = strdup("im");
name[1] = strdup("v");
dsetName = "/im/data";
}
else
{
ERROR("Failed to detect file format. Expected either \"/data\" "
"or \"/data/im\" and \"/data/v\"")
H5Fclose(fh);
fh = -1;
return -1;
}
// get size of buffers for time series
if (readDimensions(fh, dsetName, dims, nDims))
{
H5Fclose(fh);
fh = -1;
return -1;
}
nSteps = dims[0];
stepSize = dims[1];
// get size of buffers for maps
if (readDimensions(fh, "/mapping/gids", dims, 1))
{
H5Fclose(fh);
fh = -1;
return -1;
}
nNeurons = dims[0];
// allocate buffers and read maps
int egi=0, eei=0;
neuronIds = (index_t*)MALLOC(nNeurons*sizeof(index_t));
index_t *eids = (index_t*)MALLOC(stepSize*sizeof(index_t));
if ((egi = cppH5Tt<index_t>::readDataset(fh, "/mapping/gids", neuronIds)) ||
(eei = cppH5Tt<index_t>::readDataset(fh, "/mapping/element_id", eids)))
{
free(neuronIds);
H5Fclose(fh);
fh = -1;
ERROR("Failed to read. neuronIds" << EYESNO(egi) << ", eids" << EYESNO(eei))
return -1;
}
// calculate the offset and size of the neurons
neuronOffs = (index_t*)MALLOC(nNeurons*sizeof(index_t));
neuronSize = (index_t*)MALLOC(nNeurons*sizeof(index_t));
for (index_t i = 0, q = 0; i < nNeurons; ++i)
{
neuronOffs[i] = q;
while (q < stepSize)
{
++q;
if ((q == stepSize) || (eids[q] == 0))
break;
}
neuronSize[i] = eids[q-1] + 1;
}
free(eids);
//H5Fclose(fh);
return 0;
}
// --------------------------------------------------------------------------
template <typename T>
bool equal(T a, T b, T tol)
{
T diff = std::abs(a - b);
a = std::abs(a);
b = std::abs(b);
b = (b > a) ? b : a;
if (diff <= (b*tol))
return true;
return false;
}
// --------------------------------------------------------------------------
template<typename index_t, typename coord_t>
int readNeuron(const std::string &baseDir, int nId,
coord_t *& __restrict__ p0, coord_t *& __restrict__ p5,
coord_t *& __restrict__ p1, coord_t *& __restrict__ d0,
coord_t *& __restrict__ d5, coord_t *& __restrict__ d1,
coord_t spos[3], int &e_type, int &m_type, int &ei,
int &layer, index_t & nSimpleCells)
{
tbb::queuing_mutex::scoped_lock lock(ioMutex);
char fn[256];
snprintf(fn, 256, "%s/seg_coords/%d.h5", baseDir.c_str(), nId);
hid_t fh = H5Fopen(fn, H5F_ACC_RDONLY, H5P_DEFAULT);
if (fh < 0)
{
ERROR("Failed to open " << fn)
return -1;
}
hsize_t dims[2] = {0};
if (readDimensions(fh, "/p0", dims, 2))
{
H5Fclose(fh);
ERROR("Failed to get coordinate dimensions")
return -1;
}
index_t ptSize = dims[0];
nSimpleCells = dims[1];
index_t bufSize = ptSize*nSimpleCells;
char *ets = nullptr, *mts = nullptr, *eis = nullptr;
int ep0=0,ep5=0,ep1=0,ed0=0,ed1=0,eet=0,emt=0,eei=0,esp=0,el=0;
p0 = (coord_t*)MALLOC(bufSize*sizeof(coord_t));
p5 = (coord_t*)MALLOC(bufSize*sizeof(coord_t));
p1 = (coord_t*)MALLOC(bufSize*sizeof(coord_t));
d0 = (coord_t*)MALLOC(bufSize*sizeof(coord_t));
d5 = (coord_t*)MALLOC(bufSize*sizeof(coord_t));
d1 = (coord_t*)MALLOC(bufSize*sizeof(coord_t));
if ((ep0 = cppH5Tt<coord_t>::readDataset(fh, "/p0", p0)) ||
(ep5 = cppH5Tt<coord_t>::readDataset(fh, "/p05", p5)) ||
(ep1 = cppH5Tt<coord_t>::readDataset(fh, "/p1", p1)) ||
(ed0 = cppH5Tt<coord_t>::readDataset(fh, "/d0", d0)) ||
(ed1 = cppH5Tt<coord_t>::readDataset(fh, "/d1", d1)) ||
(esp = cppH5Tt<coord_t>::readDataset(fh, "/soma_pos", spos)) ||
(eet = cppH5Tt<char**>::readDataset(fh, "/e_type", ets)) ||
(emt = cppH5Tt<char**>::readDataset(fh, "/m_type", mts)) ||
(eei = cppH5Tt<char**>::readDataset(fh, "/ei", eis)) ||
(el = cppH5Tt<int>::readDataset(fh, "/layer", &layer)))
{
free(p0);
free(p5);
free(p1);
free(d0);
free(d5);
free(d1);
H5Fclose(fh);
ERROR("Failed to read. p0" << EYESNO(ep0) << ", p5" << EYESNO(ep5)
<< ", p1" << EYESNO(ep1) << ", d0" << EYESNO(ed0) << ", d1" <<EYESNO(ed1)
<< ", spos" << EYESNO(esp) << ", e_tyoe" << EYESNO(eet)
<< ", m_type" << EYESNO(emt) << ", ei" << EYESNO(eei)
<< ", layer" << EYESNO(el))
return -1;
}
e_type = e_types[ets];
m_type = m_types[mts];
ei = (eis[0] == 'e' ? 1 : 0);
free(ets);
free(mts);
free(eis);
for (index_t i = 0; i < nSimpleCells; ++i)
d5[i] = (d0[i] + d1[i])*coord_t(0.5);
H5Fclose(fh);
return 0;
}
// --------------------------------------------------------------------------
template<typename index_t, typename coord_t>
int thickness(index_t nComplexCells, index_t * __restrict__ complexCells,
index_t * __restrict__ complexCellLens,
index_t * __restrict__ complexCellLocs,
index_t nPts, coord_t *__restrict__ dist,
coord_t *& __restrict__ thick)
{
thick = (coord_t*)MALLOC(nPts*sizeof(coord_t));
for (index_t i = 0; i < nPts; ++i)
thick[i] = 1.0 - dist[i];
for (index_t i = 0; i < nComplexCells; ++i)
{
// last point in the cell has a 0 thickness
index_t q = complexCellLocs[i] + complexCellLens[i] - 1;
index_t ii = complexCells[q];
thick[ii] = 0.0;
}
return 0;
}
// --------------------------------------------------------------------------
template<typename index_t, typename coord_t>
int distance(index_t * __restrict__ complexCells,
index_t nPts, coord_t * __restrict__ x, coord_t * __restrict__ y,
coord_t * __restrict__ z, coord_t *& __restrict__ dist)
{
dist = (coord_t*)MALLOC(nPts*sizeof(coord_t));
// compute the distance from root
index_t p5c0 = complexCells[1];
coord_t x0 = x[p5c0];
coord_t y0 = y[p5c0];
coord_t z0 = z[p5c0];
for (index_t i = 0; i < nPts; ++i)
{
coord_t xi = x[i];
coord_t yi = y[i];
coord_t zi = z[i];
coord_t r = xi - x0;
r *= r;
coord_t dy2 = yi - y0;
dy2 *= dy2;
r += dy2;
coord_t dz2 = zi - z0;
dz2 *= dz2;
r += dz2;
r = sqrt(r);
dist[i] = r;
}
coord_t max_dist = 0.0;
for (index_t i = 0; i < nPts; ++i)
max_dist = dist[i] > max_dist ? dist[i] : max_dist;
// normalize
for (index_t i = 0; i < nPts; ++i)
dist[i] /= max_dist;
return 0;
}
#if defined(MEM_CHECK)
// --------------------------------------------------------------------------
template<typename index_t, typename coord_t>
void print(index_t nSimpleCells, index_t *simpleCells,
index_t nComplexCells, index_t *complexCells,
index_t *complexCellLens, index_t *complexCellLocs,
index_t complexCellArraySize, index_t nPts,
coord_t *x, coord_t *y, coord_t *z, coord_t *d)
{
tbb::queuing_mutex::scoped_lock lock(ioMutex);
std::cerr << "simpleCells(" << nSimpleCells << ")=";
for (index_t i = 0; i < 3*nSimpleCells; ++i)
std::cerr << simpleCells[i] << ", ";
std::cerr << std::endl;
std::cerr << "complexCells(" << nComplexCells << ")=";
for (index_t i = 0; i < complexCellArraySize; ++i)
std::cerr << complexCells[i] << ", ";
std::cerr << std::endl;
std::cerr << "complexCellLens=";
for (index_t i = 0; i < nComplexCells; ++i)
std::cerr << complexCellLens[i] << ", ";
std::cerr << std::endl;
std::cerr << "complexCellLocs=";
for (index_t i = 0; i < nComplexCells; ++i)
std::cerr << complexCellLocs[i] << ", ";
std::cerr << std::endl;
std::cerr << "pts(" << nPts << ")=";
for (index_t i = 0; i < nPts; ++i)
std::cerr << "(" << x[i] << ", " << y[i] << ", "
<< z[i] << ", " << d[i] << "), ";
std::cerr << std::endl;
index_t tot = 0;
index_t *cc = complexCells;
for (index_t i = 0; i < nComplexCells; ++i)
{
index_t celLen = complexCellLens[i];
std::cerr << i << " " << celLen << std::endl;
for (index_t j = 0; j < celLen; ++j)
std::cerr << " " << cc[j] << " -> " << x[cc[j]] << ", "
<< y[cc[j]] << ", " << z[cc[j]] << std::endl;
cc += celLen;
tot += celLen;
}
std::cerr << "tot=" << tot + nComplexCells << std::endl;
}
#endif
// --------------------------------------------------------------------------
template<typename index_t, typename coord_t>
int clean(index_t &nPts, coord_t *& __restrict__ x, coord_t *& __restrict__ y,
coord_t *& __restrict__ z, coord_t *& __restrict__ d,
index_t &nSimpleCells, index_t *& __restrict__ simpleCells,
index_t &nComplexCells, index_t *& __restrict__ complexCells,
index_t *& __restrict__ complexCellLens,
index_t *& __restrict__ complexCellLocs,
index_t &complexCellArraySize, coord_t tol=1.e-3)
{
// allocate space for the complex cells.
nComplexCells = 0;
complexCellArraySize = 3*3*nSimpleCells;
index_t nbytes = complexCellArraySize*sizeof(index_t);
complexCells = (index_t*)MALLOC(nbytes);
memset(complexCells, 0, nbytes);
complexCellArraySize = 0;
// random access structures
nbytes = nSimpleCells*sizeof(index_t);
complexCellLens = (index_t*)MALLOC(nbytes);
memset(complexCellLens, 0, nbytes);
// random access structure
complexCellLocs = (index_t*)MALLOC(nbytes);
memset(complexCellLocs, 0, nbytes);
// this mask indicates if we deleted a given point already
index_t nnPts = 0;
index_t nDel = 0;
nbytes = nPts*sizeof(int);
int *del = (int*)MALLOC(nbytes);
memset(del, 0, nbytes);
// this mask indicates if the cell has been checked
nbytes = nSimpleCells*sizeof(int);
int *visit = (int*)MALLOC(nbytes);
memset(visit, 0, nbytes);
// check each point for duplicates
index_t * __restrict__ cc = complexCells;
for (index_t i = 0; i < nSimpleCells; ++i)
{
// skip cells we already checked
if (visit[i])
continue;
// start the new complex cell
complexCellLocs[nComplexCells] = complexCellArraySize;
index_t * __restrict__ ccl = complexCellLens + nComplexCells;
*ccl = 3;
index_t ii = 3*i;
cc[0] = simpleCells[ii];
cc[1] = simpleCells[ii + 1];
cc[2] = simpleCells[ii + 2];
cc += 3;
complexCellArraySize += 3;
nComplexCells += 1;
nnPts += 3;
#if defined(FORCE_CLEAN_ROOT)
// pass the first simple cell, which we assume is the
// root. there are some neurons that have points close
// enough to the root that the root is included in a branch
// which breaks our i/o code. force clean is currently
// necessary, but it also makes it impossible to detect
// when the root is not the first cell.
visit[i] = 1;
if (i == 0)
continue;
#endif
// check the last point in this cell
ii = 3*i + 2;
coord_t xi = x[ii];
coord_t yi = y[ii];
coord_t zi = z[ii];
index_t j = 0;
while (j < nSimpleCells)
{
// skip cells we already checked
if (visit[j])
{
j += 1;
continue;
}
// check first point in other cells
index_t jj = 3*j;
// this point was already deleted, don't bother testing it again.
if (del[jj])
{
j += 1;
continue;
}
coord_t xj = x[jj];
coord_t yj = y[jj];
coord_t zj = z[jj];
// check if they are the same
if (equal(xi,xj,tol) && equal(yi,yj,tol) && equal(zi,zj,tol))
{
// mark cell as visited
visit[j] = 1;
// mark point as deleted
del[jj] = 1;
nDel += 1;
// update the map
simpleCells[jj] = ii;
// before merging the cell, finish the scan because we only can
// have one merger per duplicate, but there may be more than
// one deuplicate in the set and we need to remove all
for (index_t k = j + 1; k < nSimpleCells; ++k)
{
// skip cells we already checked
if (visit[k])
continue;
// first point in other cells
index_t kk = 3*k;
// this point was already deleted, don't bother testing it again.
if (del[kk])
continue;
coord_t xk = x[kk];
coord_t yk = y[kk];
coord_t zk = z[kk];
// check if they are the same
if (equal(xi,xk,tol) && equal(yi,yk,tol) && equal(zi,zk,tol))
{
// don't mark this cell as visited because it is not
// being merged here
// mark as deleted
del[kk] = 1;
nDel += 1;
// update the map
simpleCells[kk] = ii;
}
}
// merge the cell.
*ccl += 2;
cc[0] = 3*j + 1;
cc[1] = 3*j + 2;
cc += 2;
complexCellArraySize += 2;
nnPts += 2;
// update the test point
ii = 3*j + 2;
xi = x[ii];
yi = y[ii];
zi = z[ii];
// rescan with the new point
j = 0;
continue;
}
j += 1;
}
}
// transfer the remaining points and relable the cells
nnPts = nPts - nDel;
nbytes = nnPts*sizeof(coord_t);
coord_t * __restrict__ nx = (coord_t*)MALLOC(nbytes);
coord_t * __restrict__ ny = (coord_t*)MALLOC(nbytes);
coord_t * __restrict__ nz = (coord_t*)MALLOC(nbytes);
coord_t * __restrict__ nd = (coord_t*)MALLOC(nbytes);
index_t simpleCellArraySize = 3*nSimpleCells;
nbytes = simpleCellArraySize*sizeof(index_t);
index_t * __restrict__ oSimpleCells = (index_t*)MALLOC(nbytes);
memcpy(oSimpleCells, simpleCells, nbytes);
nbytes = complexCellArraySize*sizeof(index_t);
index_t * __restrict__ oComplexCells = (index_t*)MALLOC(nbytes);
memcpy(oComplexCells, complexCells, nbytes);
for (index_t i = 0, j = 0; i < nPts; ++i)
{
if (del[i])
{
// every point id greater than this is shifted
for (index_t q = 0; q < simpleCellArraySize; ++q)
{
index_t shift = oSimpleCells[q] > i ? 1 : 0;
simpleCells[q] -= shift;
}
for (index_t q = 0; q < complexCellArraySize; ++q)
{
index_t shift = oComplexCells[q] > i ? 1 : 0;
complexCells[q] -= shift;
}
}
else
{
// copy this point
nx[j] = x[i];
ny[j] = y[i];
nz[j] = z[i];
nd[j] = d[i];
j += 1;
}
}
free(oSimpleCells);
free(oComplexCells);
free(visit);
free(del);
// update the points arrays
free(x);
free(y);
free(z);
free(d);
x = nx;
y = ny;
z = nz;
d = nd;
nPts = nnPts;
#if defined(MEM_CHECK)
// this dumps the state, and in the process touches all
// values. if there is a bad cell this should flag it
print(nSimpleCells, simpleCells, nComplexCells, complexCells,
complexCellLens, complexCellLocs, complexCellArraySize,
nPts, x, y, z, d);
#endif
return 0;
}
// --------------------------------------------------------------------------
template<typename index_t, typename coord_t>
int initialize(index_t & __restrict__ nSimpleCells,
coord_t * __restrict__ p0, coord_t * __restrict__ p5,
coord_t * __restrict__ p1, coord_t * __restrict__ d0,
coord_t * __restrict__ d5, coord_t * __restrict__ d1,
index_t & nPts,
coord_t *& __restrict__ x, coord_t *& __restrict__ y,
coord_t *& __restrict__ z, coord_t *& __restrict__ d,
index_t *&simpleCells)
{
// each simple cell has 3 points. first in p0 second in p5 and last in p1
nPts = 3*nSimpleCells;