Exemple #1
0
    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
Exemple #2
0
    def fix(self, mode=None, microscope=None, out=None, pad=0, start=1):
        """
        Fixes wrong data in headers.

        Mode determines which values are fixed. Currently defined modes are:
          - 'polara_fei-tomo': images obtained on Polara (at MPI of 
          Biochemistry) using FEI tomography package and saved in EM 
          format.
          - 'krios_fei-tomo': images obtained on Krios (at MPI of 
          Biochemistry) using FEI tomography package and saved in EM 
          format.
          - 'cm300': images from cm300 in EM format
          - None: doesn't do anyhing
          
        If mode is 'polara_fei-tomo', then arg microscope has to be specified. 
        The allowed values are specified in microscope_db.py. Currently (r564)
        these are: 'polara-1_01-07', 'polara-1_01-09' and 'polara-2_01-09'.

        Works for individual images only (not for stacks), because stacks
        are typically recorded by SerialEM and do not need fixing.

        Arguments:
          - out: directory where the fixed images are written. The fixed and
          the original images have the same base names.
          - mode: fix mode
        """

        # parse arguments
        #if path is None: path = self.path
        #if mode is None: mode = self.mode

        # check for out, pad and start also?

        # make out directory if it doesn't exists
        if (out is not None) and (not os.path.isdir(out)):
            os.makedirs(out)

        # loop over images
        images_iter = self.images(out=out, pad=pad, start=start)
        for (in_path, out_path) in images_iter:

            # read image file
            image = ImageIO(file=in_path)
            image.read()

            # fix
            image.fix(mode=mode, microscope=microscope)

            # write fixed files
            image.write(file=out_path)
Exemple #3
0
    def testPixelSize(self):
        """
        Tests pixel size in read and write
        """

        # arrays
        #ar_int8_2 = numpy.arange(24, dtype='int8').reshape((4,3,2))
        ar_int16_2 = numpy.arange(24, dtype='int16').reshape((4, 3, 2))

        #
        file_out = ImageIO()
        file_out.write(file=os.path.join(self.dir, '_test.mrc'),
                       data=ar_int16_2,
                       pixel=2.1)
        file_in = ImageIO()
        file_in.read(file=os.path.join(self.dir, '_test.mrc'))
        np_test.assert_almost_equal(file_in.pixel, 2.1)
Exemple #4
0
    def setUp(self):
        """
        Sets absolute path to this file directory and saves it as self.dir
        """

        # set absolute path to current dir
        working_dir = os.getcwd()
        file_dir, name = os.path.split(__file__)
        self.dir = os.path.join(working_dir, file_dir)

        # make raw file
        self.raw_shape = (4, 3, 2)
        self.raw_dtype = 'int16'
        self.raw_data = numpy.arange(24, dtype=self.raw_dtype).reshape(
            self.raw_shape)
        raw = ImageIO()
        self.raw_file_name = 'data.raw'
        raw.write(file=self.raw_file_name, data=self.raw_data)
Exemple #5
0
    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
Exemple #6
0
    def write(
        self, file, byteOrder=None, dataType=None, fileFormat=None,
        arrayOrder=None, shape=None, length=None, pixel=1, casting=u'unsafe',
            header=False, existing=False):

        """
        Writes image to a file in em, mrc or raw format.

        If fileFormat is not given it is determined from the file extension.

        Data (image) has to be specified by arg data or previously set 
        self.data attribute.

        Data type and shape are determined by args dataType and shape, 
        or by the data type and shape of the data, in this order..

        If data type (determined as described above) is not one of the 
        data types used for the specified file format (ubyte, int16, float32, 
        complex64 for mrc and uint8, uint16, int32, float32, float64, 
        complex64 for em), then the value of arg dataType has to be one of the 
        appropriate data types. Otherwise an exception is raised.

        If data type (determined as described above) is different from the 
        type of actual data, the data is converted to the data type. Note that
        if these two types are incompatible according to arg casting, an 
        exception is raised. 

        The data is converted to the (prevously determined) shape and array
        order and then written. That means that the shape and the array order 
        may be changed in the original array (argument data or self.data).
        However, array order is not written the header. 

        Additional header parameters are determined for mrc format. Nxstart, 
        nystart and nzstart are set to 0, while mx, my and mz to the 
        corresponding data size (grid size). xlen, ylen and zlen are taken from
        arg length if given, or obtained by multiplying data size with pixel 
        size (in nm).

        If arg header is True and if the file type corresponding to arg file
        is the same as self.fileFormat, a header consisting of self.header 
        (list) and self.extendedHeaderString (str) are written as file 
        header. The extended header should be used only for mrc format.

        Arguments:
          - file: file name
          - fileFormat: 'em', 'mrc', or 'raw'
          - byteOrder: '<' (little-endian), '>' (big-endian)
          - dataType: any of the numpy types, e.g.: 'int8', 'int16', 'int32',
            'float32', 'float64'
          - arrayOrder: 'C' (z-axis fastest), or 'F' (x-axis fastest)
          - shape: (x_dim, y_dim, z_dim)
          - length: (list aor ndarray) length in each dimension in nm (used 
          only for mrc format)
          - pixel: pixel size in nm (used only for mrc format if length is 
          None)
          - casting: Controls what kind of data casting may occur: 'no', 
          'equiv', 'safe', 'same_kind', 'unsafe'. Identical to numpy.astype()
          method.
          - header: flag indicating if self.header is written as file header
          - existing: flag indicating whether the already existing file_io
          attribute is used for writting

        Returns file instance.
        """

        # write file
        from pyto.io.image_io import ImageIO as ImageIO
        if existing and (self.file_io is not None):
            fi = self.file_io
        fi = ImageIO()

        # get file format of the file to be written
        fi.setFileFormat(file_=file, fileFormat=fileFormat)
        new_file_format = fi.fileFormat

        # set header to pass as argument, if new and old file types are the same
        header_arg = None
        extended = None
        try:
            if header and (new_file_format == self.fileFormat):
                header_arg = self.header
                extended = self.extendedHeaderString
        except AttributeError:
            pass

        # write
        fi.write(
            file=file, data=self.data, fileFormat=fileFormat, 
            byteOrder=byteOrder, dataType=dataType, arrayOrder=arrayOrder, 
            shape=shape, length=length, pixel=pixel, casting=casting, 
            header=header_arg, extended=extended)

        return fi.file_
