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

pymc-devs / pymc3 / 8320

pending completion
8320

push

travis-ci

springcoil
Skipping the parallel test for py27

1 of 1 new or added line in 1 file covered. (100.0%)

9799 of 19856 relevant lines covered (49.35%)

2.49 hits per line

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

70.0
/pymc3/util.py
1
import re
8✔
2
import functools
8✔
3
from numpy import asscalar
8✔
4

5
LATEX_ESCAPE_RE = re.compile(r'(%|_|\$|#|&)', re.MULTILINE)
8✔
6

7

8
def escape_latex(strng):
8✔
9
    """Consistently escape LaTeX special characters for _repr_latex_ in IPython
10

11
    Implementation taken from the IPython magic `format_latex`
12

13
    Examples
14
    --------
15
        escape_latex('disease_rate')  # 'disease\_rate'
16

17
    Parameters
18
    ----------
19
    strng : str
20
        string to escape LaTeX characters
21

22
    Returns
23
    -------
24
    str
25
        A string with LaTeX escaped
26
    """
27
    if strng is None:
×
28
        return u'None'
×
29
    return LATEX_ESCAPE_RE.sub(r'\\\1', strng)
×
30

31

32
def get_transformed_name(name, transform):
8✔
33
    """
34
    Consistent way of transforming names
35

36
    Parameters
37
    ----------
38
    name : str
39
        Name to transform
40
    transform : transforms.Transform
41
        Should be a subclass of `transforms.Transform`
42

43
    Returns
44
    -------
45
    str
46
        A string to use for the transformed variable
47
    """
48
    return "{}_{}__".format(name, transform.name)
8✔
49

50

51
def is_transformed_name(name):
8✔
52
    """
53
    Quickly check if a name was transformed with `get_transormed_name`
54

55
    Parameters
56
    ----------
57
    name : str
58
        Name to check
59

60
    Returns
61
    -------
62
    bool
63
        Boolean, whether the string could have been produced by `get_transormed_name`
64
    """
65
    return name.endswith('__') and name.count('_') >= 3
8✔
66

67

68
def get_untransformed_name(name):
8✔
69
    """
70
    Undo transformation in `get_transformed_name`. Throws ValueError if name wasn't transformed
71

72
    Parameters
73
    ----------
74
    name : str
75
        Name to untransform
76

77
    Returns
78
    -------
79
    str
80
        String with untransformed version of the name.
81
    """
82
    if not is_transformed_name(name):
6✔
83
        raise ValueError(
×
84
            u'{} does not appear to be a transformed name'.format(name))
85
    return '_'.join(name.split('_')[:-3])
6✔
86

87

88
def get_default_varnames(var_iterator, include_transformed):
8✔
89
    """Helper to extract default varnames from a trace.
90

91
    Parameters
92
    ----------
93
    varname_iterator : iterator
94
        Elements will be cast to string to check whether it is transformed, and optionally filtered
95
    include_transformed : boolean
96
        Should transformed variable names be included in return value
97

98
    Returns
99
    -------
100
    list
101
        List of variables, possibly filtered
102
    """
103
    if include_transformed:
8✔
104
        return list(var_iterator)
8✔
105
    else:
106
        return [var for var in var_iterator if not is_transformed_name(str(var))]
6✔
107

108

109
def get_variable_name(variable):
8✔
110
    """Returns the variable data type if it is a constant, otherwise
111
    returns the argument name.
112
    """
113
    name = variable.name
×
114
    if name is None:
×
115
        if hasattr(variable, 'get_parents'):
×
116
            try:
×
117
                names = [get_variable_name(item)
×
118
                         for item in variable.get_parents()[0].inputs]
119
                # do not escape_latex these, since it is not idempotent
120
                return 'f(%s)' % ',~'.join([n for n in names if isinstance(n, str)])
×
121
            except IndexError:
×
122
                pass
×
123
        value = variable.eval()
×
124
        if not value.shape:
×
125
            return asscalar(value)
×
126
        return 'array'
×
127
    return r'\text{%s}' % name
×
128

129

130
def update_start_vals(a, b, model):
8✔
131
    """Update a with b, without overwriting existing keys. Values specified for
132
    transformed variables on the original scale are also transformed and inserted.
133
    """
134
    if model is not None:
8✔
135
        for free_RV in model.free_RVs:
8✔
136
            tname = free_RV.name
8✔
137
            for name in a:
8✔
138
                if is_transformed_name(tname) and get_untransformed_name(tname) == name:
8✔
139
                    transform_func = [
6✔
140
                        d.transformation for d in model.deterministics if d.name == name]
141
                    if transform_func:
6✔
142
                        b[tname] = transform_func[0].forward_val(
6✔
143
                            a[name], point=b)
144

145
    a.update({k: v for k, v in b.items() if k not in a})
8✔
146

147

148
def get_transformed(z):
8✔
149
    if hasattr(z, 'transformed'):
5✔
150
        z = z.transformed
2✔
151
    return z
5✔
152

153

154
def biwrap(wrapper):
8✔
155
    @functools.wraps(wrapper)
8✔
156
    def enhanced(*args, **kwargs):
157
        is_bound_method = hasattr(args[0], wrapper.__name__) if args else False
8✔
158
        if is_bound_method:
8✔
159
            count = 1
×
160
        else:
161
            count = 0
8✔
162
        if len(args) > count:
8✔
163
            newfn = wrapper(*args, **kwargs)
8✔
164
            return newfn
8✔
165
        else:
166
            newwrapper = functools.partial(wrapper, *args, **kwargs)
8✔
167
            return newwrapper
8✔
168
    return enhanced
8✔
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