Skip to content
Merged
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
26 changes: 19 additions & 7 deletions manual_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ def get_parser():
)
parser.add_argument(
'-json-metadata', metavar="<file>", required=False,
help="R|A custom JSON file containing metadata to be added to the JSON sidecar of all corrected labels. "
help="R|A custom JSON file containing metadata to be added to the output JSON sidecar of all corrected labels. "
"This flag is useful, for example, when a label was obtained automatically and you want to include this "
"information into the JSON sidecar. "
"information into the JSON sidecar. \n"
"NOTE: the script automatically checks whether the JSON file already exists (for example when "
"automatically created by SCT 6.4+). If so, the script will reuse its metadata. In such cases, you do not "
"need to use this flag.\n"
"Below is an example JSON file:\n"
+ dedent(
"""
Expand Down Expand Up @@ -514,11 +517,11 @@ def correct_centerline(fname, fname_label, viewer='sct_get_centerline'):
viewer_not_found(viewer)


def load_custom_json(fname):
def load_json(fname):
"""
Load custom JSON file.
:param fname: path to the custom JSON file.
:return: dictionary with the metadata to be added to the JSON sidecar.
Load existing JSON file. The content of the JSON file will be added to the JSON file produced by this script.
:param fname: path to the existing JSON file.
:return: dictionary with the metadata to be added to the output JSON sidecar.
"""
if not os.path.isfile(fname):
sys.exit("ERROR: The file {} does not exist.".format(fname))
Expand Down Expand Up @@ -794,7 +797,7 @@ def main():
sys.exit("ERROR: No segmentation file found in {}.".format(args.path_label))

# If a custom JSON file containing metadata was provided, load it, and verify that it is a valid JSON file
json_metadata = load_custom_json(args.json_metadata) if args.json_metadata else None
json_metadata = load_json(args.json_metadata) if args.json_metadata else None

# Get name of expert rater (skip if -qc-only is true)
if not args.qc_only:
Expand Down Expand Up @@ -906,6 +909,15 @@ def main():
if copy:
shutil.copyfile(fname_label, fname_out)
print(f'Copying: {fname_label} to {fname_out}')
# If the label has a JSON sidecar, read its content
# Context: SCT v6.4+ produces JSON sidecars for some outputs that track the provenance
# of the function, models, etc.
# Details: https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/4466
# We want to include this information in the final JSON sidecar
fname_label_json = fname_label.replace('.nii.gz', '.json')
if os.path.isfile(fname_label_json):
# Read the JSON file to include the metadata in the final JSON sidecar
json_metadata = load_json(fname_label_json)
# Create empty mask in derivatives folder
elif create_empty_mask:
utils.create_empty_mask(fname, fname_out)
Expand Down
Loading