Skip to content

Commit

Permalink
Refactor environment processing and yaw module results methods; updat…
Browse files Browse the repository at this point in the history
…e PCTExamples to include environment properties
  • Loading branch information
perceptualrobots committed Dec 20, 2024
1 parent e1b4f2d commit b676896
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 224 deletions.
147 changes: 93 additions & 54 deletions nbs/04_hierarchy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,95 @@
" return plots_list\n",
"\n",
" \n",
"\n",
"\n",
" \n",
" ## run_from_file\n",
" @classmethod\n",
" def run_from_file(cls, filename, min=None, env_props=None, seed=None, render=False, history=False, move=None, plots=None, hpct_verbose= False, \n",
" runs=None, plots_dir=None, early_termination = None, draw_file=None, experiment=None, log_experiment_figure=False, suffixes=False,\n",
" enhanced_environment_properties=None, title_prefix=\"\"):\n",
" \n",
" prp = PCTRunProperties()\n",
" prp.load_db(filename)\n",
" if experiment:\n",
" config = prp.db.pop('config')\n",
" experiment.log_parameters(prp.db)\n",
" prp.db['config'] = config\n",
" if 'environment_properties' in prp.db:\n",
" if 'history' in prp.db['environment_properties']:\n",
" ep = eval(prp.db['environment_properties'])\n",
" experiment.log_metric('history', ep['history'])\n",
"\n",
" error_collector_type = prp.db['error_collector_type'].strip()\n",
" error_response_type = prp.db['error_response_type']\n",
" error_limit = eval(prp.db['error_limit'])\n",
" if env_props is None:\n",
" environment_properties = eval(prp.db['environment_properties'])\n",
" else:\n",
" environment_properties = env_props \n",
" error_properties = prp.get_error_properties()\n",
"\n",
" if enhanced_environment_properties is not None:\n",
" environment_properties = environment_properties | enhanced_environment_properties \n",
" if environment_properties['dataset'] == 'test':\n",
" error_properties[1][1] = environment_properties['initial']\n",
" # print(environment_properties)\n",
" if runs==None:\n",
" runs = eval(prp.db['runs'])\n",
" config = eval(prp.db['config'])\n",
" if seed is None:\n",
" seed = eval(prp.db['seed'])\n",
" # print(f'Seed={seed}')\n",
" if early_termination is None:\n",
" early_termination = eval(prp.db['early_termination'])\n",
"\n",
" hierarchy, score = cls.run_from_config(config, min=min, render=render, error_collector_type=error_collector_type, error_response_type=error_response_type, \n",
" error_properties=error_properties, error_limit=error_limit, steps=runs, hpct_verbose=hpct_verbose, history=history, \n",
" environment_properties=environment_properties, seed=seed, early_termination=early_termination, move=move, plots=plots, \n",
" suffixes=suffixes, plots_dir=plots_dir, draw_file=draw_file, experiment=experiment, log_experiment_figure=log_experiment_figure, \n",
" title_prefix = title_prefix)\n",
" \n",
" return hierarchy, score \n",
"\n",
" def run_hierarchy(self, render=False, steps=500, hpct_verbose=False):\n",
" env = self.get_preprocessor()[0]\n",
" env.set_render(render)\n",
" if hpct_verbose:\n",
" self.summary()\n",
" print(self.formatted_config())\n",
" self.run(steps, hpct_verbose)\n",
" env.close()\n",
"\n",
" score = self.get_environment_score() if self.get_environment_score() is not None else self.get_error_collector().error()\n",
"\n",
" return score \n",
" \n",
" def draw_hierarchy(self, draw_file=False, move=None, with_edge_labels=True, font_size=6, node_size=100, experiment=None, log_experiment_figure=False):\n",
" # draw network file\n",
" move = {} if move == None else move\n",
" if experiment or draw_file:\n",
" if log_experiment_figure:\n",
" self.draw(file=draw_file, move=move, with_edge_labels=with_edge_labels, font_size=font_size, node_size=node_size, experiment=experiment)\n",
" else:\n",
" self.draw(file=draw_file, move=move, with_edge_labels=with_edge_labels, font_size=font_size, node_size=node_size)\n",
" if draw_file:\n",
" print(draw_file)\n",
"\n",
" def plot_hierarchy(self, plots=None,\n",
" history=False, plots_figsize=(15,4), plots_dir=None, experiment=None, title_prefix=\"\"):\n",
" if history:\n",
" if plots:\n",
" plots = self.get_plots_config(plots, title_prefix)\n",
" \n",
" for plot in plots:\n",
" plotfile=None\n",
" if plots_dir:\n",
" plotfile = plots_dir + sep + plot['title'] + '-' + str(self.get_namespace()) + '.png'\n",
" fig = self.hierarchy_plots(title=plot['title'], plot_items=plot['plot_items'], figsize=plots_figsize, file=plotfile, experiment=experiment)\n",
" import matplotlib.pyplot as plt\n",
" plt.close(fig) # Close the figure here\n",
"\n",
" @classmethod\n",
" def run_from_config(cls, config, min=None, render=False, error_collector_type=None, error_response_type=None, \n",
" error_properties=None, error_limit=100, steps=500, hpct_verbose=False, early_termination=False, \n",
Expand Down Expand Up @@ -1326,59 +1415,8 @@
" return hierarchy, score \n",
" \n",
"\n",
"\n",
" \n",
" ## run_from_file\n",
" @classmethod\n",
" def run_from_file(cls, filename, min=None, env_props=None, seed=None, render=False, history=False, move=None, plots=None, hpct_verbose= False, \n",
" runs=None, plots_dir=None, early_termination = None, draw_file=None, experiment=None, log_experiment_figure=False, suffixes=False,\n",
" enhanced_environment_properties=None, title_prefix=\"\"):\n",
" \n",
" prp = PCTRunProperties()\n",
" prp.load_db(filename)\n",
" if experiment:\n",
" config = prp.db.pop('config')\n",
" experiment.log_parameters(prp.db)\n",
" prp.db['config'] = config\n",
" if 'environment_properties' in prp.db:\n",
" if 'history' in prp.db['environment_properties']:\n",
" ep = eval(prp.db['environment_properties'])\n",
" experiment.log_metric('history', ep['history'])\n",
"\n",
" error_collector_type = prp.db['error_collector_type'].strip()\n",
" error_response_type = prp.db['error_response_type']\n",
" error_limit = eval(prp.db['error_limit'])\n",
" if env_props is None:\n",
" environment_properties = eval(prp.db['environment_properties'])\n",
" else:\n",
" environment_properties = env_props \n",
" error_properties = prp.get_error_properties()\n",
"\n",
" if enhanced_environment_properties is not None:\n",
" environment_properties = environment_properties | enhanced_environment_properties \n",
" if environment_properties['dataset'] == 'test':\n",
" error_properties[1][1] = environment_properties['initial']\n",
" # print(environment_properties)\n",
" if runs==None:\n",
" runs = eval(prp.db['runs'])\n",
" config = eval(prp.db['config'])\n",
" if seed is None:\n",
" seed = eval(prp.db['seed'])\n",
" # print(f'Seed={seed}')\n",
" if early_termination is None:\n",
" early_termination = eval(prp.db['early_termination'])\n",
"\n",
" hierarchy, score = cls.run_from_config(config, min=min, render=render, error_collector_type=error_collector_type, error_response_type=error_response_type, \n",
" error_properties=error_properties, error_limit=error_limit, steps=runs, hpct_verbose=hpct_verbose, history=history, \n",
" environment_properties=environment_properties, seed=seed, early_termination=early_termination, move=move, plots=plots, \n",
" suffixes=suffixes, plots_dir=plots_dir, draw_file=draw_file, experiment=experiment, log_experiment_figure=log_experiment_figure, \n",
" title_prefix = title_prefix)\n",
" \n",
" return hierarchy, score \n",
"\n",
"\n",
" @classmethod\n",
" def load_from_file(cls, filename, min=None, env_props=None, seed=None, render=False, runs=None, early_termination = False, experiment=None, hpct_verbose= False, history=False):\n",
" def load_from_file(cls, filename, min=None, env_props=None, seed=None, render=False, runs=None, early_termination = False, experiment=None, hpct_verbose= False, history=False, additional_props=None):\n",
" \n",
" prp = PCTRunProperties()\n",
" prp.load_db(filename)\n",
Expand All @@ -1399,7 +1437,8 @@
" else:\n",
" environment_properties = env_props \n",
" error_properties = prp.get_error_properties()\n",
"\n",
" if additional_props:\n",
" environment_properties.update(additional_props)\n",
" if runs==None:\n",
" runs = eval(prp.db['runs'])\n",
" config = eval(prp.db['config'])\n",
Expand All @@ -1413,7 +1452,7 @@
" error_properties=error_properties, error_limit=error_limit, hpct_verbose=hpct_verbose, history=history,\n",
" environment_properties=environment_properties, seed=seed, early_termination=early_termination)\n",
" \n",
" return hierarchy, env \n",
" return hierarchy, env, environment_properties\n",
"\n",
"\n",
" @classmethod\n",
Expand Down
6 changes: 4 additions & 2 deletions nbs/12_yaw_module.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,10 @@
" runs=None, plots_dir=outdir, early_termination=early, draw_file=draw_file, experiment=experiment, \n",
" log_experiment_figure=log_experiment_figure, min=min)\n",
" else:\n",
" hierarchy.run(steps=None, verbose=verbose)\n",
" score = hierarchy.get_error_collector().error()\n",
" score = hierarchy.run_hierarchy(render=False, hpct_verbose=verbose, steps=1000)\n",
"\n",
" # hierarchy.run(steps=None, verbose=verbose)\n",
" # score = hierarchy.get_error_collector().error()\n",
" \n",
" env = hierarchy.get_preprocessor()[0].env\n",
"\n",
Expand Down
5 changes: 3 additions & 2 deletions nbs/16_environment_processing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
" else:\n",
" experiment = None\n",
"\n",
" self.start = printtime('Start')\n",
"\n",
" return experiment, None\n",
"\n",
" def get_experiment_name(self):\n",
Expand Down Expand Up @@ -418,7 +418,8 @@
" def results(self, filepath=None, experiment=None, environment_properties=None, hierarchy=None):\n",
" plots=None\n",
" early=None\n",
"\n",
" self.start = printtime('Start')\n",
" \n",
" if 'log_testing_to_experiment' in self.args:\n",
" log_testing_to_experiment = self.args['log_testing_to_experiment']\n",
" else:\n",
Expand Down
Loading

0 comments on commit b676896

Please sign in to comment.