Esempio n. 1
0
    def save(self, filename, only_view = False, **kwds):
        """Saves the signal in the specified format.

        The function gets the format from the extension. You can use:
            - hdf5 for HDF5
            - nc for NetCDF
            - msa for EMSA/MSA single spectrum saving.
            - bin to produce a raw binary file
            - Many image formats such as png, tiff, jpeg...

        Please note that not all the formats supports saving datasets of
        arbitrary dimensions, e.g. msa only suports 1D data.

        Parameters
        ----------
        filename : str
        msa_format : {'Y', 'XY'}
            'Y' will produce a file without the energy axis. 'XY' will also
            save another column with the energy axis. For compatibility with
            Gatan Digital Micrograph 'Y' is the default.
        only_view : bool
            If True, only the current view will be saved. Otherwise the full
            dataset is saved. Please note that not all the formats support this
            option at the moment.
        """
        io.save(filename, self, **kwds)
Esempio n. 2
0
    def save(self, filename, only_view=False, **kwds):
        """Saves the signal in the specified format.

        The function gets the format from the extension. You can use:
            - hdf5 for HDF5
            - nc for NetCDF
            - msa for EMSA/MSA single spectrum saving.
            - bin to produce a raw binary file
            - Many image formats such as png, tiff, jpeg...

        Please note that not all the formats supports saving datasets of
        arbitrary dimensions, e.g. msa only suports 1D data.

        Parameters
        ----------
        filename : str
        msa_format : {'Y', 'XY'}
            'Y' will produce a file without the energy axis. 'XY' will also
            save another column with the energy axis. For compatibility with
            Gatan Digital Micrograph 'Y' is the default.
        only_view : bool
            If True, only the current view will be saved. Otherwise the full
            dataset is saved. Please note that not all the formats support this
            option at the moment.
        """
        io.save(filename, self, **kwds)
Esempio n. 3
0
    def compareSaveLoad(self,
                        testShape,
                        dtype='int8',
                        compressor=None,
                        clevel=1,
                        lazy=False,
                        do_async=False,
                        **kwargs):
        # This is the main function which reads and writes from disk.
        mrcName = os.path.join(tmpDir,
                               "testMage_{}_lazy_{}.mrcz".format(dtype, lazy))

        dtype = np.dtype(dtype)
        if dtype == 'float32' or dtype == 'float64':
            testData = np.random.normal(size=testShape).astype(dtype)
        elif dtype == 'complex64' or dtype == 'complex128':
            testData = np.random.normal(size=testShape).astype(
                dtype) + 1.0j * np.random.normal(size=testShape).astype(dtype)
        else:  # integers
            testData = np.random.randint(10, size=testShape).astype(dtype)

        testSignal = signals.Signal2D(testData)
        if lazy:
            testSignal = testSignal.as_lazy()
        # Unfortunately one cannot iterate over axes_manager in a Pythonic way
        # for axis in testSignal.axes_manager:
        testSignal.axes_manager[0].name = 'z'
        testSignal.axes_manager[0].scale = np.random.uniform(low=0.0, high=1.0)
        testSignal.axes_manager[0].units = 'nm'
        testSignal.axes_manager[1].name = 'x'
        testSignal.axes_manager[1].scale = np.random.uniform(low=0.0, high=1.0)
        testSignal.axes_manager[1].units = 'nm'
        testSignal.axes_manager[2].name = 'y'
        testSignal.axes_manager[2].scale = np.random.uniform(low=0.0, high=1.0)
        testSignal.axes_manager[2].units = 'nm'

        # Meta-data that goes into MRC fixed header
        testSignal.metadata.set_item('Acquisition_instrument.TEM.beam_energy',
                                     300.0)
        # Meta-data that goes into JSON extended header
        testSignal.metadata.set_item(
            'Acquisition_instrument.TEM.magnification', 25000)
        testSignal.metadata.set_item(
            'Signal.Noise_properties.Variance_linear_model.gain_factor', 1.0)

        save(mrcName,
             testSignal,
             compressor=compressor,
             clevel=clevel,
             do_async=do_async,
             **kwargs)
        if do_async:
            # Poll file on disk since we don't return the
            # concurrent.futures.Future
            t_stop = perf_counter() + MAX_ASYNC_TIME
            sleep(0.005)
            while (perf_counter() < t_stop):
                try:
                    fh = open(mrcName, 'a')
                    fh.close()
                    break
                except IOError:
                    sleep(0.001)
            print("Time to save file: {} s".format(perf_counter() -
                                                   (t_stop - MAX_ASYNC_TIME)))
            sleep(0.1)

        reSignal = load(mrcName)
        try:
            os.remove(mrcName)
        except IOError:
            print("Warning: file {} left on disk".format(mrcName))

        npt.assert_array_almost_equal(testSignal.data.shape,
                                      reSignal.data.shape)
        npt.assert_array_almost_equal(testSignal.data, reSignal.data)
        npt.assert_almost_equal(
            testSignal.metadata.Acquisition_instrument.TEM.beam_energy,
            reSignal.metadata.Acquisition_instrument.TEM.beam_energy)
        npt.assert_almost_equal(
            testSignal.metadata.Acquisition_instrument.TEM.magnification,
            reSignal.metadata.Acquisition_instrument.TEM.magnification)

        for aName in ['x', 'y', 'z']:
            npt.assert_equal(testSignal.axes_manager[aName].size,
                             reSignal.axes_manager[aName].size)
            npt.assert_almost_equal(testSignal.axes_manager[aName].scale,
                                    reSignal.axes_manager[aName].scale)

        if dtype == 'complex64':
            assert isinstance(reSignal, signals.ComplexSignal2D)
        else:
            assert isinstance(reSignal, signals.Signal2D)
        assert_deep_almost_equal(testSignal.axes_manager.as_dictionary(),
                                 reSignal.axes_manager.as_dictionary())
        assert_deep_almost_equal(testSignal.metadata.as_dictionary(),
                                 reSignal.metadata.as_dictionary())

        return reSignal
