-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_philips_list.py
1433 lines (1280 loc) · 71.4 KB
/
read_philips_list.py
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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 31 14:01:08 2011
@author: Eric
"""
def read_philips_list(filename,phasefilename='',fieldmapname='',sensefilename='',coords='',readtype='new'):
from os.path import split
from nibabel import load
from numpy import flipud, array, rot90, fliplr, shape, zeros, diag, eye, dot, ones, meshgrid, linspace, expand_dims
from numpy.linalg import inv, lstsq
from numpy.fft import ifftshift, ifftn
from scipy.signal import medfilt
from scipy.ndimage.interpolation import affine_transform
f=open(filename,'r',).readlines()
scan={}
scan['parameterfilename']=filename
scan['datafilename']=filename[:-4]+'data'
scan['pathname'],scan['basename']=split(filename)
#print scan['parameterfilename']
#print scan['datafilename']
scan['scaninfo']={}
scan['datainfo']={}
scan['datainfo']['STD']=[] #Standard data vector (image data or spectroscopy data)
scan['datainfo']['REJ']=[] #Rejected standard data vector
scan['datainfo']['PHX']=[] #Correction data vector for EPI/GraSE phase correction
scan['datainfo']['FRX']=[] #Correction data vector for frequency spectrum correction
scan['datainfo']['NOI']=[] #Preparation phase data vector for noise determination
scan['datainfo']['NAV']=[] #Phase navigator data vector
scan['scaninfo']['number_of_coil_channels']={}
scan['scaninfo']['number_of_coil_channels'][0]=1 #because it doesn't always exist
scan['scaninfo']['kx_range']=[0,0]
scan['scaninfo']['ky_range']=[0,0]
scan['scaninfo']['kz_range']=[0,0]
scan['scaninfo']['kx_oversample_factor']=1
for line in f:
if line[0]!='#': #if the line isn't a comment
#print line
splitline=line.split()
if splitline[0]=='.': #global scan data
#print len(splitline)
if len(splitline)==7: #if there is no start and end
try: #assume integers
scan['scaninfo'][splitline[4]]=int(splitline[6])
except: #if they are floats
scan['scaninfo'][splitline[4]]=float(splitline[6])
elif len(splitline)==10: #for the stupid channel number
#print splitline
scan['scaninfo'][splitline[4]+'_'+splitline[5]+'_'+splitline[6]+'_'+splitline[7]][int(splitline[3])]=int(splitline[9])
elif len(splitline)==9: #for the SENSE factor
scan['scaninfo'][splitline[4]+'_'+splitline[5]+'_'+splitline[6]]=float(splitline[8])
else:
#print line
scan['scaninfo'][splitline[4]]=(int(splitline[6]),int(splitline[7]))
else:
#print splitline
scan['datainfo'][splitline[0]].append([int(i) for i in splitline[1:]])
#print line.split()
#this is the list of parameters that you get!
#mix dyn card echo loca chan extr1 extr2 ky kz n.a. aver sign rf grad enc rtop rr size offset
#figure out the parameters of the scan for easier reading (both data and human)
if scan['scaninfo']['kx_range'][0]==scan['scaninfo']['kx_range'][1]: #if cpx data
scan['scaninfo']['ifcpx']=1
scan['scaninfo']['xsize']=scan['scaninfo']['X-resolution']
scan['scaninfo']['ysize']=scan['scaninfo']['Y-resolution']
scan['scaninfo']['zsize']=scan['scaninfo']['Z-resolution']
else:
scan['scaninfo']['ifcpx']=0
scan['scaninfo']['xsize']=(scan['scaninfo']['kx_range'][1]+1)*2#scan['scaninfo']['kx_range'][1]-scan['scaninfo']['kx_range'][0]+1
scan['scaninfo']['ysize']=scan['scaninfo']['ky_range'][1]*2+1#scan['scaninfo']['ky_range'][1]-scan['scaninfo']['ky_range'][0]+1
if scan['scaninfo']['ky_range'][1]-scan['scaninfo']['ky_range'][0]+1 !=scan['scaninfo']['ysize']:
scan['partialfourier']=1
else:
scan['partialfourier']=0
scan['scaninfo']['zsize']=scan['scaninfo']['kz_range'][1]*2+1#scan['scaninfo']['kz_range'][1]-scan['scaninfo']['kz_range'][0]+1
#print scan['scaninfo']['number_of_coil_channels']
#print 1 in scan['scaninfo']['number_of_coil_channels']
# the refscan for coil sensitivity is complicated to parse...
if (1 in scan['scaninfo']['number_of_coil_channels']) and scan['scaninfo']['number_of_coil_channels'][0]!=scan['scaninfo']['number_of_coil_channels'][1]:
scan['scaninfo']['ifrefscan']=1
#ncoils=scan['scaninfo']['number_of_coil_channels'][0]+1 #special case for refscan
#nslices=scan['scaninfo']['number_of_locations']-1
#refscan_flag=1
else:
scan['scaninfo']['ifrefscan']=0
scan['scaninfo']['ncoils']=scan['scaninfo']['number_of_coil_channels'][0] #note that these need to be edited in below in some cases...
scan['scaninfo']['nslices']=scan['scaninfo']['number_of_locations']
#refscan_flag=0
#print refscan_flag
scan['scaninfo']['ndynamics']=scan['scaninfo']['number_of_dynamic_scans']
scan['scaninfo']['naverages']=scan['scaninfo']['number_of_signal_averages'] #averages are just averages (it looks like there are 2 b=0 images by default)
scan['scaninfo']['nextra1']=scan['scaninfo']['number_of_extra_attribute_1_values'] #this looks like the number of diffusion values (including b=0)
scan['scaninfo']['nextra2']=scan['scaninfo']['number_of_extra_attribute_2_values'] #this looks like the number of diffusion directions (NOT including b=0)
try:
scan['scaninfo']['sense_factor']=max(scan['scaninfo']['Y-direction_SENSE_factor'],scan['scaninfo']['X-direction_SENSE_factor'])
except:
scan['scaninfo']['sense_factor']=1
if readtype=='large':
scan=read_philips_list_raw(scan)
else:
scan=read_philips_dic_raw(scan)
#scan=read_philips_dic_fiddle(scan)
#and lets read any external phase correction data
scan['datainfo']['auxiliary_phase_data']={}
if phasefilename!='':
print('-------------reading external phase file-------------------------')
scan['datainfo']['auxiliary_phase_data']=read_philips_list(phasefilename)
print('-------------completed reading external phase file---------------')
scan['scaninfo']['auxiliary_phase_data_filename']=phasefilename
if fieldmapname!='':
print('Reading fieldmap file '+fieldmapname)
scan['fieldmap']=array(load(fieldmapname).get_data().squeeze())
#scan['fieldmap'][scan['fieldmap']==-500]=0 #direct nii
scan['fieldmap']=fliplr(rot90(fliplr(scan['fieldmap']))) #mipav nii
#scan['fieldmap']=fliplr(rot90(scan['fieldmap'])) #direct nii
#scan['fieldmap']/=max(abs(scan['fieldmap'].flatten()))
#scan['fieldmap']=flipud(scan['fieldmap']) #not sure..
#FIELD MAP SCALING!
#test=parrec.load('/home/eric/data/20120802_prop_testing2/20120802_ETP_prop_test2_WIP_B0MAP_34_1.PAR')
#header=test.get_header()
#imdefs=header.image_defs
#intercept=imdefs[1][11]
#slope=imdefs[1][12]
scan['scaninfo']['fieldmap_filename']=fieldmapname
#and read any SENSE sensitivity maps that were acquired
scan['scaninfo']['sensemap_filename']=sensefilename
if sensefilename!='':
print('------------reading SENSE file-----------------------------------')
tmp=read_philips_list(sensefilename)
imgs=philips_dic_compactify(tmp)
del tmp
si=shape(imgs)
#print si
ncoils=si[-1]-1
nslices=scan['scaninfo']['nslices']
thresh=max(imgs[:,:,:,ncoils].ravel())/5 #arbitrary threshold
mask=zeros(si[:-1],'bool')
mask[imgs[:,:,:,ncoils]>thresh]=1
for nc in range(ncoils):
imgs[:,:,:,nc]/=imgs[:,:,:,ncoils]
imgs[:,:,:,nc]*=mask
imgs=imgs[:,:,:,0:ncoils].transpose([2,0,1,3])
#imgs=imgs[:,::-1,:,:] #flip horizontal in the final image
#imgs=imgs[:,:,::-1,:] #flip image in z
si=shape(imgs)
fovxy=coords[0] #todo, make getting the slice location automatic!
fovz=coords[1]
zloc=coords[2]
fov_sense=[300.0,300.0,450.0] #guess for now in x,y,z
rescalesize=max(scan['scaninfo']['xsize'],scan['scaninfo']['ysize'])/2.0
pixdim=[fovxy/rescalesize,fovxy/rescalesize,fovz/nslices]
pixdim_sense=[fov_sense[0]/si[0],fov_sense[1]/si[1],fov_sense[2]/si[2]]
#sp=[rescalesize,rescalesize,nslices]
#print pixdim
#print pixdim_sense
#print si
#print sp
#print fov_sense
vec=[pixdim[0]/pixdim_sense[0],pixdim[1]/pixdim_sense[1],pixdim[2]/pixdim_sense[2],1,1]
sp_fiction=array(si[0:3])/array(vec[0:3])
#print(fov_sense,fovxy)
#print(si[0:3],vec[0:3],sp_fiction)
#print sp_fiction
transmat1=eye(5) #[rescalesize/2,rescalesize/2,si[2]/2+fov_sense[2]*zloc/si[2],0]
transmat2=eye(5)
#u/d, l/r, a/p
#NOTE: this is not quite right, I am still missing something in here! What could it be?
#I am doing a shift for FOV and a shift for the image size...
#transmat1[:,4]=[si[0]*pixdim_sense[0]/(pixdim[0]*2.0),si[1]/2.0,si[2]*pixdim_sense[2]/(2.0*pixdim[2]),0,1]#zloc/fov_sense[2]+si[2]/2,0,1]
transmat1[:,4]=[sp_fiction[0]/2.0,sp_fiction[1]/2.0,sp_fiction[2]/2.0,0,1]
#the transmat1 below is almost right using a transmat2 of eye!
#transmat1[:,4]=[-rescalesize/2.0*fovxy/fov_sense[0]+rescalesize/2.0,-rescalesize/2.0*fovxy/fov_sense[1]+rescalesize/2.0,pixdim[2]*si[2]/2.0,0,1]
#vec=[fovxy*si[0]/(rescalesize*fov_sense[0]),fovxy*si[1]/(rescalesize*fov_sense[1]),fovz*si[2]/fov_sense[2],1,1]
affinemat=diag(vec)
#transmat2[:,4]=[pixdim_sense[0]*si[0]/2.0,pixdim_sense[1]*si[1]/2.0,pixdim_sense[2]*si[2]/2,0,1]
#transmat2[:,4]=[fov_sense[0]*pixdim[0]/(pixdim_sense[0]*2.0),fov_sense[1]*pixdim[1]/(pixdim_sense[1]*2.0),fov_sense[2]*pixdim[2]/(2.0),0,1]
#transmat1 and transmat2 make a good z combo!
#transmat2[:,4]=[sp[0]/2.0,sp[1]/2.0,si[2]*pixdim_sense[2]/(2.0*pixdim[2]),0,1]
#transmat2[:,4]=[sp[0]*pixdim[0]/(2.0*pixdim_sense[0]),sp[1]*pixdim[1]/(2.0*pixdim_sense[1]),si[2]*pixdim_sense[2]/(2.0*pixdim[2]),0,1]
transmat2[:,4]=[-si[0]/2.0,-si[1]/2.0,-si[2]/2,0,1]#*pixdim_sense[2]/(2.0*pixdim[2]),0,1]
#transmat2[:,4]=[sp_fiction[0]/2.0,sp_fiction[1]/2.0,sp_fiction[2]/2.0,0,1]
tmat=dot(dot(transmat1,affinemat),transmat2)
#translation=tmat[:-1,4]
#translation[2]=150.0
translation=[(fov_sense[0]-fovxy)/2.0,(fov_sense[1]-fovxy)/2.0,zloc+sp_fiction[2]/2.0,0]
vector=diag(tmat)[:-1]
#print transmat1
#print affinemat
#print transmat2
#print tmat
#print transmat1
#print affinemat
#print transmat2
#print dot(transmat1,affinemat)
#print inv(transmat2)
#print vector
#print translation
#scan['datainfo']['sense']=zeros([rescalesize,rescalesize,])
#scan['datainfo']['sense_orig']=imgs
scan['datainfo']['sense']=affine_transform(imgs.real,vector,output_shape=[rescalesize,rescalesize,nslices,si[3]],offset=translation)+1j*affine_transform(imgs.imag,vector,output_shape=[rescalesize,rescalesize,nslices,si[3]],offset=translation)
scan['datainfo']['sense']=scan['datainfo']['sense']/max(abs(scan['datainfo']['sense'].ravel())) #normalize to 1
#scan['datainfo']['sense']=affine_transform(imgs.real,[fovxy*si[0]/(rescalesize*fov_sense[0]),fovxy*si[1]/(rescalesize*fov_sense[1]),fovz*si[2]/fov_sense[2],1],output_shape=[rescalesize,rescalesize,nslices,si[3]],offset=[rescalesize/2,rescalesize/2,si[2]/2+fov_sense[2]*zloc/si[2],0])+1j*affine_transform(imgs.imag,[fovxy*si[0]/(rescalesize*fov_sense[0]),fovxy*si[1]/(rescalesize*fov_sense[1]),fovz*si[2]/fov_sense[2],1],output_shape=[rescalesize,rescalesize,nslices,si[3]],offset=[rescalesize/2,rescalesize/2,si[2]/2+fov_sense[2]*zloc/si[2],0])
scan['datainfo']['sense_orig']=scan['datainfo']['sense'].copy()
if False:
npix=rescalesize**2
xlocs,ylocs=meshgrid(linspace(-rescalesize/2,rescalesize/2,rescalesize),linspace(-rescalesize/2,rescalesize/2,rescalesize))
xlocs=xlocs.ravel()
ylocs=ylocs.ravel()
print(shape(xlocs))
v_full = array([ones(npix), xlocs, ylocs, xlocs**2, xlocs*ylocs, ylocs**2])#, xlocs**3, ylocs*xlocs**2, xlocs*ylocs**2, ylocs**3])
for k in range(ncoils):
locs=abs(scan['datainfo']['sense'][:,:,0,k].ravel())>0.01
ct=len(xlocs[locs])
print(shape(xlocs[locs]))
print(ct)
v = array([ones(ct), xlocs[locs], ylocs[locs], xlocs[locs]**2, xlocs[locs]*ylocs[locs], ylocs[locs]**2])#, xlocs[locs]**3, ylocs[locs]*xlocs[locs]**2, xlocs[locs]*ylocs[locs]**2, ylocs[locs]**3])
print(shape(scan['datainfo']['sense'][:,:,0,k]))
print(shape(v),shape(v_full))
imdata=scan['datainfo']['sense'][:,:,0,k].ravel()
print(shape(imdata[locs]))
m,_,_,_=lstsq(v.T,imdata[locs]) #fix 0 slices
print(shape(m))
#imdata[~locs]=0
#scan['datainfo']['sense'][:,:,0,k]=imdata.reshape([rescalesize,rescalesize])#dot(m,v_full).reshape([rescalesize,rescalesize])
scan['datainfo']['sense'][:,:,0,k]=dot(m,v_full).reshape([rescalesize,rescalesize])
#old k-space approach
# scan['datainfo']['sense']=read_philips_list(sensefilename)
# #sum averages
# scan['datainfo']['sense']['image']={}
# for elem in scan['datainfo']['sense']['kspace']:
# try:
# scan['datainfo']['sense']['image'][elem[0]]=scan['datainfo']['sense']['kspace'][elem]
# except:
# scan['datainfo']['sense']['image'][elem[0]]+=scan['datainfo']['sense']['kspace'][elem]
# s=shape(scan['datainfo']['sense']['image'][0])
# center=s[1]/2
# dist=s[0]/2
# #check the spatial registration!
# for elem in scan['datainfo']['sense']['image']:
# #not sure about the extra ifftshift on the following line
# scan['datainfo']['sense']['image'][elem]=ifftshift(ifftshift(ifftn(ifftshift(scan['datainfo']['sense']['image'][elem]))),axes=0)
# scan['datainfo']['sense']['image'][elem]=rot90(scan['datainfo']['sense']['image'][elem][:,center-dist:center+dist,:],-1)
# scan['datainfo']['sense']['image'][elem]=medfilt(scan['datainfo']['sense']['image'][elem][:,:,0].real,7)+1j*medfilt(scan['datainfo']['sense']['image'][elem][:,:,0].imag,7)
print('------------done reading SENSE file------------------------------')
return scan
def read_philips_list_raw(scan): #OUTDATED
from numpy import zeros, prod
from struct import unpack
xsize=scan['scaninfo']['xsize']
ysize=scan['scaninfo']['ysize']
zsize=scan['scaninfo']['zsize']
if scan['scaninfo']['ifrefscan']: #if it is a refscan
ncoils=scan['scaninfo']['ncoils']+1
nslices=scan['scaninfo']['nslices']-1
else:
ncoils=scan['scaninfo']['ncoils']
nslices=scan['scaninfo']['nslices']
ndynamics=scan['scaninfo']['ndynamics']
naverages=scan['scaninfo']['naverages']
nextra1=scan['scaninfo']['nextra1']
nextra2=scan['scaninfo']['nextra2']
matrix=[ysize,xsize,zsize,ncoils,nslices,ndynamics,naverages,nextra1,nextra2] #zsize is untested
scan['scaninfo']['ksize']=matrix
#OK, this is bumping up on memory issues. I should consider just doing a
#single image allocation for the images and coils, and use dictionary keys
#to encode everything beyond that.
print ['Allocating a',prod(matrix)*8/(1024*1024),'MB image:',matrix]
kspace=zeros(matrix,complex)
f=open(scan['datafilename'],'rb')
for k in scan['datainfo']['STD']:
if ncoils==1: #it seems uninitialized
k[5]=0
if scan['scaninfo']['ifrefscan']==1: #seems like unique numbers for the refscan
y_tmp=k[8] #using image coordinates, not kspace coordinates
if k[4]==1: #body coil I believe
k[4]=0 #set location to 0
k[5]=0 #set coil to 0
else:
k[5]+=1 #if a multi-channel coil
else:
y_tmp=k[8]+scan['scaninfo']['ky_range'][1] #kspace coordinates
f.seek(k[19])
#print('{}f'.format(k[18]/4))
floats=unpack('{}f'.format(k[18]/4),f.read(k[18]))
#print flen
#print k[12]
for fl in range(matrix[1]): #always even
#if k[12]==1 or k[12]==0: #it looks like the sign variable indicates the direction, but the reversal has already occurred
# fl_tmp=fl
#else:
# fl_tmp=matrix[1]-fl-1
kspace[y_tmp,fl,k[9],k[5],k[4],k[1],k[11],k[6],k[7]]=complex(floats[2*fl],floats[2*fl+1])
#kspace[y_tmp,fl,k[9],k[5],k[4],k[1],k[11],k[6],k[7]]=1
#print k
#print scan
scan['kspace']=kspace
return scan
def read_philips_dic_raw(scan): #reads the images into the dictionary rather than as a huge image
from numpy import zeros, prod, array#, roll, conj
from struct import unpack
scan['kspace']={}
scan['phasecorr_orig']={}
scan['frequencycorr_orig']={}
scan['scaninfo']['readout_direction']={}
#scan['kspace'][0]={}
#scan['kspace'][0][0]={}
#scan['kspace'][0][0][0]={}
#scan['kspace'][0,0,0,0,0,0]={}
#scan['kspace'][5][0]={}
#scan['kspace'][0][0][0][0][0]=[]
xsize=scan['scaninfo']['xsize']
ysize=scan['scaninfo']['ysize']
zsize=scan['scaninfo']['zsize']
#if scan['scaninfo']['ifrefscan']: #if it is a refscan
#ncoils=scan['scaninfo']['ncoils']+1
#nslices=scan['scaninfo']['nslices']-1
#else:
#ncoils=scan['scaninfo']['ncoils']
#nslices=scan['scaninfo']['nslices']
#ndynamics=scan['scaninfo']['ndynamics']
#naverages=scan['scaninfo']['naverages']
#nextra1=scan['scaninfo']['nextra1']
#nextra2=scan['scaninfo']['nextra2']
matrix=[ysize,xsize,zsize] #ncoils,nslices,ndynamics,naverages,nextra1,nextra2] #zsize is untested
scan['scaninfo']['ksize']=matrix
#OK, this is bumping up on memory issues. I should consider just doing a
#single image allocation for the images and coils, and use dictionary keys
#to encode everything beyond that.
print ['The maximum size is',[ysize,xsize,zsize,scan['scaninfo']['ncoils'],scan['scaninfo']['nslices'],scan['scaninfo']['ndynamics'],scan['scaninfo']['naverages'],scan['scaninfo']['nextra1'],scan['scaninfo']['nextra2']]]
nimages=0
#kspace=zeros(matrix,complex)
f=open(scan['datafilename'],'rb')
ctr=0
for k in scan['datainfo']['STD']:
#if ncoils==1: #it seems uninitialized
#k[5]=0
if scan['scaninfo']['ifrefscan']==1: #seems like unique numbers for the refscan
y_tmp=k[8] #using image coordinates, not kspace coordinates
#if k[4]==1: #body coil I believe
#k[4]=0 #set location to 0
#k[5]=0 #set coil to 0
#else:
#k[5]+=1 #if a multi-channel coil
else:
y_tmp=k[8]+scan['scaninfo']['ky_range'][1] #kspace coordinates
#print y_tmp
#print [k[4],k[1],k[11],k[6],k[7]]
try: #we need to create the image if it doesn't exist
scan['kspace'][k[5],k[4],k[1],k[11],k[6],k[7]][0,0,0]
except KeyError:
#print ['KeyError',k[4],k[1],k[11],k[6],k[7]]
scan['kspace'][k[5],k[4],k[1],k[11],k[6],k[7]]=zeros(matrix,complex)
nimages+=1
scan['scaninfo']['readout_direction'][k[5],k[4],k[1],k[11],k[6],k[7]]=zeros([ysize,zsize],int)
#print k
scan['scaninfo']['readout_direction'][k[5],k[4],k[1],k[11],k[6],k[7]][y_tmp,k[9]]=int(k[12])
#print type(k[19])
if scan['scaninfo']['ifcpx']==1:
if y_tmp==0:
ctr+=1
f.seek(k[19]+512*ctr) #for some reason cpx data has 512 byte buffers at each image
else:
f.seek(k[19])
#print('{}f'.format(k[18]/4))
floats=array(unpack('{}f'.format(k[18]/4),f.read(k[18])))
#print flen
#print k[12]
scan['kspace'][k[5],k[4],k[1],k[11],k[6],k[7]][y_tmp,:,k[9]]=floats[::2]+1j*floats[1::2]
#for fl in range(matrix[1]): #always even
#the phase correction takes the readout shifting into account. I am not sure how I feel about that, but thats how it is.
#if k[12]==1 or k[12]==0: #it looks like the sign variable indicates the direction, but the reversal has already occurred
#fl_tmp=fl
#else:
# fl_tmp=matrix[1]-fl-1
#if fl==0:
# scan['kspace'][k[5],k[4],k[1],k[11],k[6],k[7]][y_tmp,fl,k[9]]=complex(1e6,0)
#else:
#scan['kspace'][k[5],k[4],k[1],k[11],k[6],k[7]][y_tmp,fl,k[9]]=complex(floats[2*fl],floats[2*fl+1]) #IS GOOD WITH LOOP
#scan['kspace'][k[5],k[4],k[1],k[11],k[6],k[7]][y_tmp,fl,k[9]]=1
#[channel,location,dynamics,average,extra1,extra2]
#There is an issue with the centering of the FFT which I should deal with somehow...
#With odd numbers of phase encodes at least.
#for element in scan['kspace']:
# scan['kspace'][element]=roll(scan['kspace'][element],1,axis=0)
# print scan['kspace'][element][0,15,0]
# scan['kspace'][element][0,:,0]=conj(scan['kspace'][element][0,:,0])
# print scan['kspace'][element][0,15,0]
scan['scaninfo']['nimages']=nimages
if scan['scaninfo']['ifrefscan']==0:
for k in scan['datainfo']['PHX']:
scan['phasecorr_orig'][k[8],k[9],k[5],k[4],k[12]]=zeros(scan['scaninfo']['X-resolution'],complex)
f.seek(k[19])
floats=unpack('{}f'.format(k[18]/4),f.read(k[18]))
for fl in range(scan['scaninfo']['X-resolution']): #always even
#if k[12]==1 or k[12]==0: #it looks like the sign variable indicates the direction, but the reversal has already occurred
#fl_tmp=fl
#else:
# fl_tmp=matrix[1]-fl-1
scan['phasecorr_orig'][k[8],k[9],k[5],k[4],k[12]][fl]=complex(floats[2*fl],floats[2*fl+1])
#xsize,zsize,coil,location,sign
for k in scan['datainfo']['FRX']:
scan['frequencycorr_orig'][k[8],k[9],k[5],k[4],k[12]]=zeros(scan['scaninfo']['X-resolution'],complex)
f.seek(k[19])
floats=unpack('{}f'.format(k[18]/4),f.read(k[18]))
for fl in range(scan['scaninfo']['X-resolution']): #always even
#if k[12]==1 or k[12]==0: #it looks like the sign variable indicates the direction, but the reversal has already occurred
#fl_tmp=fl
#else:
# fl_tmp=matrix[1]-fl-1
scan['frequencycorr_orig'][k[8],k[9],k[5],k[4],k[12]][fl]=complex(floats[2*fl],floats[2*fl+1])
f.close()
#print k
#print scan
#scan['kspace']=kspace
print ['There is/are',nimages,'image(s)']
print ['Total image size is',nimages*prod(matrix)*8/(1024*1024),'MB']
return scan
def read_philips_dic_fiddle(scan):
from numpy import zeros, unique
#This function just reshapes and fiddles with the images from read_philips_dic_raw to get a reasonable image
#the format is: [ncoils, nslices,ndynamics,naverages,nextra1,nextra2][y,x,z]
#first, we need to figure out the correct size of the image
#The naverages, nextra1, and nextra2 all lie if the image is diffusion, and for the refscan, ncoils also lies
xsize=scan['scaninfo']['xsize']
ysize=scan['scaninfo']['ysize']
zsize=scan['scaninfo']['zsize']
#if scan['scaninfo']['ifrefscan']: #if it is a refscan
# ncoils=scan['scaninfo']['ncoils']+1
# nslices=scan['scaninfo']['nslices']-1
#else:
# ncoils=scan['scaninfo']['ncoils']
# nslices=scan['scaninfo']['nslices']
#ndynamics=scan['scaninfo']['ndynamics']
#naverages=scan['scaninfo']['naverages'] #averages are just averages (it looks like there are 2 b=0 images by default)
#nextra1=scan['scaninfo']['nextra1'] #this looks like the number of diffusion values (including b=0)
#nextra2=scan['scaninfo']['nextra2'] #this looks like the number of diffusion directions (NOT including b=0)
#if nextra2>1: #it is a diffusion scan
# ndirs=nextra1
# naverages-=1 #but what if we have averages?
#else:
# ndirs=1
coils=[]
slices=[]
dynamics=[]
averages=[]
extra1=[]
extra2=[]
for element in scan['kspace']:
coils.append(element[0])
slices.append(element[1])
dynamics.append(element[2])
averages.append(element[3])
extra1.append(element[4])
extra2.append(element[5])
coils=unique(coils)
slices=unique(slices)
dynamics=unique(dynamics)
averages=unique(averages)
extra1=unique(extra1)
extra2=unique(extra2)
ncoils=len(coils)
nslices=len(slices)
ndynamics=len(dynamics)
naverages=len(averages)
#nextra1=len(extra1)
#nextra2=len(extra2)
ndirs=len(extra1)
print ['coils',coils]
print ['slices',slices]
print ['dynamics',dynamics]
print ['averages',averages]
print ['extra1',extra1]
print ['extra2',extra2]
matrix=[ysize,xsize,zsize,ncoils,nslices,ndynamics,naverages,ndirs]
kspace=zeros(matrix,complex)
for element in scan['kspace']:
kspace[:,:,:,element[0],element[1],element[2],element[3],element[4],element[5]]=scan['kspace'][element]
return scan
def philips_dic_compactify(scan,image='kspace'):
from numpy import zeros, shape
#note, this now orders the output images
channel=set([])
location=set([])
dynamics=set([])
average=set([])
extra1=set([])
extra2=set([])
for element in scan[image]:
channel.add(element[0])
location.add(element[1])
dynamics.add(element[2])
average.add(element[3])
extra1.add(element[4])
extra2.add(element[5])
channel=list(channel)
location=list(location)
dynamics=list(dynamics)
average=list(average)
extra1=list(extra1)
extra2=list(extra2)
if image=='kspace':
kspace=zeros([scan['scaninfo']['ysize'],scan['scaninfo']['xsize'],scan['scaninfo']['zsize'],scan['scaninfo']['nimages']],complex)
else:
s=shape(scan['data'][0,0,0,0,0,0])
k=0
for elem in scan['data']:
k+=1
kspace=zeros([s[0],s[1],s[2],k],complex)
k=0
for loc in location:
for dyn in dynamics:
for ext1 in extra1:
for ext2 in extra2:
for chan in channel:
for avg in average:
try:
kspace[:,:,:,k]=scan[image][chan,loc,dyn,avg,ext1,ext2]
k+=1
except:
pass
#for element in scan['kspace']:
# kspace[:,:,:,k]=scan['kspace'][element]
# k+=1
return kspace #this is a little different than the other functions!
def philips_dic_compactify_image(scan,imname='img'):
from numpy import zeros, shape
#note, this now orders the output images
#print scan['scaninfo']['imsize']
#print imname
if imname=='img':
sizename='imsize'
nims='nimages'
#elif imname=='img_square':
else:
sizename='imsize_square'
nims='recon_nimages'
channel=set([])
location=set([])
dynamics=set([])
average=set([])
extra1=set([])
extra2=set([])
for element in scan[imname]:
channel.add(element[0])
location.add(element[1])
dynamics.add(element[2])
average.add(element[3])
extra1.add(element[4])
extra2.add(element[5])
channel=list(channel)
location=list(location)
dynamics=list(dynamics)
average=list(average)
extra1=list(extra1)
extra2=list(extra2)
#print('channel=',channel)
#print('loaction=',location)
#print('dynamics=',dynamics)
#print('average=',average)
#print('extra1=',extra1)
#print('extra2=',extra2)
#print sizename
img=zeros([scan['scaninfo'][sizename][0],scan['scaninfo'][sizename][1],scan['scaninfo']['nslices'],scan['scaninfo'][nims]/scan['scaninfo']['nslices']],complex)
#print shape(img)
k=0
for dyn in dynamics:
for ext1 in extra1:
for ext2 in extra2:
for chan in channel:
for avg in average:
for loc in location:
try:
img[:,:,loc,k]=scan[imname][chan,loc,dyn,avg,ext1,ext2][:,:,0] #2D only!
#k+=1
except:
#print [chan,loc,dyn,avg,ext1,ext2]
pass
try:
scan[imname][chan,0,dyn,avg,ext1,ext2]
k+=1 #stupid counter....
#print k
except:
pass
#print k
#for element in scan[imname]:
#print shape(scan['img'][element])
# print element
# img[:,:,:,k]=scan[imname][element]
# k+=1
return img #this is a little different than the other functions!
def phase_correct_philips(scan,phasecorr=1):
from numpy import zeros, interp, linspace, exp, pi, linspace, ones, conj, sort, array, nonzero, shape, meshgrid, tile, expand_dims, atleast_3d
from numpy.fft import fftshift, ifftshift, ifft, ifftn, fftn, fft
from copy import copy
from scipy.ndimage.filters import gaussian_filter1d
scan['data_tmp']={}
scan['data']={}
scan['phasecorr']={}
scan['frequencycorr']={}
scan['img']={}
scan['img_square']={}
scan['img_square_cg']={}
imsize=copy(scan['scaninfo']['ksize'])
imsize[1]=int(imsize[1]/scan['scaninfo']['kx_oversample_factor'])
scan['scaninfo']['imsize']=copy(imsize)
#imsize=scan['scaninfo']['imsize']
cropidx=[scan['scaninfo']['ksize'][1]/2-imsize[1]/2,scan['scaninfo']['ksize'][1]/2+imsize[1]/2]
freqcorr_warning=1
if phasecorr==1: #I need to acquire correction data for each dynamic angle!!!
x=linspace(scan['scaninfo']['X_range'][0],scan['scaninfo']['X_range'][1],scan['scaninfo']['imsize'][1])
xp=linspace(scan['scaninfo']['X_range'][0],scan['scaninfo']['X_range'][1],scan['scaninfo']['X_range'][1]-scan['scaninfo']['X_range'][0]+1)
if scan['scaninfo']['auxiliary_phase_data_filename']!='':
#if we are using externally collected phase data
#we are just going to go ahead and assume that the data size is correct
#note that correction_angles is only used for interpolation
#if scan['scaninfo']['ndynamics']==scan['datainfo']['auxiliary_phase_data']['scaninfo']['ndynamics']:
correction_angles=180*array(range(scan['datainfo']['auxiliary_phase_data']['scaninfo']['ndynamics']))/float(scan['datainfo']['auxiliary_phase_data']['scaninfo']['ndynamics'])
#print correction_angles
for elem in scan['datainfo']['auxiliary_phase_data']['kspace']:
#print elem
#angle=180*elem[2]/float(scan['scaninfo']['ndynamics'])
#dists=abs(correction_angles-angle)
#dists_sort=sort(dists)[:2]
#idxs=nonzero(dists<=dists_sort[1])[0][:2]
#dists_use=dists[idxs]
#print('angle=',angle)
#print('dists=',dists)
#print('dists_sort=',dists_sort)
##print sort(dists)
#print('idxs=',idxs)
#print('dists_use=',dists_use)
#dists_use/=sum(dists_use)
#dists_use=-1*(dists_use-1)
#print('dists_use=',dists_use)
#tmp1=fftshift(ifft(fftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem[0],elem[1],idxs[0],0,0,0],1),None,1),1)
#tmp1=gaussian_filter1d(tmp1.real,5,1)+1j*gaussian_filter1d(tmp1.imag,5,1)
#tmp2=fftshift(ifft(fftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem[0],elem[1],idxs[1],0,0,0],1),None,1),1)
#tmp2=gaussian_filter1d(tmp2.real,5,1)+1j*gaussian_filter1d(tmp2.imag,5,1)
#tmp=dists_use[0]*tmp1+dists_use[1]*tmp2
##tmp=tmp[:,cropidx[0]:cropidx[1],:]
##scan['phasecorr'][k,kk,elem[0],elem[1],dyn]
##tmp[tmp==0]=1 #try to get rid of zeros in tmp
##print tmp
#for k in range(scan['scaninfo']['imsize'][0]):
# for kk in range(scan['scaninfo']['imsize'][2]):
# scan['phasecorr'][k,kk,elem[0],elem[1],elem[2]]=conj(tmp[k,:,kk]/abs(tmp[k,:,kk]))
try: #if it exists already, just don't run it again.
scan['phasecorr'][0,0,elem[0],elem[1],0]
#print 'phasecorr exists!'
except:
for dyn in range(scan['scaninfo']['ndynamics']):
#print scan['datainfo']['auxiliary_phase_data']['kspace']
#if scan['scaninfo']['ndynamics']==scan['datainfo']['auxiliary_phase_data']['scaninfo']['ndynamics']:
# tmp=fftshift(ifft(fftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem],0),None,0),0)
# tmp=tmp[:,cropidx[0]:cropidx[1],:]
#else: #if we don't have the same number of external phase angles as acquired angles
#print elem
angle=180*dyn/float(scan['scaninfo']['ndynamics'])
dists=abs(correction_angles-angle)
dists_sort=sort(dists)[:2]
idxs=nonzero(dists<=dists_sort[1])[0][:2]
dists_use=dists[idxs]
#print('angle=',angle)
#print('dists=',dists)
#print('dists_sort=',dists_sort)
#print sort(dists)
#print('idxs=',idxs)
#print('dists_use=',dists_use)
dists_use/=sum(dists_use)
dists_use=-1*(dists_use-1)
#print('dists_use=',dists_use)
tmp1=ifftshift(ifft(ifftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem[0],elem[1],idxs[0],0,0,0],1),None,1),1)
#tmp1=gaussian_filter1d(tmp1.real,5,1)+1j*gaussian_filter1d(tmp1.imag,5,1)
tmp2=ifftshift(ifft(ifftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem[0],elem[1],idxs[1],0,0,0],1),None,1),1)
#tmp2=gaussian_filter1d(tmp2.real,5,1)+1j*gaussian_filter1d(tmp2.imag,5,1)
tmp=dists_use[0]*tmp1+dists_use[1]*tmp2
#print shape(tmp)
tmp=tmp[:,cropidx[0]:cropidx[1],:] #crop the readout FOV
#print shape(tmp)
#scan['phasecorr'][k,kk,elem[0],elem[1],dyn]
#tmp[tmp==0]=1 #try to get rid of zeros in tmp
#print tmp
#phase correction insanity!
mid=scan['scaninfo']['imsize'][0]/2-(scan['scaninfo']['imsize'][0]/2)%2
#mid=0
for k in range(scan['scaninfo']['imsize'][0]):
for kk in range(scan['scaninfo']['imsize'][2]):
#scan['phasecorr'][k,kk,elem[0],elem[1],dyn]=conj(tmp[k,:,kk])/abs(tmp[k,:,kk]) #was k instead of mid
#using the middle lines really helps when using field map corrections!
#I think the phase gets modified with the field map as well, so in some ways it gets over-compensated
if k%2==0: #I am only using the central correciton data, why is this such an issue?
scan['phasecorr'][k,kk,elem[0],elem[1],dyn]=conj(tmp[mid,:,kk])/abs(tmp[mid,:,kk]) #was k instead of mid
else: #TODO: fix this issue!
scan['phasecorr'][k,kk,elem[0],elem[1],dyn]=conj(tmp[mid-1,:,kk])/abs(tmp[mid-1,:,kk]) #was k instead of mid (trying mid-1)
# angle=180*dyn/float(scan['scaninfo']['ndynamics'])
# dists=abs(correction_angles-angle)
# dists_sort=sort(dists)[:2]
# idxs=nonzero(dists<=dists_sort[1])[0]
# print('angle=',angle)
# print('dists=',dists)
# print('dists_sort=',dists_sort)
# #print sort(dists)
# print('idxs=',idxs)
# tmp1=fftshift(ifft(fftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem[0],elem[1],idxs[0],0,0,0],1),None,1),1)
# tmp1=gaussian_filter1d(tmp1.real,5,1)+1j*gaussian_filter1d(tmp1.imag,5,1)
# tmp2=fftshift(ifft(fftshift(scan['datainfo']['auxiliary_phase_data']['kspace'][elem[0],elem[1],idxs[1],0,0,0],1),None,1),1)
# tmp2=gaussian_filter1d(tmp2.real,5,1)+1j*gaussian_filter1d(tmp2.imag,5,1)
# tmp=(dists_sort[0]*tmp1+dists_sort[1]*tmp2)/sum(dists_sort)
# tmp=tmp[:,cropidx[0]:cropidx[1],:]
# #scan['phasecorr'][k,kk,elem[0],elem[1],dyn]
# #tmp[tmp==0]=1 #try to get rid of zeros in tmp
# #print tmp
# for k in range(scan['scaninfo']['imsize'][0]):
# for kk in range(scan['scaninfo']['imsize'][2]):
# scan['phasecorr'][k,kk,elem[0],elem[1],dyn]=conj(tmp[k,:,kk]/abs(tmp[k,:,kk]))
#print [0,0,elem[0],elem[1],dyn]
for elem in scan['frequencycorr_orig']: #lets just assume we have to rescale the frequency!
#print elem
scan['frequencycorr'][elem]=interp(x,xp,scan['frequencycorr_orig'][elem].real) #it is real only
freqcorr_warning=0
else: #if we are using internally collected phase data
if scan['scaninfo']['imsize']!=(scan['scaninfo']['X_range'][1]-scan['scaninfo']['X_range'][0]+1):
print 'warning, interpolating phase correction!'
for elem in scan['phasecorr_orig']:
#print elem
#scan['phasecorr'][0,elem[1],elem[2],elem[3],elem[4]]=exp(-1j*interp(x,xp,angle(scan['phasecorr_orig'][elem])))
scan['phasecorr'][0,elem[1],elem[2],elem[3],elem[4]]=interp(x,xp,scan['phasecorr_orig'][elem].real)-1j*interp(x,xp,scan['phasecorr_orig'][elem].imag)
#print max(abs(scan['phasecorr'][elem]))
#print min(abs(scan['phasecorr'][elem]))
#plot(angle(scan['phasecorr'][elem]))
#plot(angle(scan['phasecorr_orig'][elem]))
#show()
for elem in scan['frequencycorr_orig']:
scan['frequencycorr'][elem]=interp(x,xp,scan['frequencycorr_orig'][elem].real) #it is real only
freqcorr_warning=0
#scan['phasecorr'][0,elem[1],elem[2],elem[3],elem[4]]=interp(x,xp,scan['phasecorr_orig'][elem].real)+1j*interp(x,xp,angle(scan['phasecorr_orig'][elem].imag))
else:
for elem in scan['phasecorr_orig']:
scan['phasecorr'][0,elem[1],elem[2],elem[3],elem[4]]=scan['phasecorr_orig'][elem]
scan['frequencycorr'][elem]=scan['frequencycorr_orig'][elem]
if freqcorr_warning==1: #why is this even happening?
print('No frequency correction data available!')
maxmatrix=max(imsize[0],imsize[1])
scan['scaninfo']['imsize_square']=[maxmatrix,maxmatrix,imsize[2]]
#scan['scaninfo']['imsize_square']=[int(imsize[0]*float(imsize[1])/float(imsize[0])),imsize[1],imsize[2]]
#print scan['scaninfo']['imsize_square']
scan['scaninfo']['recon_nimages']=0
#for kspace addition
#offset=[scan['scaninfo']['imsize_square'][0]/2-scan['scaninfo']['imsize'][0]/2,scan['scaninfo']['imsize_square'][0]/2-scan['scaninfo']['imsize'][0]/2+scan['scaninfo']['imsize'][0]]
#print offset
#offset[:]=offset[:]+1
#print offset
#offset[1]=offset[0]+scan['scaninfo']['imsize'][0]
#blade[:,offset[0]:offset[1]]=1
#scan['blade']=blade
scan['dcomp_tmp']={}
#blade phase correction (NOT EPI phase correction!!!)
x,y=meshgrid(linspace(-1,1,scan['scaninfo']['imsize'][1],False),linspace(-1,1,scan['scaninfo']['imsize'][2],False))
#w_consistency=max(abs(y.flatten()))-abs(y.transpose().copy())
dist=pow(pow(x,2)+pow(y,2),0.5)
dist=max(dist.flatten())-dist
dist=tile(atleast_3d(dist),[1,1,scan['scaninfo']['imsize'][2]])
for element in scan['kspace']:
#for k in range(scan['scaninfo']['ksize'][0]):
# for kk in range(scan['scaninfo']['ksize'][2]):
# scan['kspace'][element][k,:,kk]=scan['kspace'][element][k,:,kk]*exp(-1j*linspace(-2*pi,2*pi,scan['scaninfo']['ksize'][1],False))
tmp=ifftshift(ifft(ifftshift(scan['kspace'][element],1),None,1),1)
scan['img'][element]=copy(tmp[:,cropidx[0]:cropidx[1],:])
#print shape(scan['img'][element])
if phasecorr==1:
for k in range(scan['scaninfo']['imsize'][0]-scan['scaninfo']['ky_range'][1]+scan['scaninfo']['ky_range'][0]-1,scan['scaninfo']['imsize'][0]):
for kk in range(scan['scaninfo']['imsize'][2]):
#print scan['scaninfo']['imsize'][0]-scan['scaninfo']['ky_range'][1]+scan['scaninfo']['ky_range'][0]
#print scan['scaninfo']['imsize'][0]
#print k
#print kk
rodir=scan['scaninfo']['readout_direction'][element][k,kk]
#rodir=-2*(k%2)+1
#rodir=2*(k%2)-1
#print type(scan['img'][element][k,:,kk])
#print type(scan['phasecorr'][elem[0],0,element[0],element[1],rodir])
#print shape(scan['img'][element][k,:,kk]*scan['phasecorr'][elem[0],0,element[0],element[1],rodir])
#print [elem[0],0,element[0],element[1],rodir]
#print 'phase'
#scan['phasecorr'][elem[0],0,element[0],element[1],rodir]
#print 'freq'
#scan['frequencycorr'][elem[0],0,element[0],element[1],rodir]
#for key in scan['frequencycorr']:
# print key
#for key in scan['phasecorr']:
# print key
#for key in scan['frequencycorr']:
# print key
#try:
# scan['frequencycorr'][elem[0],0,element[0],element[1],rodir]
#except KeyError:
# print('no frequency correction!')
#print k
#print elem
if scan['scaninfo']['auxiliary_phase_data_filename']!='':
#if freqcorr_warning==0:
try:
#print [k,kk,element[0],element[1],element[2]]
#scan['phasecorr'][k,kk,element[0],element[1],element[2]]
#scan['frequencycorr'][elem[0],0,element[0],element[1],rodir]
scan['img'][element][k,:,kk]=scan['img'][element][k,:,kk]*scan['phasecorr'][k,kk,element[0],element[1],element[2]]/scan['frequencycorr'][elem[0],0,element[0],element[1],rodir]
except:
scan['img'][element][k,:,kk]=scan['img'][element][k,:,kk]*scan['phasecorr'][k,kk,element[0],element[1],element[2]]
else:
#if freqcorr_warning==0:
try:
#print [0,0,element[0],element[1],rodir]
#scan['phasecorr'][0,0,element[0],element[1],rodir]
#scan['frequencycorr'][elem[0],0,element[0],element[1],rodir]
scan['img'][element][k,:,kk]=scan['img'][element][k,:,kk]*scan['phasecorr'][0,0,element[0],element[1],rodir]/scan['frequencycorr'][elem[0],0,element[0],element[1],rodir]
except:
#print('no frequency correction data available!')
scan['img'][element][k,:,kk]=scan['img'][element][k,:,kk]*scan['phasecorr'][0,0,element[0],element[1],rodir]
#imaging: k[5],k[4],k[1],k[11],k[6],k[7]
#phase: [k[8],k[9],k[5],k[4],k[12]]
#scan['img'][element]=fftshift(fft(fftshift(scan['img'][element],1),None,1),1)
#scan['img'][element]=fftshift(ifft(fftshift(scan['img'][element],1),None,1),1)
scan['img'][element]=ifftshift(ifft(ifftshift(scan['img'][element],0),None,0),0)
#scan['img'][element]=fftshift(ifft(fftshift(scan['img'][element],1),None,1),1)
#scan['img'][element]=fftshift(fft(fftshift(scan['img'][element],1),None,1),1)
#blade phase correction (NOT EPI phase correction!!!)
img_phcorr=copy(fftshift(fftn(fftshift(scan['img'][element],axes=(0,1)),axes=(0,1)),axes=(0,1)))
#print(shape(img_phcorr))
img_phcorr*=dist
img_phcorr=ifftshift(ifftn(ifftshift(img_phcorr,axes=(0,1)),axes=(0,1)),axes=(0,1))
img_phcorr/=abs(img_phcorr)
scan['img'][element]=scan['img'][element]*img_phcorr.conj()
return scan
def philips_dic_recon_basic(scan,coilcombinealgorithm='sum',sumdynamics=1,phasecorr=1,sumaverages=1):
from numpy import zeros, interp, linspace, exp, pi, linspace, ones, conj, sort, array, nonzero
from numpy.fft import fftshift, ifft, ifftn, fftn
from copy import copy
from scipy.ndimage.interpolation import zoom, rotate
from scipy.ndimage.filters import gaussian_filter1d
#from nfft_wrappers import init_nfft_2d, init_iterative_nfft_2d, grid_nfft_2d, finalize_nfft_2d, finalize_iterative_nfft_2d
#from nfft_helpers import propeller_sampling_location_generator, calc_density_weights
#from matplotlib.pyplot import plot, show
scan['img']={}
scan['img_square']={}
scan['phasecorr']={}
scan['frequencycorr']={}
scan['scaninfo']['image_counter']={}
densitycomp={}
imsize=copy(scan['scaninfo']['ksize'])
imsize[1]=int(imsize[1]/scan['scaninfo']['kx_oversample_factor'])
scan['scaninfo']['imsize']=copy(imsize)
#cropidx=[scan['scaninfo']['ksize'][1]/2-imsize[1]/2,scan['scaninfo']['ksize'][1]/2+imsize[1]/2]
densctr={}
#print cropidx
#print imsize
#print scan['scaninfo']['kx_oversample_factor']
#print shape(x)
#print shape(xp)
#print scan['scaninfo']['X_range']
scan=phase_correct_philips(scan,phasecorr)
offset=[scan['scaninfo']['imsize_square'][0]/2+scan['scaninfo']['ky_range'][0],scan['scaninfo']['imsize_square'][0]/2+scan['scaninfo']['ky_range'][1]]
blade=zeros(scan['scaninfo']['imsize_square'][0:2])
blade[offset[0]:offset[1],:]=1
for element in scan['kspace']:
#print shape(scan['img'][element])
#zoom(scan['img'][element].imag,[float(imsize[1])/float(imsize[0]),1,1])
#print element
#print element_square
#
element_square=element
if sumdynamics==1:
element_square=(element_square[0],element_square[1],0,element_square[3],element_square[4],element_square[5])
#else:
#element_square=element
#print coilcombinealgorithm
#if coilcombinealgorithm=='none':
#print 'coilcombine none!'
#really just a null operation!!
#element_square=(element_square[0],element_square[1],element_square[2],element_square[3],element_square[4],element_square[5])
if coilcombinealgorithm!='none':
element_square=(0,element_square[1],element_square[2],element_square[3],element_square[4],element_square[5])
if sumaverages==1:
element_square=(element_square[0],element_square[1],element_square[2],0,element_square[4],element_square[5])
#print element_square
try:
scan['img_square'][element_square][0,0,0]
#just see if it exists
except KeyError:
#print 'key error!'
#print element_square
#print scan['scaninfo']['imsize_square']
scan['scaninfo']['recon_nimages']+=1
scan['img_square'][element_square]=zeros(scan['scaninfo']['imsize_square'],complex)
scan['dcomp_tmp'][element_square]=zeros(scan['scaninfo']['imsize_square'],complex)
scan['scaninfo']['image_counter'][element_square]=1
densitycomp[element_square]=zeros(scan['scaninfo']['imsize_square'])