square / pysurvival

Open source package for Survival Analysis modeling
https://www.pysurvival.io/
Apache License 2.0
350 stars 106 forks source link

Can not import custom class depend on pysurvival #4

Closed kush99993s closed 4 years ago

kush99993s commented 5 years ago

Hello,

I have following class

from pysurvival.models.multi_task import NeuralMultiTaskModel

import joblib
import numpy as np

from ml_models.preprocessing.one_hot_encoding import PreProcessingWOneHot
from ml_models.templates.model import Model
from pysurvival.utils import save_model, load_model

from util import get_my_logger

class PySurvival(Model):
    def __init__(self):
        super().__init__()
        self.pre_processing = PreProcessingWOneHot()

    def build_survival_model(self, parm: dict, row_data: dict, target: np.array) -> (np.ndarray, np.ndarray, np.float64, np.float64, np.ndarray):
        """

        Args:
            row_data: list
            target: np.array
            parm: what parameters that I need to pass to models

        Returns:
            hold_out_y: target variable for hold out set
            predict_proba: predicted score on hold out set
            logloss: logloss on hold out
            auc: auc on hold out
            feature_score_array: feature score of features
        """
        structure = parm.pop('structure')
        self.model_instance = NeuralMultiTaskModel(bins=parm.pop('bins'), structure=structure)
        self.logger.info("building training data started")
        train_x, time, event = self.build_data_survival(row_data, target)
        self.logger.info("time is {0}".format(time[:10]))
        self.logger.info("time is {0}".format(event[:10]))
        self.logger.info("target is {0}".format(target[:10]))

        self.logger.info("final model building starting")
        self.model_instance.fit(train_x, time, event, **parm)

        hazard, density, survival = self.model_instance.predict(train_x)
        risk = self.model_instance.predict_risk(train_x)

        return {"time": time, "event": event, "hazard": hazard, "density": density, "survival": survival, "risk": risk}

In another file i am importing that class like following

from ml_models.templates.py_survival.py_survival_model import PySurvival

However, this doesn't work, it throws following error

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

However, it works if I import pysurvival first, before importing class, it works like following.

import pysurvival
from ml_models.templates.py_survival.py_survival_model import PySurvival

Do you know what is happening ? Any help is appreciated.

This is great package. Thank you for making open sources

steph-likes-git commented 5 years ago

Hey @kush99993s,

Thank you very for your interest in the package. I'm not really familiar with what you're trying to accomplish, but the error Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) makes me think this probably not related to pysurvival itself, but I can be wrong.

My advice would be to have import pysurvival before the very first line from pysurvival.models.multi_task import NeuralMultiTaskModel, so that the work-around that you find works when you run from ml_models.templates.py_survival.py_survival_model import PySurvival.

Let me know if I was able to help and if you consider the matter resolved.

PS: I'm very curious to know about the application you're building.

kush99993s commented 5 years ago

Thank you for update. It worked way you described.