forked from neuropoly/mp2rage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computation.py
1178 lines (1047 loc) · 41.8 KB
/
computation.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pymrt.computation: generic computation utilities for MRI data analysis.
See Also:
pymrt.recipes
"""
# ======================================================================
# :: Future Imports
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# todo: use kwargs instead of opts
# todo: get rid of tty colorify
# ======================================================================
# :: Python Standard Library Imports
import os # Miscellaneous operating system interfaces
import shutil # High-level file operations
# import math # Mathematical functions
# import time # Time access and conversions
# import datetime # Basic date and time types
# import operator # Standard operators as functions
# import collections # High-performance container datatypes
import itertools # Functions creating iterators for efficient looping
# import functools # Higher-order functions and operations on callable objects
import re # Regular expression operations
# import subprocess # Subprocess management
import multiprocessing # Process-based parallelism
# import inspect # Inspect live objects
# import csv # CSV File Reading and Writing [CSV: Comma-Separated Values]
import json # JSON encoder and decoder [JSON: JavaScript Object Notation]
import hashlib # Secure hashes and message digests
# :: External Imports
import numpy as np # NumPy (multidimensional numerical arrays library)
# import matplotlib as mpl # Matplotlib (2D/3D plotting library)
# import sympy as sym # SymPy (symbolic CAS library)
# import PIL # Python Image Library (image manipulation toolkit)
# import SimpleITK as sitk # Image ToolKit Wrapper
# import nibabel as nib # NiBabel (NeuroImaging I/O Library)
# import nipy # NiPy (NeuroImaging in Python)
# import nipype # NiPype (NiPy Pipelines and Interfaces)
# :: External Imports Submodules
# import matplotlib.pyplot as plt # Matplotlib's pyplot: MATLAB-like syntax
# import scipy.optimize # SciPy: Optimization Algorithms
# import scipy.integrate # SciPy: Integrations facilities
# import scipy.constants # SciPy: Mathematal and Physical Constants
# import scipy.stats # SciPy: Statistical functions
# :: Local Imports
import pymrt.utils as pmu
import pymrt.naming as pmn
import pymrt.input_output as pmio
# from dcmpi.lib.common import ID
# from pymrt import INFO
from pymrt import VERB_LVL, D_VERB_LVL
from pymrt import msg, dbg
# ======================================================================
META_EXT = 'info' # ID['info']
D_OPTS = {
# sources
'data_ext': pmu.EXT['niz'],
'meta_ext': META_EXT,
'multi_acq': False,
'use_meta': True,
'param_select': [None],
'match': None,
'pattern': [None],
'groups': None,
# compute
'types': [None],
'mask': [None],
'adapt_mask': True,
}
DICOM_INTERVAL = (0, 4095)
# ======================================================================
def _simple_affines(affines):
return tuple(affines[0] for affine in affines)
# ======================================================================
def preset_t1_mp2rage_builtin():
"""
Preset to get built-in T1 maps from the MP2RAGE sequence.
"""
new_opts = {
'types': ['T1', 'INV2M'],
'param_select': ['ProtocolName', '_series'],
'match': '(?i).*mp2rage.*',
'dtype': 'float',
'mask': [[None], [None], [None], [1]],
}
new_opts.update({
'compute_func': 'match_series',
'compute_kwargs': {
'matches': (
('.*_T1_Images.*', new_opts['types'][0]),
('.*_INV2(?!_PHS).*', new_opts['types'][1]),
),
}
})
return new_opts
# ======================================================================
def preset_t2s_memp2rage_loglin2():
"""
Preset to get built-in T2* maps from the ME-MP2RAGE sequence.
"""
new_opts = {
'types': ['T2S', 'T1w'],
'param_select': ['ProtocolName', 'EchoTime::ms', '_series'],
'match': '(?i).*me-mp2rage.*_INV2(?!_PHS).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'fit_monoexp_decay_loglin2',
'compute_kwargs': {
'ti_label': 'EchoTime::ms',
'img_types': {'tau': 'T2S', 's_0': 'T1w'}}
}
return new_opts
# ======================================================================
def preset_t2s_flash_loglin2():
"""
Preset to get T2* maps from multi-echo data using a log-linear fit.
"""
new_opts = {
'types': ['T2S', 'T1w'],
'param_select': ['ProtocolName', 'EchoTime::ms', '_series'],
'match': '(?i).*(gre|flash).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'fit_monoexp_decay_loglin',
'compute_kwargs': {
'ti_label': 'EchoTime::ms',
'img_types': {'tau': 'T2S', 's_0': 'T1w'}}
}
return new_opts
# ======================================================================
def preset_t2s_flash_builtin():
"""
Preset to get built-in T2* maps from the FLASH sequence.
"""
new_opts = {
'types': ['T2S', 'T1w'],
'param_select': ['ProtocolName', '_series'],
'match': '.*T2Star_Images.*',
'dtype': 'float',
}
return new_opts
# ======================================================================
def preset_t2s_multiecho_loglin():
"""
Preset to get T2* maps from multi-echo squared data using a log-linear fit.
"""
new_opts = {
'types': ['T2S', 'T1w'],
'param_select': ['ProtocolName', 'EchoTime::ms', '_series'],
'match': '(?i).*(gre|flash|me).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'fit_monoexp_decay_loglin2',
'compute_kwargs': {
'ti_label': 'EchoTime::ms',
'img_types': {'tau': 'T2S', 's_0': 'T1w'}}
}
return new_opts
# ======================================================================
def preset_t2s_multiecho_loglin2():
"""
Preset to get T2* maps from multi-echo squared data using a log-linear fit.
"""
new_opts = {
'types': ['T2S', 'T1w'],
'param_select': ['ProtocolName', 'EchoTime::ms', '_series'],
'match': '(?i).*(gre|flash|me).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'fit_monoexp_decay_loglin2',
'compute_kwargs': {
'ti_label': 'EchoTime::ms',
'img_types': {'tau': 'T2S', 's_0': 'T1w'}}
}
return new_opts
# ======================================================================
def preset_t2s_multiecho_leasq():
"""
Preset to get T2* maps from multi-echo data using a least-squares fit.
"""
new_opts = {
'types': ['T2S', 'T1w'],
'param_select': ['ProtocolName', 'EchoTime::ms', '_series'],
'match': '.*(FLASH|ME-MP2RAGE).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'fit_monoexp_decay_leasq',
'compute_kwargs': {
'ti_label': 'EchoTime::ms',
'img_types': {'tau': 'T2S', 's_0': 'T1w'}}
}
return new_opts
# ======================================================================
def preset_b1t_afi():
"""
Preset to get B1+ maps from the AFI sequence.
"""
new_opts = {
'types': ['B1T'],
'param_select': [
'ProtocolName', 'RepetitionTime::ms', 'FlipAngle::deg',
'_series'],
'match': '.*(afi|b1).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'calc_afi',
'compute_kwargs': {
'ti_label': 'RepetitionTime::ms',
'fa_label': 'FlipAngle::deg',
'img_types': {'eff': 'B1T'}}
}
return new_opts
# ======================================================================
def preset_qsm_as_legacy():
"""
Preset to get CHI maps from a multi-echo sequence.
"""
new_opts = {
'types': ['CHI', 'MSK'],
'param_select': [
'ProtocolName', 'EchoTime::ms', 'ImagingFrequency', '_series'],
# 'match': '.*((FLASH)|(ME-MP2RAGE.*INV2)).*',
'match': '.*(ME-MP2RAGE.*INV2).*',
'dtype': 'float',
'multi_acq': False,
'compute_func': 'ext_qsm_as_legacy',
'compute_kwargs': {
'te_label': 'EchoTime::ms',
'img_types': {'qsm': 'CHI', 'mask': 'MSK'}}
}
return new_opts
# ======================================================================
def ext_qsm_as_legacy(
images,
affines,
params,
te_label,
# b0_label,
# th_label,
img_types):
"""
Args:
images ():
affines ():
params ():
te_label ():
img_types ():
Returns:
"""
# determine correct TE
max_te = 25.0 # ms
selected = len(params[te_label])
for i, te in enumerate(params[te_label]):
if te < max_te:
selected = i
tmp_dirpath = '/tmp/{}'.format(hashlib.md5(str(params)).hexdigest())
if not os.path.isdir(tmp_dirpath):
os.makedirs(tmp_dirpath)
tmp_filenames = ('magnitude.nii.gz', 'phase.nii.gz',
'qsm.nii.gz', 'mask.nii.gz')
tmp_filepaths = tuple(os.path.join(tmp_dirpath, tmp_filename)
for tmp_filename in tmp_filenames)
# export temp input
if len(images) > 2:
images = images[-2:]
affines = affines[-2:]
for image, affine, tmp_filepath in zip(images, affines, tmp_filepaths):
pmio.save(tmp_filepath, image[..., selected], affine)
# execute script on temp input
cmd = [
'qsm_as_legacy.py',
'--magnitude_input', tmp_filepaths[0],
'--phase_input', tmp_filepaths[1],
'--qsm_output', tmp_filepaths[2],
'--mask_output', tmp_filepaths[3],
'--echo_time', str(params[te_label][selected]),
# '--field_strength', str(params[b0_label][selected]),
# '--angles', str(params[th_label][selected]),
'--units', 'ppb']
pmu.execute(str(' '.join(cmd)))
# import temp output
img_list, aff_list = [], []
for tmp_filepath in tmp_filepaths[2:]:
img, aff, hdr = pmio.load(tmp_filepath, full=True)
img_list.append(img)
aff_list.append(aff)
# clean up tmp files
if os.path.isdir(tmp_dirpath):
shutil.rmtree(tmp_dirpath)
# prepare output
type_list = ('qsm', 'mask')
params_list = ({'te': params[te_label][selected]}, {})
img_type_list = tuple(img_types[key] for key in type_list)
return img_list, aff_list, img_type_list, params_list
# ======================================================================
def qsm_sdi(
images,
affines,
params,
img_types):
pass
# ======================================================================
def calc_afi(
images,
affines,
params,
ti_label,
fa_label,
img_types):
"""
Fit monoexponential decay to images using the log-linear method.
"""
y_arr = np.stack(images, -1).astype(float)
s_arr = pmu.polar2complex(y_arr[..., 0], fix_phase_interval(y_arr[..., 1]))
# s_arr = images[0]
t_r = params[ti_label]
nominal_fa = params[fa_label]
mask = s_arr[..., 0] != 0.0
r = np.zeros_like(s_arr[..., 1])
r[mask] = s_arr[..., 0][mask] / s_arr[..., 1][mask]
n = t_r[1] / t_r[0] # usually: t_r[1] > t_r[0]
fa = np.rad2deg(np.real(np.arccos((r * n - 1) / (n - r))))
img_list = [fa / nominal_fa]
aff_list = _simple_affines(affines)
type_list = ['eff']
img_type_list = tuple(img_types[key] for key in type_list)
params_list = ({},) * len(img_list)
return img_list, aff_list, img_type_list, params_list
# ======================================================================
def time_to_rate(
array,
in_units='ms',
out_units='Hz'):
k = 1.0
if in_units == 'ms':
k *= 1.0e3
if out_units == 'kHz':
k *= 1.0e-3
array[array != 0.0] = k / array[array != 0.0]
return array
# ======================================================================
def rate_to_time(
array,
in_units='Hz',
out_units='ms'):
k = 1.0
if in_units == 'kHz':
k *= 1.0e3
if out_units == 'ms':
k *= 1.0e-3
array[array != 0.0] = k / array[array != 0.0]
return array
# ======================================================================
def fix_phase_interval(arr):
"""
Ensure that the range of values is interpreted as valid phase information.
This is useful for DICOM-converted images (without post-processing).
Args:
arr (np.ndarray): Array to be processed.
Returns:
array (np.ndarray): An array scaled to (-pi,pi).
Examples:
>>> fix_phase_interval(np.arange(8))
array([-3.14159265, -2.24399475, -1.34639685, -0.44879895, 0.44879895,
1.34639685, 2.24399475, 3.14159265])
>>> fix_phase_interval(np.array([-10, -5, 0, 5, 10]))
array([-3.14159265, -1.57079633, 0. , 1.57079633, 3.14159265])
>>> fix_phase_interval(np.array([-10, 10, 1, -3]))
array([-3.14159265, 3.14159265, 0.31415927, -0.9424778 ])
"""
# correct phase value range (useful for DICOM-converted images)
if np.ptp(arr) > 2.0 * np.pi:
arr = pmu.scale(arr.astype(float), (-np.pi, np.pi))
return arr
# ======================================================================
def func_exp_recovery(t_arr, tau, s_0, eff=1.0, const=0.0):
"""
s(t)= s_0 * (1 - 2 * eff * exp(-t/tau)) + const
[s_0 > 0, tau > 0, eff > 0]
"""
if s_0 > 0.0 and tau > 0.0 and eff > 0.0:
s_t_arr = s_0 * (1.0 - 2.0 * eff * np.exp(-t_arr / tau)) + const
else:
s_t_arr = np.tile(np.inf, len(t_arr))
return s_t_arr
# ======================================================================
def func_exp_decay(t_arr, tau, s_0, const=0.0):
"""
s(t)= s_0 * exp(-t/tau) + const
[s_0 > 0, tau > 0]
"""
s_t_arr = s_0 * np.exp(-t_arr / tau) + const
# if s_0 > 0.0 and tau > 0.0:
# s_t_arr = s_0 * np.exp(-t_arr / tau) + const
# else:
# s_t_arr = np.tile(np.inf, len((t_arr)))
return s_t_arr
# ======================================================================
def func_flash(m0, fa, tr, t1, te, t2s):
"""
The FLASH (a.k.a. GRE, TFL, SPGR) signal expression:
S = M0 sin(fa) exp(-TE/T2*) (1 - exp(-TR/T1)) / (1 - cos(fa) exp(-TR/T1))
"""
return m0 * np.sin(fa) * np.exp(-te / t2s) * \
(1.0 - np.exp(-tr / t1)) / (1.0 - np.cos(fa) * np.exp(-tr / t1))
# ======================================================================
def uniform_mp2rage(
inv1m_arr,
inv1p_arr,
inv2m_arr,
inv2p_arr,
regularization=np.spacing(1),
values_interval=None):
"""
Calculate the uniform image from an MP2RAGE acquisition.
Args:
inv1m_arr (float|np.ndarray): Magnitude of the first inversion image.
inv1p_arr (float|np.ndarray): Phase of the first inversion image.
inv2m_arr (float|np.ndarray): Magnitude of the second inversion image.
inv2p_arr (float|np.ndarray): Phase of the second inversion image.
regularization (float|int): Parameter for the regularization.
This parameter is added to the denominator of the signal expression
for normalization purposes, therefore should be much smaller than
the average of the magnitude images.
Larger values of this parameter will have the side effect of
denoising the background.
values_interval (tuple[float|int]|None): The output values interval.
The standard values are linearly converted to this range.
Returns:
rho_arr (float|np.ndarray): The calculated uniform image from MP2RAGE.
"""
if not regularization:
regularization = 0
inv1m_arr = inv1m_arr.astype(float)
inv2m_arr = inv2m_arr.astype(float)
inv1p_arr = fix_phase_interval(inv1p_arr)
inv2p_arr = fix_phase_interval(inv2p_arr)
inv1_arr = pmu.polar2complex(inv1m_arr, inv1p_arr)
inv2_arr = pmu.polar2complex(inv2m_arr, inv2p_arr)
rho_arr = np.real(inv1_arr.conj() * inv2_arr /
(inv1m_arr ** 2 + inv2m_arr ** 2 + regularization))
if values_interval:
print(values_interval, 'scaling')
rho_arr = scale(rho_arr, values_interval, (-0.5, 0.5))
return rho_arr
# ======================================================================
def t1_mp2rage(
inv1m_arr=None,
inv1p_arr=None,
inv2m_arr=None,
inv2p_arr=None,
rho_arr=None,
regularization=np.spacing(1),
eff_arr=None,
t1_value_range=(100, 5000),
t1_num=512,
eff_num=32,
**acq_param_kws):
"""
Calculate the T1 map from an MP2RAGE acquisition.
Args:
inv1m_arr (float|np.ndarray): Magnitude of the first inversion image.
inv1p_arr (float|np.ndarray): Phase of the first inversion image.
inv2m_arr (float|np.ndarray): Magnitude of the second inversion image.
inv2p_arr (float|np.ndarray): Phase of the second inversion image.
eff_arr (float|np.array|None): Efficiency of the RF pulse excitation.
This is equivalent to the normalized B1T field.
Note that this must have the same spatial dimensions as the images
acquired with MP2RAGE.
If None, no correction for the RF efficiency is performed.
t1_value_range (tuple[float]): The T1 value range to consider.
The format is (min, max) where min < max.
Values should be positive.
t1_num (int): The base number of sampling points of T1.
The actual number of sampling points is usually smaller, because of
the removal of non-bijective branches.
This affects the precision of the MP2RAGE estimation.
eff_num (int): The base number of sampling points for the RF efficiency.
This affects the precision of the RF efficiency correction.
**acq_param_kws (dict): The acquisition parameters.
This should match the signature of: `mp2rage.acq_to_seq_params`.
Returns:
t1_arr (float|np.ndarray): The calculated T1 map for MP2RAGE.
"""
from pymrt.sequences import mp2rage
import matplotlib.pyplot as plt
if eff_arr:
# todo: implement B1T correction
raise NotImplementedError('B1T correction is not yet implemented')
else:
# determine the signal expression
t1 = np.linspace(t1_value_range[0], t1_value_range[1], t1_num)
seq_param_kws = mp2rage.acq_to_seq_params(**acq_param_kws)[0]
rho = mp2rage.signal(t1, **seq_param_kws)
# plot T1 vs. RHO
plt.figure()
plt.plot(rho, t1)
plt.xlabel('RHO')
plt.ylabel('T1 (ms)')
plt.title('T1 vs. RHO')
plt.savefig('T1_vs_RHO.pdf', format='PDF', transparent=True)
# remove non-bijective branches
bijective_part = pmu.bijective_part(rho)
t1 = t1[bijective_part]
rho = rho[bijective_part]
if rho[0] > rho[-1]:
rho = rho[::-1]
t1 = t1[::-1]
# plot the bijective part of the graph
plt.figure()
plt.plot(rho, t1)
plt.xlabel('RHO')
plt.ylabel('T1 (ms)')
plt.title('T1 vs. RHO (bijective part only)')
plt.savefig('T1_vs_RHO_bij.pdf', format='PDF', transparent=True)
# check that rho values are strictly increasing
if not np.all(np.diff(rho) > 0):
raise ValueError('MP2RAGE look-up table was not properly prepared.')
if rho_arr == None:
rho_arr = uniform_mp2rage(inv1m_arr, inv1p_arr, inv2m_arr, inv2p_arr, regularization, values_interval=None)
else:
rho_arr = pmu.scale(rho_arr, (-0.5, 0.5), DICOM_INTERVAL)
print(np.min(rho_arr), np.max(rho_arr))
t1_arr = np.interp(rho_arr, rho, t1)
return t1_arr, rho_arr
# ======================================================================
def fit_monoexp_decay_leasq(
images,
affines,
params,
ti_label,
img_types):
"""
Fit monoexponential decay to images using the least-squares method.
"""
norm_factor = 1e4
y_arr = np.stack(images, -1).astype(float)
y_arr = y_arr[..., 0] # use only the modulus
y_arr = y_arr / np.max(y_arr) * norm_factor
x_arr = np.array(params[ti_label]).astype(float)
p_arr = voxel_curve_fit(
y_arr, x_arr, func_exp_decay,
(np.mean(x_arr), np.mean(y_arr)), method='curve_fit')
img_list = np.split(p_arr, 2, -1)
type_list = ('tau', 's_0')
img_type_list = tuple(img_types[key] for key in type_list)
aff_list = _simple_affines(affines)
params_list = ({},) * len(img_list)
return img_list, aff_list, img_type_list, params_list
# ======================================================================
def fit_monoexp_decay_loglin(
images,
affines,
params,
ti_label,
img_types):
"""
Fit monoexponential decay to images using the log-linear method.
"""
def prepare(arr, factor=0):
log_arr = np.zeros_like(arr)
# calculate logarithm only of strictly positive values
log_arr[arr > 0.0] = np.log(arr[arr > 0.0] * np.e ** factor)
return log_arr
def fix(arr, factor=0):
# tau = p_arr[..., 0]
# s_0 = p_arr[..., 1]
mask = arr[..., 0] != 0.0
arr[..., 0][mask] = - 1.0 / arr[..., 0][mask]
arr[..., 1] = np.exp(arr[..., 1] - factor)
return arr
exp_factor = 12 # 0: untouched, other values might improve results
y_arr = np.stack(images, -1).astype(float)
y_arr = y_arr[..., 0] # use only the modulus
x_arr = np.array(params[ti_label]).astype(float)
p_arr = voxel_curve_fit(
y_arr, x_arr,
None, (np.mean(x_arr), np.mean(y_arr)),
prepare, [exp_factor], {},
fix, [exp_factor], {},
method='poly')
img_list = np.split(p_arr, 2, -1)
aff_list = _simple_affines(affines)
type_list = ('tau', 's_0')
img_type_list = tuple(img_types[key] for key in type_list)
params_list = ({},) * len(img_list)
return img_list, aff_list, img_type_list, params_list
# ======================================================================
def fit_monoexp_decay_loglin2(
images,
affines,
params,
ti_label,
img_types):
"""
Fit monoexponential decay to squared images using the log-linear method.
"""
def prepare(arr, factor=0, noise=0):
log_arr = np.zeros_like(arr)
# calculate logarithm only of strictly positive values
arr -= noise
mask = arr > 0.0
log_arr[mask] = np.log(arr[mask] ** 2.0 * np.e ** factor)
return log_arr
def fix(arr, factor=0):
# tau = p_arr[..., 0]
# s_0 = p_arr[..., 1]
mask = arr[..., 0] != 0.0
arr[..., 0][mask] = - 2.0 / arr[..., 0][mask]
arr[..., 1] = np.exp(arr[..., 1] - factor)
return arr
exp_factor = 12 # 0: untouched, other values might improve results
y_arr = np.stack(images, -1).astype(float)
y_arr = y_arr[..., 0] # use only the modulus
x_arr = np.array(params[ti_label]).astype(float)
noise_level = np.percentile(y_arr, 3)
p_arr = voxel_curve_fit(
y_arr, x_arr,
None, (np.mean(x_arr), np.mean(y_arr)),
prepare, [exp_factor, noise_level], {},
fix, [exp_factor], {},
method='poly')
img_list = np.split(p_arr, 2, -1)
aff_list = _simple_affines(affines)
type_list = ('tau', 's_0')
img_type_list = tuple(img_types[key] for key in type_list)
params_list = ({},) * len(img_list)
return img_list, aff_list, img_type_list, params_list
# ======================================================================
def voxel_curve_fit(
y_arr,
x_arr,
fit_func=None,
fit_params=None,
pre_func=None,
pre_args=None,
pre_kwargs=None,
post_func=None,
post_args=None,
post_kwargs=None,
method='curve_fit'):
"""
Curve fitting for y = F(x, p)
Args:
y_arr (np.ndarray): Dependent variable with x dependence in the n-th dim
x_arr (np.ndarray): Independent variable with same size as n-th dim of y
fit_func (func):
fit_params (list[float]):
pre_func (func):
pre_args (list):
pre_kwargs (dict):
post_func (func):
post_args (list):
post_kwargs (dict):
method (str): Method to use for the curve fitting procedure.
Returns:
p_arr (np.ndarray) :
"""
# TODO: finish documentation
# y_arr : ndarray ???
# Dependent variable (x dependence in the n-th dimension).
# x_arr : ndarray ???
# Independent variable (same number of elements as the n-th dimension).
# reshape to linearize the independent dimensions of the array
support_axis = -1
shape = y_arr.shape
support_size = shape[support_axis]
y_arr = y_arr.reshape((-1, support_size))
num_voxels = y_arr.shape[0]
p_arr = np.zeros((num_voxels, len(fit_params)))
# preprocessing
if pre_func is not None:
if pre_args is None:
pre_args = []
if pre_kwargs is None:
pre_kwargs = {}
y_arr = pre_func(y_arr, *pre_args, **pre_kwargs)
if method == 'curve_fit':
iter_param_list = [
(fit_func, x_arr, y_i_arr, fit_params)
for y_i_arr in np.split(y_arr, support_size, 0)]
pool = multiprocessing.Pool(multiprocessing.cpu_count())
for i, (par_opt, par_cov) in \
enumerate(pool.imap(pmu.curve_fit, iter_param_list)):
p_arr[i] = par_opt
elif method == 'poly':
# polyfit requires to change matrix orientation using transpose
p_arr = np.polyfit(x_arr, y_arr.transpose(), len(fit_params) - 1)
# transpose the results back
p_arr = p_arr.transpose()
else:
try:
p_arr = fit_func(y_arr, x_arr, fit_params)
except Exception as ex:
print('WW: Exception "{}" in ndarray_fit() method "{}"'.format(
ex, method))
# revert to original shape
p_arr = p_arr.reshape(list(shape[:support_axis]) + [len(fit_params)])
# post process
if post_func is not None:
if post_args is None:
post_args = []
if post_kwargs is None:
post_kwargs = {}
p_arr = post_func(p_arr, *post_args, **post_kwargs)
return p_arr
# ======================================================================
def match_series(images, affines, params, matches):
"""
TODO: finish documentation
"""
img_list, aff_list, img_type_list = [], [], []
for match, img_type in matches:
for i, series in enumerate(params['_series']):
if re.match(match, series):
# print(match, series, img_type, images[i].shape) # DEBUG
img_list.append(images[i])
aff_list.append(affines[i])
img_type_list.append(img_type)
break
params_list = ({},) * len(img_list)
return img_list, aff_list, img_type_list, params_list
# ======================================================================
def sources_generic(
data_dirpath,
meta_dirpath=None,
opts=None,
force=False,
verbose=D_VERB_LVL):
"""
Get source files (both data and metadata) from specified directories
Args:
data_dirpath (str): Directory containing data files
meta_dirpath (str|None): Directory containing metadata files
opts (dict):
Accepted options:
- data_ext (str): File extension of the data files
- meta_ext (str): File extension of the metadata files
- multi_acq (bool): Use multiple acquisitions for computation
- use_meta (bool): Use metadata, instead of filenames, to get
parameters
- param_select (list[str]): Parameters to select from metadata
- match (str): regular expression used to select data filenames
- pattern (tuple[int]): Slicing applied to data list
- groups (list[int]|None): Split results into groups
(cyclically)
force (bool): Force calculation of output
verbose (int): Set level of verbosity.
Returns:
sources_list (list[list[str]]): List of lists of filenames to be used
for computation
params_list : (list[list[str|float|int]]): List of lists of parameters
associated with the specified sources
See Also:
pymrt.computation.compute_generic,
pymrt.computation.compute,
pymrt.computation.D_OPTS
"""
sources_list = []
params_list = []
opts = pmu.merge_dicts(D_OPTS, opts)
if verbose >= VERB_LVL['medium']:
print('Opts:\t{}'.format(json.dumps(opts)))
if os.path.isdir(data_dirpath):
pattern = slice(*opts['pattern'])
sources, params = [], {}
last_acq, new_acq = None, None
data_filepath_list = pmu.listdir(
data_dirpath, opts['data_ext'], pattern)
for data_filepath in data_filepath_list:
info = pmn.parse_filename(
pmu.change_ext(pmu.os.path.basename(data_filepath), '',
pmu.EXT['niz']))
if opts['use_meta']:
# import parameters from metadata
info['seq'] = None
series_meta_filepath = os.path.join(
meta_dirpath,
pmn.to_filename(info, ext=opts['meta_ext']))
if os.path.isfile(series_meta_filepath):
with open(series_meta_filepath, 'r') as meta_file:
series_meta = json.load(meta_file)
acq_meta_filepath = os.path.join(
meta_dirpath, series_meta['_acquisition'] +
pmu.add_extsep(opts['meta_ext']))
if os.path.isfile(acq_meta_filepath):
with open(acq_meta_filepath, 'r') as meta_file:
acq_meta = json.load(meta_file)
data_params = {}
if opts['param_select']:
for item in opts['param_select']:
data_params[item] = acq_meta[item] \
if item in acq_meta else None
else:
data_params = acq_meta
new_acq = (last_acq and acq_meta['_series'] != last_acq)
last_acq = acq_meta['_series']
else:
# import parameters from filename
base, data_params = pmn.parse_series_name(info['name'])
new_acq = (last_acq and base != last_acq)
last_acq = base
if not opts['multi_acq'] and new_acq and sources:
sources_list.append(sources)
params_list.append(params)
sources, params = [], {}
if not opts['match'] or \
re.match(opts['match'], os.path.basename(data_filepath)):
sources.append(data_filepath)
if opts['use_meta']:
params.update(data_params)
else:
for key, val in data_params.items():
params[key] = (params[key] if key in params else []) \
+ [val]
if sources:
sources_list.append(sources)
params_list.append(params)
if opts['groups']:
grouped_sources_list, grouped_params_list = [], []
grouped_sources, grouped_params = [], []
for sources, params in zip(sources_list, params_list):
grouping = list(opts['groups']) * \
int((len(sources) / sum(opts['groups'])) + 1)
seps = pmu.accumulate(grouping) if grouping else []
for i, source in enumerate(sources):
grouped_sources.append(source)
grouped_params.append(params)
if i + 1 in seps or i + 1 == len(sources):
grouped_sources_list.append(grouped_sources)
grouped_params_list.append(grouped_params)
grouped_sources, grouped_params = [], []
sources_list = grouped_sources_list
params_list = grouped_params_list
if verbose >= VERB_LVL['debug']:
for sources, params in zip(sources_list, params_list):
print(pmu.tty_colorify('DEBUG', 'r'))
print(sources, params)
elif verbose >= VERB_LVL['medium']:
print("WW: no data directory '{}'. Skipping.".format(data_dirpath))
return sources_list, params_list
# ======================================================================
def compute_generic(
sources,
out_dirpath,
params=None,
opts=None,
force=False,
verbose=D_VERB_LVL):
"""
Perform the specified computation on source files.
Args:
sources (list[str]): Directory containing data files.
out_dirpath (str): Directory containing metadata files.
params (dict): Parameters associated with the sources.
opts (dict):
Accepted options:
- types (list[str]): List of image types to use for results.
- mask: (tuple[tuple[int]): Slicing for each dimension.
- adapt_mask (bool): adapt over- or under-sized mask.
- dtype (str): data type to be used for the target images.
- compute_func (str): function used for the computation.
compute_func(images, params, compute_args, compute_kwargs)
-> img_list, img_type_list
- compute_args (list): additional positional parameters for
compute_func
- compute_kwargs (dict): additional keyword parameters for
compute_func
- affine_func (str): name of the function for affine
computation: affine_func(affines, affine_args...) -> affine
- affine_args (list): additional parameters for affine_func
force (bool): Force calculation of output
verbose (int): Set level of verbosity.
Returns:
targets ():
See Also:
pymrt.computation.sources_generic,
pymrt.computation.compute,
pymrt.computation.D_OPTS
"""
# TODO: implement affine_func, affine_args, affine_kwargs?