@@ -195,10 +195,10 @@ DeNoise[dat_, sigi_, filt_, OptionsPattern[]] := Module[{kern, out, type, dimsig
195
195
Return [Message [DeNoise ::dim , dimsig , dimdat ]]
196
196
];
197
197
];
198
-
198
+
199
199
(*Check dimensions,filter must be of lower order than data*)
200
200
If [Length [filt ] > ArrayDepth [data ], Return [Message [DeNoise ::filt , Length [filt ], ArrayDepth [data ]]]];
201
-
201
+
202
202
(*Create filter*)
203
203
kern = Switch [type = OptionValue [DeNoiseKernel ],
204
204
"Box" , kern = BoxMatrix [filt ]/ Total [Flatten [BoxMatrix [filt ]]],
@@ -207,7 +207,7 @@ DeNoise[dat_, sigi_, filt_, OptionsPattern[]] := Module[{kern, out, type, dimsig
207
207
_ , Return [Message [DeNoise ::kern , type ]];
208
208
];
209
209
If [OptionValue [DeNoiseMonitor ], PrintTemporary ["Using " <> type <> " kernel." ]];
210
-
210
+
211
211
out = ToPackedArray @ N @ Nest [DeNoisei [# , sig , filt , kern ] & , data , OptionValue [DeNoiseIterations ]];
212
212
If [
213
213
ArrayQ [out ], Return [Clip [out , {0.9 Min [data ], 1.1 Max [data ]}]],
@@ -320,10 +320,10 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
320
320
totalItt , output , j , sigi , zm , ym , xm , zp , yp , xp , fitdata , sigo , Nes , datn ,
321
321
weight , posV , leng , nearPos , p , pi , pos , np
322
322
},
323
-
323
+
324
324
(*tollerane if>0 more noise components are kept*)
325
325
{mon , wht , tol , ker , clip , comp } = OptionValue [{MonitorCalc , PCAWeighting , PCATolerance , PCAKernel , PCAClipping , PCAComplex }];
326
-
326
+
327
327
(*concatinate complex data and make everything numerical to speed up*)
328
328
comps = False ;
329
329
data = ToPackedArray @ N @ If [comp ,
@@ -332,29 +332,29 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
332
332
Join [data [[1 ]], data [[2 ]], 2 ],
333
333
datai
334
334
];
335
-
335
+
336
336
{min , max } = 1.1 MinMax [Abs [data ]];
337
337
maskd = Unitize @ Total @ Transpose [data ];
338
338
mask = ToPackedArray [N @ (maski maskd )];
339
339
sigm = ToPackedArray [N @ sigmai ];
340
-
340
+
341
341
(*get data dimensions*)
342
342
dim = {zdim , ddim , ydim , xdim } = Dimensions [data ];
343
-
343
+
344
344
(*define sigma*)
345
345
sigm = If [NumberQ [sigm ], ConstantArray [sigm , {zdim , ydim , xdim }], sigm ];
346
-
346
+
347
347
Switch [OptionValue [Method ],
348
348
(*--------------use similar signals--------------*)
349
349
"Similarity" ,
350
350
(*vectorize data*)
351
351
{data , posV } = DataToVector [data , mask ];
352
352
sigm = First @ DataToVector [sigm , mask ];
353
-
353
+
354
354
(*define runtime parameters*)
355
355
m = Length [data [[1 ]]];
356
356
n = Round [ker ^ 3 ];
357
-
357
+
358
358
(*parameters for monitor*)
359
359
leng = Length [data ];
360
360
pos = Range @ leng ;
@@ -390,54 +390,54 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
390
390
If [mon , PrintTemporary [ProgressIndicator [Dynamic [j ], {0 , leng }]]];
391
391
{m , n } = MinMax [{m , n }];
392
392
Map [(j ++ ;
393
-
393
+
394
394
p = # ;
395
395
pi = First @ p ;
396
-
396
+
397
397
(*perform the fit and reconstruct the noise free data*)
398
398
{sigo , Nes , datn } = PCADeNoiseFit [data [[p ]], {m , n }, sigm [[pi ]], tol ];
399
-
399
+
400
400
(*get the weightes*)
401
401
weight = If [wht , 1. / (m - Nes ), 1. ];
402
-
402
+
403
403
(*sum data and sigma and weight for numer of components*)
404
404
datao [[p ]] + = weight datn ;
405
405
sigmat [[p ]] + = weight sigo ;
406
406
weights [[p ]] + = weight ;
407
-
407
+
408
408
(*output sig, Nest and iterations*)
409
409
sigmati [[pi ]] = sigo ;
410
410
nmati [[pi ]] = Nes ;
411
411
) & , nearPos ];
412
-
412
+
413
413
(*make everything in arrays*)
414
414
datao = VectorToData [DivideNoZero [datao , weights ], posV ];
415
415
sigmat = VectorToData [DivideNoZero [sigmat , weights ], posV ];
416
416
output = {VectorToData [sigmati , posV ], VectorToData [nmati , posV ]};
417
-
417
+
418
418
,
419
419
(*--------------use patch---------------*)
420
-
420
+
421
421
_ ,
422
422
ker = If [EvenQ [ker ], ker - 1 , ker ];
423
-
423
+
424
424
(*prepare data*)
425
425
data = RotateDimensionsLeft [Transpose [data ]];
426
-
426
+
427
427
(*define runtime parameters*)
428
428
{m , n } = MinMax [{ddim , ker ^ 3 }];
429
429
off = Round [(ker - 1 )/ 2 ];
430
-
430
+
431
431
(*ouput data*)
432
432
datao = 0. data ;
433
433
weights = sigmat = datao [[All , All , All , 1 ]];
434
-
434
+
435
435
(*parameters for monitor*)
436
436
j = 0 ;
437
437
start = off + 1 ;
438
438
{zdim , ydim , xdim } = {zdim , ydim , xdim } - off ;
439
439
totalItt = Total [Flatten [mask [[start ;; zdim , start ;; ydim , start ;; xdim ]]]];
440
-
440
+
441
441
(*perform denoising*)
442
442
If [mon , PrintTemporary [ProgressIndicator [Dynamic [j ], {0 , totalItt }]]];
443
443
output = Table [
@@ -450,32 +450,32 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
450
450
sigi = sigm [[z , y , x ]];
451
451
{{zm , ym , xm }, {zp , yp , xp }} = {{z , y , x } - off , {z , y , x } + off };
452
452
fitdata = Flatten [data [[zm ;; zp , ym ;; yp , xm ;; xp ]], 2 ];
453
-
453
+
454
454
(*perform the fit and reconstruct the noise free data*)
455
455
{sigo , Nes , datn } = PCADeNoiseFit [fitdata , {m , n }, sigi , tol ];
456
-
456
+
457
457
(*reshape the vector into kernel box and get the weightes*)
458
458
datn = Fold [Partition , datn , {ker , ker }];
459
459
weight = If [wht , 1. / (m - Nes ), 1. ];
460
-
460
+
461
461
(*sum data and sigma and weight for numer of components*)
462
462
datao [[zm ;; zp , ym ;; yp , xm ;; xp , All ]] + = (weight datn );
463
463
sigmat [[zm ;; zp , ym ;; yp , xm ;; xp ]] + = weight sigo ;
464
464
weights [[zm ;; zp , ym ;; yp , xm ;; xp ]] + = weight ;
465
-
465
+
466
466
(*output sig, Nest and iterations*)
467
467
{sigo , Nes }
468
468
],
469
469
{z , start , zdim }, {y , start , ydim }, {x , start , xdim }];
470
470
471
471
(*make everything in arrays*)
472
472
output = ArrayPad [# , off ] & /@ RotateDimensionsRight [output ];
473
-
473
+
474
474
(*correct output data for weightings*)
475
475
datao = Transpose @ RotateDimensionsRight [Re @ DivideNoZero [datao , weights ]];
476
476
sigmat = DivideNoZero [sigmat , weights ];
477
477
];
478
-
478
+
479
479
(*define output, split it if data is complex*)
480
480
datao = Which [
481
481
comp , len = Round [Length [datao [[1 ]]]/ 2 ]; {datao [[All ,1 ;; len ]], datao [[All ,len + 1 ;; - 1 ]]},
@@ -501,18 +501,18 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
501
501
PCADeNoiseFit [data_ , {m_ , n_ }, sigi_ ? NumberQ , toli_ ] := Block [{
502
502
trans , xmat , xmatT , val , mat , pi , sig , xmatN , tol , out
503
503
},
504
-
504
+
505
505
(*perform decomp*)
506
506
trans = Subtract @@ Dimensions [data ] > 0 ;
507
507
{xmat , xmatT } = If [trans , {Transpose @ data , data }, {data , Transpose @ data }];
508
508
{val , mat } = Reverse /@ Eigensystem [xmat . xmatT ];
509
509
510
510
(*if sigma is given perform with fixed sigma,else fit both*)
511
511
{pi , sig } = GridSearch [Re [val ], m , n , sigi ];
512
-
512
+
513
513
(*constartin pi plus tol*)
514
514
tol = Round [Clip [pi - toli , {1 , m }]];
515
-
515
+
516
516
(*give output,simga,number of noise comp,and denoised matrix*)
517
517
out = Transpose [mat [[tol ;; ]]] . (mat [[tol ;; ]] . xmat );
518
518
{sig , tol , If [trans , Transpose @ out , out ]}
@@ -521,21 +521,21 @@ PCADeNoiseFit[data_, {m_, n_}, sigi_?NumberQ, toli_] := Block[{
521
521
522
522
GridSearch = Compile [{{val , _ Real , 1 }, {m , _ Integer , 0 }, {n , _ Integer , 0 }, {sig , _ Real , 0 }}, Block [
523
523
{valn , gam , sigq1 , sigq2 ,p , pi },
524
-
524
+
525
525
(*calculate all possible values for eq1 and eq2*)
526
526
valn = val [[;; - 2 ]]/ n ;
527
527
p = Range [m - 1 ];
528
528
gam = 4 Sqrt [N [p / (n - (m - (p + 1 )))]];
529
529
sigq1 = Accumulate [valn ]/ p ;
530
530
sigq2 = (valn - First [valn ])/ gam ;
531
-
531
+
532
532
(*find at which value eq1>eq2*)
533
533
pi = If [sig === 0. ,
534
534
Total [1 - UnitStep [sigq2 - sigq1 ]],
535
535
Total [1 - UnitStep [Mean [{sigq1 , sigq2 }] - sig ^ 2 ]]
536
536
];
537
537
pi = If [pi <= 0 , 1 , pi ];
538
-
538
+
539
539
(*give output*)
540
540
{pi , Sqrt [Ramp [(sigq1 [[pi ]] + sigq2 [[pi ]])/ 2 ]]}
541
541
], RuntimeOptions -> "Speed" , Parallelization -> True ];
@@ -558,11 +558,11 @@ NNDeNoise[data_, mask_, opts : OptionsPattern[]] := Block[{
558
558
back = Round [mask Mask [NormalizeMeanData [data ], OptionValue [NNThreshold ]]];
559
559
{dat , coor } = DataToVector [data , back ];
560
560
dat = ToPackedArray @ N @ dat ;
561
-
561
+
562
562
(*Get dimensions, training sample and define network*)
563
563
n = Range @ Length @ First @ dat ;
564
564
ran = 1.1 MinMax [dat ];
565
-
565
+
566
566
(*trian network per volume and generate denoised data*)
567
567
(*DistributeDefinitions[dat, n];*)
568
568
dat = Monitor [Table [
@@ -574,7 +574,7 @@ NNDeNoise[data_, mask_, opts : OptionsPattern[]] := Block[{
574
574
];
575
575
trained [dati ]
576
576
, {i , n }], ProgressIndicator [Dynamic [i ], {0 , Max [n ]}]];
577
-
577
+
578
578
ToPackedArray @ N @ Clip [Transpose [VectorToData [# , coor ] & /@ dat ], ran ]
579
579
]
580
580
@@ -590,7 +590,7 @@ SyntaxInformation[DenoiseCSIdata]={"ArgumentsPattern"->{_, OptionsPattern[]}}
590
590
DenoiseCSIdata [spectra_ , OptionsPattern []] := Block [{sig , out , hist , len , spectraDen , nn ,sel },
591
591
(* assusmes data is (x,y,z,spectra)*)
592
592
len = Dimensions [spectra ][[- 1 ]];
593
-
593
+
594
594
(*get the corner voxels to calcluate the noise standard deviation or automatic estimation*)
595
595
596
596
sig = Switch [OptionValue [PCANoiseSigma ],
@@ -604,11 +604,11 @@ DenoiseCSIdata[spectra_, OptionsPattern[]] := Block[{sig, out, hist, len, spectr
604
604
,
605
605
"Automatic" , 0
606
606
];
607
-
607
+
608
608
(*Denoise the spectra data*)
609
609
{spectraDen , sig } = PCADeNoise [Transpose [Join [Re @ # , Im @ # ]]& [RotateDimensionsRight [spectra ]], 1 , sig ,
610
610
PCAClipping -> False , PCAKernel -> OptionValue [PCAKernel ], MonitorCalc -> False , Method -> "Patch" ];
611
-
611
+
612
612
ToPackedArray @ N @ RotateDimensionsLeft [Transpose [spectraDen ][[1 ;; len ]] + Transpose [spectraDen ][[len + 1 ;; ]] I ]
613
613
]
614
614
@@ -623,13 +623,13 @@ DenoiseDynamicSpectraData[spectra_] := Block[{len, data, sig, comp},
623
623
(*merge Re and Im data*)
624
624
len = Dimensions [spectra ][[- 1 ]];
625
625
data = Join [Re @ # , Im @ # ] & [Transpose [spectra ]];
626
-
626
+
627
627
(*perform denoising*)
628
628
{sig , comp , data } = PCADeNoiseFit [data , MinMax [Dimensions [data ]], 0. , 0 ];
629
-
629
+
630
630
(*reconstruct complex spectra*)
631
631
data = Transpose [data [[;; len ]] + I data [[len + 1 ;; ]]];
632
-
632
+
633
633
(*output data and sigma*)
634
634
{ToPackedArray @ N @ data , sig }
635
635
]
@@ -658,20 +658,20 @@ AnisoFilterTensor[tensi_,dat_,OptionsPattern[]]:=Block[{
658
658
time = OptionValue [AnisoStepTime ];
659
659
kappa = N @ OptionValue [AnisoKappa ];
660
660
type = Clip [Round @ OptionValue [AnisoWeightType ],{1 ,2 }];
661
-
661
+
662
662
(*calculate the edges based on the diffusion images*)
663
663
weights = If [dat === 1 ,
664
664
1 ,
665
665
PrintTemporary ["Determaning the weights based on the data." ];
666
666
WeightMapCalc [ToPackedArray @ N @ dat , AnisoKappa -> kappa , AnisoWeightType -> type ]
667
667
];
668
-
668
+
669
669
(*get the fixed parameters*)
670
670
tens = ToPackedArray @ N @ tensi ;
671
671
mn = Mean [tens [[1 ;; 3 ]]];
672
672
{kers , wts } = KernelWeights [];
673
673
lambda = 1 / Length [kers ];
674
-
674
+
675
675
(*filter the tensor*)
676
676
PrintTemporary ["Anisotropic filtering of the tensor." ];
677
677
j = 0 ; PrintTemporary [ProgressIndicator [Dynamic [j ],{0 ,itt 6 }]];
@@ -707,15 +707,15 @@ WeightMapCalc[data_,OptionsPattern[]]:=Block[{
707
707
(*get the options*)
708
708
kappa = N @ OptionValue [AnisoKappa ];
709
709
type = Clip [Round @ OptionValue [AnisoWeightType ],{1 ,2 }];
710
-
710
+
711
711
(*get the kernerl and weights*)
712
712
{kers ,wts }= KernelWeights [];
713
-
713
+
714
714
(*prepare output *)
715
715
dim = Dimensions [data ];
716
716
len = dim [[2 ]]; dim = Drop [dim ,{2 }];
717
717
weights = ConstantArray [0 ,Prepend [dim ,Length [wts ]]];
718
-
718
+
719
719
(*get the weighting for all diffusion images*)
720
720
i = 0 ; PrintTemporary [ProgressIndicator [Dynamic [i ],{0 ,len }]];
721
721
(
@@ -725,7 +725,7 @@ WeightMapCalc[data_,OptionsPattern[]]:=Block[{
725
725
(*add to the weights*)
726
726
weights + = WeightCalc [FinDiffCalc [dat ,kers ],wts ,kappa ,type ];
727
727
)& /@ Transpose [ToPackedArray @ N @ data ];
728
-
728
+
729
729
(*normalize the weights between 0 and 1*)
730
730
(*weights=Mean[weights];
731
731
weights=weights/Max[weights];*)
@@ -783,7 +783,7 @@ AnisoFilterData[data_, vox_, opts:OptionsPattern[]] := Block[{
783
783
sig , rho , step , itt , sc , max , tr , cr , ind
784
784
},
785
785
(*for implementation see 10.1002/mrm.20339*)
786
-
786
+
787
787
(*crop background for speed*)
788
788
{dati , cr } = AutoCropData [data ];
789
789
@@ -793,12 +793,12 @@ AnisoFilterData[data_, vox_, opts:OptionsPattern[]] := Block[{
793
793
tr = max / 10000 ;
794
794
ind = IdentityMatrix [3 ];
795
795
sc = Max [vox ]/ vox ;
796
-
796
+
797
797
(*aniso filter kernel size *)
798
798
{sig , rho } = OptionValue [AnisoKernel ];
799
799
step = OptionValue [AnisoStepTime ];
800
800
itt = OptionValue [AnisoIterations ];
801
-
801
+
802
802
Do [(*loop over itterrations*)
803
803
(*get the data gradients*)
804
804
grads = ToPackedArray @ N [(
@@ -820,15 +820,15 @@ AnisoFilterData[data_, vox_, opts:OptionsPattern[]] := Block[{
820
820
eval = eval = 1 / (eval + 10. ^ - 16 );
821
821
Transpose [evec ] . DiagonalMatrix [3 eval / Total [eval ]] . evec
822
822
] & , jacTot , {3 }];
823
-
823
+
824
824
(*get the time step and calculate divergence of vector field*)
825
825
div = Total [MapThread [GaussianFilter [#1 , 1 , #2 ] & , {# , ind }] & /@
826
826
RotateDimensionsRight [DivDot [tMat , RotateDimensionsLeft [grads , 2 ]], 2 ], {2 }];
827
827
828
828
(*perform the smoothing step*)
829
829
dati = ToPackedArray @ N @ Clip [dati + step div , {0 , 2 } max ];
830
830
, {itt }];
831
-
831
+
832
832
(*output the data*)
833
833
ReverseCrop [Transpose @ dati , Dimensions @ data , cr ]
834
834
]
0 commit comments