<s> from __future__ import print_function <EOL> import numpy as np <EOL> import pandas as pd <EOL> import numpy . testing as npt <EOL> import pytest <EOL> from lifelines import statistics as stats <EOL> from lifelines . datasets import load_waltons , load_g3 <EOL> def test_sample_size_necessary_under_cph ( ) : <EOL> assert stats . sample_size_necessary_under_cph ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) == ( <NUM_LIT> , <NUM_LIT> ) <EOL> assert stats . sample_size_necessary_under_cph ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT> ) == ( <NUM_LIT> , <NUM_LIT> ) <EOL> assert stats . sample_size_necessary_under_cph ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT> ) == ( <NUM_LIT> , <NUM_LIT> ) <EOL> assert stats . sample_size_necessary_under_cph ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT> , alpha = <NUM_LIT> ) == ( <NUM_LIT> , <NUM_LIT> ) <EOL> def test_power_under_cph ( ) : <EOL> assert abs ( stats . power_under_cph ( <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) - <NUM_LIT> ) < <NUM_LIT> <EOL> assert abs ( stats . power_under_cph ( <NUM_LIT:12> , <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) - <NUM_LIT> ) < <NUM_LIT> <EOL> def test_unequal_intensity_with_random_data ( ) : <EOL> data1 = np . random . exponential ( <NUM_LIT:5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> data2 = np . random . exponential ( <NUM_LIT:1> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> test_result = stats . logrank_test ( data1 , data2 ) <EOL> assert test_result . is_significant <EOL> def test_logrank_test_output_against_R_1 ( ) : <EOL> df = load_g3 ( ) <EOL> ix = ( df [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> d1 , e1 = df . ix [ ix ] [ '<STR_LIT:time>' ] , df . ix [ ix ] [ '<STR_LIT>' ] <EOL> d2 , e2 = df . ix [ ~ ix ] [ '<STR_LIT:time>' ] , df . ix [ ~ ix ] [ '<STR_LIT>' ] <EOL> expected = <NUM_LIT> <EOL> result = stats . logrank_test ( d1 , d2 , event_observed_A = e1 , event_observed_B = e2 ) <EOL> assert abs ( result . p_value - expected ) < <NUM_LIT> <EOL> def test_logrank_test_output_against_R_2 ( ) : <EOL> control_T = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:11> , <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:12> , <NUM_LIT:15> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> control_E = np . ones_like ( control_T ) <EOL> treatment_T = [ <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:20> , <NUM_LIT> , <NUM_LIT:32> , <NUM_LIT:32> , <NUM_LIT> , <NUM_LIT> ] <EOL> treatment_E = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> result = stats . logrank_test ( control_T , treatment_T , event_observed_A = control_E , event_observed_B = treatment_E ) <EOL> expected_p_value = <NUM_LIT> <EOL> assert abs ( result . p_value - expected_p_value ) < <NUM_LIT> <EOL> assert abs ( result . test_statistic - <NUM_LIT> ) < <NUM_LIT:0.1> <EOL> def test_rank_test_output_against_R_no_censorship ( ) : <EOL> """<STR_LIT>""" <EOL> result = stats . multivariate_logrank_test ( [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:50> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> r_p_value = <NUM_LIT> <EOL> r_stat = <NUM_LIT> <EOL> assert abs ( result . p_value - r_p_value ) < <NUM_LIT> <EOL> assert abs ( result . test_statistic - r_stat ) < <NUM_LIT> <EOL> def test_rank_test_output_against_R_with_censorship ( ) : <EOL> """<STR_LIT>""" <EOL> result = stats . multivariate_logrank_test ( [ <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:50> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> r_p_value = <NUM_LIT> <EOL> r_stat = <NUM_LIT> <EOL> assert abs ( result . p_value - r_p_value ) < <NUM_LIT> <EOL> assert abs ( result . test_statistic - r_stat ) < <NUM_LIT> <EOL> def test_unequal_intensity_event_observed ( ) : <EOL> data1 = np . random . exponential ( <NUM_LIT:5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> data2 = np . random . exponential ( <NUM_LIT:1> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> eventA = np . random . binomial ( <NUM_LIT:1> , <NUM_LIT:0.5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> eventB = np . random . binomial ( <NUM_LIT:1> , <NUM_LIT:0.5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> result = stats . logrank_test ( data1 , data2 , event_observed_A = eventA , event_observed_B = eventB ) <EOL> assert result . is_significant <EOL> def test_integer_times_logrank_test ( ) : <EOL> data1 = np . random . exponential ( <NUM_LIT:5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) . astype ( int ) <EOL> data2 = np . random . exponential ( <NUM_LIT:1> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) . astype ( int ) <EOL> result = stats . logrank_test ( data1 , data2 ) <EOL> assert result . is_significant <EOL> def test_equal_intensity_with_negative_data ( ) : <EOL> data1 = np . random . normal ( <NUM_LIT:0> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> data1 -= data1 . mean ( ) <EOL> data1 /= data1 . std ( ) <EOL> data2 = np . random . normal ( <NUM_LIT:0> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> data2 -= data2 . mean ( ) <EOL> data2 /= data2 . std ( ) <EOL> result = stats . logrank_test ( data1 , data2 ) <EOL> assert not result . is_significant <EOL> def test_unequal_intensity_with_negative_data ( ) : <EOL> data1 = np . random . normal ( - <NUM_LIT:5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> data2 = np . random . normal ( <NUM_LIT:5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> result = stats . logrank_test ( data1 , data2 ) <EOL> assert result . is_significant <EOL> def test_waltons_dataset ( ) : <EOL> df = load_waltons ( ) <EOL> ix = df [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> waltonT1 = df . ix [ ix ] [ '<STR_LIT:T>' ] <EOL> waltonT2 = df . ix [ ~ ix ] [ '<STR_LIT:T>' ] <EOL> result = stats . logrank_test ( waltonT1 , waltonT2 ) <EOL> assert result . is_significant <EOL> def test_logrank_test_is_symmetric ( ) : <EOL> data1 = np . random . exponential ( <NUM_LIT:5> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) . astype ( int ) <EOL> data2 = np . random . exponential ( <NUM_LIT:1> , size = ( <NUM_LIT> , <NUM_LIT:1> ) ) . astype ( int ) <EOL> result1 = stats . logrank_test ( data1 , data2 ) <EOL> result2 = stats . logrank_test ( data2 , data1 ) <EOL> assert abs ( result1 . p_value - result2 . p_value ) < <NUM_LIT> <EOL> assert result2 . is_significant == result1 . is_significant <EOL> def test_multivariate_unequal_intensities ( ) : <EOL> T = np . random . exponential ( <NUM_LIT:10> , size = <NUM_LIT> ) <EOL> g = np . random . binomial ( <NUM_LIT:2> , <NUM_LIT:0.5> , size = <NUM_LIT> ) <EOL> T [ g == <NUM_LIT:1> ] = np . random . exponential ( <NUM_LIT:1> , size = ( g == <NUM_LIT:1> ) . sum ( ) ) <EOL> result = stats . multivariate_logrank_test ( T , g ) <EOL> assert result . is_significant <EOL> def test_pairwise_waltons_dataset_is_significantly_different ( ) : <EOL> waltons_dataset = load_waltons ( ) <EOL> R = stats . pairwise_logrank_test ( waltons_dataset [ '<STR_LIT:T>' ] , waltons_dataset [ '<STR_LIT>' ] ) <EOL> assert R . values [ <NUM_LIT:0> , <NUM_LIT:1> ] . is_significant <EOL> def test_pairwise_logrank_test_with_identical_data_returns_inconclusive ( ) : <EOL> t = np . random . exponential ( <NUM_LIT:10> , size = <NUM_LIT:100> ) <EOL> T = np . tile ( t , <NUM_LIT:3> ) <EOL> g = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) . repeat ( <NUM_LIT:100> ) <EOL> R = stats . pairwise_logrank_test ( T , g , alpha = <NUM_LIT> ) . applymap ( lambda r : r . is_significant if r is not None else None ) <EOL> V = np . array ( [ [ None , False , False ] , [ False , None , False ] , [ False , False , None ] ] ) <EOL> npt . assert_array_equal ( R . values , V ) <EOL> def test_pairwise_allows_dataframes ( ) : <EOL> N = <NUM_LIT:100> <EOL> df = pd . DataFrame ( np . empty ( ( N , <NUM_LIT:3> ) ) , columns = [ "<STR_LIT:T>" , "<STR_LIT:C>" , "<STR_LIT>" ] ) <EOL> df [ "<STR_LIT:T>" ] = np . random . exponential ( <NUM_LIT:1> , size = N ) <EOL> df [ "<STR_LIT:C>" ] = np . random . binomial ( <NUM_LIT:1> , <NUM_LIT> , size = N ) <EOL> df [ "<STR_LIT>" ] = np . random . binomial ( <NUM_LIT:2> , <NUM_LIT:0.5> , size = N ) <EOL> stats . pairwise_logrank_test ( df [ '<STR_LIT:T>' ] , df [ "<STR_LIT>" ] , event_observed = df [ "<STR_LIT:C>" ] ) <EOL> def test_log_rank_returns_None_if_equal_arrays ( ) : <EOL> T = np . random . exponential ( <NUM_LIT:5> , size = <NUM_LIT:200> ) <EOL> result = stats . logrank_test ( T , T , alpha = <NUM_LIT> ) <EOL> assert not result . is_significant <EOL> C = np . random . binomial ( <NUM_LIT:2> , <NUM_LIT> , size = <NUM_LIT:200> ) <EOL> result = stats . logrank_test ( T , T , C , C , alpha = <NUM_LIT> ) <EOL> assert not result . is_significant <EOL> def test_multivariate_log_rank_is_identital_to_log_rank_for_n_equals_2 ( ) : <EOL> N = <NUM_LIT:200> <EOL> T1 = np . random . exponential ( <NUM_LIT:5> , size = N ) <EOL> T2 = np . random . exponential ( <NUM_LIT:5> , size = N ) <EOL> C1 = np . random . binomial ( <NUM_LIT:2> , <NUM_LIT> , size = N ) <EOL> C2 = np . random . binomial ( <NUM_LIT:2> , <NUM_LIT> , size = N ) <EOL> result = stats . logrank_test ( T1 , T2 , C1 , C2 , alpha = <NUM_LIT> ) <EOL> T = np . r_ [ T1 , T2 ] <EOL> C = np . r_ [ C1 , C2 ] <EOL> G = np . array ( [ <NUM_LIT:1> ] * <NUM_LIT:200> + [ <NUM_LIT:2> ] * <NUM_LIT:200> ) <EOL> result_m = stats . multivariate_logrank_test ( T , G , C , alpha = <NUM_LIT> ) <EOL> assert result . is_significant == result_m . is_significant <EOL> assert result . p_value == result_m . p_value <EOL> def test_StatisticalResult_class ( ) : <EOL> sr = stats . StatisticalResult ( True , <NUM_LIT> , <NUM_LIT> ) <EOL> assert sr . is_significant <EOL> assert sr . test_result == "<STR_LIT>" <EOL> sr = stats . StatisticalResult ( None , <NUM_LIT> , <NUM_LIT> ) <EOL> assert not sr . is_significant <EOL> assert sr . test_result == "<STR_LIT>" <EOL> sr = stats . StatisticalResult ( True , <NUM_LIT> , <NUM_LIT> , kw = '<STR_LIT>' ) <EOL> assert hasattr ( sr , '<STR_LIT>' ) <EOL> assert getattr ( sr , '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert '<STR_LIT>' in sr . __unicode__ ( ) <EOL> def test_valueerror_is_raised_if_alpha_out_of_bounds ( ) : <EOL> data1 = np . random . exponential ( <NUM_LIT:5> , size = ( <NUM_LIT:20> , <NUM_LIT:1> ) ) <EOL> data2 = np . random . exponential ( <NUM_LIT:1> , size = ( <NUM_LIT:20> , <NUM_LIT:1> ) ) <EOL> with pytest . raises ( ValueError ) : <EOL> stats . logrank_test ( data1 , data2 , alpha = <NUM_LIT> ) </s>
