Exemplo n.º 1
0
        def func(params):
            model = CoxTimeVaryingFitter(**params, **kwargs)
            model.fit(
                train_data[[META["id"], META["event"]] + ["start", "stop"] +
                           META["static"] + META["dynamic"]],
                id_col=META["id"],
                event_col=META["event"],
                start_col="start",
                stop_col="stop",
            )
            metric: List[float] = []
            for dataset in tune_data:
                predt = model.predict_partial_hazard(dataset)
                c0 = interpolate_at_times(model.baseline_cumulative_hazard_,
                                          dataset["stop"].values)
                metric.append(
                    roc_auc_score(
                        y_true=dataset[META["event"]],
                        y_score=1 - np.exp(-(c0 * predt.values)),
                    ))
            metric = -np.average(np.array(metric))

            if metric < self.metric_:
                self.metric_ = metric
                self.best_.update(params)
                self.logger.info(
                    f"New best metric value of {self.metric_} with \n\n{pformat(self.best_)}\n"
                )

            return {
                "loss": metric,
                "status": STATUS_OK,
            }
Exemplo n.º 2
0
    def _run_lifelines(model: CoxTimeVaryingFitter,
                       data: pd.DataFrame) -> pd.DataFrame:
        """Retrieve the win probability.

        Parameters
        ----------
        model : CoxTimeVaryingFitter
            The fitted model.
        data : pd.DataFrame
            The input dataset to be evaluated.

        Returns
        -------
        pd.DataFrame
            The updated dataset.
        """
        # Get the cumulative hazard -- copying from ``lifelines.fitters.SemiParametericPHFitter``
        vals = model.predict_partial_hazard(data[META["static"] +
                                                 META["dynamic"]])
        c0 = interpolate_at_times(model.baseline_cumulative_hazard_,
                                  data["stop"].values)
        # Survival is the negative exponent of the cumulative hazard
        new = data.copy()
        new[META["survival"]] = 1 - np.exp(-(c0 * vals.values))

        return new
Exemplo n.º 3
0
    def run(  # type: ignore
            self, model: CoxTimeVaryingFitter, data: pd.DataFrame,
            **kwargs) -> CoxTimeVaryingFitter:
        """Fit the lifelines model.

        Parameters
        ----------
        model : CoxTimeVaryingFitter
            The initialized model.
        data : pd.DataFrame
            The ``lifelines`` format data.
        **kwargs
            Keyword arguments for the ``fit`` method.

        Returns
        -------
        CoxTimeVaryingFitter
            The fitted model.
        """
        model = model.fit(
            data[[META["id"], META["event"]] + ["start", "stop"] +
                 META["static"] + META["dynamic"]],
            id_col=META["id"],
            event_col=META["event"],
            start_col="start",
            stop_col="stop",
            **kwargs)

        return model
Exemplo n.º 4
0
 def test_coxtv_plotting(self, block):
     df = load_stanford_heart_transplants()
     ctv = CoxTimeVaryingFitter()
     ctv.fit(df, id_col="id", event_col="event")
     ctv.plot(fmt="o")
     self.plt.title("test_coxtv_plotting")
     self.plt.show(block=block)
Exemplo n.º 5
0
 def test_coxtv_plotting_with_subset_of_columns(self, block):
     df = load_stanford_heart_transplants()
     ctv = CoxTimeVaryingFitter()
     ctv.fit(df, id_col="id", event_col="event")
     ctv.plot(columns=["age", "year"])
     self.plt.title("test_coxtv_plotting_with_subset_of_columns")
     self.plt.show(block=block)
def unit_timevarying_cox(df, features=[]):
    factors = ['user_id', 'start', 'stop', 'event'] + features
    df_ = df[factors]

    ctv = CoxTimeVaryingFitter()

    ctv.fit(df_, id_col='user_id', event_col='event', start_col='start', stop_col='stop', show_progress=True)
    ctv.print_summary(3)
Exemplo n.º 7
0
def test_predict_proba_lifelines(survivaldata):
    """Test prediction with a lifelines model."""
    train_data, test_data = survivaldata
    # Dropping null rows because of the weirdness with randomly generated data
    train_data = train_data.dropna()
    test_data = test_data.dropna()
    # Fit the model
    fitter = FitLifelinesModel()
    trained = fitter.run(model=CoxTimeVaryingFitter(), data=train_data)
    # Predict
    tsk = WinProbability()
    output = tsk.run(model=trained, data=test_data)

    assert output["WIN_PROB"].max() <= 1.0
    assert output["WIN_PROB"].min() >= 0.0
