Skip to content

Commit 12c00aa

Browse files
committed
Extract function to get row info and add test
1 parent a3beffc commit 12c00aa

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

bin/heudiconv

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ __version__ = '0.2'
2323
import argparse
2424
from glob import glob
2525
import csv
26+
import dicom as dcm
27+
import dcmstack as ds
2628
import inspect
2729
import json
2830
import os
@@ -944,27 +946,31 @@ def convert(items, symlink=True, converter=None,
944946
shutil.rmtree(tmpdir)
945947

946948

949+
def get_formatted_scans_key_row(item, outname_bids_files):
950+
dcm_fn = item[-1][0]
951+
mw = ds.wrapper_from_data(dcm.read_file(dcm_fn, stop_before_pixels=True))
952+
# we need to store filenames and acquisition times
953+
# parse date and time and get it into isoformat
954+
date = mw.dcm_data.AcquisitionDate
955+
time = mw.dcm_data.AcquisitionTime.split('.')[0]
956+
td = time + date
957+
acq_time = datetime.strptime(td, '%H%M%S%Y%m%d').isoformat()
958+
# add random string
959+
randstr = ''.join(map(chr, sample(k=8, population=range(33, 127))))
960+
row = [acq_time, mw.dcm_data.PerformingPhysicianName, randstr]
961+
return row
962+
963+
947964
def save_scans_key(items, outname_bids_files):
948-
import dicom as dcm
949-
import dcmstack as ds
950965
header = ['filename', 'acq_time', 'operator', 'randstr']
951966
rows = []
967+
import pdb; pdb.set_trace()
952968
for item, outname_bids_file in zip(items, outname_bids_files):
953-
dcm_fn = item[-1][0]
954-
mw = ds.wrapper_from_data(dcm.read_file(dcm_fn, force=True))
955-
# we need to store filenames and acquisition times
956-
# parse date and time and get it into isoformat
957-
date = mw.dcm_data.AcquisitionDate
958-
time = mw.dcm_data.AcquisitionTime.split('.')[0]
959-
td = time + date
960-
acq_time = datetime.strptime(td, '%H%M%S%Y%m%d').isoformat()
961969
# get filenames
962970
f_name = '/'.join(outname_bids_file.split('/')[-2:])
963971
f_name = f_name.replace('json', 'nii.gz')
964-
# store it
965-
randstr = ''.join(map(chr, sample(k=8, population=range(33, 127))))
966972
rows.append(
967-
(f_name, acq_time, mw.dcm_data.PerformingPhysicianName, randstr)
973+
[f_name] + get_formatted_scans_key_row(item, outname_bids_file)
968974
)
969975
# where should we store it?
970976
output_dir = dirname(dirname(outname_bids_file))

tests/test_main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,29 @@ def test_prepare_for_datalad(tmpdir):
9797
assert not ds.repo.is_under_annex(f)
9898
assert not ds.repo.is_under_annex('.gitattributes')
9999

100+
100101
def test_json_dumps_pretty():
101102
pretty = heudiconv.json_dumps_pretty
102103
assert pretty({}) == "{}"
103104
assert pretty({"a": -1, "b": "123", "c": [1, 2, 3], "d": ["1.0", "2.0"]}) \
104105
== '{\n "a": -1,\n "b": "123",\n "c": [1, 2, 3],\n "d": ["1.0", "2.0"]\n}'
105106
assert pretty({'a': ["0.3", "-1.9128906358217845e-12", "0.2"]}) \
106107
== '{\n "a": ["0.3", "-1.9128906358217845e-12", "0.2"]\n}'
108+
109+
110+
def test_get_formatted_scans_key_row():
111+
item = [
112+
('tests/data/01-fmap_acq-3mm/1.3.12.2.1107.5.2.43.66112.2016101409263663466202201.dcm',
113+
('nii.gz', 'dicom'),
114+
['tests/data/01-fmap_acq-3mm/1.3.12.2.1107.5.2.43.66112.2016101409263663466202201.dcm'])
115+
]
116+
outname_bids_file = '/a/path/Halchenko/Yarik/950_bids_test4/sub-phantom1sid1/fmap/sub-phantom1sid1_acq-3mm_phasediff.json'
117+
118+
row = heudiconv.get_formatted_scans_key_row(item, outname_bids_file)
119+
assert(len(row) == 3)
120+
assert(row[0] == '2016-10-14T09:26:34')
121+
assert(row[1] == '')
122+
randstr1 = row[2]
123+
row = heudiconv.get_formatted_scans_key_row(item, outname_bids_file)
124+
randstr2 = row[2]
125+
assert(randstr1 != randstr2)

0 commit comments

Comments
 (0)