-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add new variant of the state object with a history #70
Draft
hollandjg
wants to merge
71
commits into
main
Choose a base branch
from
feat/history-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
11d03a5
feat: add dataclass based historystate
hollandjg 5ca28b1
refactor: move dataclass history to a new file
hollandjg b08288b
feat: add history state based on UserList
hollandjg 21172ab
feat: add custom repr of history state
hollandjg 25ac836
refactor: rename function to get
hollandjg 0b60fd7
feat: add new extension to state with a simple history
hollandjg d1a5b40
feat: add initialization of the history based on the initial argument…
hollandjg d17ac7e
refactor: remove warnings on unused fields
hollandjg 1288c93
refactor: simplify tests
hollandjg 43bb491
feat: add StandardStateHistory which combines both the standaard stat…
hollandjg 33d84a1
test: fix tests – missing import
hollandjg 73d613e
feat: add an alternative state history which uses a different set of …
hollandjg 6ae0504
feat: add some special history methods to the state
hollandjg af64987
chore: remove outdated dataclass implementation
hollandjg 2ce8b1e
chore: remove outdated list implementation
hollandjg 3b2d7ef
refactor: split standard implementation into its own file
hollandjg d9f335d
test: add delta import to doctest
hollandjg e0878fb
refactor: StateHistory to DeltaHistory
hollandjg 88425f2
feat: add a new state with history which stores the full state object
hollandjg 1893502
feat: update history to be a recursive thing which uses the parents o…
hollandjg 807884d
feat: add a new stub with a linear history
hollandjg 9cefaa9
feat: include the initial version of the state in the history
hollandjg 03245cf
test: update doctests
hollandjg 35ea519
test: update doctests
hollandjg 09516c0
feat: add a new function as_of_last
hollandjg 2ea7129
test: update doctests
hollandjg b23ccdc
refactor: move post_init up to the top of the declarations
hollandjg b18acc2
refactor: move methods into separate functions
hollandjg 84960c1
refactor: update state_history_delta to use the new functions
hollandjg 48f7d9b
chore: delete state_history_linear
hollandjg d44b09d
chore: delete state_history_recursive
hollandjg 8a61167
refactor: move history functions out of the object and split into a f…
hollandjg 9441b6a
test: fix broken test
hollandjg 7288f22
test: fix broken test
hollandjg 65d5d69
docs: add documentation and testing for history_where
hollandjg 910b2a9
docs: add documentation and testing for history_contains
hollandjg 36cb45d
docs: update docstring of history_of
hollandjg 1f6ba34
chore: delete unused history_filter
hollandjg 62c00a5
chore: update type argument
hollandjg a6281b0
docs: update docs for filter_to_last
hollandjg 8560389
refactor: update history_up_to_last
hollandjg 6a15611
refactor: dont' throw an error infilter_to_last if index isn't found
hollandjg 96653d4
refactor: use mapping rather than mutablemapping
hollandjg aaffc08
refactor: update history function
hollandjg 519a5d4
feat: add new history_of_key_where function
hollandjg 17efd9e
feat: add a new function which can aggregate histories
hollandjg 35fd326
Revert "feat: add a new function which can aggregate histories"
hollandjg 16b06bb
docs: document some recipes for handling histories
hollandjg 7997f15
feat: add a new History class
hollandjg d2e83dd
refactor: make helper functions private and updated examples
hollandjg 225d691
refactor: use new HistoryObject in DeltaHistory
hollandjg a8167e8
docs: add type hints
hollandjg 40fdd99
docs: simplify doctests
hollandjg 69d6143
docs: simplify doctests
hollandjg 59d00a9
docs: simplify doctests
hollandjg f30170d
docs: simplify queries using history object
hollandjg 1e79b7b
refactor: remove unneeded special function
hollandjg 8a1d78f
refactor: rename shadowed function
hollandjg 785a91f
feat: add arbitrary history filter to last function
hollandjg c5ade53
refactor: simplify code to get values in condition
hollandjg 79b1713
test: update error string
hollandjg 8fd9b52
refactor: add reconstruct method on the history
hollandjg 8638190
refactor: make the _history_up_to_last also accept single field names…
hollandjg b54f1b3
docs: update documentation for AlternateDeltaHistory using the new hi…
hollandjg 9b5079e
test: fix doctest for python 3.9 and lower
hollandjg cfb3275
feat: add starting point of delta_to_state_transformed
hollandjg c738808
test: update formatting of tests
hollandjg faf554b
docs: add starting notebook for States with History tutorial.
hollandjg 44ef8fc
refactor: move dataclass history to a new file
hollandjg d6f1c36
feat: add history state based on UserList
hollandjg 003f2ad
type: fix type for add function
hollandjg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,364 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3a5b711036c1580c", | ||
"metadata": {}, | ||
"source": [ | ||
"# State Objects with History" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4f3b8109e7e35d3a", | ||
"metadata": {}, | ||
"source": [ | ||
"Some state objects use a history, rather than the fields direct in the state, to record changes in an experiment's \n", | ||
"data over time." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "initial_id", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"AlternateDeltaHistory(history=[...], variables=None, conditions=None, experiment_data=None, model=None)" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import random\n", | ||
"from pprint import pp\n", | ||
"\n", | ||
"from autora.state import Delta\n", | ||
"from autora.state_history_delta_alternative import AlternateDeltaHistory\n", | ||
"\n", | ||
"AlternateDeltaHistory()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f6a614d39f993a06", | ||
"metadata": {}, | ||
"source": [ | ||
"We create an empty state and add initial data using Deltas. Each time this state is updated, the values in the fields `variables`, `conditions` etc. are replaced:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1af8d93494c5256d", | ||
"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>5</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>6</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>7</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td>8</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>4</th>\n", | ||
" <td>9</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" x\n", | ||
"0 5\n", | ||
"1 6\n", | ||
"2 7\n", | ||
"3 8\n", | ||
"4 9" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"\n", | ||
"s = AlternateDeltaHistory() + Delta(conditions=dict(x=range(5))) + Delta(conditions=dict(x=range(5, 10)))\n", | ||
"s.conditions" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3aed485655696cc8", | ||
"metadata": {}, | ||
"source": [ | ||
"But the history keeps a record of the changes: the initial state (emtpy), and then the two Deltas:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b4f746a8af05c195", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"History([AlternateDeltaHistory(history=[...], variables=None, conditions=None, experiment_data=None, model=None),\n", | ||
" {'conditions': {'x': range(0, 5)}},\n", | ||
" {'conditions': {'x': range(5, 10)}}])" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"s.history" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bef0879f430ea121", | ||
"metadata": {}, | ||
"source": [ | ||
"We can reconstruct the state at any point by slicing the history and using the `.reconstruct` method, here after the \n", | ||
"first Delta but before the second:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "cc7f9c6532f28f8d", | ||
"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>0</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>1</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>2</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td>3</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>4</th>\n", | ||
" <td>4</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" x\n", | ||
"0 0\n", | ||
"1 1\n", | ||
"2 2\n", | ||
"3 3\n", | ||
"4 4" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"s.history[:2].reconstruct().conditions" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e72803574116a22f", | ||
"metadata": {}, | ||
"source": [ | ||
"By adding additional metadata to the Deltas, we can make it easier to find particular states. This might be useful in\n", | ||
" a complex AutoRA cycle where different steps need different versions of the same data. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9b6d6e76dad8beff", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def apply_transformation_to_input_state(function):\n", | ||
" \"\"\"Decorator which applies a transformation to the input state\"\"\"\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1d187204c2e7a82a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: []\n", | ||
"[AlternateDeltaHistory(history=[...], variables=None, conditions=Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: [], experiment_data=None, model=None), {'conditions': {'x': [0]}}]\n", | ||
" x\n", | ||
"0 0\n", | ||
"[AlternateDeltaHistory(history=[...], variables=None, conditions=Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: [], experiment_data=None, model=None), {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}]\n", | ||
" x\n", | ||
"0 1\n", | ||
"[AlternateDeltaHistory(history=[...], variables=None, conditions=Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: [], experiment_data=None, model=None), {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}, {'conditions': {'x': [0]}}]\n", | ||
" x\n", | ||
"0 0\n", | ||
"[AlternateDeltaHistory(history=[...], variables=None, conditions=Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: [], experiment_data=None, model=None), {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}, {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}]\n", | ||
" x\n", | ||
"0 1\n", | ||
"[AlternateDeltaHistory(history=[...], variables=None, conditions=Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: [], experiment_data=None, model=None), {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}, {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}, {'conditions': {'x': [0]}}]\n", | ||
" x\n", | ||
"0 0\n", | ||
"[AlternateDeltaHistory(history=[...], variables=None, conditions=Empty DataFrame\n", | ||
"Columns: [x]\n", | ||
"Index: [], experiment_data=None, model=None), {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}, {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}, {'conditions': {'x': [0]}}, {'conditions': {'x': [1]}}]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from autora.state_history_delta import delta_to_state_transformed\n", | ||
"from autora.state import inputs_from_state\n", | ||
"\n", | ||
"t = AlternateDeltaHistory(conditions=pd.DataFrame({\"x\": []}))\n", | ||
"\n", | ||
"# TODO: At this step we need a function which we can use to concatenate all of the \n", | ||
"# TODO: raw condition data but return them as part of the state. We don't have the \n", | ||
"# TODO: necessary helper functions on the History yet.\n", | ||
"@delta_to_state_transformed(lambda s: s.where(meta=\"raw\")) \n", | ||
"@inputs_from_state\n", | ||
"def experimentalist(conditions):\n", | ||
" possible_conditions = set(range(10))\n", | ||
" print(conditions)\n", | ||
" already_seen_conditions = set(conditions[\"x\"]) \n", | ||
" allowed_conditions = possible_conditions - already_seen_conditions\n", | ||
" conditions_out = min(allowed_conditions)\n", | ||
" return Delta(conditions=dict(x=[conditions_out]))\n", | ||
"\n", | ||
"for i in range(6):\n", | ||
" t = experimentalist(t)\n", | ||
" print(t.history)\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e42f5f53024068ff", | ||
"metadata": {}, | ||
"outputs": [], | ||
"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" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are changes to the basic state adding so that we don't have to emit a warning if there's a field missing in the Delta, which may often be the case for metadata fields. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Starting notebook on examples.