Skip to content

[BUG] brainreg-napari rescale fails when stack opened using "Open files as stack" #184

@alessandrofelder

Description

@alessandrofelder

Describe the bug
Opening an image stack with "Open files as stack" and then running brainreg in napari on the stack fails with trace below

--> 446 run_niftyreg(
        registration_output_folder = PosixPath('/home/alessandro')
        paths = <brainreg.core.paths.Paths object at 0x7de5b8ded630>
        atlas = allen mouse atlas (res. 25um)
        target_brain = <class 'numpy.ndarray'> (510, 396, 310) float64
        n_processes = 18
        additional_images_downsample = {'section_001_01': '/media/alessandro/T7/brainglobe-course-data/MS_cx_left/stitchedImages_100/2/section_001_01.tif'}
        data_orientation = 'psl'
        atlas.metadata["orientation"] = 'asr'
        atlas.metadata = {'name': 'allen_mouse', 'citation': 'Wang et al 2020, https://doi.org/10.1016/j.cell.2020.04.007', 'atlas_link': 'http://www.brain-map.org', 'species': 'Mus musculus', 'symmetric': True, 'resolution': [25.0, 25.0, 25.0], 'orientation': 'asr', 'version': '1.2', 'shape': [528, 320, 456], 'trasform_to_bg': [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]], 'additional_references': []}
        niftyreg_args = NiftyregArgs(affine_n_steps=6, affine_use_n_steps=5, freeform_n_steps=6, freeform_use_n_steps=4, bending_energy_weight=0.95, grid_spacing=-10, smoothing_sigma_reference=-1, smoothing_sigma_floating=-1.0, histogram_n_bins_floating=128.0, histogram_n_bins_reference=128.0, debug=False)
        PRE_PROCESSING_ARGS = None
        scaling = [0.2, 0.08, 0.08]
        load_parallel = True
        sort_input_file = False
        n_free_cpus = 2
        save_original_orientation = False
        brain_geometry = <geometry_keys.Full brain: 'full'>
    447     registration_output_folder,
    448     paths,
    449     atlas,
    450     target_brain,
    451     n_processes,
    452     additional_images_downsample,
    453     data_orientation,
    454     atlas.metadata["orientation"],
    455     niftyreg_args,
    456     PRE_PROCESSING_ARGS,
    457     scaling,
    458     load_parallel,
    459     sort_input_file,
    460     n_free_cpus,
    461     save_original_orientation=save_original_orientation,
    462     brain_geometry=brain_geometry.value,
    463 )
    465 logging.info("Calculating volumes of each brain area")
    466 calculate_volumes(
    467     atlas,
    468     paths.registered_atlas,
   (...)
    473     right_hemisphere_value=2,
    474 )

File ~/dev/brainreg/brainreg/core/backend/niftyreg/run.py:202, in run_niftyreg(***failed resolving arguments***)
    196 tmp_downsampled_brain_standard_path = os.path.join(
    197     niftyreg_paths.niftyreg_directory,
    198     f"downsampled_standard_{name_to_save}.nii",
    199 )
    201 # do the tiff part at the beginning
--> 202 downsampled_brain = load_any(
        filename = '/media/alessandro/T7/brainglobe-course-data/MS_cx_left/stitchedImages_100/2/section_001_01.tif'
        scaling[1] = 0.08
        scaling = [0.2, 0.08, 0.08]
        scaling[2] = 0.08
        scaling[0] = 0.2
        load_parallel = True
        sort_input_file = False
        n_free_cpus = 2
    203     filename,
    204     scaling[1],
    205     scaling[2],
    206     scaling[0],
    207     load_parallel=load_parallel,
    208     sort_input_file=sort_input_file,
    209     n_free_cpus=n_free_cpus,
    210 )
    212 downsampled_brain = bg.map_stack_to(
    213     DATA_ORIENTATION, ATLAS_ORIENTATION, downsampled_brain
    214 ).astype(np.uint16, copy=False)
    216 save_nii(
    217     downsampled_brain, atlas.resolution, tmp_downsampled_brain_path
    218 )

File ~/mambaforge/envs/brainreg/lib/python3.10/site-packages/brainglobe_utils/image_io/load.py:94, in load_any(src_path='/media/alessandro/T7/brainglobe-course-data/MS_cx_left/stitchedImages_100/2/section_001_01.tif', x_scaling_factor=0.08, y_scaling_factor=0.08, z_scaling_factor=0.2, anti_aliasing=True, load_parallel=True, sort_input_file=False, as_numpy=False, n_free_cpus=2)
     92 elif src_path.endswith((".tif", ".tiff")):
     93     logging.debug("Data type is: tif stack")
---> 94     img = load_img_stack(
        src_path = '/media/alessandro/T7/brainglobe-course-data/MS_cx_left/stitchedImages_100/2/section_001_01.tif'
        x_scaling_factor = 0.08
        y_scaling_factor = 0.08
        z_scaling_factor = 0.2
        anti_aliasing = True
     95         src_path,
     96         x_scaling_factor,
     97         y_scaling_factor,
     98         z_scaling_factor,
     99         anti_aliasing=anti_aliasing,
    100     )
    101 elif src_path.endswith(".nrrd"):
    102     logging.debug("Data type is: nrrd")

