File: get-R-crs-test-vectors.R

package info (click to toggle)
patsy 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,448 kB
  • sloc: python: 9,536; javascript: 204; makefile: 110
file content (80 lines) | stat: -rw-r--r-- 2,068 bytes parent folder | download | duplicates (7)
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
# R code to dump the output from 'mgcv' cubic splines in many situations, for
# checking compatibility.
#
# Usage:
#   Rscript tools/get-R-crs-test-vectors.R > patsy/test_splines_crs_data.py

cat("# This file auto-generated by tools/get-R-crs-test-vectors.R\n")
cat(sprintf("# Using: %s and package 'mgcv' version %s\n",
            R.Version()$version.string,
            packageVersion("mgcv")))
cat("import numpy as np\n")

options(digits=20)
library(mgcv)
x <- (-1.5)^(0:19)

pyprint <- function(arr) {
  if (is.null(arr)) {
    cat("None\n")
  } else {
    cat("np.array([")
    for (val in arr) {
      cat(val)
      cat(", ")
    }
    cat("])")
    if (!is.null(dim(arr))) {
      cat(".reshape((")
      for (size in dim(arr)) {
        cat(sprintf("%s, ", size))
      }
      cat("), order=\"F\")")
    }
    cat("\n")
  }
}

num.tests <- 0
dump.crs <- function(spline.type, nb.knots, knots, absorb.cons) {
  cat("--BEGIN TEST CASE--\n")
  cat(sprintf("spline_type=%s\n", spline.type))
  cat(sprintf("nb_knots=%s\n", nb.knots))
  cat("knots=")
  pyprint(knots)
  cat(sprintf("absorb_cons=%s\n", absorb.cons))

  args <- list(object=s(x, bs=spline.type, k=nb.knots),
               data=data.frame(x=x), knots=NULL, absorb.cons=absorb.cons)
  if (!is.null(knots)) {
    args$knots <- data.frame(x=knots)
  }

  result <- do.call(smoothCon, args)

  cat("output=")
  pyprint(result[[1]]$X)
  cat("--END TEST CASE--\n")
  assign("num.tests", num.tests + 1, envir=.GlobalEnv)
}

cat("R_crs_test_x = ")
pyprint(x)
cat("R_crs_test_data = \"\"\"\n")

for (spline.type in c("cr", "cs", "cc")) {
  for (absorb.cons in c(FALSE, TRUE)) {
    for (nb.knots in c(4, 7, 10, 12)) {
      dump.crs(spline.type, nb.knots, NULL, absorb.cons)
    }

    for (knots in list(c(-2500., -150., 300., 1500.),
                       c(-400., -50., 10., 50., 100.),
                       c(-1000., -500., -250., 0., 250., 500., 1000.))) {
      dump.crs(spline.type, length(knots), knots, absorb.cons)
    }
  }
}

cat("\"\"\"\n")
cat(sprintf("R_crs_num_tests = %s\n", num.tests))