Skip to content

Instantly share code, notes, and snippets.

@alexfoias
Created November 5, 2020 19:12
Show Gist options
  • Save alexfoias/4642e44cde25c4aebab8945b7bf8bc4e to your computer and use it in GitHub Desktop.
Save alexfoias/4642e44cde25c4aebab8945b7bf8bc4e to your computer and use it in GitHub Desktop.
pytest without prelude or dcm2niix
[5/11/20 2:10:05] (base) alex@NeuroPoly-MacBook-Pro shimming-toolbox % pytest -m "not prelude and not dcm2niix"
INFO:root:/Users/alex/code/shimming-toolbox/testing_data, testing_data
INFO:shimmingtoolbox.download:Trying URL: https://github.com/shimming-toolbox/data-testing/archive/r20201021.zip
INFO:shimmingtoolbox.download:Downloading: data-testing-r20201021.zip
Status: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11.4M/11.4M [00:00<00:00, 13.9MB/s]
INFO:shimmingtoolbox.download:Unzip data to: /var/folders/11/q0s99tk17rj_7jtmj1psydbr0000gn/T/st_download_8k3rb4g3
INFO:shimmingtoolbox.download:Removing temporary folders...
============================================================================================== test session starts ===============================================================================================
platform darwin -- Python 3.8.3, pytest-4.6.11, py-1.9.0, pluggy-0.13.1
rootdir: /Users/alex/code/shimming-toolbox, inifile: pytest.ini
plugins: cov-2.5.1
collected 62 items / 11 deselected / 51 selected
test/test_biot_savart.py ... [ 5%]
test/test_dicom_to_nifti.py . [ 7%]
test/test_example_scripts.py F [ 9%]
test/test_init.py . [ 11%]
test/test_load_nifti.py .....F.... [ 31%]
test/test_masking.py ...... [ 43%]
test/test_pmu.py ... [ 49%]
test/test_sequential_zslice.py . [ 50%]
test/test_siemens_basis.py .. [ 54%]
test/test_spherical_harmonics.py ... [ 60%]
test/simulate/test_numerical_model.py .................... [100%]
==================================================================================================== FAILURES ====================================================================================================
____________________________________________________________________________________ TestCore.test_demo_script_outputs_figure ____________________________________________________________________________________
self = <test_example_scripts.TestCore object at 0x7f8cbbdac370>
def test_demo_script_outputs_figure(self):
> fname_nifti = general_demo(self.tmp_path)
test/test_example_scripts.py:28:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
examples/general_demo.py:72: in general_demo
unwrapped_phase_e1 = prelude(phase_e1, nii_mag_e1.get_fdata(), nii_phase_e1.affine)
shimmingtoolbox/unwrap/prelude.py:79: in prelude
run_subprocess(unwrap_command)
shimmingtoolbox/utils.py:20: in run_subprocess
subprocess.run(cmd.split(' '), stdout=subprocess.PIPE, text=True, check=True)
../../miniconda3/lib/python3.8/subprocess.py:489: in run
with Popen(*popenargs, **kwargs) as process:
../../miniconda3/lib/python3.8/subprocess.py:854: in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <subprocess.Popen object at 0x7f8cbbe0e7f0>
args = ['prelude', '-p', '/var/folders/11/q0s99tk17rj_7jtmj1psydbr0000gn/T/st_preludexvk_f5dc/rawPhase', '-a', '/var/folders/11/q0s99tk17rj_7jtmj1psydbr0000gn/T/st_preludexvk_f5dc/mag', '-o', ...]
executable = b'prelude', preexec_fn = None, close_fds = True, pass_fds = (), cwd = None, env = None, startupinfo = None, creationflags = 0, shell = False, p2cread = -1, p2cwrite = -1, c2pread = 14
c2pwrite = 15, errread = -1, errwrite = -1, restore_signals = True, start_new_session = False
def _execute_child(self, args, executable, preexec_fn, close_fds,
pass_fds, cwd, env,
startupinfo, creationflags, shell,
p2cread, p2cwrite,
c2pread, c2pwrite,
errread, errwrite,
restore_signals, start_new_session):
"""Execute program (POSIX version)"""
if isinstance(args, (str, bytes)):
args = [args]
elif isinstance(args, os.PathLike):
if shell:
raise TypeError('path-like args is not allowed when '
'shell is true')
args = [args]
else:
args = list(args)
if shell:
# On Android the default shell is at '/system/bin/sh'.
unix_shell = ('/system/bin/sh' if
hasattr(sys, 'getandroidapilevel') else '/bin/sh')
args = [unix_shell, "-c"] + args
if executable:
args[0] = executable
if executable is None:
executable = args[0]
sys.audit("subprocess.Popen", executable, args, cwd, env)
if (_USE_POSIX_SPAWN
and os.path.dirname(executable)
and preexec_fn is None
and not close_fds
and not pass_fds
and cwd is None
and (p2cread == -1 or p2cread > 2)
and (c2pwrite == -1 or c2pwrite > 2)
and (errwrite == -1 or errwrite > 2)
and not start_new_session):
self._posix_spawn(args, executable, env, restore_signals,
p2cread, p2cwrite,
c2pread, c2pwrite,
errread, errwrite)
return
orig_executable = executable
# For transferring possible exec failure from child to parent.
# Data format: "exception name:hex errno:description"
# Pickle is not used; it is complex and involves memory allocation.
errpipe_read, errpipe_write = os.pipe()
# errpipe_write must not be in the standard io 0, 1, or 2 fd range.
low_fds_to_close = []
while errpipe_write < 3:
low_fds_to_close.append(errpipe_write)
errpipe_write = os.dup(errpipe_write)
for low_fd in low_fds_to_close:
os.close(low_fd)
try:
try:
# We must avoid complex work that could involve
# malloc or free in the child process to avoid
# potential deadlocks, thus we do all this here.
# and pass it to fork_exec()
if env is not None:
env_list = []
for k, v in env.items():
k = os.fsencode(k)
if b'=' in k:
raise ValueError("illegal environment variable name")
env_list.append(k + b'=' + os.fsencode(v))
else:
env_list = None # Use execv instead of execve.
executable = os.fsencode(executable)
if os.path.dirname(executable):
executable_list = (executable,)
else:
# This matches the behavior of os._execvpe().
executable_list = tuple(
os.path.join(os.fsencode(dir), executable)
for dir in os.get_exec_path(env))
fds_to_keep = set(pass_fds)
fds_to_keep.add(errpipe_write)
self.pid = _posixsubprocess.fork_exec(
args, executable_list,
close_fds, tuple(sorted(map(int, fds_to_keep))),
cwd, env_list,
p2cread, p2cwrite, c2pread, c2pwrite,
errread, errwrite,
errpipe_read, errpipe_write,
restore_signals, start_new_session, preexec_fn)
self._child_created = True
finally:
# be sure the FD is closed no matter what
os.close(errpipe_write)
self._close_pipe_fds(p2cread, p2cwrite,
c2pread, c2pwrite,
errread, errwrite)
# Wait for exec to fail or succeed; possibly raising an
# exception (limited in size)
errpipe_data = bytearray()
while True:
part = os.read(errpipe_read, 50000)
errpipe_data += part
if not part or len(errpipe_data) > 50000:
break
finally:
# be sure the FD is closed no matter what
os.close(errpipe_read)
if errpipe_data:
try:
pid, sts = os.waitpid(self.pid, 0)
if pid == self.pid:
self._handle_exitstatus(sts)
else:
self.returncode = sys.maxsize
except ChildProcessError:
pass
try:
exception_name, hex_errno, err_msg = (
errpipe_data.split(b':', 2))
# The encoding here should match the encoding
# written in by the subprocess implementations
# like _posixsubprocess
err_msg = err_msg.decode()
except ValueError:
exception_name = b'SubprocessError'
hex_errno = b'0'
err_msg = 'Bad exception data from child: {!r}'.format(
bytes(errpipe_data))
child_exception_type = getattr(
builtins, exception_name.decode('ascii'),
SubprocessError)
if issubclass(child_exception_type, OSError) and hex_errno:
errno_num = int(hex_errno, 16)
child_exec_never_called = (err_msg == "noexec")
if child_exec_never_called:
err_msg = ""
# The error must be from chdir(cwd).
err_filename = cwd
else:
err_filename = orig_executable
if errno_num != 0:
err_msg = os.strerror(errno_num)
> raise child_exception_type(errno_num, err_msg, err_filename)
E FileNotFoundError: [Errno 2] No such file or directory: 'prelude'
../../miniconda3/lib/python3.8/subprocess.py:1702: FileNotFoundError
---------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------
Example in:
/Users/alex/code/shimming-toolbox/test/__tmp__/niftis/tmp_dcm2bids/helper
---------------------------------------------------------------------------------------------- Captured stderr call ----------------------------------------------------------------------------------------------
Status: 100%|██████████| 11.4M/11.4M [00:02<00:00, 4.81MB/s]
INFO:dcm2bids.dcm2bids:--- dcm2bids start ---
INFO:dcm2bids.dcm2bids:OS:version: macOS-10.15.7-x86_64-i386-64bit
INFO:dcm2bids.dcm2bids:python:version: 3.8.3 (default, May 19 2020, 13:54:14) [Clang 10.0.0 ]
INFO:dcm2bids.dcm2bids:dcm2bids:version: 2.1.4
INFO:dcm2bids.dcm2bids:dcm2niix:version: v1.0.20201102
INFO:dcm2bids.dcm2bids:participant: sub-01
INFO:dcm2bids.dcm2bids:session:
INFO:dcm2bids.dcm2bids:config: /Users/alex/code/shimming-toolbox/config/dcm2bids.json
INFO:dcm2bids.dcm2bids:BIDS directory: /Users/alex/code/shimming-toolbox/test/__tmp__/niftis
INFO:dcm2bids.utils:Running dcm2niix -b y -ba y -z y -f '%3s_%f_%p_%t' -o /Users/alex/code/shimming-toolbox/test/__tmp__/niftis/tmp_dcm2bids/sub-01 /Users/alex/code/shimming-toolbox/test/__tmp__/testing_data/dicom_unsorted
INFO:dcm2bids.dcm2niix:Check log file for dcm2niix output
INFO:dcm2bids.sidecar:Sidecars pairing:
INFO:dcm2bids.sidecar:_magnitude1 <- 006_dicom_unsorted_a_gre_DYNshim_20191101153345_e1
INFO:dcm2bids.sidecar:_phase1 <- 007_dicom_unsorted_a_gre_DYNshim_20191101153345_e1_ph
INFO:dcm2bids.sidecar:_magnitude2 <- 006_dicom_unsorted_a_gre_DYNshim_20191101153345_e2
INFO:dcm2bids.sidecar:_magnitude3 <- 006_dicom_unsorted_a_gre_DYNshim_20191101153345_e3
INFO:dcm2bids.sidecar:_magnitude5 <- 006_dicom_unsorted_a_gre_DYNshim_20191101153345_e5
INFO:dcm2bids.sidecar:_phase2 <- 007_dicom_unsorted_a_gre_DYNshim_20191101153345_e2_ph
INFO:dcm2bids.sidecar:_phase3 <- 007_dicom_unsorted_a_gre_DYNshim_20191101153345_e3_ph
INFO:dcm2bids.sidecar:_magnitude4 <- 006_dicom_unsorted_a_gre_DYNshim_20191101153345_e4
INFO:dcm2bids.sidecar:_magnitude6 <- 006_dicom_unsorted_a_gre_DYNshim_20191101153345_e6
INFO:dcm2bids.sidecar:_phase4 <- 007_dicom_unsorted_a_gre_DYNshim_20191101153345_e4_ph
INFO:dcm2bids.sidecar:_phase5 <- 007_dicom_unsorted_a_gre_DYNshim_20191101153345_e5_ph
INFO:dcm2bids.sidecar:_phase6 <- 007_dicom_unsorted_a_gre_DYNshim_20191101153345_e6_ph
INFO:dcm2bids.dcm2bids:moving acquisitions into BIDS folder
INFO:root:Unwrap with prelude
----------------------------------------------------------------------------------------------- Captured log call ------------------------------------------------------------------------------------------------
INFO root:prelude.py:78 Unwrap with prelude
____________________________________________________________________________________ TestCore.test_load_nifti_multiple_echoes ____________________________________________________________________________________
self = <test_load_nifti.TestCore object at 0x7f8cbd4a9670>, monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f8cbd4d17c0>
def test_load_nifti_multiple_echoes(self, monkeypatch):
"""
Assert passes with correct data for multiple echoes
:return:
"""
if self.tmp_path.exists():
shutil.rmtree(self.data_path_volume)
monkeypatch.setattr('sys.stdin', StringIO('0\n'))
niftis, info, json_info = load_nifti(self.tmp_path)
assert (len(info) == 2), "Wrong number od info data 1"
assert (len(json_info) == 2), "Wrong number of JSON data 1"
self._json_phase['EchoNumber'] = 2
> assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)), \
"JSON file is not correctly loaded for first JSON1"
E AssertionError: JSON file is not correctly loaded for first JSON1
E assert '{"Acquisitio...mp": 222.944}' == '{"Acquisition...mp": 222.944}'
E Skipping 357 identical leading characters in diff, use -v to show
E Skipping 1213 identical trailing characters in diff, use -v to show
E - oNumber": 1, "EchoTi
E ? ^
E + oNumber": 2, "EchoTi
E ? ^
test/test_load_nifti.py:267: AssertionError
---------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------
0:test_data
1:test_data_2
Enter the number for the appropriate acquisition folder, (type 'q' to quit) :
---------------------------------------------------------------------------------------------- Captured stderr call ----------------------------------------------------------------------------------------------
INFO:root:Multiple acquisition directories in path. Choosing only one.
INFO:root:Reading acquisitions for run 1
----------------------------------------------------------------------------------------------- Captured log call ------------------------------------------------------------------------------------------------
INFO root:load_nifti.py:66 Multiple acquisition directories in path. Choosing only one.
INFO root:load_nifti.py:128 Reading acquisitions for run 1
=============================================================================== 2 failed, 49 passed, 11 deselected in 8.66 seconds ===============================================================================
[5/11/20 2:11:12] (base) alex@NeuroPoly-MacBook-Pro shimming-toolbox %
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment