{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A simple parameter exploration\n", "\n", "This notebook demonstrates a very simple parameter exploration of a custom function that we have defined. It is a simple function that returns the distance to a unit circle, so we expect our parameter exploration to resemble a circle. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# change to the root directory of the project\n", "import os\n", "if os.getcwd().split(\"/\")[-1] == \"examples\":\n", " os.chdir('..')\n", " \n", "# This will reload all imports as soon as the code changes\n", "%load_ext autoreload\n", "%autoreload 2 " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "try:\n", " import matplotlib.pyplot as plt\n", "except ImportError:\n", " import sys\n", " !{sys.executable} -m pip install matplotlib\n", " import matplotlib.pyplot as plt\n", " \n", "import numpy as np\n", "\n", "from neurolib.utils.parameterSpace import ParameterSpace\n", "from neurolib.optimize.exploration import BoxSearch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the evaluation function\n", "Here we define a very simple evaluation function. The function needs to take in `traj` as an argument, which is the pypet trajectory. This is how the function knows what parameters were assigned to it. Using the builtin function `search.getParametersFromTraj(traj)` we can then retrieve the parameters for this run. They are returned as a dictionary and can be accessed in the function. \n", "\n", "In the last step, we use `search.saveToPypet(result_dict, traj)` to save the results to the pypet trajectory and to an HDF. In between, the computational magic happens!" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def explore_me(traj):\n", " pars = search.getParametersFromTraj(traj)\n", " # let's calculate the distance to a circle\n", " computation_result = abs((pars['x']**2 + pars['y']**2) - 1)\n", " result_dict = {\"distance\" : computation_result}\n", " search.saveToPypet(result_dict, traj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the parameter space and exploration\n", "\n", "Here we define which space we want to cover. For this, we use the builtin class `ParameterSpace` which provides a very easy interface to the exploration. To initialize the exploration, we simply pass the evaluation function and the parameter space to the `BoxSearch` class." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "parameters = ParameterSpace({\"x\": np.linspace(-2, 2, 2), \"y\": np.linspace(-2, 2, 2)})\n", "# info: chose np.linspace(-2, 2, 40) or more, values here are low for testing\n", "search = BoxSearch(evalFunction = explore_me, parameterSpace = parameters, filename=\"example-1.1.hdf\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run\n", "\n", "And off we go!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "search.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get results\n", "\n", "We can easily obtain the results from pypet. First we call `search.loadResults()` to make sure that the results are loaded from the hdf file to our instance." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "search.loadResults()\n", "print(\"Number of results: {}\".format(len(search.results)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The runs are also ordered in a simple pandas dataframe called `search.dfResults`. We cycle through all results by calling `search.results[i]` and loading the desired result (here the distance to the circle) into the dataframe" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | x | \n", "y | \n", "distance | \n", "
|---|---|---|---|
| 0 | \n", "-2.0 | \n", "-2.000000 | \n", "7.000000 | \n", "
| 1 | \n", "-2.0 | \n", "-1.897436 | \n", "6.600263 | \n", "
| 2 | \n", "-2.0 | \n", "-1.794872 | \n", "6.221565 | \n", "
| 3 | \n", "-2.0 | \n", "-1.692308 | \n", "5.863905 | \n", "
| 4 | \n", "-2.0 | \n", "-1.589744 | \n", "5.527285 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 1595 | \n", "2.0 | \n", "1.589744 | \n", "5.527285 | \n", "
| 1596 | \n", "2.0 | \n", "1.692308 | \n", "5.863905 | \n", "
| 1597 | \n", "2.0 | \n", "1.794872 | \n", "6.221565 | \n", "
| 1598 | \n", "2.0 | \n", "1.897436 | \n", "6.600263 | \n", "
| 1599 | \n", "2.0 | \n", "2.000000 | \n", "7.000000 | \n", "
1600 rows × 3 columns
\n", "