From 67a3ee3d8862e48ae4d96bb03d2deed3813f885c Mon Sep 17 00:00:00 2001 From: Avnish Date: Mon, 4 Mar 2024 08:22:43 -0500 Subject: [PATCH] BF: Check if LTA is RAS-to-RAS --- samseg/cli/run_samseg_long.py | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/samseg/cli/run_samseg_long.py b/samseg/cli/run_samseg_long.py index a601a5f..e54142e 100644 --- a/samseg/cli/run_samseg_long.py +++ b/samseg/cli/run_samseg_long.py @@ -104,10 +104,54 @@ def main(): else: savePosteriors = args.save_posteriors + # define LTA type - reused from transform.h + type_descriptions = { + 0: "LINEAR_VOX_TO_VOX", + 1: "LINEAR_RAS_TO_RAS", + 2: "LINEAR_PHYSVOX_TO_PHYSVOX", + 10: "TRANSFORM_ARRAY_TYPE", + 11: "MORPH_3D_TYPE", + 12: "MNI_TRANSFORM_TYPE", + 13: "MATLAB_ASCII_TYPE", + 14: "REGISTER_DAT", + 15: "FSLREG_TYPE", + 21: "LINEAR_CORONAL_RAS_TO_CORONAL_RAS", + "LINEAR_VOX_TO_VOX": "LINEAR_VOXEL_TO_VOXEL", + "LINEAR_CORONAL_RAS_TO_CORONAL_RAS": "LINEAR_COR_TO_COR", + } + + def check_lta_file(filepath): + try: + with open(filepath, 'r') as file: + for line in file: + if "type" in line.lower(): + # Extract the type value, accounting for variable spacing around '=' + type_value = line.split('=')[-1].strip() + try: + type_value = int(type_value) + except ValueError: + pass # Keep as string if conversion fails, to handle descriptive keys + + if type_value == 1: + return True, "LTA is of type RAS-to-RAS, proceeding as before." + elif type_value in type_descriptions: + error_message = f"Error: LTA file type is {type_descriptions[type_value]}. Convert to RAS-to-RAS and try again." + return False, error_message + else: + return False, "Error: LTA file type is of an unknown type." + return False, "Error: 'Type' line not found in the file - unable to determine if LTA is of type RAS-to-RAS." + except Exception as e: + return False, f"An error occurred: {str(e)}" + tpToBaseTransforms = [] if args.tp_to_base_transform: for tpTobase in args.tp_to_base_transform: - tpToBaseTransforms.append(sf.load_affine(tpTobase)) + should_continue, message = check_lta_file(tpTobase) + if should_continue: + tpToBaseTransforms.append(sf.load_affine(tpTobase)) + else: + print(message) + sys.exit(1) else: print("Assuming an identity transformation between base and each time point") for tp in args.timepoint: