|
435 | 435 | "outputs": [], |
436 | 436 | "source": [ |
437 | 437 | "import tempfile\n", |
| 438 | + "from pathlib import Path\n", |
438 | 439 | "import numpy as np\n", |
439 | 440 | "from fileformats.medimage import Nifti1\n", |
440 | 441 | "import fileformats.medimage_mrtrix3 as mrtrix3\n", |
441 | 442 | "from pydra.engine.environments import Docker\n", |
442 | 443 | "from pydra.design import workflow, python\n", |
443 | 444 | "from pydra.tasks.mrtrix3.v3_0 import MrConvert, MrThreshold\n", |
444 | 445 | "\n", |
| 446 | + "MRTRIX2NUMPY_DTYPES = {\n", |
| 447 | + " \"Int8\": np.dtype(\"i1\"),\n", |
| 448 | + " \"UInt8\": np.dtype(\"u1\"),\n", |
| 449 | + " \"Int16LE\": np.dtype(\"<i2\"),\n", |
| 450 | + " \"Int16BE\": np.dtype(\">i2\"),\n", |
| 451 | + " \"UInt16LE\": np.dtype(\"<u2\"),\n", |
| 452 | + " \"UInt16BE\": np.dtype(\">u2\"),\n", |
| 453 | + " \"Int32LE\": np.dtype(\"<i4\"),\n", |
| 454 | + " \"Int32BE\": np.dtype(\">i4\"),\n", |
| 455 | + " \"UInt32LE\": np.dtype(\"<u4\"),\n", |
| 456 | + " \"UInt32BE\": np.dtype(\">u4\"),\n", |
| 457 | + " \"Float32LE\": np.dtype(\"<f4\"),\n", |
| 458 | + " \"Float32BE\": np.dtype(\">f4\"),\n", |
| 459 | + " \"Float64LE\": np.dtype(\"<f8\"),\n", |
| 460 | + " \"Float64BE\": np.dtype(\">f8\"),\n", |
| 461 | + " \"CFloat32LE\": np.dtype(\"<c8\"),\n", |
| 462 | + " \"CFloat32BE\": np.dtype(\">c8\"),\n", |
| 463 | + " \"CFloat64LE\": np.dtype(\"<c16\"),\n", |
| 464 | + " \"CFloat64BE\": np.dtype(\">c16\"),\n", |
| 465 | + "}\n", |
| 466 | + "\n", |
| 467 | + "\n", |
445 | 468 | "@workflow.define(outputs=[\"out_image\"])\n", |
446 | 469 | "def ToyMedianThreshold(in_image: Nifti1) -> mrtrix3.ImageFormat:\n", |
447 | 470 | " \"\"\"A toy example workflow that\n", |
|
457 | 480 | " )\n", |
458 | 481 | "\n", |
459 | 482 | " @python.define\n", |
460 | | - " def SelectDataFile(in_file: mrtrix3.ImageHeader) -> mrtrix3.ImageDataFile:\n", |
461 | | - " return in_file.data_file\n", |
462 | | - "\n", |
463 | | - " select_data = workflow.add(SelectDataFile(in_file=input_conversion.out_file))\n", |
464 | | - "\n", |
465 | | - " @python.define\n", |
466 | | - " def Median(data_file: mrtrix3.ImageDataFile) -> float:\n", |
467 | | - " data = np.load(data_file)\n", |
| 483 | + " def Median(mih: mrtrix3.ImageHeader) -> float:\n", |
| 484 | + " \"\"\"A bespoke function that reads the separate data file in the MRTrix3 image\n", |
| 485 | + " header format (i.e. .mih) and calculates the median value.\"\"\"\n", |
| 486 | + " dtype = MRTRIX2NUMPY_DTYPES[mih.metadata[\"datatype\"].strip()]\n", |
| 487 | + " data = np.frombuffer(Path.read_bytes(mih.data_file), dtype=dtype)\n", |
468 | 488 | " return np.median(data)\n", |
469 | 489 | "\n", |
470 | | - " median = workflow.add(Median(data_file=select_data.out))\n", |
| 490 | + " median = workflow.add(Median(mih=input_conversion.out_file))\n", |
471 | 491 | " threshold = workflow.add(\n", |
472 | | - " MrThreshold(\n", |
473 | | - " in_file=in_image,\n", |
474 | | - " abs=median.out\n", |
475 | | - " ), \n", |
476 | | - " environment=Docker(\"mrtrix3/mrtrix3\", tag=\"\")\n", |
| 492 | + " MrThreshold(in_file=in_image, out_file=\"binary.mif\", abs=median.out),\n", |
| 493 | + " environment=Docker(\"mrtrix3/mrtrix3\", tag=\"latest\"),\n", |
477 | 494 | " )\n", |
478 | 495 | "\n", |
479 | 496 | " output_conversion = workflow.add(\n", |
|
0 commit comments