def load_tab(self, load_item):
        """
        It verifies given configuration file, reads the parameters, and fills out the window.
        Parameters
        ----------
        conf : str
            configuration file (config_disp)
        Returns
        -------
        nothing
        """
        if os.path.isfile(load_item):
            conf = load_item
        else:
            conf = os.path.join(load_item, 'conf', 'config_disp')
            if not os.path.isfile(conf):
                msg_window(
                    'info: the load directory does not contain config_disp file'
                )
                return
        if not ver.ver_config_data(conf):
            msg_window('please check configuration file ' + conf +
                       '. Cannot parse, ')
            return
        try:
            conf_map = ut.read_config(conf)
        except Exception as e:
            msg_window('please check configuration file ' + conf +
                       '. Cannot parse, ' + str(e))
            return

        self.parse_spec()

        try:
            self.results_dir = str(conf_map.results_dir).replace(" ", "")
        except AttributeError:
            if self.results_dir is None:
                self.results_dir = self.main_win.experiment_dir
        self.result_dir_button.setStyleSheet("Text-align:left")
        self.result_dir_button.setText(self.results_dir)
        # if parameters are configured, override the readings from spec file
        try:
            self.diffractometer.setText(
                str(conf_map.diffractometer).replace(" ", ""))
        except AttributeError:
            pass
        try:
            self.crop.setText(str(conf_map.crop).replace(" ", ""))
        except AttributeError:
            pass
        try:
            self.rampups.setText(str(conf_map.rampups).replace(" ", ""))
        except AttributeError:
            pass
        try:
            self.energy.setText(str(conf_map.energy).replace(" ", ""))
            self.energy.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.delta.setText(str(conf_map.delta).replace(" ", ""))
            self.delta.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.gamma.setText(str(conf_map.gamma).replace(" ", ""))
            self.gamma.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.detdist.setText(str(conf_map.detdist).replace(" ", ""))
            self.detdist.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.theta.setText(str(conf_map.theta).replace(" ", ""))
            self.theta.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.chi.setText(str(conf_map.chi).replace(" ", ""))
            self.chi.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.phi.setText(str(conf_map.phi).replace(" ", ""))
            self.phi.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.scanmot.setText(str(conf_map.scanmot).replace(" ", ""))
            self.scanmot.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.scanmot_del.setText(
                str(conf_map.scanmot_del).replace(" ", ""))
            self.scanmot_del.setStyleSheet('color: black')
        except AttributeError:
            pass
        try:
            self.detector.setText(str(conf_map.detector).replace(" ", ""))
            self.detector.setStyleSheet('color: black')
        except AttributeError:
            pass
