Esempio n. 1
0
    def setup(self):
        """Set up the Experiment including its Population, Resources, and Actions"""
        if self.seed == -1:
            configseed = self.config.getint(self.config_section,
                                            "seed",
                                            default=-1)
            if configseed != -1:
                self.seed = configseed
            else:
                self.seed = int(time.time() * 10)

        random.seed(self.seed)
        self.config.set(self.config_section, 'seed', self.seed)

        self.experiment_epochs = self.config.getint(self.config_section,
                                                    'epochs',
                                                    default=-1)

        # Create the data directory.  If the directory already exists, move it
        # to a new directory named after the current name with a timestamp
        # appended
        data_dir = self.config.get(section=self.config_section,
                                   name='data_dir',
                                   default='data')

        if os.path.exists(data_dir):
            newname = data_dir + '-' + datetime.datetime.now().strftime(
                "%Y%m%d%H%M%S")
            shutil.move(data_dir, newname)

        os.mkdir(data_dir)

        # Create a plugin manager.  Append the system-wide plugins
        # to the list of plugin sources.
        self.plugin_manager = PluginManager(self)
        global_plugin_path = os.path.join(os.path.dirname(seeds.__file__),
                                          "plugins")
        for d in ["cell", "topology", "action", "resource"]:
            plugin_path = os.path.join(global_plugin_path, d)
            self.plugin_manager.append_dir(plugin_path)

        # Initialize all of the Resources
        self.data['resources'] = {}
        resourcestring = self.config.get(self.config_section, "resources")
        if resourcestring:
            reslist = [res.strip() for res in resourcestring.split(',')]

            for res in reslist:
                sec = "Resource:{resname}".format(resname=res)
                if not self.config.has_section(sec):
                    raise ConfigurationError(
                        "No configuration for resource '{resname}'".format(
                            resname=res))

                r = Resource(experiment=self, label=res)

                if r.name not in self.resources:
                    self.resources[r.name] = r
                else:
                    warn(
                        "Resource '{resname}' listed twice. Skipping duplicates."
                        .format(resname=res))

        # Create the Population
        self.data['population'] = {}

        population_raw = self.config.get(self.config_section,
                                         "population",
                                         default="Population")
        parsed = population_raw.split(':')

        if parsed[0] != "Population":
            raise ConfigurationError(
                "Population configuration section name must begin with 'Population'"
            )

        if len(parsed) > 1:
            poplabel = parsed[1]
        else:
            poplabel = None

        self.population = Population(experiment=self, label=poplabel)

        # Setup the list of Actions to be run
        actionstring = self.config.get(section=self.config_section,
                                       name="actions")

        if actionstring:
            actionlist = [action.strip() for action in actionstring.split(',')]

            for item in actionlist:
                parsed = item.split(':')
                action = parsed[0]

                if len(parsed) > 1:
                    label = parsed[1]
                else:
                    label = None

                oref = self.plugin_manager.get_action_plugin(action)
                a = oref(self, label=label)
                self.add_action(a)

        self.is_setup = True