def test_toy_create_difference_matrix_direct_neighbor_without_censoring( toy_data): _, y = toy_data status = numpy.ones(y.shape, dtype=bool) mat = create_difference_matrix(status.astype(numpy.uint8), y["time"], kind="next") expected = numpy.zeros((5, 6), dtype=numpy.int8) expected[0, 0] = -1 expected[0, 1] = 1 expected[1, 1] = -1 expected[1, 2] = 1 expected[2, 2] = -1 expected[2, 3] = 1 expected[3, 3] = -1 expected[3, 4] = 1 expected[4, 4] = -1 expected[4, 5] = 1 assert_array_equal(expected, mat.toarray()) # should return first-order differences of y["time"] actual_diff = mat.dot(y["time"]) expected_diff = (y["time"][1:] - y["time"][:-1]) assert_array_almost_equal(expected_diff, actual_diff)
def test_toy_create_difference_matrix_direct_neighbor_without_censoring_shuffled( toy_data): _, y = toy_data status = numpy.ones(y.shape, dtype=bool) order = [3, 2, 5, 0, 1, 4] # = [ 20. 11. 70. 3. 6. 37.] time = y["time"][order] mat = create_difference_matrix(status.astype(numpy.uint8), time, kind="next") expected = numpy.zeros((5, 6), dtype=numpy.int8) expected[0, 3] = -1 expected[0, 4] = 1 expected[1, 4] = -1 expected[1, 1] = 1 expected[2, 1] = -1 expected[2, 0] = 1 expected[3, 0] = -1 expected[3, 5] = 1 expected[4, 5] = -1 expected[4, 2] = 1 assert_array_equal(expected, mat.toarray()) # should return first-order differences of y["time"] actual_diff = mat.dot(time) expected_diff = (y["time"][1:] - y["time"][:-1]) assert_array_almost_equal(expected_diff, actual_diff)
def test_toy_create_difference_matrix_full_censored(toy_data): _, y = toy_data mat = create_difference_matrix(y["status"].astype(numpy.uint8), y["time"], kind="all") expected = numpy.zeros((11, 6), dtype=numpy.int8) expected[0, 1] = 1 expected[0, 0] = -1 expected[1:3, 2] = 1 expected[1, 0] = -1 expected[2, 1] = -1 expected[3:5, 3] = 1 expected[3, 0] = -1 expected[4, 1] = -1 expected[5:8, 4] = 1 expected[5, 0] = -1 expected[6, 1] = -1 expected[7, 3] = -1 expected[8:12, 5] = 1 expected[8, 0] = -1 expected[9, 1] = -1 expected[10, 3] = -1 assert_array_equal(expected, mat.toarray())
def test_toy_create_difference_matrix_full(toy_data): _, y = toy_data status = numpy.repeat(True, len(y)) mat = create_difference_matrix(status.astype(numpy.uint8), y["time"], kind="all") expected = numpy.zeros((15, 6), dtype=numpy.int8) expected[0, 1] = 1 expected[0, 0] = -1 expected[1:3, 2] = 1 expected[1, 0] = -1 expected[2, 1] = -1 expected[3:6, 3] = 1 expected[3, 0] = -1 expected[4, 1] = -1 expected[5, 2] = -1 expected[6:10, 4] = 1 expected[6, 0] = -1 expected[7, 1] = -1 expected[8, 2] = -1 expected[9, 3] = -1 expected[10:15, 5] = 1 expected[10, 0] = -1 expected[11, 1] = -1 expected[12, 2] = -1 expected[13, 3] = -1 expected[14, 4] = -1 assert_array_equal(expected, mat.toarray())
def test_toy_create_difference_matrix_nearest_neighbor_censored(self): mat = create_difference_matrix(self.y["status"].astype(numpy.uint8), self.y["time"], kind="nearest") expected = numpy.zeros((5, 6), dtype=numpy.int8) expected[0, 0] = -1 expected[0, 1] = 1 expected[1, 1] = -1 expected[1, 2] = 1 expected[2, 1] = -1 expected[2, 3] = 1 expected[3, 3] = -1 expected[3, 4] = 1 expected[4, 3] = -1 expected[4, 5] = 1 assert_array_equal(expected, mat.toarray())
def test_toy_create_difference_matrix_nearest_neighbor(self): status = numpy.repeat(True, len(self.y)) mat = create_difference_matrix(status.astype(numpy.uint8), self.y["time"], kind="nearest") expected = numpy.zeros((5, 6), dtype=numpy.int8) expected[0, 0] = -1 expected[0, 1] = 1 expected[1, 1] = -1 expected[1, 2] = 1 expected[2, 2] = -1 expected[2, 3] = 1 expected[3, 3] = -1 expected[3, 4] = 1 expected[4, 4] = -1 expected[4, 5] = 1 assert_array_equal(expected, mat.toarray())
def test_toy_create_difference_matrix_direct_neighbor_with_censoring(self): mat = create_difference_matrix(self.y["status"].astype(numpy.uint8), self.y["time"], kind="next") expected = numpy.zeros((3, 6), dtype=numpy.int8) expected[0, 0] = -1 expected[0, 1] = 1 expected[1, 1] = -1 expected[1, 2] = 1 expected[2, 3] = -1 expected[2, 4] = 1 assert_array_equal(expected, mat.toarray()) # should return first-order differences of self.y["time"] actual_diff = mat.dot(self.y["time"]) comparable_pairs = numpy.array([True, True, False, True, False]) expected_diff = (self.y["time"][1:] - self.y["time"][:-1])[comparable_pairs] assert_array_almost_equal(expected_diff, actual_diff)