Skip to content

Commit

Permalink
Update imio dependency (#181)
Browse files Browse the repository at this point in the history
* update imio -> brainglobe_utils.image_io everywhere

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
willGraham01 and pre-commit-ci[bot] authored Feb 9, 2024
1 parent 89101e7 commit 6fa87eb
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 38 deletions.
38 changes: 18 additions & 20 deletions brainreg/core/backend/niftyreg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from pathlib import Path

import brainglobe_space as bg
import imio
import numpy as np
from bg_atlasapi import BrainGlobeAtlas
from brainglobe_utils.general.system import delete_directory_contents
from brainglobe_utils.image.scale import scale_and_convert_to_16_bits
from brainglobe_utils.image_io import load_any, to_tiff

from brainreg.core.backend.niftyreg.parameters import RegistrationParams
from brainreg.core.backend.niftyreg.paths import NiftyRegPaths
Expand Down Expand Up @@ -72,7 +72,7 @@ def run_niftyreg(
save_nii(reference, atlas.resolution, niftyreg_paths.brain_filtered)
save_nii(target_brain, atlas.resolution, niftyreg_paths.downsampled_brain)

imio.to_tiff(
to_tiff(
scale_and_convert_to_16_bits(target_brain),
paths.downsampled_brain_path,
)
Expand Down Expand Up @@ -125,32 +125,30 @@ def run_niftyreg(
brain_reg.generate_deformation_field(niftyreg_paths.deformation_field)

logging.info("Exporting images as tiff")
imio.to_tiff(
imio.load_any(niftyreg_paths.registered_atlas_path).astype(
to_tiff(
load_any(niftyreg_paths.registered_atlas_path).astype(
np.uint32, copy=False
),
paths.registered_atlas,
)

if save_original_orientation:
registered_atlas = imio.load_any(
registered_atlas = load_any(
niftyreg_paths.registered_atlas_path
).astype(np.uint32, copy=False)
atlas_remapped = bg.map_stack_to(
ATLAS_ORIENTATION, DATA_ORIENTATION, registered_atlas
).astype(np.uint32, copy=False)
imio.to_tiff(
atlas_remapped, paths.registered_atlas_original_orientation
)
to_tiff(atlas_remapped, paths.registered_atlas_original_orientation)

imio.to_tiff(
imio.load_any(niftyreg_paths.registered_hemispheres_img_path).astype(
to_tiff(
load_any(niftyreg_paths.registered_hemispheres_img_path).astype(
np.uint8, copy=False
),
paths.registered_hemispheres,
)
imio.to_tiff(
imio.load_any(niftyreg_paths.downsampled_brain_standard_space).astype(
to_tiff(
load_any(niftyreg_paths.downsampled_brain_standard_space).astype(
np.uint16, copy=False
),
paths.downsampled_brain_standard_space,
Expand All @@ -159,16 +157,16 @@ def run_niftyreg(
del reference
del target_brain

deformation_image = imio.load_any(niftyreg_paths.deformation_field)
imio.to_tiff(
deformation_image = load_any(niftyreg_paths.deformation_field)
to_tiff(
deformation_image[..., 0, 0].astype(np.float32, copy=False),
paths.deformation_field_0,
)
imio.to_tiff(
to_tiff(
deformation_image[..., 0, 1].astype(np.float32, copy=False),
paths.deformation_field_1,
)
imio.to_tiff(
to_tiff(
deformation_image[..., 0, 2].astype(np.float32, copy=False),
paths.deformation_field_2,
)
Expand Down Expand Up @@ -201,7 +199,7 @@ def run_niftyreg(
)

# do the tiff part at the beginning
downsampled_brain = imio.load_any(
downsampled_brain = load_any(
filename,
scaling[1],
scaling[2],
Expand All @@ -219,16 +217,16 @@ def run_niftyreg(
downsampled_brain, atlas.resolution, tmp_downsampled_brain_path
)

imio.to_tiff(downsampled_brain, downsampled_brain_path)
to_tiff(downsampled_brain, downsampled_brain_path)

logging.info("Transforming to standard space")

brain_reg.transform_to_standard_space(
tmp_downsampled_brain_path, tmp_downsampled_brain_standard_path
)

imio.to_tiff(
imio.load_any(tmp_downsampled_brain_standard_path).astype(
to_tiff(
load_any(tmp_downsampled_brain_standard_path).astype(
np.uint16, copy=False
),
downsampled_brain_standard_path,
Expand Down
4 changes: 2 additions & 2 deletions brainreg/core/backend/niftyreg/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import imio
import numpy as np
from brainglobe_utils.image_io import to_nii


def save_nii(stack, atlas_pixel_sizes, dest_path):
Expand All @@ -11,7 +11,7 @@ def save_nii(stack, atlas_pixel_sizes, dest_path):
:param str dest_path: Where to save the image on the filesystem
"""
transformation_matrix = get_transf_matrix_from_res(atlas_pixel_sizes)
imio.to_nii(
to_nii(
stack,
dest_path,
scale=(
Expand Down
7 changes: 4 additions & 3 deletions brainreg/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def search_dir_for_image_files(target_brain_path):
class LoadFileException(Exception):
"""
Custom exception class for errors found loading image with
imio.load_any (in main.py).
brainglobe_utils.image_io.load_any (in main.py).
If the passed target brain directory contains only a single
.tiff, alert the user.
Expand All @@ -58,8 +58,9 @@ def __init__(self, error_type=None, base_error=None):
)
self.message = (
f"{origional_traceback}\nFile failed to load with "
f"imio. Ensure all image files contain the "
f"same number of pixels. Full traceback above."
"brainglobe_utils.image_io. "
"Ensure all image files contain the "
"same number of pixels. Full traceback above."
)

super().__init__(self.message)
4 changes: 2 additions & 2 deletions brainreg/core/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging

import brainglobe_space as bg
import imio
from bg_atlasapi import BrainGlobeAtlas
from brainglobe_utils.general.system import get_num_processes
from brainglobe_utils.image_io import load_any

from brainreg.core.backend.niftyreg.run import run_niftyreg
from brainreg.core.exceptions import (
Expand Down Expand Up @@ -55,7 +55,7 @@ def main(
logging.info("Loading raw image data")

try:
target_brain = imio.load_any(
target_brain = load_any(
target_brain_path,
scaling[1],
scaling[2],
Expand Down
6 changes: 3 additions & 3 deletions brainreg/core/utils/boundaries.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

import imio
import numpy as np
from brainglobe_utils.image_io import load_any, to_tiff
from skimage.segmentation import find_boundaries


Expand All @@ -14,9 +14,9 @@ def boundaries(registered_atlas, boundaries_out_path):
:param registered_atlas: The registered atlas
:param boundaries_out_path: Path to save the boundary image
"""
atlas_img = imio.load_any(registered_atlas)
atlas_img = load_any(registered_atlas)
boundaries_image = find_boundaries(atlas_img, mode="inner").astype(
np.int8, copy=False
)
logging.debug("Saving segmentation boundary image")
imio.to_tiff(boundaries_image, boundaries_out_path)
to_tiff(boundaries_image, boundaries_out_path)
6 changes: 3 additions & 3 deletions brainreg/core/utils/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import logging

import imio
import numpy as np
import pandas as pd
from brainglobe_utils.image_io import load_any
from brainglobe_utils.pandas.misc import initialise_df, safe_pandas_concat


Expand Down Expand Up @@ -41,8 +41,8 @@ def get_lateralised_atlas(
left_hemisphere_value=2,
right_hemisphere_value=1,
):
atlas = imio.load_any(atlas_path)
hemispheres = imio.load_any(hemispheres_path)
atlas = load_any(atlas_path)
hemispheres = load_any(hemispheres_path)

atlas_left, atlas_right = lateralise_atlas(
atlas,
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ requires-python = ">=3.9"
dependencies = [
"bg-atlasapi",
"brainglobe-space>=1.0.0",
"brainglobe-utils>=0.2.7",
"brainglobe-utils>=0.4.0",
"fancylog",
"imio",
"numpy",
"scikit-image",
]
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/test_integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import pandas as pd
from imio.load import load_any
from brainglobe_utils.image_io import load_any

relative_tolerance = 0.01
absolute_tolerance = 10
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/test_unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def test_mismatched_dims_error(test_output_dir):
brainreg_run()

assert (
"File failed to load with "
"imio. Ensure all image files contain the "
"File failed to load with brainglobe_utils.image_io. "
"Ensure all image files contain the "
"same number of pixels. Full traceback above." in e.value.message
)

Expand Down

0 comments on commit 6fa87eb

Please sign in to comment.