Example #1
0
File: image.py Project: bbarad/Pyto
    def modify(cls, old, new, fun, fun_kwargs={}, memmap=True):
        """
        Reads an image (arg old), modifies old.data using function
        passed as arg fun and writes an image containing the modified
        data as a new image (arg new).

        The function passes (arg fun) has to have signature
          fun(Image, **fun_kwargs)
        and to return image data (ndarray).

        Meant for mrc files. The new image will have exactly the same 
        header as the old image, except for the shape, length and 
        min/max/mean values, which are set according to the new image 
        data.

        Also works if old is an mrc and new is a raw file.  
        
        Arguments:
          - old: old mrc image file name
          - new: new (subtomogram) mrc image file name
          - fun: function that takes old.data as an argument 
          - memmap: if True, read memory map instead of the whole image

        Returns an instance of this class that holds the new image. This
        instance contains attribute image_io (pyto.io.ImageIO) that
        was used to write the new file.
 
        """
        # read header
        from pyto.io.image_io import ImageIO as ImageIO
        image_io = ImageIO()
        image_io.readHeader(file=old)

        # read image 
        image = cls.read(file=old, memmap=memmap)

        # modify data
        data = fun(image, **fun_kwargs)

        # write new (modified data)
        image_io.setData(data=data)
        image_io.write(file=new, pixel=image_io.pixel)
        #image_io.setFileFormat(file_=new)
        #if image_io.fileFormat == 'mrc':
        #    image_io.write(file=new, pixel=image_io.pixel)
        #elif image_io.fileFormat == 'raw':
        #    image_io.write(file=new, pixel=image_io.pixel)
            
        #
        image.image_io = image_io
        return image
Example #2
0
def save_numpy(array, fname):
    """
    Saves a numpy array to a file in MRC, EM or VTI format.

    Args:
        array (numpy.ndarray): input array
        fname (str): full path to the tomogram, has to end with '.mrc', '.em' or
            '.vti'

    Returns:
        None
    """
    _, ext = os.path.splitext(fname)

    # Parse input array for fulfilling format requirements
    if (ext == '.mrc') or (ext == '.em'):
        if ((array.dtype != 'ubyte') and (array.dtype != 'int16') and
                (array.dtype != 'float32')):
            array = array.astype('float32')
        # if (len(array.shape) == 3) and (array.shape[2] == 1):
        #   array = np.reshape(array, (array.shape[0], array.shape[1]))

    if ext == '.vti':
        pname, fnameh = os.path.split(fname)
        save_vti(numpy_to_vti(array), fnameh, pname)
    # elif ext == '.fits':
    #     warnings.resetwarnings()
    #     warnings.filterwarnings('ignore', category=UserWarning, append=True)
    #     pyfits.writeto(fname, array, clobber=True, output_verify='silentfix')
    #     warnings.resetwarnings()
    #     warnings.filterwarnings('always', category=UserWarning, append=True)
    elif ext == '.mrc':
        img = ImageIO()
        # img.setData(np.transpose(array, (1, 0, 2)))
        img.setData(array)
        img.writeMRC(fname)
    elif ext == '.em':
        img = ImageIO()
        # img.setData(np.transpose(array, (1, 0, 2)))
        img.setData(array)
        img.writeEM(fname)
    else:
        error_msg = 'Format not valid %s.' % ext
        raise pexceptions.PySegInputError(expr='save_numpy', msg=error_msg)
