Skip to main content

Ryomen

Ryomen is a lightweight, no dependency utility for working with large biological microscopy images.

Why do we need this?

Modern microscopes can image cells, tissue, or whatever else, over huge spatial areas, resulting in images that can be quite large (hundreds of gigabytes). This can make applying image processing or deep learning algorithms difficult. Ryomen makes this easy by automatically slicing up large images and providing an easy-to-use interface for working with the crops. Check out the docs here: https://ryomen.readthedocs.io/

What can it do?

Ryomen works with N dimensional, arbitrary array types, including numpy, zarr, pytorch, or anything else. It simplifies the process of slicing large images, with slices that may overlap, applying a function to the image, and extracting the non-overlapping subregions. First, install with pip: pip install ryomen. The following example crops a large image into 512x512 crops overlapping by 64px, aplies a hard to run function expensive_fn, and takes the result and puts it into a pre-allocated output array.

crop_size = (512, 512)
overlap = (64, 64)
slices = ryomen.Slicer(image, crop_size=crop_size, overlap=overlap)

for crop, source, destination in slices:
    # Run the expensive fn on a crop of the whole image
    crop = expensive_fn(crop)
    
    # Slot the crop into an output array, excluding the overlap
    output[destination] = crop[source]  

A full code example might look like this:

import skimage.io as io
import numpy as np
import zarr
from ryomen import Slicer

image = io.imread("really_huge_8bit_2d_image.tif")  # Shape of [3, 10000, 10000] 
c, x, y = image.shape
output: zarr.Array = zarr.open(
    'really_huge_output_on_disk.zarr',
    mode="w",
    shape=(c, x, y),
    dtype="|f2",
)

expensive_fn = lambda x: x + 1  # might be a ML model, or anything else

crop_size = (512, 512)
overlap = (64, 64)
slices = Slicer(image, crop_size=crop_size, overlap=overlap)
for crop, source, destination in slices:
    # Run the expensive fn on a crop of the whole image
    crop = expensive_fn(crop)
    
    # Slot the crop into an output array, excluding the overlap
    output[destination] = crop[source]  

You can apply arbitrary functions to a crop after it has been extracted from the large image. This is useful for working with a large uint8 image that might need to be cast to float for a ML model.

import skimage.io as io
import numpy as np
import zarr
from ryomen import Slicer

image = io.imread("really_huge_8bit_3d_image.tif")  # Shape of [3, 10000, 10000, 500] 
c, x, y, z = image.shape
output: zarr.Array = zarr.open(
    'really_huge_output_on_disk.zarr',
    mode="w",
    shape=(c, x, y, z),
    dtype="|f2",
)
crop_size = (512, 512, 64)
overlap = (64, 64, 8)
# Output transform will be applied to each crop.
output_transform = lambda x: x.astype(float).div(255).sub(0.5).mul(2)  # cast to float an normalize 

slices = Slicer(image, crop_size=crop_size, overlap=overlap, output_transform=output_transform)

expensive_fn = lambda x: x + 1  # might be a ML model, or anything else

for crop, source, destination in slices:
    assert crop.max() <= 1  # True
    assert crop.dtype() == np.float32  # True
    
    # Run the expensive fn on a crop of the whole image
    crop = expensive_fn(crop)
    
    # Slot the crop into an output array, excluding the overlap
    output[destination] = crop[source]  

Finally, ryomen can automatically batch tiles if you wanted to run each through a ML model. By default, a list of tensors will be returned, however, you may suply a custom collate fn to handle batched inputs.

import skimage.io as io
import torch
from torch import nn as nn
import zarr
from ryomen import Slicer

image = io.imread("really_huge_8bit_2d_image.tif")  # Shape of [3, 10000, 10000] 
c, x, y = image.shape
output: zarr.Array = zarr.open(
    'really_huge_output_on_disk.zarr',
    mode="w",
    shape=(c, x, y),
    dtype="|f2",
)
crop_size = (512, 512, 64)
overlap = (64, 64, 8)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
batch_size = 64

# Output transform will be applied to each crop.
output_transform = lambda x: torch.from_numpy(x).to(device).div(255).sub(0.5).div(0.5)  # cast to cuda tensor an normalize 
collate = lambda x: torch.stack(x, dim=0)  # turns a list of tensors to one tensor

slices = Slicer(image, 
                crop_size=crop_size, 
                overlap=overlap, 
                output_transform=output_transform, 
                batch_size=64,
                collate=collate)

pytorch_model = lambda x: nn.Conv3d(3, 1, kernel_size=3)

for crop, source, destination in slices:
    assert crop.ndim == 4  # crop shape is -> [B=64, 3, 512, 512]
    
    # Run the expensive fn on a crop of the whole image
    crop = pytorch_model(crop)
    
    # Slot the batched crop into an output array, excluding the overlap
    for b, (_source, _destination) in enumerate(zip(source, destination)):
        output[_destination] = crop[b, ...][_source]  

Change Log

0.0.8

- bugfix where entire array was being deepcopied, leading to poor performance on large tensors.

0.0.7

- small performance inprovements

0.0.6

- fixed bug where leading dimensions break the slicer when padding is applied
- changed padding to be on by default
- refactored for code clarity

0.0.5

- fixed bug with duplicate indices being shown, leading to redundant behaviro
- added support for zarr arrays, as they do not implement a flip method or support negative indexing
- refactored for readability

0.0.4

- padding now works with pure indexing, for reflections. Tested up to 3D with numpy. 

0.0.3

- First working version

Ryomen is titled after the first name of the main antagonist of JJK who slices up everything.

Metadata

Release files for ryomen 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ryomen 0.0.10
File Size Uploaded Provenance Actions
ryomen-0.0.10.tar.gz 17.8 kB Without provenance Details for ryomen-0.0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ryomen 0.0.10
File Interpreter ABI Platform Actions
ryomen-0.0.10-py3-none-any.whl Python 3 none any Details for ryomen-0.0.10-py3-none-any.whl

Total release size: 34.9 kB

Release files / ryomen-0.0.10.tar.gz

Download URL ryomen-0.0.10.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
54d582381265bee15f3f8a1392460a3bc9c67ba670d7185b7f5d2c308d39b66e
BLAKE2b-256 checksum
How to use checksums
3f26fca7367b171d528dced0c2caa6ff3e949e402e86035ca6d2b2d5849b5d3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Release files / ryomen-0.0.10-py3-none-any.whl

Download URL ryomen-0.0.10-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d8fd059fad427550e67b22af29e5d38bfffb95f69415f3c93b169e3682d7abc
BLAKE2b-256 checksum
How to use checksums
fbd422beaa3f2d99d9ab3289df9fbbabfefd8723ea7cbe776934f0dd60e6d067
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Security for ryomen 0.0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page