Esempio n. 1
0
    def xtomo_exchange(
        self,
        data,
        data_white=None,
        data_dark=None,
        theta=None,
        data_exchange_type=None,
        source_name=None,
        source_mode=None,
        source_datetime=None,
        beamline=None,
        energy=None,
        current=None,
        actual_pixel_size=None,
        experimenter_name=None,
        experimenter_affiliation=None,
        experimenter_email=None,
        instrument_comment=None,
        sample_name=None,
        sample_comment=None,
        acquisition_mode=None,
        acquisition_comment=None,
        sample_position_x=None,
        sample_position_y=None,
        sample_position_z=None,
        sample_image_shift_x=None,
        sample_image_shift_y=None,
        image_exposure_time=None,
        image_time=None,
        image_theta=None,
        hdf5_file_name=None,
        axes="theta:y:x",
        log="INFO",
    ):
        """ 
        Write 3-D data to a data-exchange file.

        Parameters
                    
        data : ndarray
            3-D X-ray absorption tomography raw data.
            Size of the dimensions should be:
            [projections, slices, pixels].
            
        data_white, data_dark : ndarray, optional
            3-D white-field/dark_field data. Multiple
            projections are stacked together to obtain
            a 3-D matrix. 2nd and 3rd dimensions should
            be the same as data: [shots, slices, pixels].
            
        theta : ndarray, optional
            Data acquisition angles corresponding
            to each projection.

        data_excahnge_type : str, optional
            label defyining the type of data contained in data exchange file
            for raw data tomography data use 'tomography_raw_projections'

        source_name, source_mode, source_datetime : str, optional
            label defining the source name, operation mode and date/time when these values were taken

        beamline : str, optional
            label defining the beamline name
        
        energy, current : float, optional
            X-ray energy and bean current

        actual_pixel_size : float, optional
            pixel size on the sample plane
 
        experimenter_name, experimenter_affiliation, experimenter_email : str, optional
            user name, affiliation and e-mail address

        instrument_comment : str, optional
            instrument comment

        sample_name, sample_comment : str, optional
            sample name and comment
        
        acquisition_mode, acquisition_comment : str, optional
            acquisition mode and comment

        hd5_file_name : str
            Output file.

        Notes
        -----
        If file exists, does nothing
                
        Examples
        
        - Convert tomographic projection series (raw, dark, white)  of tiff in data exchange:
            
            >>> import dataexchange

            >>> file_name = '/local/dataraid/databank/Anka/radios/image_.tif'
            >>> dark_file_name = '/local/dataraid/databank/Anka/darks/image_.tif'
            >>> white_file_name = '/local/dataraid/databank/Anka/flats/image_.tif'

            >>> hdf5_file_name = '/local/dataraid/databank/dataExchange/tmp/Anka.h5'

            >>> projections_start = 0
            >>> projections_end = 3167
            >>> white_start = 0
            >>> white_end = 100
            >>> dark_start = 0
            >>> dark_end = 100

            >>> sample_name = 'Anka'
            >>> 
            >>> # Read raw data
            >>> read = dataexchange.Import()
            >>> data, white, dark, theta = read.xtomo_raw(file_name,
            >>>                                                    projections_start = projections_start,
            >>>                                                    projections_end = projections_end,
            >>>                                                    white_file_name = white_file_name,
            >>>                                                    white_start = white_start,
            >>>                                                    white_end = white_end,
            >>>                                                    dark_file_name = dark_file_name,
            >>>                                                    dark_start = dark_start,
            >>>                                                    dark_end = dark_end,
            >>>                                                    projections_digits = 5,
            >>>                                                    log='INFO'
            >>>                                                    )
            >>>
            >>> # Save data
            >>> write = dataexchange.Export()
            >>> write.xtomo_exchange(data = data,
            >>>                       data_white = white,
            >>>                       data_dark = dark,
            >>>                       theta = theta,
            >>>                       hdf5_file_name = hdf5_file_name,
            >>>                       data_exchange_type = 'tomography_raw_projections',
            >>>                       sample_name = sample_name
            >>>                       )

        """

        if hdf5_file_name != None:
            if os.path.isfile(hdf5_file_name):
                self.logger.error("Data Exchange file: [%s] already exists", hdf5_file_name)
            else:
                # Create new folder.
                dirPath = os.path.dirname(hdf5_file_name)
                if not os.path.exists(dirPath):
                    os.makedirs(dirPath)

                # Get the file_name in lower case.
                lFn = hdf5_file_name.lower()

                # Split the string with the delimeter '.'
                end = lFn.split(".")

                # Write the Data Exchange HDF5 file.
                # Open DataExchange file
                f = DataExchangeFile(hdf5_file_name, mode="w")

                self.logger.info("Creating Data Exchange File [%s]", hdf5_file_name)

                # Create core HDF5 dataset in exchange group for projections_theta_range
                # deep stack of x,y images /exchange/data
                self.logger.info("Adding projections to Data Exchange File [%s]", hdf5_file_name)
                f.add_entry(
                    DataExchangeEntry.data(
                        data={"value": data, "units": "counts", "description": "transmission", "axes": axes}
                    )
                )
                #                f.add_entry( DataExchangeEntry.data(data={'value': data, 'units':'counts', 'description': 'transmission', 'axes':'theta:y:x', 'dataset_opts':  {'compression': 'gzip', 'compression_opts': 4} }))
                if theta != None:
                    f.add_entry(DataExchangeEntry.data(theta={"value": theta, "units": "degrees"}))
                    self.logger.info("Adding theta to Data Exchange File [%s]", hdf5_file_name)
                else:
                    self.logger.warning("theta is not defined")
                if data_dark != None:
                    self.logger.info("Adding dark fields to  Data Exchange File [%s]", hdf5_file_name)
                    f.add_entry(
                        DataExchangeEntry.data(
                            data_dark={"value": data_dark, "units": "counts", "axes": "theta_dark:y:x"}
                        )
                    )
                #                    f.add_entry( DataExchangeEntry.data(data_dark={'value': data_dark, 'units':'counts', 'axes':'theta_dark:y:x', 'dataset_opts':  {'compression': 'gzip', 'compression_opts': 4} }))
                else:
                    self.logger.warning("data dark is not defined")
                if data_white != None:
                    self.logger.info("Adding white fields to  Data Exchange File [%s]", hdf5_file_name)
                    f.add_entry(
                        DataExchangeEntry.data(
                            data_white={"value": data_white, "units": "counts", "axes": "theta_white:y:x"}
                        )
                    )
                #                    f.add_entry( DataExchangeEntry.data(data_white={'value': data_white, 'units':'counts', 'axes':'theta_white:y:x', 'dataset_opts':  {'compression': 'gzip', 'compression_opts': 4} }))
                else:
                    self.logger.warning("data white is not defined")
                if data_exchange_type != None:
                    self.logger.info("Adding data type to  Data Exchange File [%s]", hdf5_file_name)
                    f.add_entry(DataExchangeEntry.data(title={"value": data_exchange_type}))

                if source_name != None:
                    f.add_entry(DataExchangeEntry.source(name={"value": source_name}))
                if source_mode != None:
                    f.add_entry(DataExchangeEntry.source(mode={"value": source_mode}))
                if source_datetime != None:
                    f.add_entry(DataExchangeEntry.source(datetime={"value": source_datetime}))

                if beamline != None:
                    f.add_entry(DataExchangeEntry.source(beamline={"value": beamline}))
                if energy != None:
                    f.add_entry(
                        DataExchangeEntry.monochromator(
                            energy={"value": energy, "units": "keV", "dataset_opts": {"dtype": "d"}}
                        )
                    )
                if current != None:
                    f.add_entry(
                        DataExchangeEntry.source(
                            current={"value": current, "units": "mA", "dataset_opts": {"dtype": "d"}}
                        )
                    )

                if actual_pixel_size != None:
                    f.add_entry(
                        DataExchangeEntry.detector(
                            actual_pixel_size_x={
                                "value": actual_pixel_size,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            },
                            actual_pixel_size_y={
                                "value": actual_pixel_size,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            },
                        )
                    )

                if experimenter_name != None:
                    f.add_entry(DataExchangeEntry.experimenter(name={"value": experimenter_name}))
                if experimenter_affiliation != None:
                    f.add_entry(DataExchangeEntry.experimenter(affiliation={"value": experimenter_affiliation}))
                if experimenter_email != None:
                    f.add_entry(DataExchangeEntry.experimenter(email={"value": experimenter_email}))

                if instrument_comment != None:
                    f.add_entry(DataExchangeEntry.instrument(comment={"value": instrument_comment}))
                if sample_name == None:
                    sample_name = end[0]
                    f.add_entry(
                        DataExchangeEntry.sample(
                            name={"value": sample_name},
                            description={
                                "value": "Sample name assigned by the HDF5 converter and based on the HDF5 file name"
                            },
                        )
                    )
                else:
                    f.add_entry(DataExchangeEntry.sample(name={"value": sample_name}))
                if sample_comment != None:
                    f.add_entry(DataExchangeEntry.sample(comment={"value": sample_comment}))

                if acquisition_mode != None:
                    f.add_entry(DataExchangeEntry.acquisition(mode={"value": acquisition_mode}))
                if acquisition_comment != None:
                    f.add_entry(DataExchangeEntry.acquisition(comment={"value": acquisition_comment}))

                if sample_position_x != None:
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_position_x={
                                "value": sample_position_x,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            }
                        )
                    )
                if sample_position_y != None:
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_position_y={
                                "value": sample_position_y,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            }
                        )
                    )
                if sample_position_z != None:
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_position_z={
                                "value": sample_position_z,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            }
                        )
                    )
                if sample_image_shift_x != None:
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_image_shift_x={
                                "value": sample_image_shift_x,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            }
                        )
                    )
                if sample_image_shift_y != None:
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_image_shift_y={
                                "value": sample_image_shift_y,
                                "units": "microns",
                                "dataset_opts": {"dtype": "d"},
                            }
                        )
                    )

                if image_exposure_time != None:
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            image_exposure_time={
                                "value": image_exposure_time,
                                "units": "s",
                                "dataset_opts": {"dtype": "d"},
                            }
                        )
                    )
                if image_time != None:
                    f.add_entry(DataExchangeEntry.acquisition(image_time={"value": image_time}))
                if image_theta != None:
                    f.add_entry(DataExchangeEntry.acquisition(image_theta={"value": image_theta, "units": "degrees"}))
                f.close()
                self.logger.info("DONE!!!!. Created Data Exchange File [%s]", hdf5_file_name)

        else:
            self.logger.warning("Nothing to do ...")
    def xtomo_exchange(self,
                       data,
                       data_white=None,
                       data_dark=None,
                       theta=None,
                       data_exchange_type=None,
                       source_name=None,
                       source_mode=None,
                       source_datetime=None,
                       beamline=None,
                       energy=None,
                       current=None,
                       actual_pixel_size=None,
                       experimenter_name=None,
                       experimenter_affiliation=None,
                       experimenter_email=None,
                       instrument_comment=None,
                       sample_name=None,
                       sample_comment=None,
                       acquisition_mode=None,
                       acquisition_comment=None,
                       sample_position_x=None,
                       sample_position_y=None,
                       sample_position_z=None,
                       sample_image_shift_x=None,
                       sample_image_shift_y=None,
                       hdf5_file_name=None,
                       axes='theta:y:x',
                       log='INFO'):
        """ 
        Write 3-D data to a data-exchange file.

        Parameters
                    
        data : ndarray
            3-D X-ray absorption tomography raw data.
            Size of the dimensions should be:
            [projections, slices, pixels].
            
        data_white, data_dark : ndarray, optional
            3-D white-field/dark_field data. Multiple
            projections are stacked together to obtain
            a 3-D matrix. 2nd and 3rd dimensions should
            be the same as data: [shots, slices, pixels].
            
        theta : ndarray, optional
            Data acquisition angles corresponding
            to each projection.

        data_excahnge_type : str, optional
            label defyining the type of data contained in data exchange file
            for raw data tomography data use 'tomography_raw_projections'

        source_name, source_mode, source_datetime : str, optional
            label defining the source name, operation mode and date/time when these values were taken

        beamline : str, optional
            label defining the beamline name
        
        energy, current : float, optional
            X-ray energy and bean current

        actual_pixel_size : float, optional
            pixel size on the sample plane
 
        experimenter_name, experimenter_affiliation, experimenter_email : str, optional
            user name, affiliation and e-mail address

        instrument_comment : str, optional
            instrument comment

        sample_name, sample_comment : str, optional
            sample name and comment
        
        acquisition_mode, acquisition_comment : str, optional
            acquisition mode and comment

        hd5_file_name : str
            Output file.

        Notes
        -----
        If file exists, does nothing
                
        Examples
        
        - Convert tomographic projection series (raw, dark, white)  of tiff in data exchange:
            
            >>> import dataexchange

            >>> file_name = '/local/dataraid/databank/Anka/radios/image_.tif'
            >>> dark_file_name = '/local/dataraid/databank/Anka/darks/image_.tif'
            >>> white_file_name = '/local/dataraid/databank/Anka/flats/image_.tif'

            >>> hdf5_file_name = '/local/dataraid/databank/dataExchange/tmp/Anka.h5'

            >>> projections_start = 0
            >>> projections_end = 3167
            >>> white_start = 0
            >>> white_end = 100
            >>> dark_start = 0
            >>> dark_end = 100

            >>> sample_name = 'Anka'
            >>> 
            >>> # Read raw data
            >>> read = dataexchange.Import()
            >>> data, white, dark, theta = read.xtomo_raw(file_name,
            >>>                                                    projections_start = projections_start,
            >>>                                                    projections_end = projections_end,
            >>>                                                    white_file_name = white_file_name,
            >>>                                                    white_start = white_start,
            >>>                                                    white_end = white_end,
            >>>                                                    dark_file_name = dark_file_name,
            >>>                                                    dark_start = dark_start,
            >>>                                                    dark_end = dark_end,
            >>>                                                    projections_digits = 5,
            >>>                                                    log='INFO'
            >>>                                                    )
            >>>
            >>> # Save data
            >>> write = dataexchange.Export()
            >>> write.xtomo_exchange(data = data,
            >>>                       data_white = white,
            >>>                       data_dark = dark,
            >>>                       theta = theta,
            >>>                       hdf5_file_name = hdf5_file_name,
            >>>                       data_exchange_type = 'tomography_raw_projections',
            >>>                       sample_name = sample_name
            >>>                       )

        """

        if (hdf5_file_name != None):
            if os.path.isfile(hdf5_file_name):
                self.logger.error("Data Exchange file: [%s] already exists",
                                  hdf5_file_name)
            else:
                # Create new folder.
                dirPath = os.path.dirname(hdf5_file_name)
                if not os.path.exists(dirPath):
                    os.makedirs(dirPath)

                # Get the file_name in lower case.
                lFn = hdf5_file_name.lower()

                # Split the string with the delimeter '.'
                end = lFn.split('.')

                # Write the Data Exchange HDF5 file.
                # Open DataExchange file
                f = DataExchangeFile(hdf5_file_name, mode='w')

                self.logger.info("Creating Data Exchange File [%s]",
                                 hdf5_file_name)

                # Create core HDF5 dataset in exchange group for projections_theta_range
                # deep stack of x,y images /exchange/data
                self.logger.info(
                    "Adding projections to Data Exchange File [%s]",
                    hdf5_file_name)
                f.add_entry(
                    DataExchangeEntry.data(
                        data={
                            'value': data,
                            'units': 'counts',
                            'description': 'transmission',
                            'axes': axes
                        }))
                #                f.add_entry( DataExchangeEntry.data(data={'value': data, 'units':'counts', 'description': 'transmission', 'axes':'theta:y:x', 'dataset_opts':  {'compression': 'gzip', 'compression_opts': 4} }))
                if (theta != None):
                    f.add_entry(
                        DataExchangeEntry.data(theta={
                            'value': theta,
                            'units': 'degrees'
                        }))
                    self.logger.info("Adding theta to Data Exchange File [%s]",
                                     hdf5_file_name)
                else:
                    self.logger.warning("theta is not defined")
                if (data_dark != None):
                    self.logger.info(
                        "Adding dark fields to  Data Exchange File [%s]",
                        hdf5_file_name)
                    f.add_entry(
                        DataExchangeEntry.data(
                            data_dark={
                                'value': data_dark,
                                'units': 'counts',
                                'axes': 'theta_dark:y:x'
                            }))