Example #2
0
def prep(fname, conf_info):
    """
    This function formats data for reconstruction. It uses configured parameters. The preparation consists of the following steps:
    1. removing the "aliens" - aliens are areas that are effect of interference. The area is manually set in a configuration file after inspecting the data. It could be also a mask file of the same dimensions that data.
    2. clearing the noise - the values below an amplitude threshold are set to zero
    3. amplitudes are set to sqrt
    4. cropping and padding. If the adjust_dimention is negative in any dimension, the array is cropped in this dimension.
    The cropping is followed by padding in the dimensions that have positive adjust dimension. After adjusting, the dimensions
    are adjusted further to find the smallest dimension that is supported by opencl library (multiplier of 2, 3, and 5).
    5. centering - finding the greatest amplitude and locating it at a center of new array. If shift center is defined, the center will be shifted accordingly.
    6. binning - adding amplitudes of several consecutive points. Binning can be done in any dimension.
    The modified data is then saved in data directory as data.tif.
    Parameters
    ----------
    fname : str
        tif file containing raw data
    conf_info : str
        experiment directory or configuration file. If it is directory, the "conf/config_data" will be
        appended to determine configuration file
    Returns
    -------
    nothing
    """
    
    # The data has been transposed when saved in tif format for the ImageJ to show the right orientation
    data = ut.read_tif(fname)

    if os.path.isdir(conf_info):
        experiment_dir = conf_info
        conf = os.path.join(experiment_dir, 'conf', 'config_data')
        # if the experiment contains separate scan directories
        if not os.path.isfile(conf):
            base_dir = os.path.abspath(os.path.join(experiment_dir, os.pardir))
            conf = os.path.join(base_dir, 'conf', 'config_data')
    else:
        #assuming it's a file
        conf = conf_info
        experiment_dir = None

    # verify the configuration file
    if not ver.ver_config_data(conf):
        return

    try:
        config_map = ut.read_config(conf)
        if config_map is None:
            print ("can't read configuration file")
            return
    except:
        print ('Please check the configuration file ' + conf + '. Cannot parse')
        return

    try:
        data_dir = config_map.data_dir
    except AttributeError:
        data_dir = 'data'
        if experiment_dir is not None:
            data_dir = os.path.join(experiment_dir, data_dir)
    if not os.path.exists(data_dir):
        os.makedirs(data_dir)

    try:
        data = at.remove_aliens(data, config_map, data_dir)
    except AttributeError:
        pass
    except Exception as e:
        print ('exiting, error in aliens removal ', str(e))
        return

    try:
        amp_threshold = config_map.amp_threshold
    except AttributeError:
        print ('define amplitude threshold. Exiting')
        return

    # zero out the noise
    prep_data = np.where(data <= amp_threshold, 0, data)

    # square root data
    prep_data = np.sqrt(prep_data)

    try:
        crops_pads = config_map.adjust_dimensions
        # the adjust_dimension parameter list holds adjustment in each direction. Append 0s, if shorter
        if len(crops_pads) < 6:
            for _ in range (6 - len(crops_pads)):
                crops_pads.append(0)
    except AttributeError:
        # the size still has to be adjusted to the opencl supported dimension
        crops_pads = (0, 0, 0, 0, 0, 0)
    # adjust the size, either pad with 0s or crop array
    pairs = []
    for i in range(int(len(crops_pads)/2)):
        pair = crops_pads[2*i:2*i+2]
        pairs.append(pair)

    prep_data = ut.adjust_dimensions(prep_data, pairs)
    if prep_data is None:
        print('check "adjust_dimensions" configuration')
        return

    try:
        center_shift = config_map.center_shift
        print ('shift center')
        prep_data = ut.get_centered(prep_data, center_shift)
    except AttributeError:
        prep_data = ut.get_centered(prep_data, [0,0,0])

    try:
        binsizes = config_map.binning
        try:
            bins = []
            for binsize in binsizes:
                bins.append(binsize)
            filler = len(prep_data.shape) - len(bins)
            for _ in range(filler):
                bins.append(1)
            prep_data = ut.binning(prep_data, bins)
        except:
            print ('check "binning" configuration')
    except AttributeError:
        pass

    # save data
    data_file = os.path.join(data_dir, 'data.tif')
    ut.save_tif(prep_data, data_file)
    print ('data ready for reconstruction, data dims:', prep_data.shape)
def write_conf(conf_map, dir, file):
    """
    It creates configuration file from the parameters included in dictionary, verifies, and saves in the configuration directory.
    Parameters
    ----------
    conf_map : dict
        dictionary containing configuration parameters
    dir : str
        a directory where the configuration file will be saved
    file : str
        name of the configuration file to save
    Returns
    -------
    nothing
    """
    # create "temp" file first, verify it, and if ok, copy to a configuration file
    if not os.path.exists(dir):
        os.makedirs(dir)
    conf_file = os.path.join(dir, file)
    temp_file = os.path.join(dir, 'temp')
    if os.path.isfile(temp_file):
        os.remove(temp_file)
    with open(temp_file, 'a') as f:
        for key in conf_map:
            value = conf_map[key]
            if len(value) > 0:
                f.write(key + ' = ' + conf_map[key] + '\n')
    f.close()

    if file == 'config':
        if not ver.ver_config(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the main window. Cannot save this format'
            )
            return False
    elif file == 'config_prep':
        if not ver.ver_config_prep(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Data prep tab. Cannot save this format'
            )
            return False
    elif file == 'config_data':
        if not ver.ver_config_data(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Data tab. Cannot save this format'
            )
            return False
    elif file.endswith('config_rec'):
        if not ver.ver_config_rec(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Reconstruction tab. Cannot save this format'
            )
            return False
    elif file == 'config_disp':
        if not ver.ver_config_disp(temp_file):
            os.remove(temp_file)
            msg_window(
                'please check the entries in the Display tab. Cannot save this format'
            )
            return False
    # copy if verified
    shutil.copy(temp_file, conf_file)
    os.remove(temp_file)
    return True