Exemplo n.º 1
0
 def test_works(self):
     output_file = tempfile.mkdtemp()
     config = Configuration(output_file)
     config.build_configs()
     config.next()
     config['random_seed'] = '234524'
     self.assertTrue(config['random_seed'] == '234524')
     content = config.generate_experiement_config()
     self.assertTrue('234524' in content)
     while config.next():
         content = config.generate_experiement_config()
     shutil.rmtree(output_file)
    def setup_experiments(self, output_directory):
        '''
        Generate experiement configurationas and create file structure
        '''
        logging.info('setting up experiments ...')
        count = 0

        exp_file_path = os.path.join(output_directory, 'experiments.json')
        # load existing experiment configurations
        if os.path.exists(exp_file_path):
            with open(exp_file_path, 'r') as e_file:
                self._experiement_configurations = json.loads(e_file.read())
            count = len(self._experiement_configurations)
            logging.info('Loaded %s existing experiment configurations', count)

        # build the new experiment configurations
        config_manager = Configuration(output_directory)
        config_manager.build_configs()

        config_count = 1
        while config_manager.next():
            logging.info('Writing config %d of %d', config_count,
                         config_manager.get_total_count())
            config_count += 1

            exp_file_name = os.path.join(output_directory,
                                         config_manager.get_file_path(),
                                         'config.cfg')
            exp_file_name = os.path.abspath(exp_file_name)
            dir_name = os.path.dirname(exp_file_name)

            config_file_name = os.path.join(dir_name, 'config.json')
            exp_archived = os.path.join(dir_name, 'archive.zip')

            # check if directory exists
            if not os.path.exists(dir_name):
                os.makedirs(dir_name)

            # write the experiment config
            exp_files = {}
            current_config = config_manager.get_config()
            # hack to fix an issue
            # write the settings in JSON format for easy parsing
            with open(config_file_name, 'w') as c_file:
                c_file.write(json.dumps(current_config))
            # end hack
            # only write new files if there isn't already files there
            if os.path.exists(exp_archived):
                logging.info(
                    'Experiment already run and archived, skipping file write')
            elif not os.path.exists(exp_file_name) or not os.path.exists(
                    config_file_name):
                with open(exp_file_name, 'w') as c_file:
                    c_file.write(config_manager.generate_experiement_config())
            else:
                logging.info(
                    'Experiment config already exists, skipping file write')

            config_file_name = config_file_name.replace(
                output_directory + os.sep, '')
            exp_file_name = exp_file_name.replace(output_directory + os.sep,
                                                  '')

            # check if we need to add the experiment entry
            if not self._find(config_file_name):
                count += 1
                exp_files[CONST_EXPERIMENT] = exp_file_name
                exp_files[CONST_CONFIG] = config_file_name
                exp_files[CONST_GROUP] = config_manager.get_group_hash(
                    current_config)
                exp_files[CONST_ID] = count
                self._experiement_configurations.append(exp_files)

        with open(exp_file_path, 'w') as e_file:
            e_file.write(json.dumps(self._experiement_configurations))

        total = len(self._experiement_configurations)
        logging.info('Generated %s experiment configurations', total)
        return total