Skip to content

Commit

Permalink
Fix Python commands, documentation generation, CLI test
Browse files Browse the repository at this point in the history
- Fix importlib call in cmake-generated Python executables for stand-alone Python files; this regressed in cleanup commit 291a747, which worked for the Python CLI test (as both executable and source file resided in the same directory) but broke execution of other Python commands.
- Change handling of Python CLI test: executable will be placed in ${PROJECT_BINARY_DIR}/bin; source code will be placed in ${PROJECT_BUILD_DIR}/lib/mrtrix3/commands/.
- Update docs/generate_user_docs.sh to reflect new handling of Python commands.
- Auto-update of some Python command help pages to propagate prior changes.
- Add per-test labels to unit tests.
  • Loading branch information
Lestropie committed Jun 21, 2024
1 parent f87bc0e commit 3fce648
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 40 deletions.
15 changes: 3 additions & 12 deletions cmake/MakePythonExecutable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Inputs:
# - CMDNAME: Name of the command
# - OUTPUT_DIR: Directory in which to create the executable
# - EXTRA_PATH_DIRS: an optional list of directories to be prepended to the Python path (shouldn't be necessary for most commands).

set(BINPATH_CONTENTS
"#!/usr/bin/python3\n"
Expand All @@ -14,18 +13,10 @@ set(BINPATH_CONTENTS
"\n"
"mrtrix_lib_path = os.path.normpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, 'lib'))\n"
"sys.path.insert(0, mrtrix_lib_path)\n"
"from mrtrix3.app import _execute\n"
"\n"
)

if(EXTRA_PATH_DIRS)
foreach(PATH_DIR ${EXTRA_PATH_DIRS})
string(APPEND BINPATH_CONTENTS
"sys.path.insert(0, '${PATH_DIR}')\n"
)
endforeach()
endif()

string(APPEND BINPATH_CONTENTS "from mrtrix3.app import _execute\n")

# Three possible interfaces:
# 1. Standalone file residing in commands/
# 2. File stored in location commands/<cmdname>/<cmdname>.py, which will contain usage() and execute() functions
Expand All @@ -47,7 +38,7 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/__init__.py")
endif()
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}.py")
string(APPEND BINPATH_CONTENTS
"module = importlib.import_module('${CMDNAME}')\n"
"module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands')\n"
"_execute(module.usage, module.execute)\n"
)
else()
Expand Down
6 changes: 3 additions & 3 deletions docs/generate_user_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function prepend {
# Generating documentation for all commands

mrtrix_root=$( cd "$(dirname "${BASH_SOURCE}")"/../ ; pwd -P )
export PATH=$build_dir/bin:${mrtrix_root}/python/bin:"$PATH"
export PATH=$build_dir/bin:$PATH
dirpath=${mrtrix_root}'/docs/reference/commands'
export LC_ALL=C

Expand Down Expand Up @@ -82,8 +82,8 @@ cmdlist=""
for n in `find "${mrtrix_root}"/cmd/ -name "*.cpp"`; do
cmdlist=$cmdlist$'\n'`basename $n`
done
for n in `find "${mrtrix_root}"/python/bin/ -type f -print0 | xargs -0 grep -l "import mrtrix3"`; do
cmdlist=$cmdlist$'\n'`basename $n`
for n in `ls "${mrtrix_root}"/python/mrtrix3/commands/ --ignore=__init__.py* --ignore=CMakeLists.txt`; do
cmdlist=$cmdlist$'\n'`basename $n .py`
done

for n in `echo "$cmdlist" | sort`; do
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commands/5ttgen.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage

5ttgen algorithm [ options ] ...

- *algorithm*: Select the algorithm to be used to complete the script operation; additional details and options become available once an algorithm is nominated. Options are: freesurfer, fsl, gif, hsvs
- *algorithm*: Select the algorithm to be used; additional details and options become available once an algorithm is nominated. Options are: freesurfer, fsl, gif, hsvs

Description
-----------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commands/dwi2mask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage

dwi2mask algorithm [ options ] ...

- *algorithm*: Select the algorithm to be used to complete the script operation; additional details and options become available once an algorithm is nominated. Options are: 3dautomask, ants, b02template, consensus, fslbet, hdbet, legacy, mean, mtnorm, synthstrip, trace
- *algorithm*: Select the algorithm to be used; additional details and options become available once an algorithm is nominated. Options are: 3dautomask, ants, b02template, consensus, fslbet, hdbet, legacy, mean, mtnorm, synthstrip, trace

Description
-----------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commands/dwi2response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage

dwi2response algorithm [ options ] ...

- *algorithm*: Select the algorithm to be used to complete the script operation; additional details and options become available once an algorithm is nominated. Options are: dhollander, fa, manual, msmt_5tt, tax, tournier
- *algorithm*: Select the algorithm to be used; additional details and options become available once an algorithm is nominated. Options are: dhollander, fa, manual, msmt_5tt, tax, tournier

Description
-----------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commands/dwibiascorrect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage

dwibiascorrect algorithm [ options ] ...

- *algorithm*: Select the algorithm to be used to complete the script operation; additional details and options become available once an algorithm is nominated. Options are: ants, fsl, mtnorm
- *algorithm*: Select the algorithm to be used; additional details and options become available once an algorithm is nominated. Options are: ants, fsl, mtnorm

Description
-----------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commands/dwinormalise.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage

dwinormalise algorithm [ options ] ...

- *algorithm*: Select the algorithm to be used to complete the script operation; additional details and options become available once an algorithm is nominated. Options are: group, manual, mtnorm
- *algorithm*: Select the algorithm to be used; additional details and options become available once an algorithm is nominated. Options are: group, manual, mtnorm

Description
-----------
Expand Down
45 changes: 26 additions & 19 deletions testing/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ set(CPP_TOOLS_SRCS
testing_npywrite.cpp
)

set(PYTHON_TOOLS_SRCS
set(PYTHON_TOOLS_STANDALONE
testing_check_npy.py
testing_gen_npy.py
)

set(PYTHON_TOOLS_MRTRIXAPI
testing_python_cli.py
)

Expand All @@ -36,28 +39,32 @@ function(add_cpp_tool TOOL_SRC)
add_dependencies(testing_tools ${TOOL_NAME})
endfunction()

function(add_python_tool TOOL_SRC IS_MRTRIX_CMD)
function(add_python_tool_standalone TOOL_SRC)
get_filename_component(TOOL_NAME ${TOOL_SRC} NAME_WE)
# If the tool behaves like a Python MRtrix3 command, then we need to generate its executable using
# the MakePythonExecutable script
if(IS_MRTRIX_CMD)
set(MRTRIX_PYTHON_LIB_PATH "${PROJECT_BINARY_DIR}/lib")
add_custom_command(
TARGET testing_tools PRE_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -DCMDNAME=${TOOL_NAME} -DOUTPUT_DIR="${CMAKE_CURRENT_BINARY_DIR}" -DEXTRA_PATH_DIRS="${MRTRIX_PYTHON_LIB_PATH}" -P ${PROJECT_SOURCE_DIR}/cmake/MakePythonExecutable.cmake
)
# Install "module" file alongside the executable
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TOOL_SRC} ${CMAKE_CURRENT_BINARY_DIR}/${TOOL_SRC} COPYONLY)
else()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TOOL_SRC} ${CMAKE_CURRENT_BINARY_DIR}/${TOOL_NAME} COPYONLY)
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TOOL_SRC} ${CMAKE_CURRENT_BINARY_DIR}/${TOOL_NAME} COPYONLY)
endfunction()

# If the tool behaves like a Python MRtrix3 command, then we need to generate its executable using
# the MakePythonExecutable script
function(add_python_tool_mrtrixapi TOOL_SRC)
get_filename_component(TOOL_NAME ${TOOL_SRC} NAME_WE)
set(MRTRIX_PYTHON_LIB_PATH "${PROJECT_BINARY_DIR}/lib")
add_custom_command(
TARGET testing_tools PRE_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -DCMDNAME=${TOOL_NAME} -DOUTPUT_DIR="${PROJECT_BINARY_DIR}/bin" -P ${PROJECT_SOURCE_DIR}/cmake/MakePythonExecutable.cmake
)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TOOL_SRC} ${PROJECT_BINARY_DIR}/lib/mrtrix3/commands/${TOOL_SRC} COPYONLY)
endfunction()

foreach(CMD ${CPP_TOOLS_SRCS})
add_cpp_tool(${CMD})
endforeach(CMD)

add_python_tool(testing_check_npy.py FALSE)
add_python_tool(testing_gen_npy.py FALSE)
add_python_tool(testing_python_cli.py TRUE)
foreach(CMD ${PYTHON_TOOLS_STANDALONE})
add_python_tool_standalone(${CMD})
endforeach(CMD)

foreach(CMD ${PYTHON_TOOLS_MRTRIXAPI})
add_python_tool_mrtrixapi(${CMD})
endforeach(CMD)
2 changes: 1 addition & 1 deletion testing/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function (add_bash_unit_test FILE_SRC)
WORKING_DIRECTORY ${DATA_DIR}
EXEC_DIRECTORIES "${EXEC_DIRS}"
ENVIRONMENT "PYTHONPATH=${PYTHON_ENV_PATH}"
LABELS "unittest"
LABELS "unittest;${NAME}"
)
endfunction()

Expand Down

0 comments on commit 3fce648

Please sign in to comment.