Skip to content

Commit

Permalink
Merge pull request #413 from Deep-MI/dev
Browse files Browse the repository at this point in the history
PR for eTIV fix and new stable release
  • Loading branch information
m-reuter authored Dec 14, 2023
2 parents 8d6e7ee + 5ab400d commit 9f37d02
Show file tree
Hide file tree
Showing 91 changed files with 9,967 additions and 2,748 deletions.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
**/.git
**/.github
checkpoints
Singularity
images
venv
Tutorial
**/*.md
Docker/Dockerfile*
*.txt
venv
/srun_fastsurfer.sh
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/BUILD.info
/.idea/**
47 changes: 47 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,53 @@ Enhancement suggestions are tracked as [GitHub issues](issues).
- **Describe the current behavior** and **explain which behavior you expected to see instead** and why.
- **Explain why this enhancement would be useful** to most users.

## Contributing Code

1. [Fork](https://docs.github.com/en/get-started/quickstart/fork-a-repo) this repository to your github account
2. Clone your fork to your computer (`git clone https://github.com/<username>/FastSurfer.git`)
3. Change into the project directory (`cd FastSurfer`)
4. Add Deep-MI repo as upstream (`git remote add upstream https://github.com/Deep-MI/FastSurfer.git`)
5. Update information from upstream (`git fetch upstream`)
6. Checkout the upstream dev branch (`git checkout -b dev upstream/dev`)
7. Create your feature branch from dev (`git checkout -b my-new-feature`)
8. Commit your changes (`git commit -am 'Add some feature'`)
9. Push to the branch to your github (`git push origin my-new-feature`)
10. Create new pull request on github web interface from that branch into Deep-NI **dev branch** (not into stable, which is default)

If lots of things changed in the meantime or the pull request is showing conflicts you should rebase your branch to the current upstream dev.
This is the preferred way, but only possible if you are the sole develop or your branch:

10. Switch into dev branch (`git checkout dev`)
11. Update your dev branch (`git pull upstream dev`)
12. Switch into your feature (`git chekcout my-new-feature`)
13. Rebase your branch onto dev (`git rebase dev`), resolve conflicts and continue until complete
14. Force push the updated feature branch to your gihub (`git push -f origin my-new-feature`)

If other people co-develop the my-new-feature branch, rewriting history with a rebase is not possible.
Instead you need to merge upstream dev into your branch:

10. Switch into dev branch (`git checkout dev`)
11. Update your dev branch (`git pull upstream dev`)
12. Switch into your feature (`git chekcout my-new-feature`)
13. Merge dev into your feature (`git merge dev`), resolve conflicts and commit
14. Push to origin (`git push origin my-new-feature`)

Either method updates the pull request and resolves conflicts, so that we can merge it once it is complete.
Once the pull request is merged by us you can delete the feature branch in your clone and on your fork:

15. Switch into dev branch (`git checkout dev`)
16. Delete feature branch (`git branch -D my-new-feature`)
17. Delete the branch on your github fork either via GUI, or via command line (`git push origin --delete my-new-feature`)

This procedure will ensure that your local dev branch always follows our dev branch and will never diverge. You can, once in a while, push the dev branch, or similarly update stable and push it to your fork (origin), but that is not really necessary.

Next time you contribute a feature, you do not need to go through the steps 1-6 above, but simply:
- Switch to dev branch (`git checkout dev`)
- Make sure it is identical to upstream (`git pull upstream dev`)
- Check out a new feature branch and continue from 7. above.

Another good command, if for some reasons your dev branch diverged, which should never happen as you never commit to it, you can reset it by `git reset --hard upstream/dev`. Make absolutely sure you are in your dev branch (not the feature branch) and be aware that this will delete any local changes!

## Attribution

This guide is based on the **contributing-gen**. [Make your own](https://github.com/bttger/contributing-gen)!
85 changes: 85 additions & 0 deletions CerebNet/apply_warp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

# Copyright 2022 Image Analysis Lab, German Center for Neurodegenerative Diseases (DZNE), Bonn
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# IMPORTS
from os.path import join
import numpy as np
import nibabel as nib

from CerebNet.datasets import utils


def save_nii_image(img_data, save_path, header, affine):
img_out = nib.Nifti1Image(img_data, header=header, affine=affine)
print(f"Saving {save_path}")
nib.save(img_out, save_path)


def store_warped_data(img_path, lbl_path, warp_path, result_path, patch_size):

img, img_file = utils.load_reorient_rescale_image(img_path)

lbl_file = nib.load(lbl_path)
label = np.asarray(lbl_file.get_fdata(), dtype=np.int16)

warp_field = np.asarray(nib.load(warp_path).get_fdata())
img = utils.map_size(img, base_shape=warp_field.shape[:3])
label = utils.map_size(label, base_shape=warp_field.shape[:3])
warped_img = utils.apply_warp_field(warp_field, img, interpol_order=3)
warped_lbl = utils.apply_warp_field(warp_field, label, interpol_order=0)
utils.map_subseg2label(warped_lbl, label_type='cereb_subseg')
roi = utils.bounding_volume(label, patch_size)

img = utils.map_size(warped_img[roi], patch_size)
label = utils.map_size(warped_lbl[roi], patch_size)

img_file.header['dim'][1:4] = patch_size
img_file.set_data_dtype(img.dtype)
lbl_file.header['dim'][1:4] = patch_size
save_nii_image(img,
join(result_path, "T1_warped_cropped.nii.gz"),
header=img_file.header,
affine=img_file.affine)
save_nii_image(label,
join(result_path, "label_warped_cropped.nii.gz"),
header=lbl_file.header,
affine=lbl_file.affine)


if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--img_path",
help="path to T1 image",
type=str)
parser.add_argument("--lbl_path",
help="path to label image",
type=str)
parser.add_argument("--result_path",
help="folder to store the results",
type=str)

parser.add_argument("--warp_filename",
help="Warp field file",
default='1Warp.nii.gz',
type=str)

args = parser.parse_args()
warp_path = join(args.result_path, args.warp_filename)
store_warped_data(args.img_path,
args.lbl_path,
warp_path=warp_path,
result_path=args.result_path,
patch_size=(128, 128, 128))
8 changes: 5 additions & 3 deletions CerebNet/datasets/realistic_deformations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


# IMPORTS
if ["$ANTSPATH" = "" ] || [ -f "$ANTSPATH/antsRegistrationSyNQuick.sh" ]
if [ "$ANTSPATH" = "" ] || [ -f "$ANTSPATH/antsRegistrationSyNQuick.sh" ]
then
exit "environment \$ANTSPATH not defined or invalid. \$ANTSPATH must contain antsRegistrationSyNQuick.sh."
fi
Expand Down Expand Up @@ -114,14 +114,16 @@ output_dir=$moving_dataroot/subj2subj_reg
mkdir -p $output_dir

if [ "$unlabeled_subject" != "UNDEFINED" ]
then
IFS=',' read -r -a unlabeled_subject_array <<< "$unlabeled_subject"
fi

if [ "$labeled_subject" != "UNDEFINED" ]
then
IFS=',' read -r -a labeled_subject_array <<< "$labeled_subject"
fi

if [ ${#labeled_subject_array} != 1 ] && [ ${#unlabeled_subject_array} != 1 ] && [ ${#labeled_subject_array} != ${#unlabeled_subject_array}]
if [ ${#labeled_subject_array} != 1 ] && [ ${#unlabeled_subject_array} != 1 ] && [ ${#labeled_subject_array} != ${#unlabeled_subject_array} ]
then
exit "invalid parameters for labeled_subject and unlabeled_subject"
fi
Expand Down Expand Up @@ -179,7 +181,7 @@ do
else
echo "$result_path$output_warp already exists, skipping $sub1 -> $sub2."
fi
elif
else
echo "WARNING: $lab_img or $unlab_img does not exist, skipping $sub1 -> $sub2."
fi

Expand Down
11 changes: 6 additions & 5 deletions CerebNet/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

# IMPORTS
import time
from os import makedirs, cpu_count
from os import makedirs
from os.path import join, dirname, isfile
from typing import Dict, List, Tuple, Optional, Literal
from typing import Dict, List, Tuple, Optional
from concurrent.futures import Future, ThreadPoolExecutor

import nibabel as nib
Expand All @@ -26,6 +26,7 @@
from tqdm import tqdm

from FastSurferCNN.utils import logging
from FastSurferCNN.utils.threads import get_num_threads
from FastSurferCNN.utils.mapper import JsonColorLookupTable, TSVLookupTable
from FastSurferCNN.utils.common import (
find_device,
Expand All @@ -34,8 +35,8 @@
NoParallelExecutor,
)
from CerebNet.data_loader.augmentation import ToTensorTest
from CerebNet.data_loader.dataset import SubjectDataset, Plane, LocalizerROI, PLANES
from CerebNet.datasets.utils import crop_transform, load_reorient_lia
from CerebNet.data_loader.dataset import SubjectDataset, Plane, PLANES
from CerebNet.datasets.utils import crop_transform
from CerebNet.models.networks import build_model
from CerebNet.utils import checkpoint as cp

Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(
self.pool = None
self._threads = None
self.threads = threads
torch.set_num_threads(cpu_count() if self._threads is None else self._threads)
torch.set_num_threads(get_num_threads() if self._threads is None else self._threads)
self.pool = (
ThreadPoolExecutor(self._threads) if async_io else NoParallelExecutor()
)
Expand Down
Loading

0 comments on commit 9f37d02

Please sign in to comment.