From b882788010a40811820cda3c8176cc2da6363205 Mon Sep 17 00:00:00 2001 From: iback Date: Fri, 2 Feb 2024 14:47:59 +0000 Subject: [PATCH] replaced BIDS with TPTBox --- .github/workflows/python-publish.yml | 7 ++--- .github/workflows/tests.yml | 40 ++++++++++++++++++++++++++++ pyproject.toml | 3 ++- spineps/entrypoint.py | 6 ++--- spineps/example/helper_parallel.py | 2 +- spineps/example/template_roll_out.py | 20 +++++++------- spineps/models.py | 2 +- spineps/phase_instance.py | 5 ++-- spineps/phase_post.py | 4 +-- spineps/phase_semantic.py | 2 +- spineps/seg_model.py | 2 +- spineps/seg_modelconfig.py | 2 +- spineps/seg_pipeline.py | 6 ++--- spineps/seg_run.py | 4 +-- spineps/seg_utils.py | 2 +- spineps/utils/inference_api.py | 4 +-- spineps/utils/proc_functions.py | 2 +- 17 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index dc7ade3..3f72482 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -17,7 +17,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v3 with: - python-version: '3.x' + python-version: '3.11' - name: Install dependencies run: | python -m pip install --upgrade pip @@ -25,7 +25,8 @@ jobs: - name: Build package run: python -m build - name: Publish package - uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 + uses: pypa/gh-action-pypi-publish@release/v1 #pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 with: - user: Hendrik_Code + #user: Hendrik_Code + verbose: true password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..b9adcfc --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,40 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: tests + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install flake8 pytest + pip install -e . + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest diff --git a/pyproject.toml b/pyproject.toml index 4cce78d..6f47dfa 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry_dynamic_versioning.backend" [tool.poetry] name = "SPINEPS" -version = "0.0.0" +version = "1.0.0" description = "Framework for out-of-the box whole spine MRI segmentation." authors = ["Hendrik Möller "] repository = "https://github.com/Hendrik-code/spineps" @@ -29,6 +29,7 @@ torchmetrics = "^1.1.2" tqdm = "^4.66.1" einops= "^0.6.1" nnunetv2 = "2.2" +tptbox = "*" [tool.poetry-dynamic-versioning] diff --git a/spineps/entrypoint.py b/spineps/entrypoint.py index f4ad3df..99cfae8 100755 --- a/spineps/entrypoint.py +++ b/spineps/entrypoint.py @@ -5,7 +5,7 @@ from pathlib import Path from time import perf_counter -from BIDS import BIDS_FILE, Log_Type, No_Logger +from TPTBox import BIDS_FILE, Log_Type, No_Logger from spineps.models import get_instance_model, get_segmentation_model, get_semantic_model, modelid2folder_instance, modelid2folder_semantic from spineps.seg_run import process_dataset, process_img_nii @@ -214,7 +214,7 @@ def run_sample(opt: Namespace): start_time = perf_counter() if opt.run_cprofiler: - from BIDS.logger.log_file import format_time_short, get_time + from TPTBox.logger.log_file import format_time_short, get_time timestamp = format_time_short(get_time()) cprofile_out = bids_sample.get_changed_path( @@ -286,7 +286,7 @@ def run_dataset(opt: Namespace): } if opt.run_cprofiler: - from BIDS.logger.log_file import format_time_short, get_time + from TPTBox.logger.log_file import format_time_short, get_time start_time = get_time() start_time_short = format_time_short(start_time) diff --git a/spineps/example/helper_parallel.py b/spineps/example/helper_parallel.py index 9cb587b..028df4b 100755 --- a/spineps/example/helper_parallel.py +++ b/spineps/example/helper_parallel.py @@ -5,7 +5,7 @@ sys.path.append(str(file.parents[1])) sys.path.append(str(file.parents[2])) -from BIDS import ( +from TPTBox import ( BIDS_FILE, ) from spineps.models import get_segmentation_model diff --git a/spineps/example/template_roll_out.py b/spineps/example/template_roll_out.py index 51353ef..d17cf94 100755 --- a/spineps/example/template_roll_out.py +++ b/spineps/example/template_roll_out.py @@ -5,7 +5,7 @@ sys.path.append(str(file.parents[1])) sys.path.append(str(file.parents[2])) -from BIDS import ( +from TPTBox import ( BIDS_Global_info, BIDS_FILE, No_Logger, @@ -14,12 +14,12 @@ ) import time from spineps.seg_run import process_img_nii, ErrCode -from spineps.models import MODELS +from spineps.models import get_semantic_model, get_instance_model import numpy as np # INPUT in_ds = Path("DATASET_PATH") -raw = "rawdata" +raw = "rawdata" # TODO der = "derivatives" # TODO head_logger = No_Logger() # (in_ds, log_filename="source-convert-to-unet-train", default_verbose=True) @@ -29,8 +29,8 @@ parent_raw = str(Path(raw).joinpath(str(block))) parent_der = str(Path(der).joinpath(str(block))) -model_subreg = MODELS.T2w_NAKOSPIDER_HIGHRES_CUT().load() -model_vert = MODELS.VERT_HIGHRES().load() +model_semantic = get_semantic_model("Model_name") +model_instance = get_instance_model("Model_name") bids_ds = BIDS_Global_info(datasets=[in_ds], parents=[parent_raw, parent_der], verbose=False) @@ -70,8 +70,8 @@ def injection_function(seg_nii: NII): output_paths, errcode = process_img_nii( img_ref=ref, derivative_name=der, - model_semantic=model_subreg, - model_instance=model_vert, + model_semantic=model_semantic, + model_instance=model_instance, override_semantic=False, override_instance=False, lambda_semantic=injection_function, @@ -89,9 +89,9 @@ def injection_function(seg_nii: NII): # Load Outputs img_nii = ref.open_nii() - seg_nii = NII.load(output_paths["out_spine"], seg=True) # subregion mask - vert_nii = NII.load(output_paths["out_vert"], seg=True) # subregion mask - ctd = POI.load(output_paths["out_ctd"]) # subregion mask + seg_nii = NII.load(output_paths["out_spine"], seg=True) # semantic mask + vert_nii = NII.load(output_paths["out_vert"], seg=True) # instance mask + ctd = POI.load(output_paths["out_ctd"]) # centroid file # TODO do something with outputs, potentially saving them again to the output paths diff --git a/spineps/models.py b/spineps/models.py index aa22350..009e77d 100755 --- a/spineps/models.py +++ b/spineps/models.py @@ -1,7 +1,7 @@ import os from pathlib import Path -from BIDS import Log_Type, No_Logger +from TPTBox import Log_Type, No_Logger from spineps.seg_enums import Modality from spineps.seg_model import Segmentation_Model, modeltype2class diff --git a/spineps/phase_instance.py b/spineps/phase_instance.py index 7b9ebbc..b4e3aac 100755 --- a/spineps/phase_instance.py +++ b/spineps/phase_instance.py @@ -1,8 +1,7 @@ # from utils.predictor import nnUNetPredictor import numpy as np -from BIDS import NII, Location, Log_Type -from BIDS.core.np_utils import np_approx_center_of_mass, np_calc_crop_around_centerpoint, np_dice -from scipy.ndimage import center_of_mass +from TPTBox import NII, Location, Log_Type +from TPTBox.core.np_utils import np_calc_crop_around_centerpoint, np_dice from tqdm import tqdm from spineps.seg_enums import ErrCode, OutputType diff --git a/spineps/phase_post.py b/spineps/phase_post.py index ff0a27c..fa1e497 100644 --- a/spineps/phase_post.py +++ b/spineps/phase_post.py @@ -1,7 +1,7 @@ # from utils.predictor import nnUNetPredictor import numpy as np -from BIDS import NII, Location, Log_Type, v_idx2name, v_name2idx -from BIDS.core.np_utils import np_bbox_nd, np_connected_components, np_dilate_msk, np_map_labels, np_approx_center_of_mass +from TPTBox import NII, Location, Log_Type, v_idx2name, v_name2idx +from TPTBox.core.np_utils import np_bbox_nd, np_connected_components, np_dilate_msk, np_map_labels, np_approx_center_of_mass from scipy.ndimage import center_of_mass from spineps.seg_pipeline import logger, vertebra_subreg_labels diff --git a/spineps/phase_semantic.py b/spineps/phase_semantic.py index f171efc..629e17c 100755 --- a/spineps/phase_semantic.py +++ b/spineps/phase_semantic.py @@ -1,5 +1,5 @@ # from utils.predictor import nnUNetPredictor -from BIDS import NII, Location, Log_Type +from TPTBox import NII, Location, Log_Type import numpy as np from spineps.utils.proc_functions import clean_cc_artifacts, n4_bias from spineps.seg_model import Segmentation_Model diff --git a/spineps/seg_model.py b/spineps/seg_model.py index a8e3dec..99b38c2 100755 --- a/spineps/seg_model.py +++ b/spineps/seg_model.py @@ -1,7 +1,7 @@ from pathlib import Path import os -from BIDS import Image_Reference, NII, to_nii, Logger, No_Logger, Zooms, Log_Type +from TPTBox import Image_Reference, NII, to_nii, Logger, No_Logger, Zooms, Log_Type from spineps.utils.filepaths import search_path import torch from torch import from_numpy diff --git a/spineps/seg_modelconfig.py b/spineps/seg_modelconfig.py index 8db8c57..1a1ac56 100755 --- a/spineps/seg_modelconfig.py +++ b/spineps/seg_modelconfig.py @@ -1,6 +1,6 @@ from pathlib import Path -from BIDS import Zooms, Location, v_name2idx, Ax_Codes, Logger_Interface, Log_Type +from TPTBox import Zooms, Location, v_name2idx, Ax_Codes, Logger_Interface, Log_Type import json from spineps.seg_enums import Modality, Acquisition, ModelType, InputType diff --git a/spineps/seg_pipeline.py b/spineps/seg_pipeline.py index 306a222..a7e6547 100755 --- a/spineps/seg_pipeline.py +++ b/spineps/seg_pipeline.py @@ -1,7 +1,7 @@ # from utils.predictor import nnUNetPredictor -from BIDS import NII, Location, No_Logger, Zooms -from BIDS.core import poi -from BIDS.logger.log_file import get_time, format_time_short +from TPTBox import NII, Location, No_Logger, Zooms +from TPTBox.core import poi +from TPTBox.logger.log_file import get_time, format_time_short from spineps.seg_model import Segmentation_Model import subprocess diff --git a/spineps/seg_run.py b/spineps/seg_run.py index 891f681..5e2c981 100755 --- a/spineps/seg_run.py +++ b/spineps/seg_run.py @@ -6,8 +6,8 @@ import nibabel as nib import numpy as np -from BIDS import BIDS_FILE, NII, BIDS_Global_info, Centroids, Location, Log_Type, Logger -from BIDS.snapshot2D.snapshot_templates import mri_snapshot +from TPTBox import BIDS_FILE, NII, BIDS_Global_info, Centroids, Location, Log_Type, Logger +from TPTBox.spine.snapshot2D.snapshot_templates import mri_snapshot from spineps.phase_instance import predict_instance_mask from spineps.phase_post import phase_postprocess_combined diff --git a/spineps/seg_utils.py b/spineps/seg_utils.py index 146787b..64ae961 100755 --- a/spineps/seg_utils.py +++ b/spineps/seg_utils.py @@ -1,5 +1,5 @@ # from utils.predictor import nnUNetPredictor -from BIDS import BIDS_FILE, Log_Type, Zooms +from TPTBox import BIDS_FILE, Log_Type, Zooms from spineps.seg_model import Segmentation_Model from spineps.seg_pipeline import logger from spineps.seg_enums import Modality, Acquisition diff --git a/spineps/utils/inference_api.py b/spineps/utils/inference_api.py index 2fb8f3a..d98865b 100755 --- a/spineps/utils/inference_api.py +++ b/spineps/utils/inference_api.py @@ -2,8 +2,8 @@ import os.path import torch from spineps.utils.predictor import nnUNetPredictor -from BIDS import NII, No_Logger, Log_Type -from BIDS.core import sitk_utils +from TPTBox import NII, No_Logger, Log_Type +from TPTBox.core import sitk_utils import nibabel as nib import numpy as np diff --git a/spineps/utils/proc_functions.py b/spineps/utils/proc_functions.py index af2e46c..3910473 100755 --- a/spineps/utils/proc_functions.py +++ b/spineps/utils/proc_functions.py @@ -1,5 +1,5 @@ from ants.utils.convert_nibabel import from_nibabel -from BIDS import Logger_Interface, NII, np_utils +from TPTBox import Logger_Interface, NII, np_utils import numpy as np from scipy.ndimage import center_of_mass from tqdm import tqdm