havakv / pycox

Survival analysis with PyTorch
BSD 2-Clause "Simplified" License
781 stars 180 forks source link

error in Logistic Hazard Label transform #108

Open paulamartingonzalez opened 2 years ago

paulamartingonzalez commented 2 years ago

Hi! I have a very odd problem. I am trying to work with three different time-to-event variables and I am trying to discrete them. I am using this code based on the examples:

labtrans = LogisticHazard.label_transform(num_durations, scheme)
get_target = lambda df: (df[label_time].values, df[label].values)

y_train = labtrans.fit_transform(*get_target(clin_train))
y_val = labtrans.transform(*get_target(clin_val))
y_test = labtrans.transform(*get_target(clin_test))

and one of the labels works great and the other two give me the error:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-233-312396acdaf0> in <module>
      8     get_target = lambda df: (df[label_time].values, df[label].values)
      9 
---> 10     y_train = labtrans.fit_transform(*get_target(clin_train))
     11     y_val = labtrans.transform(*get_target(clin_val))
     12     y_test = labtrans.transform(*get_target(clin_test))

/home/martin09/anaconda3/lib/python3.7/site-packages/pycox/preprocessing/label_transforms.py in fit_transform(self, durations, events)
    125 
    126     def fit_transform(self, durations, events):
--> 127         self.fit(durations, events)
    128         idx_durations, events = self.transform(durations, events)
    129         return idx_durations, events

/home/martin09/anaconda3/lib/python3.7/site-packages/pycox/preprocessing/label_transforms.py in fit(self, durations, events)
    120                 self._dtype = np.dtype('float64')
    121         durations = durations.astype(self._dtype)
--> 122         self.cuts = make_cuts(self._cuts, self._scheme, durations, events, self._min, self._dtype)
    123         self.idu = IdxDiscUnknownC(self.cuts)
    124         return self

/home/martin09/anaconda3/lib/python3.7/site-packages/pycox/preprocessing/discretization.py in make_cuts(n_cuts, scheme, durations, events, min_, dtype)
      9         cuts = cuts_equidistant(durations.max(), n_cuts, min_, dtype)
     10     elif scheme == 'quantiles':
---> 11         cuts = cuts_quantiles(durations, events, n_cuts, min_, dtype)
     12     else:
     13         raise ValueError(f"Got invalid `scheme` {scheme}.")

/home/martin09/anaconda3/lib/python3.7/site-packages/pycox/preprocessing/discretization.py in cuts_quantiles(durations, events, num, min_, dtype)
     28     If min_ = None, we will use durations.min() for the first cut.
     29     """
---> 30     km = utils.kaplan_meier(durations, events)
     31     surv_est, surv_durations = km.values, km.index.values
     32     s_cuts = np.linspace(km.values.min(), km.values.max(), num)

/home/martin09/anaconda3/lib/python3.7/site-packages/pycox/utils.py in kaplan_meier(durations, events, start_duration)
     63     ni = np.zeros(len(surv_idx), dtype='int')
     64     di = np.zeros_like(ni)
---> 65     di, ni = _group_loop(n, surv_idx, durations, events, di, ni)
     66     ni = n - ni.cumsum()
     67     ni[1:] = ni[:-1]

/home/martin09/anaconda3/lib/python3.7/site-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws)
    413                 e.patch_message(msg)
    414 
--> 415             error_rewrite(e, 'typing')
    416         except errors.UnsupportedError as e:
    417             # Something unsupported is present in the user code, add help info

/home/martin09/anaconda3/lib/python3.7/site-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type)
    356                 raise e
    357             else:
--> 358                 reraise(type(e), e, None)
    359 
    360         argtypes = []

/home/martin09/anaconda3/lib/python3.7/site-packages/numba/core/utils.py in reraise(tp, value, tb)
     78         value = tp()
     79     if value.__traceback__ is not tb:
---> 80         raise value.with_traceback(tb)
     81     raise value
     82 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type array(pyobject, 1d, C)
During: typing of argument at /home/martin09/anaconda3/lib/python3.7/site-packages/pycox/utils.py (33)

File "../../../../home/martin09/anaconda3/lib/python3.7/site-packages/pycox/utils.py", line 33:
def _group_loop(n, surv_idx, durations, events, di, ni):
    idx = 0

Do you have any idea of what might be going on?

Thanks in advance!

havakv commented 2 years ago

Hmm, I'm not sure what's wrong here. The numba error are notoriously hard to understand. Could you post a minimal example that reproduce the error? Alternatively you could try running without the scheme variable to get an equidistant grid. That doesn't rely on any numba code

FayeqJeelani12 commented 1 year ago

I am facing a similar error. image Any help would be greatly appreciated.