#                    f.add_entry( DataExchangeEntry.data(data_dark={'value': data_dark, 'units':'counts', 'axes':'theta_dark:y:x', 'dataset_opts':  {'compression': 'gzip', 'compression_opts': 4} }))
                else:
                    self.logger.warning("data dark is not defined")
                if (data_white != None):
                    self.logger.info(
                        "Adding white fields to  Data Exchange File [%s]",
                        hdf5_file_name)
                    f.add_entry(
                        DataExchangeEntry.data(
                            data_white={
                                'value': data_white,
                                'units': 'counts',
                                'axes': 'theta_white:y:x'
                            }))
#                    f.add_entry( DataExchangeEntry.data(data_white={'value': data_white, 'units':'counts', 'axes':'theta_white:y:x', 'dataset_opts':  {'compression': 'gzip', 'compression_opts': 4} }))
                else:
                    self.logger.warning("data white is not defined")
                if (data_exchange_type != None):
                    self.logger.info(
                        "Adding data type to  Data Exchange File [%s]",
                        hdf5_file_name)
                    f.add_entry(
                        DataExchangeEntry.data(
                            title={'value': data_exchange_type}))

                if (source_name != None):
                    f.add_entry(
                        DataExchangeEntry.source(name={'value': source_name}))
                if (source_mode != None):
                    f.add_entry(
                        DataExchangeEntry.source(mode={'value': source_mode}))
                if (source_datetime != None):
                    f.add_entry(
                        DataExchangeEntry.source(
                            datetime={'value': source_datetime}))

                if (beamline != None):
                    f.add_entry(
                        DataExchangeEntry.source(beamline={'value': beamline}))
                if (energy != None):
                    f.add_entry(
                        DataExchangeEntry.monochromator(
                            energy={
                                'value': energy,
                                'units': 'keV',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))
                if (current != None):
                    f.add_entry(
                        DataExchangeEntry.source(
                            current={
                                'value': current,
                                'units': 'mA',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))

                if (actual_pixel_size != None):
                    f.add_entry(
                        DataExchangeEntry.detector(actual_pixel_size_x={
                            'value': actual_pixel_size,
                            'units': 'microns',
                            'dataset_opts': {
                                'dtype': 'd'
                            }
                        },
                                                   actual_pixel_size_y={
                                                       'value':
                                                       actual_pixel_size,
                                                       'units': 'microns',
                                                       'dataset_opts': {
                                                           'dtype': 'd'
                                                       }
                                                   }))

                if (experimenter_name != None):
                    f.add_entry(
                        DataExchangeEntry.experimenter(
                            name={'value': experimenter_name}))
                if (experimenter_affiliation != None):
                    f.add_entry(
                        DataExchangeEntry.experimenter(
                            affiliation={'value': experimenter_affiliation}))
                if (experimenter_email != None):
                    f.add_entry(
                        DataExchangeEntry.experimenter(
                            email={'value': experimenter_email}))

                if (instrument_comment != None):
                    f.add_entry(
                        DataExchangeEntry.instrument(
                            comment={'value': instrument_comment}))
                if (sample_name == None):
                    sample_name = end[0]
                    f.add_entry(
                        DataExchangeEntry.sample(
                            name={'value': sample_name},
                            description={
                                'value':
                                'Sample name assigned by the HDF5 converter and based on the HDF5 file name'
                            }))
                else:
                    f.add_entry(
                        DataExchangeEntry.sample(name={'value': sample_name}))
                if (sample_comment != None):
                    f.add_entry(
                        DataExchangeEntry.sample(
                            comment={'value': sample_comment}))

                if (acquisition_mode != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            mode={'value': acquisition_mode}))
                if (acquisition_comment != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            comment={'value': acquisition_comment}))

                if (sample_position_x != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_position_x={
                                'value': sample_position_x,
                                'units': 'microns',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))
                if (sample_position_y != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_position_y={
                                'value': sample_position_y,
                                'units': 'microns',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))
                if (sample_position_z != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_position_z={
                                'value': sample_position_z,
                                'units': 'microns',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))

                if (sample_image_shift_x != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_image_shift_x={
                                'value': sample_image_shift_x,
                                'units': 'microns',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))
                if (sample_image_shift_y != None):
                    f.add_entry(
                        DataExchangeEntry.acquisition(
                            sample_image_shift_y={
                                'value': sample_image_shift_y,
                                'units': 'microns',
                                'dataset_opts': {
                                    'dtype': 'd'
                                }
                            }))

                f.close()
                self.logger.info("DONE!!!!. Created Data Exchange File [%s]",
                                 hdf5_file_name)

        else:
            self.logger.warning("Nothing to do ...")