Skip to content

From abtem zarr#182

Draft
gvarnavi wants to merge 2 commits intodevfrom
from_abtem_zarr
Draft

From abtem zarr#182
gvarnavi wants to merge 2 commits intodevfrom
from_abtem_zarr

Conversation

@gvarnavi
Copy link
Collaborator

Adding from_abtem reader, subject to abTEM/abTEM#250 being merged. @TomaSusi

@CSSFrancis feel free to adapt for rosettasciio, but I actually think for quantem's purposes we might simply use this, since it supports arbitrary dataset dimensions.

@TomaSusi
Copy link

TomaSusi commented Mar 1, 2026

Awesome :) Will review and merge the required abTEM change soon.

How about in-memory abTEM measurements? Might be often handy to have a function to convert them to quantem datasets.

@gvarnavi
Copy link
Collaborator Author

gvarnavi commented Mar 5, 2026

Sure, something like the following does the trick (tested with DiffractionPatterns, Images, and PolarMeasurements with arbitrary metadata):

def to_quantem(abtem_array):
    """
    Convert an in-memory abTEM measurement into a quantem Dataset.
    Assumes both abtem and quantem are installed.
    """

    from quantem.core.datastructures import Dataset

    def _normalize_unit(unit):
        if unit is None:
            return "pixels"

        UNIT_MAP = {
            "Å": "A",
            "Ångström": "A",
            "Angstrom": "A",
            "1/Å": "A^-1",
            "Å^-1": "A^-1",
            "1/A": "A^-1",
        }

        return UNIT_MAP.get(unit, unit)

    sampling = []
    origin = []
    units = []

    for axis in abtem_array.axes_metadata:
        sampling.append(getattr(axis, "sampling", 1.0))
        units.append(_normalize_unit(getattr(axis, "units", None)))
        origin.append(0.0)

    is_lazy = abtem_array.is_lazy
    array = abtem_array.compute().array if is_lazy else abtem_array.array
    
    dataset = Dataset.from_array(
        array=array,
        name=type(abtem_array).__name__,
        origin=tuple(origin),
        sampling=tuple(sampling),
        units=tuple(units),
        signal_units=getattr(abtem_array, "units", "arb. units"),
    )

    # preserve abTEM metadata if present
    if hasattr(abtem_array, "metadata"):
        dataset._metadata = dict(abtem_array.metadata)

    return dataset

However, this should live in abtem.array.ArrayObject imo, not quantem. Will add to abTEM/abTEM#250.

@TomaSusi
Copy link

TomaSusi commented Mar 5, 2026

Okay, I don't mind if we add this to abTEM with an optional quantem dependency, just thought it might not be necessary... but perhaps this is the easiest way.

@gvarnavi
Copy link
Collaborator Author

gvarnavi commented Mar 5, 2026

I mean, you don't have hyperspy or xarray as optional dependencies either. The code will issue an error if it can't import quantem and prompt users to do so. I think that's sufficient.

@TomaSusi
Copy link

TomaSusi commented Mar 5, 2026

Right, I meant optional in the sense of a import try and a raised exception, it is true we do this for other external packages, too.

@TomaSusi
Copy link

TomaSusi commented Mar 5, 2026

The relevant PR (with the merged intermediate changes) is now abTEM/abTEM#252, sorry for any confusion

@TomaSusi
Copy link

The abTEM PR is now merged to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants