Skip to content

Commit 6b914c9

Browse files
authored
Merge pull request #283 from daeh/master
added decoding of bytes literal strings returned by subprocess during anonymization
2 parents 53dbf71 + 9aaf9ed commit 6b914c9

File tree

8 files changed

+41
-7
lines changed

8 files changed

+41
-7
lines changed

.gitignore

100644100755
File mode changed.

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ RUN export PATH="/opt/miniconda-latest/bin:$PATH" \
9494
'traits>=4.6.0' \
9595
'scipy' \
9696
'numpy' \
97+
'pandas' \
9798
'nomkl' \
9899
&& sync && conda clean -tipsy && sync \
99100
&& bash -c "source activate base \
@@ -146,6 +147,7 @@ RUN echo '{ \
146147
\n "traits>=4.6.0", \
147148
\n "scipy", \
148149
\n "numpy", \
150+
\n "pandas", \
149151
\n "nomkl" \
150152
\n ], \
151153
\n "pip_install": [ \

heudiconv/convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
100100
anon_outdir = outdir
101101

102102
# Generate heudiconv info folder
103-
idir = op.join(outdir, '.heudiconv', sid)
103+
idir = op.join(outdir, '.heudiconv', anon_sid)
104104
if bids and ses:
105105
idir = op.join(idir, 'ses-%s' % str(ses))
106106
if anon_outdir == outdir:

heudiconv/tests/anonymize_script.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env python
2+
3+
import sys
4+
import re
5+
import ctypes
6+
7+
8+
def bids_id_(sid):
9+
parsed_id = re.compile(r"^(?:sub-|)(.+)$").search(sid).group(1)
10+
return str(ctypes.c_size_t(hash(parsed_id)).value)
11+
12+
13+
def main():
14+
sid = sys.argv[1]
15+
return bids_id_(sid)
16+
17+
18+
if __name__ == '__main__':
19+
main()

heudiconv/tests/test_regression.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919

2020
@pytest.mark.parametrize('subject', ['sub-sid000143'])
2121
@pytest.mark.parametrize('heuristic', ['reproin.py'])
22+
@pytest.mark.parametrize('anon_cmd', [None, 'anonymize_script.py'])
2223
@pytest.mark.skipif(not have_datalad, reason="no datalad")
23-
def test_conversion(tmpdir, subject, heuristic):
24+
def test_conversion(tmpdir, subject, heuristic, anon_cmd):
2425
tmpdir.chdir()
2526
try:
2627
datadir = fetch_data(tmpdir.strpath, subject)
2728
except IncompleteResultsError as exc:
2829
pytest.skip("Failed to fetch test data: %s" % str(exc))
2930
outdir = tmpdir.mkdir('out').strpath
3031

31-
args = gen_heudiconv_args(datadir, outdir, subject, heuristic)
32+
args = gen_heudiconv_args(datadir, outdir, subject, heuristic, anon_cmd)
3233
runner(args) # run conversion
3334

3435
# verify functionals were converted

heudiconv/tests/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
TESTS_DATA_PATH = op.join(op.dirname(__file__), 'data')
66

77

8-
def gen_heudiconv_args(datadir, outdir, subject, heuristic_file, xargs=None):
8+
def gen_heudiconv_args(datadir, outdir, subject, heuristic_file, anon_cmd=None, xargs=None):
99
heuristic = op.realpath(op.join(HEURISTICS_PATH, heuristic_file))
1010
args = ["-d", op.join(datadir, 'sourcedata/{subject}/*/*/*.tgz'),
1111
"-c", "dcm2niix",
1212
"-o", outdir,
1313
"-s", subject,
1414
"-f", heuristic,
1515
"--bids",]
16+
if anon_cmd:
17+
args += ["--anon-cmd", op.join(op.dirname(__file__), anon_cmd), "-a", outdir]
1618
if xargs:
1719
args += xargs
1820

heudiconv/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,19 @@ def dec(obj):
103103

104104

105105
def anonymize_sid(sid, anon_sid_cmd):
106+
import sys
106107
from subprocess import check_output
108+
107109
cmd = [anon_sid_cmd, sid]
108-
return check_output(cmd).strip()
110+
shell_return = check_output(cmd)
111+
112+
### Handle subprocess returning a bytes literal string to a python3 interpreter
113+
if all([sys.version_info[0] > 2, isinstance(shell_return, bytes), isinstance(sid, str)]):
114+
anon_sid = shell_return.decode()
115+
else:
116+
anon_sid = shell_return
117+
118+
return anon_sid.strip()
109119

110120

111121
def create_file_if_missing(filename, content):
@@ -448,4 +458,4 @@ def create_tree(path, tree, archives_leading_dir=True):
448458
load = load.encode('utf-8')
449459
f.write(load)
450460
if executable:
451-
os.chmod(full_name, os.stat(full_name).st_mode | stat.S_IEXEC)
461+
os.chmod(full_name, os.stat(full_name).st_mode | stat.S_IEXEC)

utils/gen-docker-image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ docker run --rm $image generate docker -b neurodebian:stretch -p apt \
1010
--dcm2niix version=v1.0.20180622 method=source \
1111
--install git gcc pigz liblzma-dev libc-dev git-annex-standalone netbase \
1212
--copy . /src/heudiconv \
13-
--miniconda use_env=base conda_install="python=3.6 traits>=4.6.0 scipy numpy nomkl" \
13+
--miniconda use_env=base conda_install="python=3.6 traits>=4.6.0 scipy numpy nomkl pandas" \
1414
pip_install="/src/heudiconv[all]" \
1515
pip_opts="--editable" \
1616
--entrypoint "heudiconv" \

0 commit comments

Comments
 (0)