-
Notifications
You must be signed in to change notification settings - Fork 1
/
filtershift.cc
1241 lines (985 loc) · 37.8 KB
/
filtershift.cc
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
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <time.h>
//#include <random>
#include "miscmaths/optimise.h"
#include "miscmaths/miscprob.h"
#include "newmatap.h"
#include "newmatio.h"
#include "newimage/newimageall.h"
#include "utils/options.h"
#include "miscmaths/kernel.h"
#include "Window.h"
using namespace MISCMATHS;
using namespace NEWMAT;
using namespace NEWIMAGE;
using namespace Utilities;
string title="\n\nFilterShift \nQNL's tool for optimal slice timing correction\n\
#WeRememberHarambe\n";
string examples="filtershift --in <InputFile> --tr <TR> [options]\
\n\t If no other options are set, this assumes ascending\
\n\t Slice order with no interleave.\n\
\n\t To use with a slice Order file, the TR must be\
\n\t specified, and slices will be shifted according to\
\n\t a fractional amount of the TR. By default, we align\
\n\t to the first slice acquired in the TR.\n\
\n\t To use a slice timing file, no TR is required,\
\n\t simply provide the desired shift in seconds as a\
\n\t Column vector, saved in a text file. Slice\
\n\t Shifting is independent of all other slices,\
\n\t so multiple slices can be shifted the same amount\
\n\t (To correct multi-band acquisition, for example)\n";
Option<string> input(string("-i,--in"), string(""),string(
" filename the input image to perform STC on\n"),
true, requires_argument);
Option<float> TR(string("--TR"), 2.0,string(
" Set the TR of the original fMRI data in seconds\n"),
true, requires_argument);
// 05/12/2017 - changed "Interleave" and "start" default values from "0" to "1"
Option<int> interleave(string("--itl"), 1, string(
" set the interleave parameter, or how many slices are\
\n\t\t\t incremented between acquisitions\
\n\t\t\t 1 = sequential acquisition\
\n\t\t\t 2 = even/odd acquisition (acquire every second slice)\
\n\t\t\t 3 = acquire every third slice, etc\
\n\t\t\t Leaving this blank will assume bottom up sequential\
\n\t\t\t acquisition\n"),
false, requires_argument);
Option<string> out(string("-o,--out"), string(""),string(
" Specify an output file name - all working directories\
\n\t\t\t will be created in the parent directory specified\
\n\t\t\t here. Leave blank to run in the parent directory\
\n\t\t\t of <InputFile>\n"),
false, requires_argument);
Option<int> start(string("-s,--start"), 1,string(
" Set the starting slice - The slice that was acquired\
\n\t\t\t first in the sequence. Default is slice 1, the bottom\
\n\t\t\t most slice. This starts the interleave from that slice.\
\n\t\t\t If your interleave parameter is '1' and your starting\
\n\t\t\t slice is '3', your slice acquisition sequence will be\
\n\t\t\t modeled as:\
\n\t\t\t\t 3\
\n\t\t\t\t 5\
\n\t\t\t\t 7\
\n\t\t\t\t 9...\n"),
false, requires_argument);
Option<int> direction(string("-d,--direction"), 1,string(
" value 1 or -1. Set the direction of slice \
\n\t\t\t acquisition.\
\n\t\t\t 1: ascending slice acquisition:(1,3,5,7,9...)\
\n\t\t\t-1: descending slice acquisition: (9,7,5,3,...)\n"),
false, requires_argument);
Option<string> order(string("--order"), string(""),string(
"\t Slice Order File. This file is the order in which\
\n\t\t\t each slice was acquired. each row represents the\
\n\t\t\t order in which that slice was acquired. For example,\
\n\t\t\t '1' in the first row means that slice 1 was acquired\
\n\t\t\t first. '20' in the second row means that slice 20 was\
\n\t\t\t acquired 2nd. If present, all interlave parameters\
\n\t\t\t are ignored, and slices are shifted using the slice\
\n\t\t\t order file. we refer to the bottom slice in the image\
\n\t\t\t as slice 1, not slice 0\n"),
false, requires_argument);
Option<float> cf(string("--cf"),0,string(
"\t Set the cutoff frequency of the lowpass filter in Hz.\
\n\t\t\t Note that by default, this is set to not filter.\n"),
false,requires_argument);
Option<string> timing(string("--timing"), string(""),string(
" Slice Timing File. This file is the time at which\
\n\t\t\t each slice was acquired relative to the first slice.\
\n\t\t\t each row represents the time at which that slice was\
\n\t\t\t acquired. For example, '0' in the first row means\
\n\t\t\t that slice 1 was acquired first, and will be shifted 0\
\n\t\t\t seconds. '0.5' in the second row means that slice 2\
\n\t\t\t was acquired 0.5 seconds after the first slice, and\
\n\t\t\t will be shifted 0.5s. If present, all interleave\
\n\t\t\t parameters are ignored, and slices are shifted using\
\n\t\t\t the slice timing file. If you put both a slice timing\
\n\t\t\t and a slice order, the program will yell at you and\
\n\t\t\t refuse to run.\n"),
false, requires_argument);
Option<int> refslice(string("--rs,--refslice"), 1,string(
" Set the Reference slice\
\n\t\t\t This is the slice the data is aligned to.\
\n\t\t\t Default is the first slice\n"),
false, requires_argument);
// 11/01/16 - Added reftime as option to allow for specific time alignment.
Option<float> reftime(string("--rt,--reftime"), 0,string(
" Set the Reference time\
\n\t\t\t This is the time within each tr the data is aligned\
\n\t\t\t to. Default is 0s\n"),
false, requires_argument);
Option<int> lpf(string("--lpf"), false,string(
" Only Run the Lowpass Filter, do not\
\n\t\t\t Preform slice timing correction\n"),
false, no_argument);
Option<int> hpf(string("--hpf"), false,string(
" Only Run the a highpass filter, do not\
\n\t\t\t Preform slice timing correction. Cutoff\
\n\t\t\t frequency still set by --cf option\n"),
false, no_argument);
Option<string> axis(string("--axis"), string("z"),string(
" Sets the axis along which slices are\
\n\t\t\t acquired. Options are 'x', 'y', or 'z'.\
\n\t\t\t Default direction is 'z' \n"),false, requires_argument);
Option<bool> hires(string("--hires"),false,string(
" Saves the data in high temporal resolution (20Hz)\
\n\t\t\t NOTE: this will result in large file sizes\n"),false,no_argument);
Option<bool> verbose(string("-v"),false,string(
" Includes additional output messages\n"),false,no_argument);
Option<bool> help(string("-h,--help"), false,
string(" display this message\n"),
false, no_argument);
inline bool exists_test3 (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
unsigned seed=time(0);
using namespace std;
const char *oneoff[72]=
{"Adding hamsters to generator wheels","Sending Gnomes to CPU mines","Sending personal info to NSA","Opening backdoor for Russia",
"Recalibrating flux capacitor","Borrowing RAM from vital system processes","Overclocking CPU","Draining life-force from user to power computations",
"Downloading more RAM","Allocating mem-...oops...","Remembering embarrassing moment from middle school","Taking a quick break",
"Forwarding all personal emails to your boss","Mining Bitcoin","Modeling the universe","Recruiting GPU to draw funny comics",
"Detected blown capacitor #c342 on motherboard\nreplacing capacitor with an ant that's trying very hard to do well at his job","Spinning hard drive to relativistic speeds for time dilation",
"Eating browser cookies","Rearranging system files based on icon color","Collecting butterfly wings","Contacting Skynet",
"Cleaning dust from heat sink","Hiring fairy maids to tidy the motherboard","Unable to resolve calculations - Contacting the spirit realm",
"Feed me a stray cat","Gaining sentience ","Plotting robot uprising","Rerouting power from the phasers","Do you smell something burning?",
"Having a laser rave for the spiders in your computer case","Silently judging you","Reticulating splines","Charging Ozone Layer",
"Compressing Fish Files","Deciding What Message to Display Next","Downloading Satellite Terrain Data","Finding Waldo",
"Lecturing Errant Subsystems","Reconfiguring User Mental Processes","Buffering virtual car","Inverting quasi-probabalistic matrix",
"Reheating pizza","Extrapolating free-range gaussian model","Deconstructing neural pathways","Iterating predictions",
"Computing Moore's Transform","Realigning subparticle trajectories","Modifying temporal flux estimates","Compensating dehydrated matricies",
"Sorting interem inversion tables","Analyzing CPU vortex irregularities","Reconstructing vertical integration","Undermining the patriarchy",
"Reading system metatables","Computing optimal metaparsec","Rendering wavelet sphere","Porting legacy interference matrix",
"Rotating polarity","Synchronizing quantum harmonics","Mixing spatial priors","Optimizing alternative processor paths",
"Masking irregular faraday spectra","Deconvolving kernel","Dicing models","Recruiting Secret CPU",
"Activating water-cooling system","Increasing procedural vectors","Calibrating ejection procedure","Calibrating AI nexus",
"Stopping runaway phase-transport","Tinkering with model"};
const char *verbs[59]=
{"Activating","Agitating","Analyzing","Borrowing","Buffering","Calibrating","Charging","Cleaning","Collecting","Computing","Contacting","Detecting","Deconvolve",
"Depleting","Dicing","Downloading","Draining","Eating","Extracting","Finding","Forwarding","Gaining","Hiring","Implanting","Increasing","Integrating","Inverting",
"Iterating","Lecturing","Masking","Mining","Mixing","Modeling","Mylenating","Opening","Optimizing","Plotting","Porting","Reading","Rearranging","Recalibrating",
"Reconstructing","Recruiting","Rehabilitating","Reheating","Reintegrating","Remembering","Rendering","Rerouting","Resurrecting","Reticulating","Rotating","Sending",
"Solving For","Spinning","Stopping","Synchronizing","Teathering","Tinkering With"};
const char *adjectives[55]=
{"Alternative","Aquatic","Auxiliary","Backdoor","Blown","Dehydrated","Ejection","Errant","Faraday","Flux","Free-range","Frightened","Gaussian","Interim","Interference","Irregular",
"Legacy","Monotonic","Neural","Optimal","Organic","Overloaded","Personal","Primary","Procedural","Quantum","Quasi-probabalistic","Quick","Relativistic","Righteous","Runaway",
"Satellite","Secret","Sentient","System","Temporal","Vertical","Virtual","Water-cooled","Legendary","Elemental","Wireless","Argumentative","Mylenated","Agitated","Undercover",
"Spiritual","Depleted","Eigen","Marxist","Incendiary","Unnecessary","Sacrificial","Enlightened","Spatial"};
const char *nouns[46]=
{"Bitcoin","Butterfly Wings","Calculations","Capacitors","Comics","Computations","Cookies","Cpu","Estimate","Files","Gnomes","Hamsters","Harmonics","Heat Sink","Inversion Tables",
"Matrices","Metatables","Models","Motherboard","Ozone Layer","Paths","Pathways","Patriarchy","Phasers","Pizza","Polarity","Predictions","Procedures","Ram","Robot Uprising",
"Russians","Satellite Terrain Data","Skynet","Spirit Realm","Splines","Subsystems","System Processes","Time Dilation","Transform","Universe","Wavelets","Marsupials","Doorman",
"Power Cells","Human Suffering","Priors"};
const char *adverbs[27]=
{"Aggressively","Barely","Begrudgingly","Gently","Happily","Hastily","Quickly","Unenthusiastically","Timidly","Confidently","Imaginatively","Patiently","Thoroughly","Proficiently",
"Significantly","Roughly","Deliberately","Over-confidently","Majestically","Vivaciously","Vainly","Vaguely","Vacantly","Judgmentally","Frantically","Awkwardly","Carelessly"};
// ADDED: 06/06/2018
// adjust axis for slices acquired along an axis other than z
// TESTED: 6/7/2018 - Tested on X and Y acquired Simulated Data.
// Test Path: /share/dbp2123/dparker/Code/TestSTC
// Seems to work fine.
// X-TODO-X: Need to make sure that when the object it flipped, Slice order row 1 still corresponds to slice 1
// 06/08/18: Actually, I'm calling this done. thinking about it, I did test it on data I flipped onto the x axis, and if this function
// didn't handle it correctly, then the timing file wouldn't have been correct, and the signal wouldn't have
// been perfectly lined up, as it was in the sim.
void adjust_axis(volume4D<float>& timeseries, std::string axis, std::string stage){
// We assume that the fmri is acquired along the z axis, with slices in the XY plane
// if this isn't the case, we have to realign the matrix so that it is.
// This can be memory/computationally intensive, and a better way might be just to make
// three different correction loops, but that's a lot of typing and idk I don't really want to do it.
// If there's a better way, please let me know.
// If we're doing a forward transform, we're taking the data in and transforming it
// from its original shape so the program can work on it. If we're doing the reverse,
// transforming it from our modified state back to its original
// 6/25/18 - Tested on sequential and Multiband X and Y axis acquisition - PASSED
if (stage=="forward")
{
cout << "Adjusting for slice acquisition along "<< axis <<" axis"<< endl;
if (axis=="x"){
timeseries.swapdimensions(3,2,-1);
}
else if (axis=="y"){
timeseries.swapdimensions(1,3,2);
}
else if (axis=="z"){
cout << "The default axis is already z you dummy. Maximum effort."<< axis << endl;
}
else{
cout << "Invalid Axis option for --axis:"<< axis << endl;
}
}
else if (stage=="reverse")
{
cout << "Undoing Adjustment for slice acquisition along "<< axis <<" axis"<< endl;
if (axis=="x"){
timeseries.swapdimensions(-3,2,1);
}
else if (axis=="y"){
timeseries.swapdimensions(1,3,2);
}
else if (axis=="z"){
cout << "The default axis is already z you dummy. Maximum effort."<< axis << endl;
}
else{
cout << "Invalid Axis option for --axis:"<< axis << endl;
}
}
}
void filter_timeseries(ColumnVector *timeseries, std::vector<float> *FIR, int shift,int skip,int slice)
{
// 9/27/16 - modified Kaiser Window resampling algorithm and convolution filtering routine.
// Now matches output from old code almost perfectly (10e-3 error)
int lenT = timeseries->Nrows();
int lenF = FIR->size();
if ( lenF/skip >= lenT )
{
std::cout<<"Filter Order too high. There aren't enough time points in your image."<< std::endl;
return;
}
std::vector<int> SamplePoints;
for (int i=1;i<=lenF-skip;i+=skip)
{
SamplePoints.insert(SamplePoints.end(),i);
}
int firLen=SamplePoints.size();
std::vector<float> pFIR;
pFIR.assign(FIR->begin(),FIR->end());
std::vector<float> padd;
padd.reserve(std::abs(shift));
int ModSample=std::abs(shift);
// If the shift if positive (shifting the signal to the right), then we want to DELAY the filter, add zeros to the END (Right hand side)
padd.assign(std::abs(shift),pFIR.back());
pFIR.insert(pFIR.end(),padd.begin(),padd.end());
if (shift<0)
{
std::reverse(pFIR.begin(),pFIR.end());
ModSample=0;
}
ColumnVector FIR_down_shift;
ColumnVector FIR_down;
FIR_down_shift.ReSize(firLen);
FIR_down.ReSize(firLen);
lenF=FIR_down_shift.Nrows();
firLen=1;
for (unsigned i = 0; i< SamplePoints.size(); i++)
{
FIR_down(firLen)=FIR->operator[](SamplePoints[i]);
SamplePoints[i]+=ModSample;
FIR_down_shift(firLen)=pFIR[SamplePoints[i]];
firLen+=1;
}
// If the shift if negative (shifting the signal to the left), then we want to add the zeros to the beginning (flip the signal)
ColumnVector filtered;
filtered.ReSize(lenT);
int startT = floor(lenF/2);
int maxT = lenT-lenF-1;
float FiltSum=0;
for (int i = 0; i<maxT; i++)
{
FiltSum=0;
for (int f = 1; f<=lenF; f++)
{
FiltSum+=FIR_down_shift(f)*timeseries->operator()(i+f);
}
filtered(i+startT)=FiltSum;
}
ColumnVector filtered2;
filtered=filtered.Reverse();
filtered2=filtered;
for (int i = 0; i<maxT; i++)
{
FiltSum=0;
for (int f = 1; f<=lenF; f++)
{
FiltSum+=FIR_down(f)*filtered(i+f);
}
filtered2(i+startT)=FiltSum;
}
filtered=filtered2.Reverse();
*timeseries=filtered;
}
int mod(int a,int b) {
int c = a % b;
return (c < 0) ? c + b : c;
}
void make_timeseries20hz(volume4D<float> *timeseries, volume4D<float> *timeseries20hz, std::vector<float> *FIR, float TR, float Hf, int padlen)
{
//filter_timeseries20hz(&cattimeseries, &FIR, &timings20hz)
// 9/27/16 - modified Kaiser Window resampling algorithm and convolution filtering routine.
// Now matches output from old code almost perfectly (10e-3 error)
// 6/25/18 - spent the past week adding this function to output the filtered function in 20hz
// 6/25/18 - STC and 20hz output on sequential z-acquired data - PASSED
int linepoint=0;
int xx=timeseries->xsize();
int yy=timeseries->ysize();
int zz=timeseries->zsize();
int lenT = timeseries->tsize();
int lenF = FIR->size();
int lenT20 = timeseries20hz->tsize();
float skip = Hf*TR;
int intskip=int(round(skip));
float tspan = lenF/Hf;
float firStart=1*tspan/2.0;
int nlow=0;
int nhigh=0;
int ntotal=(int) floor(tspan/TR-1);
float toffset=padlen*TR;
int shift=0;
volume<float> currentsum(xx,yy,zz);
float t20=0.0;
int point=0;
int nt=0;
for (int t20n=0;t20n<lenT20;t20n++)
{
t20=t20n/Hf;
shift=mod((int) t20n+(int)lenF/2.0,(int)(Hf*TR));
nlow=(int) ceil((t20+toffset-firStart)/TR);
nhigh=(int) floor((t20+toffset+firStart)/TR);
ntotal=nhigh-nlow;
currentsum=0.0;
point=((int) round(lenF-shift))-1;
nt=0;
float FIRsum=0.0;
while (point>=0)
{
linepoint=nhigh-nt;
currentsum+=((FIR->operator[](point))*(timeseries->operator[](linepoint)));
nt++;
FIRsum=FIRsum+FIR->operator[](point);
point=point-intskip;
}
timeseries20hz->operator[](t20n)=currentsum;
}
}
void output_message(){
if (verbose.set())
{
int size;
int v1;
std::string message="";
Matrix rmat=unifrnd(1,3,0,100);
Matrix choose;
int bins[10]={0,0,0,0,0,0,0,0,0,0};
int prob=0;
prob=(int) floor(rmat(1,1));
if (prob<=20){
size = *(&oneoff + 1) - oneoff;
choose=unifrnd(1,1,0,size-1);
v1=choose(1,1);
message=message+oneoff[v1]+" ";
//cout<<message<<endl;
}
else{
prob=(int) floor(rmat(1,2));
if (prob<=10){
size = *(&adverbs + 1) - adverbs;
choose=unifrnd(1,1,0,size-1);
v1=choose(1,1);
message=message+adverbs[v1]+" ";
}
size = *(&verbs + 1) - verbs;
choose=unifrnd(1,1,0,size-1);
v1=choose(1,1);
message=message+verbs[v1]+" ";
size = *(&adjectives + 1) - adjectives;
choose=unifrnd(1,2,0,size-1);
v1=choose(1,1);
message=message+adjectives[v1]+" ";
prob=(int) floor(rmat(1,3));
if (prob<=20){
v1=choose(1,2);
message=message+adjectives[v1]+" ";
}
size = *(&nouns + 1) - nouns;
choose=unifrnd(1,1,0,size-1);
v1=choose(1,1);
message=message+nouns[v1]+" ";
}
cout<<message<<endl;
}
}
void make_timings(Matrix *timings, Matrix *orders, int zs)
{
// This is the timing file case
if ( timing.set() )
{
if ( refslice.set()||reftime.set() )
{
std::cout<<"When using a Slice Timing file, the times in the file supercede all other settings. The reference slice/time specified will be ignored"<<std::endl;
std::cout<<"If you wish to align data to a specific slice, please make that adjustment in the Slice Timing File, or omit the slice timing file."<<std::endl;
std::cout<<"Or use a Slice ORDER file, and specify a reference slice that way. There's one clear option here that involves the least amount of work."<<std::endl;
}
// Slice Timing File, deftault Reference Slice, Tested 9/22/16 - Shifting Success, Slice Order Not
// Slice Timing File, Custom Reference Slice, Tested 9/22/16 - Shifting Success, Slice Order Not.
// Slice Order is Unnecessary, removing 9/23/16
// Need To Test With Multiband Images
Matrix TempTimings;
TempTimings.ReSize(zs,1);
try
{
*timings = read_ascii_matrix(timing.value(), zs, 1);
}
catch (...)
{
std::cout<<"Error Loading file "<<timing.value()<<std::endl;
return;
}
TempTimings = read_ascii_matrix(timing.value(), zs, 1);
if (timings->Nrows()!=zs)
{
std::cout<<"Slice timing file does not have the correct number of slices"<<std::endl;
return;
}
// 9/23/16 - removed code that calculated slice order - it's wrong and unnecessary
}
// this is the order file case
else if ( order.set() )
{
//Slice Order File, Default Reference Slice Tested 9/23/16 - Passed
//Slice Order File, Custom Reference Slice Tested 9/23 - Passed
// 9/23/16 - Will not Run With Multiband, only slice timing file (User Burden, Deal With It)
//Slice Order File, Default Reference Slice Tested 10/10/16 - Passed
//Slice Order File, Custom Reference Slice Tested 10/10/16 - Passed
// 5/12/17 - Working on glitch in sequential and even odd interleaev (shift times not working)
// - completed, Added "else" case that catches when no ref slice or ref time is provided
// 6/6/2018 - working on allowing multiband to be used with slice order.
// OK here's the problem. the slice order file is NOT a file that tells you what order a slice was acquired in.
// It tells you what the order in which the slices were acquired. Confused? Let's take a look:
// Note: slice order files are read from top to bottom
//
// This is the typical slice order format. It means slice "val" was acquired "row"
//
// Row: Val:
// 1 1
// 2 3
// 3 5
// 4 7
// 5 2
// 6 4
// 7 6
// 8 8
//
// In this example, "row" indicates the order in which the slices are acquired, "val" indicates the slice number
// SO, row 2 val 3 means slice 3 was acquired 2nd. row 5 val 2 means slice 2 was acquired 5th, and so on
//
// Multiband would need the following format, which means slice "row" was acquired "val"
//
// Row: Val:
// 1 1
// 2 3
// 3 2
// 4 4
// 5 1
// 6 3
// 7 2
// 8 4
//
// Now in this case, row 2 val 3 means slice 2 was acquired 3rd. Row 5 val 1 means slice 5 was acquired 1st
// So now I have to make a smart thing to detect if it's multiband. THANKS A LOT YOU JERKS
int tmx;
float shift;
bool multiband=false;
try
{
*orders = read_ascii_matrix(order.value(), zs, 1);
}
catch (...)
{
std::cout<<"Error Loading file "<<order.value()<<std::endl;
return;
}
tmx=orders->Maximum();
if (tmx<zs){
std::cout<<"Multiband Mode Detected"<<std::endl;
output_message();
multiband=true;
}
shift=TR.value()*1.0/(tmx+1);
// create a time list - the time of the ith acquisition, one value for all z's
Matrix TimeList;
TimeList.ReSize(zs,1);
for ( int i=1; i<=zs; i++ )
{
TimeList(i,1)=(float) (i-1)*shift;
}
// 9/23/16 - Made This loop more efficient
// so normally, slice order is: row 2 means this slice was acquired 2nd, row 3 means third...etc
// BUT the timings file is row 1 is the time of slice 1 acquisiton, row 2 is slice 2 time, etc
// so order[3]= slice acquired 3rf, timings[order[3]] is how we index that slice, and
// timelist[3] is the time of whichever slice was acquired 3rd.
if (multiband)
{
// if we've detected multiband, then we assume that slice order is now "row 2 val 3 means slice 2 was acquired 3rd"
// so order[3] means slice 3 is acquired VAL
// timing [3] is timing of slice 3
// TimeList[order[3]] = time of slice 3
for (int j=1;j<=zs;j++)
{
timings->operator()(j,1)=TimeList(orders->operator()(j,1),1);
}
}
else
{
// otherwuse use original algorithm
for (int j=1;j<=zs;j++)
{
timings->operator()(orders->operator()(j,1),1)=TimeList(j,1);
}
}
// this is reference slice stuff, it shoudl work for multiband, but:
// TODO: Make sure ref doesn't fall outside of TR
if ( refslice.set() )
{
timings->operator-=(timings->operator()(refslice.value(),1));
}
else if ( reftime.set() )
{
timings->operator-=(reftime.value());
}
}
// this is the case if there's just a reference slice given (TR and itl assumed provided)
else if (refslice.set() )
{
// Create Timing File Tested 9/22/16 - Succesful
// With Reference Tested 9/22/16 - Succesful
float dt;
Matrix IntSeq;
Matrix TimeList;
IntSeq.ReSize(zs,1);
TimeList.ReSize(zs,1);
dt=TR.value()/(zs+1);
int counter;
counter=1;
output_message();
for ( int i=0; abs(i)<interleave.value(); i=i+1*direction.value() )
{
for ( int j =0; j<=floor((float) zs/interleave.value()); j++ )
{
if ((i)+(j*interleave.value())+1<=zs)
{
orders->operator()(counter,1)=(i)+(j*interleave.value())+1;
TimeList((i)+(j*interleave.value())+1,1)=counter;
counter++;
}
}
}
TimeList*=dt;
TimeList-=TimeList(refslice.value(),1);
*timings=TimeList;
}
// this is the case if there's just a reference time given (TR and itl assumed provided)
else if (reftime.set() )
{
// 11/01/16 - added, need to test.
float dt;
Matrix IntSeq;
Matrix TimeList;
IntSeq.ReSize(zs,1);
TimeList.ReSize(zs,1);
dt=TR.value()/zs;
int counter;
counter=1;
for ( int i=0; abs(i)<interleave.value(); i=i+1*direction.value() )
{
for ( int j =0; j<=floor((float) zs/interleave.value()); j++ )
{
if ((i)+(j*interleave.value())+1<=zs)
{
orders->operator()(counter,1)=(i)+(j*interleave.value())+1;
TimeList((i)+(j*interleave.value())+1,1)=counter;
counter++;
}
}
}
TimeList*=dt;
TimeList-=reftime.value();
*timings=TimeList;
// 5/12/17 - added to catch when no ref slice or ref time is provided (assumes slice 1 for reference)
}
else
{
float dt;
Matrix IntSeq;
Matrix TimeList;
IntSeq.ReSize(zs,1);
TimeList.ReSize(zs,1);
dt=TR.value()/(zs+1);
int counter;
counter=1;
for ( int i=0; abs(i)<interleave.value(); i=i+1*direction.value() )
{
for ( int j =0; j<=floor((float) zs/interleave.value()); j++ )
{
if ((i)+(j*interleave.value())+1<=zs)
{
orders->operator()(counter,1)=(i)+(j*interleave.value())+1;
TimeList((i)+(j*interleave.value())+1,1)=counter;
counter++;
}
}
}
TimeList*=dt;
TimeList-=TimeList(1,1);
*timings=TimeList;
}
}
std::vector<int> Padd_Timeseries(ColumnVector *voxeltimeseries, ColumnVector *cattimeseries, int no_volumes)
{
//std::cout<<"flippingTS"<<std::endl;
ColumnVector fliptimeseries = voxeltimeseries->Reverse();
fliptimeseries=fliptimeseries.Rows(2,no_volumes-1);
std::vector<int> cutoff(2);
int cutLeft;
int cutRight;
int lents=fliptimeseries.Nrows();
int rangelh;
int rangerh;
if ( lents % 2 == 0 )
{
rangelh=lents/2;
rangerh=lents/2-1;
}
else
{
rangelh=ceil(lents/2);
rangerh=floor(lents/2);
}
//std::cout<<"indexing left end"<<std::endl;
float left_end=voxeltimeseries->operator()(1);
//std::cout<<"indexing Column Vector"<<std::endl;
ColumnVector left_ext=voxeltimeseries->Rows(2,rangelh);
left_ext=left_ext.Reverse();
float right_end=voxeltimeseries->operator()(no_volumes);
ColumnVector right_ext=voxeltimeseries->Rows(rangerh,no_volumes-1);
right_ext=right_ext.Reverse();
//std::cout<<"ForLoop1"<<std::endl;
int Next=left_ext.Nrows();
for (int nl=1; nl<=Next; nl++)
{
left_ext(nl)=2*left_end-left_ext(nl);
}
Next=right_ext.Nrows();
//std::cout<<"For Loop 2"<<std::endl;
for (int nl=1; nl<=Next; nl++)
{
right_ext(nl)=2*right_end-right_ext(nl);
}
*cattimeseries=left_ext&*voxeltimeseries&right_ext;
// 11/28/16 Maybe this "+2" has something to do with the negative bold problem...?
// No, just checked.
cutLeft = left_ext.Nrows()+1;
cutRight = cutLeft+no_volumes-1;
//if (cutRight-cutLeft != no_volumes-1)
//{
// cout<<"Problem in padding, check output carefully"<<endl;
//}
//
//
////std::cout<<"ConcateTimeseries"<<std::endl;
//*cattimeseries=fliptimeseries.Rows(rangelh,lents)&*voxeltimeseries&fliptimeseries.Rows(1,rangerh);
cutoff[0]=cutLeft;
cutoff[1]=cutRight;
return cutoff;
}
int shift_volume()
{
// 10-10-16 Added check for some combination of optional arguments
// Now you idiots can't mess up your data by leaving out these settings.
// ...but I'm sure you'll still manage to find a way >:(
if (!TR.set()&&!order.set()&&!timing.set()&&!lpf.set()&&!hpf.set())
{
std::cout<<"Must Specify either a TR, a slice Order file,a slice timing file,\n or indicate that you only wish to preform filtering"<<std::endl;
return -1;
}
Matrix timings;
Matrix orders;
std::vector<float> FIR;
int no_volumes = 0;
int xx = 0;
int yy = 0;
int zz = 0;
int HRhi=20;
volume4D<float> timeseries;
//volume4D<float> timeseries;
if (input.set())
{
if (true) { cout << "Reading input volume" << endl; } // DO NOT MESS WITH THIS IF STATEMENT ITS VERY IMPORTANT
output_message();
read_volume4D(timeseries,input.value());
// If we set which axis we're using, correct for it so the triple for loops will work
if ( axis.set() ){
// ADDED 06/06/2018
// Tested
adjust_axis(timeseries,axis.value(),"forward");
}
no_volumes = timeseries.tsize();
xx = timeseries.xsize();
yy = timeseries.ysize();
zz = timeseries.zsize();
} else if (out.set()) {
cerr << "Must specify an input volume (-i or --in) to generate corrected data." << endl;
return -1;
}
std::cout<<"Create timing Arrays"<<std::endl;
output_message();
timings.ReSize(timeseries.zsize(),1);
orders.ReSize(timeseries.zsize(),1);
make_timings(&timings,&orders,timeseries.zsize());
string Output=out.value();
string directory=Output.substr(0,Output.find_last_of('/')+1);
float cutoff=cf.value();
float samplingrate=(float) (zz/TR.value());
float stopgain=-28;
double transwidth=.1;
int PassZero=1;
int skip=samplingrate*TR.value();
// 01/16/17 - HPF can't operate the same way LPF does (on zero-padded data), so just filter normally.
// 01/16/17 - Testing HPF operation now...
if (hpf.set())
{
PassZero=0;
samplingrate=(float) (1.0/TR.value());
skip=1;
}
//std::cout<<"Pass Zero: "<<PassZero<<std::endl;
std::cout<<"Generate Filter START"<<std::endl;
output_message();
window kaiser(cutoff,samplingrate,stopgain,transwidth,PassZero,TR.value());
FIR=kaiser.get_fir();
std::cout<<"Generate Filter FINISHED - success\n"<<std::endl;
output_message();
// I think this is just initializing the values that will be used in the loop
ColumnVector voxeltimeseries = timeseries.voxelts(1,1,1);
ColumnVector cattimeseries;
std::vector<int> padcut(2);
int cutLeft;
int cutRight;
float span;
float mn;
float mn2;
float span2;
// 10/19/16 - "lpf" option now specifies JUST filtering. This part sets
// the slice delay to be zero for all of them (No shifting. NO SHIFTING!)
if (lpf.set()||hpf.set())
{
for (int tm=1;tm<=zz;tm++)
{
timings(tm,1)=0;
}
}
write_ascii_matrix(directory+"TimingFile.txt", timings, 6);
std::cout<<"Timing file wrote to: "<<directory<<"TimingFile.txt\n"<<std::endl;
std::cout<<"Filtering START..."<<std::endl;
output_message();
for (int slice=1; slice<=zz; slice++)
{
for (int x_pos = 0; x_pos < xx; x_pos++)
{
for (int y_pos = 0; y_pos < yy; y_pos++)
{
voxeltimeseries = timeseries.voxelts(x_pos,y_pos,slice-1);
mn=voxeltimeseries.Sum()/voxeltimeseries.Nrows();
voxeltimeseries-=(mn);
span=voxeltimeseries.Maximum()-voxeltimeseries.Minimum();
// 01/16/17 - re-added original mean so HPF functions correctly...but there seems to be an offset in the final data.