Skip to content

Commit

Permalink
removed generated pydra files from git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Feb 23, 2024
1 parent 8adb7ff commit 9f41b6a
Show file tree
Hide file tree
Showing 227 changed files with 119 additions and 28,934 deletions.
6 changes: 3 additions & 3 deletions core/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,9 @@ std::string restructured_text_usage() {

std::string pydra_usage() {

std::string CMD_PREFIXES[] = {"Fivett", "Afd", "Amp", "Connectome", "Dcm", "Dir", "Dwi",
"Fixel", "Fod", "Label", "Mask", "Mesh", "Mr", "Mt",
"Peaks", "Sh", "Tck", "Transform", "Tsf", "Voxel", "Vector"};
std::string CMD_PREFIXES[] = {"Fivett", "Afd", "Amp", "Connectome", "Dcm", "Dir", "Dwi", "Fixel",
"Fod", "Label", "Mask", "Mesh", "Mr", "Mt", "Peaks", "Sh",
"Tck", "Tensor", "Transform", "Tsf", "Voxel", "Vector", "Warp"};

auto convert_to_pascal_case = [&](const std::string &input) {
std::string result;
Expand Down
87 changes: 79 additions & 8 deletions interfaces/pydra/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from importlib import import_module
import logging
import re
from tqdm import tqdm
import click
import black.report
import black.parsing
Expand All @@ -26,6 +27,37 @@
"convert_bruker",
"gen_scheme",
"notfound",
"for_each",
"mrview",
"shview",
]


CMD_PREFIXES = [
"fivett",
"afd",
"amp",
"connectome",
"dcm",
"dir",
"dwi",
"fixel",
"fod",
"label",
"mask",
"mesh",
"mr",
"mt",
"peaks",
"sh",
"tck",
"tensor",
"transform",
"tsf",
"voxel",
"vector",
"warp",
"response",
]


Expand Down Expand Up @@ -68,17 +100,35 @@ def auto_gen_mrtrix3_pydra(
# generate the tests
sys.path.insert(0, str(output_dir))

cmds = []
manual_cmds = []
manual_path = output_dir / "pydra" / "tasks" / "mrtrix3" / "manual"
if manual_path.exists():
for manual_file in manual_path.iterdir():
manual_cmd = manual_file.stem[:-1]
if not manual_cmd.startswith(".") and not manual_cmd.startswith("__"):
manual_cmds.append(manual_cmd)

for cmd_name in sorted(os.listdir(cmd_dir)):
if cmd_name.startswith("_") or "." in cmd_name or cmd_name in IGNORE:
cmds = []
for cmd_name in tqdm(
sorted(os.listdir(cmd_dir)),
"generating Pydra interfaces for all MRtrix3 commands",
):
if (
cmd_name.startswith("_")
or "." in cmd_name
or cmd_name in IGNORE
or cmd_name in manual_cmds
):
continue
cmd = [str(cmd_dir / cmd_name)]
cmds.extend(auto_gen_cmd(cmd, cmd_name, output_dir, log_errors, pkg_version))
cmds.extend(auto_gen_cmd(cmd, cmd_name, output_dir, cmd_dir, log_errors, pkg_version))

# Write init
init_path = output_dir / "pydra" / "tasks" / "mrtrix3" / pkg_version / "__init__.py"
imports = "\n".join(f"from .{c}_ import {c}" for c in cmds)
imports = "\n".join(f"from .{c}_ import {pascal_case_task_name(c)}" for c in cmds)
imports += "\n" + "\n".join(
f"from ..manual.{c}_ import {pascal_case_task_name(c)}" for c in manual_cmds
)
init_path.write_text(f"# Auto-generated, do not edit\n\n{imports}\n")

if latest:
Expand All @@ -88,14 +138,19 @@ def auto_gen_mrtrix3_pydra(
)
print(f"Generated pydra.tasks.mrtrix3.{pkg_version} package")

# Test out import
import_module(f"pydra.tasks.mrtrix3.{pkg_version}")

def auto_gen_cmd(
cmd: ty.List[str],
cmd_name: str,
output_dir: Path,
cmd_dir: Path,
log_errors: bool,
pkg_version: str,
) -> ty.List[str]:
base_cmd = str(cmd_dir / cmd[0])
cmd = [base_cmd] + cmd[1:]
try:
code_str = sp.check_output(cmd + ["__print_usage_pydra__"]).decode("utf-8")
except sp.CalledProcessError:
Expand All @@ -113,6 +168,7 @@ def auto_gen_cmd(
cmd + [algorithm],
f"{cmd_name}_{algorithm}",
output_dir,
cmd_dir,
log_errors,
pkg_version,
)
Expand Down Expand Up @@ -154,7 +210,7 @@ def auto_gen_test(cmd_name: str, output_dir: Path, log_errors: bool, pkg_version
tests_dir = output_dir / "pydra" / "tasks" / "mrtrix3" / pkg_version / "tests"
tests_dir.mkdir(exist_ok=True)
module = import_module(f"pydra.tasks.mrtrix3.{pkg_version}.{cmd_name}_")
interface = getattr(module, cmd_name)
interface = getattr(module, pascal_case_task_name(cmd_name))
task = interface()

code_str = f"""# Auto-generated test for {cmd_name}
Expand All @@ -165,7 +221,7 @@ def auto_gen_test(cmd_name: str, output_dir: Path, log_errors: bool, pkg_version
from pydra.tasks.mrtrix3.{pkg_version} import {cmd_name}
def test_{cmd_name}(tmp_path, cli_parse_only):
def test_{cmd_name.lower()}(tmp_path, cli_parse_only):
task = {cmd_name}(
"""
Expand Down Expand Up @@ -264,6 +320,21 @@ def escape_cmd_name(cmd_name: str) -> str:
return cmd_name.replace("5tt", "fivett")


def pascal_case_task_name(cmd_name: str) -> str:
# convert to PascalCase
if cmd_name == "population_template":
return "PopulationTemplate"
try:
return "".join(
g.capitalize()
for g in re.match(rf"({'|'.join(CMD_PREFIXES)}?)(2?)([^_]+)(_?)(.*)", cmd_name).groups()
)
except AttributeError as e:
raise ValueError(
f"Could not convert {cmd_name} to PascalCase, please add its prefix to CMD_PREFIXES"
) from e


if __name__ == "__main__":
from pathlib import Path

Expand All @@ -275,7 +346,7 @@ def escape_cmd_name(cmd_name: str) -> str:

auto_gen_mrtrix3_pydra(
[
str(script_dir.parent / "bin"),
sys.argv[1],
str(script_dir / "src"),
mrtrix_version,
"--raise-errors",
Expand Down
5 changes: 3 additions & 2 deletions interfaces/pydra/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
black >=22.12.0
click >=8.1.3
tqdm
attrs >=23.1.0
fileformats >= 0.8
fileformats-extras >= 0.2.1
fileformats-medimage >= 0.4.4
fileformats-medimage-extras >= 0.1.5
fileformats-mrtrix
fileformats-mrtrix-extras
fileformats-medimage-mrtrix3
fileformats-medimage-mrtrix3-extras
10 changes: 5 additions & 5 deletions interfaces/pydra/src/pydra/tasks/mrtrix3/manual/mrcalc_.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def operations_formatter(operations: ty.List[ty.Tuple[ty.List[ty.Union[ImageIn,
),
]

mrcalc_input_spec = specs.SpecInfo(
MrCalcInputSpec = specs.SpecInfo(
name="mrcalc_input", fields=input_fields, bases=(specs.ShellSpec,)
)

Expand All @@ -220,12 +220,12 @@ def operations_formatter(operations: ty.List[ty.Tuple[ty.List[ty.Union[ImageIn,
},
),
]
mrcalc_output_spec = specs.SpecInfo(
MrCalcOutputSpec = specs.SpecInfo(
name="mrcalc_output", fields=output_fields, bases=(specs.ShellOutSpec,)
)


class mrcalc(ShellCommandTask):
class MrCalc(ShellCommandTask):
"""This command will only compute per-voxel operations. Use 'mrmath' to compute summary statistics across images or along image axes.
This command uses a stack-based syntax, with operators (specified using options) operating on the top-most entries (i.e. images or values) in the stack. Operands (values or images) are pushed onto the stack in the order they appear (as arguments) on the command-line, and operators (specified as options) operate on and consume the top-most entries in the stack, and push their output as a new entry on the stack.
Expand Down Expand Up @@ -296,5 +296,5 @@ class mrcalc(ShellCommandTask):
Op = Operator = MrCalcOp

executable = "mrcalc"
input_spec = mrcalc_input_spec
output_spec = mrcalc_output_spec
input_spec = MrCalcInputSpec
output_spec = MrCalcOutputSpec
113 changes: 0 additions & 113 deletions interfaces/pydra/src/pydra/tasks/mrtrix3/v3_0/__init__.py

This file was deleted.

Loading

0 comments on commit 9f41b6a

Please sign in to comment.