{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Xmen Experiment Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining Experiments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```experiments\n", "from xmen.experiment import Experiment\n", "import os\n", "import time\n", "from typing import List\n", "\n", "\n", "class BaseExperiment(Experiment):\n", " \"\"\"A basic experiments experiment demonstrating the features of the xmen api.\"\"\"\n", "\n", " # Parameters are defined as attributes in the class body with the\n", " # @p identifier\n", "\n", " t = 'cat' # @p\n", " w = 3 # @p parameter w has a help message whilst t does not\n", " h: int = 10 # @p h declared with typing is very concise and neat\n", "\n", " # Parameters can also be defined in the __init__ method\n", " def __init__(self, *args, **kwargs):\n", " super(BaseExperiment, self).__init__(*args, **kwargs)\n", "\n", " self.a: str = 'h' # @p A parameter\n", " self.b: int = 17 # @p Another parameter\n", "\n", " # Normal attributes are still allowed\n", " self.c: int = 5 # This is not a parameter\n", "\n", "\n", "class AnotherExperiment(BaseExperiment):\n", " m: str = 'Another value' # @p Multiple inheritance example\n", " p: str = 'A parameter only in Another Experiment ' # @p\n", "\n", "\n", "class AnExperiment(BaseExperiment):\n", " # |\n", " # Experiments can inherit from other experiments\n", " # parameters are inherited too\n", " \"\"\"An experiment testing the xmen experiment API. The __docstring__ will\n", " appear in both the docstring of the class __and__ as the prolog in the\n", " command line interface.\"\"\"\n", "\n", " # Feel free to define more parameters\n", " x: List[float] = [3., 2.] # @p Parameters can be defined cleanly as class attributes\n", " y: float = 5 # @p This parameter will have this\n", " # Parameters can be overridden\n", " a: float = 0.5 # a's default and type will be changed. Its help will be overridden\n", " b: int = 17 # @p b's help will be changed\n", "\n", " m: str = 'Defined in AnExperiment' # @p m is defined in AnExperiment\n", "\n", " def run(self):\n", " # Experiment execution is defined in the run method\n", " print(f'The experiment state inside run is {self.status}')\n", "\n", " # recording messaging is super easy\n", " self.message({'time': time.time()})\n", "\n", " # Each experiment has its own unique directory. You are encourage to\n", " # write out data accumulated through the execution (snapshots, logs etc.)\n", " # to this directory.\n", " with open(os.path.join(self.directory, 'logs.txt'), 'w') as f:\n", " f.write('This was written from a running experiment')\n", "\n", " def debug(self):\n", " self.a = 'In debug mode'\n", " return self\n", "\n", " @property\n", " def h(self):\n", " return 'h has been overloaded as propery and will no longer' \\\n", " 'considered as a parameter'\n", "\n", "\n", "# Experiments can inheret from multiple classes\n", "class MultiParentsExperiment(AnotherExperiment, AnExperiment):\n", " pass\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: the above is a copy of what is defined in ``xmen.examplex.inheritance``" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from xmen.examples.inheritance import AnExperiment, AnotherExperiment, MultiParentsExperiment\n", "from notebook.services.config import ConfigManager\n", "cm = ConfigManager().update('notebook', {'limit_output': 10})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Automatic Documentation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class AnExperiment in module xmen.examples.inheritance:\n", "\n", "class AnExperiment(BaseExperiment)\n", " | AnExperiment(*args, **kwargs)\n", " | \n", " | An experiment testing the xmen experiment API. The __docstring__ will\n", " | appear in both the docstring of the class __and__ as the prolog in the\n", " | command line interface.\n", " | \n", " | Parameters:\n", " | BaseExperiment\n", " | t: None (default=cat)\n", " | w: parameter w has a help message whilst t does not (default=3)\n", " | a (float): A parameter (default=0.5)\n", " | AnExperiment\n", " | b: int=17 ~ b's help will be changed\n", " | x: List[float]=[3., 2.] ~ Parameters can be defined cleanly as class attributes\n", " | y: float=5 ~ This parameter will have this\n", " | m: str='Defined in AnExperiment' ~ m is defined in AnExperiment\n", " | \n", " | Method resolution order:\n", " | AnExperiment\n", " | BaseExperiment\n", " | xmen.experiment.Experiment\n", " | builtins.object\n", " | \n", " | Methods defined here:\n", " | \n", " | debug(self)\n", " | Inherited classes may overload debug. Used to define a set of open_socket for minimum example\n", " | \n", " | run(self)\n", " | \n", " | ----------------------------------------------------------------------\n", " | Readonly properties defined here:\n", " | \n", " | h\n", " | int([x]) -> integer\n", " | int(x, base=10) -> integer\n", " | \n", " | Convert a number or string to an integer, or return 0 if no arguments\n", " | are given. If x is a number, return x.__int__(). For floating point\n", " | numbers, this truncates towards zero.\n", " | \n", " | If x is not a number or if base is given, then x must be a string,\n", " | bytes, or bytearray instance representing an integer literal in the\n", " | given base. The literal can be preceded by '+' or '-' and be surrounded\n", " | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n", " | Base 0 means to interpret the base from the string as an integer literal.\n", " | >>> int('0b100', base=0)\n", " | 4\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data and other attributes defined here:\n", " | \n", " | __annotations__ = {'a': , 'b': , 'm':