Skip to content

Commit

Permalink
New loader: Load PNG/TIF/JPG microscopy files as Nibabel objects (#813)
Browse files Browse the repository at this point in the history
* Remove apply_canonical function, revert to nib.as_closest_canonical

* Remove get_voxel_size, revert to header.get_zooms

* Remove get_shape, revert to header.get_data_shape

* Remove get_data, revert to nibabel get_fdata

* In read_file, convert png/tif/jpg to Nifti1Image object

* Get pixel size from json metadata and set zooms in nifti header

* loader/utils.py: Correction to filtering of extensions in bids_dataframe
(was not filtering derivatives extensions before)

* Update read_file in SegmentationPair to convert PNG to Nibabel object
and write Nifti file

* configuration_file.rst: Update extensions definition and example

* Move file extension check from SegmentationPair to get_file_extension in
loader/utils.py

* Add update_filename_to_nifti function to loader/utils.py

* test_loader.py: update test_load_dataset_2d_png

* testing.py: Check extentions and update paths if not NifTI in get_gt

* evaluation.py: Check extentions and update paths if not NifTI in
evaluate

* inference.py: Check extentions and update paths if not NifTI in
pred_to_nib

* Add TODO comment for inference in main.py

* Add .tif and .tiff microscopy extensions to bids_dataframe

* loader/utils.py: Correction to update_filename_to_nifti

* Update config_new_loader.json to test the PR

* Set self.extensions to nii and nii.gz by default in BidsDataframe when
no extensions are provided in config

* Update docs/source/configuration_file.rst

Co-authored-by: Joshua Newton <joshuacwnewton@gmail.com>

* Move file extension validation to read_file in SegmentationPair

* Use update_filename_to_nifti to update path in read_file

* Remove if self.extensions condition in filtering of bids_dataframe
(always true)

* Correction to PixelSize assignment and comments

* Add convert_file_to_nifti method to SegmentationPair

* Update update_filename_to_nifti docstings

Co-authored-by: Joshua Newton <joshuacwnewton@gmail.com>

* Update read_file docstrings

* Add list of supported file extensions in error message in read_file

* Fix resize of PixelSize

Co-authored-by: Joshua Newton <joshuacwnewton@gmail.com>
  • Loading branch information
mariehbourget and joshuacwnewton committed Jun 14, 2021
1 parent 1d81b32 commit 805b394
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 165 deletions.
4 changes: 2 additions & 2 deletions docs/source/configuration_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ will be randomly chosen.
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "extensions",
"description": "Used to specify a list of file extensions to be selected for
training/testing.",
training/testing. If not specified, then `.nii` and `.nii.gz` will be used by default.",
"type": "list, string"
}

Expand All @@ -297,7 +297,7 @@ will be randomly chosen.
{
"loader_parameters": {
"extensions": [".nii.gz"]
"extensions": [".png"]
}
}
Expand Down
25 changes: 13 additions & 12 deletions ivadomed/config/config_new_loader.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
"safety_factor": [1.0, 1.0, 1.0]
},
"loader_parameters": {
"path_data": ["data_example_spinegeneric"],
"path_data": ["data_example_microscopy_sem"],
"bids_config": "ivadomed/config/config_bids.json",
"subject_selection": {"n": [], "metadata": [], "value": []},
"target_suffix": ["_seg-manual"],
"extensions": [],
"target_suffix": ["_seg-axon-manual", "_seg-myelin-manual"],
"extensions": [".png"],
"roi_params": {
"suffix": null,
"slice_filter_roi": null
},
"contrast_params": {
"training_validation": ["T2w"],
"testing": ["T2w"],
"training_validation": ["SEM"],
"testing": ["SEM"],
"balance": {}
},
"slice_filter_params": {
Expand All @@ -34,19 +34,19 @@
"split_dataset": {
"fname_split": null,
"random_seed": 6,
"split_method" : "participant_id",
"split_method" : "sample_id",
"data_testing": {"data_type": null, "data_value":[]},
"balance": null,
"train_fraction": 0.1,
"test_fraction": 0.8
"train_fraction": 0.2,
"test_fraction": 0.2
},
"training_parameters": {
"batch_size": 18,
"loss": {
"name": "DiceLoss"
},
"training_time": {
"num_epochs": 1,
"num_epochs": 5,
"early_stopping_patience": 50,
"early_stopping_epsilon": 0.001
},
Expand Down Expand Up @@ -75,8 +75,8 @@
"bn_momentum": 0.1,
"final_activation": "sigmoid",
"depth": 3,
"length_2D": [50, 50],
"stride_2D": [45, 45]
"length_2D": [],
"stride_2D": []
},
"FiLMedUnet": {
"applied": false,
Expand All @@ -85,7 +85,8 @@
},
"transformation": {
"CenterCrop": {
"size": [128, 128]},
"size": [256, 256]
},
"NumpyToTensor": {},
"NormalizeInstance": {"applied_to": ["im"]}
}
Expand Down
4 changes: 4 additions & 0 deletions ivadomed/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ivadomed import metrics as imed_metrics
from ivadomed import postprocessing as imed_postpro
from ivadomed.loader import utils as imed_loader_utils

# labels of paint_objects method
TP_COLOUR = 1
Expand Down Expand Up @@ -52,6 +53,9 @@ def evaluate(bids_df, path_output, target_suffix, eval_params):
fname_gt = bids_df.df[bids_df.df['filename']
.str.contains('|'.join(bids_df.get_derivatives(subj_acq, all_deriv)))]['path'].to_list()

# Check fname_gt extentions and update paths if not NifTI
fname_gt = [imed_loader_utils.update_filename_to_nifti(fname) for fname in fname_gt]

# Uncertainty
data_uncertainty = None

Expand Down
7 changes: 6 additions & 1 deletion ivadomed/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def pred_to_nib(data_lst: List[np.ndarray], z_lst: List[int], fname_ref: str, fn
Returns:
nibabel.Nifti1Image: NiBabel object containing the Network prediction.
"""

# Check fname_ref extention and update path if not NifTI
fname_ref = imed_loader_utils.update_filename_to_nifti(fname_ref)

# Load reference nibabel object
nib_ref = nib.load(fname_ref)
nib_ref_can = nib.as_closest_canonical(nib_ref)
Expand Down Expand Up @@ -192,7 +196,7 @@ def pred_to_nib(data_lst: List[np.ndarray], z_lst: List[int], fname_ref: str, fn
affine=None,
header=nib_ref.header.copy()
)
# save as nifti file
# save as NifTI file
if fname_out is not None:
nib.save(nib_pred, fname_out)

Expand Down Expand Up @@ -341,6 +345,7 @@ def segment_volume(folder_model: str, fname_images: list, gpu_id: int = 0, optio
logger.warning("fname_roi has not been specified, then the entire volume is processed.")
loader_params["slice_filter_params"]["filter_empty_mask"] = False

# TODO: Add PixelSize from options to filename_pairs metadata for microscopy inference (issue #306)
filename_pairs = [(fname_images, None, fname_roi, metadata if isinstance(metadata, list) else [metadata])]

kernel_3D = bool('Modified3DUNet' in context and context['Modified3DUNet']['applied']) or \
Expand Down

0 comments on commit 805b394

Please sign in to comment.