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

New pipeline function for code usage | Updates on torch version #64

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 21 additions & 21 deletions .github/workflows/macos_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ name: macos build
on: [push, pull_request, workflow_dispatch]

jobs:
build:
runs-on: ["macos-13"]
build:
runs-on: ["macos-latest"]

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install spare scores
run: |
python -m pip cache purge
pip install -r requirements.txt
pip install setuptools twine wheel
python -m pip install .
- name: Run unit tests
run: |
cd tests/ && pytest --cov=../ --cov-report=xml
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: CBICA/NiChart_DLMUSE
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install spare scores
run: |
python -m pip cache purge
pip install -r requirements.txt
pip install setuptools twine wheel
python -m pip install .
- name: Run unit tests
run: |
cd tests/ && pytest --cov=../ --cov-report=xml
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: CBICA/NiChart_DLMUSE
2 changes: 0 additions & 2 deletions NiChart_DLMUSE/SegmentImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import os
import shutil
from typing import Any
import DLMUSE
import DLICV


def run_dlicv(
Expand Down
2 changes: 1 addition & 1 deletion NiChart_DLMUSE/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .dlmuse_pipeline import run_pipeline, run_dlicv, run_dlmuse
from .dlmuse_pipeline import run_dlicv, run_dlmuse, run_ndlmuse_pipeline
114 changes: 17 additions & 97 deletions NiChart_DLMUSE/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@
"""

import argparse
import os
import shutil
import threading

from .dlmuse_pipeline import run_pipeline
from .utils import (
collect_T1,
merge_bids_output_data,
merge_output_data,
remove_subfolders,
split_data,
)

from NiChart_DLMUSE.dlmuse_pipeline import run_ndlmuse_pipeline

# VERSION = pkg_resources.require("NiChart_DLMUSE")[0].version
VERSION = "1.0.7"
VERSION = "1.0.9"


def main() -> None:
Expand Down Expand Up @@ -74,8 +64,7 @@ def main() -> None:

# DEVICE argument
parser.add_argument(
"-d",
"--device",
"-device",
type=str,
help="Device (cpu, cuda, or mps)",
default=None,
Expand Down Expand Up @@ -142,94 +131,25 @@ def main() -> None:
device = args.device
dlicv_extra_args = args.dlicv_args
dlmuse_extra_args = args.dlmuse_args
clear_cache = args.clear_cache
bids = args.bids
cores = args.cores

print()
print("Arguments:")
print(args)
print()

if not os.path.isdir(out_dir):
print(f"Can't find {out_dir}, creating it...")
# os.system(f"mkdir {out_dir}")
os.mkdir(out_dir)

elif len(os.listdir(out_dir)) != 0:
print(f"Emptying output folder: {out_dir}...")
for root, dirs, files in os.walk(out_dir):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))

if args.clear_cache:
os.system("DLICV -i ./dummy -o ./dummy --clear_cache")
os.system("DLMUSE -i ./dummy -o ./dummy --clear_cache")

working_dir = os.path.join(os.path.abspath(out_dir))

# Run pipeline
if args.bids is True:
if int(args.cores) > 1:
collect_T1(in_dir, out_dir)

no_threads = int(args.cores)
subfolders = split_data("raw_temp_T1", no_threads)
threads = []
for i in range(len(subfolders)):
curr_out_dir = out_dir + f"/split_{i}"
curr_thread = threading.Thread(
target=run_pipeline,
args=(
subfolders[i],
curr_out_dir,
device,
dlmuse_extra_args,
dlicv_extra_args,
i,
),
)
curr_thread.start()
threads.append(curr_thread)

for t in threads:
t.join()

merge_bids_output_data(working_dir)
remove_subfolders("raw_temp_T1")
remove_subfolders(out_dir)
else: # No core parallelization
run_pipeline(in_dir, out_dir, device, dlmuse_extra_args, dlicv_extra_args)

else: # Non-BIDS
if int(args.cores) > 1:
no_threads = int(args.cores)
subfolders = split_data(in_dir, no_threads)

threads = []
for i in range(len(subfolders)):
curr_out_dir = out_dir + f"/split_{i}"
curr_thread = threading.Thread(
target=run_pipeline,
args=(
subfolders[i],
curr_out_dir,
device,
dlmuse_extra_args,
dlicv_extra_args,
i,
),
)
curr_thread.start()
threads.append(curr_thread)

for t in threads:
t.join()

merge_output_data(out_dir)
remove_subfolders(in_dir)
remove_subfolders(out_dir)
else: # No core parallelization
run_pipeline(in_dir, out_dir, device, dlmuse_extra_args, dlicv_extra_args)
run_ndlmuse_pipeline(
in_dir,
out_dir,
device,
dlicv_extra_args,
dlmuse_extra_args,
clear_cache,
bids,
cores,
)


if __name__ == "__main__":
Expand Down
146 changes: 135 additions & 11 deletions NiChart_DLMUSE/dlmuse_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import logging
import os
import shutil
import threading
from typing import Any

import pkg_resources # type: ignore

from .CalcROIVol import apply_create_roi_csv, combine_roi_csv
from .MaskImage import apply_combine_masks, apply_mask_img
from .RelabelROI import apply_relabel_rois
from .ReorientImage import apply_reorient_img, apply_reorient_to_init
from .SegmentImage import run_dlicv, run_dlmuse
from .utils import make_img_list
from NiChart_DLMUSE.CalcROIVol import apply_create_roi_csv, combine_roi_csv
from NiChart_DLMUSE.MaskImage import apply_combine_masks, apply_mask_img
from NiChart_DLMUSE.RelabelROI import apply_relabel_rois
from NiChart_DLMUSE.ReorientImage import apply_reorient_img, apply_reorient_to_init
from NiChart_DLMUSE.SegmentImage import run_dlicv, run_dlmuse
from NiChart_DLMUSE.utils import (
collect_T1,
make_img_list,
merge_bids_output_data,
merge_output_data,
remove_subfolders,
split_data,
)

# Config vars
SUFF_LPS = "_LPS.nii.gz"
Expand All @@ -35,17 +45,131 @@
logging.basicConfig(filename="pipeline.log", encoding="utf-8", level=logging.DEBUG)


def run_pipeline(
def run_ndlmuse_pipeline(
in_dir: str,
out_dir: str,
device: str,
dlicv_extra_args: str,
dlmuse_extra_args: str,
clear_cache: bool,
bids: bool,
cores: str,
) -> None:
"""
NiChart pipeline

:param in_dir: The input directory
:type in_dir: str
:param out_dir: The output directory
:type out_dir: str
:type device: cpu/cuda/mps
:param device: str
:param dlicv_extra_args: Extra arguments for DLICV API
:type dlicv_extra_args: str
:param dlmuse_extra_args: Extra arguments for DLMUSE API
:type dlmuse_extra_args: str
:param clear_cache: True if cache should be cleared
:type clear_cache: bool
:param bids: True if your input is a bids type folder
:type bids: bool
:param cores: The number of cores(default is 4)
:type cores: str

:rtype: None
"""
if not os.path.isdir(out_dir):
print(f"Can't find {out_dir}, creating it...")
# os.system(f"mkdir {out_dir}")
os.mkdir(out_dir)

elif len(os.listdir(out_dir)) != 0:
print(f"Emptying output folder: {out_dir}...")
for root, dirs, files in os.walk(out_dir):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
if clear_cache:
os.system("DLICV -i ./dummy -o ./dummy --clear_cache")
os.system("DLMUSE -i ./dummy -o ./dummy --clear_cache")

working_dir = os.path.join(os.path.abspath(out_dir))

if bids is True:
if int(cores) > 1:
collect_T1(in_dir, out_dir)

no_threads = int(cores)
subfolders = split_data("raw_temp_T1", no_threads)
threads = []
for i in range(len(subfolders)):
curr_out_dir = out_dir + f"/split_{i}"
curr_thread = threading.Thread(
target=run_thread,
args=(
subfolders[i],
curr_out_dir,
device,
dlmuse_extra_args,
dlicv_extra_args,
i,
),
)
curr_thread.start()
threads.append(curr_thread)

for t in threads:
t.join()

merge_bids_output_data(working_dir)
remove_subfolders("raw_temp_T1")
remove_subfolders(out_dir)
else: # No core parallelization
run_thread(in_dir, out_dir, device, dlmuse_extra_args, dlicv_extra_args)

else: # Non-BIDS
if int(cores) > 1:
no_threads = int(cores)
subfolders = split_data(in_dir, no_threads)

threads = []
for i in range(len(subfolders)):
curr_out_dir = out_dir + f"/split_{i}"
curr_thread = threading.Thread(
target=run_thread,
args=(
subfolders[i],
curr_out_dir,
device,
dlmuse_extra_args,
dlicv_extra_args,
i,
),
)
curr_thread.start()
threads.append(curr_thread)

for t in threads:
t.join()

merge_output_data(out_dir)
remove_subfolders(in_dir)
remove_subfolders(out_dir)
else: # No core parallelization
run_thread(in_dir, out_dir, device, dlmuse_extra_args, dlicv_extra_args)


def run_thread(
in_data: str,
out_dir: str,
device: str,
dlmuse_extra_args: str = '',
dlicv_extra_args: str = '',
dlmuse_extra_args: str = "",
dlicv_extra_args: str = "",
sub_fldr: int = 1,
progress_bar = None,
progress_bar: Any = None,
) -> None:
"""
NiChart pipeline
Run a thread of the pipeline

:param in_data: the input directory
:type in_data: str
Expand Down
6 changes: 4 additions & 2 deletions NiChart_DLMUSE/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def remove_common_suffix(list_files: list) -> list:

bnames = list_files
if len(list_files) == 1:
if list_files[0].endswith('_T1'): # If there is a single image with suffix _T1, remove it
if list_files[0].endswith(
"_T1"
): # If there is a single image with suffix _T1, remove it
bnames = [x[0:-3] for x in bnames]
return bnames

Expand Down Expand Up @@ -347,7 +349,7 @@ def remove_subfolders(in_dir: str) -> None:

def merge_output_data(in_dir: str) -> None:
"""
Takes all the results from the temp_working_fir and moves them into
Takes all the results from the temp_working_dir and moves them into
the output folder

:param in_dir: the input directory
Expand Down
Loading
Loading