Skip to content

Commit

Permalink
Update Dynamically Extending and Altering the State.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
chadcwilliams committed Dec 20, 2023
1 parent f862a7d commit a151e83
Showing 1 changed file with 95 additions and 137 deletions.
232 changes: 95 additions & 137 deletions docs/cycle/Dynamically Extending and Altering the State.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@
"source": [
"### Defining The State\n",
"\n",
"We use the standard State object bundled with `autora`: `StandardState`. This state has four build in fields:\n",
"We use the standard State object bundled with `autora`: `StandardState`. This state has four built in fields:\n",
"`variables`, `conditions`, `experiment_data` and `models`. We can initialize some (or all) of these fields:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"StandardState(variables=VariableCollection(independent_variables=[Variable(name='x', value_range=(-15, 15), allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], dependent_variables=[Variable(name='y', value_range=None, allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], covariates=[]), conditions=None, experiment_data=None, models=[])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
Expand All @@ -41,46 +52,53 @@
"s = StandardState(\n",
" variables=VariableCollection(independent_variables=[Variable(\"x\", value_range=(-15,15))],\n",
" dependent_variables=[Variable(\"y\")]),\n",
")"
")\n",
"\n",
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding Pool To The State\n",
"First, we add a new field, `pool` to state `s`. To do this, we must expand the StandardState class, while adding the field. We want the content of this field to be replaced each time a function writes into the field."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'_metadata': {'variables': {'default': None, 'delta': 'replace'}, 'conditions': {'default': None, 'delta': 'replace'}, 'experiment_data': {'default': None, 'delta': 'extend'}, 'models': {'default': None, 'delta': 'extend'}}, 'variables': VariableCollection(independent_variables=[Variable(name='x', value_range=(-15, 15), allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], dependent_variables=[Variable(name='y', value_range=None, allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], covariates=[]), 'conditions': None, 'experiment_data': None, 'models': None}"
"ExtendedStandardState(variables=VariableCollection(independent_variables=[Variable(name='x', value_range=(-15, 15), allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], dependent_variables=[Variable(name='y', value_range=None, allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], covariates=[]), conditions=None, experiment_data=None, models=[], pool=[])"
]
},
"execution_count": null,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dataclasses import dataclass, field\n",
"\n",
"@dataclass(frozen=True)\n",
"class ExtendedStandardState(StandardState):\n",
" pool: pd.DataFrame = field(\n",
" default_factory=list,\n",
" metadata={'delta': 'replace'}\n",
" )\n",
"\n",
"s = ExtendedStandardState(\n",
" variables=VariableCollection(independent_variables=[Variable(\"x\", value_range=(-15,15))],\n",
" dependent_variables=[Variable(\"y\")])\n",
")\n",
"\n",
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding Pool To The State\n",
"First, we add a new field to `s`. We want the content of this field to be replaced each time a function writes into the field."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.add_field(name='pool', delta='replace')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -90,26 +108,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'_metadata': {'variables': {'default': None, 'delta': 'replace'}, 'conditions': {'default': None, 'delta': 'extend'}, 'experiment_data': {'default': None, 'delta': 'extend'}, 'models': {'default': None, 'delta': 'extend'}, 'pool': {'default': None, 'delta': 'replace', 'aliases': None}}, 'variables': VariableCollection(independent_variables=[Variable(name='x', value_range=(-15, 15), allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], dependent_variables=[Variable(name='y', value_range=None, allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], covariates=[]), 'conditions': None, 'experiment_data': None, 'models': None, 'pool': x\n",
"0 -12.659490\n",
"1 5.983803\n",
"2 -10.337078\n",
"3 9.287066\n",
"4 -3.707592\n",
"5 -14.107050\n",
"6 4.233732\n",
"7 -10.066782\n",
"8 -10.494731\n",
"9 -2.577266}"
"ExtendedStandardState(variables=VariableCollection(independent_variables=[Variable(name='x', value_range=(-15, 15), allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], dependent_variables=[Variable(name='y', value_range=None, allowed_values=None, units='', type=<ValueType.REAL: 'real'>, variable_label='', rescale=1, is_covariate=False)], covariates=[]), conditions=None, experiment_data=None, models=[], pool= x\n",
"0 -3.599348\n",
"1 -14.328625\n",
"2 -13.764225\n",
"3 3.656028\n",
"4 2.723904\n",
"5 -7.214785\n",
"6 6.466772\n",
"7 7.363881\n",
"8 13.304111\n",
"9 2.923905)"
]
},
"execution_count": null,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -135,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -164,39 +182,39 @@
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-13.405719</td>\n",
" <th>0</th>\n",
" <td>-3.599348</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>12.941968</td>\n",
" <th>1</th>\n",
" <td>-14.328625</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5.300515</td>\n",
" <td>-7.214785</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>3.970537</td>\n",
" <th>9</th>\n",
" <td>2.923905</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>-3.867838</td>\n",
" <th>8</th>\n",
" <td>13.304111</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" x\n",
"4 -13.405719\n",
"9 12.941968\n",
"5 5.300515\n",
"7 3.970537\n",
"6 -3.867838"
"0 -3.599348\n",
"1 -14.328625\n",
"5 -7.214785\n",
"9 2.923905\n",
"8 13.304111"
]
},
"execution_count": null,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -222,7 +240,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -251,29 +269,29 @@
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>12.941968</td>\n",
" <th>2</th>\n",
" <td>-13.764225</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>-3.867838</td>\n",
" <th>3</th>\n",
" <td>3.656028</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>11.739368</td>\n",
" <th>9</th>\n",
" <td>2.923905</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" x\n",
"9 12.941968\n",
"6 -3.867838\n",
"3 11.739368"
"2 -13.764225\n",
"3 3.656028\n",
"9 2.923905"
]
},
"execution_count": null,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -292,84 +310,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>x</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>12.941968</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-3.867838</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>11.739368</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>-3.867838</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-9.164803</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>11.739368</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>-1.972083</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" x\n",
"0 12.941968\n",
"1 -3.867838\n",
"2 11.739368\n",
"3 -3.867838\n",
"4 -9.164803\n",
"5 11.739368\n",
"6 -1.972083"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
"ename": "AttributeError",
"evalue": "'ExtendedStandardState' object has no attribute 'set_delta'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[18], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ms_3\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset_delta\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mconditions\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mextend\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 2\u001b[0m s_4 \u001b[38;5;241m=\u001b[39m sample(s_3, num_samples\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m4\u001b[39m)\n\u001b[0;32m 3\u001b[0m s_4\u001b[38;5;241m.\u001b[39mconditions\n",
"\u001b[1;31mAttributeError\u001b[0m: 'ExtendedStandardState' object has no attribute 'set_delta'"
]
}
],
"source": [
"s_3.set_delta('conditions', 'extend')\n",
"s_3.conditions.metadata['delta'] = 'extend'\n",
"s_4 = sample(s_3, num_samples=4)\n",
"s_4.conditions"
]
Expand Down Expand Up @@ -517,7 +474,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down

0 comments on commit a151e83

Please sign in to comment.