Esempio n. 4
0
    def compareSaveLoad(self, testShape, dtype='int8', compressor=None,
                        clevel=1, lazy=False, do_async=False, **kwargs):
        # This is the main function which reads and writes from disk.
        mrcName = os.path.join(
            tmpDir, "testMage_{}_lazy_{}.mrcz".format(dtype, lazy))

        dtype = np.dtype(dtype)
        if dtype == 'float32' or dtype == 'float64':
            testData = np.random.normal(size=testShape).astype(dtype)
        elif dtype == 'complex64' or dtype == 'complex128':
            testData = np.random.normal(size=testShape).astype(
                dtype) + 1.0j * np.random.normal(size=testShape).astype(dtype)
        else:  # integers
            testData = np.random.randint(10, size=testShape).astype(dtype)

        testSignal = signals.Signal2D(testData)
        if lazy:
            testSignal = testSignal.as_lazy()
        # Unfortunately one cannot iterate over axes_manager in a Pythonic way
        # for axis in testSignal.axes_manager:
        testSignal.axes_manager[0].name = 'z'
        testSignal.axes_manager[0].scale = np.random.uniform(low=0.0, high=1.0)
        testSignal.axes_manager[0].units = 'nm'
        testSignal.axes_manager[1].name = 'x'
        testSignal.axes_manager[1].scale = np.random.uniform(low=0.0, high=1.0)
        testSignal.axes_manager[1].units = 'nm'
        testSignal.axes_manager[2].name = 'y'
        testSignal.axes_manager[2].scale = np.random.uniform(low=0.0, high=1.0)
        testSignal.axes_manager[2].units = 'nm'

        # Meta-data that goes into MRC fixed header
        testSignal.metadata.set_item(
            'Acquisition_instrument.TEM.beam_energy', 300.0)
        # Meta-data that goes into JSON extended header
        testSignal.metadata.set_item(
            'Acquisition_instrument.TEM.magnification', 25000)
        testSignal.metadata.set_item(
            'Signal.Noise_properties.Variance_linear_model.gain_factor', 1.0)

        save(mrcName, testSignal, compressor=compressor,
             clevel=clevel, do_async=do_async, **kwargs)
        if do_async:
            # Poll file on disk since we don't return the
            # concurrent.futures.Future
            t_stop = perf_counter() + MAX_ASYNC_TIME
            sleep(0.005)
            while(perf_counter() < t_stop):
                try:
                    fh = open(mrcName, 'a')
                    fh.close()
                    break
                except IOError:
                    sleep(0.001)
            print("Time to save file: {} s".format(
                perf_counter() - (t_stop - MAX_ASYNC_TIME)))
            sleep(0.005)

        reSignal = load(mrcName)
        try:
            os.remove(mrcName)
        except IOError:
            print("Warning: file {} left on disk".format(mrcName))

        npt.assert_array_almost_equal(
            testSignal.data.shape,
            reSignal.data.shape)
        npt.assert_array_almost_equal(testSignal.data, reSignal.data)
        npt.assert_almost_equal(
            testSignal.metadata.Acquisition_instrument.TEM.beam_energy,
            reSignal.metadata.Acquisition_instrument.TEM.beam_energy)
        npt.assert_almost_equal(
            testSignal.metadata.Acquisition_instrument.TEM.magnification,
            reSignal.metadata.Acquisition_instrument.TEM.magnification)

        for aName in ['x', 'y', 'z']:
            npt.assert_equal(
                testSignal.axes_manager[aName].size,
                reSignal.axes_manager[aName].size)
            npt.assert_almost_equal(
                testSignal.axes_manager[aName].scale,
                reSignal.axes_manager[aName].scale)

        if dtype == 'complex64':
            assert isinstance(reSignal, signals.ComplexSignal2D)
        else:
            assert isinstance(reSignal, signals.Signal2D)
        assert_deep_almost_equal(testSignal.axes_manager.as_dictionary(),
                                 reSignal.axes_manager.as_dictionary())
        assert_deep_almost_equal(testSignal.metadata.as_dictionary(),
                                 reSignal.metadata.as_dictionary())

        return reSignal