Skip to content

Commit df1be62

Browse files
author
Gertjan Bisschop
committed
fix 2
1 parent 292c8eb commit df1be62

7 files changed

Lines changed: 61 additions & 53 deletions

File tree

R/R/derived_variables.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
.series_to_list <- function(x) {
1414
if (is.null(x)) return(NULL)
15-
x$to_list()
15+
as.vector(x)
1616
}
1717

1818
.series_to_numeric <- function(x) {
@@ -52,13 +52,13 @@
5252
return(as.numeric(x[[1]]))
5353
}
5454

55-
if (is.environment(x) && !is.null(x$to_list)) {
56-
vals <- x$to_list()
55+
if (.is_polars_series(x)) {
56+
vals <- .series_to_list(x)
5757
if (length(vals) == 0) {
5858
stop(sprintf("Parameter '%s' must contain at least one value", name), call. = FALSE)
5959
}
6060
first <- vals[[1]]
61-
if (is.null(first)) {
61+
if (is.null(first) || is.na(first)) {
6262
stop(sprintf("Parameter '%s' cannot be null", name), call. = FALSE)
6363
}
6464
return(as.numeric(first))
@@ -72,11 +72,11 @@
7272
}
7373

7474
.is_polars_series <- function(x) {
75-
is.environment(x) && !is.null(x$to_list)
75+
inherits(x, "polars_series")
7676
}
7777

7878
.to_series <- function(values) {
79-
polars::pl$Series(values)
79+
polars::pl$Series("", values)
8080
}
8181

8282
.validate_same_length <- function(...) {
@@ -101,7 +101,7 @@
101101
}
102102

103103
.is_series_like <- function(x) {
104-
is.environment(x) && !is.null(x$to_list)
104+
.is_polars_series(x)
105105
}
106106

107107
.split_named_weighted_inputs <- function(values_by_name) {

R/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Example:
5151
```r
5252
library(polars)
5353

54-
measured <- pl$Series(c(50.0, 100.0, 75.0))
55-
sg <- pl$Series(c(1.020, 1.015, 1.025))
54+
measured <- pl$Series("measured", c(50.0, 100.0, 75.0))
55+
sg <- pl$Series("sg_measured", c(1.020, 1.015, 1.025))
5656

5757
out <- compehndly_apply(
5858
"normalize_specific_gravity",
@@ -61,15 +61,15 @@ out <- compehndly_apply(
6161
.params = list(sg_ref = 1.024)
6262
)
6363

64-
out$to_list()
64+
as.vector(out)
6565
```
6666

6767
Bin decoding uses the same numbered pair contract as Python:
6868

6969
```r
70-
values <- pl$Series(c(-10.0, 1.25, -3.0, 4.5))
71-
copy_a <- pl$Series(c(10.0, 20.0, 30.0, 40.0))
72-
copy_b <- pl$Series(c(50.0, 60.0, 70.0, 80.0))
70+
values <- pl$Series("values", c(-10.0, 1.25, -3.0, 4.5))
71+
copy_a <- pl$Series("copy_from_1", c(10.0, 20.0, 30.0, 40.0))
72+
copy_b <- pl$Series("copy_from_2", c(50.0, 60.0, 70.0, 80.0))
7373

7474
out <- compehndly_apply(
7575
"bin_decoding",
@@ -79,7 +79,7 @@ out <- compehndly_apply(
7979
.params = list(filter_value_1 = -10.0, filter_value_2 = -3.0)
8080
)
8181

82-
out$to_list()
82+
as.vector(out)
8383
```
8484

8585
## Run Shared Conformance Tests

R/tests/testthat/helper-conformance.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ to_numeric_with_na <- function(x) {
2424
numeric(1)
2525
)
2626
}
27+
28+
test_series <- function(values) {
29+
polars::pl$Series("", values)
30+
}
31+
32+
series_values <- function(x) {
33+
as.vector(x)
34+
}

R/tests/testthat/test-bin-decoding.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ test_that("bin_decoding decodes numbered pairs", {
33

44
out <- compehndly_apply(
55
"bin_decoding",
6-
values = polars::pl$Series(c(-10, 1.25, -3, 4.5, -2)),
7-
copy_from_1 = polars::pl$Series(c(10, 20, 30, 40, 50)),
8-
copy_from_2 = polars::pl$Series(c(60, 70, 80, 90, 100)),
6+
values = test_series(c(-10, 1.25, -3, 4.5, -2)),
7+
copy_from_1 = test_series(c(10, 20, 30, 40, 50)),
8+
copy_from_2 = test_series(c(60, 70, 80, 90, 100)),
99
.params = list(filter_value_1 = -10, filter_value_2 = -3)
1010
)
1111

1212
expect_equal(
13-
to_numeric_with_na(out$to_list()),
13+
to_numeric_with_na(series_values(out)),
1414
c(10, 1.25, 80, 4.5, -2)
1515
)
1616
})
@@ -21,7 +21,7 @@ test_that("bin_decoding requires complete contiguous unique pairs", {
2121
expect_error(
2222
compehndly_apply(
2323
"bin_decoding",
24-
values = polars::pl$Series(c(-10)),
24+
values = test_series(c(-10)),
2525
.params = list(filter_value_1 = -10)
2626
),
2727
"missing copy_from_1"
@@ -30,8 +30,8 @@ test_that("bin_decoding requires complete contiguous unique pairs", {
3030
expect_error(
3131
compehndly_apply(
3232
"bin_decoding",
33-
values = polars::pl$Series(c(-10)),
34-
copy_from_2 = polars::pl$Series(c(10)),
33+
values = test_series(c(-10)),
34+
copy_from_2 = test_series(c(10)),
3535
.params = list(filter_value_2 = -10)
3636
),
3737
"contiguous"
@@ -40,9 +40,9 @@ test_that("bin_decoding requires complete contiguous unique pairs", {
4040
expect_error(
4141
compehndly_apply(
4242
"bin_decoding",
43-
values = polars::pl$Series(c(-10)),
44-
copy_from_1 = polars::pl$Series(c(10)),
45-
copy_from_2 = polars::pl$Series(c(20)),
43+
values = test_series(c(-10)),
44+
copy_from_1 = test_series(c(10)),
45+
copy_from_2 = test_series(c(20)),
4646
.params = list(filter_value_1 = -10, filter_value_2 = -10)
4747
),
4848
"unique"

R/tests/testthat/test-conformance.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ test_that("shared conformance vectors are available and runnable", {
1313
cases <- payload$cases
1414

1515
for (case in cases) {
16-
function_name <- case$function
16+
function_name <- case[["function"]]
1717
params <- case$params %||% list()
1818
input <- case$input %||% list()
1919

2020
invoke <- function() {
2121
if (!is.null(input$positional)) {
22-
series <- lapply(input$positional, polars::pl$Series)
22+
series <- lapply(input$positional, test_series)
2323
return(do.call(compehndly_apply, c(list(function_name), series, .params = list(params))))
2424
}
2525

2626
if (!is.null(input$named)) {
27-
named_series <- lapply(input$named, polars::pl$Series)
27+
named_series <- lapply(input$named, test_series)
2828
return(do.call(compehndly_apply, c(list(function_name), named_series, .params = list(params))))
2929
}
3030

@@ -40,35 +40,35 @@ test_that("shared conformance vectors are available and runnable", {
4040
assertions <- case$assertions %||% list()
4141

4242
if (!is.null(assertions$expected)) {
43-
got <- to_numeric_with_na(out$to_list())
43+
got <- to_numeric_with_na(series_values(out))
4444
expected <- to_numeric_with_na(assertions$expected)
4545
expect_equal(got, expected, tolerance = 1e-8)
4646
}
4747

4848
if (isTRUE(assertions$all_null)) {
49-
expect_equal(out$null_count(), out$len())
49+
expect_equal(sum(is.na(series_values(out))), length(out))
5050
}
5151

5252
if (isTRUE(assertions$non_negative)) {
53-
got <- as.numeric(out$to_numpy())
53+
got <- as.numeric(series_values(out))
5454
expect_true(all(got >= 0))
5555
}
5656

5757
if (isTRUE(assertions$no_nan)) {
58-
got <- as.numeric(out$to_numpy())
58+
got <- as.numeric(series_values(out))
5959
expect_false(any(is.nan(got)))
6060
}
6161

6262
if (!is.null(assertions$equals_at_indices)) {
63-
got <- as.numeric(out$to_numpy())
63+
got <- as.numeric(series_values(out))
6464
for (idx_name in names(assertions$equals_at_indices)) {
6565
idx <- as.integer(idx_name) + 1
6666
expect_equal(got[[idx]], as.numeric(assertions$equals_at_indices[[idx_name]]), tolerance = 1e-8)
6767
}
6868
}
6969

7070
if (!is.null(assertions$ranges_at_indices)) {
71-
got <- as.numeric(out$to_numpy())
71+
got <- as.numeric(series_values(out))
7272
for (idx_name in names(assertions$ranges_at_indices)) {
7373
idx <- as.integer(idx_name) + 1
7474
bounds <- assertions$ranges_at_indices[[idx_name]]
@@ -78,7 +78,7 @@ test_that("shared conformance vectors are available and runnable", {
7878
}
7979

8080
if (!is.null(assertions$min_value_from_index)) {
81-
got <- as.numeric(out$to_numpy())
81+
got <- as.numeric(series_values(out))
8282
start <- as.integer(assertions$min_value_from_index$start) + 1
8383
min_value <- as.numeric(assertions$min_value_from_index$value)
8484
expect_true(all(got[start:length(got)] >= min_value))

R/tests/testthat/test-imputation.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ test_that("medium_bound_imputation follows decision table", {
2121
measurements <- c(-10, -3, -2, -1, 0.5, 1.5, 3)
2222
out <- compehndly_apply(
2323
"medium_bound_imputation",
24-
measurement = polars::pl$Series(rep(measurements, 4)),
25-
lod = polars::pl$Series(c(rep(NA_real_, 7), rep(1, 7), rep(NA_real_, 7), rep(1, 7))),
26-
loq = polars::pl$Series(c(rep(NA_real_, 14), rep(2, 14)))
24+
measurement = test_series(rep(measurements, 4)),
25+
lod = test_series(c(rep(NA_real_, 7), rep(1, 7), rep(NA_real_, 7), rep(1, 7))),
26+
loq = test_series(c(rep(NA_real_, 14), rep(2, 14)))
2727
)
2828

2929
expect_equal(
30-
to_numeric_with_na(out$to_list()),
30+
to_numeric_with_na(series_values(out)),
3131
c(
3232
rep(NA_real_, 7),
3333
NA_real_, NA_real_, NA_real_, 0.5, 0.5, 1.5, 3,
@@ -43,13 +43,13 @@ test_that("lab_sensitivity_dichotomization follows decision table", {
4343
measurements <- c(-10, -3, -2, -1, 0.5, 1.5, 3)
4444
out <- compehndly_apply(
4545
"lab_sensitivity_dichotomization",
46-
measurement = polars::pl$Series(rep(measurements, 4)),
47-
lod = polars::pl$Series(c(rep(NA_real_, 7), rep(1, 7), rep(NA_real_, 7), rep(1, 7))),
48-
loq = polars::pl$Series(c(rep(NA_real_, 14), rep(2, 14)))
46+
measurement = test_series(rep(measurements, 4)),
47+
lod = test_series(c(rep(NA_real_, 7), rep(1, 7), rep(NA_real_, 7), rep(1, 7))),
48+
loq = test_series(c(rep(NA_real_, 14), rep(2, 14)))
4949
)
5050

5151
expect_equal(
52-
to_logical_with_na(out$to_list()),
52+
to_logical_with_na(series_values(out)),
5353
c(
5454
rep(NA, 7),
5555
NA, NA, NA, FALSE, FALSE, TRUE, TRUE,
@@ -65,12 +65,12 @@ test_that("random_single_imputation follows null and bound decisions", {
6565
measurements <- c(-10, -3, -2, -1, 0.5, 1.5, 3)
6666
out <- compehndly_apply(
6767
"random_single_imputation",
68-
biomarker = polars::pl$Series(rep(measurements, 4)),
69-
lod = polars::pl$Series(c(rep(NA_real_, 7), rep(1, 7), rep(NA_real_, 7), rep(1, 7))),
70-
loq = polars::pl$Series(c(rep(NA_real_, 14), rep(2, 14))),
68+
biomarker = test_series(rep(measurements, 4)),
69+
lod = test_series(c(rep(NA_real_, 7), rep(1, 7), rep(NA_real_, 7), rep(1, 7))),
70+
loq = test_series(c(rep(NA_real_, 14), rep(2, 14))),
7171
.params = list(seed = 42)
7272
)
73-
values <- to_numeric_with_na(out$to_list())
73+
values <- to_numeric_with_na(series_values(out))
7474

7575
expect_true(all(is.na(values[1:7])))
7676
expect_true(all(is.na(values[8:10])))

docs/r/usage.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ compehndly_apply(function_name, ..., .params = list())
5757
For example, normalize a measured value by specific gravity:
5858

5959
```r
60-
measured <- pl$Series(c(50.0, 100.0, 75.0))
61-
sg <- pl$Series(c(1.020, 1.015, 1.025))
60+
measured <- pl$Series("measured", c(50.0, 100.0, 75.0))
61+
sg <- pl$Series("sg_measured", c(1.020, 1.015, 1.025))
6262

6363
out <- compehndly_apply(
6464
"normalize_specific_gravity",
@@ -67,15 +67,15 @@ out <- compehndly_apply(
6767
.params = list(sg_ref = 1.024)
6868
)
6969

70-
out$to_list()
70+
as.vector(out)
7171
```
7272

7373
Bin decoding uses the same numbered pair contract as Python:
7474

7575
```r
76-
values <- pl$Series(c(-10.0, 1.25, -3.0, 4.5))
77-
copy_a <- pl$Series(c(10.0, 20.0, 30.0, 40.0))
78-
copy_b <- pl$Series(c(50.0, 60.0, 70.0, 80.0))
76+
values <- pl$Series("values", c(-10.0, 1.25, -3.0, 4.5))
77+
copy_a <- pl$Series("copy_from_1", c(10.0, 20.0, 30.0, 40.0))
78+
copy_b <- pl$Series("copy_from_2", c(50.0, 60.0, 70.0, 80.0))
7979

8080
out <- compehndly_apply(
8181
"bin_decoding",
@@ -85,7 +85,7 @@ out <- compehndly_apply(
8585
.params = list(filter_value_1 = -10.0, filter_value_2 = -3.0)
8686
)
8787

88-
out$to_list()
88+
as.vector(out)
8989
```
9090

9191
## Run R Tests

0 commit comments

Comments
 (0)