Skip to content

Commit

Permalink
remove patsy dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaChristina committed Oct 27, 2023
1 parent 9c23d03 commit d03a079
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
23 changes: 9 additions & 14 deletions ncem/tl/fit/backend/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pandas as pd
import patsy
from formulaic import Formula

from ncem.tl.fit.constants import PREFIX_INDEX, PREFIX_NEIGHBOR

Expand Down Expand Up @@ -248,11 +249,9 @@ def get_dmat_from_obs(obs: pd.DataFrame, obs_niche: pd.DataFrame, formula: str,
# Merge sample annotation:
obs_full = pd.concat([obs, obs_index_type, obs_niche], axis=1)
columns_names = formula.replace('~0+','').split('+')

dmat = patsy.dmatrix(formula, obs_full)
# Simplify names, this is necessary for patsy to accept these as terms later.
column_names = [f"{x.split('[')[0]}{x.split('[')[1].split(']')[-1]}" for x in dmat.design_info.column_names]
dmat = pd.DataFrame(np.asarray(dmat), index=obs.index, columns=column_names)

dmat = Formula(formula).get_model_matrix(obs_full)
dmat = pd.DataFrame(np.asarray(dmat), index=obs.index, columns=dmat.columns)
return dmat


Expand Down Expand Up @@ -298,13 +297,8 @@ def get_dmats_from_deconvoluted(
obs_index_type_x.index = obs.index
# Merge sample annotation:
obs_full = pd.concat([obs, obs_index_type_x, obs_niche], axis=1)
dmats[x] = patsy.dmatrix(formulas[x], obs_full)
# ensure that column names start with index type name
dmat_columns = [
col if col.startswith(PREFIX_INDEX) else PREFIX_INDEX + x + col for col in dmats[x].design_info.column_names
]
dmat_columns = [f"{x.split('[')[0]}{x.split('[')[1].split(']')[-1]}" for x in dmat_columns]
dmats[x] = pd.DataFrame(np.asarray(dmats[x]), index=obs.index, columns=dmat_columns)
dmats[x] = Formula(formulas[x]).get_model_matrix(obs_full)
dmats[x] = pd.DataFrame(np.asarray(dmats[x]), index=obs.index, columns=dmats[x].columns)
return dmats


Expand Down Expand Up @@ -353,7 +347,8 @@ def get_dmat_global_from_deconvoluted(obs: pd.DataFrame, deconv: pd.DataFrame, f
obs_index_type[x].index = obs.index
# Merge sample annotation:
obs_full = pd.concat([obs, obs_index_type_x, obs_niche], axis=1)
dmat_x = patsy.dmatrix(formula, obs_full)
dmats.append(pd.DataFrame(np.asarray(dmat_x), index=obs.index, columns=dmat_x.design_info.column_names))

dmat_x = Formula(formula).get_model_matrix(obs_full)
dmats.append(pd.DataFrame(np.asarray(dmat_x), index=obs.index, columns=dmat_x.columns))
dmat = pd.concat(dmats, axis=0)
return dmat
1 change: 0 additions & 1 deletion ncem/tl/fit/backend/linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ def linear_ncem_deconvoluted(
)
dmats = get_dmats_from_deconvoluted(deconv=adata.obsm[key_deconvolution], formulas=formulas, obs=adata.obs)
for k, v in dmats.items():
print(k)
dmat_key = f"{OBSM_KEY_DMAT}_{k}"
adata.obsm[dmat_key] = v
params = ols_fit(x_=adata.obsm[dmat_key].values, y_=adata.layers[k])
Expand Down
1 change: 1 addition & 0 deletions ncem/tl/fit/backend/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def test_deconvoluted(
for y in coef_to_test:
if y in parameter_names:
idx = parameter_names.index(y)
print(idx)
theta_mle = params.values[:, idx]
theta_sd = fisher_inv[:, idx, idx]
theta_sd = np.nextafter(0, np.inf, out=theta_sd, where=theta_sd < np.nextafter(0, np.inf))
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ python = ">=3.8,<=3.10"
click = "^8.0.0"
rich = "^10.1.0"
PyYAML = "^5.4.1"
Jinja2 = ">=2.11.3,<4.0.0"
Jinja2 = ">=2.11.3,<=4.0.0"
scanpy = "^1.9.3"
squidpy = "^1.2.3"
patsy = "^0.5.1"
formulaic = "=0.6.6"
scipy = "=1.9.1"
seaborn = "^0.12.2"
matplotlib = "^3.7.1"
Expand Down

0 comments on commit d03a079

Please sign in to comment.