a b/notebooks/4_ElastixTransformix_experimenting.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 1,
6
   "metadata": {},
7
   "outputs": [],
8
   "source": [
9
    "import sys\n",
10
    "sys.path.append('../')\n",
11
    "\n",
12
    "import os\n",
13
    "import json\n",
14
    "import time\n",
15
    "from glob import glob\n",
16
    "\n",
17
    "from utils.elastix import excute_cmd, register_elastix, control_points_transformix\n",
18
    "from utils.filemanager import create_directory_if_not_exists, replace_text_in_file, add_and_delete_rows, delete_added_rows\n",
19
    "from utils.landmarks import get_landmarks_from_txt, write_landmarks_to_list\n",
20
    "from utils.metrics import compute_TRE\n",
21
    "from utils.utils import format_elapsed_time\n",
22
    "\n",
23
    "# To allow auto reload to this notebook after modifying any external file imported\n",
24
    "%load_ext autoreload\n",
25
    "%autoreload 2"
26
   ]
27
  },
28
  {
29
   "cell_type": "code",
30
   "execution_count": 2,
31
   "metadata": {},
32
   "outputs": [
33
    {
34
     "data": {
35
      "text/plain": [
36
       "['../dataset/train/copd1/copd1_eBHCT.nii.gz',\n",
37
       " '../dataset/train/copd2/copd2_eBHCT.nii.gz',\n",
38
       " '../dataset/train/copd3/copd3_eBHCT.nii.gz',\n",
39
       " '../dataset/train/copd4/copd4_eBHCT.nii.gz']"
40
      ]
41
     },
42
     "execution_count": 2,
43
     "metadata": {},
44
     "output_type": "execute_result"
45
    }
46
   ],
47
   "source": [
48
    "train_path = '../dataset/train'\n",
49
    "\n",
50
    "# prepare the paths\n",
51
    "exhale_volumes = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*eBHCT.nii.gz\"), recursive=True))]\n",
52
    "inhale_volumes = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*iBHCT.nii.gz\"), recursive=True))]\n",
53
    "\n",
54
    "exhale_seg = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*eBHCT_lung.nii.gz\"), recursive=True))]\n",
55
    "inhale_seg = [path.replace('\\\\', '/') for path in sorted(glob(os.path.join(train_path, \"***\" , \"*iBHCT_lung.nii.gz\"), recursive=True))]\n",
56
    "\n",
57
    "exhale_volumes"
58
   ]
59
  },
60
  {
61
   "cell_type": "code",
62
   "execution_count": 4,
63
   "metadata": {},
64
   "outputs": [
65
    {
66
     "name": "stdout",
67
     "output_type": "stream",
68
     "text": [
69
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.affine.txt params command...\n",
70
      "Key for the experiment: Par0003.affine.txt...\n",
71
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
72
      "{'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",
73
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
74
      "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",
75
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
76
      "Time for current registration loop: 0 minutes and 30 seconds\n",
77
      "Transformed landmarks saved to:  output/Par0003.affine.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
78
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
79
      "TRE (After Registration):-  (Mean TRE: 11.87) (STD TRE: 6.72). \n",
80
      "\n",
81
      "{'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",
82
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
83
      "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",
84
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
85
      "Time for current registration loop: 0 minutes and 26 seconds\n",
86
      "Transformed landmarks saved to:  output/Par0003.affine.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
87
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
88
      "TRE (After Registration):-  (Mean TRE: 10.96) (STD TRE: 4.92). \n",
89
      "\n",
90
      "{'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",
91
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
92
      "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",
93
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
94
      "Time for current registration loop: 0 minutes and 31 seconds\n",
95
      "Transformed landmarks saved to:  output/Par0003.affine.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
96
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
97
      "TRE (After Registration):-  (Mean TRE: 4.41) (STD TRE: 2.93). \n",
98
      "\n",
99
      "{'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",
100
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
101
      "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",
102
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
103
      "Time for current registration loop: 0 minutes and 31 seconds\n",
104
      "Transformed landmarks saved to:  output/Par0003.affine.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
105
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
106
      "TRE (After Registration):-  (Mean TRE: 7.66) (STD TRE: 3.55). \n",
107
      "\n",
108
      "Time for overall registrations: 1 minutes and 59 seconds\n"
109
     ]
110
    }
111
   ],
112
   "source": [
113
    "Par_base = '../elastix-parameters/Par0003'\n",
114
    "\n",
115
    "# Setting the experiment registration parameter\n",
116
    "params_list = [\n",
117
    "    'Par0003.affine.txt',\n",
118
    "    ]\n",
119
    "\n",
120
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
121
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
122
    "\n",
123
    "print(f\"Experimenting using {reg_params} params command...\")\n",
124
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
125
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
126
    "\n",
127
    "overall_start_time = time.time()\n",
128
    "\n",
129
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
130
    "    # get file name\n",
131
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
132
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
133
    "\n",
134
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
135
    "\n",
136
    "    # load the dataset dictionary\n",
137
    "    with open('../dataset/description.json', 'r') as json_file:\n",
138
    "        dictionary = json.loads(json_file.read())\n",
139
    "    file_information = dictionary['train'][sample_name]\n",
140
    "    print(file_information)\n",
141
    "\n",
142
    "    # get control points path from dataset dir\n",
143
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
144
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
145
    "\n",
146
    "    # start the time only when the registration starts\n",
147
    "    loop_start_time = time.time()\n",
148
    "\n",
149
    "    # elastix registration\n",
150
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
151
    "    register_elastix(\n",
152
    "        fixed_path = i_path, \n",
153
    "        moving_path = e_path, \n",
154
    "        fMask = i_seg_path,\n",
155
    "        mMask= e_seg_path,\n",
156
    "        reg_params = reg_params,\n",
157
    "        reg_params_key = reg_params_key,\n",
158
    "        create_dir_callback = create_directory_if_not_exists,\n",
159
    "        excute_cmd_callback = excute_cmd)\n",
160
    "\n",
161
    "    # transformix control point transformation\n",
162
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
163
    "    output_path = control_points_transformix(\n",
164
    "        fixed_path = i_path, \n",
165
    "        moving_path = e_path,\n",
166
    "        reg_params_key = reg_params_key,\n",
167
    "        input_points = i_cntl_pt, \n",
168
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
169
    "        replace_text_in_file_callback = replace_text_in_file,\n",
170
    "        create_dir_callback = create_directory_if_not_exists, \n",
171
    "        excute_cmd_callback = excute_cmd)\n",
172
    "\n",
173
    "    loop_end_time = time.time()\n",
174
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
175
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
176
    "\n",
177
    "    # get the transformed landmarks\n",
178
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
179
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
180
    "\n",
181
    "    # write the landmarks into the output directory of the points\n",
182
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
183
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
184
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
185
    "\n",
186
    "    # evaluate\n",
187
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
188
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
189
    "    \n",
190
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
191
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
192
    "    \n",
193
    "overall_end_time = time.time()\n",
194
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
195
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
196
   ]
197
  },
198
  {
199
   "cell_type": "code",
200
   "execution_count": 5,
201
   "metadata": {},
202
   "outputs": [
203
    {
204
     "name": "stdout",
205
     "output_type": "stream",
206
     "text": [
207
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R1-fg.txt params command...\n",
208
      "Key for the experiment: Par0003.bs-R1-fg.txt...\n",
209
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
210
      "{'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",
211
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
212
      "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",
213
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
214
      "Time for current registration loop: 0 minutes and 10 seconds\n",
215
      "Transformed landmarks saved to:  output/Par0003.bs-R1-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
216
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
217
      "TRE (After Registration):-  (Mean TRE: 25.97) (STD TRE: 11.5). \n",
218
      "\n",
219
      "{'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",
220
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
221
      "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",
222
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
223
      "Time for current registration loop: 0 minutes and 8 seconds\n",
224
      "Transformed landmarks saved to:  output/Par0003.bs-R1-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
225
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
226
      "TRE (After Registration):-  (Mean TRE: 21.48) (STD TRE: 6.47). \n",
227
      "\n",
228
      "{'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",
229
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
230
      "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",
231
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
232
      "Time for current registration loop: 0 minutes and 10 seconds\n",
233
      "Transformed landmarks saved to:  output/Par0003.bs-R1-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
234
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
235
      "TRE (After Registration):-  (Mean TRE: 11.99) (STD TRE: 6.47). \n",
236
      "\n",
237
      "{'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",
238
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
239
      "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",
240
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
241
      "Time for current registration loop: 0 minutes and 9 seconds\n",
242
      "Transformed landmarks saved to:  output/Par0003.bs-R1-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
243
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
244
      "TRE (After Registration):-  (Mean TRE: 29.3) (STD TRE: 13.05). \n",
245
      "\n",
246
      "Time for overall registrations: 0 minutes and 39 seconds\n"
247
     ]
248
    }
249
   ],
250
   "source": [
251
    "Par_base = '../elastix-parameters/Par0003'\n",
252
    "\n",
253
    "# Setting the experiment registration parameter\n",
254
    "params_list = [\n",
255
    "    'Par0003.bs-R1-fg.txt',\n",
256
    "    ]\n",
257
    "\n",
258
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
259
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
260
    "\n",
261
    "print(f\"Experimenting using {reg_params} params command...\")\n",
262
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
263
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
264
    "\n",
265
    "overall_start_time = time.time()\n",
266
    "\n",
267
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
268
    "    # get file name\n",
269
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
270
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
271
    "\n",
272
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
273
    "\n",
274
    "    # load the dataset dictionary\n",
275
    "    with open('../dataset/description.json', 'r') as json_file:\n",
276
    "        dictionary = json.loads(json_file.read())\n",
277
    "    file_information = dictionary['train'][sample_name]\n",
278
    "    print(file_information)\n",
279
    "\n",
280
    "    # get control points path from dataset dir\n",
281
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
282
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
283
    "\n",
284
    "    # start the time only when the registration starts\n",
285
    "    loop_start_time = time.time()\n",
286
    "\n",
287
    "    # elastix registration\n",
288
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
289
    "    register_elastix(\n",
290
    "        fixed_path = i_path, \n",
291
    "        moving_path = e_path, \n",
292
    "        fMask = i_seg_path,\n",
293
    "        mMask= e_seg_path,\n",
294
    "        reg_params = reg_params,\n",
295
    "        reg_params_key = reg_params_key,\n",
296
    "        create_dir_callback = create_directory_if_not_exists,\n",
297
    "        excute_cmd_callback = excute_cmd)\n",
298
    "\n",
299
    "    # transformix control point transformation\n",
300
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
301
    "    output_path = control_points_transformix(\n",
302
    "        fixed_path = i_path, \n",
303
    "        moving_path = e_path,\n",
304
    "        reg_params_key = reg_params_key,\n",
305
    "        input_points = i_cntl_pt, \n",
306
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
307
    "        replace_text_in_file_callback = replace_text_in_file,\n",
308
    "        create_dir_callback = create_directory_if_not_exists, \n",
309
    "        excute_cmd_callback = excute_cmd)\n",
310
    "\n",
311
    "    loop_end_time = time.time()\n",
312
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
313
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
314
    "\n",
315
    "    # get the transformed landmarks\n",
316
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
317
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
318
    "\n",
319
    "    # write the landmarks into the output directory of the points\n",
320
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
321
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
322
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
323
    "\n",
324
    "    # evaluate\n",
325
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
326
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
327
    "    \n",
328
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
329
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
330
    "    \n",
331
    "overall_end_time = time.time()\n",
332
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
333
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
334
   ]
335
  },
