-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiffSim_ToolBox.m
More file actions
1286 lines (978 loc) · 59.8 KB
/
DiffSim_ToolBox.m
File metadata and controls
1286 lines (978 loc) · 59.8 KB
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
classdef DiffSim_ToolBox
methods(Static)
%% General function to calculate the Tensor out of signal attenuation
function [T6, EigValue,EigVector,MD,FA,Trace_DTI]= Tensor(dwi,dir,bval)
Tensor=[];
EigValue=[];
EigVector=[];
MD=[];
FA=[];
Trace_DTI=[];
tmpInput=[];
tmpInput(1,1,1)=1;
tmpInput(1,1,(2:size(dir,1)+1))=dwi;
[T6,EigVector,EigValue, Mat] = DiffSim_ToolBox.Tensor_local(tmpInput,dir,bval);
[MD, FA, Trace] = DiffSim_ToolBox.Maps_local(EigValue);
end
function [T6, EigValue,EigVector,MD,FA,Trace_DTI]= Tensor_Water(Water,dT,DiffTime)
Tensor=[];
EigValue=[];
EigVector=[];
MD=[];
FA=[];
Trace_DTI=[];
tmpInput=[];
if (nargin<3)
DiffTime=size(Water,3)*dT;
end
% b-value and dir are arbitrary to give us enought precision.
bval=1000;
dir=[0.831862183551238,-0.978020640898505,-0.746594402068549,0.618503898022940,0.0357105488849347,0.0690933229952567,0.285311347832409,0.541890310423585,0.696013916160111,-0.586381611851074,-0.361852167119390,-0.107233806522707;
-0.474849677041022,-0.117331253822020,-0.566355417065775,-0.0500144660490100,-0.296253369933249,-0.451075167295507,-0.874813688131557,-0.803757865561315,-0.264300268733346,-0.487477487497046,-0.922081582695813,-0.849061587106308;
-0.287268170935018,-0.172362813165683,0.349053404091507,-0.784188429834895,0.954441552711641,-0.889807334332884,-0.391533602085168,0.245617655503534,0.667615050863187,-0.646933288596948,-0.137217684961672,0.517296200740132]';
% create synthetic dwi values.
for cpt_dwi=1:1:size(dir,1)
[tmp_adc] = DiffSim_ToolBox.ADC_Water_dir(Water,squeeze(dir(cpt_dwi,:)),dT,DiffTime);
dwi(cpt_dwi)=exp(-bval*tmp_adc);
end
tmpInput(1,1,1)=1;
tmpInput(1,1,(2:size(dir,1)+1))=dwi;
[T6,EigVector,EigValue, Mat] = DiffSim_ToolBox.Tensor_local(tmpInput,dir,bval);
[MD, FA, Trace] = DiffSim_ToolBox.Maps_local(EigValue);
end
%% Local function to calculte the fit the tensor
function [Tensor,EigVector,EigValue, Mat2] = Tensor_local(slice,dir,bvalue)
% slice : x y [B0 B1-dir1 B2-dir2...]
% dir : x1 y1 z1
% x2 y2 z2
% . . .
% . . .
%
% Tensor : x y [6]
% EigVector : x y [3 coor][3 num]
% EigValue : x y [3]
nb_dir=size(dir,1);
H = zeros(nb_dir,6);
for i=1:nb_dir
H(i,:)=[dir(i,1)*dir(i,1),dir(i,2)*dir(i,2),dir(i,3)*dir(i,3),2*dir(i,1)*dir(i,2),2*dir(i,1)*dir(i,3),2*dir(i,2)*dir(i,3)];
end
[U,S,V] = svd(H,0);
H_inv=V*inv(S)*U'; %H is a 6*30 matrix
Tensor=zeros(size(slice,1),size(slice,2),6);
EigVector=zeros(size(slice,1),size(slice,2),3,3);
EigValue= zeros(size(slice,1),size(slice,2),3);
Mat=zeros(size(slice,1),size(slice,2),1,9);
Mat2=zeros(size(slice,1),size(slice,2),3,3);
for y=1:1:size(slice,1)
for x=1:1:size(slice,2)
Y=[];
if slice(y,x,1)~=0
for z=1:1:nb_dir
if double(slice(y,x,z+1))~=0
Y=[Y;log(double(slice(y,x,1))/double(slice(y,x,z+1)))/bvalue]; % Y = [log(S0/S1), log(S0/S2), log(S0,S3)....]
else
Y=[Y;0]; % Y = [log(S0/S1), log(S0/S2), log(S0,S3)....]
end
end
Tensor(y,x,:)=H_inv*Y;
Mat=[Tensor(y,x,1),Tensor(y,x,4),Tensor(y,x,5);Tensor(y,x,4),Tensor(y,x,2),Tensor(y,x,6);Tensor(y,x,5),Tensor(y,x,6),Tensor(y,x,3)];
if mean(mean(isnan(Mat)))>0 || mean(mean(isinf(Mat)))>0
Mat=zeros(3,3);
end
Mat2(y,x,:,:)=Mat;
[Vect,Diag]=eig(Mat);
EigValue(y,x,:)=[abs(Diag(1,1)),abs(Diag(2,2)),abs(Diag(3,3))];
[t,index]=sort(EigValue(y,x,:),'descend');
EigValue(y,x,:)=EigValue(y,x,index);
EigVector(y,x,:,:)=Vect(:,index);
else
Mat2(y,x,:,:)=0;
Tensor(y,x,:)=0;
EigValue(y,x,:)=0;
EigVector(y,x,:,:)=0;
end
end
end
end
%% Local function to calculte the invarient of the tensor
function [MD, FA, TRACE, I1, I2, I3] = Maps_local(EigValue)
MD=zeros(size(EigValue,1),size(EigValue,2));
FA=zeros(size(EigValue,1),size(EigValue,2));
TRACE=zeros(size(EigValue,1),size(EigValue,2));
I1=zeros(size(EigValue,1),size(EigValue,2));
I2=zeros(size(EigValue,1),size(EigValue,2));
I3=zeros(size(EigValue,1),size(EigValue,2));
for y=1:1:size(EigValue,1)
for x=1:1:size(EigValue,2)
TRACE(y,x)=EigValue(y,x,1)+EigValue(y,x,2)+EigValue(y,x,3);
MD(y,x)=(EigValue(y,x,1)+EigValue(y,x,2)+EigValue(y,x,3))/3;
FA(y,x)=sqrt((3*((EigValue(y,x,1)-MD(y,x))^2+(EigValue(y,x,2)-MD(y,x))^2+(EigValue(y,x,3)-MD(y,x))^2))/(2*(EigValue(y,x,1)^2+EigValue(y,x,2)^2+EigValue(y,x,3)^2)));
I1(y,x)=EigValue(y,x,1)+EigValue(y,x,2)+EigValue(y,x,3);
I2(y,x)=EigValue(y,x,1)*EigValue(y,x,2)+EigValue(y,x,2)*EigValue(y,x,3)+EigValue(y,x,1)*EigValue(y,x,3);
I3(y,x)=EigValue(y,x,1)*EigValue(y,x,2)*EigValue(y,x,3);
end
end
end
%% Function to calculate the mean ADC from several signal attenuations
function [ADC] = ADC_mean(dwi,bval)
ADC=log(mean(dwi))/-bval;
end
%% Function to calculate the ADC per direction from signal attenuations
function [ADC] = ADC_dir(dwi,bval)
for cpt_dir=1:1:length(dwi)
ADC(cpt_dir)=log(dwi(cpt_dir))/-bval;
end
end
%% Calculate the coefficient of diffusion from a dispacement distribution
function [ADC] = ADC_Water_mean(Water,dT,DiffTime)
if (nargin<3)
DiffTime=size(Water,3)*dT;
end
% sum of the mean square of distance
dx=abs(Water(:,:,end)-Water(:,:,1));
% sum of x y z
ssdx=squeeze(sum(dx.*dx,2));
% Coefficient of diffusion mean square distance over dT in 3d, unit in mm2/s
ADC=1e6*mean(ssdx)/(6*DiffTime);
end
%% Calculate the coefficient of diffusion from a dispacement distribution per direction
function [ADC] = ADC_Water_dir(Water,dir,dT,DiffTime)
if (nargin<4)
DiffTime=size(Water,3)*dT;
end
% sum of the mean square of distance
dx=(Water(:,:,end)-Water(:,:,1));
for cpt_dir=1:1:size(dir,1)
% Make the projection on the corresponding axis
dx_dir= Vector_ToolBox.Projection_vect_n(dx, dir(cpt_dir,:));
% dx_dir=dx.*(repmat((dir(cpt_dir,:)),size(Water,1),1));
% dx_dir3=dx.*(repmat(abs(dir(cpt_dir,:)),size(Water,1),1));
% Take the norm of the vectors
%dx_norm=sqrt(dx_dir(:,1).*dx_dir(:,1)+dx_dir(:,2).*dx_dir(:,2)+dx_dir(:,3).*dx_dir(:,3));
dx_norm=Vector_ToolBox.Norm_vect_n(dx_dir);
% sum of x y z
ssdx2=squeeze(mean(dx_norm.*dx_norm));
% Coeffiecient of diffusion mean square distance over dT in 3d, unit in mm2/s
% NB: the true equation to calculate the diffusion from a
% displacement distribution is D= <X^2>/ ( 2^Dim * t )
% By calculating my diffusion coeffcient in 3 Dimension,
% my ADC is too small by a factor 3
% I believe that because in MR diffusion we only calculate
% the ADC based on a projection of diffusion gradient
% the correct dimension is actually only 1
% To be verified...
ADC(cpt_dir)=1e6*ssdx2/(2*DiffTime);
end
end
%% Calculate the coefficient of diffusion from a dispacement distribution in X Y Z
function [ADC] = ADC_Water_xyz(Water,dT)
dir=[[1 0 0];[0 1 0];[0 0 1];];
[ADC] = DiffSim_ToolBox.ADC_Water_dir(Water,dir,dT) ;
end
%% Calculate the theorical FA considering XYZ direction as principal Eigen vector directions
function [FA] = FA_Water(Water,dT)
[ADC_xyz] = DiffSim_ToolBox.ADC_Water_xyz(Water,dT);
FA= sqrt(1/2)* sqrt( (ADC_xyz(1)-ADC_xyz(2)).^2 +(ADC_xyz(2)-ADC_xyz(3)).^2 +(ADC_xyz(3)-ADC_xyz(1)).^2)./sqrt( ADC_xyz(1).^2 +ADC_xyz(2).^2 +ADC_xyz(3).^2 ) ;
end
%% Calculate the theorical EigenValues considering XYZ direction as principal Eigen vector directions
function [Eigen] = EigenValue_Water(Water,dT)
[ADC_xyz] = DiffSim_ToolBox.ADC_Water_xyz(Water,dT);
Eigen=sort(ADC_xyz,'descend');
end
%% Simulate a diffusion step
function [Dstep] = diffusion_step(N,D,Dim,dT)
KD = sqrt(D * 2 * Dim * dT);
Dvar=KD * randn(N,1);
VectorDir=randn(N,3);
VectorDir=VectorDir./repmat(Vector_ToolBox.Norm_vect_n(VectorDir),1,3);
Dstep=VectorDir.*Dvar;
end
%% Simulate MR from a vector gradient file and water
function [Att_approx, Phi_Dist]= Simulate_MR(Water,Seq,Grad)
%%% Simulation gradients
Phi_Dist=[];
Nb_Water=size(Water,1);
%%% Loop over Direction %%%
for cpt_dir=1:1:size(Seq.Dir,1)
%%% Loop over Waveform and or B-values %%%
for cpt_g=1:1:size(Grad,2)
%%% Intregral Diffusion + gradients %%%
phi=zeros(Nb_Water,1);
% tmp=[];
for cpt=2:1:size(Grad,1)
phi(:)=phi(:)+ nansum( Seq.Gamma*Seq.dT*( repmat(Grad(cpt,cpt_g),Nb_Water,3).*repmat(Seq.Dir(cpt_dir,:),Nb_Water,1).*squeeze(Water(:,:,cpt))),2);
%tmp(cpt,:)=phi(:);
end
%%% Phase Distribution
Phi_Dist(cpt_g,cpt_dir,:)=phi(:);
%%% MR Signal attenuation
Att_approx(cpt_g,cpt_dir)=nanmean(cos(phi(:))+i*sin(phi(:)));
%%%
end
end
end
%% Simulate MR from a vector gradient file and water
function [Att_approx, Phi_Dist]= Simulate_MR_optim(Water,Seq,Grad,World)
%%% Simulation gradients
Phi_Dist=[];
Nb_Water=size(Water,1);
L_Grad=size(Grad,1);
%tmp_w=permute(squeeze(Water(:,:,1:L_Grad)),[2 3 1]);
%%% Loop over Direction %%%
for cpt_g=1:1:size(Grad,2)
%%% Intregral Diffusion + gradients %%%
phi=zeros(Nb_Water,1);
% for cpt=2:1:size(Grad,1)
% phi(:)=phi(:)+ nansum( World.Gamma *World.dT*( repmat(Grad(cpt,cpt_g),Nb_Water,3).*repmat(Seq.Dir(cpt_dir,:),Nb_Water,1).*squeeze(Water(:,:,cpt))),2);
% end
% tic
% for cpt=1:1:Nb_Water
% tmp= cumsum( nansum(World.Gamma *World.dT*( repmat(Grad(:,cpt_g),1,3).*Seq.Dir(cpt_dir,:).*squeeze(Water(cpt,:,1:L_Grad))')));
% phi(cpt)=tmp(end);
% end
% toc
tmp_g=squeeze(Grad(:,cpt_g));
for cpt=1:1:Nb_Water
tmp_x= World.Gamma *World.dT*(tmp_g.*squeeze(Water(cpt,1,1:L_Grad)));
tmp_y= World.Gamma *World.dT*(tmp_g.*squeeze(Water(cpt,2,1:L_Grad)));
tmp_z= World.Gamma *World.dT*(tmp_g.*squeeze(Water(cpt,3,1:L_Grad)));
for cpt_dir=1:1:size(Seq.Dir,1)
tmp_x2=cumsum(tmp_x.*Seq.Dir(cpt_dir,1));
tmp_y2=cumsum(tmp_y.*Seq.Dir(cpt_dir,2));
tmp_z2=cumsum(tmp_z.*Seq.Dir(cpt_dir,3));
Phi_Dist(cpt_g,cpt_dir,cpt)=tmp_x2(end)+tmp_y2(end)+tmp_z2(end);
end
end
for cpt_dir=1:1:size(Seq.Dir,1)
Att_approx(cpt_g,cpt_dir)=nanmean(cos( Phi_Dist(cpt_g,cpt_dir,:))+i*sin( Phi_Dist(cpt_g,cpt_dir,:)));
end
%%% Phase Distribution
%%% MR Signal attenuation
%%%
end
end
%% Old simulation
function [txq,In_out_List] = SimDiff(Cells,Voxel_dim,Diff)
% disp(['Simulate diffusion distribution ']);
% tic
%% Generate Diffusion Distribution
In_out_List=[];
for cpt_dim=1:1:Diff.dim
xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel
end
h = waitbar(0,['Diffusion distribution ']);
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection(Cells, squeeze(xt(:,:,1)));
for cpt_t=2:1:(Diff.dur+1)
% Diffusion-Collision solver - final positions
% Reroll Diffusion until we don't have collision issues anymore
List_coll=[1:1:Diff.N];
while List_coll
% Extracellular
List_Extra = find(In_out_List(:,cpt_t-1));
List_Extra=List_Extra(ismember(List_Extra, List_coll));
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT);
xt(List_Extra,:,cpt_t)=xt(List_Extra,:,cpt_t-1)+tmp_water;
% Intracellular
List_Intra = find(~In_out_List(:,cpt_t-1));
List_Intra=List_Intra(ismember(List_Intra, List_coll));
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT);
xt(List_Intra,:,cpt_t)=xt(List_Intra,:,cpt_t-1)+tmp_water;
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection(Cells, squeeze(xt(:,:,cpt_t)));
In_out_List(:,cpt_t)=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability
[List_coll] = Collision_ToolBox2.Permeability(In_out_List(:,cpt_t-1),In_out_List(:,cpt_t),Diff.Perma);
end
waitbar(cpt_t/ Diff.dur ,h);
end
% reinterpolate to a dt of 1e-6
%txq=xt;
tt=[0:Diff.dT:(Diff.dur*Diff.dT)];
tq=[0:1e-6:(Diff.dur*Diff.dT)-1e-6];
%
% tic
for cpt_mol=1:1:size(xt,1)
txq(cpt_mol,1,:) = interp1(tt,squeeze(xt(cpt_mol,1,:)),tq);
txq(cpt_mol,2,:) = interp1(tt,squeeze(xt(cpt_mol,2,:)),tq);
txq(cpt_mol,3,:) = interp1(tt,squeeze(xt(cpt_mol,3,:)),tq);
end
% toc
close(h)
end
%% Old simulation mask
function [txq,In_out_List] = SimDiff_mask(Cells_mask,Voxel_dim,Diff)
% disp(['Simulate diffusion distribution from a mask']);
% tic
%% Generate Diffusion Distribution
In_out_List=[];
for cpt_dim=1:1:Diff.dim
xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel
end
h = waitbar(0,['Diffusion distribution ']);
Resolution=Voxel_dim./size(Cells_mask);
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,1)),Resolution);
for cpt_t=2:1:(Diff.dur+1)
% Diffusion-Collision solver - final positions
% Reroll Diffusion until we don't have collision issues anymore
List_coll=[1:1:Diff.N];
while List_coll
% Extracellular
List_Extra = find(In_out_List(:,cpt_t-1));
List_Extra=List_Extra(ismember(List_Extra, List_coll));
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT);
xt(List_Extra,:,cpt_t)=xt(List_Extra,:,cpt_t-1)+tmp_water;
% Intracellular
List_Intra = find(~In_out_List(:,cpt_t-1));
List_Intra=List_Intra(ismember(List_Intra, List_coll));
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT);
xt(List_Intra,:,cpt_t)=xt(List_Intra,:,cpt_t-1)+tmp_water;
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,cpt_t)),Resolution);
In_out_List(:,cpt_t)=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability
[List_coll] = Collision_ToolBox2.Permeability(In_out_List(:,cpt_t-1),In_out_List(:,cpt_t),Diff.Perma);
% figure,
% hold on
% imagesc(Cells_mask(:,:,1))
% %scatter(xt(:,1,cpt_t)./Resolution(1),xt(:,2,cpt_t)./Resolution(1),40,'g')
% plot(squeeze(xt(List_coll,2,1:cpt_t)./Resolution(1))',squeeze(xt(List_coll,1,1:cpt_t)./Resolution(1))','g')
% scatter(xt(List_coll,2,cpt_t)./Resolution(1),xt(List_coll,1,cpt_t)./Resolution(1),40,'r','filled')
end
waitbar(cpt_t/ Diff.dur ,h);
end
txq=xt;
close(h)
end
%% Old simulation mask
function [txq,In_out_List] = Water_distribution_mask_old(Cells_mask,Voxel_dim,Diff)
% disp(['Simulate diffusion distribution from a mask']);
% tic
%% Generate Diffusion Distribution
In_out_List=[];
for cpt_dim=1:1:Diff.dim
xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel
end
h = waitbar(0,['Diffusion distribution ']);
Resolution=Voxel_dim./size(Cells_mask);
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,1)),Resolution);
for cpt_t=2:1:(Diff.dur+1)
% Diffusion-Collision solver - final positions
% Reroll Diffusion until we don't have collision issues anymore
List_coll=[1:1:Diff.N];
while List_coll
% Extracellular
List_Extra = find(In_out_List(:,cpt_t-1));
List_Extra=List_Extra(ismember(List_Extra, List_coll));
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT);
xt(List_Extra,:,cpt_t)=xt(List_Extra,:,cpt_t-1)+tmp_water;
% Intracellular
List_Intra = find(~In_out_List(:,cpt_t-1));
List_Intra=List_Intra(ismember(List_Intra, List_coll));
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT);
xt(List_Intra,:,cpt_t)=xt(List_Intra,:,cpt_t-1)+tmp_water;
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,cpt_t)),Resolution);
In_out_List(:,cpt_t)=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability
[List_coll] = Collision_ToolBox2.Permeability(In_out_List(:,cpt_t-1),In_out_List(:,cpt_t),Diff.Perma);
% figure,
% hold on
% imagesc(Cells_mask(:,:,1))
% %scatter(xt(:,1,cpt_t)./Resolution(1),xt(:,2,cpt_t)./Resolution(1),40,'g')
% plot(squeeze(xt(List_coll,2,1:cpt_t)./Resolution(1))',squeeze(xt(List_coll,1,1:cpt_t)./Resolution(1))','g')
% scatter(xt(List_coll,2,cpt_t)./Resolution(1),xt(List_coll,1,cpt_t)./Resolution(1),40,'r','filled')
end
waitbar(cpt_t/ Diff.dur ,h);
end
% reinterpolate to a dt of 1e-6
txq=xt;
% tt=[0:Diff.dT:(Diff.dur*Diff.dT)];
% tq=[0:1e-6:(Diff.dur*Diff.dT)-1e-6];
%
%
% tic
% for cpt_mol=1:1:size(xt,1)
% txq(cpt_mol,1,:) = interp1(tt,squeeze(xt(cpt_mol,1,:)),tq);
% txq(cpt_mol,2,:) = interp1(tt,squeeze(xt(cpt_mol,2,:)),tq);
% txq(cpt_mol,3,:) = interp1(tt,squeeze(xt(cpt_mol,3,:)),tq);
% end
% toc
% tic
% for cpt_mol=1:1:size(xt,1)
%
%
% for cpt_dim=1:1:3
% ttxq=[];
% for cpt_time=2:1:Diff.dur
% ttxq=[ttxq linspace(xt(cpt_mol,cpt_dim,cpt_time-1),xt(cpt_mol,cpt_dim,cpt_time),round(Diff.dT/1e-6))];
% end
% txq2(cpt_mol,cpt_dim,:)=ttxq;
% end
% end
% toc
close(h)
end
%% Simulation of water inside the mask
function [txq,In_out_List] = Water_distribution_mask(xt,Cells_mask,Voxel_dim,Diff)
%disp(['Simulate diffusion distribution from a mask']);
% tic
Diff.Res=Voxel_dim./size(Cells_mask);
% Generate Diffusion Distribution
In_out_List=[];
% for cpt_dim=1:1:Diff.dim
% xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel %repmat(250*Diff.Res(cpt_dim),Diff.N,1);%
% end
h = waitbar(0,['Diffusion distribution ']);
xt(:,:,1)=DiffSim_ToolBox.Water_distribution_init(Voxel_dim,Cells_mask,Diff);
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,1)),Diff.Res);
tmp_in_out=In_out_List(:,1);
tmp_xt= xt(:,:,1);
cpt_t=2;
txq(:,:,1)=xt(:,:,1);
for cpt_t_tmp=2:1:(Diff.dur+1)
if Diff.Iterative
[tmp_xt,tmp_in_out]=DiffSim_ToolBox.Water_collision_iterative(tmp_xt,tmp_in_out,Cells_mask,Diff,1,1);
else
[tmp_xt,tmp_in_out]=DiffSim_ToolBox.Water_collision(tmp_xt,tmp_in_out,Cells_mask,Diff);
end
% Save only the relevant diffusion displacement
if isfield(Diff,'vMt')
if ~isempty(Diff.vMt)
Idx=find(cpt_t_tmp<Diff.vMt(:,1));
if (~isempty(Idx))
if Diff.vMt(Idx(1),2)
txq(:,:,cpt_t)=tmp_xt;
In_out_List(:,cpt_t)=tmp_in_out;
cpt_t=cpt_t+1;
end
else
txq(:,:,cpt_t)=tmp_xt;
In_out_List(:,cpt_t)=tmp_in_out;
cpt_t=cpt_t+1;
end
else
txq(:,:,cpt_t)=tmp_xt;
In_out_List(:,cpt_t)=tmp_in_out;
cpt_t=cpt_t+1;
end
else
txq(:,:,cpt_t)=tmp_xt;
In_out_List(:,cpt_t)=tmp_in_out;
cpt_t=cpt_t+1;
end
waitbar(cpt_t_tmp/ Diff.dur ,h);
end
% reinterpolate to a dt of 1e-6
% txq=xt;
% toc;
close(h)
end
%% Iterative collision solver
function [xt2,In_out_List] = Water_collision(xt,In_out_List,Cells_mask,Diff)
% Diffusion-Collision solver - final positions
% Reroll Diffusion until we don't have collision issues anymore
List_coll=[1:1:size(xt,1)];
% Extracellular
List_Extra = find(In_out_List);
if List_Extra
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT);
xt2(List_Extra,:)=xt(List_Extra,:)+tmp_water;
end
% Intracellular
List_Intra = find(~In_out_List);
if List_Intra
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT);
xt2(List_Intra,:)=xt(List_Intra,:)+tmp_water;
end
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt2),Diff.Res);
In_out_List_new=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability only
[List_coll] = Collision_ToolBox2.Permeability(In_out_List,In_out_List_new,Diff.Perma);
% We reroll smaller case until we have no more collision.
while List_coll
% Extracellular
List_Extra = find(In_out_List);
List_Extra=List_Extra(ismember(List_Extra, List_coll));
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT);
xt2(List_Extra,:)=xt(List_Extra,:)+tmp_water;
% Intracellular
List_Intra = find(~In_out_List);
List_Intra=List_Intra(ismember(List_Intra, List_coll));
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT);
xt2(List_Intra,:)=xt(List_Intra,:)+tmp_water;
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt2),Diff.Res);
In_out_List_new=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability
[List_coll] = Collision_ToolBox2.Permeability(In_out_List,In_out_List_new,Diff.Perma);
end
end
%% Iterative collision solver
function [xt2,In_out_List] = Water_collision_iterative(xt,In_out_List,Cells_mask,Diff,scale,iter)
% Diffusion-Collision solver - final positions
% Reroll Diffusion until we don't have collision issues anymore
List_coll=[1:1:size(xt,1)];
% Extracellular
List_Extra = find(In_out_List);
if List_Extra
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT/scale);
xt2(List_Extra,:)=xt(List_Extra,:)+tmp_water;
end
% Intracellular
List_Intra = find(~In_out_List);
if List_Intra
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT/scale);
xt2(List_Intra,:)=xt(List_Intra,:)+tmp_water;
end
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt2),Diff.Res);
In_out_List_new=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability only
% in the first iterative test
if scale==1
[List_coll] = Collision_ToolBox2.Permeability(In_out_List,In_out_List_new,Diff.Perma);
else
[List_coll] = Collision_ToolBox2.Permeability(In_out_List,In_out_List_new,0);
end
% We reroll smaller case until we have no more collision.
if List_coll
if scale<=2
[xt2(List_coll,:),~] = DiffSim_ToolBox.Water_collision_iterative(xt(List_coll,:),In_out_List(List_coll,:),Cells_mask,Diff,scale*2,1);
else % To avoid ultra long recursive call, we impose a bottom floor
while List_coll
% Extracellular
List_Extra = find(In_out_List);
List_Extra=List_Extra(ismember(List_Extra, List_coll));
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT/scale);
xt2(List_Extra,:)=xt(List_Extra,:)+tmp_water;
% Intracellular
List_Intra = find(~In_out_List);
List_Intra=List_Intra(ismember(List_Intra, List_coll));
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT/scale);
xt2(List_Intra,:)=xt(List_Intra,:)+tmp_water;
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt2),Diff.Res);
In_out_List_new=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability
[List_coll] = Collision_ToolBox2.Permeability(In_out_List,In_out_List_new,0);
end
end
end
% At this point we have run everything for dT/scale so now we
% have to rerun the same dT/scale to get the other half of the
% trajectory
if scale~=1 & iter
[xt2,~] = DiffSim_ToolBox.Water_collision_iterative(xt2,In_out_List,Cells_mask,Diff,scale,0);
end
end
%% Old simulation
function [txq,In_out_List] = Water_distribution(Cells,Voxel_dim,Diff)
%disp(['Simulate diffusion distribution ']);
% tic
%% Generate Diffusion Distribution
In_out_List=[];
for cpt_dim=1:1:Diff.dim
xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel
end
h = waitbar(0,['Diffusion distribution ']);
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection(Cells, squeeze(xt(:,:,1)));
for cpt_t=2:1:(Diff.dur+1)
% Diffusion-Collision solver - final positions
% Reroll Diffusion until we don't have collision issues anymore
List_coll=[1:1:Diff.N];
while List_coll
% Extracellular
List_Extra = find(In_out_List(:,cpt_t-1));
List_Extra=List_Extra(ismember(List_Extra, List_coll));
tmp_water=[];
tmp_water = DiffSim_ToolBox.diffusion_step(length(List_Extra),Diff.D,Diff.dim,Diff.dT);
xt(List_Extra,:,cpt_t)=xt(List_Extra,:,cpt_t-1)+tmp_water;
% Intracellular
List_Intra = find(~In_out_List(:,cpt_t-1));
List_Intra=List_Intra(ismember(List_Intra, List_coll));
tmp_water=[];
tmp_water= DiffSim_ToolBox.diffusion_step(length(List_Intra),Diff.Din,Diff.dim,Diff.dT);
xt(List_Intra,:,cpt_t)=xt(List_Intra,:,cpt_t-1)+tmp_water;
% Collision detection
[tmp_List] = Collision_ToolBox2.Collision_Detection(Cells, squeeze(xt(:,:,cpt_t)));
In_out_List(:,cpt_t)=tmp_List; % New list of locations
% Diffusion-Collision solver with or without Permability
[List_coll] = Collision_ToolBox2.Permeability(In_out_List(:,cpt_t-1),In_out_List(:,cpt_t),Diff.Perma);
end
waitbar(cpt_t/ Diff.dur ,h);
end
% reinterpolate to a dt of 1e-6
%txq=xt;
tt=[0:Diff.dT:(Diff.dur*Diff.dT)];
tq=[0:1e-6:(Diff.dur*Diff.dT)-1e-6];
%
% tic
for cpt_mol=1:1:size(xt,1)
txq(cpt_mol,1,:) = interp1(tt,squeeze(xt(cpt_mol,1,:)),tq);
txq(cpt_mol,2,:) = interp1(tt,squeeze(xt(cpt_mol,2,:)),tq);
txq(cpt_mol,3,:) = interp1(tt,squeeze(xt(cpt_mol,3,:)),tq);
end
% toc
% tic
% for cpt_mol=1:1:size(xt,1)
%
%
% for cpt_dim=1:1:3
% ttxq=[];
% for cpt_time=2:1:Diff.dur
% ttxq=[ttxq linspace(xt(cpt_mol,cpt_dim,cpt_time-1),xt(cpt_mol,cpt_dim,cpt_time),round(Diff.dT/1e-6))];
% end
% txq2(cpt_mol,cpt_dim,:)=ttxq;
% end
% end
% toc
close(h)
end
%% Simulation of isotropique water
function [xt]= Water_distribution_iso(Voxel_dim,Diff)
for cpt_dim=1:1:Diff.dim
xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel %repmat(250*Diff.Res(cpt_dim),Diff.N,1);%
% xt(:,cpt_dim,1)= zeros(Diff.N,1)*Voxel_dim(cpt_dim);
end
h = waitbar(0,['Diffusion distribution ']);
for cpt_t=2:1:(Diff.dur)
tmp_water = DiffSim_ToolBox.diffusion_step(Diff.N,Diff.D,Diff.dim,Diff.dT);
xt(:,:,cpt_t)=xt(:,:,cpt_t-1)+tmp_water;
waitbar(cpt_t/ Diff.dur ,h);
end
close(h)
end
%% Init the water in the voxel in the given compartment
function [xt]= Water_distribution_init(Voxel_dim,Cells_mask,Diff)
xt=[];
for cpt_dim=1:1:Diff.dim
xt(:,cpt_dim,1)= rand(Diff.N,1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel %repmat(250*Diff.Res(cpt_dim),Diff.N,1);%
end
% If we specified a compartement we are interested in
tic
if ~isempty(Diff.Compartement)
In_out_List=[];
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,1)),Diff.Res);
List = find(In_out_List==Diff.Compartement); % List of Molecules outside the compartment of interest
while List % We reroll the molecules until everything is in the compartement of interest
for cpt_dim=1:1:Diff.dim
xt(List,cpt_dim,1)= rand(length(List),1)*Voxel_dim(cpt_dim); % Uniform distribution along the voxel %repmat(250*Diff.Res(cpt_dim),Diff.N,1);%
end
[In_out_List(:,1)] = Collision_ToolBox2.Collision_Detection_Mask(Cells_mask, squeeze(xt(:,:,1)),Diff.Res);
List = find(In_out_List~=Diff.Compartement);
end
end
toc
end
function MRResults=local_MR_distribution(MR_struct,Water)
for cpt_MR=1:1:length(MR_struct.struct)
MR_params=MR_struct.struct{cpt_MR};
%disp([' Simulating MR distribution ' num2str(cpt_MR) ' with Diff distribution ' ]);
%%% 0) Init
MRResults=[];
for cpt_g=1:1:MR_params.Waveform % Loop over waveforms
for cpt_b=1:1:length(MR_params.Bval) % Loop over b-values
%%% 1) Generate the MR waveform
[G, t]= Waveform_ToolBox.Factory(MR_params.Dn(cpt_g,:),MR_params.Gn(cpt_g,:),MR_params.RampTime,MR_params.dT*1e6,MR_params.Bval);
G=G*10^-3; % Convert from mT/m to T/m
%%% 2) Simulate the MR experiement
[MRResults.Att_approx(cpt_g,cpt_b,:,:), MRResults.Phi_dist(cpt_g,cpt_b,:,:,:)]= DiffSim_ToolBox.Simulate_MR(Water,MR_params,G) ; % [Waveform Direction] % Grad in [T/m], dimension : NPoints x Waveform with dT corresponding to World.dT
%%% 3) Calculate the ADC and tensor paramaters from the MR simulation and collect the results
MRResults.ADC(cpt_g,cpt_b) = DiffSim_ToolBox.ADC_mean(abs(squeeze(MRResults.Att_approx(cpt_g,cpt_b,:,:))),MR_params.Bval(cpt_b));
[~, MRResults.EigValue(cpt_g,cpt_b,:), MRResults.EigVector(cpt_g,cpt_b,:,:),MRResults.MD(cpt_g,cpt_b),MRResults.FA(cpt_g,cpt_b),~]= DiffSim_ToolBox.Tensor(abs(squeeze(MRResults.Att_approx(cpt_g,cpt_b,:,:))),MR_params.Dir,MR_params.Bval(cpt_b));
MRResults.Magn(cpt_g,cpt_b,:)= MRResults.Att_approx(cpt_g,cpt_b,1,:);
MRResults.Ph(cpt_g,cpt_b,:)= mean( MRResults.Phi_dist(cpt_g,cpt_b,1,:,:),5);
end
end
%%% 3) Save the results
end
end
%%%% Main functions %%%%%
function Simulate_cell_structure(Cell_struct,varargin)
for cpt_cell=1:1:length(Cell_struct.struct)
tic
disp([' Simulating cell structure ' num2str(cpt_cell)]);
%%% 0) Init
Cell_params=Cell_struct.struct{cpt_cell};
Cells=[];
Cells_mask=[];
Mask_id=[];
%%% 1) Simulate a basic 2D cell structure
[Cells] = Cellule_ToolBox2.Cell_structure(Cell_params.Cell,Cell_params.Voxel,Cell_params.ECV,1000,5,Cell_params.Bridge);
%%% 2) We generate a mask from structure
[Cells_mask, Mask_id]=Cellule_ToolBox2.Cells_2_Mask(Cells,Cell_params.Voxel,Cell_params.MaskRes);
Cells_mask(Cells_mask>=1)=1;
%%% 3) Check the real ECV from the Mask
Cell_params.ECV_calculate=1-sum(Cells_mask(:))/(size(Cells_mask,1)*size(Cells_mask,2)*size(Cells_mask,3));
disp([' Cell structure generated in ' num2str(toc) ' with ECV from mask = ' num2str(Cell_params.ECV_calculate)]);
%%% 4)SAve the results
save([Cell_params.folder_out '_' num2str(Cell_params.ID) '.mat'],'Cell_params','Cells','Cells_mask','Mask_id')
end
end
function Simulate_diffusion_distribution(Diff_struct,varargin)
for cpt_diff=1:1:length(Diff_struct.struct)
Diff_params=Diff_struct.struct{cpt_diff};
CellList = dir([Diff_params.folder_in '*.mat']); %get list of files and folders in any subfolder
CellList = CellList(~[CellList.isdir]); %remove folders from list
for cpt_cell=1:1:length(CellList)
tic
disp([' Simulating diff distribution ' num2str(cpt_diff) ' with cell structure ' CellList(cpt_cell).name]);
%%% 0) Init
Water=[];
In_out_List=[];
DiffResults=[];
Diff_params.file_in=[CellList(cpt_cell).folder '/' CellList(cpt_cell).name];
load(Diff_params.file_in);
Diff_params.Res=Cell_params.Voxel./size(Cells_mask);
Water_in = DiffSim_ToolBox.Water_distribution_init(Cells_mask,Cell_params.Voxel,Diff_params);
%%% 1) Simulate the water inside the cells
[Water,In_out_List] = DiffSim_ToolBox.Water_distribution_mask(Water_in,Cells_mask,Cell_params.Voxel,Diff_params);
%%% 2) Calculate the diff parameters of the distribution
DiffResults.ADC = DiffSim_ToolBox.ADC_Water_mean(Water,Diff_params.dT,Diff_params.dur*Diff_params.dT);
[~, DiffResults.EigValue,DiffResults.EigVector,DiffResults.MD,DiffResults.FA,~] = DiffSim_ToolBox.Tensor_Water(Water,Diff_params.dT,Diff_params.dur*Diff_params.dT);
DiffResults.ADC_water_in = DiffSim_ToolBox.ADC_Water_mean(Water(find(In_out_List(:,1)),:,:),Diff_params.dT,Diff_params.dur*Diff_params.dT);
[~, DiffResults.EigValue_in,DiffResults.EigVector_in,DiffResults.MD_in,DiffResults.FA_in,~] = DiffSim_ToolBox.Tensor_Water(Water(find(In_out_List(:,1)),:,:),Diff_params.dT,Diff_params.dur*Diff_params.dT);
DiffResults.ADC_water_out = DiffSim_ToolBox.ADC_Water_mean(Water(find(~In_out_List(:,1)),:,:),Diff_params.dT,Diff_params.dur*Diff_params.dT);
[~, DiffResults.EigValue_out,DiffResults.EigVector_out,DiffResults.MD_out,DiffResults.FA_out,~] = DiffSim_ToolBox.Tensor_Water(Water(find(~In_out_List(:,1)),:,:),Diff_params.dT,Diff_params.dur*Diff_params.dT);
disp([' Diff distribution generated in ' num2str(toc)]);
%%% 3) Save the results
save([Diff_params.folder_out '_' num2str(Diff_params.ID) '_Cell' num2str(Cell_params.ID) '.mat'],'Diff_params','Cell_params','Water','In_out_List','DiffResults')
end
end
end
function Simulate_displ_distribution(Disp_struct,varargin)
for cpt_disp=1:1:length(Disp_struct.struct)
Disp_params=Disp_struct.struct{cpt_disp};
DiffList = dir([Disp_params.folder_in '*.mat']); %get list of files and folders in any subfolder
DiffList = DiffList(~[DiffList.isdir]); %remove folders from list
load(Disp_params.file_in);
for cpt_diff=1:1:length(DiffList)
tic
disp([' Simulating Displ distribution ' num2str(cpt_disp) ' with Diff distribution ' DiffList(cpt_diff).name]);