Exemple #7
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)
Exemple #8
0
    def sort(self,
             seq=None,
             out=None,
             pad=0,
             start=1,
             fix_mode=None,
             microscope=None,
             limit=None,
             limit_mode='std',
             size=5,
             byte_order=None,
             test=False):
        """
        Sorts series, fixes image headers, corrects the data and writes the
        sorted and corrected images. 

        A series is sorted by tilt angle that is read from each image, or by the
        elements of seq (not necessarily angles) if given.

        Sorted images names have the form:
          directory/name + number + extension.
        where out is: directory/name_.extension and numbers start with start
        argument and are padded with pad (argument) number of zeros (see
        self.images).

        Argument fix_mode detemines how the headers are fixed:
          - None: no fixing
          - 'polara_fei-tomo': for series obtained on polara with the FEI tomo
          software
          - 'krios_fei-tomo': for series obtained on krios with the FEI tomo
          software
          - 'cm300': for series from CM300
        (see pyto.io.image_io). 

        If fix_mode is 'polara_fei-tomo', then arg microscope has to be 
        specified. The allowed values are specified in microscope_db.py. 
        Currently (r564) these are: 'polara-1_01-07', 'polara-1_01-09' and 
        'polara-2_01-09'.

        If fix_mode is None, microscope does nor need to be specified.
        Series recorded by SerialEM typically do not need fixing.

        Works for individual images only (not for stacks).

        Arguments:
          - seq: if specified this sequence used for sorting projection,
          otherwise tilt angles are used
          - out: sorted series path
          - pad: number of digits of a projection number in the sorted 
          series file name
          - start: start number for sorted series file names 
          - fix_mode: determined if and how headers are fixed
          - microscope: microscope type, see pyto.io.microscope_db
          - limit_mode: 'abs' or 'std'
          - limit: absolute greyscale limit if limit_mode is 'abs', or the 
          number of stds above and below the mean 
          - size: size of the subarray used to find the replacement value 
          for a voxel that's out of the limits 
          - byte_order: '<' or '>', default is the byte order of the machine
          - test: if True all steps are done except writing corrected images
        
        """

        # shortcuts
        path = self.path
        match_mode = self.mode

        # make tilt angles - file names dictionary
        # ToDo: use self.sortPath() instead
        in_paths = []
        images_iter = self.images(mode=match_mode)
        if seq is None:
            seq = []

            # get tilt angles from file headers
            for in_path in images_iter:
                image = ImageIO(file=in_path)
                image.readHeader()
                seq.append(image.tiltAngle)
                in_paths.append(in_path)
                image.file_.close()

        else:

            # use seq to sort
            for in_path in images_iter:
                in_paths.append(in_path)

        # sort (note: dictionary angle:in_path fails for multiple files with
        # same angles)
        seq_arr = numpy.array(seq)
        in_paths_arr = numpy.array(in_paths)
        sort_ind = seq_arr.argsort()
        sorted_seq = seq_arr[sort_ind]
        sorted_in_paths = in_paths_arr[sort_ind]

        # parse out and make out directory if it doesn't exists
        if out is not None:
            out_dir, out_base_pat = os.path.split(out)
            if out_dir == '':
                out_dir = '.'
            if (not test) and (not os.path.isdir(out_dir)):
                os.makedirs(out_dir)
        else:
            out_path = None

        # initialize file counter
        if start is None:
            ind = None
        else:
            ind = start - 1

        # loop over sorted in files
        for (item, in_path) in zip(sorted_seq, sorted_in_paths):
            if ind is not None: ind += 1

            # parse in file path
            in_dir, in_base = os.path.split(in_path)

            if out is not None:

                # make out file path
                out_base = self.convertBase(in_base=in_base,
                                            out_base=out_base_pat,
                                            pad=pad,
                                            index=ind)
                out_path = os.path.join(out_dir, out_base)

                # read files
                im_io = ImageIO(file=in_path)
                im_io.read()

                # log
                logging.info("%5.1f: %s -> %s  %6.1f %6.1f" \
                                 % (item, in_path, out_path, \
                                        im_io.data.mean(), im_io.data.std()))

                # fix
                if fix_mode is not None:
                    im_io.fix(mode=fix_mode, microscope=microscope)

                # limit
                if limit is not None:
                    image = Image(im_io.data)
                    image.limit(limit=limit, mode=limit_mode, size=size)

                # write fixed file
                if not test:
                    if byte_order is None:
                        byte_order = im_io.machineByteOrder
                    im_io.write(file=out_path,
                                byteOrder=byte_order,
                                data=image.data)

            else:

                logging.info(" " + str(item) + ": " + in_path + " -> "\
                             + str(out_path))