336
  {
337
   "cell_type": "code",
338
   "execution_count": 6,
339
   "metadata": {},
340
   "outputs": [
341
    {
342
     "name": "stdout",
343
     "output_type": "stream",
344
     "text": [
345
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R1-ug.txt params command...\n",
346
      "Key for the experiment: Par0003.bs-R1-ug.txt...\n",
347
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
348
      "{'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",
349
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
350
      "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",
351
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
352
      "Time for current registration loop: 0 minutes and 9 seconds\n",
353
      "Transformed landmarks saved to:  output/Par0003.bs-R1-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
354
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
355
      "TRE (After Registration):-  (Mean TRE: 25.97) (STD TRE: 11.5). \n",
356
      "\n",
357
      "{'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",
358
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
359
      "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",
360
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
361
      "Time for current registration loop: 0 minutes and 8 seconds\n",
362
      "Transformed landmarks saved to:  output/Par0003.bs-R1-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
363
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
364
      "TRE (After Registration):-  (Mean TRE: 21.48) (STD TRE: 6.47). \n",
365
      "\n",
366
      "{'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",
367
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
368
      "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",
369
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
370
      "Time for current registration loop: 0 minutes and 9 seconds\n",
371
      "Transformed landmarks saved to:  output/Par0003.bs-R1-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
372
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
373
      "TRE (After Registration):-  (Mean TRE: 11.99) (STD TRE: 6.47). \n",
374
      "\n",
375
      "{'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",
376
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
377
      "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",
378
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
379
      "Time for current registration loop: 0 minutes and 9 seconds\n",
380
      "Transformed landmarks saved to:  output/Par0003.bs-R1-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
381
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
382
      "TRE (After Registration):-  (Mean TRE: 29.3) (STD TRE: 13.05). \n",
383
      "\n",
384
      "Time for overall registrations: 0 minutes and 37 seconds\n"
385
     ]
386
    }
387
   ],
388
   "source": [
389
    "Par_base = '../elastix-parameters/Par0003'\n",
390
    "\n",
391
    "# Setting the experiment registration parameter\n",
392
    "params_list = [\n",
393
    "    'Par0003.bs-R1-ug.txt',\n",
394
    "    ]\n",
395
    "\n",
396
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
397
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
398
    "\n",
399
    "print(f\"Experimenting using {reg_params} params command...\")\n",
400
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
401
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
402
    "\n",
403
    "overall_start_time = time.time()\n",
404
    "\n",
405
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
406
    "    # get file name\n",
407
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
408
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
409
    "\n",
410
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
411
    "\n",
412
    "    # load the dataset dictionary\n",
413
    "    with open('../dataset/description.json', 'r') as json_file:\n",
414
    "        dictionary = json.loads(json_file.read())\n",
415
    "    file_information = dictionary['train'][sample_name]\n",
416
    "    print(file_information)\n",
417
    "\n",
418
    "    # get control points path from dataset dir\n",
419
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
420
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
421
    "\n",
422
    "    # start the time only when the registration starts\n",
423
    "    loop_start_time = time.time()\n",
424
    "\n",
425
    "    # elastix registration\n",
426
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
427
    "    register_elastix(\n",
428
    "        fixed_path = i_path, \n",
429
    "        moving_path = e_path, \n",
430
    "        fMask = i_seg_path,\n",
431
    "        mMask= e_seg_path,\n",
432
    "        reg_params = reg_params,\n",
433
    "        reg_params_key = reg_params_key,\n",
434
    "        create_dir_callback = create_directory_if_not_exists,\n",
435
    "        excute_cmd_callback = excute_cmd)\n",
436
    "\n",
437
    "    # transformix control point transformation\n",
438
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
439
    "    output_path = control_points_transformix(\n",
440
    "        fixed_path = i_path, \n",
441
    "        moving_path = e_path,\n",
442
    "        reg_params_key = reg_params_key,\n",
443
    "        input_points = i_cntl_pt, \n",
444
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
445
    "        replace_text_in_file_callback = replace_text_in_file,\n",
446
    "        create_dir_callback = create_directory_if_not_exists, \n",
447
    "        excute_cmd_callback = excute_cmd)\n",
448
    "\n",
449
    "    loop_end_time = time.time()\n",
450
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
451
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
452
    "\n",
453
    "    # get the transformed landmarks\n",
454
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
455
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
456
    "\n",
457
    "    # write the landmarks into the output directory of the points\n",
458
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
459
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
460
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
461
    "\n",
462
    "    # evaluate\n",
463
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
464
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
465
    "    \n",
466
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
467
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
468
    "    \n",
469
    "overall_end_time = time.time()\n",
470
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
471
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
472
   ]
473
  },
474
  {
475
   "cell_type": "code",
476
   "execution_count": 7,
477
   "metadata": {},
478
   "outputs": [
479
    {
480
     "name": "stdout",
481
     "output_type": "stream",
482
     "text": [
483
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R2-fg.txt params command...\n",
484
      "Key for the experiment: Par0003.bs-R2-fg.txt...\n",
485
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
486
      "{'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",
487
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
488
      "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",
489
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
490
      "Time for current registration loop: 0 minutes and 19 seconds\n",
491
      "Transformed landmarks saved to:  output/Par0003.bs-R2-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
492
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
493
      "TRE (After Registration):-  (Mean TRE: 25.45) (STD TRE: 11.45). \n",
494
      "\n",
495
      "{'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",
496
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
497
      "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",
498
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
499
      "Time for current registration loop: 0 minutes and 15 seconds\n",
500
      "Transformed landmarks saved to:  output/Par0003.bs-R2-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
501
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
502
      "TRE (After Registration):-  (Mean TRE: 21.05) (STD TRE: 6.55). \n",
503
      "\n",
504
      "{'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",
505
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
506
      "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",
507
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
508
      "Time for current registration loop: 0 minutes and 18 seconds\n",
509
      "Transformed landmarks saved to:  output/Par0003.bs-R2-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
510
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
511
      "TRE (After Registration):-  (Mean TRE: 11.07) (STD TRE: 6.63). \n",
512
      "\n",
513
      "{'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",
514
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
515
      "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",
516
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
517
      "Time for current registration loop: 0 minutes and 17 seconds\n",
518
      "Transformed landmarks saved to:  output/Par0003.bs-R2-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
519
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
520
      "TRE (After Registration):-  (Mean TRE: 28.89) (STD TRE: 13.07). \n",
521
      "\n",
522
      "Time for overall registrations: 1 minutes and 10 seconds\n"
523
     ]
524
    }
525
   ],
526
   "source": [
527
    "Par_base = '../elastix-parameters/Par0003'\n",
528
    "\n",
529
    "# Setting the experiment registration parameter\n",
530
    "params_list = [\n",
531
    "    'Par0003.bs-R2-fg.txt',\n",
532
    "    ]\n",
533
    "\n",
534
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
535
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
536
    "\n",
537
    "print(f\"Experimenting using {reg_params} params command...\")\n",
538
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
539
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
540
    "\n",
541
    "overall_start_time = time.time()\n",
542
    "\n",
543
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
544
    "    # get file name\n",
545
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
546
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
547
    "\n",
548
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
549
    "\n",
550
    "    # load the dataset dictionary\n",
551
    "    with open('../dataset/description.json', 'r') as json_file:\n",
552
    "        dictionary = json.loads(json_file.read())\n",
553
    "    file_information = dictionary['train'][sample_name]\n",
554
    "    print(file_information)\n",
555
    "\n",
556
    "    # get control points path from dataset dir\n",
557
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
558
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
559
    "\n",
560
    "    # start the time only when the registration starts\n",
561
    "    loop_start_time = time.time()\n",
562
    "\n",
563
    "    # elastix registration\n",
564
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
565
    "    register_elastix(\n",
566
    "        fixed_path = i_path, \n",
567
    "        moving_path = e_path, \n",
568
    "        fMask = i_seg_path,\n",
569
    "        mMask= e_seg_path,\n",
570
    "        reg_params = reg_params,\n",
571
    "        reg_params_key = reg_params_key,\n",
572
    "        create_dir_callback = create_directory_if_not_exists,\n",
573
    "        excute_cmd_callback = excute_cmd)\n",
574
    "\n",
575
    "    # transformix control point transformation\n",
576
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
577
    "    output_path = control_points_transformix(\n",
578
    "        fixed_path = i_path, \n",
579
    "        moving_path = e_path,\n",
580
    "        reg_params_key = reg_params_key,\n",
581
    "        input_points = i_cntl_pt, \n",
582
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
583
    "        replace_text_in_file_callback = replace_text_in_file,\n",
584
    "        create_dir_callback = create_directory_if_not_exists, \n",
585
    "        excute_cmd_callback = excute_cmd)\n",
586
    "\n",
587
    "    loop_end_time = time.time()\n",
588
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
589
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
590
    "\n",
591
    "    # get the transformed landmarks\n",
592
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
593
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
594
    "\n",
595
    "    # write the landmarks into the output directory of the points\n",
596
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
597
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
598
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
599
    "\n",
600
    "    # evaluate\n",
601
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
602
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
603
    "    \n",
604
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
605
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
606
    "    \n",
607
    "overall_end_time = time.time()\n",
608
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
609
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
610
   ]
611
  },
612
  {
613
   "cell_type": "code",
614
   "execution_count": 8,
615
   "metadata": {},
616
   "outputs": [
617
    {
618
     "name": "stdout",
619
     "output_type": "stream",
620
     "text": [
621
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R2-ug.txt params command...\n",
622
      "Key for the experiment: Par0003.bs-R2-ug.txt...\n",
623
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
624
      "{'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",
625
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
626
      "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",
627
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
628
      "Time for current registration loop: 0 minutes and 17 seconds\n",
629
      "Transformed landmarks saved to:  output/Par0003.bs-R2-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
630
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
631
      "TRE (After Registration):-  (Mean TRE: 23.74) (STD TRE: 11.18). \n",
632
      "\n",
633
      "{'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",
634
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
635
      "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",
636
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
637
      "Time for current registration loop: 0 minutes and 15 seconds\n",
638
      "Transformed landmarks saved to:  output/Par0003.bs-R2-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
639
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
640
      "TRE (After Registration):-  (Mean TRE: 18.93) (STD TRE: 6.88). \n",
641
      "\n",
642
      "{'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",
643
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
644
      "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",
645
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
646
      "Time for current registration loop: 0 minutes and 17 seconds\n",
647
      "Transformed landmarks saved to:  output/Par0003.bs-R2-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
648
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
649
      "TRE (After Registration):-  (Mean TRE: 6.38) (STD TRE: 6.02). \n",
650
      "\n",
651
      "{'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",
652
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
653
      "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",
654
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
655
      "Time for current registration loop: 0 minutes and 17 seconds\n",
656
      "Transformed landmarks saved to:  output/Par0003.bs-R2-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
657
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
658
      "TRE (After Registration):-  (Mean TRE: 27.81) (STD TRE: 13.32). \n",
659
      "\n",
660
      "Time for overall registrations: 1 minutes and 8 seconds\n"
661
     ]
662
    }
663
   ],
664
   "source": [
665
    "Par_base = '../elastix-parameters/Par0003'\n",
666
    "\n",
667
    "# Setting the experiment registration parameter\n",
668
    "params_list = [\n",
669
    "    'Par0003.bs-R2-ug.txt',\n",
670
    "    ]\n",
671
    "\n",
672
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
673
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
674
    "\n",
675
    "print(f\"Experimenting using {reg_params} params command...\")\n",
676
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
677
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
678
    "\n",
679
    "overall_start_time = time.time()\n",
680
    "\n",
681
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
682
    "    # get file name\n",
683
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
684
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
685
    "\n",
686
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
687
    "\n",
688
    "    # load the dataset dictionary\n",
689
    "    with open('../dataset/description.json', 'r') as json_file:\n",
690
    "        dictionary = json.loads(json_file.read())\n",
691
    "    file_information = dictionary['train'][sample_name]\n",
692
    "    print(file_information)\n",
693
    "\n",
694
    "    # get control points path from dataset dir\n",
695
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
696
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
697
    "\n",
698
    "    # start the time only when the registration starts\n",
699
    "    loop_start_time = time.time()\n",
700
    "\n",
701
    "    # elastix registration\n",
702
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
703
    "    register_elastix(\n",
704
    "        fixed_path = i_path, \n",
705
    "        moving_path = e_path, \n",
706
    "        fMask = i_seg_path,\n",
707
    "        mMask= e_seg_path,\n",
708
    "        reg_params = reg_params,\n",
709
    "        reg_params_key = reg_params_key,\n",
710
    "        create_dir_callback = create_directory_if_not_exists,\n",
711
    "        excute_cmd_callback = excute_cmd)\n",
712
    "\n",
713
    "    # transformix control point transformation\n",
714
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
715
    "    output_path = control_points_transformix(\n",
716
    "        fixed_path = i_path, \n",
717
    "        moving_path = e_path,\n",
718
    "        reg_params_key = reg_params_key,\n",
719
    "        input_points = i_cntl_pt, \n",
720
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
721
    "        replace_text_in_file_callback = replace_text_in_file,\n",
722
    "        create_dir_callback = create_directory_if_not_exists, \n",
723
    "        excute_cmd_callback = excute_cmd)\n",
724
    "\n",
725
    "    loop_end_time = time.time()\n",
726
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
727
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
728
    "\n",
729
    "    # get the transformed landmarks\n",
730
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
731
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
732
    "\n",
733
    "    # write the landmarks into the output directory of the points\n",
734
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
735
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
736
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
737
    "\n",
738
    "    # evaluate\n",
739
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
740
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
741
    "    \n",
742
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
743
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
744
    "    \n",
745
    "overall_end_time = time.time()\n",
746
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
747
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
748
   ]
749
  },
750
  {
751
   "cell_type": "code",
752
   "execution_count": 9,
753
   "metadata": {},
754
   "outputs": [
755
    {
756
     "name": "stdout",
757
     "output_type": "stream",
758
     "text": [
759
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R3-fg.txt params command...\n",
760
      "Key for the experiment: Par0003.bs-R3-fg.txt...\n",
761
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
762
      "{'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",
763
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
764
      "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",
765
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
766
      "Time for current registration loop: 0 minutes and 26 seconds\n",
767
      "Transformed landmarks saved to:  output/Par0003.bs-R3-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
768
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
769
      "TRE (After Registration):-  (Mean TRE: 24.91) (STD TRE: 11.26). \n",
770
      "\n",
771
      "{'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",
772
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
773
      "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",
774
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
775
      "Time for current registration loop: 0 minutes and 23 seconds\n",
776
      "Transformed landmarks saved to:  output/Par0003.bs-R3-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
777
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
778
      "TRE (After Registration):-  (Mean TRE: 20.56) (STD TRE: 6.87). \n",
779
      "\n",
780
      "{'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",
781
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
782
      "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",
783
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
784
      "Time for current registration loop: 0 minutes and 26 seconds\n",
785
      "Transformed landmarks saved to:  output/Par0003.bs-R3-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
786
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
787
      "TRE (After Registration):-  (Mean TRE: 9.59) (STD TRE: 6.82). \n",
788
      "\n",
789
      "{'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",
790
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
791
      "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",
792
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
793
      "Time for current registration loop: 0 minutes and 24 seconds\n",
794
      "Transformed landmarks saved to:  output/Par0003.bs-R3-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
795
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
796
      "TRE (After Registration):-  (Mean TRE: 28.37) (STD TRE: 13.12). \n",
797
      "\n",
798
      "Time for overall registrations: 1 minutes and 41 seconds\n"
799
     ]
800
    }
801
   ],
802
   "source": [
803
    "Par_base = '../elastix-parameters/Par0003'\n",
804
    "\n",
805
    "# Setting the experiment registration parameter\n",
806
    "params_list = [\n",
807
    "    'Par0003.bs-R3-fg.txt',\n",
808
    "    ]\n",
809
    "\n",
810
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
811
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
812
    "\n",
813
    "print(f\"Experimenting using {reg_params} params command...\")\n",
814
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
815
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
816
    "\n",
817
    "overall_start_time = time.time()\n",
818
    "\n",
819
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
820
    "    # get file name\n",
821
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
822
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
823
    "\n",
824
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
825
    "\n",
826
    "    # load the dataset dictionary\n",
827
    "    with open('../dataset/description.json', 'r') as json_file:\n",
828
    "        dictionary = json.loads(json_file.read())\n",
829
    "    file_information = dictionary['train'][sample_name]\n",
830
    "    print(file_information)\n",
831
    "\n",
832
    "    # get control points path from dataset dir\n",
833
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
834
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
835
    "\n",
836
    "    # start the time only when the registration starts\n",
837
    "    loop_start_time = time.time()\n",
838
    "\n",
839
    "    # elastix registration\n",
840
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
841
    "    register_elastix(\n",
842
    "        fixed_path = i_path, \n",
843
    "        moving_path = e_path, \n",
844
    "        fMask = i_seg_path,\n",
845
    "        mMask= e_seg_path,\n",
846
    "        reg_params = reg_params,\n",
847
    "        reg_params_key = reg_params_key,\n",
848
    "        create_dir_callback = create_directory_if_not_exists,\n",
849
    "        excute_cmd_callback = excute_cmd)\n",
850
    "\n",
851
    "    # transformix control point transformation\n",
852
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
853
    "    output_path = control_points_transformix(\n",
854
    "        fixed_path = i_path, \n",
855
    "        moving_path = e_path,\n",
856
    "        reg_params_key = reg_params_key,\n",
857
    "        input_points = i_cntl_pt, \n",
858
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
859
    "        replace_text_in_file_callback = replace_text_in_file,\n",
860
    "        create_dir_callback = create_directory_if_not_exists, \n",
861
    "        excute_cmd_callback = excute_cmd)\n",
862
    "\n",
863
    "    loop_end_time = time.time()\n",
864
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
865
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
866
    "\n",
867
    "    # get the transformed landmarks\n",
868
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
869
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
870
    "\n",
871
    "    # write the landmarks into the output directory of the points\n",
872
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
873
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
874
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
875
    "\n",
876
    "    # evaluate\n",
877
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
878
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
879
    "    \n",
880
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
881
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
882
    "    \n",
883
    "overall_end_time = time.time()\n",
884
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
885
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
886
   ]
887
  },
888
  {
889
   "cell_type": "code",
890
   "execution_count": 10,
891
   "metadata": {},
892
   "outputs": [
893
    {
894
     "name": "stdout",
895
     "output_type": "stream",
896
     "text": [
897
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R3-ug.txt params command...\n",
898
      "Key for the experiment: Par0003.bs-R3-ug.txt...\n",
899
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
900
      "{'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",
901
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
902
      "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",
903
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
904
      "Time for current registration loop: 0 minutes and 24 seconds\n",
905
      "Transformed landmarks saved to:  output/Par0003.bs-R3-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
906
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
907
      "TRE (After Registration):-  (Mean TRE: 13.2) (STD TRE: 9.19). \n",
908
      "\n",
909
      "{'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",
910
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
911
      "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",
912
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
913
      "Time for current registration loop: 0 minutes and 21 seconds\n",
914
      "Transformed landmarks saved to:  output/Par0003.bs-R3-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
915
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
916
      "TRE (After Registration):-  (Mean TRE: 4.41) (STD TRE: 5.15). \n",
917
      "\n",
918
      "{'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",
919
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
920
      "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",
921
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
922
      "Time for current registration loop: 0 minutes and 25 seconds\n",
923
      "Transformed landmarks saved to:  output/Par0003.bs-R3-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
924
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
925
      "TRE (After Registration):-  (Mean TRE: 1.67) (STD TRE: 1.62). \n",
926
      "\n",
927
      "{'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",
928
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
929
      "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",
930
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
931
      "Time for current registration loop: 0 minutes and 23 seconds\n",
932
      "Transformed landmarks saved to:  output/Par0003.bs-R3-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
933
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
934
      "TRE (After Registration):-  (Mean TRE: 24.22) (STD TRE: 10.83). \n",
935
      "\n",
936
      "Time for overall registrations: 1 minutes and 34 seconds\n"
937
     ]
938
    }
939
   ],
940
   "source": [
941
    "Par_base = '../elastix-parameters/Par0003'\n",
942
    "\n",
943
    "# Setting the experiment registration parameter\n",
944
    "params_list = [\n",
945
    "    'Par0003.bs-R3-ug.txt',\n",
946
    "    ]\n",
947
    "\n",
948
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
949
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
950
    "\n",
951
    "print(f\"Experimenting using {reg_params} params command...\")\n",
952
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
953
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
954
    "\n",
955
    "overall_start_time = time.time()\n",
956
    "\n",
957
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
958
    "    # get file name\n",
959
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
960
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
961
    "\n",
962
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
963
    "\n",
964
    "    # load the dataset dictionary\n",
965
    "    with open('../dataset/description.json', 'r') as json_file:\n",
966
    "        dictionary = json.loads(json_file.read())\n",
967
    "    file_information = dictionary['train'][sample_name]\n",
968
    "    print(file_information)\n",
969
    "\n",
970
    "    # get control points path from dataset dir\n",
971
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
972
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
973
    "\n",
974
    "    # start the time only when the registration starts\n",
975
    "    loop_start_time = time.time()\n",
976
    "\n",
977
    "    # elastix registration\n",
978
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
979
    "    register_elastix(\n",
980
    "        fixed_path = i_path, \n",
981
    "        moving_path = e_path, \n",
982
    "        fMask = i_seg_path,\n",
983
    "        mMask= e_seg_path,\n",
984
    "        reg_params = reg_params,\n",
985
    "        reg_params_key = reg_params_key,\n",
986
    "        create_dir_callback = create_directory_if_not_exists,\n",
987
    "        excute_cmd_callback = excute_cmd)\n",
988
    "\n",
989
    "    # transformix control point transformation\n",
990
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
991
    "    output_path = control_points_transformix(\n",
992
    "        fixed_path = i_path, \n",
993
    "        moving_path = e_path,\n",
994
    "        reg_params_key = reg_params_key,\n",
995
    "        input_points = i_cntl_pt, \n",
996
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
997
    "        replace_text_in_file_callback = replace_text_in_file,\n",
998
    "        create_dir_callback = create_directory_if_not_exists, \n",
999
    "        excute_cmd_callback = excute_cmd)\n",
1000
    "\n",
1001
    "    loop_end_time = time.time()\n",
1002
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1003
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1004
    "\n",
1005
    "    # get the transformed landmarks\n",
1006
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1007
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1008
    "\n",
1009
    "    # write the landmarks into the output directory of the points\n",
1010
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1011
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1012
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1013
    "\n",
1014
    "    # evaluate\n",
1015
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1016
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1017
    "    \n",
1018
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1019
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1020
    "    \n",
1021
    "overall_end_time = time.time()\n",
1022
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1023
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1024
   ]
1025
  },
1026
  {
1027
   "cell_type": "code",
1028
   "execution_count": 11,
1029
   "metadata": {},
1030
   "outputs": [
1031
    {
1032
     "name": "stdout",
1033
     "output_type": "stream",
1034
     "text": [
1035
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R4-fg.txt params command...\n",
1036
      "Key for the experiment: Par0003.bs-R4-fg.txt...\n",
1037
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1038
      "{'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",
1039
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1040
      "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",
1041
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1042
      "Time for current registration loop: 0 minutes and 34 seconds\n",
1043
      "Transformed landmarks saved to:  output/Par0003.bs-R4-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1044
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1045
      "TRE (After Registration):-  (Mean TRE: 24.33) (STD TRE: 10.93). \n",
1046
      "\n",
1047
      "{'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",
1048
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1049
      "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",
1050
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1051
      "Time for current registration loop: 0 minutes and 29 seconds\n",
1052
      "Transformed landmarks saved to:  output/Par0003.bs-R4-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1053
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1054
      "TRE (After Registration):-  (Mean TRE: 19.9) (STD TRE: 6.81). \n",
1055
      "\n",
1056
      "{'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",
1057
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1058
      "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",
1059
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1060
      "Time for current registration loop: 0 minutes and 34 seconds\n",
1061
      "Transformed landmarks saved to:  output/Par0003.bs-R4-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1062
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1063
      "TRE (After Registration):-  (Mean TRE: 8.18) (STD TRE: 6.89). \n",
1064
      "\n",
1065
      "{'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",
1066
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1067
      "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",
1068
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1069
      "Time for current registration loop: 0 minutes and 31 seconds\n",
1070
      "Transformed landmarks saved to:  output/Par0003.bs-R4-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1071
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1072
      "TRE (After Registration):-  (Mean TRE: 28.09) (STD TRE: 12.76). \n",
1073
      "\n",
1074
      "Time for overall registrations: 2 minutes and 10 seconds\n"
1075
     ]
1076
    }
1077
   ],
1078
   "source": [
1079
    "Par_base = '../elastix-parameters/Par0003'\n",
1080
    "\n",
1081
    "# Setting the experiment registration parameter\n",
1082
    "params_list = [\n",
1083
    "    'Par0003.bs-R4-fg.txt',\n",
1084
    "    ]\n",
1085
    "\n",
1086
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1087
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1088
    "\n",
1089
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1090
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1091
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1092
    "\n",
1093
    "overall_start_time = time.time()\n",
1094
    "\n",
1095
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1096
    "    # get file name\n",
1097
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1098
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1099
    "\n",
1100
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1101
    "\n",
1102
    "    # load the dataset dictionary\n",
1103
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1104
    "        dictionary = json.loads(json_file.read())\n",
1105
    "    file_information = dictionary['train'][sample_name]\n",
1106
    "    print(file_information)\n",
1107
    "\n",
1108
    "    # get control points path from dataset dir\n",
1109
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1110
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1111
    "\n",
1112
    "    # start the time only when the registration starts\n",
1113
    "    loop_start_time = time.time()\n",
1114
    "\n",
1115
    "    # elastix registration\n",
1116
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1117
    "    register_elastix(\n",
1118
    "        fixed_path = i_path, \n",
1119
    "        moving_path = e_path, \n",
1120
    "        fMask = i_seg_path,\n",
1121
    "        mMask= e_seg_path,\n",
1122
    "        reg_params = reg_params,\n",
1123
    "        reg_params_key = reg_params_key,\n",
1124
    "        create_dir_callback = create_directory_if_not_exists,\n",
1125
    "        excute_cmd_callback = excute_cmd)\n",
1126
    "\n",
1127
    "    # transformix control point transformation\n",
1128
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1129
    "    output_path = control_points_transformix(\n",
1130
    "        fixed_path = i_path, \n",
1131
    "        moving_path = e_path,\n",
1132
    "        reg_params_key = reg_params_key,\n",
1133
    "        input_points = i_cntl_pt, \n",
1134
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1135
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1136
    "        create_dir_callback = create_directory_if_not_exists, \n",
1137
    "        excute_cmd_callback = excute_cmd)\n",
1138
    "\n",
1139
    "    loop_end_time = time.time()\n",
1140
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1141
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1142
    "\n",
1143
    "    # get the transformed landmarks\n",
1144
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1145
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1146
    "\n",
1147
    "    # write the landmarks into the output directory of the points\n",
1148
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1149
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1150
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1151
    "\n",
1152
    "    # evaluate\n",
1153
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1154
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1155
    "    \n",
1156
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1157
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1158
    "    \n",
1159
    "overall_end_time = time.time()\n",
1160
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1161
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1162
   ]
1163
  },
1164
  {
1165
   "cell_type": "code",
1166
   "execution_count": 12,
1167
   "metadata": {},
1168
   "outputs": [
1169
    {
1170
     "name": "stdout",
1171
     "output_type": "stream",
1172
     "text": [
1173
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R4-ug.txt params command...\n",
1174
      "Key for the experiment: Par0003.bs-R4-ug.txt...\n",
1175
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1176
      "{'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",
1177
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1178
      "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",
1179
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1180
      "Time for current registration loop: 0 minutes and 30 seconds\n",
1181
      "Transformed landmarks saved to:  output/Par0003.bs-R4-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1182
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1183
      "TRE (After Registration):-  (Mean TRE: 2.12) (STD TRE: 2.67). \n",
1184
      "\n",
1185
      "{'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",
1186
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1187
      "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",
1188
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1189
      "Time for current registration loop: 0 minutes and 27 seconds\n",
1190
      "Transformed landmarks saved to:  output/Par0003.bs-R4-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1191
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1192
      "TRE (After Registration):-  (Mean TRE: 3.58) (STD TRE: 5.25). \n",
1193
      "\n",
1194
      "{'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",
1195
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1196
      "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",
1197
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1198
      "Time for current registration loop: 0 minutes and 32 seconds\n",
1199
      "Transformed landmarks saved to:  output/Par0003.bs-R4-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1200
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1201
      "TRE (After Registration):-  (Mean TRE: 1.55) (STD TRE: 1.49). \n",
1202
      "\n",
1203
      "{'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",
1204
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1205
      "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",
1206
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1207
      "Time for current registration loop: 0 minutes and 31 seconds\n",
1208
      "Transformed landmarks saved to:  output/Par0003.bs-R4-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1209
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1210
      "TRE (After Registration):-  (Mean TRE: 1.86) (STD TRE: 1.45). \n",
1211
      "\n",
1212
      "Time for overall registrations: 2 minutes and 2 seconds\n"
1213
     ]
1214
    }
1215
   ],
1216
   "source": [
1217
    "Par_base = '../elastix-parameters/Par0003'\n",
1218
    "\n",
1219
    "# Setting the experiment registration parameter\n",
1220
    "params_list = [\n",
1221
    "    'Par0003.bs-R4-ug.txt',\n",
1222
    "    ]\n",
1223
    "\n",
1224
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1225
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1226
    "\n",
1227
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1228
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1229
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1230
    "\n",
1231
    "overall_start_time = time.time()\n",
1232
    "\n",
1233
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1234
    "    # get file name\n",
1235
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1236
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1237
    "\n",
1238
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1239
    "\n",
1240
    "    # load the dataset dictionary\n",
1241
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1242
    "        dictionary = json.loads(json_file.read())\n",
1243
    "    file_information = dictionary['train'][sample_name]\n",
1244
    "    print(file_information)\n",
1245
    "\n",
1246
    "    # get control points path from dataset dir\n",
1247
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1248
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1249
    "\n",
1250
    "    # start the time only when the registration starts\n",
1251
    "    loop_start_time = time.time()\n",
1252
    "\n",
1253
    "    # elastix registration\n",
1254
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1255
    "    register_elastix(\n",
1256
    "        fixed_path = i_path, \n",
1257
    "        moving_path = e_path, \n",
1258
    "        fMask = i_seg_path,\n",
1259
    "        mMask= e_seg_path,\n",
1260
    "        reg_params = reg_params,\n",
1261
    "        reg_params_key = reg_params_key,\n",
1262
    "        create_dir_callback = create_directory_if_not_exists,\n",
1263
    "        excute_cmd_callback = excute_cmd)\n",
1264
    "\n",
1265
    "    # transformix control point transformation\n",
1266
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1267
    "    output_path = control_points_transformix(\n",
1268
    "        fixed_path = i_path, \n",
1269
    "        moving_path = e_path,\n",
1270
    "        reg_params_key = reg_params_key,\n",
1271
    "        input_points = i_cntl_pt, \n",
1272
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1273
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1274
    "        create_dir_callback = create_directory_if_not_exists, \n",
1275
    "        excute_cmd_callback = excute_cmd)\n",
1276
    "\n",
1277
    "    loop_end_time = time.time()\n",
1278
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1279
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1280
    "\n",
1281
    "    # get the transformed landmarks\n",
1282
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1283
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1284
    "\n",
1285
    "    # write the landmarks into the output directory of the points\n",
1286
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1287
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1288
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1289
    "\n",
1290
    "    # evaluate\n",
1291
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1292
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1293
    "    \n",
1294
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1295
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1296
    "    \n",
1297
    "overall_end_time = time.time()\n",
1298
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1299
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1300
   ]
1301
  },
1302
  {
1303
   "cell_type": "code",
1304
   "execution_count": 13,
1305
   "metadata": {},
1306
   "outputs": [
1307
    {
1308
     "name": "stdout",
1309
     "output_type": "stream",
1310
     "text": [
1311
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R5-fg.txt params command...\n",
1312
      "Key for the experiment: Par0003.bs-R5-fg.txt...\n",
1313
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1314
      "{'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",
1315
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1316
      "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",
1317
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1318
      "Time for current registration loop: 0 minutes and 39 seconds\n",
1319
      "Transformed landmarks saved to:  output/Par0003.bs-R5-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1320
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1321
      "TRE (After Registration):-  (Mean TRE: 23.26) (STD TRE: 10.34). \n",
1322
      "\n",
1323
      "{'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",
1324
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1325
      "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",
1326
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1327
      "Time for current registration loop: 0 minutes and 35 seconds\n",
1328
      "Transformed landmarks saved to:  output/Par0003.bs-R5-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1329
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1330
      "TRE (After Registration):-  (Mean TRE: 19.37) (STD TRE: 6.62). \n",
1331
      "\n",
1332
      "{'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",
1333
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1334
      "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",
1335
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1336
      "Time for current registration loop: 0 minutes and 42 seconds\n",
1337
      "Transformed landmarks saved to:  output/Par0003.bs-R5-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1338
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1339
      "TRE (After Registration):-  (Mean TRE: 7.61) (STD TRE: 6.38). \n",
1340
      "\n",
1341
      "{'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",
1342
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1343
      "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",
1344
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1345
      "Time for current registration loop: 0 minutes and 39 seconds\n",
1346
      "Transformed landmarks saved to:  output/Par0003.bs-R5-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1347
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1348
      "TRE (After Registration):-  (Mean TRE: 27.5) (STD TRE: 12.18). \n",
1349
      "\n",
1350
      "Time for overall registrations: 2 minutes and 37 seconds\n"
1351
     ]
1352
    }
1353
   ],
1354
   "source": [
1355
    "Par_base = '../elastix-parameters/Par0003'\n",
1356
    "\n",
1357
    "# Setting the experiment registration parameter\n",
1358
    "params_list = [\n",
1359
    "    'Par0003.bs-R5-fg.txt',\n",
1360
    "    ]\n",
1361
    "\n",
1362
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1363
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1364
    "\n",
1365
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1366
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1367
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1368
    "\n",
1369
    "overall_start_time = time.time()\n",
1370
    "\n",
1371
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1372
    "    # get file name\n",
1373
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1374
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1375
    "\n",
1376
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1377
    "\n",
1378
    "    # load the dataset dictionary\n",
1379
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1380
    "        dictionary = json.loads(json_file.read())\n",
1381
    "    file_information = dictionary['train'][sample_name]\n",
1382
    "    print(file_information)\n",
1383
    "\n",
1384
    "    # get control points path from dataset dir\n",
1385
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1386
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1387
    "\n",
1388
    "    # start the time only when the registration starts\n",
1389
    "    loop_start_time = time.time()\n",
1390
    "\n",
1391
    "    # elastix registration\n",
1392
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1393
    "    register_elastix(\n",
1394
    "        fixed_path = i_path, \n",
1395
    "        moving_path = e_path, \n",
1396
    "        fMask = i_seg_path,\n",
1397
    "        mMask= e_seg_path,\n",
1398
    "        reg_params = reg_params,\n",
1399
    "        reg_params_key = reg_params_key,\n",
1400
    "        create_dir_callback = create_directory_if_not_exists,\n",
1401
    "        excute_cmd_callback = excute_cmd)\n",
1402
    "\n",
1403
    "    # transformix control point transformation\n",
1404
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1405
    "    output_path = control_points_transformix(\n",
1406
    "        fixed_path = i_path, \n",
1407
    "        moving_path = e_path,\n",
1408
    "        reg_params_key = reg_params_key,\n",
1409
    "        input_points = i_cntl_pt, \n",
1410
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1411
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1412
    "        create_dir_callback = create_directory_if_not_exists, \n",
1413
    "        excute_cmd_callback = excute_cmd)\n",
1414
    "\n",
1415
    "    loop_end_time = time.time()\n",
1416
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1417
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1418
    "\n",
1419
    "    # get the transformed landmarks\n",
1420
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1421
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1422
    "\n",
1423
    "    # write the landmarks into the output directory of the points\n",
1424
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1425
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1426
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1427
    "\n",
1428
    "    # evaluate\n",
1429
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1430
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1431
    "    \n",
1432
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1433
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1434
    "    \n",
1435
    "overall_end_time = time.time()\n",
1436
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1437
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1438
   ]
1439
  },
1440
  {
1441
   "cell_type": "code",
1442
   "execution_count": 14,
1443
   "metadata": {},
1444
   "outputs": [
1445
    {
1446
     "name": "stdout",
1447
     "output_type": "stream",
1448
     "text": [
1449
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R5-ug.txt params command...\n",
1450
      "Key for the experiment: Par0003.bs-R5-ug.txt...\n",
1451
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1452
      "{'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",
1453
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1454
      "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",
1455
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1456
      "Time for current registration loop: 0 minutes and 40 seconds\n",
1457
      "Transformed landmarks saved to:  output/Par0003.bs-R5-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1458
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1459
      "TRE (After Registration):-  (Mean TRE: 2.03) (STD TRE: 2.64). \n",
1460
      "\n",
1461
      "{'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",
1462
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1463
      "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",
1464
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1465
      "Time for current registration loop: 0 minutes and 35 seconds\n",
1466
      "Transformed landmarks saved to:  output/Par0003.bs-R5-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1467
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1468
      "TRE (After Registration):-  (Mean TRE: 3.58) (STD TRE: 5.3). \n",
1469
      "\n",
1470
      "{'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",
1471
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1472
      "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",
1473
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1474
      "Time for current registration loop: 0 minutes and 39 seconds\n",
1475
      "Transformed landmarks saved to:  output/Par0003.bs-R5-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1476
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1477
      "TRE (After Registration):-  (Mean TRE: 1.53) (STD TRE: 1.5). \n",
1478
      "\n",
1479
      "{'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",
1480
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1481
      "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",
1482
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1483
      "Time for current registration loop: 0 minutes and 38 seconds\n",
1484
      "Transformed landmarks saved to:  output/Par0003.bs-R5-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1485
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1486
      "TRE (After Registration):-  (Mean TRE: 1.8) (STD TRE: 1.4). \n",
1487
      "\n",
1488
      "Time for overall registrations: 2 minutes and 34 seconds\n"
1489
     ]
1490
    }
1491
   ],
1492
   "source": [
1493
    "Par_base = '../elastix-parameters/Par0003'\n",
1494
    "\n",
1495
    "# Setting the experiment registration parameter\n",
1496
    "params_list = [\n",
1497
    "    'Par0003.bs-R5-ug.txt',\n",
1498
    "    ]\n",
1499
    "\n",
1500
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1501
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1502
    "\n",
1503
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1504
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1505
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1506
    "\n",
1507
    "overall_start_time = time.time()\n",
1508
    "\n",
1509
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1510
    "    # get file name\n",
1511
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1512
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1513
    "\n",
1514
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1515
    "\n",
1516
    "    # load the dataset dictionary\n",
1517
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1518
    "        dictionary = json.loads(json_file.read())\n",
1519
    "    file_information = dictionary['train'][sample_name]\n",
1520
    "    print(file_information)\n",
1521
    "\n",
1522
    "    # get control points path from dataset dir\n",
1523
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1524
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1525
    "\n",
1526
    "    # start the time only when the registration starts\n",
1527
    "    loop_start_time = time.time()\n",
1528
    "\n",
1529
    "    # elastix registration\n",
1530
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1531
    "    register_elastix(\n",
1532
    "        fixed_path = i_path, \n",
1533
    "        moving_path = e_path, \n",
1534
    "        fMask = i_seg_path,\n",
1535
    "        mMask= e_seg_path,\n",
1536
    "        reg_params = reg_params,\n",
1537
    "        reg_params_key = reg_params_key,\n",
1538
    "        create_dir_callback = create_directory_if_not_exists,\n",
1539
    "        excute_cmd_callback = excute_cmd)\n",
1540
    "\n",
1541
    "    # transformix control point transformation\n",
1542
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1543
    "    output_path = control_points_transformix(\n",
1544
    "        fixed_path = i_path, \n",
1545
    "        moving_path = e_path,\n",
1546
    "        reg_params_key = reg_params_key,\n",
1547
    "        input_points = i_cntl_pt, \n",
1548
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1549
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1550
    "        create_dir_callback = create_directory_if_not_exists, \n",
1551
    "        excute_cmd_callback = excute_cmd)\n",
1552
    "\n",
1553
    "    loop_end_time = time.time()\n",
1554
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1555
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1556
    "\n",
1557
    "    # get the transformed landmarks\n",
1558
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1559
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1560
    "\n",
1561
    "    # write the landmarks into the output directory of the points\n",
1562
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1563
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1564
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1565
    "\n",
1566
    "    # evaluate\n",
1567
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1568
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1569
    "    \n",
1570
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1571
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1572
    "    \n",
1573
    "overall_end_time = time.time()\n",
1574
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1575
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1576
   ]
1577
  },
1578
  {
1579
   "cell_type": "code",
1580
   "execution_count": 15,
1581
   "metadata": {},
1582
   "outputs": [
1583
    {
1584
     "name": "stdout",
1585
     "output_type": "stream",
1586
     "text": [
1587
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R6-fg.txt params command...\n",
1588
      "Key for the experiment: Par0003.bs-R6-fg.txt...\n",
1589
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1590
      "{'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",
1591
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1592
      "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",
1593
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1594
      "Time for current registration loop: 0 minutes and 54 seconds\n",
1595
      "Transformed landmarks saved to:  output/Par0003.bs-R6-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1596
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1597
      "TRE (After Registration):-  (Mean TRE: 22.67) (STD TRE: 9.65). \n",
1598
      "\n",
1599
      "{'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",
1600
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1601
      "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",
1602
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1603
      "Time for current registration loop: 0 minutes and 45 seconds\n",
1604
      "Transformed landmarks saved to:  output/Par0003.bs-R6-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1605
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1606
      "TRE (After Registration):-  (Mean TRE: 19.29) (STD TRE: 7.28). \n",
1607
      "\n",
1608
      "{'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",
1609
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1610
      "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",
1611
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1612
      "Time for current registration loop: 0 minutes and 50 seconds\n",
1613
      "Transformed landmarks saved to:  output/Par0003.bs-R6-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1614
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1615
      "TRE (After Registration):-  (Mean TRE: 7.23) (STD TRE: 5.93). \n",
1616
      "\n",
1617
      "{'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",
1618
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1619
      "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",
1620
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1621
      "Time for current registration loop: 0 minutes and 46 seconds\n",
1622
      "Transformed landmarks saved to:  output/Par0003.bs-R6-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1623
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1624
      "TRE (After Registration):-  (Mean TRE: 26.71) (STD TRE: 11.71). \n",
1625
      "\n",
1626
      "Time for overall registrations: 3 minutes and 17 seconds\n"
1627
     ]
1628
    }
1629
   ],
1630
   "source": [
1631
    "Par_base = '../elastix-parameters/Par0003'\n",
1632
    "\n",
1633
    "# Setting the experiment registration parameter\n",
1634
    "params_list = [\n",
1635
    "    'Par0003.bs-R6-fg.txt',\n",
1636
    "    ]\n",
1637
    "\n",
1638
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1639
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1640
    "\n",
1641
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1642
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1643
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1644
    "\n",
1645
    "overall_start_time = time.time()\n",
1646
    "\n",
1647
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1648
    "    # get file name\n",
1649
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1650
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1651
    "\n",
1652
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1653
    "\n",
1654
    "    # load the dataset dictionary\n",
1655
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1656
    "        dictionary = json.loads(json_file.read())\n",
1657
    "    file_information = dictionary['train'][sample_name]\n",
1658
    "    print(file_information)\n",
1659
    "\n",
1660
    "    # get control points path from dataset dir\n",
1661
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1662
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1663
    "\n",
1664
    "    # start the time only when the registration starts\n",
1665
    "    loop_start_time = time.time()\n",
1666
    "\n",
1667
    "    # elastix registration\n",
1668
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1669
    "    register_elastix(\n",
1670
    "        fixed_path = i_path, \n",
1671
    "        moving_path = e_path, \n",
1672
    "        fMask = i_seg_path,\n",
1673
    "        mMask= e_seg_path,\n",
1674
    "        reg_params = reg_params,\n",
1675
    "        reg_params_key = reg_params_key,\n",
1676
    "        create_dir_callback = create_directory_if_not_exists,\n",
1677
    "        excute_cmd_callback = excute_cmd)\n",
1678
    "\n",
1679
    "    # transformix control point transformation\n",
1680
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1681
    "    output_path = control_points_transformix(\n",
1682
    "        fixed_path = i_path, \n",
1683
    "        moving_path = e_path,\n",
1684
    "        reg_params_key = reg_params_key,\n",
1685
    "        input_points = i_cntl_pt, \n",
1686
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1687
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1688
    "        create_dir_callback = create_directory_if_not_exists, \n",
1689
    "        excute_cmd_callback = excute_cmd)\n",
1690
    "\n",
1691
    "    loop_end_time = time.time()\n",
1692
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1693
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1694
    "\n",
1695
    "    # get the transformed landmarks\n",
1696
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1697
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1698
    "\n",
1699
    "    # write the landmarks into the output directory of the points\n",
1700
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1701
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1702
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1703
    "\n",
1704
    "    # evaluate\n",
1705
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1706
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1707
    "    \n",
1708
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1709
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1710
    "    \n",
1711
    "overall_end_time = time.time()\n",
1712
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1713
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1714
   ]
1715
  },
1716
  {
1717
   "cell_type": "code",
1718
   "execution_count": 16,
1719
   "metadata": {},
1720
   "outputs": [
1721
    {
1722
     "name": "stdout",
1723
     "output_type": "stream",
1724
     "text": [
1725
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R6-ug.txt params command...\n",
1726
      "Key for the experiment: Par0003.bs-R6-ug.txt...\n",
1727
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1728
      "{'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",
1729
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1730
      "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",
1731
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1732
      "Time for current registration loop: 0 minutes and 46 seconds\n",
1733
      "Transformed landmarks saved to:  output/Par0003.bs-R6-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1734
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1735
      "TRE (After Registration):-  (Mean TRE: 2.04) (STD TRE: 2.54). \n",
1736
      "\n",
1737
      "{'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",
1738
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1739
      "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",
1740
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1741
      "Time for current registration loop: 0 minutes and 40 seconds\n",
1742
      "Transformed landmarks saved to:  output/Par0003.bs-R6-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1743
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1744
      "TRE (After Registration):-  (Mean TRE: 3.67) (STD TRE: 5.46). \n",
1745
      "\n",
1746
      "{'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",
1747
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1748
      "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",
1749
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1750
      "Time for current registration loop: 0 minutes and 47 seconds\n",
1751
      "Transformed landmarks saved to:  output/Par0003.bs-R6-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1752
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1753
      "TRE (After Registration):-  (Mean TRE: 1.54) (STD TRE: 1.51). \n",
1754
      "\n",
1755
      "{'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",
1756
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1757
      "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",
1758
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1759
      "Time for current registration loop: 0 minutes and 46 seconds\n",
1760
      "Transformed landmarks saved to:  output/Par0003.bs-R6-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1761
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1762
      "TRE (After Registration):-  (Mean TRE: 1.77) (STD TRE: 1.37). \n",
1763
      "\n",
1764
      "Time for overall registrations: 3 minutes and 1 seconds\n"
1765
     ]
1766
    }
1767
   ],
1768
   "source": [
1769
    "Par_base = '../elastix-parameters/Par0003'\n",
1770
    "\n",
1771
    "# Setting the experiment registration parameter\n",
1772
    "params_list = [\n",
1773
    "    'Par0003.bs-R6-ug.txt',\n",
1774
    "    ]\n",
1775
    "\n",
1776
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1777
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1778
    "\n",
1779
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1780
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1781
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1782
    "\n",
1783
    "overall_start_time = time.time()\n",
1784
    "\n",
1785
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1786
    "    # get file name\n",
1787
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1788
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1789
    "\n",
1790
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1791
    "\n",
1792
    "    # load the dataset dictionary\n",
1793
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1794
    "        dictionary = json.loads(json_file.read())\n",
1795
    "    file_information = dictionary['train'][sample_name]\n",
1796
    "    print(file_information)\n",
1797
    "\n",
1798
    "    # get control points path from dataset dir\n",
1799
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1800
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1801
    "\n",
1802
    "    # start the time only when the registration starts\n",
1803
    "    loop_start_time = time.time()\n",
1804
    "\n",
1805
    "    # elastix registration\n",
1806
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1807
    "    register_elastix(\n",
1808
    "        fixed_path = i_path, \n",
1809
    "        moving_path = e_path, \n",
1810
    "        fMask = i_seg_path,\n",
1811
    "        mMask= e_seg_path,\n",
1812
    "        reg_params = reg_params,\n",
1813
    "        reg_params_key = reg_params_key,\n",
1814
    "        create_dir_callback = create_directory_if_not_exists,\n",
1815
    "        excute_cmd_callback = excute_cmd)\n",
1816
    "\n",
1817
    "    # transformix control point transformation\n",
1818
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1819
    "    output_path = control_points_transformix(\n",
1820
    "        fixed_path = i_path, \n",
1821
    "        moving_path = e_path,\n",
1822
    "        reg_params_key = reg_params_key,\n",
1823
    "        input_points = i_cntl_pt, \n",
1824
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1825
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1826
    "        create_dir_callback = create_directory_if_not_exists, \n",
1827
    "        excute_cmd_callback = excute_cmd)\n",
1828
    "\n",
1829
    "    loop_end_time = time.time()\n",
1830
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1831
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1832
    "\n",
1833
    "    # get the transformed landmarks\n",
1834
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1835
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1836
    "\n",
1837
    "    # write the landmarks into the output directory of the points\n",
1838
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1839
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1840
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1841
    "\n",
1842
    "    # evaluate\n",
1843
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1844
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1845
    "    \n",
1846
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1847
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1848
    "    \n",
1849
    "overall_end_time = time.time()\n",
1850
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1851
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1852
   ]
1853
  },
1854
  {
1855
   "cell_type": "code",
1856
   "execution_count": 17,
1857
   "metadata": {},
1858
   "outputs": [
1859
    {
1860
     "name": "stdout",
1861
     "output_type": "stream",
1862
     "text": [
1863
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R7-fg.txt params command...\n",
1864
      "Key for the experiment: Par0003.bs-R7-fg.txt...\n",
1865
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
1866
      "{'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",
1867
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
1868
      "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",
1869
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1870
      "Time for current registration loop: 0 minutes and 55 seconds\n",
1871
      "Transformed landmarks saved to:  output/Par0003.bs-R7-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
1872
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
1873
      "TRE (After Registration):-  (Mean TRE: 22.37) (STD TRE: 9.43). \n",
1874
      "\n",
1875
      "{'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",
1876
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
1877
      "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",
1878
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1879
      "Time for current registration loop: 0 minutes and 50 seconds\n",
1880
      "Transformed landmarks saved to:  output/Par0003.bs-R7-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
1881
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
1882
      "TRE (After Registration):-  (Mean TRE: 19.28) (STD TRE: 7.45). \n",
1883
      "\n",
1884
      "{'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",
1885
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
1886
      "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",
1887
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1888
      "Time for current registration loop: 1 minutes and 0 seconds\n",
1889
      "Transformed landmarks saved to:  output/Par0003.bs-R7-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
1890
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
1891
      "TRE (After Registration):-  (Mean TRE: 7.14) (STD TRE: 5.82). \n",
1892
      "\n",
1893
      "{'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",
1894
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
1895
      "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",
1896
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
1897
      "Time for current registration loop: 0 minutes and 54 seconds\n",
1898
      "Transformed landmarks saved to:  output/Par0003.bs-R7-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
1899
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
1900
      "TRE (After Registration):-  (Mean TRE: 25.81) (STD TRE: 11.66). \n",
1901
      "\n",
1902
      "Time for overall registrations: 3 minutes and 40 seconds\n"
1903
     ]
1904
    }
1905
   ],
1906
   "source": [
1907
    "Par_base = '../elastix-parameters/Par0003'\n",
1908
    "\n",
1909
    "# Setting the experiment registration parameter\n",
1910
    "params_list = [\n",
1911
    "    'Par0003.bs-R7-fg.txt',\n",
1912
    "    ]\n",
1913
    "\n",
1914
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
1915
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
1916
    "\n",
1917
    "print(f\"Experimenting using {reg_params} params command...\")\n",
1918
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
1919
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
1920
    "\n",
1921
    "overall_start_time = time.time()\n",
1922
    "\n",
1923
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
1924
    "    # get file name\n",
1925
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
1926
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
1927
    "\n",
1928
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
1929
    "\n",
1930
    "    # load the dataset dictionary\n",
1931
    "    with open('../dataset/description.json', 'r') as json_file:\n",
1932
    "        dictionary = json.loads(json_file.read())\n",
1933
    "    file_information = dictionary['train'][sample_name]\n",
1934
    "    print(file_information)\n",
1935
    "\n",
1936
    "    # get control points path from dataset dir\n",
1937
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
1938
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
1939
    "\n",
1940
    "    # start the time only when the registration starts\n",
1941
    "    loop_start_time = time.time()\n",
1942
    "\n",
1943
    "    # elastix registration\n",
1944
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
1945
    "    register_elastix(\n",
1946
    "        fixed_path = i_path, \n",
1947
    "        moving_path = e_path, \n",
1948
    "        fMask = i_seg_path,\n",
1949
    "        mMask= e_seg_path,\n",
1950
    "        reg_params = reg_params,\n",
1951
    "        reg_params_key = reg_params_key,\n",
1952
    "        create_dir_callback = create_directory_if_not_exists,\n",
1953
    "        excute_cmd_callback = excute_cmd)\n",
1954
    "\n",
1955
    "    # transformix control point transformation\n",
1956
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
1957
    "    output_path = control_points_transformix(\n",
1958
    "        fixed_path = i_path, \n",
1959
    "        moving_path = e_path,\n",
1960
    "        reg_params_key = reg_params_key,\n",
1961
    "        input_points = i_cntl_pt, \n",
1962
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
1963
    "        replace_text_in_file_callback = replace_text_in_file,\n",
1964
    "        create_dir_callback = create_directory_if_not_exists, \n",
1965
    "        excute_cmd_callback = excute_cmd)\n",
1966
    "\n",
1967
    "    loop_end_time = time.time()\n",
1968
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
1969
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
1970
    "\n",
1971
    "    # get the transformed landmarks\n",
1972
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
1973
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
1974
    "\n",
1975
    "    # write the landmarks into the output directory of the points\n",
1976
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
1977
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
1978
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
1979
    "\n",
1980
    "    # evaluate\n",
1981
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1982
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
1983
    "    \n",
1984
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
1985
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
1986
    "    \n",
1987
    "overall_end_time = time.time()\n",
1988
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
1989
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
1990
   ]
1991
  },
1992
  {
1993
   "cell_type": "code",
1994
   "execution_count": 18,
1995
   "metadata": {},
1996
   "outputs": [
1997
    {
1998
     "name": "stdout",
1999
     "output_type": "stream",
2000
     "text": [
2001
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R7-ug.txt params command...\n",
2002
      "Key for the experiment: Par0003.bs-R7-ug.txt...\n",
2003
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
2004
      "{'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",
2005
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
2006
      "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",
2007
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2008
      "Time for current registration loop: 0 minutes and 51 seconds\n",
2009
      "Transformed landmarks saved to:  output/Par0003.bs-R7-ug.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
2010
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
2011
      "TRE (After Registration):-  (Mean TRE: 2.05) (STD TRE: 2.56). \n",
2012
      "\n",
2013
      "{'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",
2014
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
2015
      "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",
2016
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2017
      "Time for current registration loop: 0 minutes and 46 seconds\n",
2018
      "Transformed landmarks saved to:  output/Par0003.bs-R7-ug.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
2019
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
2020
      "TRE (After Registration):-  (Mean TRE: 3.64) (STD TRE: 5.48). \n",
2021
      "\n",
2022
      "{'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",
2023
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
2024
      "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",
2025
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2026
      "Time for current registration loop: 0 minutes and 54 seconds\n",
2027
      "Transformed landmarks saved to:  output/Par0003.bs-R7-ug.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
2028
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
2029
      "TRE (After Registration):-  (Mean TRE: 1.53) (STD TRE: 1.48). \n",
2030
      "\n",
2031
      "{'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",
2032
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
2033
      "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",
2034
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2035
      "Time for current registration loop: 0 minutes and 53 seconds\n",
2036
      "Transformed landmarks saved to:  output/Par0003.bs-R7-ug.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
2037
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
2038
      "TRE (After Registration):-  (Mean TRE: 1.77) (STD TRE: 1.37). \n",
2039
      "\n",
2040
      "Time for overall registrations: 3 minutes and 26 seconds\n"
2041
     ]
2042
    }
2043
   ],
2044
   "source": [
2045
    "Par_base = '../elastix-parameters/Par0003'\n",
2046
    "\n",
2047
    "# Setting the experiment registration parameter\n",
2048
    "params_list = [\n",
2049
    "    'Par0003.bs-R7-ug.txt',\n",
2050
    "    ]\n",
2051
    "\n",
2052
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
2053
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
2054
    "\n",
2055
    "print(f\"Experimenting using {reg_params} params command...\")\n",
2056
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
2057
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
2058
    "\n",
2059
    "overall_start_time = time.time()\n",
2060
    "\n",
2061
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
2062
    "    # get file name\n",
2063
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
2064
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
2065
    "\n",
2066
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
2067
    "\n",
2068
    "    # load the dataset dictionary\n",
2069
    "    with open('../dataset/description.json', 'r') as json_file:\n",
2070
    "        dictionary = json.loads(json_file.read())\n",
2071
    "    file_information = dictionary['train'][sample_name]\n",
2072
    "    print(file_information)\n",
2073
    "\n",
2074
    "    # get control points path from dataset dir\n",
2075
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
2076
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
2077
    "\n",
2078
    "    # start the time only when the registration starts\n",
2079
    "    loop_start_time = time.time()\n",
2080
    "\n",
2081
    "    # elastix registration\n",
2082
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
2083
    "    register_elastix(\n",
2084
    "        fixed_path = i_path, \n",
2085
    "        moving_path = e_path, \n",
2086
    "        fMask = i_seg_path,\n",
2087
    "        mMask= e_seg_path,\n",
2088
    "        reg_params = reg_params,\n",
2089
    "        reg_params_key = reg_params_key,\n",
2090
    "        create_dir_callback = create_directory_if_not_exists,\n",
2091
    "        excute_cmd_callback = excute_cmd)\n",
2092
    "\n",
2093
    "    # transformix control point transformation\n",
2094
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
2095
    "    output_path = control_points_transformix(\n",
2096
    "        fixed_path = i_path, \n",
2097
    "        moving_path = e_path,\n",
2098
    "        reg_params_key = reg_params_key,\n",
2099
    "        input_points = i_cntl_pt, \n",
2100
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
2101
    "        replace_text_in_file_callback = replace_text_in_file,\n",
2102
    "        create_dir_callback = create_directory_if_not_exists, \n",
2103
    "        excute_cmd_callback = excute_cmd)\n",
2104
    "\n",
2105
    "    loop_end_time = time.time()\n",
2106
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
2107
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
2108
    "\n",
2109
    "    # get the transformed landmarks\n",
2110
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
2111
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
2112
    "\n",
2113
    "    # write the landmarks into the output directory of the points\n",
2114
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
2115
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
2116
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
2117
    "\n",
2118
    "    # evaluate\n",
2119
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
2120
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
2121
    "    \n",
2122
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
2123
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
2124
    "    \n",
2125
    "overall_end_time = time.time()\n",
2126
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
2127
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
2128
   ]
2129
  },
2130
  {
2131
   "cell_type": "code",
2132
   "execution_count": 19,
2133
   "metadata": {},
2134
   "outputs": [
2135
    {
2136
     "name": "stdout",
2137
     "output_type": "stream",
2138
     "text": [
2139
      "Experimenting using -p ../elastix-parameters/Par0003/Par0003.bs-R8-fg.txt params command...\n",
2140
      "Key for the experiment: Par0003.bs-R8-fg.txt...\n",
2141
      "-----------------------------------------------------------------------------------------------------------------------------------------------\n",
2142
      "{'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",
2143
      "Registering copd1_eBHCT and copd1_iBHCT...\n",
2144
      "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",
2145
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2146
      "Time for current registration loop: 1 minutes and 3 seconds\n",
2147
      "Transformed landmarks saved to:  output/Par0003.bs-R8-fg.txt/points/output_copd1_iBHCT/copd1_eBHCT\\outputpoints_transformed.txt\n",
2148
      "TRE (Before Registration):-  (Mean TRE: 25.9) (STD TRE: 11.57).\n",
2149
      "TRE (After Registration):-  (Mean TRE: 22.45) (STD TRE: 9.52). \n",
2150
      "\n",
2151
      "{'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",
2152
      "Registering copd2_eBHCT and copd2_iBHCT...\n",
2153
      "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",
2154
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2155
      "Time for current registration loop: 0 minutes and 56 seconds\n",
2156
      "Transformed landmarks saved to:  output/Par0003.bs-R8-fg.txt/points/output_copd2_iBHCT/copd2_eBHCT\\outputpoints_transformed.txt\n",
2157
      "TRE (Before Registration):-  (Mean TRE: 21.77) (STD TRE: 6.46).\n",
2158
      "TRE (After Registration):-  (Mean TRE: 19.06) (STD TRE: 7.25). \n",
2159
      "\n",
2160
      "{'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",
2161
      "Registering copd3_eBHCT and copd3_iBHCT...\n",
2162
      "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",
2163
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2164
      "Time for current registration loop: 1 minutes and 5 seconds\n",
2165
      "Transformed landmarks saved to:  output/Par0003.bs-R8-fg.txt/points/output_copd3_iBHCT/copd3_eBHCT\\outputpoints_transformed.txt\n",
2166
      "TRE (Before Registration):-  (Mean TRE: 12.29) (STD TRE: 6.39).\n",
2167
      "TRE (After Registration):-  (Mean TRE: 8.15) (STD TRE: 5.75). \n",
2168
      "\n",
2169
      "{'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",
2170
      "Registering copd4_eBHCT and copd4_iBHCT...\n",
2171
      "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",
2172
      "Accessing the last transform parameter file TransformParameters.0.txt\n",
2173
      "Time for current registration loop: 1 minutes and 4 seconds\n",
2174
      "Transformed landmarks saved to:  output/Par0003.bs-R8-fg.txt/points/output_copd4_iBHCT/copd4_eBHCT\\outputpoints_transformed.txt\n",
2175
      "TRE (Before Registration):-  (Mean TRE: 30.9) (STD TRE: 13.49).\n",
2176
      "TRE (After Registration):-  (Mean TRE: 27.31) (STD TRE: 10.6). \n",
2177
      "\n",
2178
      "Time for overall registrations: 4 minutes and 9 seconds\n"
2179
     ]
2180
    }
2181
   ],
2182
   "source": [
2183
    "Par_base = '../elastix-parameters/Par0003'\n",
2184
    "\n",
2185
    "# Setting the experiment registration parameter\n",
2186
    "params_list = [\n",
2187
    "    'Par0003.bs-R8-fg.txt',\n",
2188
    "    ]\n",
2189
    "\n",
2190
    "reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
2191
    "reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
2192
    "\n",
2193
    "print(f\"Experimenting using {reg_params} params command...\")\n",
2194
    "print(f\"Key for the experiment: {reg_params_key}...\")\n",
2195
    "print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
2196
    "\n",
2197
    "overall_start_time = time.time()\n",
2198
    "\n",
2199
    "for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
2200
    "    # get file name\n",
2201
    "    e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
2202
    "    i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
2203
    "\n",
2204
    "    sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
2205
    "\n",
2206
    "    # load the dataset dictionary\n",
2207
    "    with open('../dataset/description.json', 'r') as json_file:\n",
2208
    "        dictionary = json.loads(json_file.read())\n",
2209
    "    file_information = dictionary['train'][sample_name]\n",
2210
    "    print(file_information)\n",
2211
    "\n",
2212
    "    # get control points path from dataset dir\n",
2213
    "    e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
2214
    "    i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
2215
    "\n",
2216
    "    # start the time only when the registration starts\n",
2217
    "    loop_start_time = time.time()\n",
2218
    "\n",
2219
    "    # elastix registration\n",
2220
    "    print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
2221
    "    register_elastix(\n",
2222
    "        fixed_path = i_path, \n",
2223
    "        moving_path = e_path, \n",
2224
    "        fMask = i_seg_path,\n",
2225
    "        mMask= e_seg_path,\n",
2226
    "        reg_params = reg_params,\n",
2227
    "        reg_params_key = reg_params_key,\n",
2228
    "        create_dir_callback = create_directory_if_not_exists,\n",
2229
    "        excute_cmd_callback = excute_cmd)\n",
2230
    "\n",
2231
    "    # transformix control point transformation\n",
2232
    "    print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
2233
    "    output_path = control_points_transformix(\n",
2234
    "        fixed_path = i_path, \n",
2235
    "        moving_path = e_path,\n",
2236
    "        reg_params_key = reg_params_key,\n",
2237
    "        input_points = i_cntl_pt, \n",
2238
    "        transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
2239
    "        replace_text_in_file_callback = replace_text_in_file,\n",
2240
    "        create_dir_callback = create_directory_if_not_exists, \n",
2241
    "        excute_cmd_callback = excute_cmd)\n",
2242
    "\n",
2243
    "    loop_end_time = time.time()\n",
2244
    "    loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
2245
    "    print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
2246
    "\n",
2247
    "    # get the transformed landmarks\n",
2248
    "    landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
2249
    "    transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
2250
    "\n",
2251
    "    # write the landmarks into the output directory of the points\n",
2252
    "    output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
2253
    "    write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
2254
    "    print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
2255
    "\n",
2256
    "    # evaluate\n",
2257
    "    # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
2258
    "    print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
2259
    "    \n",
2260
    "    TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
2261
    "    print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
2262
    "    \n",
2263
    "overall_end_time = time.time()\n",
2264
    "overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
2265
    "print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")"
2266
   ]
2267
  },
2268
  {
2269
   "cell_type": "code",
2270
   "execution_count": 24,
2271
   "metadata": {},
2272
   "outputs": [],
2273
   "source": [
2274
    "# Par_base = '../elastix-parameters/Par0003'\n",
2275
    "\n",
2276
    "# # Setting the experiment registration parameter\n",
2277
    "# params_list = [\n",
2278
    "#     'Par0003.bs-R8-ug.txt',\n",
2279
    "#     ]\n",
2280
    "\n",
2281
    "# reg_params      = ' '.join(['-p {}'.format(os.path.join(Par_base,param)) for param in params_list]).replace('\\\\', '/')\n",
2282
    "# reg_params_key  = '+'.join(['{}'.format(param) for param in params_list])\n",
2283
    "\n",
2284
    "# print(f\"Experimenting using {reg_params} params command...\")\n",
2285
    "# print(f\"Key for the experiment: {reg_params_key}...\")\n",
2286
    "# print( \"-----------------------------------------------------------------------------------------------------------------------------------------------\")\n",
2287
    "\n",
2288
    "# overall_start_time = time.time()\n",
2289
    "\n",
2290
    "# for e_path, i_path, e_seg_path, i_seg_path in zip(exhale_volumes, inhale_volumes, exhale_seg, inhale_seg):\n",
2291
    "#     # get file name\n",
2292
    "#     e_filename_full = e_path.split('/')[-1].split('.')[0] #copd1_eBHCT, ..\n",
2293
    "#     i_filename_full = i_path.split('/')[-1].split('.')[0] #copd1_iBHCT, ..\n",
2294
    "\n",
2295
    "#     sample_name = i_path.split('/')[-1].split('_')[0] #copd1, copd2, ...\n",
2296
    "\n",
2297
    "#     # load the dataset dictionary\n",
2298
    "#     with open('../dataset/description.json', 'r') as json_file:\n",
2299
    "#         dictionary = json.loads(json_file.read())\n",
2300
    "#     file_information = dictionary['train'][sample_name]\n",
2301
    "#     print(file_information)\n",
2302
    "\n",
2303
    "#     # get control points path from dataset dir\n",
2304
    "#     e_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_eBH_xyz_r1.txt'\n",
2305
    "#     i_cntl_pt = f'../dataset/train/{sample_name}/{sample_name}_300_iBH_xyz_r1.txt'\n",
2306
    "\n",
2307
    "#     # start the time only when the registration starts\n",
2308
    "#     loop_start_time = time.time()\n",
2309
    "\n",
2310
    "#     # elastix registration\n",
2311
    "#     print(f\"Registering {e_filename_full} and {i_filename_full}...\")    \n",
2312
    "#     register_elastix(\n",
2313
    "#         fixed_path = i_path, \n",
2314
    "#         moving_path = e_path, \n",
2315
    "#         fMask = i_seg_path,\n",
2316
    "#         mMask= e_seg_path,\n",
2317
    "#         reg_params = reg_params,\n",
2318
    "#         reg_params_key = reg_params_key,\n",
2319
    "#         create_dir_callback = create_directory_if_not_exists,\n",
2320
    "#         excute_cmd_callback = excute_cmd)\n",
2321
    "\n",
2322
    "#     # transformix control point transformation\n",
2323
    "#     print(f\"Accessing the last transform parameter file TransformParameters.{len(params_list)-1}.txt\")\n",
2324
    "#     output_path = control_points_transformix(\n",
2325
    "#         fixed_path = i_path, \n",
2326
    "#         moving_path = e_path,\n",
2327
    "#         reg_params_key = reg_params_key,\n",
2328
    "#         input_points = i_cntl_pt, \n",
2329
    "#         transform_path = f'output/{reg_params_key}/images/output_{i_filename_full}/{e_filename_full}/TransformParameters.{len(params_list)-1}.txt',\n",
2330
    "#         replace_text_in_file_callback = replace_text_in_file,\n",
2331
    "#         create_dir_callback = create_directory_if_not_exists, \n",
2332
    "#         excute_cmd_callback = excute_cmd)\n",
2333
    "\n",
2334
    "#     loop_end_time = time.time()\n",
2335
    "#     loop_elapsed_minutes, loop_elapsed_seconds = format_elapsed_time(loop_start_time, loop_end_time)\n",
2336
    "#     print(f\"Time for current registration loop: {loop_elapsed_minutes} minutes and {loop_elapsed_seconds} seconds\")\n",
2337
    "\n",
2338
    "#     # get the transformed landmarks\n",
2339
    "#     landmarks_path = os.path.join(output_path, 'outputpoints.txt')\n",
2340
    "#     transformed_landmarks = get_landmarks_from_txt(landmarks_path, search_key='OutputIndexFixed')\n",
2341
    "\n",
2342
    "#     # write the landmarks into the output directory of the points\n",
2343
    "#     output_landmarks_path = os.path.join(output_path, 'outputpoints_transformed.txt')\n",
2344
    "#     write_landmarks_to_list(transformed_landmarks, output_landmarks_path)\n",
2345
    "#     print(\"Transformed landmarks saved to: \", output_landmarks_path)\n",
2346
    "\n",
2347
    "#     # evaluate\n",
2348
    "#     # TRE_mean, TRE_std = compute_TRE(i_cntl_pt, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
2349
    "#     print(\"TRE (Before Registration):- \", f\"(Mean TRE: {file_information['displacement_mean']})\", f\"(STD TRE: {file_information['displacement_std']}).\")\n",
2350
    "    \n",
2351
    "#     TRE_mean, TRE_std = compute_TRE(output_landmarks_path, e_cntl_pt, tuple(file_information['voxel_dim']))\n",
2352
    "#     print(\"TRE (After Registration):- \", f\"(Mean TRE: {TRE_mean})\", f\"(STD TRE: {TRE_std}). \\n\")\\\n",
2353
    "    \n",
2354
    "# overall_end_time = time.time()\n",
2355
    "# overall_elapsed_minutes, overall_elapsed_seconds = format_elapsed_time(overall_start_time, overall_end_time)\n",
2356
    "# print(f\"Time for overall registrations: {overall_elapsed_minutes} minutes and {overall_elapsed_seconds} seconds\")\n",
2357
    "\n",
2358
    "# command fails: Description: itk::ERROR: AdvancedMattesMutualInformationMetric(0000000002B00790): Too many samples map outside moving image buffer: 499 / 2000"
2359
   ]
2360
  },
2361
  {
2362
   "cell_type": "code",
2363
   "execution_count": null,
2364
   "metadata": {},
2365
   "outputs": [],
2366
   "source": []
2367
  }
2368
 ],
2369
 "metadata": {
2370
  "kernelspec": {
2371
   "display_name": "env",
2372
   "language": "python",
2373
   "name": "python3"
2374
  },
2375
  "language_info": {
2376
   "codemirror_mode": {
2377
    "name": "ipython",
2378
    "version": 3
2379
   },
2380
   "file_extension": ".py",
2381
   "mimetype": "text/x-python",
2382
   "name": "python",
2383
   "nbconvert_exporter": "python",
2384
   "pygments_lexer": "ipython3",
2385
   "version": "3.10.4"
2386
  }
2387
 },
2388
 "nbformat": 4,
2389
 "nbformat_minor": 2
2390
}