-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstormdb_functions.py
More file actions
71 lines (49 loc) · 1.84 KB
/
stormdb_functions.py
File metadata and controls
71 lines (49 loc) · 1.84 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
from pathlib import Path
import os
import numpy as np
from tempfile import TemporaryDirectory
from glob import glob
import dicom2nifti
import shutil
def convert_pwi(filtered_serie: dict, outfile: str, clobber: bool = False, path_prefix=None):
if path_prefix is None:
if str(Path('__file__').resolve()).startswith('/Volumes'):
path_prefix = '/Volumes'
else:
path_prefix = ''
if not os.path.isfile(outfile) or clobber:
with TemporaryDirectory() as tmp_dir:
# Convert image data
dicom2nifti.convert_directory(path_prefix + filtered_serie['path'], tmp_dir, compression=False)
# Rename output file
out_file_temp = glob(tmp_dir + '/*.nii')[0]
Path(os.path.dirname(outfile)).mkdir(parents=True, exist_ok=True)
shutil.move(out_file_temp, outfile)
else:
print(f'{outfile} exits. Use clobber to overwrite.')
# INFO
dcm_file = f'{path_prefix}/{filtered_serie["path"]}/{filtered_serie["files"][0]}'
info = get_info(dcm_file)
return info
def get_info(dcm_file):
import pydicom
ds = pydicom.dcmread(dcm_file)
info = {}
# --- Echo time ---
echoTime = ds.EchoTime
# --- Repetition time ---
repetitionTime = ds.RepetitionTime
# --- Aquisition times ---
for element in ds:
if "MosaicRefAcqTimes" in element.name:
acqtime = element.value
acqorder = np.argsort(acqtime)
uniq_acqtime = list(dict.fromkeys(acqtime))
_, uniq_acqorder = np.unique(uniq_acqtime, return_index=True)
for i, time in enumerate(uniq_acqtime):
acqorder[np.array(acqtime) == time] = uniq_acqorder[i]
# --- Return ---
info['TE'] = echoTime * 1e-3 # In seconds
info['TR'] = repetitionTime * 1e-3 # In seconds
info['acqorder'] = acqorder
return info