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_prep)
        Returns
        -------
        nothing
        """
        if os.path.isfile(load_item):
            # the configuration file was given
            conf = load_item
        else:
            conf = os.path.join(load_item, 'conf', 'config_prep')
            if not os.path.isfile(conf):
                msg_window(
                    'info: the load directory does not contain config_prep file'
                )
                return
        if not ver.ver_config_prep(conf):
            msg_window('please check configuration file ' + conf +
                       '. Cannot parse, ')
            return

        self.parse_spec()

        try:
            conf_map = ut.read_config(conf)
        except Exception as e:
            msg_window('please check configuration file ' + conf +
                       '. Cannot parse, ' + str(e))
            return

        try:
            separate_scans = conf_map.separate_scans
            if separate_scans:
                self.separate_scans.setChecked(True)
            else:
                self.separate_scans.setChecked(False)
        except:
            self.separate_scans.setChecked(False)
        # the separate_scan parameter affects other tab (results_dir in dispaly tab)
        # this tab has to notify observer about the initial setup
        self.notify()
        try:
            if os.path.isdir(conf_map.data_dir):
                self.data_dir_button.setStyleSheet("Text-align:left")
                self.data_dir_button.setText(conf_map.data_dir)
            else:
                msg_window('The data_dir directory in config_prep file  ' +
                           conf_map.data_dir + ' does not exist')
        except:
            self.data_dir_button.setText('')
        try:
            if os.path.isfile(conf_map.darkfield_filename):
                self.dark_file_button.setStyleSheet("Text-align:left")
                self.dark_file_button.setText(conf_map.darkfield_filename)
            else:
                msg_window('The darkfield file ' +
                           conf_map.darkfield_filename +
                           ' in config_prep file does not exist')
                self.dark_file_button.setText('')
        except:
            self.dark_file_button.setText('')
        try:
            if os.path.isfile(conf_map.whitefield_filename):
                self.white_file_button.setStyleSheet("Text-align:left")
                self.white_file_button.setText(conf_map.whitefield_filename)
            else:
                self.white_file_button.setText('')
                msg_window('The whitefield file ' +
                           conf_map.whitefield_filename +
                           ' in config_prep file does not exist')
        except:
            self.white_file_button.setText('')
        try:
            self.Imult.setText(str(conf_map.Imult).replace(" ", ""))
        except:
            pass
        try:
            self.detector.setText(str(conf_map.detector).replace(" ", ""))
        except:
            pass
        try:
            self.min_files.setText(str(conf_map.min_files).replace(" ", ""))
        except:
            pass
        try:
            self.exclude_scans.setText(
                str(conf_map.exclude_scans).replace(" ", ""))
        except:
            pass
        try:
            self.roi.setText(str(conf_map.roi).replace(" ", ""))
        except:
            pass
Exemplo n.º 2
0
def setup_rundirs(prefix, scan, conf_dir, **kwargs):
    """
    Concludes the experiment directory, creates main configuration files, and calls function to copy other configuration files.

    Parameters
    ----------
    prefix : str
        prefix to name of the experiment/data reconstruction
    scan : str
        a range of scans to prepare data from
    conf_dir : str
        directory from where the configuration files will be copied
    specfile : str
        optional, from kwargs, specfile configuration to write to config file
    copy_prep : bool
        optional, from kwargs, if sets to true, the prepared file is also copied
        
    Returns
    -------
    nothing
    """
    id = prefix + '_' + scan

    if not os.path.isdir(conf_dir):
        print('configured directory ' + conf_dir + ' does not exist')
        return

    main_conf = os.path.join(conf_dir, 'config')
    if not os.path.isfile(main_conf):
        print('the configuration directory does not contain "config" file')
        return

    if not ver.ver_config_prep(main_conf):
        return

    try:
        with open(main_conf, 'r') as f:
            config_map = cfg.Config(f.read())
    except Exception as e:
        print('Please check the configuration file ' + main_conf +
              '. Cannot parse ' + str(e))
        return

    try:
        working_dir = config_map.working_dir.strip()
    except:
        working_dir = os.getcwd()

    experiment_dir = os.path.join(working_dir, id)
    if not os.path.exists(experiment_dir):
        os.makedirs(experiment_dir)
    # copy config_data, config_rec, cofig_disp files from cofig directory into the experiment conf directory
    experiment_conf_dir = os.path.join(experiment_dir, 'conf')
    if not os.path.exists(experiment_conf_dir):
        os.makedirs(experiment_conf_dir)

    # here we want the command line to be used if present, so need to check if None was passed or not.
    if 'specfile' in kwargs:
        specfile = kwargs['specfile']
    if specfile is None:
        try:
            specfile = config_map.specfile.strip()
        except:
            print("Specfile not in config or command line")

    # Based on params passed to this function create a temp config file and then copy it to the experiment dir.
    experiment_main_config = os.path.join(experiment_conf_dir, 'config')
    conf_map = {}
    conf_map['working_dir'] = '"' + working_dir + '"'
    conf_map['experiment_id'] = '"' + prefix + '"'
    conf_map['scan'] = '"' + scan + '"'
    if specfile is not None:
        conf_map['specfile'] = '"' + specfile + '"'
    temp_file = os.path.join(experiment_conf_dir, 'temp')
    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 not ver.ver_config(temp_file):
        print('please check the entered parameters. Cannot save this format')
    else:
        shutil.copy(temp_file, experiment_main_config)
    os.remove(temp_file)

    copy_conf(conf_dir, experiment_conf_dir)
    if 'copy_prep' in kwargs:
        copy_prep = kwargs['copy_prep']
    if copy_prep:
        # use abspath to get rid of trailing dir sep if it is there
        other_exp_dir = os.path.split(os.path.abspath(conf_dir))[0]
        new_exp_dir = os.path.split(os.path.abspath(experiment_conf_dir))[0]

        # get case of single scan or summed
        prep_dir_list = glob.glob(os.path.join(other_exp_dir, 'prep'),
                                  recursive=True)
        for dir in prep_dir_list:
            shutil.copytree(dir, os.path.join(new_exp_dir, 'prep'))

            # get case of split scans
        prep_dir_list = glob.glob(os.path.join(other_exp_dir, "scan*/prep"),
                                  recursive=True)
        for dir in prep_dir_list:
            scandir = os.path.basename(os.path.split(dir)[0])
            shutil.copytree(dir, os.path.join(new_exp_dir, *(scandir, 'prep')))
    return experiment_dir
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