Skip to content

Commit

Permalink
Merge pull request #40 from reghbali/reghbali_test_qunatification_tas…
Browse files Browse the repository at this point in the history
…k_run

Reghbali test qunatification task run
  • Loading branch information
reghbali authored Sep 19, 2023
2 parents 8d9410a + f00f22e commit b11864d
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ jobs:
cp c3d*/bin/c3d $BIN/c3d
fi
- name: Download models
run: |
pyalfe download models
- name: Run tests
run: |
pytest tests/integration
2 changes: 1 addition & 1 deletion pyalfe/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from pathlib import Path

models_url = 'https://ucsf.box.com/shared/static/' 'a93ruk3m26mso38jm8gk3qs5iod0h90h.gz'
models_url = 'https://ucsf.box.com/shared/static/a93ruk3m26mso38jm8gk3qs5iod0h90h.gz'

MODELS_PATH = Path(os.path.expanduser(os.path.join('~', '.cache', 'pyalfe')))

Expand Down
86 changes: 51 additions & 35 deletions pyalfe/tasks/quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Quantification(Task):
All the modalities for processed by the pipeline.
modalities_target: list[Modality]
Target modalities that are used to defince lesions.
dominant_tissue: bool
dominant_tissue: str
The dominant tissue where the tumor or lesion is expected to be
located at. THe options are `white_matter`, `gray_matter`, `auto`.
If `auto` is chosen, then the dominant tissue is chosen to be the tissue
Expand All @@ -41,7 +41,7 @@ def __init__(
pipeline_dir: PipelineDataDir,
modalities_all: list[Modality],
modalities_target: list[Modality],
dominant_tissue: bool = None,
dominant_tissue: str = None,
):
self.modalities_all = modalities_all
self.modalities_target = modalities_target
Expand Down Expand Up @@ -81,26 +81,33 @@ def load_modality_images(self, accession, target):

if os.path.exists(modality_image_to_target_file):
modality_images[modality], _ = self.load(modality_image_to_target_file)
else:
modality_images[modality] = None
return modality_images

def load_template_images(self, accession, target):
def load_template_images(self, accession, modality):
target_images = {}

for roi_key, roi_properties in roi_dict.items():

roi_sub_dir = roi_properties['sub_dir']

if roi_properties['type'] == 'template':
template_image_to_target_file = self.pipeline_dir.get_output_image(
accession,
modality=target,
image_type=roi_key,
resampling_target=target,
resampling_origin=Modality.T1,
sub_dir_name=roi_sub_dir,
)
if modality == Modality.T1:
template_image_to_target_file = self.pipeline_dir.get_output_image(
accession,
modality=modality,
resampling_target=modality,
resampling_origin=roi_key,
sub_dir_name=roi_sub_dir,
)
else:
template_image_to_target_file = self.pipeline_dir.get_output_image(
accession,
modality=modality,
image_type=roi_key,
resampling_target=modality,
resampling_origin=Modality.T1,
sub_dir_name=roi_sub_dir,
)

if os.path.exists(template_image_to_target_file):
target_images[roi_key], _ = self.load(template_image_to_target_file)
Expand All @@ -112,7 +119,6 @@ def load_template_images(self, accession, target):
self.logger.info(
'missing template: ' + template_image_to_target_file
)
target_images[roi_key] = None
return target_images

def get_radiomics(self, skullstripped_file, lesion_seg_file):
Expand Down Expand Up @@ -241,6 +247,11 @@ def get_lesion_stats(
stats['enhancement'] = np.mean(t1post_image[lesion_indices]) / np.mean(
t1_image[lesion_indices]
)
else:
self.logger.info(
'Cannot quantify avg enhancement over the leasion because T1'
' or T1Post are not registered to target modality'
)

if ventricles_distance is not None:
stats['average_dist_to_ventricles_(voxels)'] = np.mean(
Expand Down Expand Up @@ -284,32 +295,38 @@ def run(self, accession):
accession=accession, modality=Modality.T1, image_type='skullstripping_mask'
)

brain_mask, voxel_volume = self.load(brain_mask_file)
if not os.path.exists(brain_mask_file):
self.logger.info(
'T1 skullstripping mask is missing.'
'Skipping brain volume quantification.'
)
else:
brain_mask, voxel_volume = self.load(brain_mask_file)

tissue_seg_file = self.pipeline_dir.get_output_image(
accession=accession, modality=Modality.T1, image_type='tissue_seg'
)
tissue_seg_file = self.pipeline_dir.get_output_image(
accession=accession, modality=Modality.T1, image_type='tissue_seg'
)

if not os.path.exists(tissue_seg_file):
tissue_seg = None
else:
tissue_seg, _ = self.load(tissue_seg_file)
if not os.path.exists(tissue_seg_file):
tissue_seg = None
else:
tissue_seg, _ = self.load(tissue_seg_file)

template_images = self.load_template_images(accession, Modality.T1)
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'
)
ventricles_seg_file = self.pipeline_dir.get_output_image(
accession=accession, modality=Modality.T1, image_type='VentriclesSeg'
)

if not os.path.exists(ventricles_seg_file):
ventricles_seg = None
else:
ventricles_seg, _ = self.load(ventricles_seg_file)
if not os.path.exists(ventricles_seg_file):
ventricles_seg = None
else:
ventricles_seg, _ = self.load(ventricles_seg_file)

volume_stats = self.get_brain_volume_stats(
brain_mask, tissue_seg, ventricles_seg, voxel_volume
)
pd.Series(volume_stats).to_csv(volumetric_quantification_file)
volume_stats = self.get_brain_volume_stats(
brain_mask, tissue_seg, ventricles_seg, template_images, voxel_volume
)
pd.Series(volume_stats).to_csv(volumetric_quantification_file)

for target in self.modalities_target:
summary_quantification_file = self.pipeline_dir.get_quantification_file(
Expand Down Expand Up @@ -356,7 +373,6 @@ def run(self, accession):
)

if not os.path.exists(tissue_seg_file):
print('no tissue seg', tissue_seg_file)
self.logger.info(
f'Tissue seg file for {target} is missing.'
f'Skipping quantification for {target}.'
Expand Down
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.45,
0.47,
)

moving2_path = self.get_image_path('moving2.nii.gz')
Expand Down
Loading

0 comments on commit b11864d

Please sign in to comment.