# Path: sksurv/nonparametric.py
# def ipc_weights(event, time):
#     """Compute inverse probability of censoring weights
# 
#     Parameters
#     ----------
#     event : array, shape = (n_samples,)
#         Boolean event indicator.
# 
#     time : array, shape = (n_samples,)
#         Time when a subject experienced an event or was censored.
# 
#     Returns
#     -------
#     weights : array, shape = (n_samples,)
#         inverse probability of censoring weights
# 
#     See also
#     --------
#     CensoringDistributionEstimator
#         An estimator interface for estimating inverse probability
#         of censoring weights for unseen time points.
#     """
#     if event.all():
#         return numpy.ones(time.shape[0])
# 
#     unique_time, p = kaplan_meier_estimator(event, time, reverse=True)
# 
#     idx = numpy.searchsorted(unique_time, time[event])
#     Ghat = p[idx]
# 
#     assert (Ghat > 0).all()
# 
#     weights = numpy.zeros(time.shape[0])
#     weights[event] = 1.0 / Ghat
# 
#     return weights


from abc import ABCMeta
from sklearn.dummy import DummyRegressor
from sklearn.ensemble._gb_losses import RegressionLossFunction
from sklearn.utils.extmath import squared_norm
from ..nonparametric import ipc_weights
from ._coxph_loss import coxph_loss, coxph_negative_gradient
import numpy
        pred_time = y['time'] - raw_predictions.ravel()
        mask = (pred_time > 0) | y['event']
        ret = numpy.zeros(y['event'].shape[0])
        ret[mask] = pred_time.compress(mask, axis=0)
        return ret

    def update_terminal_regions(self, tree, X, y, residual, raw_predictions,
                                sample_weight, sample_mask,
                                learning_rate=0.1, k=0):
        """Least squares does not need to update terminal regions.

        But it has to update the predictions.
        """
        # update predictions
        raw_predictions[:, k] += learning_rate * tree.predict(X).ravel()

    def _update_terminal_region(self, tree, terminal_regions, leaf, X, y,
                                residual, raw_predictions, sample_weight):
        """Least squares does not need to update terminal regions"""

    def _scale_raw_prediction(self, raw_predictions):
        numpy.exp(raw_predictions, out=raw_predictions)
        return raw_predictions


class IPCWLeastSquaresError(SurvivalLossFunction):
    """Inverse probability of censoring weighted least squares error"""
    # pylint: disable=no-self-use

    def __call__(self, y, raw_predictions, sample_weight=None):

# Path: sksurv/nonparametric.py
# def ipc_weights(event, time):
#     """Compute inverse probability of censoring weights
# 
#     Parameters
#     ----------
#     event : array, shape = (n_samples,)
#         Boolean event indicator.
# 
#     time : array, shape = (n_samples,)
#         Time when a subject experienced an event or was censored.
# 
#     Returns
#     -------
#     weights : array, shape = (n_samples,)
#         inverse probability of censoring weights
# 
#     See also
#     --------
#     CensoringDistributionEstimator
#         An estimator interface for estimating inverse probability
#         of censoring weights for unseen time points.
#     """
#     if event.all():
#         return numpy.ones(time.shape[0])
# 
#     unique_time, p = kaplan_meier_estimator(event, time, reverse=True)
# 
#     idx = numpy.searchsorted(unique_time, time[event])
#     Ghat = p[idx]
# 
#     assert (Ghat > 0).all()
# 
#     weights = numpy.zeros(time.shape[0])
#     weights[event] = 1.0 / Ghat
# 
#     return weights


# Path: sksurv/ensemble/survival_loss.py
from abc import ABCMeta
from sklearn.dummy import DummyRegressor
from sklearn.ensemble._gb_losses import RegressionLossFunction
from sklearn.utils.extmath import squared_norm
from ..nonparametric import ipc_weights
from ._coxph_loss import coxph_loss, coxph_negative_gradient
import numpy

        pred_time = y['time'] - raw_predictions.ravel()
        mask = (pred_time > 0) | y['event']
        ret = numpy.zeros(y['event'].shape[0])
        ret[mask] = pred_time.compress(mask, axis=0)
        return ret

    def update_terminal_regions(self, tree, X, y, residual, raw_predictions,
                                sample_weight, sample_mask,
                                learning_rate=0.1, k=0):
        """Least squares does not need to update terminal regions.

        But it has to update the predictions.
        """
        # update predictions
        raw_predictions[:, k] += learning_rate * tree.predict(X).ravel()

    def _update_terminal_region(self, tree, terminal_regions, leaf, X, y,
                                residual, raw_predictions, sample_weight):
        """Least squares does not need to update terminal regions"""

    def _scale_raw_prediction(self, raw_predictions):
        numpy.exp(raw_predictions, out=raw_predictions)
        return raw_predictions


class IPCWLeastSquaresError(SurvivalLossFunction):
    """Inverse probability of censoring weighted least squares error"""
    # pylint: disable=no-self-use

    def __call__(self, y, raw_predictions, sample_weight=None):

        sample_weight = ipc_weights(y['event'], y['time'])