Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 4 additions & 91 deletions examples/registration/atlas_poi_transfer_leg/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from TPTBox.segmentation.TotalVibeSeg import run_totalvibeseg\n",
"\n",
"path_to_ct = \"...\"\n",
"out_seg = \"seg.nii.gz\"\n",
"ddevice = \"cpu\" #\"cuda\" / \"mps\"\n",
"run_totalvibeseg(path_to_ct,out_seg=out_seg,override=False,gpu=1,ddevice=ddevice,dataset_id=10,keep_size=True)"
]
"source": "from TPTBox.segmentation import run_vibeseg\n\npath_to_ct = \"...\"\nout_seg = \"seg.nii.gz\"\nddevice = \"cpu\" # \"cuda\" / \"mps\"\nrun_vibeseg(path_to_ct, out_seg=out_seg, override=False, gpu=1, ddevice=ddevice, dataset_id=10, keep_size=True)\n"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -130,58 +123,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Step 1\n",
"from pathlib import Path\n",
"\n",
"from atlas_poi_transfer import parse_coordinates_to_poi, prep_Atlas\n",
"\n",
"from TPTBox import POI, to_nii\n",
"from TPTBox.core.vert_constants import Full_Body_Instance, Lower_Body\n",
"\n",
"##########################################\n",
"#Settings\n",
"text_file_is_left_leg = True\n",
"file_text = \"010__left.txt\"\n",
"segmentation_path = \"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0001/bone.nii.gz\"\n",
"out_folder = Path(\"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/atlas\")\n",
"atlas_id = 1\n",
"##########################################\n",
"# Load segmentation\n",
"seg = to_nii(segmentation_path,True)\n",
"\n",
"if not text_file_is_left_leg:\n",
" axis = seg.get_axis(\"R\")\n",
" if axis == 0:\n",
" target = seg.set_array(seg.get_array()[::-1]).copy()\n",
" elif axis == 1:\n",
" target = seg.set_array(seg.get_array()[:, ::-1]).copy()\n",
" elif axis == 2:\n",
" target = seg.set_array(seg.get_array()[:, :, ::-1]).copy()\n",
"assert text_file_is_left_leg, \"Not implement: Flip NII and POI\"\n",
"# Prep atlas\n",
"atlas_path = out_folder/f\"atlas{atlas_id:03}.nii.gz\"\n",
"atlas_cms_poi_path = out_folder/f\"atlas{atlas_id:03}_cms_poi.json\" # Center of mass\n",
"atlas_poi_path = out_folder/f\"atlas{atlas_id:03}_poi.json\"\n",
"prep_Atlas(target,atlas_path,atlas_cms_poi_path,text_file_is_left_leg)\n",
"\n",
"\n",
"poi = parse_coordinates_to_poi(file_text, True).to_other(seg) if \".txt\" in file_text else POI.load(file_text).resample_from_to(seg)\n",
"if not text_file_is_left_leg:\n",
" for k1, k2, (x, y, z) in poi.items():\n",
" axis = poi.get_axis(\"R\")\n",
" if axis == 0:\n",
" poi[k1, k2] = (poi.shape[0] - 1 - x, y, z)\n",
" elif axis == 1:\n",
" poi[k1, k2] = (x, poi.shape[1] - 1 - y, z)\n",
" elif axis == 2:\n",
" poi[k1, k2] = (x, y, poi.shape[2] - 1 - z)\n",
" else:\n",
" raise ValueError(axis)\n",
"poi.level_one_info=Full_Body_Instance\n",
"poi.level_two_info=Lower_Body\n",
"poi.to_global().save(atlas_poi_path)\n"
]
"source": "#Step 1\nfrom pathlib import Path\n\nfrom atlas_poi_transfer_leg_ct import parse_coordinates_to_poi, prep_Atlas\n\nfrom TPTBox import POI, to_nii\nfrom TPTBox.core.vert_constants import Full_Body_Instance, Lower_Body\n\n##########################################\n#Settings\ntext_file_is_left_leg = True\nfile_text = \"010__left.txt\"\nsegmentation_path = \"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0001/bone.nii.gz\"\nct_path = \"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0001/ct.nii.gz\"\nout_folder = Path(\"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/atlas\")\natlas_id = 1\n##########################################\n# Load segmentation\nseg = to_nii(segmentation_path,True)\n\nif not text_file_is_left_leg:\n axis = seg.get_axis(\"R\")\n if axis == 0:\n target = seg.set_array(seg.get_array()[::-1]).copy()\n elif axis == 1:\n target = seg.set_array(seg.get_array()[:, ::-1]).copy()\n elif axis == 2:\n target = seg.set_array(seg.get_array()[:, :, ::-1]).copy()\nassert text_file_is_left_leg, \"Not implement: Flip NII and POI\"\n# Prep atlas\natlas_path = out_folder/f\"atlas{atlas_id:03}.nii.gz\"\nct_atlas_path = out_folder/f\"atlas{atlas_id:03}_ct.nii.gz\" # cropped CT saved next to the atlas\natlas_cms_poi_path = out_folder/f\"atlas{atlas_id:03}_cms_poi.json\" # Center of mass\natlas_poi_path = out_folder/f\"atlas{atlas_id:03}_poi.json\"\nprep_Atlas(seg, ct_path, atlas_path, ct_atlas_path, atlas_cms_poi_path, atlas_left=text_file_is_left_leg)\n\n\npoi = parse_coordinates_to_poi(file_text, True).to_other(seg) if \".txt\" in file_text else POI.load(file_text).resample_from_to(seg)\nif not text_file_is_left_leg:\n for k1, k2, (x, y, z) in poi.items():\n axis = poi.get_axis(\"R\")\n if axis == 0:\n poi[k1, k2] = (poi.shape[0] - 1 - x, y, z)\n elif axis == 1:\n poi[k1, k2] = (x, poi.shape[1] - 1 - y, z)\n elif axis == 2:\n poi[k1, k2] = (x, y, poi.shape[2] - 1 - z)\n else:\n raise ValueError(axis)\npoi.level_one_info=Full_Body_Instance\npoi.level_two_info=Lower_Body\npoi.to_global().save(atlas_poi_path)\n"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -217,36 +159,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Step 1\n",
"from pathlib import Path\n",
"\n",
"from atlas_poi_transfer import Register_Point_Atlas\n",
"\n",
"from TPTBox import POI, to_nii\n",
"\n",
"##########################################\n",
"#Settings\n",
"target_seg_path = \"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0003/bone.nii.gz\"#TODO Path to target seg\n",
"s = str(target_seg_path).split('.')[0]\n",
"split_leg_path = s + \"_seg-left-right-split_msk.nii.gz\"\n",
"out_new_pois = s + \"_desc-leg_poi.json\"\n",
"out_new_pois_nii = s + \"_desc-leg_poi.nii.gz\"\n",
"atlas_id = 1\n",
"ddevice = \"cuda\"\n",
"gpu = 0\n",
"##########################################\n",
"# Atlas\n",
"atlas_p = out_folder/f\"atlas{atlas_id:03}.nii.gz\"\n",
"atlas_centroids = out_folder/f\"atlas{atlas_id:03}_cms_poi.json\" # Center of mass\n",
"atlas_poi_path = out_folder/f\"atlas{atlas_id:03}_poi.json\"\n",
"# Load segmentation\n",
"target = to_nii(target_seg_path,True)\n",
"atlas = to_nii(atlas_p,True)\n",
"\n",
"#Creating this object will start the registration\n",
"registration_obj = Register_Point_Atlas(target,atlas,split_leg_path=split_leg_path,atlas_centroids=atlas_centroids,gpu=gpu,ddevice=ddevice, verbose=0)"
]
"source": "#Step 1\nfrom pathlib import Path\n\nfrom atlas_poi_transfer_leg_ct import Register_Point_Atlas\n\nfrom TPTBox import POI, to_nii\n\n##########################################\n#Settings\ntarget_seg_path = \"/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0003/bone.nii.gz\"#TODO Path to target seg\ns = str(target_seg_path).split('.')[0]\nsplit_leg_path = s + \"_seg-left-right-split_msk.nii.gz\"\nout_new_pois = s + \"_desc-leg_poi.json\"\nout_new_pois_nii = s + \"_desc-leg_poi.nii.gz\"\natlas_id = 1\nddevice = \"cuda\"\ngpu = 0\n##########################################\n# Atlas\natlas_p = out_folder/f\"atlas{atlas_id:03}.nii.gz\"\natlas_centroids = out_folder/f\"atlas{atlas_id:03}_cms_poi.json\" # Center of mass\natlas_poi_path = out_folder/f\"atlas{atlas_id:03}_poi.json\"\n# Load segmentation\ntarget = to_nii(target_seg_path,True)\natlas = to_nii(atlas_p,True)\n\n#Creating this object will start the registration\nregistration_obj = Register_Point_Atlas(target,atlas,split_leg_path=split_leg_path,atlas_centroids=atlas_centroids,gpu=gpu,ddevice=ddevice, verbose=0)\n"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -321,4 +234,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
4 changes: 3 additions & 1 deletion examples/registration/atlas_poi_transfer_leg/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
text_file_is_left_leg = True
file_text = "/DATA/NAS/tools/TPTBox/examples/atlas_poi_transfer_leg/010__left.txt"
segmentation_path = "/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0001/bone.nii.gz"
ct_path = "/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/source/Dataset001_all/0001/ct.nii.gz"
out_folder = Path("/DATA/NAS/datasets_processed/CT_fullbody/dataset-watrinet/atlas2")
atlas_id = 1
##########################################
Expand All @@ -31,9 +32,10 @@
assert text_file_is_left_leg, "Not implement: Flip NII and POI"
# Prep atlas
atlas_path = out_folder / f"atlas{atlas_id:03}.nii.gz"
ct_atlas_path = out_folder / f"atlas{atlas_id:03}_ct.nii.gz" # cropped CT saved next to the atlas
atlas_cms_poi_path = out_folder / f"atlas{atlas_id:03}_cms_poi.json" # Center of mass
atlas_poi_path = out_folder / f"atlas{atlas_id:03}_poi.json"
prep_Atlas(seg, atlas_path, atlas_cms_poi_path, text_file_is_left_leg)
prep_Atlas(seg, ct_path, atlas_path, ct_atlas_path, atlas_cms_poi_path, atlas_left=text_file_is_left_leg)


