Skip to content

refactor: dataset accepts torch (cpu/gpu) array-like for streamlined 4DSTEM algo & workflows #222

Description

@bobleesj

Problem

The main goal of the Dataset class is to containerize and retrieve the underlying data, including metadata.

Currently it is forced to numpy. This deviates from the main workflow for 4D-STEM reconstructions, which primarily rely
on torch or cupy - both for using quantem directly and for algo dev in forks.

Ref:

if not isinstance(arr, np.ndarray):
    raise TypeError(                                                                                                     
        "Dataset requires a NumPy array (CuPy is not supported on this branch)."
    )                                                                                                                    

Two cases where I don't get to use Dataset4dstem:

Case i) When a 4D-STEM dataset is loaded via the CUDA path (#135),
wrapping it in Dataset4dstem requires converting from VRAM to CPU RAM. With a duck-typed dataset, the data can stay in
VRAM and the widget (Show4DSTEM) reads it directly, eliminating the RAM↔VRAM round trip.

Case ii) Live SSB. Detector frames stream into VRAM and are processed on the GPU - they never touch CPU RAM.

Proposed solution

Remove isinstance(arr, np.ndarray) and replace it with a minimal duck-type check.

Accept any object exposing shape, ndim, dtype, and the standard reduction methods (.sum, .mean, .max, .min).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions