Source code for thesis.workflows.preprocess.nodes.utils

"""Utility node builders for file operations.

This module provides Nipype Function node builders that wrap file manipulation
operations from operations.file_ops. These nodes can be integrated into
preprocessing workflows.
"""

from nipype import Node
from nipype.interfaces.utility import Function

from thesis.core.logging import get_logger

__all__ = [
    "prepare_modify_bval_node",
    "prepare_create_index_node",
]

logger = get_logger(__name__)


def _modify_bval_wrapper(input_bval: str, output_bval: str) -> str:
    """Wrapper function for modify_bval operation.

    Args:
        input_bval: Path to input bval file
        output_bval: Path to output bval file

    Returns:
        Absolute path to the output bval file as a string
    """
    from thesis.workflows.preprocess.operations.file_ops import modify_bval

    return modify_bval(input_bval, output_bval)


def _create_index_wrapper(bval_file: str, output_index: str) -> str:
    """Wrapper function for create_index_file operation.

    Args:
        bval_file: Path to input bval file
        output_index: Path to output index file

    Returns:
        Absolute path to the output index file as a string
    """
    from thesis.workflows.preprocess.operations.file_ops import create_index_file

    return create_index_file(bval_file, output_index)


[docs] def prepare_modify_bval_node(name: str = "modify_bval") -> Node: """Create a Nipype node for modifying bval files. This node wraps the modify_bval operation, which replaces standalone "100" values with "101" in bval files. Args: name: Name for the Nipype node Returns: Configured Nipype Function Node Example: >>> node = prepare_modify_bval_node("modify_bval") >>> node.inputs.input_bval = "input.bval" >>> node.inputs.output_bval = "output.bval" >>> result = node.run() """ logger.debug(f"Creating modify_bval node: {name}") node = Node( Function( input_names=["input_bval", "output_bval"], output_names=["output_bval"], function=_modify_bval_wrapper, ), name=name, ) return node
[docs] def prepare_create_index_node(name: str = "create_index") -> Node: """Create a Nipype node for creating index files. This node wraps the create_index_file operation, which creates an index file with "1" for each b-value in the bval file. Args: name: Name for the Nipype node Returns: Configured Nipype Function Node Example: >>> node = prepare_create_index_node("create_index") >>> node.inputs.bval_file = "input.bval" >>> node.inputs.output_index = "index.txt" >>> result = node.run() """ logger.debug(f"Creating create_index node: {name}") node = Node( Function( input_names=["bval_file", "output_index"], output_names=["output_index"], function=_create_index_wrapper, ), name=name, ) return node