Exemplo n.º 8
0
    def run(self,
            params: Optional[Dict] = None
            ) -> CoxTimeVaryingFitter:  # type: ignore
        """Initialize a new ``lifelines`` model.

        Parameters
        ----------
        params : dict, optional (default None)
            Keyword arguments for ``CoxTimeVaryingFitter``

        Returns
        -------
        CoxTimeVaryingFitter
            The initialized model.
        """
        return CoxTimeVaryingFitter(**params or {})
Exemplo n.º 9
0
def test_load_model(tmpdir):
    """Test writing and reading a model."""
    model = CoxTimeVaryingFitter(penalizer=1.0)
    location = tmpdir.mkdir("fake-model")
    with open(Path(str(location), "mymodel.pkl"), "wb") as outfile:
        cloudpickle.dump(model, outfile)
    calibrator = IsotonicRegression(out_of_bounds="clip")
    with open(Path(str(location), "calibrator.pkl"), "wb") as outfile:
        cloudpickle.dump(calibrator, outfile)
    
    tsk = LoadModel()
    output = tsk.run(filepath=Path(str(location), "mymodel.pkl"))

    assert isinstance(output[0], CoxTimeVaryingFitter)
    assert output[0].penalizer == 1.0
    assert isinstance(output[1], IsotonicRegression)
    assert output[1].out_of_bounds == "clip"
Exemplo n.º 10
0
        event_observed=kmfdata["Observed"][wins2],
        label="2 - 3")
kmf.plot(ax=ax)
kmf.fit(kmfdata["Duration"][wins3],
        event_observed=kmfdata["Observed"][wins3],
        label="> 3")
kmf.plot(ax=ax)

plt.title("Survival separated by number of wins")
plt.ylim(0, 1)
plt.show()
"""
Cox Propotional Model using Age, Quickfire Wins, Wins, Highs and Lows to predict Out
"""
from lifelines import CoxTimeVaryingFitter
ctv = CoxTimeVaryingFitter()
ctv.fit(trainData[[
    "Age", "QuickfireWins", "Wins", "Highs", "Lows", "Start", "End", "ID",
    "Out"
]],
        id_col="ID",
        event_col="Out",
        start_col="Start",
        stop_col="End",
        show_progress=True)
ctv.print_summary()

#Get Hazards for specific episode
####EDIT THIS TO CHANGE WHICH SEASON/EPISODE YOU WANT TO APPLY THE MODEL TO#####
episode = 5
season = 13
Exemplo n.º 11
0
# -*- coding: utf-8 -*-
if __name__ == "__main__":
    import time
    import pandas as pd
    from lifelines import CoxTimeVaryingFitter
    from lifelines.datasets import load_rossi
    from lifelines.utils import to_long_format

    df = load_rossi()
    df = pd.concat([df] * 20)
    df = df.reset_index()
    df = to_long_format(df, duration_col="week")
    ctv = CoxTimeVaryingFitter()
    start_time = time.time()
    ctv.fit(df,
            id_col="index",
            event_col="arrest",
            start_col="start",
            stop_col="stop")
    time_took = time.time() - start_time
    print("--- %s seconds ---" % time_took)
    ctv.print_summary()
Exemplo n.º 12
0
# -*- coding: utf-8 -*-
if __name__ == "__main__":
    import time
    import pandas as pd
    from lifelines import CoxTimeVaryingFitter
    from lifelines.datasets import load_rossi
    from lifelines.utils import to_long_format

    df = load_rossi()
    df = pd.concat([df] * 20)
    df = df.reset_index()
    df = to_long_format(df, duration_col="week")
    ctv = CoxTimeVaryingFitter()
    start_time = time.time()
    ctv.fit(df,
            id_col="index",
            event_col="arrest",
            start_col="start",
            stop_col="stop",
            strata=["wexp", "prio"])
    time_took = time.time() - start_time
    print("--- %s seconds ---" % time_took)
    ctv.print_summary()