Example #1
0
def file_writer(filename,
                signal,
                do_async=False,
                compressor=None,
                clevel=1,
                n_threads=None,
                **kwds):
    import hyperspy.signals
    if not isinstance(
            signal,
        (hyperspy.signals.Signal2D, hyperspy.signals.ComplexSignal2D)):
        raise TypeError("MRCZ supports 2D and 3D data only. type(signal) is "
                        "{}".format(type(signal)))

    endianess = kwds.pop('endianess', '<')
    mrcz_endian = 'le' if endianess == '<' else 'be'

    meta = signal.metadata.as_dictionary()

    # Get pixelsize and pixelunits from the axes
    pixelunits = signal.axes_manager[-1].units

    pixelsize = [signal.axes_manager[I].scale for I in _WRITE_ORDER]

    # Strip out voltage from meta-data
    voltage = signal.metadata.get_item(
        'Acquisition_instrument.TEM.beam_energy')
    # There aren't hyperspy fields for spherical aberration or detector gain
    C3 = 0.0
    gain = signal.metadata.get_item(
        "Signal.Noise_properties."
        "Variance_linear_model.gain_factor", 1.0)
    if do_async:
        _mrcz.asyncWriteMRC(signal.data,
                            filename,
                            meta=meta,
                            endian=mrcz_endian,
                            pixelsize=pixelsize,
                            pixelunits=pixelunits,
                            voltage=voltage,
                            C3=C3,
                            gain=gain,
                            compressor=compressor,
                            clevel=clevel,
                            n_threads=n_threads)
    else:
        _mrcz.writeMRC(signal.data,
                       filename,
                       meta=meta,
                       endian=mrcz_endian,
                       pixelsize=pixelsize,
                       pixelunits=pixelunits,
                       voltage=voltage,
                       C3=C3,
                       gain=gain,
                       compressor=compressor,
                       clevel=clevel,
                       n_threads=n_threads)
Example #2
0
    def test_Async(self):
        testMage = np.random.uniform(high=10, size=[3, 128, 64]).astype('int8')
        meta = {'foo': 5, 'bar': 42}
        mrcName = os.path.join(tmpDir, "testMage.mrcz")

        pixelsize = [1.2, 5.6, 3.4]

        worker = mrcz.asyncWriteMRC(testMage,
                                    mrcName,
                                    meta=meta,
                                    pixelsize=pixelsize,
                                    pixelunits=u"\AA",
                                    voltage=300.0,
                                    C3=2.7,
                                    gain=1.05,
                                    compressor='zstd',
                                    clevel=1,
                                    n_threads=4)

        worker.result()  # Wait for write to finish

        worker = mrcz.asyncReadMRC(mrcName, pixelunits=u"\AA")
        rereadMage, rereadHeader = worker.result()

        try:
            os.remove(mrcName)
        except IOError:
            log.info("Warning: file {} left on disk".format(mrcName))

        assert (np.all(testMage.shape == rereadMage.shape))
        assert (testMage.dtype == rereadMage.dtype)
        for key in meta:
            assert (meta[key] == rereadHeader[key])

        npt.assert_array_almost_equal(testMage, rereadMage)
        npt.assert_array_equal(rereadHeader['voltage'], 300.0)
        npt.assert_array_almost_equal(rereadHeader['pixelsize'], pixelsize)
        npt.assert_array_equal(rereadHeader['pixelunits'], u"\AA")
        npt.assert_array_equal(rereadHeader['C3'], 2.7)
        npt.assert_array_equal(rereadHeader['gain'], 1.05)
        pass