Example #1
0
def file_reader(filename,
                record_by=None,
                order=None,
                lazy=False,
                optimize=True):
    """Reads a DM3 file and loads the data into the appropriate class.
    data_id can be specified to load a given image within a DM3 file that
    contains more than one dataset.

    Parameters
    ----------
    record_by: Str
        One of: SI, Signal2D
    order : Str
        One of 'C' or 'F'
    lazy : bool, default False
        Load the signal lazily.
    %s
    """

    with open(filename, "rb") as f:
        dm = DigitalMicrographReader(f)
        dm.parse_file()
        images = [
            ImageObject(imdict, f, order=order, record_by=record_by)
            for imdict in dm.get_image_dictionaries()
        ]
        imd = []
        del dm.tags_dict['ImageList']
        dm.tags_dict['ImageList'] = {}

        for image in images:
            dm.tags_dict['ImageList'][
                'TagGroup0'] = image.imdict.as_dictionary()
            axes = image.get_axes_dict()
            mp = image.get_metadata()
            mp['General']['original_filename'] = os.path.split(filename)[1]
            post_process = []
            if image.to_spectrum is True:
                post_process.append(lambda s: s.to_signal1D(optimize=optimize))
            post_process.append(lambda s: s.squeeze())
            if lazy:
                image.filename = filename
                from dask.array import from_delayed
                import dask.delayed as dd
                val = dd(image.get_data, pure=True)()
                data = from_delayed(val, shape=image.shape, dtype=image.dtype)
            else:
                data = image.get_data()
            imd.append({
                'data': data,
                'axes': axes,
                'metadata': mp,
                'original_metadata': dm.tags_dict,
                'post_process': post_process,
                'mapping': image.get_mapping(),
            })

    return imd
    file_reader.__doc__ %= (OPTIMIZE_ARG.replace('False', 'True'))
Example #2
0
class CommonSignal1D:
    """Common functions for 1-dimensional signals."""
    def to_signal2D(self, optimize=True):
        """Returns the one dimensional signal as a two dimensional signal.

        By default ensures the data is stored optimally, hence often making a
        copy of the data. See `transpose` for a more general method with more
        options.

        %s

        See Also
        --------
        transpose, as_signal1D, as_signal2D, hs.transpose

        Raises
        ------
        DataDimensionError
            When data.ndim < 2


        """
        if self.data.ndim < 2:
            raise DataDimensionError(
                "A Signal dimension must be >= 2 to be converted to Signal2D")
        nat = self.axes_manager._get_axes_in_natural_order()
        im = self.transpose(signal_axes=nat[:2],
                            navigation_axes=nat[2:],
                            optimize=optimize)
        return im

    to_signal2D.__doc__ %= (OPTIMIZE_ARG.replace('False', 'True'))
Example #3
0
def file_reader(filename, record_by=None, order=None, lazy=False,
                optimize=True):
    """Reads a DM3 file and loads the data into the appropriate class.
    data_id can be specified to load a given image within a DM3 file that
    contains more than one dataset.

    Parameters
    ----------
    record_by: Str
        One of: SI, Signal2D
    order : Str
        One of 'C' or 'F'
    lazy : bool, default False
        Load the signal lazily.
    %s
    """

    with open(filename, "rb") as f:
        dm = DigitalMicrographReader(f)
        dm.parse_file()
        images = [ImageObject(imdict, f, order=order, record_by=record_by)
                  for imdict in dm.get_image_dictionaries()]
        imd = []
        del dm.tags_dict['ImageList']
        dm.tags_dict['ImageList'] = {}

        for image in images:
            dm.tags_dict['ImageList'][
                'TagGroup0'] = image.imdict.as_dictionary()
            axes = image.get_axes_dict()
            mp = image.get_metadata()
            mp['General']['original_filename'] = os.path.split(filename)[1]
            post_process = []
            if image.to_spectrum is True:
                post_process.append(lambda s: s.to_signal1D(optimize=optimize))
            post_process.append(lambda s: s.squeeze())
            if lazy:
                image.filename = filename
                from dask.array import from_delayed
                import dask.delayed as dd
                val = dd(image.get_data, pure=True)()
                data = from_delayed(val, shape=image.shape,
                                    dtype=image.dtype)
            else:
                data = image.get_data()
            imd.append(
                {'data': data,
                 'axes': axes,
                 'metadata': mp,
                 'original_metadata': dm.tags_dict,
                 'post_process': post_process,
                 'mapping': image.get_mapping(),
                 })

    return imd
    file_reader.__doc__ %= (OPTIMIZE_ARG.replace('False', 'True'))
Example #4
0
class CommonSignal2D(object):
    """Common functions for 2-dimensional signals."""
    def to_signal1D(self, optimize=True):
        """Returns the image as a spectrum.

        %s

        See Also
        --------
        as_signal1D : a method for the same purpose with more options.
        signals.Signal1D.to_signal1D : performs the inverse operation on one
        dimensional signals.

        as_signal2D, transpose, hs.transpose

        """
        return self.as_signal1D(0 + 3j, optimize=optimize)

    to_signal1D.__doc__ %= (OPTIMIZE_ARG.replace('False', 'True'))