py4dstem / py4DSTEM

GNU General Public License v3.0
201 stars 136 forks source link

`DataCube` is malformed after binning #396

Closed sezelt closed 1 year ago

sezelt commented 1 year ago

If you load a DataCube and then bin it, the resulting DataCube is malformed. It will successfully write to a file, but upon reading it cannot be reconstituted. Minimal example:

import py4DSTEM
import numpy as np

dc = py4DSTEM.io.DataCube(np.random.rand(5,5,24,24))

dc.bin_Q(2)

py4DSTEM.save("test.h5",dc)

py4DSTEM.read("test.h5")
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Input In [8], in <cell line: 10>()
      6 dc.bin_Q(2)
      8 py4DSTEM.save("test.h5",dc)
---> 10 py4DSTEM.read("test.h5")

File ~/git/py4DSTEM/py4DSTEM/io/read.py:68, in read(filepath, root, tree, **kwargs)
     65 kwargs['tree'] = tree
     67 # load data
---> 68 data = read_py4DSTEM(
     69     filepath,
     70     **kwargs
     71 )
     73 return data

File ~/git/py4DSTEM/py4DSTEM/io/native/read.py:126, in read_py4DSTEM(filepath, root, tree, **legacy_options)
    124 # Read
    125 if tree is True:
--> 126     return _read_with_tree(group_data)
    128 elif tree is False:
    129     return _read_without_tree(group_data)

File ~/git/py4DSTEM/py4DSTEM/io/native/read.py:171, in _read_with_tree(grp)
    170 def _read_with_tree(grp):
--> 171     data = _read_without_tree(grp)
    172     _populate_tree(
    173         data.tree,
    174         grp
    175     )
    176     return data

File ~/git/py4DSTEM/py4DSTEM/io/native/read.py:158, in _read_without_tree(grp)
    156 # read all other data
    157 __class__ = _get_class(grp)
--> 158 data = __class__.from_h5(grp)
    159 if not isinstance(data, Calibration):
    160     cal = _add_calibration(
    161         data.tree,
    162         grp
    163     )

File ~/git/py4DSTEM/py4DSTEM/io/datastructure/py4dstem/datacube.py:216, in DataCube.from_h5(group)
    214 def from_h5(group):
    215     from py4DSTEM.io.datastructure.py4dstem.io import DataCube_from_h5
--> 216     return DataCube_from_h5(group)

File ~/git/py4DSTEM/py4DSTEM/io/datastructure/py4dstem/io.py:73, in DataCube_from_h5(group)
     60 def DataCube_from_h5(group:h5py.Group):
     61     """
     62     Takes a valid HDF5 group for an HDF5 file object which is open in read
     63     mode.  Determines if an Array object of this name exists inside this group,
   (...)
     71         A DataCube instance
     72     """
---> 73     datacube = Array_from_h5(group)
     74     datacube = DataCube_from_Array(datacube)
     75     return datacube

File ~/git/py4DSTEM/py4DSTEM/io/datastructure/emd/io.py:513, in Array_from_h5(group)
    510     slicelabels = None
    512 # make Array
--> 513 ar = Array(
    514     data = data,
    515     name = basename(group.name),
    516     units = units,
    517     dims = dims,
    518     dim_names = dim_names,
    519     dim_units = dim_units,
    520     slicelabels = slicelabels
    521 )
    523 # add metadata
    524 _read_metadata(ar, group)

File ~/git/py4DSTEM/py4DSTEM/io/datastructure/emd/array.py:231, in Array.__init__(self, data, name, units, dims, dim_names, dim_units, slicelabels)
    229     self.dims = []
    230     for n in range(self.rank):
--> 231         dim = self._unpack_dim(_dims[n],self.shape[n])
    232         self.dims.append(dim)
    234 # otherwise
    235 else:

File ~/git/py4DSTEM/py4DSTEM/io/datastructure/emd/array.py:397, in Array._unpack_dim(dim, length)
    395     return np.arange(start,stop,step)
    396 else:
--> 397     raise Exception(f"dim vector length must be either 2 or equal to the length of the corresponding array dimension; dim vector length was {dim} and the array dimension length was {length}")

Exception: dim vector length must be either 2 or equal to the length of the corresponding array dimension; dim vector length was [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] and the array dimension length was 12
sezelt commented 1 year ago

This is also affecting people who crop datasets and try to save them

sezelt commented 1 year ago

Appears to have been fixed in emdfile