Skip to content

Commit

Permalink
Python: Initial fixes to piped image support on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Lestropie committed Aug 17, 2024
1 parent 6709049 commit f609027
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
13 changes: 8 additions & 5 deletions python/mrtrix3/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ def __format__(self, _):
# (which itself through __new__() / __init__() could be Posix or Windows)
# and a desired augmentation that provides additional functions
# TODO Can this be made a callable?
# TODO Trouble with exposing this outside of the Parser is that
# as soon as one of the member functions is called,
# the returned type will no longer be derived from _QuoteEscapedPath
def make_quote_escaped_path_object(base_class, *args):
abspath = pathlib.Path(WORKING_DIR, *args).resolve()
super_class = type(abspath)
Expand Down Expand Up @@ -663,21 +666,21 @@ class _UserInPathExtras(_QuoteEscapedPath):
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
def check_input(self, item_type='path'):
if not self.exists():
if not super().exists(): # pylint: disable=no-member
raise argparse.ArgumentTypeError(f'Input {item_type} "{self}" does not exist')
class _UserFileInPathExtras(_UserInPathExtras):
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
def check_input(self): # pylint: disable=arguments-differ
super().check_input('file')
if not self.is_file():
if not super().is_file(): # pylint: disable=no-member
raise argparse.ArgumentTypeError(f'Input path "{self}" is not a file')
class _UserDirInPathExtras(_UserInPathExtras):
def __init__(self, *args, **kwargs):
super().__init__(self, *args, **kwargs)
def check_input(self): # pylint: disable=arguments-differ
super().check_input('directory')
if not self.is_dir():
if not super().is_dir(): # pylint: disable=no-member
raise argparse.ArgumentTypeError(f'Input path "{self}" is not a directory')

# Various callable types for use as argparse argument types
Expand Down Expand Up @@ -832,8 +835,8 @@ def __call__(self, input_value):
input_value = sys.stdin.readline().strip()
if shutil.which('cygpath.exe'):
try:
new_path = subprocess.run(['cygpath.exe', '-m', input_value], check=True, capture_output=True, text=True).stdout.strip()
debug(f'stdin contents to cygpath.exe: {input_value} -> {new_path}\n')
new_path = subprocess.run(['cygpath.exe', '-u', input_value], check=True, capture_output=True, text=True).stdout.strip()
debug(f'stdin contents to cygpath.exe: {input_value} -> {new_path}')
abspath = make_quote_escaped_path_object(_QuoteEscapedPath, new_path)
except subprocess.CalledProcessError:
warn(f'Error converting input piped image path {input_value} using cygpath;'
Expand Down
5 changes: 2 additions & 3 deletions python/mrtrix3/commands/dwi2response/dhollander.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# For more details, see http://www.mrtrix.org/.


import math, shlex, shutil
import math, shutil
from mrtrix3 import CONFIG, MRtrixError
from mrtrix3 import app, image, run

Expand Down Expand Up @@ -303,8 +303,7 @@ def execute(): #pylint: disable=unused-variable
app.warn('Single-fibre WM response function selection algorithm "tax" will not honour requested WM voxel percentage')
run.command(f'dwi2response {app.ARGS.wm_algo} dwi.mif _respsfwmss.txt -mask refined_wm.mif -voxels voxels_sfwm.mif'
+ ('' if app.ARGS.wm_algo == 'tax' else (' -number ' + str(voxsfwmcount)))
+ ' -scratch ' + shlex.quote(app.SCRATCH_DIR)
+ recursive_cleanup_option,
+ f' -scratch {app.SCRATCH_DIR}{recursive_cleanup_option}',
show=False)
else:
app.console(' Selecting WM single-fibre voxels using built-in (Dhollander et al., 2019) algorithm')
Expand Down
6 changes: 2 additions & 4 deletions python/mrtrix3/commands/dwi2response/msmt_5tt.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,11 @@ def execute(): #pylint: disable=unused-variable
if not app.ARGS.sfwm_fa_threshold:
app.console(f'Selecting WM single-fibre voxels using "{app.ARGS.wm_algo}" algorithm')
run.command(f'dwi2response {app.ARGS.wm_algo} dwi.mif wm_ss_response.txt -mask wm_mask.mif -voxels wm_sf_mask.mif'
' -scratch %s' % shlex.quote(app.SCRATCH_DIR)
+ recursive_cleanup_option)
f' -scratch {app.SCRATCH_DIR}{recursive_cleanup_option}')
else:
app.console(f'Selecting WM single-fibre voxels using "fa" algorithm with a hard FA threshold of {app.ARGS.sfwm_fa_threshold}')
run.command(f'dwi2response fa dwi.mif wm_ss_response.txt -mask wm_mask.mif -threshold {app.ARGS.sfwm_fa_threshold} -voxels wm_sf_mask.mif'
' -scratch %s' % shlex.quote(app.SCRATCH_DIR)
+ recursive_cleanup_option)
f' -scratch {app.SCRATCH_DIR}{recursive_cleanup_option}')

# Check for empty masks
wm_voxels = image.statistics('wm_sf_mask.mif', mask='wm_sf_mask.mif').count
Expand Down
2 changes: 1 addition & 1 deletion python/mrtrix3/commands/dwinormalise/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, filename, prefix, mask_filename = ''):
in_path,
wm_mask_warped_path,
dwi_normalised_path])
run.command(['mrconvert', dwi_normalised_path, app.ARGS.output_dir / i.filename],
run.command(['mrconvert', dwi_normalised_path, app.make_quote_escaped_path_object(app.ARGS.output_dir, i.filename)],
mrconvert_keyval=in_path,
force=app.FORCE_OVERWRITE)
app.cleanup([warp_path, fa_path, wm_mask_warped_path, dwi_normalised_path])
Expand Down

0 comments on commit f609027

Please sign in to comment.