TPTBox


NameTPTBox JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/Hendrik-code/TPTBox
SummaryA Torso Processing Toolbox capable of processing BIDS-compatible datasets, singular niftys, points of interests, segmentations, and much more.
upload_time2024-11-14 10:01:20
maintainerNone
docs_urlNone
authorRobert Graf
requires_python<4.0,>=3.10
licenseGNU AFFERO GENERAL PUBLIC LICENSE v3.0, 19 November 2007
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version tptbox](https://badge.fury.io/py/tptbox.svg)](https://pypi.python.org/pypi/tptbox/)
[![Python Versions](https://img.shields.io/pypi/pyversions/tptbox)](https://pypi.org/project/tptbox/)
[![Stable Version](https://img.shields.io/pypi/v/tptbox?label=stable)](https://pypi.python.org/pypi/tptbox/)
[![tests](https://github.com/Hendrik-code/TPTBox/actions/workflows/tests.yml/badge.svg)](https://github.com/Hendrik-code/TPTBox/actions/workflows/tests.yml)
[![codecov](https://codecov.io/gh/Hendrik-code/TPTBox/graph/badge.svg?token=A7FWUKO9Y4)](https://codecov.io/gh/Hendrik-code/TPTBox)
[![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)

# Torso Processing ToolBox (TPTBox)

This is a multi-functional package to handle any sort of bids-conform dataset (CT, MRI, ...)
It can find, filter, search any BIDS_Family and subjects, and has many functionalities, among them:
- Easily loop over datasets, and the required files
- Read, Write Niftys, centroid jsons, ...
- Reorient, Resample, Shift Niftys, Centroids, labels
- Modular 2D snapshot generation (different views, MIPs, ...)
- 3D Mesh generation from segmentation and snapshots from them
- Registration
- Logging everything consistently
- ...

## Install the package
```bash
conda create -n 3.10 python=3.10
conda activate 3.10
pip install TPTBox
```
### Install via github:
(you should be in the project folder)
```bash
pip install poetry
poetry install
```
or:
Develop mode is really, really nice:
```bash
pip install poetry
poetry install --with dev
```

## Functionalities

Each folder in this package represents a different functionality.

The top-level-hierarchy incorporates the most important files, the BIDS_files.

### BIDS_Files

This file builds a data model out of the BIDS file names.
It can load a dataset as a BIDS_Global_info file, from which search queries and loops over the dataset can be started.
See ```tutorial_BIDS_files.ipynb``` for details.

### bids_constants
Defines constants for the BIDS nomenclature (sequence-splitting keys, naming conventions...)

### vert_constants

Contains definitions and sort order for our intern labels, for vertebrae, POI, ...

### Rotation and Resampling

Example rotate and resample.

```python
from TPTBox import NII

nii = NII.load("...path/xyz.nii.gz", seg=True)
# R right, L left
# S superior/up, I inferior/down
# A anterior/front, P posterior/back
img_rot = nii.reorient(axcodes_to=("P", "I", "R"))
img_scale = nii.rescale((1.5, 5, 1))  # in mm as currently rotated
# resample to an other image
img_resampled_to_other = nii.resample_from_to(img_scale)

nii.get_array()  # get numpy array
nii.affine  # Affine matrix
nii.header  # NIFTY header
nii.orientation  # Orientation in 3-Letters
nii.zoom # Scale of the three image axis
nii.shape #shape
```
### Stitching
Python function and script for arbitrary image stitching. [See Details](TPTBox/stitching/)
### Spineps and Points of Interests (POI) integration
For our Spine segmentation pipline follow the installation of [SPINEPS](https://github.com/Hendrik-code/spineps).

SPINEPS will produce two mask: instance and semantic labels. With these we can compute our POIs. There are either center of mass points or surface points with bioloical meaning. See [Validation of a Patient-Specific Musculoskeletal Model for Lumbar Load Estimation Generated by an Automated Pipeline From Whole Body CT](https://pubmed.ncbi.nlm.nih.gov/35898642/)
```python
from TPTBox import NII, POI, Location, calc_poi_from_subreg_vert
from TPTBox.core.vert_constants import v_name2idx

p = "/dataset-DATASET/derivatives/A/"
semantic_nii = NII.load(f"{p}sub-A_sequ-stitched_acq-sag_mod-T2w_seg-spine_msk.nii.gz", seg=True)
instance_nii = NII.load(f"{p}sub-A_sequ-stitched_acq-sag_mod-T2w_seg-vert_msk.nii.gz", seg=True)
poi_path = f"{p}sub-A_sequ-stitched_acq-sag_mod-T2w_seg-spine_ctd.json"
poi = POI.load(poi_path)
poi = calc_poi_from_subreg_vert(
    instance_nii,
    semantic_nii,
    # buffer_file=poi_path,
    subreg_id=[
        Location.Vertebra_Full,
        Location.Arcus_Vertebrae,
        Location.Spinosus_Process,
        Location.Costal_Process_Left,
        Location.Costal_Process_Right,
        Location.Superior_Articular_Left,
        Location.Superior_Articular_Right,
        Location.Inferior_Articular_Left,
        Location.Inferior_Articular_Right,
        # Location.Vertebra_Corpus_border, CT only
        Location.Vertebra_Corpus,
        Location.Vertebra_Disc,
        Location.Muscle_Inserts_Spinosus_Process,
        Location.Muscle_Inserts_Transverse_Process_Left,
        Location.Muscle_Inserts_Transverse_Process_Right,
        Location.Muscle_Inserts_Vertebral_Body_Left,
        Location.Muscle_Inserts_Vertebral_Body_Right,
        Location.Muscle_Inserts_Articulate_Process_Inferior_Left,
        Location.Muscle_Inserts_Articulate_Process_Inferior_Right,
        Location.Muscle_Inserts_Articulate_Process_Superior_Left,
        Location.Muscle_Inserts_Articulate_Process_Superior_Right,
        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Superior_Median,
        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Superior_Median,
        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Inferior_Median,
        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Inferior_Median,
        Location.Additional_Vertebral_Body_Middle_Superior_Median,
        Location.Additional_Vertebral_Body_Posterior_Central_Median,
        Location.Additional_Vertebral_Body_Middle_Inferior_Median,
        Location.Additional_Vertebral_Body_Anterior_Central_Median,
        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Superior_Left,
        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Superior_Left,
        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Inferior_Left,
        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Inferior_Left,
        Location.Additional_Vertebral_Body_Middle_Superior_Left,
        Location.Additional_Vertebral_Body_Posterior_Central_Left,
        Location.Additional_Vertebral_Body_Middle_Inferior_Left,
        Location.Additional_Vertebral_Body_Anterior_Central_Left,
        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Superior_Right,
        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Superior_Right,
        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Inferior_Right,
        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Inferior_Right,
        Location.Additional_Vertebral_Body_Middle_Superior_Right,
        Location.Additional_Vertebral_Body_Posterior_Central_Right,
        Location.Additional_Vertebral_Body_Middle_Inferior_Right,
        Location.Additional_Vertebral_Body_Anterior_Central_Right,
        Location.Ligament_Attachment_Point_Flava_Superior_Median,
        Location.Ligament_Attachment_Point_Flava_Inferior_Median,
        Location.Vertebra_Direction_Posterior,
        Location.Vertebra_Direction_Inferior,
        Location.Vertebra_Direction_Right,
    ],
)
# poi.save(poi_path)
poi = poi.round(2)
print("Vertebra T4 Vertebra Corpus Center of mass:", poi[v_name2idx["T4"], Location.Vertebra_Corpus])
# rescale/reorante like nii
poi_new = poi.reorient(("P", "I", "R")).rescale((1, 1, 1))
poi_new = poi.resample_from_to(other_nii_or_poi)

```


### Snapshot2D Spine

The snapshot function automatically generates sag, cor, axial cuts in the center of a segmentation.

```python
from TPTBox.spine.snapshot2D import Snapshot_Frame, create_snapshot

ct = Path("Path to CT")
mri = Path("Path to MRI")
vert = Path("Path to Vertebra segmentation")
subreg = Path("Path to Vertebra subregions")
poi_ct = Path("Path to Vertebra poi")
poi_mr = Path("Path to Vertebra poi")

ct_frame = Snapshot_Frame(image=ct, segmentation=vert, centroids=poi_ct, mode="CT", coronal=True, axial=True)
mr_frame = Snapshot_Frame(image=mri, segmentation=vert, centroids=poi_mr, mode="MRI", coronal=True, axial=True)
create_snapshot(snp_path="snapshot.jpg", frames=[ct_frame, mr_frame])
```


### Snapshot3D

Requires additonal python packages: vtk fury xvfbwrapper

```python
from TPTBox.mesh3D.snapshot3D import make_snapshot3D
# all segmentation; view give the rotation of an image
make_snapshot3D("sub-101000_msk.nii.gz","snapshot3D.png",view=["A", "L", "P", "R"])
# Select witch segmentation per panel are chosen.
make_snapshot3D("sub-101000_msk.nii.gz","snapshot3D_v2.png",view=["A"], ids_list=[[1,2],[3]])
```

### Logger

```python
TBD
```

### Point registration with POIs
```python
TBD
```


> [!Note]
> Notably, ...

> [!TIP]
> A Tip

> [!IMPORTANT]
> Importantly


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Hendrik-code/TPTBox",
    "name": "TPTBox",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Robert Graf",
    "author_email": "robert.graf@tum.de",
    "download_url": null,
    "platform": null,
    "description": "[![PyPI version tptbox](https://badge.fury.io/py/tptbox.svg)](https://pypi.python.org/pypi/tptbox/)\n[![Python Versions](https://img.shields.io/pypi/pyversions/tptbox)](https://pypi.org/project/tptbox/)\n[![Stable Version](https://img.shields.io/pypi/v/tptbox?label=stable)](https://pypi.python.org/pypi/tptbox/)\n[![tests](https://github.com/Hendrik-code/TPTBox/actions/workflows/tests.yml/badge.svg)](https://github.com/Hendrik-code/TPTBox/actions/workflows/tests.yml)\n[![codecov](https://codecov.io/gh/Hendrik-code/TPTBox/graph/badge.svg?token=A7FWUKO9Y4)](https://codecov.io/gh/Hendrik-code/TPTBox)\n[![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)\n\n# Torso Processing ToolBox (TPTBox)\n\nThis is a multi-functional package to handle any sort of bids-conform dataset (CT, MRI, ...)\nIt can find, filter, search any BIDS_Family and subjects, and has many functionalities, among them:\n- Easily loop over datasets, and the required files\n- Read, Write Niftys, centroid jsons, ...\n- Reorient, Resample, Shift Niftys, Centroids, labels\n- Modular 2D snapshot generation (different views, MIPs, ...)\n- 3D Mesh generation from segmentation and snapshots from them\n- Registration\n- Logging everything consistently\n- ...\n\n## Install the package\n```bash\nconda create -n 3.10 python=3.10\nconda activate 3.10\npip install TPTBox\n```\n### Install via github:\n(you should be in the project folder)\n```bash\npip install poetry\npoetry install\n```\nor:\nDevelop mode is really, really nice:\n```bash\npip install poetry\npoetry install --with dev\n```\n\n## Functionalities\n\nEach folder in this package represents a different functionality.\n\nThe top-level-hierarchy incorporates the most important files, the BIDS_files.\n\n### BIDS_Files\n\nThis file builds a data model out of the BIDS file names.\nIt can load a dataset as a BIDS_Global_info file, from which search queries and loops over the dataset can be started.\nSee ```tutorial_BIDS_files.ipynb``` for details.\n\n### bids_constants\nDefines constants for the BIDS nomenclature (sequence-splitting keys, naming conventions...)\n\n### vert_constants\n\nContains definitions and sort order for our intern labels, for vertebrae, POI, ...\n\n### Rotation and Resampling\n\nExample rotate and resample.\n\n```python\nfrom TPTBox import NII\n\nnii = NII.load(\"...path/xyz.nii.gz\", seg=True)\n# R right, L left\n# S superior/up, I inferior/down\n# A anterior/front, P posterior/back\nimg_rot = nii.reorient(axcodes_to=(\"P\", \"I\", \"R\"))\nimg_scale = nii.rescale((1.5, 5, 1))  # in mm as currently rotated\n# resample to an other image\nimg_resampled_to_other = nii.resample_from_to(img_scale)\n\nnii.get_array()  # get numpy array\nnii.affine  # Affine matrix\nnii.header  # NIFTY header\nnii.orientation  # Orientation in 3-Letters\nnii.zoom # Scale of the three image axis\nnii.shape #shape\n```\n### Stitching\nPython function and script for arbitrary image stitching. [See Details](TPTBox/stitching/)\n### Spineps and Points of Interests (POI) integration\nFor our Spine segmentation pipline follow the installation of [SPINEPS](https://github.com/Hendrik-code/spineps).\n\nSPINEPS will produce two mask: instance and semantic labels. With these we can compute our POIs. There are either center of mass points or surface points with bioloical meaning. See [Validation of a Patient-Specific Musculoskeletal Model for Lumbar Load Estimation Generated by an Automated Pipeline From Whole Body CT](https://pubmed.ncbi.nlm.nih.gov/35898642/)\n```python\nfrom TPTBox import NII, POI, Location, calc_poi_from_subreg_vert\nfrom TPTBox.core.vert_constants import v_name2idx\n\np = \"/dataset-DATASET/derivatives/A/\"\nsemantic_nii = NII.load(f\"{p}sub-A_sequ-stitched_acq-sag_mod-T2w_seg-spine_msk.nii.gz\", seg=True)\ninstance_nii = NII.load(f\"{p}sub-A_sequ-stitched_acq-sag_mod-T2w_seg-vert_msk.nii.gz\", seg=True)\npoi_path = f\"{p}sub-A_sequ-stitched_acq-sag_mod-T2w_seg-spine_ctd.json\"\npoi = POI.load(poi_path)\npoi = calc_poi_from_subreg_vert(\n    instance_nii,\n    semantic_nii,\n    # buffer_file=poi_path,\n    subreg_id=[\n        Location.Vertebra_Full,\n        Location.Arcus_Vertebrae,\n        Location.Spinosus_Process,\n        Location.Costal_Process_Left,\n        Location.Costal_Process_Right,\n        Location.Superior_Articular_Left,\n        Location.Superior_Articular_Right,\n        Location.Inferior_Articular_Left,\n        Location.Inferior_Articular_Right,\n        # Location.Vertebra_Corpus_border, CT only\n        Location.Vertebra_Corpus,\n        Location.Vertebra_Disc,\n        Location.Muscle_Inserts_Spinosus_Process,\n        Location.Muscle_Inserts_Transverse_Process_Left,\n        Location.Muscle_Inserts_Transverse_Process_Right,\n        Location.Muscle_Inserts_Vertebral_Body_Left,\n        Location.Muscle_Inserts_Vertebral_Body_Right,\n        Location.Muscle_Inserts_Articulate_Process_Inferior_Left,\n        Location.Muscle_Inserts_Articulate_Process_Inferior_Right,\n        Location.Muscle_Inserts_Articulate_Process_Superior_Left,\n        Location.Muscle_Inserts_Articulate_Process_Superior_Right,\n        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Superior_Median,\n        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Superior_Median,\n        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Inferior_Median,\n        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Inferior_Median,\n        Location.Additional_Vertebral_Body_Middle_Superior_Median,\n        Location.Additional_Vertebral_Body_Posterior_Central_Median,\n        Location.Additional_Vertebral_Body_Middle_Inferior_Median,\n        Location.Additional_Vertebral_Body_Anterior_Central_Median,\n        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Superior_Left,\n        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Superior_Left,\n        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Inferior_Left,\n        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Inferior_Left,\n        Location.Additional_Vertebral_Body_Middle_Superior_Left,\n        Location.Additional_Vertebral_Body_Posterior_Central_Left,\n        Location.Additional_Vertebral_Body_Middle_Inferior_Left,\n        Location.Additional_Vertebral_Body_Anterior_Central_Left,\n        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Superior_Right,\n        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Superior_Right,\n        Location.Ligament_Attachment_Point_Anterior_Longitudinal_Inferior_Right,\n        Location.Ligament_Attachment_Point_Posterior_Longitudinal_Inferior_Right,\n        Location.Additional_Vertebral_Body_Middle_Superior_Right,\n        Location.Additional_Vertebral_Body_Posterior_Central_Right,\n        Location.Additional_Vertebral_Body_Middle_Inferior_Right,\n        Location.Additional_Vertebral_Body_Anterior_Central_Right,\n        Location.Ligament_Attachment_Point_Flava_Superior_Median,\n        Location.Ligament_Attachment_Point_Flava_Inferior_Median,\n        Location.Vertebra_Direction_Posterior,\n        Location.Vertebra_Direction_Inferior,\n        Location.Vertebra_Direction_Right,\n    ],\n)\n# poi.save(poi_path)\npoi = poi.round(2)\nprint(\"Vertebra T4 Vertebra Corpus Center of mass:\", poi[v_name2idx[\"T4\"], Location.Vertebra_Corpus])\n# rescale/reorante like nii\npoi_new = poi.reorient((\"P\", \"I\", \"R\")).rescale((1, 1, 1))\npoi_new = poi.resample_from_to(other_nii_or_poi)\n\n```\n\n\n### Snapshot2D Spine\n\nThe snapshot function automatically generates sag, cor, axial cuts in the center of a segmentation.\n\n```python\nfrom TPTBox.spine.snapshot2D import Snapshot_Frame, create_snapshot\n\nct = Path(\"Path to CT\")\nmri = Path(\"Path to MRI\")\nvert = Path(\"Path to Vertebra segmentation\")\nsubreg = Path(\"Path to Vertebra subregions\")\npoi_ct = Path(\"Path to Vertebra poi\")\npoi_mr = Path(\"Path to Vertebra poi\")\n\nct_frame = Snapshot_Frame(image=ct, segmentation=vert, centroids=poi_ct, mode=\"CT\", coronal=True, axial=True)\nmr_frame = Snapshot_Frame(image=mri, segmentation=vert, centroids=poi_mr, mode=\"MRI\", coronal=True, axial=True)\ncreate_snapshot(snp_path=\"snapshot.jpg\", frames=[ct_frame, mr_frame])\n```\n\n\n### Snapshot3D\n\nRequires additonal python packages: vtk fury xvfbwrapper\n\n```python\nfrom TPTBox.mesh3D.snapshot3D import make_snapshot3D\n# all segmentation; view give the rotation of an image\nmake_snapshot3D(\"sub-101000_msk.nii.gz\",\"snapshot3D.png\",view=[\"A\", \"L\", \"P\", \"R\"])\n# Select witch segmentation per panel are chosen.\nmake_snapshot3D(\"sub-101000_msk.nii.gz\",\"snapshot3D_v2.png\",view=[\"A\"], ids_list=[[1,2],[3]])\n```\n\n### Logger\n\n```python\nTBD\n```\n\n### Point registration with POIs\n```python\nTBD\n```\n\n\n> [!Note]\n> Notably, ...\n\n> [!TIP]\n> A Tip\n\n> [!IMPORTANT]\n> Importantly\n\n",
    "bugtrack_url": null,
    "license": "GNU AFFERO GENERAL PUBLIC LICENSE v3.0, 19 November 2007",
    "summary": "A Torso Processing Toolbox capable of processing BIDS-compatible datasets, singular niftys, points of interests, segmentations, and much more.",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "https://github.com/Hendrik-code/TPTBox",
        "Repository": "https://github.com/Hendrik-code/TPTBox"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8ee0316a83becbed45784ba08045d8d646a71d9ebb3bc1dd48d25e61340bc627",
                "md5": "5b6daf549ae828266210e85c7f549f3f",
                "sha256": "153ea22e56600dc9dfbe7215766c49a5a9095ae4ef65b29f84b229e27f3c4cd6"
            },
            "downloads": -1,
            "filename": "tptbox-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5b6daf549ae828266210e85c7f549f3f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 1208613,
            "upload_time": "2024-11-14T10:01:20",
            "upload_time_iso_8601": "2024-11-14T10:01:20.078768Z",
            "url": "https://files.pythonhosted.org/packages/8e/e0/316a83becbed45784ba08045d8d646a71d9ebb3bc1dd48d25e61340bc627/tptbox-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-14 10:01:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Hendrik-code",
    "github_project": "TPTBox",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tptbox"
}
        
Elapsed time: 4.37162s