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

Enhancement/issue 822 #825

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Enhancement/issue 822 #825

wants to merge 8 commits into from

Conversation

aGuyLearning
Copy link
Collaborator

@aGuyLearning aGuyLearning commented Nov 13, 2024

Description of changes
closes #822

Changed the method signature of univariate models to allow the user to pass parameters to the lifeline. Now weilbull, neil_aalen and kmf look like

adata: AnnData, duration_col: str, event_col: str, **kwargs

and are passed on to the _univariate_model function

Technical details

this could be changed to explicitly have all parameters in each function and pass them on.

Additional context

image

@eroell eroell self-requested a review November 13, 2024 14:12
@eroell
Copy link
Collaborator

eroell commented Nov 13, 2024

Thanks a lot @aGuyLearning, awesome! Comments as per review :)

model = model_class()
model.fit(T, event_observed=E)
model.fit(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add fit_left_censoring call upon argument input left

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if nelson-aalen gracefully crashes with meaningful error message

ehrapy/tools/_sa.py Show resolved Hide resolved
ehrapy/tools/_sa.py Outdated Show resolved Hide resolved
ehrapy/tools/_sa.py Show resolved Hide resolved
ehrapy/tools/_sa.py Show resolved Hide resolved
@@ -116,15 +116,10 @@ def glm(


def kmf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one additional thing here; we'll also have to watch out for backwards compatibility.
Here, we could while still accepting the arguments raise a deprecation warning, where we briefly highlight the new behaviour.

- removed kwargs
- updated documentation
@eroell
Copy link
Collaborator

eroell commented Nov 20, 2024

On a sidenote: having a descriptive PR title is better here - the titles end up in the changelog, forgot to mention

@eroell
Copy link
Collaborator

eroell commented Nov 20, 2024

imblearn maintenance for sklearn usage (failing test) which @aGuyLearning noted is on the way btw

@eroell eroell self-requested a review November 20, 2024 17:33
@@ -116,15 +117,19 @@ def glm(


def kmf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small trap here; None of the current arguments are keyword only, but could be called in the past with positionals.
If we make this API change of kmf(adata, duration_col, ….) , and not have all of the arguments being keyword only, then the previous style of calling kmf
`ep.tl.kmf(adata[:, ["mort_day_censored"]].X, adata[:, ["censor_flg”]].X)
will fail.

Instead of complex branching, I’m in favor of

  1. keeping kmf as deprecated function in a legacy module, working as it did in the past.
    2.creating a new function kaplan_meier which has a) a name harmonized with nelson_aalen, weibull b) the same API structure c) is being recommended from the deprecation warning in 1.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, interested in your take @Zethson. Does this seem like a reasonable option for you?
I think its the cleanest way of improving the API into the future

@@ -97,7 +97,7 @@ def _sa_func_test(self, sa_function, sa_class, mimic_2_sa):

def test_kmf(self, mimic_2_sa):
adata, _, _ = mimic_2_sa
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'd make this change with the legacy module, could

  1. keep this test, and additionally check for the correct raise of a deprecation warning
  2. add a test for the new function kaplan_meyer

ci_labels=ci_labels,
weights=weights,
kmf = KaplanMeierFitter()
if censoring == "None" or "right":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branching is not required anymore, but happens in _univariate_model if I understand correctly? might overlook something here though!

@@ -386,12 +434,38 @@ def _univariate_model(adata: AnnData, duration_col: str, event_col: str, model_c
E = df[event_col]

model = model_class()
model.fit(T, event_observed=E)
function_name = "fit" if censoring == "right" else "fit_left_censoring"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should raise a ValueError here if its neither left nor right; as we don't pass the argument into a downstream function which would fail gracefully, it should fail here (else a user who e.g. enters censoring="rigth" gets accidentally an unexpected behaviour)

adata: AnnData,
duration_col: str,
event_col: str,
timeline: list[float] | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In favor of taking this chance to make everything after event_col a keyword-only argument

Suggested change
timeline: list[float] | None = None,
*,
timeline: list[float] | None = None,

def nelson_aalen(
adata: AnnData,
duration_col: str,
event_col: str,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think event_col is optional also for NelsonAalen's fit right

Suggested change
event_col: str,
event_col: str | None = None,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make Survival Analysis Estimators consistent, and pass all required arguments
2 participants