Example #3
0
def save_numpy(array, fname):
    """
    Saves a numpy array to a file in MRC, EM or VTI format.

    Args:
        array (numpy.ndarray): input array
        fname (str): full path to the tomogram, has to end with '.mrc', '.em' or
            '.vti'

    Returns:
        None
    """
    _, ext = os.path.splitext(fname)

    # Parse input array for fulfilling format requirements
    if (ext == '.mrc') or (ext == '.em'):
        if ((array.dtype != 'ubyte') and (array.dtype != 'int16')
                and (array.dtype != 'float32')):
            array = array.astype('float32')
        # if (len(array.shape) == 3) and (array.shape[2] == 1):
        #   array = np.reshape(array, (array.shape[0], array.shape[1]))

    if ext == '.vti':
        pname, fnameh = os.path.split(fname)
        save_vti(numpy_to_vti(array), fnameh, pname)
    elif ext == '.mrc':
        img = ImageIO()
        # img.setData(np.transpose(array, (1, 0, 2)))
        img.setData(array)
        img.writeMRC(fname)
    elif ext == '.em':
        img = ImageIO()
        # img.setData(np.transpose(array, (1, 0, 2)))
        img.setData(array)
        img.writeEM(fname)
    else:
        raise pexceptions.PySegInputError(
            expr='save_numpy', msg='Format not valid {}.'.format(ext))
Example #4
0
File: image.py Project: bbarad/Pyto
    def cut(cls, old, new, inset, memmap=True):
        """
        Reads an image (arg old), cuts a subtomo defined by arg inset
        and writes the new image (arg new).

        Meant for mrc files. The new image will have exactly the same 
        header as the old image, except for the shape, length and 
        min/max/mean values, which are set according to the new image 
        data.

        Arguments:
          - old: old mrc image file name
          - new: new (subtomogram) mrc image file name
          - inset: defines the subtomogram
          - memmap: if True, read memory map instead of the whole image

        Returns an instance of this class that holds the new image. This
        instance contains attribute image_io (pyto.io.ImageIO) that
        was used to write the new file.
        """

        # read header
        from pyto.io.image_io import ImageIO as ImageIO
        image_io = ImageIO()
        image_io.readHeader(file=old)

        # read image and cut inset
        image = cls.read(file=old, memmap=memmap)
        image.useInset(inset=inset)

        # write new
        image_io.setData(data=image.data)
        image_io.write(file=new, pixel=image_io.pixel)

        #
        image.image_io = image_io
        return image
Example #5
0
    def testWrite(self):
        """
        Tests write (and implicitly read), for em, mrc and raw format.
        """

        # arrays
        ar_uint8 = numpy.array([54, 200, 5, 7, 45, 123],
                               dtype='uint8').reshape((3, 1, 2))
        ar_int8 = numpy.array([54, 2, -5, 7, 45, 123], dtype='uint8').reshape(
            (3, 1, 2))
        ar_uint16 = numpy.array([1034, 546, 248, 40000, 2345, 365, 4876, 563],
                                dtype='uint16').reshape((2, 2, 2))
        ar_int16 = numpy.array([1034, 546, -248, 156, 2345, 365, -4876, 563],
                               dtype='int16').reshape((2, 2, 2))
        ar_int32 = numpy.array(
            [1034, 56546, -223448, 156, 2345, 2**31 - 10, -884876, 563],
            dtype='int32').reshape((2, 2, 2))
        ar_uint32 = numpy.array(
            [1034, 56546, 223448, 156, 2345, 365, 884876, 2**32 - 10],
            dtype='uint32').reshape((2, 2, 2))
        ar_int8_2 = numpy.arange(24, dtype='int8').reshape((4, 3, 2))
        ar_int16_2 = numpy.arange(24, dtype='int16').reshape((4, 3, 2))
        ar2_int16 = numpy.array([1034, 546, -248, 156, 2345, 365, -4876, 563],
                                dtype='int16').reshape((2, 4))
        ar_int16_f = numpy.array([1034, 546, -248, 156, 2345, 365, -4876, 563],
                                 dtype='int16',
                                 order='F').reshape((2, 2, 2))
        ar_int16_c = numpy.array([1034, 546, -248, 156, 2345, 365, -4876, 563],
                                 dtype='int16',
                                 order='C').reshape((2, 2, 2))

        # em uint8
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'), data=ar_uint8)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'uint8')
        np_test.assert_equal(file_in.data, ar_uint8)

        # em uint16
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'), data=ar_uint16)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'uint16')
        np_test.assert_equal(file_in.data, ar_uint16)

        # em int16 converted to int32, safe casting
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'),
                       data=ar_int16,
                       dataType='int32',
                       casting='safe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'int32')
        np_test.assert_equal(file_in.data, ar_int16)

        # em int16, safe casting
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.em'),
                'data': ar_int16,
                'casting': 'safe'
            })

        # em int16 converted to uint16, unsafe casting
        file_out = ImageIO()
        print("int16 to uint16")
        file_out.write(file=os.path.join(self.dir, '_test.em'),
                       data=ar_int16,
                       dataType='uint16',
                       casting='unsafe')
        print("int16 to uint16 end")
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'uint16')
        np_test.assert_equal(file_in.data.dtype, numpy.dtype('uint16'))
        np_test.assert_equal(file_in.data[0, 1, 0] == ar_int16[0, 1, 0], False)

        # em int16 to uint16, safe casting
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.em'),
                'data': ar_int16,
                'dataType': 'uint16',
                'casting': 'safe'
            })

        # em uint16 to int16, unsafe casting
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.em'),
                'data': ar_uint16,
                'dataType': 'int16',
                'casting': 'unsafe'
            })

        # em uint32 to int32, safe casting
        print("uint32 to int32 safe")
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.em'),
                'data': ar_uint32,
                'dataType': 'int32',
                'casting': 'safe'
            })

        # em uint32 converted to int32, unsafe casting
        print("uint32 to int32")
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'),
                       data=ar_uint32,
                       dataType='int32',
                       casting='unsafe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'int32')
        #np_test.assert_equal(file_in.data, ar_uint32) should fail
        np_test.assert_equal(file_in.data[0, 0, 0] == ar_uint32[0, 0, 0], True)
        np_test.assert_equal(file_in.data[1, 1, 1] == ar_uint32[1, 1, 1],
                             False)

        # em uint32 to float32, safe casting
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.em'),
                'data': ar_uint32,
                'dataType': 'float32',
                'casting': 'safe'
            })

        # em uint32 to float32, unsafe casting
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'),
                       data=ar_uint32,
                       dataType='float32',
                       casting='unsafe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'float32')
        #np_test.assert_almost_equal(file_in.data, ar_uint32)  should fail
        np_test.assert_equal(file_in.data[0, 0, 0] == ar_uint32[0, 0, 0], True)
        np_test.assert_equal(file_in.data[1, 1, 1] == ar_uint32[1, 1, 1],
                             False)

        # em int32 to float32, unsafe casting
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'),
                       data=ar_int32,
                       dataType='float32',
                       casting='unsafe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'float32')
        #np_test.assert_almost_equal(file_in.data, ar_int32)  should fail
        np_test.assert_equal(file_in.data[0, 0, 0] == ar_int32[0, 0, 0], True)
        np_test.assert_equal(file_in.data[1, 0, 1] == ar_int32[1, 0, 1], False)

        # em int32 to float64, safe casting
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.em'),
                       data=ar_int32,
                       dataType='float64',
                       casting='safe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.em'))
        np_test.assert_equal(file_in.dataType, 'float64')
        np_test.assert_almost_equal(file_in.data, ar_int32)

        # mrc data type and shape from args
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int8_2,
                       shape=(2, 3, 4),
                       dataType='int16')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.shape, (2, 3, 4))

        # mrc data type and shape from previously given data
        file_out = ImageIO()
        file_out.setData(ar_int16_2)
        file_out.write(file=os.path.join(self.dir, '_test.mrc'))
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.shape, (4, 3, 2))

        # mrc data type and shape from attributes
        file_out = ImageIO()
        file_out.data = ar_int8_2
        file_out.shape = (2, 3, 4)
        file_out.dataType = 'int16'
        file_out.write(file=os.path.join(self.dir, '_test.mrc'))
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.shape, (2, 3, 4))

        # mrc data type and shape from data
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16_2)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.shape, (4, 3, 2))

        # mrc uint8, same as ubyte
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'), data=ar_uint8)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'ubyte')
        np_test.assert_almost_equal(file_in.data, ar_uint8)

        # mrc uint16
        file_out = ImageIO()
        np_test.assert_raises((KeyError, TypeError), file_out.write, **{
            'file': os.path.join(self.dir, '_test.mrc'),
            'data': ar_uint16
        })

        # mrc uint16 to int16, safe casting
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.mrc'),
                'data': ar_uint16,
                'dataType': 'ubyte',
                'casting': 'safe'
            })

        # mrc uint16 to int16, unsafe casting
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_uint16,
                       dataType='int16',
                       casting='unsafe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        #np_test.assert_almost_equal(file_in.data, ar_uint16)  should fail
        np_test.assert_equal(file_in.data[0, 0, 0] == ar_uint16[0, 0, 0], True)
        np_test.assert_equal(file_in.data[0, 1, 1] == ar_uint16[0, 1, 1],
                             False)

        # mrc int16
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16,
                       pixel=2.3)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.data, ar_int16)
        np_test.assert_equal(file_in.pixel, [2.3, 2.3, 2.3])
        np_test.assert_equal(file_in.pixelsize, 2.3)

        # mrc int16 2D
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar2_int16,
                       pixel=3.4)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.data[:, :, 0], ar2_int16)
        np_test.assert_equal(file_in.pixelsize, 3.4)

        # mrc int8 to int16
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int8,
                       dataType='int16',
                       casting='safe')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.data, ar_int8)

        # mrc int32
        file_out = ImageIO()
        np_test.assert_raises((KeyError, TypeError), file_out.write, **{
            'file': os.path.join(self.dir, '_test.mrc'),
            'data': ar_int32
        })

        # mrc int32 to int16
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.mrc'),
                'data': ar_int32,
                'dataType': 'int16',
                'casting': 'safe'
            })

        # mrc int32 to float32
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.mrc'),
                'data': ar_int32,
                'dataType': 'float32',
                'casting': 'safe'
            })

        # mrc int32 to complex64
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.mrc'),
                'data': ar_int32,
                'dataType': 'complex64',
                'casting': 'safe'
            })

        # raw int16
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.raw'), data=ar_int16)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.raw'),
                     dataType='int16',
                     shape=(2, 2, 2))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.data, ar_int16)

        # raw int8 to int16
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.raw'),
                       data=ar_int8,
                       dataType='int16')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.raw'),
                     dataType='int16',
                     shape=(3, 1, 2))
        np_test.assert_equal(file_in.dataType, 'int16')
        np_test.assert_equal(file_in.data, ar_int8)

        # raw int16 to int8
        file_out = ImageIO()
        np_test.assert_raises(
            TypeError, file_out.write, **{
                'file': os.path.join(self.dir, '_test.raw'),
                'data': ar_int16,
                'dataType': 'int8',
                'casting': 'safe'
            })

        # explain error messages printed before
        print("It's fine if few error messages were printed just before " +
              "this line, because they have been caught.")

        # shape param
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16,
                       dataType='int16')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'),
                     dataType='int16')
        np_test.assert_equal(file_in.data.shape, (2, 2, 2))
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16,
                       dataType='int16',
                       shape=(1, 4, 2))
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'),
                     dataType='int16')
        np_test.assert_equal(file_in.data.shape, (1, 4, 2))
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16,
                       dataType='int16',
                       shape=(4, 2))
        file_in.readHeader(file=os.path.join(self.dir, '_test.mrc'))
        file_in.read(file=os.path.join(self.dir, '_test.mrc'),
                     dataType='int16')
        np_test.assert_equal(file_in.data.shape, (4, 2, 1))
        file_in.read(file=os.path.join(self.dir, '_test.mrc'),
                     dataType='int16',
                     shape=(2, 2, 2))
        np_test.assert_equal(file_in.data.shape, (2, 2, 2))

        # array order C, read write default (F)
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16_c)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.data, ar_int16_c)

        # array order C, read write C
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16_c,
                       arrayOrder='C')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'), arrayOrder='C')
        np_test.assert_equal(file_in.data, ar_int16_c)

        # array order F, read write default (F)
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16_f)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_equal(file_in.data, ar_int16_f)

        # array order F, read write F
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16_f,
                       arrayOrder='F')
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'), arrayOrder='F')
        np_test.assert_equal(file_in.data, ar_int16_f)