def test_set_model(self): """...Test set_model of saga, should only accept childs of ModelGeneralizedLinear""" # We try to pass a ModelCoxRegPartialLik which is not a generalized # linear model to SAGA to check that the error is raised msg = '^SAGA accepts only childs of `ModelGeneralizedLinear`$' with self.assertRaisesRegex(ValueError, msg): w = weights_sparse_gauss(n_weights=2, nnz=0) X, T, C = SimuCoxReg(w).simulate() model = ModelCoxRegPartialLik().fit(X, T, C) SAGA().set_model(model) msg = '^SAGA accepts only childs of `ModelGeneralizedLinear`$' with self.assertRaisesRegex(RuntimeError, msg): w = weights_sparse_gauss(n_weights=2, nnz=0) X, T, C = SimuCoxReg(w).simulate() model = ModelCoxRegPartialLik().fit(X, T, C) saga = SAGA() saga._solver.set_model(model._model)
def test_coxreg_serialize_and_compare(self): """Test serialization (cereal/pickle) of Cox Regression.""" np.random.seed(123) n_samples, n_features = 100, 5 w0 = np.random.randn(n_features) features, times, censoring = SimuCoxReg( w0, n_samples=n_samples, verbose=False, seed=1234).simulate() model = ModelCoxRegPartialLik() model.fit(features, times, censoring) pickled = pickle.loads(pickle.dumps(model)) self.assertTrue(model._model.compare(pickled._model))
def test_ModelCoxRegPartialLik(self): """...Numerical consistency check of loss and gradient for Cox Regression """ np.random.seed(123) n_samples, n_features = 100, 5 w0 = np.random.randn(n_features) features, times, censoring = SimuCoxReg( w0, n_samples=n_samples, verbose=False, seed=1234).simulate() model = ModelCoxRegPartialLik() model.fit(features, times, censoring) model_spars = ModelCoxRegPartialLik() model_spars.fit(csr_matrix(features), times, censoring) self.run_test_for_glm(model, model_spars)
def test_SimuCoxReg(self): """...Test simulation of a Cox Regression """ # Simulate a Cox model with specific seed n_samples = 10 n_features = 3 idx = np.arange(n_features) # Parameters of the Cox simu coeffs = np.exp(-idx / 10.) coeffs[::2] *= -1 seed = 123 simu = SimuCoxReg(coeffs, n_samples=n_samples, seed=seed, verbose=False) features_, times_, censoring_ = simu.simulate() times = np.array([ 1.5022119, 5.93102441, 6.82837051, 0.50940341, 0.14859682, 30.22922996, 3.54945974, 0.8671229, 1.4228358, 0.11483298 ]) censoring = np.array([1, 0, 1, 1, 1, 1, 1, 1, 0, 1], dtype=np.ushort) features = np.array([[1.4912667, 0.80881799, 0.26977298], [1.23227551, 0.50697013, 1.9409132], [1.8891494, 1.49834791, 2.41445794], [0.19431319, 0.80245126, 1.02577552], [-1.61687582, -1.08411865, -0.83438387], [2.30419894, -0.68987056, -0.39750262], [-0.28826405, -1.23635074, -0.76124386], [-1.32869473, -1.8752391, -0.182537], [0.79464218, 0.65055633, 1.57572506], [0.71524202, 1.66759831, 0.88679047]]) np.testing.assert_almost_equal(features, features_) np.testing.assert_almost_equal(times, times_) np.testing.assert_almost_equal(censoring, censoring_)
def get_train_data(n_features=10, n_samples=10000, nnz=3, seed=12): np.random.seed(seed) coeffs0 = weights_sparse_gauss(n_features, nnz=nnz) features, times, censoring = SimuCoxReg(coeffs0, verbose=False).simulate() return features, times, censoring
============================== Cox regression data simulation ============================== Generates Cox Regression realization given a weight vector """ import matplotlib.pyplot as plt import numpy as np from tick.survival import SimuCoxReg n_samples = 150 weights = np.array([0.3, 1.2]) seed = 123 simu_coxreg = SimuCoxReg(weights, n_samples=n_samples, seed=123, verbose=False) X, T, C = simu_coxreg.simulate() plt.figure(figsize=(6, 4)) plt.scatter(*X[C == 0].T, c=T[C == 0], cmap='RdBu', marker="x", label="censoring") plt.scatter(*X[C == 1].T, c=T[C == 1], cmap='RdBu', marker="o", label="failure") plt.colorbar()