Skip to content

Commit d4a27d1

Browse files
committed
fixes
1 parent bb37e74 commit d4a27d1

18 files changed

+56465
-1565
lines changed

QMRITools/Kernel/DenoiseTools.wl

+57-57
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ DeNoise[dat_, sigi_, filt_, OptionsPattern[]] := Module[{kern, out, type, dimsig
195195
Return[Message[DeNoise::dim, dimsig, dimdat]]
196196
];
197197
];
198-
198+
199199
(*Check dimensions,filter must be of lower order than data*)
200200
If[Length[filt] > ArrayDepth[data], Return[Message[DeNoise::filt, Length[filt], ArrayDepth[data]]]];
201-
201+
202202
(*Create filter*)
203203
kern = Switch[type = OptionValue[DeNoiseKernel],
204204
"Box", kern = BoxMatrix[filt]/Total[Flatten[BoxMatrix[filt]]],
@@ -207,7 +207,7 @@ DeNoise[dat_, sigi_, filt_, OptionsPattern[]] := Module[{kern, out, type, dimsig
207207
_, Return[Message[DeNoise::kern, type]];
208208
];
209209
If[OptionValue[DeNoiseMonitor], PrintTemporary["Using " <> type <> " kernel."]];
210-
210+
211211
out = ToPackedArray@N@Nest[DeNoisei[#, sig, filt, kern] &, data, OptionValue[DeNoiseIterations]];
212212
If[
213213
ArrayQ[out], Return[Clip[out, {0.9 Min[data], 1.1 Max[data]}]],
@@ -320,10 +320,10 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
320320
totalItt, output, j, sigi, zm, ym, xm, zp, yp, xp, fitdata, sigo, Nes, datn,
321321
weight, posV, leng, nearPos, p, pi, pos, np
322322
},
323-
323+
324324
(*tollerane if>0 more noise components are kept*)
325325
{mon, wht, tol, ker, clip, comp} = OptionValue[{MonitorCalc, PCAWeighting, PCATolerance, PCAKernel, PCAClipping, PCAComplex}];
326-
326+
327327
(*concatinate complex data and make everything numerical to speed up*)
328328
comps = False;
329329
data = ToPackedArray@N@If[comp,
@@ -332,29 +332,29 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
332332
Join[data[[1]], data[[2]], 2],
333333
datai
334334
];
335-
335+
336336
{min, max} = 1.1 MinMax[Abs[data]];
337337
maskd = Unitize@Total@Transpose[data];
338338
mask = ToPackedArray[N@(maski maskd)];
339339
sigm = ToPackedArray[N@sigmai];
340-
340+
341341
(*get data dimensions*)
342342
dim = {zdim, ddim, ydim, xdim} = Dimensions[data];
343-
343+
344344
(*define sigma*)
345345
sigm = If[NumberQ[sigm], ConstantArray[sigm, {zdim, ydim, xdim}], sigm];
346-
346+
347347
Switch[OptionValue[Method],
348348
(*--------------use similar signals--------------*)
349349
"Similarity",
350350
(*vectorize data*)
351351
{data, posV} = DataToVector[data, mask];
352352
sigm = First@DataToVector[sigm, mask];
353-
353+
354354
(*define runtime parameters*)
355355
m = Length[data[[1]]];
356356
n = Round[ker^3];
357-
357+
358358
(*parameters for monitor*)
359359
leng = Length[data];
360360
pos = Range@leng;
@@ -390,54 +390,54 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
390390
If[mon, PrintTemporary[ProgressIndicator[Dynamic[j], {0, leng}]]];
391391
{m, n} = MinMax[{m, n}];
392392
Map[(j++;
393-
393+
394394
p = #;
395395
pi = First@p;
396-
396+
397397
(*perform the fit and reconstruct the noise free data*)
398398
{sigo, Nes, datn} = PCADeNoiseFit[data[[p]], {m, n}, sigm[[pi]], tol];
399-
399+
400400
(*get the weightes*)
401401
weight = If[wht, 1./(m - Nes), 1.];
402-
402+
403403
(*sum data and sigma and weight for numer of components*)
404404
datao[[p]] += weight datn;
405405
sigmat[[p]] += weight sigo;
406406
weights[[p]] += weight;
407-
407+
408408
(*output sig, Nest and iterations*)
409409
sigmati[[pi]] = sigo;
410410
nmati[[pi]] = Nes;
411411
) &, nearPos];
412-
412+
413413
(*make everything in arrays*)
414414
datao = VectorToData[DivideNoZero[datao, weights], posV];
415415
sigmat = VectorToData[DivideNoZero[sigmat, weights], posV];
416416
output = {VectorToData[sigmati, posV], VectorToData[nmati, posV]};
417-
417+
418418
,
419419
(*--------------use patch---------------*)
420-
420+
421421
_,
422422
ker = If[EvenQ[ker], ker - 1, ker];
423-
423+
424424
(*prepare data*)
425425
data = RotateDimensionsLeft[Transpose[data]];
426-
426+
427427
(*define runtime parameters*)
428428
{m, n} = MinMax[{ddim, ker^3}];
429429
off = Round[(ker - 1)/2];
430-
430+
431431
(*ouput data*)
432432
datao = 0. data;
433433
weights = sigmat = datao[[All, All, All, 1]];
434-
434+
435435
(*parameters for monitor*)
436436
j = 0;
437437
start = off + 1;
438438
{zdim, ydim, xdim} = {zdim, ydim, xdim} - off;
439439
totalItt = Total[Flatten[mask[[start ;; zdim, start ;; ydim, start ;; xdim]]]];
440-
440+
441441
(*perform denoising*)
442442
If[mon, PrintTemporary[ProgressIndicator[Dynamic[j], {0, totalItt}]]];
443443
output = Table[
@@ -450,32 +450,32 @@ PCADeNoise[datai_, maski_, sigmai_, OptionsPattern[]] := Block[{
450450
sigi = sigm[[z, y, x]];
451451
{{zm, ym, xm}, {zp, yp, xp}} = {{z, y, x} - off, {z, y, x} + off};
452452
fitdata = Flatten[data[[zm ;; zp, ym ;; yp, xm ;; xp]], 2];
453-
453+
454454
(*perform the fit and reconstruct the noise free data*)
455455
{sigo, Nes, datn} = PCADeNoiseFit[fitdata, {m, n}, sigi, tol];
456-
456+
457457
(*reshape the vector into kernel box and get the weightes*)
458458
datn = Fold[Partition, datn, {ker, ker}];
459459
weight = If[wht, 1./(m - Nes), 1.];
460-
460+
461461
(*sum data and sigma and weight for numer of components*)
462462
datao[[zm ;; zp, ym ;; yp, xm ;; xp, All]] += (weight datn);
463463
sigmat[[zm ;; zp, ym ;; yp, xm ;; xp]] += weight sigo;
464464
weights[[zm ;; zp, ym ;; yp, xm ;; xp]] += weight;
465-
465+
466466
(*output sig, Nest and iterations*)
467467
{sigo, Nes}
468468
],
469469
{z, start, zdim}, {y, start, ydim}, {x, start, xdim}];
470470

471471
(*make everything in arrays*)
472472
output = ArrayPad[#, off] & /@ RotateDimensionsRight[output];
473-
473+
474474
(*correct output data for weightings*)
475475
datao = Transpose@RotateDimensionsRight[Re@DivideNoZero[datao, weights]];
476476
sigmat = DivideNoZero[sigmat, weights];
477477
];
478-
478+
479479
(*define output, split it if data is complex*)
480480
datao = Which[
481481
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[{
501501
PCADeNoiseFit[data_, {m_, n_}, sigi_?NumberQ, toli_] := Block[{
502502
trans, xmat, xmatT, val, mat, pi, sig, xmatN, tol, out
503503
},
504-
504+
505505
(*perform decomp*)
506506
trans = Subtract @@ Dimensions[data] > 0;
507507
{xmat, xmatT} = If[trans, {Transpose@data, data}, {data, Transpose@data}];
508508
{val, mat} = Reverse /@ Eigensystem[xmat . xmatT];
509509

510510
(*if sigma is given perform with fixed sigma,else fit both*)
511511
{pi, sig} = GridSearch[Re[val], m, n, sigi];
512-
512+
513513
(*constartin pi plus tol*)
514514
tol = Round[Clip[pi - toli, {1, m}]];
515-
515+
516516
(*give output,simga,number of noise comp,and denoised matrix*)
517517
out = Transpose[mat[[tol ;;]]] . (mat[[tol ;;]] . xmat);
518518
{sig, tol, If[trans, Transpose@out, out]}
@@ -521,21 +521,21 @@ PCADeNoiseFit[data_, {m_, n_}, sigi_?NumberQ, toli_] := Block[{
521521

522522
GridSearch = Compile[{{val, _Real, 1}, {m, _Integer, 0}, {n, _Integer, 0}, {sig, _Real, 0}}, Block[
523523
{valn, gam, sigq1, sigq2,p, pi},
524-
524+
525525
(*calculate all possible values for eq1 and eq2*)
526526
valn = val[[;; -2]]/n;
527527
p = Range[m - 1];
528528
gam = 4 Sqrt[N[p/(n - (m - (p + 1)))]];
529529
sigq1 = Accumulate[valn]/p;
530530
sigq2 = (valn - First[valn])/gam;
531-
531+
532532
(*find at which value eq1>eq2*)
533533
pi = If[sig === 0.,
534534
Total[1 - UnitStep[sigq2 - sigq1]],
535535
Total[1 - UnitStep[Mean[{sigq1, sigq2}] - sig^2]]
536536
];
537537
pi = If[pi <= 0, 1, pi];
538-
538+
539539
(*give output*)
540540
{pi, Sqrt[Ramp[(sigq1[[pi]] + sigq2[[pi]])/2]]}
541541
], RuntimeOptions -> "Speed", Parallelization -> True];
@@ -558,11 +558,11 @@ NNDeNoise[data_, mask_, opts : OptionsPattern[]] := Block[{
558558
back = Round[mask Mask[NormalizeMeanData[data], OptionValue[NNThreshold]]];
559559
{dat, coor} = DataToVector[data, back];
560560
dat = ToPackedArray@N@dat;
561-
561+
562562
(*Get dimensions, training sample and define network*)
563563
n = Range@Length@First@dat;
564564
ran = 1.1 MinMax[dat];
565-
565+
566566
(*trian network per volume and generate denoised data*)
567567
(*DistributeDefinitions[dat, n];*)
568568
dat = Monitor[Table[
@@ -574,7 +574,7 @@ NNDeNoise[data_, mask_, opts : OptionsPattern[]] := Block[{
574574
];
575575
trained[dati]
576576
, {i, n}], ProgressIndicator[Dynamic[i], {0, Max[n]}]];
577-
577+
578578
ToPackedArray@N@Clip[Transpose[VectorToData[#, coor] & /@ dat], ran]
579579
]
580580

@@ -590,7 +590,7 @@ SyntaxInformation[DenoiseCSIdata]={"ArgumentsPattern"->{_, OptionsPattern[]}}
590590
DenoiseCSIdata[spectra_, OptionsPattern[]] := Block[{sig, out, hist, len, spectraDen, nn ,sel},
591591
(* assusmes data is (x,y,z,spectra)*)
592592
len = Dimensions[spectra][[-1]];
593-
593+
594594
(*get the corner voxels to calcluate the noise standard deviation or automatic estimation*)
595595

596596
sig = Switch[OptionValue[PCANoiseSigma],
@@ -604,11 +604,11 @@ DenoiseCSIdata[spectra_, OptionsPattern[]] := Block[{sig, out, hist, len, spectr
604604
,
605605
"Automatic", 0
606606
];
607-
607+
608608
(*Denoise the spectra data*)
609609
{spectraDen, sig} = PCADeNoise[Transpose[Join[Re@#, Im@#]]&[RotateDimensionsRight[spectra]], 1, sig,
610610
PCAClipping -> False, PCAKernel -> OptionValue[PCAKernel], MonitorCalc->False, Method -> "Patch"];
611-
611+
612612
ToPackedArray@N@RotateDimensionsLeft[Transpose[spectraDen][[1 ;; len]] + Transpose[spectraDen][[len + 1 ;;]] I]
613613
]
614614

@@ -623,13 +623,13 @@ DenoiseDynamicSpectraData[spectra_] := Block[{len, data, sig, comp},
623623
(*merge Re and Im data*)
624624
len = Dimensions[spectra][[-1]];
625625
data = Join[Re@#, Im@#] &[Transpose[spectra]];
626-
626+
627627
(*perform denoising*)
628628
{sig, comp, data} = PCADeNoiseFit[data, MinMax[Dimensions[data]], 0., 0];
629-
629+
630630
(*reconstruct complex spectra*)
631631
data = Transpose[data[[;;len]] + I data[[len+1;;]]];
632-
632+
633633
(*output data and sigma*)
634634
{ToPackedArray@N@data, sig}
635635
]
@@ -658,20 +658,20 @@ AnisoFilterTensor[tensi_,dat_,OptionsPattern[]]:=Block[{
658658
time=OptionValue[AnisoStepTime];
659659
kappa=N@OptionValue[AnisoKappa];
660660
type=Clip[Round@OptionValue[AnisoWeightType],{1,2}];
661-
661+
662662
(*calculate the edges based on the diffusion images*)
663663
weights = If[dat===1,
664664
1,
665665
PrintTemporary["Determaning the weights based on the data."];
666666
WeightMapCalc[ToPackedArray@N@dat, AnisoKappa->kappa, AnisoWeightType->type]
667667
];
668-
668+
669669
(*get the fixed parameters*)
670670
tens = ToPackedArray@N@tensi;
671671
mn = Mean[tens[[1;;3]]];
672672
{kers, wts} = KernelWeights[];
673673
lambda = 1/Length[kers];
674-
674+
675675
(*filter the tensor*)
676676
PrintTemporary["Anisotropic filtering of the tensor."];
677677
j=0;PrintTemporary[ProgressIndicator[Dynamic[j],{0,itt 6}]];
@@ -707,15 +707,15 @@ WeightMapCalc[data_,OptionsPattern[]]:=Block[{
707707
(*get the options*)
708708
kappa=N@OptionValue[AnisoKappa];
709709
type=Clip[Round@OptionValue[AnisoWeightType],{1,2}];
710-
710+
711711
(*get the kernerl and weights*)
712712
{kers,wts}=KernelWeights[];
713-
713+
714714
(*prepare output *)
715715
dim=Dimensions[data];
716716
len=dim[[2]];dim=Drop[dim,{2}];
717717
weights=ConstantArray[0,Prepend[dim,Length[wts]]];
718-
718+
719719
(*get the weighting for all diffusion images*)
720720
i=0;PrintTemporary[ProgressIndicator[Dynamic[i],{0,len}]];
721721
(
@@ -725,7 +725,7 @@ WeightMapCalc[data_,OptionsPattern[]]:=Block[{
725725
(*add to the weights*)
726726
weights+=WeightCalc[FinDiffCalc[dat,kers],wts,kappa,type];
727727
)&/@Transpose[ToPackedArray@N@data];
728-
728+
729729
(*normalize the weights between 0 and 1*)
730730
(*weights=Mean[weights];
731731
weights=weights/Max[weights];*)
@@ -783,7 +783,7 @@ AnisoFilterData[data_, vox_, opts:OptionsPattern[]] := Block[{
783783
sig, rho , step, itt, sc, max, tr, cr, ind
784784
},
785785
(*for implementation see 10.1002/mrm.20339*)
786-
786+
787787
(*crop background for speed*)
788788
{dati, cr} = AutoCropData[data];
789789

@@ -793,12 +793,12 @@ AnisoFilterData[data_, vox_, opts:OptionsPattern[]] := Block[{
793793
tr = max/10000;
794794
ind = IdentityMatrix[3];
795795
sc = Max[vox]/vox;
796-
796+
797797
(*aniso filter kernel size *)
798798
{sig, rho} = OptionValue[AnisoKernel];
799799
step = OptionValue[AnisoStepTime];
800800
itt = OptionValue[AnisoIterations];
801-
801+
802802
Do[(*loop over itterrations*)
803803
(*get the data gradients*)
804804
grads = ToPackedArray@N[(
@@ -820,15 +820,15 @@ AnisoFilterData[data_, vox_, opts:OptionsPattern[]] := Block[{
820820
eval = eval = 1/(eval + 10.^-16);
821821
Transpose[evec] . DiagonalMatrix[3 eval/Total[eval]] . evec
822822
] &, jacTot, {3}];
823-
823+
824824
(*get the time step and calculate divergence of vector field*)
825825
div = Total[MapThread[GaussianFilter[#1, 1, #2] &, {#, ind}] & /@
826826
RotateDimensionsRight[DivDot[tMat, RotateDimensionsLeft[grads, 2]], 2], {2}];
827827

828828
(*perform the smoothing step*)
829829
dati = ToPackedArray@N@Clip[dati + step div, {0, 2} max];
830830
, {itt}];
831-
831+
832832
(*output the data*)
833833
ReverseCrop[Transpose@dati, Dimensions@data, cr]
834834
]

0 commit comments

Comments
 (0)