{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# Basic Usage\n", "\n", "Here, we demonstrate how to filter conditions where we suspect the model to be undefined" ] }, { "cell_type": "code", "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-09T23:33:58.949255Z", "start_time": "2024-08-09T23:33:57.538974Z" } }, "source": [ "import math\n", "\n", "import numpy as np\n", "\n", "from autora.experimentalist.prediction_filter import prediction_filter\n", "import pandas as pd" ], "outputs": [], "execution_count": 1 }, { "cell_type": "markdown", "source": [ "Create a model:" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "source": [ "class ModelWithNans:\n", " def predict(self, x):\n", " try:\n", " return math.sqrt((x**2 - 4))\n", " except:\n", " return None\n", "model = ModelWithNans()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-09T23:34:00.789425Z", "start_time": "2024-08-09T23:34:00.787357Z" } }, "outputs": [], "execution_count": 2 }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Let's create our pool:" ] }, { "cell_type": "code", "source": [ "pool = pd.DataFrame({'x': np.linspace(-5, 5, 11)})\n", "pool" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-09T23:34:01.863883Z", "start_time": "2024-08-09T23:34:01.853300Z" } }, "outputs": [ { "data": { "text/plain": [ " x\n", "0 -5.0\n", "1 -4.0\n", "2 -3.0\n", "3 -2.0\n", "4 -1.0\n", "5 0.0\n", "6 1.0\n", "7 2.0\n", "8 3.0\n", "9 4.0\n", "10 5.0" ], "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
x
0-5.0
1-4.0
2-3.0
3-2.0
4-1.0
50.0
61.0
72.0
83.0
94.0
105.0
\n", "
" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 3 }, { "cell_type": "markdown", "source": [ "Let's create our filter function. This is a function that returns True if a value should be included" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "source": [ "def filter_function(x):\n", " return x is not None" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-09T23:34:03.180033Z", "start_time": "2024-08-09T23:34:03.177992Z" } }, "outputs": [], "execution_count": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's use our filter on the conditions:" ] }, { "cell_type": "code", "source": [ "filtered_conditions = prediction_filter(pool, model, filter_function)\n", "filtered_conditions" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-09T23:34:06.315173Z", "start_time": "2024-08-09T23:34:06.145004Z" } }, "outputs": [ { "ename": "AxisError", "evalue": "axis 1 is out of bounds for array of dimension 0", "output_type": "error", "traceback": [ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[0;31mAxisError\u001B[0m Traceback (most recent call last)", "Cell \u001B[0;32mIn[5], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m filtered_conditions \u001B[38;5;241m=\u001B[39m \u001B[43mprediction_filter\u001B[49m\u001B[43m(\u001B[49m\u001B[43mpool\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mfilter_function\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 2\u001B[0m filtered_conditions\n", "File \u001B[0;32m~/Documents/GitHub/AutoResearch/autora-experimentalist-prediction-filter/src/autora/experimentalist/prediction_filter/__init__.py:71\u001B[0m, in \u001B[0;36mfilter\u001B[0;34m(conditions, model, filter_function)\u001B[0m\n\u001B[1;32m 16\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m 17\u001B[0m \u001B[38;5;124;03mFilter conditions based on the expected outcome io the mdeol\u001B[39;00m\n\u001B[1;32m 18\u001B[0m \n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 68\u001B[0m \u001B[38;5;124;03m 0 1 1\u001B[39;00m\n\u001B[1;32m 69\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m 70\u001B[0m _pred \u001B[38;5;241m=\u001B[39m model\u001B[38;5;241m.\u001B[39mpredict(conditions)\n\u001B[0;32m---> 71\u001B[0m _filter \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mapply_along_axis\u001B[49m\u001B[43m(\u001B[49m\u001B[43mfilter_function\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m_pred\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 72\u001B[0m _filter \u001B[38;5;241m=\u001B[39m _filter\u001B[38;5;241m.\u001B[39mreshape(\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m)\n\u001B[1;32m 74\u001B[0m new_conditions \u001B[38;5;241m=\u001B[39m conditions[\u001B[38;5;28mlist\u001B[39m(_filter[\u001B[38;5;241m0\u001B[39m])]\n", "File \u001B[0;32m~/Documents/GitHub/AutoResearch/autora-experimentalist-prediction-filter/venv/lib/python3.11/site-packages/numpy/lib/shape_base.py:361\u001B[0m, in \u001B[0;36mapply_along_axis\u001B[0;34m(func1d, axis, arr, *args, **kwargs)\u001B[0m\n\u001B[1;32m 359\u001B[0m arr \u001B[38;5;241m=\u001B[39m asanyarray(arr)\n\u001B[1;32m 360\u001B[0m nd \u001B[38;5;241m=\u001B[39m arr\u001B[38;5;241m.\u001B[39mndim\n\u001B[0;32m--> 361\u001B[0m axis \u001B[38;5;241m=\u001B[39m \u001B[43mnormalize_axis_index\u001B[49m\u001B[43m(\u001B[49m\u001B[43maxis\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mnd\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 363\u001B[0m \u001B[38;5;66;03m# arr, with the iteration axis at the end\u001B[39;00m\n\u001B[1;32m 364\u001B[0m in_dims \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mlist\u001B[39m(\u001B[38;5;28mrange\u001B[39m(nd))\n", "\u001B[0;31mAxisError\u001B[0m: axis 1 is out of bounds for array of dimension 0" ] } ], "execution_count": 5 }, { "cell_type": "code", "source": [], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-09T23:34:12.104384Z", "start_time": "2024-08-09T23:34:12.102929Z" } }, "outputs": [], "execution_count": 5 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }