示例#1
0
    def xtomo_read_images(self,
                          file_name,
                          images_start=0,
                          images_end=0,
                          images_step=1,
                          sino_start=0,
                          sino_end=0,
                          sino_step=1,
                          pixels_start=0,
                          pixels_end=0,
                          pixels_step=1,
                          images_digits=-1,
                          images_zeros=True,
                          dtype='uint16',
                          data_type='fabio',
                          array_type='projections',
                          flip='false',
                          log='INFO'):

        # Determine file name and extension type.
        data_file = os.path.splitext(file_name)[0]
        data_extension = os.path.splitext(file_name)[1]

        images_file_index = ["" for x in range(images_digits)]
        for m in range(images_digits):
            if images_zeros is True:
                images_file_index[m] = '0' * (images_digits - m - 1)
            elif images_zeros is False:
                images_file_index[m] = ''

        _file_name = ' '
        image_exist = False
        # Start reading projections one-by-one.
        ind = range(images_start, images_end, images_step)
        for m in range(len(ind)):
            for n in range(images_digits):
                if ind[m] < np.power(10, n + 1):
                    _file_name = data_file + images_file_index[n] + str(
                        ind[m]) + data_extension
                    self.logger.info("Generating image file names: [%s]",
                                     _file_name)
                    break

            if os.path.isfile(_file_name):
                image_exist = True
                self.logger.info("Reading image file: [%s]",
                                 os.path.realpath(_file_name))
                self.logger.info("data type: [%s]", data_type)

                f = XTomoReader(_file_name)

                if (data_type is 'spe'):
                    tmpdata = f.spe(y_start=sino_start,
                                    y_end=sino_end,
                                    y_step=sino_step)

                elif (data_type is 'nc'):
                    tmpdata = f.netcdf(y_start=sino_start,
                                       y_end=sino_end,
                                       y_step=sino_step)

                elif (data_type is 'tiff'):
                    tmpdata = f.tiff(x_start=sino_start,
                                     x_end=sino_end,
                                     x_step=sino_step,
                                     dtype=dtype,
                                     flip=flip)

                elif (data_type is 'compressed_tiff'):
                    tmpdata = f.tiffc(x_start=sino_start,
                                      x_end=sino_end,
                                      x_step=sino_step,
                                      dtype=dtype)

                elif (data_type is 'hdf5'):
                    tmpdata = f.hdf5_2d(x_start=sino_start,
                                        x_end=sino_end,
                                        x_step=sino_step,
                                        array_name='/entry1/data/data')

                elif (data_type is 'edf2'):
                    tmpdata = f.edf2(x_start=sino_start,
                                     x_end=sino_end,
                                     x_step=sino_step)

                elif (data_type is 'fabio'):
                    tmpdata = f.fabio(x_start=sino_start,
                                      x_end=sino_end,
                                      x_step=sino_step,
                                      flip=flip)

                if ((data_type is 'spe') or (data_type is 'nc')):
                    if m == 0:  # Get resolution once.
                        input_data = np.vstack([tmpdata])
                    else:
                        input_data = np.concatenate((input_data, tmpdata),
                                                    axis=0)

                if ((data_type is 'tiff') or (data_type is 'compressed_tiff')
                        or (data_type is 'hdf5') or (data_type is 'edf2')
                        or (data_type is 'fabio')):
                    if m == 0:  # Get resolution once.
                        input_data = np.empty(
                            (len(ind), tmpdata.shape[0], tmpdata.shape[1]),
                            dtype=dtype)
                    input_data[m, :, :] = tmpdata

        # Update data.
        if image_exist:
            data = input_data
            dtype = input_data.dtype

        elif (file_name == "does_not_exist"):
            self.logger.error("image file [%s]", file_name)
            data = None

        else:
            self.logger.info("Attempt reading images from a single file: [%s]",
                             file_name)
            data = None
            if (data_type is 'h5'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)
                    f = XTomoReader(file_name)
                    array_name = '/'.join([exchange_base, "data"])
                    tmpdata = f.hdf5(z_start=images_start,
                                     z_end=images_end,
                                     z_step=images_step,
                                     y_start=sino_start,
                                     y_end=sino_end,
                                     y_step=sino_step,
                                     x_start=pixels_start,
                                     x_end=pixels_end,
                                     x_step=pixels_step,
                                     array_name=array_name)
                    data = tmpdata
            elif (data_type is 'nxs'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)
                    f = XTomoReader(file_name)
                    array_type = 'projections'
                    tmpdata = f.nxs(z_start=images_start,
                                    z_end=images_end,
                                    z_step=images_step,
                                    y_start=sino_start,
                                    y_end=sino_end,
                                    y_step=sino_step,
                                    x_start=pixels_start,
                                    x_end=pixels_end,
                                    x_step=pixels_step,
                                    array_type=array_type)
                    data = tmpdata
            elif (data_type is 'edf'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)
                    f = XTomoReader(file_name)
                    tmpdata = f.edf(y_start=sino_start,
                                    y_end=sino_end,
                                    y_step=sino_step)
                    data = tmpdata
            elif (data_type is 'xradia'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)
                    f = XTomoReader(file_name)
                    tmpdata = f.txrm(y_start=sino_start,
                                     y_end=sino_end,
                                     y_step=sino_step)
                    data = tmpdata
                    dtype = tmpdata.dtype
            elif (data_type is 'dpt'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)
                    f = XTomoReader(file_name)
                    tmpdata = f.dpt(y_start=sino_start,
                                    y_end=sino_end,
                                    y_step=sino_step)
                    data = tmpdata
            else:
                data = None
                self.logger.error("No valid images found")
                self.logger.error("Missng white/dark images will be generated")

        return data
    def xtomo_read_images(self, file_name,
                         images_start=0,
                         images_end=0,
                         images_step=1,
                         sino_start=0,
                         sino_end=0,
                         sino_step=1,
                         pixels_start=0,
                         pixels_end=0,
                         pixels_step=1,
                         images_digits=-1,
                         images_zeros=True,
                         dtype='uint16',
                         data_type='fabio',
                         array_type='projections',
                         flip='false',
                         log='INFO'):

        # Determine file name and extension type.
        data_file = os.path.splitext(file_name)[0]
        data_extension = os.path.splitext(file_name)[1]

        images_file_index = ["" for x in range(images_digits)]
        for m in range(images_digits):
            if images_zeros is True:
                images_file_index[m] = '0' * (images_digits-m-1)
            elif images_zeros is False:
                images_file_index[m] = ''

        _file_name = ' '
        image_exist = False
        # Start reading projections one-by-one.
        ind = range(images_start, images_end, images_step)
        for m in range(len(ind)):
            for n in range(images_digits):
                if ind[m] < np.power(10, n+1):
                    _file_name = data_file + images_file_index[n] + str(ind[m]) + data_extension
                    self.logger.info("Generating image file names: [%s]", _file_name)                    
                    break

            if os.path.isfile(_file_name):
                image_exist = True
                self.logger.info("Reading image file: [%s]", os.path.realpath(_file_name))
                self.logger.info("data type: [%s]", data_type)

                f = XTomoReader(_file_name)

                if (data_type is 'spe'):
                    tmpdata = f.spe(y_start = sino_start,
                                    y_end = sino_end,
                                    y_step = sino_step)

                elif (data_type is 'nc'):
                    tmpdata = f.netcdf(y_start = sino_start,
                                    y_end = sino_end,
                                    y_step = sino_step)

                elif (data_type is 'tiff'):
                    tmpdata = f.tiff(x_start=sino_start,
                                     x_end=sino_end,
                                     x_step=sino_step,
                                     dtype=dtype,
                                     flip=flip)

                elif (data_type is 'compressed_tiff'):
                    tmpdata = f.tiffc(x_start=sino_start,
                                      x_end=sino_end,
                                      x_step=sino_step,
                                      dtype=dtype)
 
                elif (data_type is 'hdf5'):
                    tmpdata = f.hdf5_2d(x_start=sino_start,
                                     x_end=sino_end,
                                     x_step=sino_step,
                                     array_name ='/entry1/data/data')

                elif (data_type is 'edf2'):
                    tmpdata = f.edf2(x_start=sino_start,
                                     x_end=sino_end,
                                     x_step=sino_step)

                elif (data_type is 'fabio'):
                    tmpdata = f.fabio(x_start=sino_start,
                                     x_end=sino_end,
                                     x_step=sino_step,
                                     flip=flip)

                if ((data_type is 'spe') or
                    (data_type is 'nc')):
                    if m == 0: # Get resolution once.
                        input_data = np.vstack([tmpdata])
                    else:
                        input_data = np.concatenate((input_data, tmpdata), axis=0)

                if ((data_type is 'tiff') or
                    (data_type is 'compressed_tiff') or
                    (data_type is 'hdf5') or
                    (data_type is 'edf2') or
                    (data_type is 'fabio')):
                    if m == 0: # Get resolution once.
                        input_data = np.empty((len(ind), tmpdata.shape[0], tmpdata.shape[1]), dtype=dtype)
                    input_data[m, :, :] = tmpdata

        # Update data.
        if image_exist:
            data = input_data
            dtype = input_data.dtype

        elif (file_name == "does_not_exist"):        
            self.logger.error("image file [%s]", file_name)
            data = None

        else:
            self.logger.info("Attempt reading images from a single file: [%s]", file_name)
            data = None                    
            if (data_type is 'h5'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)                    
                    f = XTomoReader(file_name)
                    array_name = '/'.join([exchange_base, "data"])
                    tmpdata = f.hdf5(z_start = images_start,
                                    	z_end = images_end,
                                    	z_step = images_step,
                                        y_start = sino_start,
                                    	y_end = sino_end,
                                    	y_step = sino_step,
                                        x_start = pixels_start,
                                    	x_end = pixels_end,
                                    	x_step = pixels_step,
                                    	array_name = array_name)
                    data = tmpdata
            elif (data_type is 'nxs'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)                    
                    f = XTomoReader(file_name)
                    array_type = 'projections'
                    tmpdata = f.nxs(z_start = images_start,
                                    	z_end = images_end,
                                    	z_step = images_step,
                                        y_start = sino_start,
                                    	y_end = sino_end,
                                    	y_step = sino_step,
                                        x_start = pixels_start,
                                    	x_end = pixels_end,
                                    	x_step = pixels_step,
                                    	array_type = array_type)
                    data = tmpdata
            elif (data_type is 'edf'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)                    
                    f = XTomoReader(file_name)
                    tmpdata = f.edf(y_start = sino_start,
                                    y_end = sino_end,
                                    y_step = sino_step)
                    data = tmpdata
            elif (data_type is 'xradia'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)                    
                    f = XTomoReader(file_name)
                    tmpdata = f.txrm(y_start = sino_start,
                                    y_end = sino_end,
                                    y_step = sino_step)
                    data = tmpdata
                    dtype = tmpdata.dtype                    
            elif (data_type is 'dpt'):
                # Read the projections that are all in a single file
                if os.path.isfile(file_name):
                    self.logger.info("Image file: [%s] exists", file_name)                    
                    f = XTomoReader(file_name)
                    tmpdata = f.dpt(y_start = sino_start,
                                    y_end = sino_end,
                                    y_step = sino_step)
                    data = tmpdata
            else:
                data = None
                self.logger.error("No valid images found")
                self.logger.error("Missng white/dark images will be generated")

        return data