Skip to content

Commit

Permalink
Adding tests to dwi_reorder_philips
Browse files Browse the repository at this point in the history
  • Loading branch information
karp2601 authored and karp2601 committed Feb 22, 2024
1 parent 594fbb7 commit 5351cea
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
8 changes: 4 additions & 4 deletions scripts/scil_dwi_reorder_philips.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ def _build_arg_parser():

p.add_argument('in_dwi',
help='Input dwi file.')
p.add_argument('in_bvec',
help='Input bvec FSL format.')
p.add_argument('in_bval',
help='Input bval FSL format.')
p.add_argument('in_bvec',
help='Input bvec FSL format.')
p.add_argument('in_table',
help='Original philips table - first line is skipped.')
p.add_argument('out_basename',
help='Basename output file.')

p.add_argument('--json',
help='If you json file, it will check if you need'
' to reorder your Philips dwi.')
help='If you give a json file, it will check if you need '
'to reorder your Philips dwi.')

add_verbose_arg(p)
add_overwrite_arg(p)
Expand Down
78 changes: 78 additions & 0 deletions scripts/tests/test_dwi_reorder_philips.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import json
import tempfile

from dipy.io.gradients import read_bvals_bvecs
import numpy as np

from scilpy.io.fetcher import fetch_data, get_home, get_testing_files_dict

# If they already exist, this only takes 5 seconds (check md5sum)
fetch_data(get_testing_files_dict(), keys=['processing.zip'])
tmp_dir = tempfile.TemporaryDirectory()


def test_help_option(script_runner):
ret = script_runner.run('scil_dwi_reorder_philips.py', '--help')
assert ret.success


def test_reorder(script_runner):
os.chdir(os.path.expanduser(tmp_dir.name))
in_dwi = os.path.join(get_home(), 'processing', 'dwi_crop.nii.gz')
in_bval = os.path.join(get_home(), 'processing', 'dwi.bval')
in_bvec = os.path.join(get_home(), 'processing', 'dwi.bvec')
table = np.ones((64, 4))
bval, bvec = read_bvals_bvecs(in_bval, in_bvec)
table[:, :3] = bvec
table[:, 3] = bval
tmp = np.copy(table[15])
table[15] = table[30]
table[30] = tmp
in_table = os.path.expanduser(tmp_dir.name) + "/in_table.txt"
np.savetxt(in_table, table, header="Test")
ret = script_runner.run('scil_dwi_reorder_philips.py', in_dwi, in_bval,
in_bvec, in_table, 'out1', '-f')
assert ret.success


def test_reorder_w_json_old_version(script_runner):
os.chdir(os.path.expanduser(tmp_dir.name))
in_dwi = os.path.join(get_home(), 'processing', 'dwi_crop.nii.gz')
in_bval = os.path.join(get_home(), 'processing', 'dwi.bval')
in_bvec = os.path.join(get_home(), 'processing', 'dwi.bvec')
table = np.ones((64, 4))
bval, bvec = read_bvals_bvecs(in_bval, in_bvec)
table[:, :3] = bvec
table[:, 3] = bval
tmp = np.copy(table[15])
table[15] = table[30]
table[30] = tmp
in_table = os.path.expanduser(tmp_dir.name) + "/in_table.txt"
np.savetxt(in_table, table, header="Test")
in_json = os.path.expanduser(tmp_dir.name) + "/in_json.json"
info = {'SoftwareVersions': '5.5'}
with open(in_json, 'w') as f:
json.dump(info, f)
ret = script_runner.run('scil_dwi_reorder_philips.py', in_dwi, in_bval,
in_bvec, in_table, 'out2', '--json', in_json)
assert ret.success


def test_reorder_w_json_new_version(script_runner):
os.chdir(os.path.expanduser(tmp_dir.name))
in_dwi = os.path.join(get_home(), 'processing', 'dwi_crop.nii.gz')
in_bval = os.path.join(get_home(), 'processing', 'dwi.bval')
in_bvec = os.path.join(get_home(), 'processing', 'dwi.bvec')
table = np.ones((64, 4))
bval, bvec = read_bvals_bvecs(in_bval, in_bvec)
table[:, :3] = bvec
table[:, 3] = bval
tmp = np.copy(table[15])
table[15] = table[30]
table[30] = tmp
in_table = os.path.expanduser(tmp_dir.name) + "/in_table.txt"
np.savetxt(in_table, table, header="Test")
in_json = os.path.expanduser(tmp_dir.name) + "/in_json.json"
info = {'SoftwareVersions': '5.6'}
with open(in_json, 'w') as f:
json.dump(info, f)
ret = script_runner.run('scil_dwi_reorder_philips.py', in_dwi, in_bval,
in_bvec, in_table, 'out3', '--json', in_json)
assert not ret.success

0 comments on commit 5351cea

Please sign in to comment.