File ~/mambaforge/envs/brainreg/lib/python3.10/site-packages/brainglobe_utils/image_io/load.py:163, in load_img_stack(stack_path='/media/alessandro/T7/brainglobe-course-data/MS_cx_left/stitchedImages_100/2/section_001_01.tif', x_scaling_factor=0.08, y_scaling_factor=0.08, z_scaling_factor=0.2, anti_aliasing=True)
    160 logging.debug("Downsampling stack in X/Y")
    161 for plane in tqdm(range(0, len(stack))):
    162     downsampled_stack.append(
--> 163         transform.rescale(
        downsampled_stack = []
        transform = <module 'skimage.transform' from '/home/alessandro/mambaforge/envs/brainreg/lib/python3.10/site-packages/skimage/transform/__init__.py'>
        stack = <class 'numpy.ndarray'> (4949, 3873) uint16
        plane = 0
        x_scaling_factor = 0.08
        y_scaling_factor = 0.08
        (y_scaling_factor, x_scaling_factor) = (0.08, 0.08)
        anti_aliasing = True
    164             stack[plane],
    165             (y_scaling_factor, x_scaling_factor),
    166             mode="constant",
    167             preserve_range=True,
    168             anti_aliasing=anti_aliasing,
    169         )
    170     )
    172 logging.debug("Converting downsampled stack to array")
    173 stack = np.array(downsampled_stack)

File ~/mambaforge/envs/brainreg/lib/python3.10/site-packages/skimage/_shared/utils.py:316, in channel_as_last_axis.__call__.<locals>.fixed_func(*args=(<class 'numpy.ndarray'> (3873,) uint16, (0.08, 0.08)), **kwargs={'anti_aliasing': True, 'mode': 'constant', 'preserve_range': True})
    313 channel_axis = kwargs.get('channel_axis', None)
    315 if channel_axis is None:
--> 316     return func(*args, **kwargs)
        func = <function rescale at 0x7de5a7cdeef0>
        args = (<class 'numpy.ndarray'> (3873,) uint16, (0.08, 0.08))
        kwargs = {'mode': 'constant', 'preserve_range': True, 'anti_aliasing': True}
    318 # TODO: convert scalars to a tuple in anticipation of eventually
    319 #       supporting a tuple of channel axes. Right now, only an
    320 #       integer or a single-element tuple is supported, though.
    321 if np.isscalar(channel_axis):

File ~/mambaforge/envs/brainreg/lib/python3.10/site-packages/skimage/transform/_warps.py:280, in rescale(image=<class 'numpy.ndarray'> (3873,) uint16, scale=<class 'numpy.ndarray'> (2,) float64, order=None, mode='constant', cval=0, clip=True, preserve_range=True, anti_aliasing=True, anti_aliasing_sigma=None, channel_axis=None)
    277 if len(scale) > 1:
    278     if ((not multichannel and len(scale) != image.ndim) or
    279             (multichannel and len(scale) != image.ndim - 1)):
--> 280         raise ValueError("Supply a single scale, or one value per spatial "
    281                          "axis")
    282     if multichannel:
    283         scale = np.concatenate((scale, [1]))

But brainreg runs fine if you use Open Folder instead.

To Reproduce

  1. Open napari with brainreg-napari installed
  2. Run Open files as stack and select all files in a folder
  3. Run brainreg

Expected behaviour
Brainreg should work irrespective of how the image stack was opened.

Log file
n/a

Screenshots
n/a

Computer used (please complete the following information):

Platform: Linux-6.5.0-25-generic-x86_64-with-glibc2.35
System: Ubuntu 22.04.4 LTS
Python: 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) [GCC 12.3.0]
Qt: 5.15.2
PyQt5: 5.15.10
NumPy: 1.26.4
SciPy: 1.12.0
Dask: 2024.3.0
VisPy: 0.14.1
magicgui: 0.8.2
superqt: 0.6.2
in-n-out: 0.1.9
app-model: 0.2.4
npe2: 0.7.4

OpenGL:
  - GL version:  4.6.0 NVIDIA 535.161.07
  - MAX_TEXTURE_SIZE: 32768

Screens:
  - screen 1: resolution 3440x1440, scale 1.0

Settings path:
  - /home/alessandro/.config/napari/brainreg_fae7ff8a72d2d6e4cddefe1455346cfd57814491/settings.yaml
Plugins:
  - brainglobe-napari-io: 0.3.4 (10 contributions)
  - brainglobe-segmentation: 1.2.3 (2 contributions)
  - brainreg: 1.0.7.dev1+g31e2111 (4 contributions)
  - napari: 0.4.19.post1 (77 contributions)
  - napari-console: 0.0.9 (0 contributions)
  - napari-svg: 0.1.10 (2 contributions)

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions