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

Allow creation of empty mask #15

Merged
merged 4 commits into from
Feb 16, 2023
Merged
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
25 changes: 19 additions & 6 deletions manual_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,13 @@ def create_json(fname_nifti, name_rater):
outfile.write("\n")


def ask_if_modify(fname_label):
def ask_if_modify(fname_label, fname_seg):
"""
Check if file under derivatives already exists. If so, asks user if they want to modify it.
Check if the label file under derivatives already exists. If so, asks user if they want to modify it.
If the label file under derivatives does not exist, copy it from processed data.
If the file under derivatives and the file under processed data do not exist, create a new empty mask.
:param fname_label: file under derivatives
:param fname_seg: file under processed data
:return:
"""
# Check if file under derivatives already exists
Expand All @@ -302,12 +305,19 @@ def ask_if_modify(fname_label):
print("Please answer with 'y' or 'n'")
# We don't want to copy because we want to modify the existing file
copy = False
create_empty_mask = False
# If the file under derivatives does not exist, copy it from processed data
else:
elif not os.path.isfile(fname_label) and os.path.isfile(fname_seg):
do_labeling = True
copy = True
create_empty_mask = False
# If the file under derivatives and the file under processed data do not exist, create a new empty mask
else:
do_labeling = True
copy = False
create_empty_mask = True

return do_labeling, copy
return do_labeling, copy, create_empty_mask


def generate_qc(fname, fname_label, task, fname_qc, subject, config_file):
Expand Down Expand Up @@ -409,13 +419,16 @@ def main():
os.makedirs(os.path.join(path_out_deriv, subject, ses, contrast), exist_ok=True)
if not args.qc_only:
# Check if file under derivatives already exists. If so, asks user if they want to modify it.
do_labeling, copy = ask_if_modify(fname_label)
do_labeling, copy, create_empty_mask = ask_if_modify(fname_label, fname_seg)
# Perform labeling (i.e., segmentation correction, labeling correction etc.) for the specific task
if do_labeling:
# Copy file to derivatives folder
if copy:
# Copy file to derivatives folder
shutil.copyfile(fname_seg, fname_label)
print(f'Copying: {fname_seg} to {fname_label}')
# Create empty mask in derivatives folder
elif create_empty_mask:
utils.create_empty_mask(fname, fname_label)
if task in ['FILES_SEG', 'FILES_GMSEG']:
if not args.add_seg_only:
correct_segmentation(fname, fname_label, args.viewer, args.fsl_color)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coloredlogs==15.0.1
humanfriendly==10.0
PyYAML==6.0
numpy~=1.24.2
nibabel~=5.0.1
16 changes: 16 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import shutil
import yaml

import numpy as np
valosekj marked this conversation as resolved.
Show resolved Hide resolved
import nibabel as nib
valosekj marked this conversation as resolved.
Show resolved Hide resolved


# BIDS utility tool
def fetch_subject_and_session(filename_path):
Expand Down Expand Up @@ -246,3 +249,16 @@ def check_software_installed(list_software=['sct']):
logging.error("'{}' is not installed. Please install it before using this program.".format(software))
install_ok = False
return install_ok


def create_empty_mask(fname, fname_label):
"""
Create empty mask from reference image
:param fname: absolute path to reference image
:param fname_label: absolute path to output mask under derivatives
"""
img = nib.load(fname)
data = np.zeros(img.shape)
img_mask = nib.Nifti1Image(data, affine=img.affine, header=img.header)
nib.save(img_mask, fname_label)
print("Empty mask created at: {}".format(fname_label))