forked from statsmodels/statsmodels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopy.R
125 lines (115 loc) · 4.25 KB
/
topy.R
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
sanitize_name <- function(name) {
#"[%s]" % "]|[".join(map(re.escape, list(string.punctuation.replace("_","")
punctuation <- '[\\!]|[\\"]|[\\#]|[\\$]|[\\%]|[\\&]|[\\\']|[\\(]|[\\)]|[\\*]|[\\+]|[\\,]|[\\-]|[\\.]|[\\/]|[\\:]|[\\;]|[\\<]|[\\=]|[\\>]|[\\?]|[\\@]|[\\[]|[\\\\]|[\\]]|[\\^]|[\\`]|[\\{]|[\\|]|[\\}]|[\\~]'
# handle spaces,tabs,etc. and periods specially
name <- gsub("[[:blank:]\\.]", "_", name)
name <- gsub(punctuation, "", name)
return(name)
}
cat_items <- function(object, prefix="", blacklist=NULL, trans=list()) {
#print content (names) of object into python expressions for defining variables
#
#Parameters
#----------
#object : object with names attribute
# names(object) will be written as python assignments
#prefix : string
# string that is prepended to the variable names
#blacklist : list of strings
# names that are in the blacklist are ignored
#trans : named list (dict_like)
# names that are in trans will be replaced by the corresponding value
#
#example cat_items(fitresult, blacklist=c("eq"))
#
#currently limited type inference, mainly numerical
items = names(object)
for (name in items) {
if (is.element(name, blacklist)) next
#cat(name); cat("\n")
item = object[[name]]
#fix name
#Skipper's sanitize name
name_ <- gsub("\\.", "_", name) # make name pythonic
#translate name
newname = trans[[name_]] #translation table on sanitized names
if (!is.null(newname)) {
name_ = newname
}
name_ = paste(prefix, name_, sep="")
if (is.numeric(item)) {
if (!is.null(names(item))) { #named list, class numeric ?
mkarray2(as.matrix(item), name_);
if (!is.null(dimnames(item))) write_dimnames(item, prefix=name_)
}
else if (class(item) == 'matrix') {
mkarray2(item, name_);
if (!is.null(dimnames(item))) write_dimnames(item, prefix=name_)
}
else if (class(item) == 'numeric') { #scalar
cat(name_); cat(" = "); cat(item); cat("\n")
}
}
else if (is.character(item)) {
#assume string doesn't contain single quote
cat(name_); cat(" = '"); cat(item); cat("'\n")
}
else {
cat(name_); cat(" = '''"); cat(deparse(item)); cat("'''"); cat("\n")
}
}
} #end function
write_dimnames <- function(mat, prefix="") {
if (prefix != "") {
prefix = paste(prefix, "_", sep="")
}
dimn = list("rownames", "colnames", "thirdnames") #up to 3 dimension ?
for (ii in c(1:length(dimnames(mat)))) {
cat(paste(prefix, dimn[[ii]], sep=""))
cat (" = [");
for (dname in dimnames(mat)[[ii]]) {
cat("'"); cat(dname); cat("', ")
}
cat("]\n")
}
}
write_header <-function() {
cat("import numpy as np\n\n")
cat("class Bunch(dict):\n")
cat(" def __init__(self, **kw):\n")
cat(" dict.__init__(self, kw)\n")
cat(" self.__dict__ = self\n\n")
}
mkhtest <- function(ht, name, distr="f") {
#function to write results of a statistical test of class htest to a python dict
#
#Parameters
#----------
#ht : instance of ht
# return of many statistical tests
#name : string
# name of variable that holds results dict
#distr : string
# distribution of the test statistic
#
cat(name); cat(" = dict(");
cat("statistic="); cat(ht$statistic); cat(", ");
cat("pvalue="); cat(ht$p.value); cat(", ");
cat("parameters=("); cat(ht$parameter, sep=","); cat(",), ");
cat("distr='"); cat(distr); cat("'");
cat(")");
cat("\n\n")
}
mkarray2 <- function(X, name, sanitize=FALSE) {
indent = " "
if (sanitize) {
cat(sanitize_name(name)); cat(" = np.array([\n"); cat(X, sep=", ", fill=76, labels=indent); cat(indent); cat("])") }
else{
cat(name); cat(" = np.array([\n"); cat(X, sep=", ", fill=76, labels=indent); cat(indent); cat("])") }
if (is.matrix(X)) {
i <- as.character(nrow(X))
j <- as.character(ncol(X))
cat(".reshape("); cat(i); cat(","); cat(j); cat(", order='F')")
}
cat("\n")
}