Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inferrer class #5

Closed
wants to merge 55 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
e49ced7
- added poetry.lock to gitignore
MarcelRosier Nov 9, 2023
611fb0e
- created basic abstract inferer class
MarcelRosier Nov 9, 2023
4c7ae56
- rename weights file to match convention
MarcelRosier Nov 9, 2023
5706253
- add refactored version of lib (core)
MarcelRosier Nov 9, 2023
502897f
- fixed bugs
MarcelRosier Nov 9, 2023
0b20c31
- added logging
MarcelRosier Nov 21, 2023
e4db6fd
- changed enums file name to constants
MarcelRosier Nov 21, 2023
0da769c
- added basic cpu/gpu inferer
MarcelRosier Nov 21, 2023
3693d78
- fixed inconsistent naming
MarcelRosier Nov 21, 2023
9e787b9
- fix wrong usage of changed constants
MarcelRosier Nov 21, 2023
b31fbe6
- add input modes and rename to inference mode
MarcelRosier Nov 23, 2023
0ba6395
- add support for nifit and np array inputs
MarcelRosier Nov 23, 2023
1be8264
- add primiotive unittest setup
MarcelRosier Nov 23, 2023
bf4f508
- fixed crash for saving data after np input -> TODO
MarcelRosier Nov 23, 2023
034189f
- add missing type annotations
MarcelRosier Nov 23, 2023
3cc4164
- fix typo
MarcelRosier Nov 28, 2023
c5070cb
Merge branch 'feature/simplify-doc-deps' into feature/inferrer-class
MarcelRosier Nov 28, 2023
a413121
- rm redundant cpu class
MarcelRosier Nov 30, 2023
8c704b0
- add missing enum
MarcelRosier Nov 30, 2023
70c2283
- add more logging
MarcelRosier Nov 30, 2023
85683a0
- migrate to pytest
MarcelRosier Dec 5, 2023
f3fdafd
Merge branch 'feature/inferrer-class' of github.com:BrainLesion/AUROR…
MarcelRosier Dec 5, 2023
a0c76d9
- fix config migration error
MarcelRosier Dec 5, 2023
78c6d8b
- fix mssing config access
MarcelRosier Dec 5, 2023
3f2b72d
- refactorings
MarcelRosier Dec 5, 2023
ce29d05
- fix wrong save file path
MarcelRosier Dec 5, 2023
6ad9149
- correct expected exception
MarcelRosier Dec 5, 2023
a17bf66
Merge branch 'feature/inferrer-class' of github.com:BrainLesion/AUROR…
MarcelRosier Dec 5, 2023
2a01fdc
- fix cpu inference bug
MarcelRosier Dec 5, 2023
e7f88d0
- imporec type error msg
MarcelRosier Dec 5, 2023
46fe98c
- improve output logic
MarcelRosier Dec 5, 2023
ac52c3b
- rm duplicate
MarcelRosier Dec 5, 2023
cbd2740
- bug fix and output improvements
MarcelRosier Dec 5, 2023
b675710
- simplified tests
MarcelRosier Dec 5, 2023
f18153d
- add test for extra outputs and np output
MarcelRosier Dec 5, 2023
ea17875
- add baseconfig
MarcelRosier Dec 5, 2023
4e55ef6
Merge branch 'feature/inferrer-class' of github.com:BrainLesion/AUROR…
MarcelRosier Dec 5, 2023
30a5eee
- improve reference file logic
MarcelRosier Dec 5, 2023
b6ddb34
add author
MarcelRosier Dec 5, 2023
47832d2
- improve test setup
MarcelRosier Dec 5, 2023
0a79fea
- move dataclasses to separate file
MarcelRosier Dec 5, 2023
f5153c9
- moved inferer code into subfolder
MarcelRosier Dec 12, 2023
dc32116
- add basic docker inferer
MarcelRosier Dec 12, 2023
b677caf
- improve logging folder name
MarcelRosier Dec 14, 2023
d34259d
- cleanup docker related code
MarcelRosier Dec 19, 2023
07eeef6
- add docstrings
MarcelRosier Dec 19, 2023
fd33c4c
fixed bug on no inputs
MarcelRosier Dec 19, 2023
d7a0f7a
- add test badge
MarcelRosier Dec 19, 2023
bef6832
- fix duplicate ignore
MarcelRosier Dec 19, 2023
be5f6f4
- rm 3.11 and 3.12 from test action
MarcelRosier Dec 19, 2023
85d64f3
- make dependencies less strict
MarcelRosier Dec 19, 2023
d8a3648
-revert back to only 3.10
MarcelRosier Dec 19, 2023
b593ef7
- test redirect stderr to logs
MarcelRosier Jan 16, 2024
bf3b6b4
Merge branch 'main' into feature/inferrer-class
MarcelRosier Jan 16, 2024
7bb83de
Autoformat with black
brainless-bot[bot] Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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"] #TODO add 3.11 support (for 3.12 torch is not available yet)

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
Expand Down Expand Up @@ -130,5 +129,4 @@ dmypy.json

.vscode
poetry.lock

.DS_Store
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![PyPI version panoptica](https://badge.fury.io/py/brainles-aurora.svg)](https://pypi.python.org/pypi/brainles-aurora/)
[![Documentation Status](https://readthedocs.org/projects/brainles-aurora/badge/?version=latest)](http://brainles-aurora.readthedocs.io/?badge=latest)
[![tests](https://github.com/BrainLesion/AURORA/actions/workflows/tests.yml/badge.svg)](https://github.com/BrainLesion/AURORA/actions/workflows/tests.yml)

# AURORA

Expand Down
2 changes: 1 addition & 1 deletion brainles_aurora/aux.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from path import Path
from pathlib import Path
import os


Expand Down
45 changes: 45 additions & 0 deletions brainles_aurora/inferer/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from enum import Enum


class InferenceMode(str, Enum):
"""Enum representing different modes of inference based on available image inputs."""

T1_T1C_T2_FLA = "t1-t1c-t2-fla"
T1_T1C_FLA = "t1-t1c-fla"
T1_T1C = "t1-t1c"
T1C_FLA = "t1c-fla"
T1C_O = "t1c-o"
FLA_O = "fla-o"
T1_O = "t1-o"


class ModelSelection(str, Enum):
"""Enum representing different strategies for model selection."""

BEST = "best"
LAST = "last"
VANILLA = "vanilla"


class DataMode(str, Enum):
"""Enum representing different modes for handling input and output data.

Enum Values:
NIFTI_FILE (str): Input data is provided as NIFTI file paths/ output is writte to NIFTI files.
NUMPY (str): Input data is provided as NumPy arrays/ output is returned as NumPy arrays.
"""

NIFTI_FILE = "NIFTI_FILEPATH"
NUMPY = "NP_NDARRAY"


# booleans indicate presence of files in order: T1 T1C T2 FLAIR
IMGS_TO_MODE_DICT = {
(True, True, True, True): InferenceMode.T1_T1C_T2_FLA,
(True, True, False, True): InferenceMode.T1_T1C_FLA,
(True, True, False, False): InferenceMode.T1_T1C,
(False, True, False, True): InferenceMode.T1C_FLA,
(False, True, False, False): InferenceMode.T1C_O,
(False, False, False, True): InferenceMode.FLA_O,
(True, False, False, False): InferenceMode.T1_O,
}
68 changes: 68 additions & 0 deletions brainles_aurora/inferer/dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Tuple

import numpy as np

from brainles_aurora.inferer.constants import DataMode, ModelSelection


@dataclass
class BaseConfig:
"""Base configuration for the Aurora model inferer.

Attributes:
output_mode (DataMode, optional): Output mode for the inference results. Defaults to DataMode.NIFTI_FILE.
output_folder (str | Path, optional): Output folder for the results. Defaults to "aurora_output".
log_level (int | str, optional): Logging level. Defaults to logging.INFO.
segmentation_file_name (str, optional): File name for the segmentation result. Defaults to "segmentation.nii.gz". (The segmentation will be saved in output_folder/{timestamp}/segmentation_file_name)
t1 (str | Path | np.ndarray | None, optional): Path or NumPy array for T1 image. Defaults to None.
t1c (str | Path | np.ndarray | None, optional): Path or NumPy array for T1 contrast-enhanced image. Defaults to None.
t2 (str | Path | np.ndarray | None, optional): Path or NumPy array for T2 image. Defaults to None.
fla (str | Path | np.ndarray | None, optional): Path or NumPy array for FLAIR image. Defaults to None.
"""

output_mode: DataMode = DataMode.NIFTI_FILE
output_folder: str | Path = "aurora_output"
segmentation_file_name: str | None = "segmentation.nii.gz"
log_level: int | str = logging.INFO
t1: str | Path | np.ndarray | None = None
t1c: str | Path | np.ndarray | None = None
t2: str | Path | np.ndarray | None = None
fla: str | Path | np.ndarray | None = None


@dataclass
class AuroraInfererConfig(BaseConfig):
"""Configuration for the Aurora model inferer.

Attributes:
output_mode (DataMode, optional): Output mode for the inference results. Defaults to DataMode.NIFTI_FILE.
output_folder (str | Path, optional): Output folder for the results. Defaults to "aurora_output".
segmentation_file_name (str, optional): File name for the segmentation result. Defaults to "segmentation.nii.gz". (The segmentation will be saved in output_folder/{timestamp}/segmentation_file_name)
log_level (int | str, optional): Logging level. Defaults to logging.INFO.
t1 (str | Path | np.ndarray | None, optional): Path or NumPy array for T1 image. Defaults to None.
t1c (str | Path | np.ndarray | None, optional): Path or NumPy array for T1 contrast-enhanced image. Defaults to None.
t2 (str | Path | np.ndarray | None, optional): Path or NumPy array for T2 image. Defaults to None.
fla (str | Path | np.ndarray | None, optional): Path or NumPy array for FLAIR image. Defaults to None.
output_whole_network (bool, optional): Whether to output the whole network results. Defaults to False.
output_metastasis_network (bool, optional): Whether to output the metastasis network results. Defaults to False.
tta (bool, optional): Whether to apply test-time augmentations. Defaults to True.
sliding_window_batch_size (int, optional): Batch size for sliding window inference. Defaults to 1.
workers (int, optional): Number of workers for data loading. Defaults to 0.
threshold (float, optional): Threshold for binarizing the model outputs. Defaults to 0.5.
sliding_window_overlap (float, optional): Overlap ratio for sliding window inference. Defaults to 0.5.
crop_size (Tuple[int, int, int], optional): Crop size for sliding window inference. Defaults to (192, 192, 32).
model_selection (ModelSelection, optional): Model selection strategy. Defaults to ModelSelection.BEST.
"""

output_whole_network: bool = False
output_metastasis_network: bool = False
tta: bool = True
sliding_window_batch_size: int = 1
workers: int = 0
threshold: float = 0.5
sliding_window_overlap: float = 0.5
crop_size: Tuple[int, int, int] = (192, 192, 32)
model_selection: ModelSelection = ModelSelection.BEST
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# copied from https://github.com/Nordgaren/Github-Folder-Downloader/blob/master/gitdl.py
import os
from github import Github, Repository, ContentFile
import requests

import shutil as sh

import requests
from github import ContentFile, Github, Repository


def download(c: ContentFile, out: str):
r = requests.get(c.download_url)
Expand Down
Loading
Loading