-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-parse_input_data.R
More file actions
169 lines (139 loc) · 4.4 KB
/
Copy pathtest-parse_input_data.R
File metadata and controls
169 lines (139 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
test_that("parse_input_data handles regular data frame input, with OBS_TYPE label but not specified as `obs_type_label`", {
# Create test data
data <- data.frame(
T = c(2, 1, 3),
OBS_TYPE = c(2, 1, 1),
Y = c(10, 20, 30)
)
result <- parse_input_data(data, obs_type_label = NULL)
# Check column names are lowercase
expect_equal(names(result), c("t", "y", "obs_type"))
# Check sorting
expect_equal(result$t, c(1, 2, 3))
expect_equal(result$obs_type, c(1, 1, 1))
})
test_that("parse_input_data picks right column, even with lower/uppercase duplicates", {
data <- data.frame(
T = c(1, 2, 3),
obs_type = c(1, 0, 1),
OBS_TYPE = c(2, 1, 1),
Y = c(10, 20, 30)
)
result <- parse_input_data(data, obs_type_label = "OBS_TYPE")
expect_equal(result$obs_type, c(2, 1, 1))
expect_equal(sum(tolower(names(result)) == "obs_type"), 1)
})
test_that("parse_input_data handles regular data frame input, with OBS_TYPE label, properly specified as `obs_type_label`", {
# Create test data
data <- data.frame(
T = c(2, 1, 3),
OBS_TYPE = c(2, 1, 1),
Y = c(10, 20, 30)
)
result <- parse_input_data(data, obs_type_label = "OBS_TYPE")
# Check column names are lowercase
expect_equal(names(result), c("t", "y", "obs_type"))
# Check sorting
expect_equal(result$t, c(1, 2, 3))
expect_equal(result$obs_type, c(1, 2, 1))
})
test_that("parse_input_data handles PKPDsim object", {
# Create mock PKPDsim object
data <- structure(
data.frame(
t = c(2, 1, 3),
comp = c("obs", "dose", "obs"),
dv = c(10, 20, 30)
),
class = c("data.frame", "PKPDsim")
)
result <- parse_input_data(data, cols=list(x = "t", y = "dv"))
# Check that only obs rows are kept
expect_equal(nrow(result), 2)
expect_true(all(result$comp == "obs"))
# Check that evid column is added and set to 0
expect_true("evid" %in% names(result))
expect_true(all(result$evid == 0))
})
test_that("parse_input_data handles EVID filtering", {
# Create test data with EVID column
data <- data.frame(
t = c(2, 1, 3),
obs_type = c(2, 1, 1),
dv = c(10, 20, 30),
EVID = c(0, 1, 0)
)
result <- parse_input_data(data, cols=list(x = "t", y = "dv"))
# Check that only EVID=0 rows are kept
expect_equal(nrow(result), 2)
expect_true(all(result$evid == 0))
})
test_that("parse_input_data handles obs_type_label argument", {
# Test with NULL obs_type_label (default behavior)
data <- data.frame(
t = c(2, 1, 3),
dv = c(10, 20, 30)
)
result <- parse_input_data(data, cols=list(x = "t", y = "dv"))
expect_equal(result$obs_type, c(1, 1, 1))
# Test with custom obs_type_label
data <- data.frame(
t = c(2, 1, 3),
dv = c(10, 20, 30),
measurement_type = c(2, 1, 3)
)
result <- parse_input_data(
data,
cols=list(x = "t", y = "dv"),
obs_type_label = "measurement_type"
)
expect_equal(result$obs_type, c(1, 2, 3))
})
test_that("parse_input_data handles PKPDsim object with obs_type_label", {
# Create mock PKPDsim object with obs_type_label
data <- structure(
data.frame(
t = c(2, 1, 3),
comp = c("obs", 1, "obs"),
dv = c(10, 20, 30),
measurement_type = c(2, 1, 3)
),
class = c("data.frame", "PKPDsim")
)
result <- parse_input_data(
data,
cols=list(x = "t", y = "dv"),
obs_type_label = "measurement_type"
)
# Check that only obs rows are kept and obs_type is set correctly
expect_equal(nrow(result), 2)
expect_true(all(result$comp == "obs"))
expect_equal(result$obs_type, c(2, 3))
})
test_that("parse_input_data handles multiple EVID values", {
# Create test data with multiple EVID values
data <- data.frame(
t = c(2, 1, 3, 4),
obs_type = c(2, 1, 1, 1),
dv = c(10, 20, 30, 40),
EVID = c(0, 1, 2, 0)
)
result <- parse_input_data(data, cols=list(x = "t", y = "dv"))
# Check that only EVID=0 rows are kept
expect_equal(nrow(result), 2)
expect_true(all(result$evid == 0))
expect_equal(result$t, c(2, 4))
})
test_that("parse_input_data preserves data values after transformations", {
# Create test data
data <- data.frame(
t = c(2, 1, 3),
obs_type = c(2, 1, 1),
DV = c(10, 20, 30),
EVID = c(0, 1, 0)
)
result <- parse_input_data(data, cols=list(x = "t", y = "dv"))
# Check that data values are preserved after transformations
expect_equal(result$dv, c(10, 30))
expect_equal(result$t, c(2, 3))
})