From 1a1c897994148b25b15a2fc7c1601e31cb52dc5f Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Fri, 25 Aug 2023 17:00:25 +0200 Subject: [PATCH] refactor: rename estimator_on_state from state_fn_from_estimator --- ...Introduction to Functions and States.ipynb | 4 +- ...Workflows using Functions and States.ipynb | 6 +-- src/autora/state.py | 50 +------------------ 3 files changed, 7 insertions(+), 53 deletions(-) diff --git a/docs/cycle/Basic Introduction to Functions and States.ipynb b/docs/cycle/Basic Introduction to Functions and States.ipynb index ca2b77f1..a41bf38a 100644 --- a/docs/cycle/Basic Introduction to Functions and States.ipynb +++ b/docs/cycle/Basic Introduction to Functions and States.ipynb @@ -274,9 +274,9 @@ "outputs": [], "source": [ "from sklearn.linear_model import LinearRegression\n", - "from autora.state import state_fn_from_estimator\n", + "from autora.state import estimator_on_state\n", "\n", - "theorist = state_fn_from_estimator(LinearRegression(fit_intercept=True))" + "theorist = estimator_on_state(LinearRegression(fit_intercept=True))" ] }, { diff --git a/docs/cycle/Linear and Cyclical Workflows using Functions and States.ipynb b/docs/cycle/Linear and Cyclical Workflows using Functions and States.ipynb index 808818d1..f04ee104 100644 --- a/docs/cycle/Linear and Cyclical Workflows using Functions and States.ipynb +++ b/docs/cycle/Linear and Cyclical Workflows using Functions and States.ipynb @@ -268,7 +268,7 @@ "### Defining The Theorist\n", "\n", "Now we define a theorist, which does a linear regression on the polynomial of degree 5. We define a regressor and a\n", - "method to return its feature names and coefficients, and then the theorist to handle it. Here, we use a different wrapper `state_fn_from_estimator` that wraps the regressor and returns a function with the same functionality, but operating on `State` fields. In this case, we want to use the `State` field `experiment_data` and extend the `State` field `models`." + "method to return its feature names and coefficients, and then the theorist to handle it. Here, we use a different wrapper `estimator_on_state` that wraps the regressor and returns a function with the same functionality, but operating on `State` fields. In this case, we want to use the `State` field `experiment_data` and extend the `State` field `models`." ] }, { @@ -278,13 +278,13 @@ "outputs": [], "source": [ "from sklearn.linear_model import LinearRegression\n", - "from autora.state import state_fn_from_estimator\n", + "from autora.state import estimator_on_state\n", "from sklearn.pipeline import make_pipeline as make_theorist_pipeline\n", "from sklearn.preprocessing import PolynomialFeatures\n", "\n", "# Completely standard scikit-learn pipeline regressor\n", "regressor = make_theorist_pipeline(PolynomialFeatures(degree=5), LinearRegression())\n", - "theorist = state_fn_from_estimator(regressor)\n", + "theorist = estimator_on_state(regressor)\n", "\n", "def get_equation(r):\n", " t = r.named_steps['polynomialfeatures'].get_feature_names_out()\n", diff --git a/src/autora/state.py b/src/autora/state.py index 65d6c18b..c50cc566 100644 --- a/src/autora/state.py +++ b/src/autora/state.py @@ -1277,7 +1277,7 @@ def model(self): XY = TypeVar("XY") -def state_fn_from_estimator(estimator: BaseEstimator) -> StateFunction: +def estimator_on_state(estimator: BaseEstimator) -> StateFunction: """ Convert a scikit-learn compatible estimator into a function on a `State` object. @@ -1287,7 +1287,7 @@ def state_fn_from_estimator(estimator: BaseEstimator) -> StateFunction: Examples: Initialize a function which operates on the state, `state_fn` and runs a LinearRegression. >>> from sklearn.linear_model import LinearRegression - >>> state_fn = state_fn_from_estimator(LinearRegression()) + >>> state_fn = estimator_on_state(LinearRegression()) Define the state on which to operate (here an instance of the `StandardState`): >>> from autora.state import StandardState @@ -1319,52 +1319,6 @@ def theorist( return theorist -def state_fn_from_x_to_y_fn_df(f: Callable[[X], Y]) -> StateFunction: - """Wrapper for experiment_runner of the form $f(x) \rarrow y$, where `f` returns just the $y$ - values, with inputs and outputs as a DataFrame or Series with correct column names. - - Examples: - The conditions are some x-values in a StandardState object: - >>> from autora.state import StandardState - >>> s = StandardState(conditions=pd.DataFrame({"x": [1, 2, 3]})) - - The function can be defined on a DataFrame (allowing the explicit inclusion of - metadata like column names). - >>> def x_to_y_fn(c: pd.DataFrame) -> pd.Series: - ... result = pd.Series(2 * c["x"] + 1, name="y") - ... return result - - We apply the wrapped function to `s` and look at the returned experiment_data: - >>> state_fn_from_x_to_y_fn_df(x_to_y_fn)(s).experiment_data - x y - 0 1 3 - 1 2 5 - 2 3 7 - - We can also define functions of several variables: - >>> def xs_to_y_fn(c: pd.DataFrame) -> pd.Series: - ... result = pd.Series(c["x0"] + c["x1"], name="y") - ... return result - - With the relevant variables as conditions: - >>> t = StandardState(conditions=pd.DataFrame({"x0": [1, 2, 3], "x1": [10, 20, 30]})) - >>> state_fn_from_x_to_y_fn_df(xs_to_y_fn)(t).experiment_data - x0 x1 y - 0 1 10 11 - 1 2 20 22 - 2 3 30 33 - """ - - @on_state() - def experiment_runner(conditions: pd.DataFrame, **kwargs): - x = conditions - y = f(x, **kwargs) - experiment_data = pd.DataFrame.merge(x, y, left_index=True, right_index=True) - return Delta(experiment_data=experiment_data) - - return experiment_runner - - def state_fn_from_x_to_xy_fn_df(f: Callable[[X], XY]) -> StateFunction: """Wrapper for experiment_runner of the form $f(x) \rarrow (x,y)$, where `f` returns both $x$ and $y$ values in a complete dataframe.