2391 lines (2390 with data), 163.2 kB
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../')\n",
"\n",
"import os\n",
"import json\n",
"import time\n",
"from glob import glob\n",
"\n",
"from utils.elastix import excute_cmd, register_elastix, control_points_transformix\n",
"from utils.filemanager import create_directory_if_not_exists, replace_text_in_file, add_and_delete_rows, delete_added_rows\n",
"from utils.landmarks import get_landmarks_from_txt, write_landmarks_to_list\n",
"from utils.metrics import compute_TRE\n",
"from utils.utils import format_elapsed_time\n",
"\n",
"# To allow auto reload to this notebook after modifying any external file imported\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['../dataset/train/copd1/copd1_eBHCT.nii.gz',\n",
" '../dataset/train/copd2/copd2_eBHCT.nii.gz',\n",
" '../dataset/train/copd3/copd3_eBHCT.nii.gz',\n",
" '../dataset/train/copd4/copd4_eBHCT.nii.gz']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_path = '../dataset/train'\n",
"\n",
"# prepare the paths\n",
"exhale_volumes = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*eBHCT.nii.gz\"), recursive=True))]\n",
"inhale_volumes = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*iBHCT.nii.gz\"), recursive=True))]\n",
"\n",
"exhale_seg = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*eBHCT_lung.nii.gz\"), recursive=True))]\n",
"inhale_seg = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*iBHCT_lung.nii.gz\"), recursive=True))]\n",
"\n",
"exhale_volumes"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.affine.txt params command...\n",
"Key for the experiment: Par0003.affine.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.affine.txt -out \"output/Par0003.affine.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 30 seconds\n",
"Transformed landmarks saved to: output/Par0003.affine.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 11.87) (STD TRE: 6.72). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.affine.txt -out \"output/Par0003.affine.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 26 seconds\n",
"Transformed landmarks saved to: output/Par0003.affine.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 10.96) (STD TRE: 4.92). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.affine.txt -out \"output/Par0003.affine.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 31 seconds\n",
"Transformed landmarks saved to: output/Par0003.affine.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 4.41) (STD TRE: 2.93). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.affine.txt -out \"output/Par0003.affine.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 31 seconds\n",
"Transformed landmarks saved to: output/Par0003.affine.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 7.66) (STD TRE: 3.55). \n",
"\n",
"Time for overall registrations: 1 minutes and 59 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.affine.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R1-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R1-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-fg.txt -out \"output/Par0003.bs-R1-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 10 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 25.97) (STD TRE: 11.5). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-fg.txt -out \"output/Par0003.bs-R1-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 8 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 21.48) (STD TRE: 6.47). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-fg.txt -out \"output/Par0003.bs-R1-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 10 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 11.99) (STD TRE: 6.47). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-fg.txt -out \"output/Par0003.bs-R1-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 9 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 29.3) (STD TRE: 13.05). \n",
"\n",
"Time for overall registrations: 0 minutes and 39 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R1-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R1-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R1-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-ug.txt -out \"output/Par0003.bs-R1-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 9 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 25.97) (STD TRE: 11.5). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-ug.txt -out \"output/Par0003.bs-R1-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 8 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 21.48) (STD TRE: 6.47). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-ug.txt -out \"output/Par0003.bs-R1-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 9 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 11.99) (STD TRE: 6.47). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R1-ug.txt -out \"output/Par0003.bs-R1-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 9 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R1-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 29.3) (STD TRE: 13.05). \n",
"\n",
"Time for overall registrations: 0 minutes and 37 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R1-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R2-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R2-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-fg.txt -out \"output/Par0003.bs-R2-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 19 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 25.45) (STD TRE: 11.45). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-fg.txt -out \"output/Par0003.bs-R2-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 15 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 21.05) (STD TRE: 6.55). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-fg.txt -out \"output/Par0003.bs-R2-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 18 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 11.07) (STD TRE: 6.63). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-fg.txt -out \"output/Par0003.bs-R2-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 17 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 28.89) (STD TRE: 13.07). \n",
"\n",
"Time for overall registrations: 1 minutes and 10 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R2-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R2-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R2-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-ug.txt -out \"output/Par0003.bs-R2-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 17 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 23.74) (STD TRE: 11.18). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-ug.txt -out \"output/Par0003.bs-R2-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 15 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 18.93) (STD TRE: 6.88). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-ug.txt -out \"output/Par0003.bs-R2-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 17 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 6.38) (STD TRE: 6.02). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R2-ug.txt -out \"output/Par0003.bs-R2-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 17 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R2-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 27.81) (STD TRE: 13.32). \n",
"\n",
"Time for overall registrations: 1 minutes and 8 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R2-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R3-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R3-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-fg.txt -out \"output/Par0003.bs-R3-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 26 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 24.91) (STD TRE: 11.26). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-fg.txt -out \"output/Par0003.bs-R3-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 23 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 20.56) (STD TRE: 6.87). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-fg.txt -out \"output/Par0003.bs-R3-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 26 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 9.59) (STD TRE: 6.82). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-fg.txt -out \"output/Par0003.bs-R3-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 24 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 28.37) (STD TRE: 13.12). \n",
"\n",
"Time for overall registrations: 1 minutes and 41 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R3-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R3-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R3-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-ug.txt -out \"output/Par0003.bs-R3-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 24 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 13.2) (STD TRE: 9.19). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-ug.txt -out \"output/Par0003.bs-R3-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 21 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 4.41) (STD TRE: 5.15). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-ug.txt -out \"output/Par0003.bs-R3-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 25 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 1.67) (STD TRE: 1.62). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R3-ug.txt -out \"output/Par0003.bs-R3-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 23 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R3-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 24.22) (STD TRE: 10.83). \n",
"\n",
"Time for overall registrations: 1 minutes and 34 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R3-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R4-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R4-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-fg.txt -out \"output/Par0003.bs-R4-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 34 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 24.33) (STD TRE: 10.93). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-fg.txt -out \"output/Par0003.bs-R4-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 29 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 19.9) (STD TRE: 6.81). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-fg.txt -out \"output/Par0003.bs-R4-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 34 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 8.18) (STD TRE: 6.89). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-fg.txt -out \"output/Par0003.bs-R4-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 31 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 28.09) (STD TRE: 12.76). \n",
"\n",
"Time for overall registrations: 2 minutes and 10 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R4-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R4-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R4-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-ug.txt -out \"output/Par0003.bs-R4-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 30 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 2.12) (STD TRE: 2.67). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-ug.txt -out \"output/Par0003.bs-R4-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 27 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 3.58) (STD TRE: 5.25). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-ug.txt -out \"output/Par0003.bs-R4-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 32 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 1.55) (STD TRE: 1.49). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R4-ug.txt -out \"output/Par0003.bs-R4-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 31 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R4-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 1.86) (STD TRE: 1.45). \n",
"\n",
"Time for overall registrations: 2 minutes and 2 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R4-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R5-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R5-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-fg.txt -out \"output/Par0003.bs-R5-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 39 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 23.26) (STD TRE: 10.34). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-fg.txt -out \"output/Par0003.bs-R5-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 35 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 19.37) (STD TRE: 6.62). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-fg.txt -out \"output/Par0003.bs-R5-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 42 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 7.61) (STD TRE: 6.38). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-fg.txt -out \"output/Par0003.bs-R5-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 39 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 27.5) (STD TRE: 12.18). \n",
"\n",
"Time for overall registrations: 2 minutes and 37 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R5-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R5-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R5-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-ug.txt -out \"output/Par0003.bs-R5-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 40 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 2.03) (STD TRE: 2.64). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-ug.txt -out \"output/Par0003.bs-R5-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 35 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 3.58) (STD TRE: 5.3). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-ug.txt -out \"output/Par0003.bs-R5-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 39 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 1.53) (STD TRE: 1.5). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R5-ug.txt -out \"output/Par0003.bs-R5-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 38 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R5-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 1.8) (STD TRE: 1.4). \n",
"\n",
"Time for overall registrations: 2 minutes and 34 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R5-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R6-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R6-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-fg.txt -out \"output/Par0003.bs-R6-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 54 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 22.67) (STD TRE: 9.65). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-fg.txt -out \"output/Par0003.bs-R6-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 45 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 19.29) (STD TRE: 7.28). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-fg.txt -out \"output/Par0003.bs-R6-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 50 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 7.23) (STD TRE: 5.93). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-fg.txt -out \"output/Par0003.bs-R6-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 46 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 26.71) (STD TRE: 11.71). \n",
"\n",
"Time for overall registrations: 3 minutes and 17 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R6-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R6-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R6-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-ug.txt -out \"output/Par0003.bs-R6-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 46 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 2.04) (STD TRE: 2.54). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-ug.txt -out \"output/Par0003.bs-R6-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 40 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 3.67) (STD TRE: 5.46). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-ug.txt -out \"output/Par0003.bs-R6-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 47 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 1.54) (STD TRE: 1.51). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R6-ug.txt -out \"output/Par0003.bs-R6-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 46 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R6-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 1.77) (STD TRE: 1.37). \n",
"\n",
"Time for overall registrations: 3 minutes and 1 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R6-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R7-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R7-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-fg.txt -out \"output/Par0003.bs-R7-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 55 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 22.37) (STD TRE: 9.43). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-fg.txt -out \"output/Par0003.bs-R7-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 50 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 19.28) (STD TRE: 7.45). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-fg.txt -out \"output/Par0003.bs-R7-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 1 minutes and 0 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 7.14) (STD TRE: 5.82). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-fg.txt -out \"output/Par0003.bs-R7-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 54 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 25.81) (STD TRE: 11.66). \n",
"\n",
"Time for overall registrations: 3 minutes and 40 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R7-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R7-ug.txt params command...\n",
"Key for the experiment: Par0003.bs-R7-ug.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-ug.txt -out \"output/Par0003.bs-R7-ug.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 51 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 2.05) (STD TRE: 2.56). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-ug.txt -out \"output/Par0003.bs-R7-ug.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 46 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 3.64) (STD TRE: 5.48). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-ug.txt -out \"output/Par0003.bs-R7-ug.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 54 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 1.53) (STD TRE: 1.48). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R7-ug.txt -out \"output/Par0003.bs-R7-ug.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 53 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R7-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 1.77) (STD TRE: 1.37). \n",
"\n",
"Time for overall registrations: 3 minutes and 26 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R7-ug.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R8-fg.txt params command...\n",
"Key for the experiment: Par0003.bs-R8-fg.txt...\n",
"-----------------------------------------------------------------------------------------------------------------------------------------------\n",
"{'name': 'copd1', 'image_dim': [512, 512, 121], 'voxel_dim': [0.625, 0.625, 2.5], 'features': 773, 'displacement_mean': 25.9, 'displacement_std': 11.57, 'origin': [1, 1, 1]}\n",
"Registering copd1_eBHCT and copd1_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd1/copd1_iBHCT.nii.gz\" -m \"../dataset/train/copd1/copd1_eBHCT.nii.gz\" -fMask ../dataset/train/copd1/copd1_iBHCT_lung.nii.gz -mMask ../dataset/train/copd1/copd1_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R8-fg.txt -out \"output/Par0003.bs-R8-fg.txt/images/output_copd1_iBHCT/copd1_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 1 minutes and 3 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R8-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 25.9) (STD TRE: 11.57).\n",
"TRE (After Registration):- (Mean TRE: 22.45) (STD TRE: 9.52). \n",
"\n",
"{'name': 'copd2', 'image_dim': [512, 512, 102], 'voxel_dim': [0.645, 0.645, 2.5], 'features': 612, 'displacement_mean': 21.77, 'displacement_std': 6.46, 'origin': [1, 1, 1]}\n",
"Registering copd2_eBHCT and copd2_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd2/copd2_iBHCT.nii.gz\" -m \"../dataset/train/copd2/copd2_eBHCT.nii.gz\" -fMask ../dataset/train/copd2/copd2_iBHCT_lung.nii.gz -mMask ../dataset/train/copd2/copd2_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R8-fg.txt -out \"output/Par0003.bs-R8-fg.txt/images/output_copd2_iBHCT/copd2_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 0 minutes and 56 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R8-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 21.77) (STD TRE: 6.46).\n",
"TRE (After Registration):- (Mean TRE: 19.06) (STD TRE: 7.25). \n",
"\n",
"{'name': 'copd3', 'image_dim': [512, 512, 126], 'voxel_dim': [0.652, 0.652, 2.5], 'features': 1172, 'displacement_mean': 12.29, 'displacement_std': 6.39, 'origin': [1, 1, 1]}\n",
"Registering copd3_eBHCT and copd3_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd3/copd3_iBHCT.nii.gz\" -m \"../dataset/train/copd3/copd3_eBHCT.nii.gz\" -fMask ../dataset/train/copd3/copd3_iBHCT_lung.nii.gz -mMask ../dataset/train/copd3/copd3_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R8-fg.txt -out \"output/Par0003.bs-R8-fg.txt/images/output_copd3_iBHCT/copd3_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 1 minutes and 5 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R8-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 12.29) (STD TRE: 6.39).\n",
"TRE (After Registration):- (Mean TRE: 8.15) (STD TRE: 5.75). \n",
"\n",
"{'name': 'copd4', 'image_dim': [512, 512, 126], 'voxel_dim': [0.59, 0.59, 2.5], 'features': 786, 'displacement_mean': 30.9, 'displacement_std': 13.49, 'origin': [1, 1, 1]}\n",
"Registering copd4_eBHCT and copd4_iBHCT...\n",
"Excuting command: elastix -f \"../dataset/train/copd4/copd4_iBHCT.nii.gz\" -m \"../dataset/train/copd4/copd4_eBHCT.nii.gz\" -fMask ../dataset/train/copd4/copd4_iBHCT_lung.nii.gz -mMask ../dataset/train/copd4/copd4_eBHCT_lung.nii.gz -p ../elastix-parameters/Par0003/Par0003.bs-R8-fg.txt -out \"output/Par0003.bs-R8-fg.txt/images/output_copd4_iBHCT/copd4_eBHCT\"\n",
"Accessing the last transform parameter file TransformParameters.0.txt\n",
"Time for current registration loop: 1 minutes and 4 seconds\n",
"Transformed landmarks saved to: output/Par0003.bs-R8-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
"TRE (Before Registration):- (Mean TRE: 30.9) (STD TRE: 13.49).\n",
"TRE (After Registration):- (Mean TRE: 27.31) (STD TRE: 10.6). \n",
"\n",
"Time for overall registrations: 4 minutes and 9 seconds\n"
]
}
],
"source": [
"Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# Setting the experiment registration parameter\n",
"params_list = [\n",
" 'Par0003.bs-R8-fg.txt',\n",
" ]\n",
"\n",
"reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"print(f\"Experimenting using {reg_params} params command...\")\n",
"print(f\"Key for the experiment: {reg_params_key}...\")\n",
"print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"overall_start_time = time.time()\n",
"\n",
"for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
" # get file name\n",
" e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
" i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
" sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
" # load the dataset dictionary\n",
" with open('../dataset/description.json', 'r') as json_file:\n",
" dictionary = json.loads(json_file.read())\n",
" file_information = dictionary['train'][sample_name]\n",
" print(file_information)\n",
"\n",
" # get control points path from dataset dir\n",
" e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
" i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
" # start the time only when the registration starts\n",
" loop_start_time = time.time()\n",
"\n",
" # elastix registration\n",
" print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
" register_elastix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path, \n",
" fMask = i_seg_path,\n",
" mMask= e_seg_path,\n",
" reg_params = reg_params,\n",
" reg_params_key = reg_params_key,\n",
" create_dir_callback = create_directory_if_not_exists,\n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" # transformix control point transformation\n",
" print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
" output_path = control_points_transformix(\n",
" fixed_path = i_path, \n",
" moving_path = e_path,\n",
" reg_params_key = reg_params_key,\n",
" input_points = i_cntl_pt, \n",
" transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
" replace_text_in_file_callback = replace_text_in_file,\n",
" create_dir_callback = create_directory_if_not_exists, \n",
" excute_cmd_callback = excute_cmd)\n",
"\n",
" loop_end_time = time.time()\n",
" loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
" print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
" # get the transformed landmarks\n",
" landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
" transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
" # write the landmarks into the output directory of the points\n",
" output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
" write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
" print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
" # evaluate\n",
" # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
" TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
" print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"overall_end_time = time.time()\n",
"overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# Par_base = '../elastix-parameters/Par0003'\n",
"\n",
"# # Setting the experiment registration parameter\n",
"# params_list = [\n",
"# 'Par0003.bs-R8-ug.txt',\n",
"# ]\n",
"\n",
"# reg_params = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
"# reg_params_key = '+'.join(['{}'.format(param) for param in params_list])\n",
"\n",
"# print(f\"Experimenting using {reg_params} params command...\")\n",
"# print(f\"Key for the experiment: {reg_params_key}...\")\n",
"# print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
"\n",
"# overall_start_time = time.time()\n",
"\n",
"# for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
"# # get file name\n",
"# e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
"# i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
"\n",
"# sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
"\n",
"# # load the dataset dictionary\n",
"# with open('../dataset/description.json', 'r') as json_file:\n",
"# dictionary = json.loads(json_file.read())\n",
"# file_information = dictionary['train'][sample_name]\n",
"# print(file_information)\n",
"\n",
"# # get control points path from dataset dir\n",
"# e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
"# i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
"\n",
"# # start the time only when the registration starts\n",
"# loop_start_time = time.time()\n",
"\n",
"# # elastix registration\n",
"# print(f\"Registering {e_filename_full} and {i_filename_full}...\") \n",
"# register_elastix(\n",
"# fixed_path = i_path, \n",
"# moving_path = e_path, \n",
"# fMask = i_seg_path,\n",
"# mMask= e_seg_path,\n",
"# reg_params = reg_params,\n",
"# reg_params_key = reg_params_key,\n",
"# create_dir_callback = create_directory_if_not_exists,\n",
"# excute_cmd_callback = excute_cmd)\n",
"\n",
"# # transformix control point transformation\n",
"# print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
"# output_path = control_points_transformix(\n",
"# fixed_path = i_path, \n",
"# moving_path = e_path,\n",
"# reg_params_key = reg_params_key,\n",
"# input_points = i_cntl_pt, \n",
"# transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
"# replace_text_in_file_callback = replace_text_in_file,\n",
"# create_dir_callback = create_directory_if_not_exists, \n",
"# excute_cmd_callback = excute_cmd)\n",
"\n",
"# loop_end_time = time.time()\n",
"# loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
"# print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
"\n",
"# # get the transformed landmarks\n",
"# landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
"# transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
"\n",
"# # write the landmarks into the output directory of the points\n",
"# output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
"# write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
"# print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
"\n",
"# # evaluate\n",
"# # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
"# print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
" \n",
"# TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
"# print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
" \n",
"# overall_end_time = time.time()\n",
"# overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
"# print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")\n",
"\n",
"# command fails: Description: itk::ERROR: AdvancedMattesMutualInformationMetric(0000000002B00790): Too many samples map outside moving image buffer: 499 / 2000"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}