Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/statsmodels/base/wrapper.py : 49%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from statsmodels.compat.python import iteritems
3import functools
4import inspect
5from textwrap import dedent
8class ResultsWrapper(object):
9 """
10 Class which wraps a statsmodels estimation Results class and steps in to
11 reattach metadata to results (if available)
12 """
13 _wrap_attrs = {}
14 _wrap_methods = {}
16 def __init__(self, results):
17 self._results = results
18 self.__doc__ = results.__doc__
20 def __dir__(self):
21 return [x for x in dir(self._results)]
23 def __getattribute__(self, attr):
24 get = lambda name: object.__getattribute__(self, name)
26 try:
27 results = get('_results')
28 except AttributeError:
29 pass
31 try:
32 return get(attr)
33 except AttributeError:
34 pass
36 obj = getattr(results, attr)
37 data = results.model.data
38 how = self._wrap_attrs.get(attr)
39 if how and isinstance(how, tuple):
40 obj = data.wrap_output(obj, how[0], *how[1:])
41 elif how:
42 obj = data.wrap_output(obj, how=how)
44 return obj
46 def __getstate__(self):
47 # print 'pickling wrapper', self.__dict__
48 return self.__dict__
50 def __setstate__(self, dict_):
51 # print 'unpickling wrapper', dict_
52 self.__dict__.update(dict_)
54 def save(self, fname, remove_data=False):
55 """
56 Save a pickle of this instance.
58 Parameters
59 ----------
60 fname : {str, handle}
61 Either a filename or a valid file handle.
62 remove_data : bool
63 If False (default), then the instance is pickled without changes.
64 If True, then all arrays with length nobs are set to None before
65 pickling. See the remove_data method.
66 In some cases not all arrays will be set to None.
67 """
68 from statsmodels.iolib.smpickle import save_pickle
70 if remove_data:
71 self.remove_data()
73 save_pickle(self, fname)
75 @classmethod
76 def load(cls, fname):
77 """
78 Load a pickled results instance
80 .. warning::
82 Loading pickled models is not secure against erroneous or
83 maliciously constructed data. Never unpickle data received from
84 an untrusted or unauthenticated source.
86 Parameters
87 ----------
88 fname : {str, handle}
89 A string filename or a file handle.
91 Returns
92 -------
93 Results
94 The unpickled results instance.
95 """
96 from statsmodels.iolib.smpickle import load_pickle
97 return load_pickle(fname)
100def union_dicts(*dicts):
101 result = {}
102 for d in dicts:
103 result.update(d)
104 return result
107def make_wrapper(func, how):
108 @functools.wraps(func)
109 def wrapper(self, *args, **kwargs):
110 results = object.__getattribute__(self, '_results')
111 data = results.model.data
112 if how and isinstance(how, tuple):
113 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:])
114 elif how:
115 obj = data.wrap_output(func(results, *args, **kwargs), how)
116 return obj
118 sig = inspect.signature(func)
119 formatted = str(sig)
121 doc = dedent(wrapper.__doc__) if wrapper.__doc__ else ''
122 wrapper.__doc__ = "\n%s%s\n%s" % (func.__name__, formatted, doc)
124 return wrapper
127def populate_wrapper(klass, wrapping):
128 for meth, how in iteritems(klass._wrap_methods):
129 if not hasattr(wrapping, meth):
130 continue
132 func = getattr(wrapping, meth)
133 wrapper = make_wrapper(func, how)
134 setattr(klass, meth, wrapper)