-
Notifications
You must be signed in to change notification settings - Fork 20
BAMF Lung and FDG-Avid Tumor #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
25c975f
create PET CT multi modality nnUnetRunner
jithenece 74232c5
Resampler module
jithenece 5167ae4
define flow
jithenece ec8ba04
use FDG_AVID_TUMOR entry
jithenece aa3d6d7
add meta information
jithenece 8dcbd53
update label info
jithenece ef8433a
fix entrypoint
jithenece b0024ed
Merge branch 'MHubAI:main' into bamf_nnunet_pet_ct_lung
jithenece 3660f06
update results stats
jithenece d118d3f
change post-processing logic
jithenece 3e84dd9
comments
jithenece ea50a98
format logic
jithenece 5a3ba63
rename the folder
jithenece 0f7bb8c
comments updated
jithenece f9e1456
use NiftiConverter
jithenece 670823d
fix review comments
jithenece 1f12571
update dcm2niix to fix conversion errors
jithenece c9b74f8
extract lung only segments from ts
jithenece effdccc
fix pip errors
jithenece d781beb
remove dcm2niix
jithenece a65489e
Merge branch 'MHubAI:main' into bamf_pet_ct_lung_tumor
jithenece 368654a
Merge branch 'MHubAI:main' into bamf_pet_ct_lung_tumor
jithenece 0f43346
Merge branch 'MHubAI:main' into bamf_pet_ct_lung_tumor
jithenece 3a4ee41
/test validation
jithenece 7223fee
Update Dockerfile
LennyN95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
models/bamf_pet_ct_lung_tumor/utils/LungSegmentatorRunner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """ | ||
| ------------------------------------------------- | ||
| MHub - Run Lung segmentator Module using TotalSegmentator. | ||
| ------------------------------------------------- | ||
|
|
||
| ------------------------------------------------- | ||
| Author: Jithendra Kumar | ||
| Email: Jithendra.kumar@bamfhealth.com | ||
| ------------------------------------------------- | ||
| """ | ||
|
|
||
| from mhubio.core import Module, Instance, InstanceData, DataType, FileType, CT, SEG, IO, DataTypeQuery | ||
| import os, subprocess | ||
| import SimpleITK as sitk | ||
| import numpy as np | ||
| from skimage import measure | ||
| from mhubio.core import IO | ||
|
|
||
| from totalsegmentator.map_to_binary import class_map | ||
|
|
||
|
|
||
| @IO.ConfigInput('in_data', 'nifti:mod=ct', the="input data to run Lung Segmentator on") | ||
| class LungSegmentatorRunner(Module): | ||
|
|
||
| def mask_labels(self, labels, ts): | ||
| """ | ||
| Create a mask based on given labels. | ||
|
|
||
| Args: | ||
| labels (list): List of labels to be masked. | ||
| ts (np.ndarray): Image data. | ||
|
|
||
| Returns: | ||
| np.ndarray: Masked image data. | ||
| """ | ||
| lung = np.zeros(ts.shape) | ||
| for lbl in labels: | ||
| lung[ts == lbl] = 1 | ||
| return lung | ||
|
|
||
| def n_connected(self, img_data): | ||
| """ | ||
| Get the largest connected component in a binary image. | ||
|
|
||
| Args: | ||
| img_data (np.ndarray): image data. | ||
|
|
||
| Returns: | ||
| np.ndarray: Processed image with the largest connected component. | ||
| """ | ||
| img_data_mask = np.zeros(img_data.shape) | ||
| img_data_mask[img_data >= 1] = 1 | ||
| img_filtered = np.zeros(img_data_mask.shape) | ||
| blobs_labels = measure.label(img_data_mask, background=0) | ||
| lbl, counts = np.unique(blobs_labels, return_counts=True) | ||
| lbl_dict = {} | ||
| for i, j in zip(lbl, counts): | ||
| lbl_dict[i] = j | ||
| sorted_dict = dict(sorted(lbl_dict.items(), key=lambda x: x[1], reverse=True)) | ||
| count = 0 | ||
|
|
||
| for key, value in sorted_dict.items(): | ||
| if count == 1: | ||
| print(key, value) | ||
| img_filtered[blobs_labels == key] = 1 | ||
| count += 1 | ||
|
|
||
| img_data[img_filtered != 1] = 0 | ||
| return img_data | ||
|
|
||
| @IO.Instance() | ||
| @IO.Input('in_data', the="input whole body ct scan") | ||
| @IO.Output('out_data', 'lung_segmentations.nii.gz', 'nifti:mod=seg:model=LungSegmentator:roi=LEFT_LUNG,RIGHT_LUNG', | ||
| data='in_data', the="output segmentation mask containing lung labels") | ||
| def task(self, instance: Instance, in_data: InstanceData, out_data: InstanceData) -> None: | ||
| # use total segmentator to extract lung segmentation | ||
| bash_command = ["TotalSegmentator"] | ||
| bash_command += ["-i", in_data.abspath] | ||
LennyN95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| tmp_dir = self.config.data.requestTempDir(label="lung-segment-processor") | ||
| segments_file = os.path.join(tmp_dir, f'segmentations.nii.gz') | ||
|
|
||
| # multi-label output (one nifti file containing all labels instead of one nifti file per label) | ||
| self.v("Generating multi-label output ('--ml')") | ||
| bash_command += ["-o", segments_file] | ||
| bash_command += ["--ml"] | ||
|
|
||
| # fast mode | ||
| self.v("Running TotalSegmentator in default mode (1.5mm)") | ||
| self.v(">> run: ", " ".join(bash_command)) | ||
LennyN95 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # run the model | ||
| self.subprocess(bash_command, text=True) | ||
|
|
||
| # Extract labels for left lung and right lung from total segmentator v1 output | ||
| left_lung_labels = [label for label, name in class_map["total"].items() if "left" in name and "lung" in name] | ||
| right_lung_labels = [label for label, name in class_map["total"].items() if "right" in name and "lung" in name] | ||
|
|
||
| segments_arr = sitk.GetArrayFromImage(sitk.ReadImage(segments_file)) | ||
| lung_left = self.n_connected(self.mask_labels(left_lung_labels, segments_arr)) | ||
| lung_right = self.n_connected(self.mask_labels(right_lung_labels, segments_arr)) | ||
| op_data = np.zeros(segments_arr.shape) | ||
| op_data[lung_left > 0] = 1 | ||
| op_data[lung_right > 0] = 2 | ||
| ref = sitk.ReadImage(in_data.abspath) | ||
| op_img = sitk.GetImageFromArray(op_data) | ||
| op_img.CopyInformation(ref) | ||
| sitk.WriteImage(op_img, out_data.abspath) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.