Skip to content

Commit

Permalink
Merge pull request #38 from reghbali/reghbali_more_volume_features
Browse files Browse the repository at this point in the history
Reghbali more volume features
  • Loading branch information
reghbali authored Sep 8, 2023
2 parents 614b915 + 4548a07 commit 8d9410a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ jobs:
python-version: [ "3.9", "3.11" ]

steps:
- name: Get branch
uses: xt0rted/pull-request-comment-branch@v2
id: comment-branch

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}

- name: Cache dependency binaries
id: cache-bin
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Cache dependency binaries
id: cache-bin
Expand Down
22 changes: 20 additions & 2 deletions pyalfe/tasks/quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ def load_template_images(self, accession, target):
resampling_origin=Modality.T1,
sub_dir_name=roi_sub_dir,
)
print(template_image_to_target_file)

if os.path.exists(template_image_to_target_file):
target_images[roi_key], _ = self.load(template_image_to_target_file)
self.logger.info(
'loaded template: ' + template_image_to_target_file
)
target_images[roi_key] = np.round(target_images[roi_key])
else:
self.logger.info(
'missing template: ' + template_image_to_target_file
)
target_images[roi_key] = None
return target_images

Expand All @@ -117,7 +123,7 @@ def get_radiomics(self, skullstripped_file, lesion_seg_file):
return {}

def get_brain_volume_stats(
self, brain_mask, tissue_seg, ventricles_seg, voxel_volume
self, brain_mask, tissue_seg, ventricles_seg, template_images, voxel_volume
):
stats = {}
brain_indices = np.where(brain_mask == 1)[0]
Expand All @@ -134,6 +140,15 @@ def get_brain_volume_stats(
len(np.where(tissue_seg == tissue)[0]) * voxel_volume
)

for template_key, template_image in template_images.items():
if 'regions' not in roi_dict[template_key]:
continue
regions = roi_dict[template_key]['regions']
for region_key, region_values in regions.items():
stats[f'volume_of_{region_key}'] = (
np.sum(np.isin(template_image, region_values)) * voxel_volume
)

return stats

def get_lesion_stats(
Expand Down Expand Up @@ -219,6 +234,7 @@ def get_lesion_stats(
stats[
f'ninety_five_percentile_{modality_name.lower()}_signal'
] = np.percentile(modality_image[lesion_indices], 95)

if Modality.T1 in modality_images and Modality.T1Post in modality_images:
t1_image = modality_images[Modality.T1]
t1post_image = modality_images[Modality.T1Post]
Expand Down Expand Up @@ -279,6 +295,8 @@ def run(self, accession):
else:
tissue_seg, _ = self.load(tissue_seg_file)

template_images = self.load_template_images(accession, Modality.T1)

ventricles_seg_file = self.pipeline_dir.get_output_image(
accession=accession, modality=Modality.T1, image_type='VentriclesSeg'
)
Expand Down
23 changes: 16 additions & 7 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def test_run(self):
Modality.FLAIR,
Modality.ADC,
]
pipeline_dir = DefaultALFEDataDir(output=self.output_dir, input=self.input_dir)
pipeline_dir = DefaultALFEDataDir(
output_dir=self.output_dir, input_dir=self.input_dir
)

for modality in modalities:
pipeline_dir.create_dir('input', accession, modality)
Expand Down Expand Up @@ -82,19 +84,26 @@ def test_run(self):
segmentation_path = pipeline_dir.get_output_image(
accession,
modality,
image_type='CNNAbnormalMap_seg',
image_type='abnormal_seg',
sub_dir_name='abnormalmap',
)
self.assertTrue(
os.path.exists(segmentation_path),
msg=f'{segmentation_path} does not exist.',
)
quantification_path = pipeline_dir.get_quantification_file(
summary_quantification_path = pipeline_dir.get_quantification_file(
accession, modality, 'SummaryLesionMeasures'
)
individual_quantification_path = pipeline_dir.get_quantification_file(
accession, modality, 'IndividualLesionMeasures'
)

self.assertTrue(
os.path.exists(quantification_path),
msg=f'{quantification_path} does not exist.',
os.path.exists(summary_quantification_path),
msg=f'{summary_quantification_path} does not exist.',
)
quantification = pd.read_csv(quantification_path)
self.assertEqual(quantification.dropna().shape, (14, 2))
summary_quantification = pd.read_csv(summary_quantification_path)
self.assertEqual(summary_quantification.dropna().shape, (53, 2))

individual_quantification = pd.read_csv(individual_quantification_path)
self.assertEqual(individual_quantification.dropna().shape, (226, 51))
2 changes: 1 addition & 1 deletion tests/unit/test_image_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_register_affine(self):
np.testing.assert_array_less(
np.linalg.norm(get_nifti_data(output) - fixed_data)
/ np.linalg.norm(get_nifti_data(output) - moving_data),
0.4,
0.45,
)

moving2_path = self.get_image_path('moving2.nii.gz')
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ def test_get_brain_volume_stats(self):
brain_seg = np.array([0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0])
tissue_seg = np.array([0, 0, 1, 2, 3, 4, 5, 6, 1, 0, 0])
ventricles_seg = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
template_images = {
'template': np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 0.0]),
'lobes': np.array([0, 1, 2, 3, 4, 5, 6, 6, 2, 3]),
'CorpusCallosum': np.array([0, 1, 2, 3, 4, 5, 4, 3, 0, 0]),
}

task = Quantification(
pipeline_dir=self.pipeline_dir,
Expand All @@ -593,7 +598,7 @@ def test_get_brain_volume_stats(self):
)

volume_stats = task.get_brain_volume_stats(
brain_seg, tissue_seg, ventricles_seg, voxel_volume=2.0
brain_seg, tissue_seg, ventricles_seg, template_images, voxel_volume=2.0
)

self.assertEqual(14.0, volume_stats['total_brain_volume'])
Expand All @@ -605,3 +610,16 @@ def test_get_brain_volume_stats(self):
self.assertEqual(2.0, volume_stats['volume_of_deep_gray_matter'])
self.assertEqual(2.0, volume_stats['volume_of_brain_stem'])
self.assertEqual(2.0, volume_stats['volume_of_cerebellum'])
self.assertEqual(2.0, volume_stats['volume_of_Frontal'])
self.assertEqual(4.0, volume_stats['volume_of_Parietal'])
self.assertEqual(4.0, volume_stats['volume_of_Occipital'])
self.assertEqual(2.0, volume_stats['volume_of_AnteriorTemporal'])
self.assertEqual(2.0, volume_stats['volume_of_MiddleTemporal'])
self.assertEqual(4.0, volume_stats['volume_of_PosteriorTemporal'])
self.assertEqual(8.0, volume_stats['volume_of_Parietal_Occipital'])
self.assertEqual(14.0, volume_stats['volume_of_CorpusCallosum'])
self.assertEqual(2.0, volume_stats['volume_of_CorpusCallosum_Rostrum'])
self.assertEqual(2.0, volume_stats['volume_of_CorpusCallosum_Genu'])
self.assertEqual(4.0, volume_stats['volume_of_CorpusCallosum_Body'])
self.assertEqual(4.0, volume_stats['volume_of_CorpusCallosum_Isthmus'])
self.assertEqual(2.0, volume_stats['volume_of_CorpusCallosum_Splenium'])

0 comments on commit 8d9410a

Please sign in to comment.