Skip to content

Commit

Permalink
fit_line() can output LI and HI (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
wleoncio committed Jan 16, 2025
1 parent 42bb05b commit 5d655f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/pCRscore/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def combine_fractions_shap(data_norm, shap):


# Function to fit a line to SHAP vs Fraction for each cell type
def fit_line(data):
def fit_line(data, split_ci=False):
result = []
grouped = data.groupby('Feature')
for name, group in grouped:
Expand All @@ -39,7 +39,14 @@ def fit_line(data):
model = statsmodels.api.OLS(y, X).fit()
coef = model.params['Fraction']
ci = model.conf_int(alpha=0.001).loc['Fraction']
result.append({'Feature': name, 'Coef': coef, 'CI': ci[0] * ci[1]})
if split_ci:
result.append(
{'Feature': name, 'Coef': coef, 'LI': ci[0], 'HI': ci[1]}
)
else:
result.append(
{'Feature': name, 'Coef': coef, 'CI': ci[0] * ci[1]}
)
return pandas.DataFrame(result)


Expand Down
23 changes: 23 additions & 0 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,26 @@ def test_combine_discovery_validation():
assert 'SHAP value' in all_pat.columns
if local:
all_pat.shape == (15888, 3)


# A new fitting is performed on discovery and validation data
fit = pipeline.fit_line(all_pat, split_ci=True)


def test_fit():
assert fit.shape[0] == len(all_pat['Feature'].unique())
assert 'Feature' in fit.columns
assert 'Coef' in fit.columns
assert 'LI' in fit.columns
assert 'HI' in fit.columns
if local:
line = fit['Feature'] == 'CAFs.myCAF.like'
assert math.isclose(
fit.loc[line, 'Coef'].values[0], -0.028020, rel_tol=1e-2
)
assert math.isclose(
fit.loc[line, 'LI'].values[0], -0.041878, rel_tol=1e-2
)
assert math.isclose(
fit.loc[line, 'HI'].values[0], -0.014161, rel_tol=1e-2
)

0 comments on commit 5d655f7

Please sign in to comment.