-
Notifications
You must be signed in to change notification settings - Fork 0
/
LMEM_settings.py
73 lines (62 loc) · 2.33 KB
/
LMEM_settings.py
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
import json
import numpy as np
import pandas as pd
import rpy2
LMEM_FAMILY_BINOMIAL = 'binomial'
LMEM_FAMILY_GAUSSIAN = 'gaussian'
LMEM_FAMILY_GAMMA = 'Gamma'
LMEM_FAMILY_POISSON = 'Poisson'
LMEM_ERROR_IDENTITY = 'identity'
LMEM_ERROR_LOG = 'log'
LMEM_RESULT_MODEL = 'model'
LMEM_RESULT_SUMMARY = 'summary'
LMEM_RESULT_ANOVA = 'anova'
LMEM_RESULT_FIXED_EFFECTS = 'fixed effects'
LMEM_RESULT_TERMS = 'terms'
LMEM_RESULT_RANDOM_EFFECTS = 'random effects'
LMEM_RESULT_COEFFICIENTS = 'coefficients'
LMEM_RESULT_FORMULA = 'formula'
LMEM_RESULT_FAMILY = 'family'
LMEM_RESULT_CONF_INT = 'confidence intervals'
LMEM_RESULTS = [LMEM_RESULT_MODEL,
LMEM_RESULT_SUMMARY,
LMEM_RESULT_ANOVA,
LMEM_RESULT_FIXED_EFFECTS,
LMEM_RESULT_TERMS,
LMEM_RESULT_RANDOM_EFFECTS,
LMEM_RESULT_COEFFICIENTS,
LMEM_RESULT_FORMULA,
LMEM_RESULT_FAMILY,
LMEM_RESULT_CONF_INT]
ENCODING_CP1252 = 'cp1252'
def cchar_to_str(c, encoding: str = ENCODING_CP1252) -> str:
s = rpy2.rinterface_lib.openrlib.ffi.string(c).decode(encoding)
return s
def cchar_to_str_with_maxlen(c, maxlen: int, encoding: str = ENCODING_CP1252) -> str:
s = rpy2.rinterface_lib.openrlib.ffi.string(
c, maxlen).decode(ENCODING_CP1252)
return s
class RTypesEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, pd.DataFrame):
return obj.to_dict(orient='index')
if isinstance(obj, pd.Series):
return obj.to_dict()
if isinstance(obj, rpy2.robjects.Vector):
return [i for i in obj.items()]
if isinstance(obj, rpy2.rinterface_lib.sexp.NULLType):
return None
if isinstance(obj, rpy2.robjects.language.LangVector):
return repr(obj)
if isinstance(obj, rpy2.robjects.Environment):
return [k for k in obj.keys()]
if isinstance(obj, rpy2.robjects.Formula) or isinstance(obj, rpy2.robjects.methods.RS4):
obj_dict = {}
for k, value in obj.slots.items():
# print(k, type(value))
obj_dict[k] = self.default(value)
return obj_dict
print(type(obj))
return json.JSONEncoder.default(self, obj)