{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Parameter Configurations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When larvaworld is imported the registry is automatically initialized.\n", "\n", "We import the registry module **reg**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import larvaworld\n", "from larvaworld.lib import reg, sim, util\n", "\n", "# Tutorial safety switches (set True to run side-effect demos)\n", "RUN_WRITE_DEMOS = False # writes to the on-disk conf dict\n", "RUN_VISUAL_DEMOS = False # opens pygame windows / runs visualization loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The configuration types (conftypes) stored in the registry can be accessed easily :" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each conftype is managed by a dedicated instance of the **reg.generators.ConfType** class.\n", "\n", "These instances are located in a dictionary accessed as **reg.conf** under the respective conftype as key.\n", "\n", "They are easily accessed as the reg.conf is an AttrDict, \n", "\n", "For example, the ConfType instance responsible for a conftype :\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(larvaworld.CONFTYPES)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conftype = \"Env\"\n", "ct = reg.conf.Env\n", "assert ct == reg.conf[conftype]\n", "assert ct.conftype == conftype\n", "\n", "print(f\"The ConfType for {conftype} is an instance of {ct.__class__}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each ConfType instance **ct** manages configurations of conftype **ct.conftype**.\n", "\n", "A number of stored configurations are kept as entries in a dictionary **ct.dict** stored at **ct.path_to_dict**.\n", "\n", "Each entry has a unique ID (confID) as key. The list of available IDs cna be accessed as **ct.confIDs**.\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The dictionary\n", "print(\n", " f\"The dictionary storing {ct.conftype} configurations is an instance of {ct.dict.__class__}\"\n", ")\n", "\n", "\n", "# The path where the dictionary is stored:\n", "print(f\"It is stored at {ct.path_to_dict}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"The number of stored configurations per conftype & some example IDs:\")\n", "print()\n", "for k in larvaworld.CONFTYPES:\n", " ct = reg.conf[k]\n", " assert k == ct.conftype\n", " ids = ct.confIDs\n", " print(f\" - {k} : {len(ids)} eg : {ids[:3]}\")\n", " # print(f' {ids}')\n", " # print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ct = reg.conf.Env\n", "id = ct.confIDs[1]\n", "\n", "\n", "# The configuration IDs are the keys. They correspond to a configuration stored as a nested dictionary :\n", "entry1 = ct.dict[id]\n", "print()\n", "print(f\"An instance of {entry1.__class__}\")\n", "\n", "# The configuration can be retrieved directly by :\n", "entry2 = ct.getID(id)\n", "assert entry1 == entry2\n", "\n", "# The configuration entry is a nested dict and can be printed easily as such :\n", "print(f\"The lab-specific data-format configuration stored under ID {id} is :\")\n", "entry2.print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A new configuration can be created by altering an existing :\n", "new_conf = entry1.get_copy()\n", "new_conf.arena.dims = (0.5, 0.1)\n", "print(f\"Old : {entry1.arena.dims} vs New : {new_conf.arena.dims}\")\n", "\n", "\n", "# and then stored under an ID :\n", "\n", "# Persisting configuration IDs to disk is optional (writes to the conf dict file).\n", "if RUN_WRITE_DEMOS:\n", " new_id = \"new_confID\"\n", " assert new_id not in ct.confIDs\n", " ct.setID(id=new_id, conf=new_conf)\n", " assert new_id in ct.confIDs\n", "\n", " # an entry can be deleted :\n", " ct.delete(id=new_id)\n", " assert new_id not in ct.confIDs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The configuration object can be retrieved directly by :\n", "obj = ct.get(id)\n", "print(f\"The object under the ID : {id} is an instance of {obj.__class__}\")\n", "print()\n", "\n", "# %params obj" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext param.ipython\n", "from param.ipython import ParamPager\n", "import panel as pn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from larvaworld.lib.reg.generators import EnvConf\n", "\n", "%params EnvConf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for id in ct.confIDs:\n", " obj = ct.get(id)\n", " # %params obj" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "obj = ct.get(ct.confIDs[2])\n", "%params obj.odorscape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if RUN_VISUAL_DEMOS:\n", " for id in ct.confIDs:\n", " obj = ct.get(id)\n", " obj.visualize(duration=0.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if RUN_VISUAL_DEMOS:\n", " for id in ct.confIDs:\n", " obj = ct.get(id)\n", " print(id)\n", " p = util.AttrDict({\"env_params\": obj.nestedConf})\n", "\n", " m = sim.base_run.BaseRun(\n", " runtype=\"Exp\",\n", " experiment=\"dish\",\n", " parameters=p,\n", " id=obj.name,\n", " duration=0.3,\n", " screen_kws={\n", " \"show_display\": True,\n", " \"vis_mode\": \"video\",\n", " \"odor_aura\": True,\n", " \"intro_text\": False,\n", " \"fps\": 60,\n", " },\n", " )\n", " m.build_env(m.p.env_params)\n", " m.set_obj_visibility(m.sensorscapes, True)\n", " m.run()\n", " m.screen_manager.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "larvaworld_autoversioning", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }