Skip to content
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 convenience properties iv_names, dv_names, X, and y #84

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/autora/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,100 @@ class StandardState(State):
metadata={"delta": "extend"},
)

@property
def iv_names(self) -> List[str]:
"""
Returns the names of the independent variables
Examples:
>>> s_empty = StandardState()
>>> s_empty.iv_names
[]
>>> from autora.variable import VariableCollection, Variable
>>> iv_1 = Variable('variable_1')
>>> iv_2 = Variable('variable_2')
>>> variables = VariableCollection(independent_variables=[iv_1, iv_2])
>>> s_variables = StandardState(variables=variables)
>>> s_variables.iv_names
['variable_1', 'variable_2']
"""
if not self.variables or not self.variables.independent_variables:
return []
return [iv.name for iv in self.variables.independent_variables]

@property
def dv_names(self) -> List[str]:
"""
Returns the names of the independent variables
Examples:
>>> s_empty = StandardState()
>>> s_empty.dv_names
[]
>>> from autora.variable import VariableCollection, Variable
>>> x = Variable('x')
>>> y = Variable('y')
>>> variables = VariableCollection(independent_variables=[x], dependent_variables=[y])
>>> s_variables = StandardState(variables=variables)
>>> s_variables.dv_names
['y']
"""
if not self.variables or not self.variables.dependent_variables:
return []
return [dv.name for dv in self.variables.dependent_variables]

@property
def X(self) -> pd.DataFrame:
"""
Returns the already observed conditions as a pd.DataFrame
Examples:
>>> s_empty = StandardState()
>>> s_empty.X
Empty DataFrame
Columns: []
Index: []
>>> from autora.variable import VariableCollection, Variable
>>> x = Variable('x')
>>> y = Variable('y')
>>> variables = VariableCollection(independent_variables=[x], dependent_variables=[y])
>>> experiment_data = pd.DataFrame({'x': [0, 1, 2, 3], 'y': [0, 2, 4, 6]})
>>> s = StandardState(variables=variables, experiment_data=experiment_data)
>>> s.X
x
0 0
1 1
2 2
3 3
"""
if self.experiment_data is None:
return pd.DataFrame()
return self.experiment_data[self.iv_names]

@property
def y(self) -> pd.DataFrame:
"""
Returns the observations as a pd.DataFrame
Examples:
>>> s_empty = StandardState()
>>> s_empty.y
Empty DataFrame
Columns: []
Index: []
>>> from autora.variable import VariableCollection, Variable
>>> x = Variable('x')
>>> y = Variable('y')
>>> variables = VariableCollection(independent_variables=[x], dependent_variables=[y])
>>> experiment_data = pd.DataFrame({'x': [0, 1, 2, 3], 'y': [0, 2, 4, 6]})
>>> s = StandardState(variables=variables, experiment_data=experiment_data)
>>> s.y
y
0 0
1 2
2 4
3 6
"""
if self.experiment_data is None:
return pd.DataFrame()
return self.experiment_data[self.dv_names]


X = TypeVar("X")
Y = TypeVar("Y")
Expand Down
Loading