def rescale(url: str, new_scale: Dict[str, float]) -> Dict[str, Any]:

    validate_axis_keys(list(new_scale.keys()))
    new_meta: Dict[str, Any] = {}

    arr = access(url, mode="a")
    if not isinstance(arr, zarr.core.Array):
        raise ValueError(
            "This function only works on Zarr arrays or N5 arrays accessed via zarr"
        )

    if arr.attrs.get("pixelResolution", None) is not None:
        old_pixr = arr.attrs["pixelResolution"]
        # pixel resolution is x y z
        _new_scale = [new_scale[k] for k in ["x", "y", "z"]]
        new_meta.update({
            "[pixelResolution":
            neuroglancer.PixelResolution(dimensions=_new_scale,
                                         unit=old_pixr["unit"]).asdict()
        })

    if arr.attrs.get("transform", None) is not None:
        old_transform = cosem_ome.SpatialTransform(
            **arr.attrs.get("transform"))
        new_transform = cosem_ome.SpatialTransform(**old_transform.asdict())
        for key, value in new_scale.items():
            idx = old_transform.axes.index(key)
            new_transform.scale[idx] = value
        new_meta.update({"transform": new_transform.asdict()})

    return new_meta
def reunit(url: str, new_units: Dict[str, str]):
    validate_axis_keys(list(new_units.keys()))
    if not len(set(new_units.values())) == 1:
        raise ValueError(
            "Due to legacy support for the PixelResolution attribute, units for all axes must be identical."
        )

    new_unit: str = list(new_units.values())[0]
    new_meta: Dict[str, Any] = {}

    arr = access(url, mode="a")
    if not isinstance(arr, zarr.core.Array):
        raise ValueError(
            "This function only works on Zarr arrays or N5 arrays accessed via zarr"
        )

    if arr.attrs.get("pixelResolution", None) is not None:
        old_pixr = arr.attrs["pixelResolution"]
        # pixel resolution is x y z
        new_meta.update({
            "[pixelResolution":
            neuroglancer.PixelResolution(dimensions=old_pixr["dimensions"],
                                         unit=new_unit).asdict()
        })

    if arr.attrs.get("transform", None) is not None:
        old_transform = cosem_ome.SpatialTransform(
            **arr.attrs.get("transform"))
        new_transform = cosem_ome.SpatialTransform(**old_transform.asdict())
        for key, value in new_units.items():
            idx = old_transform.axes.index(key)
            new_transform.units[idx] = new_unit
        new_meta.update({"transform": new_transform.asdict()})

    return new_meta
Exemplo n.º 3
0
def main(group_path: str):
    group = access(group_path, mode='a')

    arrays = dict(sorted(group.arrays(), key = lambda kv: int(kv[0][1:])))
    base_pixelRes = neuroglancer.PixelResolution(**group.attrs['pixelResolution'])
    base_transform = cosem.SpatialTransform(axes=group.attrs['axes'][::-1], 
                                            units=(base_pixelRes.unit,) * 3, 
                                            translate=[0.0,] * 3, 
                                        scale=base_pixelRes.dimensions[::-1])
    scale_factors = (2,2,2)

    full_rank = {k: v for k,v in arrays.items() if np.all(np.array(v.shape) > 1)}

    base_data = DataArray(da.from_array(group['s0']), coords=transform_to_coords(group['s0'], base_transform))
    multi = multiscale(base_data, da.mean, scale_factors=scale_factors)[:len(full_rank)]
    multi_meta = [cosem.SpatialTransform.fromDataArray(m).dict() for m in multi]
    [a.attrs.update({'transform' : mmeta}) for a,mmeta in zip(full_rank.values(), multi_meta)]
    group_meta = cosem.COSEMGroupMetadata.fromDataArrays(multi, name='raw', paths=tuple(full_rank.keys())).dict()
    group.attrs.update(group_meta)
    return 1