poi = parse_coordinates_to_poi(file_text, True).to_other(seg) if ".txt" in file_text else POI.load(file_text).resample_from_to(seg)
Expand Down
103 changes: 4 additions & 99 deletions tutorials/tutorial_Dataset_processing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -896,54 +896,14 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"\n",
"from TPTBox import NII, Location, Vertebra_Instance, calc_poi_from_subreg_vert, to_nii\n",
"from TPTBox.registration import Point_Registration, ridged_points_from_poi\n",
"\n",
"# Example registration two sagittal images, like for compensating movement between scans.\n",
"poi_fixed = calc_poi_from_subreg_vert(nii_instance1, nii_semantic1, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n",
" 2\n",
")\n",
"poi_moving = calc_poi_from_subreg_vert(nii_instance2, nii_semantic2, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n",
" 2\n",
")\n",
"\n",
"registration_object: Point_Registration = ridged_points_from_poi(poi_fixed, poi_moving, c_val=0)\n",
"\n",
"# Move image\n",
"moved_nii = registration_object.transform_nii(image2)\n",
"print(moved_nii, \"\\n\", image2, \"\\n\")\n",
"# Move poi\n",
"moved_poi = registration_object.transform_poi(poi_moving).round(1)\n",
"print(moved_poi, \"\\n\", poi_moving, \"\\n\")\n"
]
"source": "from pathlib import Path\n\nfrom TPTBox import NII, Location, Vertebra_Instance, calc_poi_from_subreg_vert, to_nii\nfrom TPTBox.registration import Point_Registration, ridged_points_from_poi\n\n# Example registration two sagittal images, like for compensating movement between scans.\npoi_fixed = calc_poi_from_subreg_vert(nii_instance1, nii_semantic1, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n 2\n)\npoi_moving = calc_poi_from_subreg_vert(nii_instance2, nii_semantic2, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n 2\n)\n\nregistration_object: Point_Registration = ridged_points_from_poi(poi_fixed, poi_moving)\n\n# Move image\nmoved_nii = registration_object.transform_nii(image2)\nprint(moved_nii, \"\\n\", image2, \"\\n\")\n# Move poi\nmoved_poi = registration_object.transform_poi(poi_moving).round(1)\nprint(moved_poi, \"\\n\", poi_moving, \"\\n\")\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from TPTBox import NII, Location, Vertebra_Instance, calc_poi_from_subreg_vert\n",
"from TPTBox.registration import Point_Registration, ridged_points_from_poi\n",
"\n",
"# Example registration two sagittal images, like for compensating movement between scans.\n",
"poi_fixed = calc_poi_from_subreg_vert(nii_instance1, nii_semantic1, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n",
" 2\n",
")\n",
"poi_moving = calc_poi_from_subreg_vert(nii_instance2, nii_semantic2, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n",
" 2\n",
")\n",
"registration_object: Point_Registration = ridged_points_from_poi(poi_fixed, poi_moving, c_val=0)\n",
"# Move image\n",
"moved_nii = registration_object.transform_nii(image2, output_space=fixed_image)\n",
"print(moved_nii, \"\\n\", image2, \"\\n\")\n",
"# Move poi\n",
"moved_poi = registration_object.transform_poi(poi_moving).round(1)\n",
"print(moved_poi, \"\\n\", poi_moving, \"\\n\")\n"
]
"source": "from TPTBox import NII, Location, Vertebra_Instance, calc_poi_from_subreg_vert\nfrom TPTBox.registration import Point_Registration, ridged_points_from_poi\n\n# Example registration two sagittal images, like for compensating movement between scans.\npoi_fixed = calc_poi_from_subreg_vert(nii_instance1, nii_semantic1, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n 2\n)\npoi_moving = calc_poi_from_subreg_vert(nii_instance2, nii_semantic2, subreg_id=[Location.Vertebra_Corpus, Location.Spinosus_Process]).round(\n 2\n)\nregistration_object: Point_Registration = ridged_points_from_poi(poi_fixed, poi_moving)\n# Move image\nmoved_nii = registration_object.transform_nii(image2, output_space=fixed_image)\nprint(moved_nii, \"\\n\", image2, \"\\n\")\n# Move poi\nmoved_poi = registration_object.transform_poi(poi_moving).round(1)\nprint(moved_poi, \"\\n\", poi_moving, \"\\n\")\n"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -980,62 +940,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from TPTBox import NII, Location, Vertebra_Instance, calc_poi_from_subreg_vert\n",
"from TPTBox.registration import Point_Registration, ridged_points_from_poi\n",
"from TPTBox.segmentation.VibeSeg.vibeseg import extract_vertebra_bodies_from_VibeSeg\n",
"\n",
"# Example registration axial and sagittal with points.\n",
"# T2w axial points are computed from the vibe segment.\n",
"dataset = target_folder / \"dataset-tutorial-data-processing\"\n",
"\n",
"nii_instance_path2 = out_paths[\"out_vert\"]\n",
"nii_semantic_path2 = out_paths[\"out_spine\"]\n",
"# We recommend if you registrade spineps points too use at least two points.\n",
"poi_fixed = calc_poi_from_subreg_vert(nii_instance_path, nii_semantic_path, subreg_id=[Location.Vertebra_Corpus]).round(1)\n",
"fixed_image = BIDS_FILE(\n",
" f\"{dataset}/rawdata_stiched/sub-111168223/ses-20230128/T2w/sub-111168223_ses-20230128_sequ-401-stiched_acq-sag_part-inphase_T2w.nii.gz\",\n",
" dataset,\n",
")\n",
"\n",
"moving_file = BIDS_FILE(\n",
" f\"{dataset}/rawdata_stiched/sub-111168223/ses-20230128/dixon/sub-111168223_ses-20230128_sequ-501-stiched_acq-ax_part-water_dixon.nii.gz\",\n",
" dataset,\n",
")\n",
"out_file = moving_file.get_changed_path(\n",
" \"nii.gz\", \"msk\", parent=\"derivative\", info={\"seg\": \"VibeSegmentator\", \"mod\": moving_file.bids_format}\n",
")\n",
"moving_image = to_nii(moving_file)\n",
"out_file = to_nii(out_file, True)\n",
"\n",
"#### Let's move the image, so we see better that the registration is working.\n",
"moving_image.origin = (moving_image.origin[0], moving_image.origin[1], moving_image.origin[2] + 175)\n",
"out_file.origin = moving_image.origin\n",
"####\n",
"\n",
"# Ensure that we count the same as in the T2w sagittal\n",
"num_thoracic_verts = 12\n",
"if Vertebra_Instance.T13.value in poi_fixed.keys_region():\n",
" num_thoracic_verts = 13\n",
"if Vertebra_Instance.T12.value not in poi_fixed.keys_region():\n",
" num_thoracic_verts = 11\n",
"num_lumbar_verts = 5\n",
"if Vertebra_Instance.L6.value in poi_fixed.keys_region():\n",
" num_lumbar_verts = 6\n",
"if Vertebra_Instance.L5.value not in poi_fixed.keys_region():\n",
" num_lumbar_verts = 4\n",
"# Note: this function currently assumes that we see the sacrum in the image.\n",
"nii, poi_moving = extract_vertebra_bodies_from_VibeSeg(out_file, num_lumbar_verts=num_lumbar_verts, num_thoracic_verts=num_thoracic_verts)\n",
"\n",
"registration_object: Point_Registration = ridged_points_from_poi(poi_fixed, poi_moving, c_val=0)\n",
"\n",
"# Move image\n",
"moved_nii = registration_object.transform_nii(moving_image)\n",
"print(moved_nii, \"\\n\", moving_image, \"\\n\")\n",
"# Move poi\n",
"moved_poi = registration_object.transform_poi(poi_moving).round(1)\n",
"print(moved_poi, \"\\n\", poi_moving, \"\\n\")\n"
]
"source": "from TPTBox import NII, Location, Vertebra_Instance, calc_poi_from_subreg_vert\nfrom TPTBox.registration import Point_Registration, ridged_points_from_poi\nfrom TPTBox.segmentation.VibeSeg.vibeseg import extract_vertebra_bodies_from_VibeSeg\n\n# Example registration axial and sagittal with points.\n# T2w axial points are computed from the vibe segment.\ndataset = target_folder / \"dataset-tutorial-data-processing\"\n\nnii_instance_path2 = out_paths[\"out_vert\"]\nnii_semantic_path2 = out_paths[\"out_spine\"]\n# We recommend if you registrade spineps points too use at least two points.\npoi_fixed = calc_poi_from_subreg_vert(nii_instance_path, nii_semantic_path, subreg_id=[Location.Vertebra_Corpus]).round(1)\nfixed_image = BIDS_FILE(\n f\"{dataset}/rawdata_stiched/sub-111168223/ses-20230128/T2w/sub-111168223_ses-20230128_sequ-401-stiched_acq-sag_part-inphase_T2w.nii.gz\",\n dataset,\n)\n\nmoving_file = BIDS_FILE(\n f\"{dataset}/rawdata_stiched/sub-111168223/ses-20230128/dixon/sub-111168223_ses-20230128_sequ-501-stiched_acq-ax_part-water_dixon.nii.gz\",\n dataset,\n)\nout_file = moving_file.get_changed_path(\n \"nii.gz\", \"msk\", parent=\"derivative\", info={\"seg\": \"VibeSegmentator\", \"mod\": moving_file.bids_format}\n)\nmoving_image = to_nii(moving_file)\nout_file = to_nii(out_file, True)\n\n#### Let's move the image, so we see better that the registration is working.\nmoving_image.origin = (moving_image.origin[0], moving_image.origin[1], moving_image.origin[2] + 175)\nout_file.origin = moving_image.origin\n####\n\n# Ensure that we count the same as in the T2w sagittal\nnum_thoracic_verts = 12\nif Vertebra_Instance.T13.value in poi_fixed.keys_region():\n num_thoracic_verts = 13\nif Vertebra_Instance.T12.value not in poi_fixed.keys_region():\n num_thoracic_verts = 11\nnum_lumbar_verts = 5\nif Vertebra_Instance.L6.value in poi_fixed.keys_region():\n num_lumbar_verts = 6\nif Vertebra_Instance.L5.value not in poi_fixed.keys_region():\n num_lumbar_verts = 4\n# Note: this function currently assumes that we see the sacrum in the image.\nnii, poi_moving = extract_vertebra_bodies_from_VibeSeg(out_file, num_lumbar_verts=num_lumbar_verts, num_thoracic_verts=num_thoracic_verts)\n\nregistration_object: Point_Registration = ridged_points_from_poi(poi_fixed, poi_moving)\n\n# Move image\nmoved_nii = registration_object.transform_nii(moving_image)\nprint(moved_nii, \"\\n\", moving_image, \"\\n\")\n# Move poi\nmoved_poi = registration_object.transform_poi(poi_moving).round(1)\nprint(moved_poi, \"\\n\", poi_moving, \"\\n\")\n"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -1192,4 +1097,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
Loading
Loading