4255 |
16 Dec 22 |
peter |
// $Id$ |
4255 |
16 Dec 22 |
peter |
2 |
|
4255 |
16 Dec 22 |
peter |
3 |
/* |
4255 |
16 Dec 22 |
peter |
Copyright (C) 2022 Peter Johansson |
4255 |
16 Dec 22 |
peter |
5 |
|
4255 |
16 Dec 22 |
peter |
This file is part of the yat library, https://dev.thep.lu.se/yat |
4255 |
16 Dec 22 |
peter |
7 |
|
4255 |
16 Dec 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4255 |
16 Dec 22 |
peter |
modify it under the terms of the GNU General Public License as |
4255 |
16 Dec 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4255 |
16 Dec 22 |
peter |
License, or (at your option) any later version. |
4255 |
16 Dec 22 |
peter |
12 |
|
4255 |
16 Dec 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4255 |
16 Dec 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4255 |
16 Dec 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4255 |
16 Dec 22 |
peter |
General Public License for more details. |
4255 |
16 Dec 22 |
peter |
17 |
|
4255 |
16 Dec 22 |
peter |
You should have received a copy of the GNU General Public License |
4255 |
16 Dec 22 |
peter |
along with yat. If not, see <https://www.gnu.org/licenses/>. |
4255 |
16 Dec 22 |
peter |
20 |
*/ |
4255 |
16 Dec 22 |
peter |
21 |
|
4255 |
16 Dec 22 |
peter |
22 |
#include <config.h> |
4255 |
16 Dec 22 |
peter |
23 |
|
4255 |
16 Dec 22 |
peter |
24 |
#include "Suite.h" |
4255 |
16 Dec 22 |
peter |
25 |
|
4255 |
16 Dec 22 |
peter |
26 |
#include <yat/statistics/KaplanMeier.h> |
4255 |
16 Dec 22 |
peter |
27 |
|
4255 |
16 Dec 22 |
peter |
28 |
#include <cassert> |
4255 |
16 Dec 22 |
peter |
29 |
|
4255 |
16 Dec 22 |
peter |
30 |
using namespace theplu; |
4255 |
16 Dec 22 |
peter |
31 |
using namespace yat; |
4255 |
16 Dec 22 |
peter |
32 |
using statistics::KaplanMeier; |
4255 |
16 Dec 22 |
peter |
33 |
|
4255 |
16 Dec 22 |
peter |
34 |
void test1(yat::test::Suite& suite); |
4255 |
16 Dec 22 |
peter |
35 |
|
4255 |
16 Dec 22 |
peter |
36 |
int main(int argc, char* argv[]) |
4255 |
16 Dec 22 |
peter |
37 |
{ |
4255 |
16 Dec 22 |
peter |
38 |
yat::test::Suite suite(argc, argv); |
4255 |
16 Dec 22 |
peter |
39 |
try { |
4255 |
16 Dec 22 |
peter |
40 |
test1(suite); |
4255 |
16 Dec 22 |
peter |
41 |
} |
4255 |
16 Dec 22 |
peter |
42 |
catch (std::runtime_error& e) { |
4255 |
16 Dec 22 |
peter |
43 |
suite.err() << "error: " << e.what() << "\n"; |
4255 |
16 Dec 22 |
peter |
44 |
return EXIT_FAILURE; |
4255 |
16 Dec 22 |
peter |
45 |
} |
4255 |
16 Dec 22 |
peter |
46 |
return suite.return_value(); |
4255 |
16 Dec 22 |
peter |
47 |
} |
4255 |
16 Dec 22 |
peter |
48 |
|
4255 |
16 Dec 22 |
peter |
49 |
|
4255 |
16 Dec 22 |
peter |
50 |
bool equal(int y, int x, yat::test::Suite& suite) |
4255 |
16 Dec 22 |
peter |
51 |
{ |
4255 |
16 Dec 22 |
peter |
52 |
if (x == y) |
4255 |
16 Dec 22 |
peter |
53 |
return true; |
4255 |
16 Dec 22 |
peter |
54 |
|
4255 |
16 Dec 22 |
peter |
55 |
suite.add(false); |
4255 |
16 Dec 22 |
peter |
56 |
suite.err() << x << " not equal " << y << "\n"; |
4255 |
16 Dec 22 |
peter |
57 |
return false; |
4255 |
16 Dec 22 |
peter |
58 |
} |
4255 |
16 Dec 22 |
peter |
59 |
|
4255 |
16 Dec 22 |
peter |
60 |
|
4255 |
16 Dec 22 |
peter |
61 |
void test_result(yat::test::Suite& suite, |
4255 |
16 Dec 22 |
peter |
62 |
const std::pair<const double, KaplanMeier::TimePoint>& x, |
4255 |
16 Dec 22 |
peter |
63 |
double time, int event, int censored, int at_risk, double s) |
4255 |
16 Dec 22 |
peter |
64 |
{ |
4255 |
16 Dec 22 |
peter |
65 |
const KaplanMeier::TimePoint& tp = x.second; |
4255 |
16 Dec 22 |
peter |
66 |
if (!suite.add(suite.equal(x.first, time))) |
4255 |
16 Dec 22 |
peter |
67 |
suite.err() << "error: time incorrect\n"; |
4255 |
16 Dec 22 |
peter |
68 |
|
4255 |
16 Dec 22 |
peter |
69 |
if (!equal(tp.event(), event, suite)) |
4255 |
16 Dec 22 |
peter |
70 |
suite.err() << "error: event incorrect\n"; |
4255 |
16 Dec 22 |
peter |
71 |
|
4255 |
16 Dec 22 |
peter |
72 |
if (!equal(tp.censored(), censored, suite)) |
4255 |
16 Dec 22 |
peter |
73 |
suite.err() << "error: censored incorrect\n"; |
4255 |
16 Dec 22 |
peter |
74 |
|
4255 |
16 Dec 22 |
peter |
75 |
if (!equal(tp.at_risk(), at_risk, suite)) |
4255 |
16 Dec 22 |
peter |
76 |
suite.err() << "error: at_risk incorrect\n"; |
4255 |
16 Dec 22 |
peter |
77 |
|
4255 |
16 Dec 22 |
peter |
78 |
if (!suite.add(suite.equal_fix(tp.survival_proportion(), s, 5*1e-6))) |
4255 |
16 Dec 22 |
peter |
79 |
suite.err() << "error: survival proportions incorrect\n"; |
4255 |
16 Dec 22 |
peter |
80 |
} |
4255 |
16 Dec 22 |
peter |
81 |
|
4255 |
16 Dec 22 |
peter |
82 |
|
4255 |
16 Dec 22 |
peter |
83 |
|
4255 |
16 Dec 22 |
peter |
84 |
void test1(yat::test::Suite& suite) |
4255 |
16 Dec 22 |
peter |
85 |
{ |
4255 |
16 Dec 22 |
peter |
86 |
statistics::KaplanMeier km; |
4255 |
16 Dec 22 |
peter |
87 |
std::vector<double> time = |
4255 |
16 Dec 22 |
peter |
88 |
{ 610, 1015, 1155, 92, 780, 1143, 944, 901, 961, 725, 406, 668, 668, 259, |
4255 |
16 Dec 22 |
peter |
89 |
282, 791, 720, 645, 513, 552, 617, 609}; |
4255 |
16 Dec 22 |
peter |
90 |
std::vector<char> event = |
4255 |
16 Dec 22 |
peter |
91 |
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0}; |
4255 |
16 Dec 22 |
peter |
92 |
assert(time.size() == event.size()); |
4255 |
16 Dec 22 |
peter |
93 |
for (size_t i=0; i<time.size(); ++i) |
4255 |
16 Dec 22 |
peter |
94 |
km.add(time[i], event[i]); |
4255 |
16 Dec 22 |
peter |
95 |
|
4255 |
16 Dec 22 |
peter |
96 |
auto it = km.begin(); |
4255 |
16 Dec 22 |
peter |
97 |
if (!equal(it->second.at_risk(), 22, suite)) |
4255 |
16 Dec 22 |
peter |
98 |
suite.err() << "error: at_risk incorrect\n"; |
4255 |
16 Dec 22 |
peter |
99 |
|
4255 |
16 Dec 22 |
peter |
100 |
it = km.find(259.0); |
4255 |
16 Dec 22 |
peter |
101 |
assert(it!=km.end()); |
4255 |
16 Dec 22 |
peter |
102 |
test_result(suite, *it, 259, 1, 0, 21, 0.90909); |
4255 |
16 Dec 22 |
peter |
103 |
|
4255 |
16 Dec 22 |
peter |
104 |
it = km.find(668.0); |
4255 |
16 Dec 22 |
peter |
105 |
assert(it!=km.end()); |
4255 |
16 Dec 22 |
peter |
106 |
test_result(suite, *it, 668, 2, 0, 12, 0.67549); |
4255 |
16 Dec 22 |
peter |
107 |
|
4255 |
16 Dec 22 |
peter |
108 |
it = km.find(720.0); |
4255 |
16 Dec 22 |
peter |
109 |
assert(it!=km.end()); |
4255 |
16 Dec 22 |
peter |
110 |
test_result(suite, *it, 720, 0, 1, 10, 0.67549); |
4255 |
16 Dec 22 |
peter |
111 |
|
4255 |
16 Dec 22 |
peter |
112 |
++it; |
4255 |
16 Dec 22 |
peter |
113 |
assert(it!=km.end()); |
4255 |
16 Dec 22 |
peter |
114 |
test_result(suite, *it, 725, 1, 0, 9, 0.60043); |
4255 |
16 Dec 22 |
peter |
115 |
|
4255 |
16 Dec 22 |
peter |
116 |
++it; |
4255 |
16 Dec 22 |
peter |
117 |
assert(it!=km.end()); |
4255 |
16 Dec 22 |
peter |
118 |
test_result(suite, *it, 780, 1, 0, 8, 0.52538); |
4255 |
16 Dec 22 |
peter |
119 |
|
4255 |
16 Dec 22 |
peter |
120 |
++it; |
4255 |
16 Dec 22 |
peter |
121 |
assert(it!=km.end()); |
4255 |
16 Dec 22 |
peter |
122 |
test_result(suite, *it, 791, 0, 1, 7, 0.52538); |
4255 |
16 Dec 22 |
peter |
123 |
} |