• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pymc-devs / pymc3 / 9362

pending completion
9362

Pull #3597

travis-ci

web-flow
Fix test bugs.
Pull Request #3597: WIP: Second try to vectorize sample_posterior_predictive.

513 of 513 new or added lines in 16 files covered. (100.0%)

12617 of 20534 relevant lines covered (61.44%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.69
/pymc3/memoize.py
1
import functools
1✔
2
import pickle
1✔
3
import collections
1✔
4
from .util import biwrap
1✔
5
CACHE_REGISTRY = []
1✔
6

7

8
@biwrap
1✔
9
def memoize(obj, bound=False):
1✔
10
    """
11
    An expensive memoizer that works with unhashables
12
    """
13
    # this is declared not to be a bound method, so just attach new attr to obj
14
    if not bound:
1✔
15
        obj.cache = {}
1✔
16
        CACHE_REGISTRY.append(obj.cache)
1✔
17

18
    @functools.wraps(obj)
1✔
19
    def memoizer(*args, **kwargs):
20
        if not bound:
1✔
21
            key = (hashable(args), hashable(kwargs))
1✔
22
            cache = obj.cache
1✔
23
        else:
24
            # bound methods have self as first argument, remove it to compute key
25
            key = (hashable(args[1:]), hashable(kwargs))
1✔
26
            if not hasattr(args[0], '_cache'):
1✔
27
                setattr(args[0], '_cache', collections.defaultdict(dict))
1✔
28
                # do not add to cache regestry
29
            cache = getattr(args[0], '_cache')[obj.__name__]
1✔
30
        if key not in cache:
1✔
31
            cache[key] = obj(*args, **kwargs)
1✔
32

33
        return cache[key]
1✔
34
    return memoizer
1✔
35

36

37
def clear_cache(obj=None):
1✔
38
    if obj is None:
×
39
        for c in CACHE_REGISTRY:
×
40
            c.clear()
×
41
    else:
42
        if isinstance(obj, WithMemoization):
×
43
            for v in getattr(obj, '_cache', {}).values():
×
44
                v.clear()
×
45
        else:
46
            obj.cache.clear()
×
47

48

49
class WithMemoization:
1✔
50
    def __hash__(self):
1✔
51
        return hash(id(self))
×
52

53
    def __getstate__(self):
1✔
54
        state = self.__dict__.copy()
1✔
55
        state.pop('_cache', None)
1✔
56
        return state
1✔
57

58
    def __setstate__(self, state):
1✔
59
        self.__dict__.update(state)
1✔
60

61

62
def hashable(a):
1✔
63
    """
64
    Turn some unhashable objects into hashable ones.
65
    """
66
    if isinstance(a, dict):
1✔
67
        return hashable(tuple((hashable(a1), hashable(a2)) for a1, a2 in a.items()))
1✔
68
    try:
1✔
69
        return hash(a)
1✔
70
    except TypeError:
1✔
71
        pass
1✔
72
    # Not hashable >>>
73
    try:
1✔
74
        return hash(pickle.dumps(a))
1✔
75
    except Exception:
1✔
76
        if hasattr(a, '__dict__'):
1✔
77
            return hashable(a.__dict__)
×
78
        else:
79
            return id(a)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc