Skip to content

Commit

Permalink
Merge pull request #5 from scverse/design-matrix-name
Browse files Browse the repository at this point in the history
Rename design to design_matrix
  • Loading branch information
grst authored Dec 1, 2024
2 parents 66cd05f + 5cfdf83 commit 9d62570
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning][].
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## v0.2.0

- Rename `FormulaicContrasts.design` to `FormulaicContrasts.design_matrix`

## v0.1.0

Initial release
19 changes: 6 additions & 13 deletions docs/model_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"\n",
"class StatsmodelsOLS(formulaic_contrasts.FormulaicContrasts):\n",
" def fit(self, variable: str):\n",
" self.mod = sm.OLS(self.data[variable], self.design)\n",
" self.mod = sm.OLS(self.data[variable], self.design_matrix)\n",
" self.mod = self.mod.fit()\n",
"\n",
" def t_test(self, contrast: np.ndarray):\n",
Expand Down Expand Up @@ -240,7 +240,7 @@
" self.contrast_builder = formulaic_contrasts.FormulaicContrasts(data, design)\n",
"\n",
" def fit(self, variable: str):\n",
" self.mod = sm.OLS(self.data[variable], self.contrast_builder.design)\n",
" self.mod = sm.OLS(self.data[variable], self.contrast_builder.design_matrix)\n",
" self.mod = self.mod.fit()\n",
"\n",
" def cond(self, **kwargs):\n",
Expand All @@ -262,7 +262,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -569,12 +569,12 @@
"data": {
"text/plain": [
"defaultdict(set,\n",
" {'biomarker': {'np.log(biomarker)'},\n",
" 'np.log': {'np.log(biomarker)'},\n",
" {'np.log': {'np.log(biomarker)'},\n",
" 'biomarker': {'np.log(biomarker)'},\n",
" 'C': {'C(response)',\n",
" \"C(treatment, contr.treatment(base='drugB'))\"},\n",
" 'contr.treatment': {\"C(treatment, contr.treatment(base='drugB'))\"},\n",
" 'treatment': {\"C(treatment, contr.treatment(base='drugB'))\"},\n",
" 'contr.treatment': {\"C(treatment, contr.treatment(base='drugB'))\"},\n",
" 'response': {'C(response)'}})"
]
},
Expand All @@ -586,13 +586,6 @@
"source": [
"variables_to_factors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
6 changes: 3 additions & 3 deletions src/formulaic_contrasts/_contrasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class FormulaicContrasts:
def __init__(self, data: pd.DataFrame, design: str):
self.factor_storage, self.variable_to_factors, materializer_class = get_factor_storage_and_materializer()
self.data = data
self.design = materializer_class(data, record_factor_metadata=True).get_model_matrix(design)
self.design_matrix = materializer_class(data, record_factor_metadata=True).get_model_matrix(design)

@property
def variables(self):
"""Get the names of the variables used in the model definition."""
try:
return self.design.model_spec.variables_by_source["data"]
return self.design_matrix.model_spec.variables_by_source["data"]
except AttributeError:
raise ValueError(
"Retrieving variables is only possible if the model was initialized using a formula."
Expand Down Expand Up @@ -63,7 +63,7 @@ def cond(self, **kwargs):
else:
cond_dict[var] = self._get_default_value(var)
df = pd.DataFrame([kwargs])
return self.design.model_spec.get_model_matrix(df).iloc[0]
return self.design_matrix.model_spec.get_model_matrix(df).iloc[0]

def contrast(self, column, baseline, group_to_compare):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_contrasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ def test_model_cond(test_dataframe, MockModel, formula, cond_kwargs, expected_co
else:
actual_contrast = mod.cond(**cond_kwargs)
assert actual_contrast.tolist() == expected_contrast
assert actual_contrast.index.tolist() == mod.design.columns.tolist()
assert actual_contrast.index.tolist() == mod.design_matrix.columns.tolist()

0 comments on commit 9d62570

Please sign in to comment.