• 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

0.0
/pymc3/tests/test_model_helpers.py
1
import numpy as np
×
2
import numpy.ma as ma
×
3
import numpy.testing as npt
×
4
import pandas as pd
×
5
import pymc3 as pm
×
6
import scipy.sparse as sps
×
7

8
import theano
×
9
import theano.tensor as tt
×
10
import theano.sparse as sparse
×
11

12

13
class TestHelperFunc(object):
×
14
    def test_pandas_to_array(self):
×
15
        """
16
        Ensure that pandas_to_array returns the dense array, masked array,
17
        graph variable, TensorVariable, or sparse matrix as appropriate.
18
        """
19
        # Create the various inputs to the function
20
        sparse_input = sps.csr_matrix(np.eye(3))
×
21
        dense_input = np.arange(9).reshape((3, 3))
×
22

23
        input_name = 'input_variable'
×
24
        theano_graph_input = tt.as_tensor(dense_input, name=input_name)
×
25

26
        pandas_input = pd.DataFrame(dense_input)
×
27

28
        # All the even numbers are replaced with NaN
29
        missing_pandas_input = pd.DataFrame(np.array([[np.nan, 1, np.nan],
×
30
                                                      [3, np.nan, 5],
31
                                                      [np.nan, 7, np.nan]]))
32
        masked_array_input = ma.array(dense_input,
×
33
                                      mask=(np.mod(dense_input, 2) == 0))
34

35
        # Create a generator object. Apparently the generator object needs to
36
        # yield numpy arrays.
37
        square_generator = (np.array([i**2], dtype=int) for i in range(100))
×
38

39
        # Alias the function to be tested
40
        func = pm.model.pandas_to_array
×
41

42
        #####
43
        # Perform the various tests
44
        #####
45
        # Check function behavior with dense arrays and pandas dataframes
46
        # without missing values
47
        for input_value in [dense_input, pandas_input]:
×
48
            func_output = func(input_value)
×
49
            assert isinstance(func_output, np.ndarray)
×
50
            assert func_output.shape == input_value.shape
×
51
            npt.assert_allclose(func_output, dense_input)
×
52

53
        # Check function behavior with sparse matrix inputs
54
        sparse_output = func(sparse_input)
×
55
        assert sps.issparse(sparse_output)
×
56
        assert sparse_output.shape == sparse_input.shape
×
57
        npt.assert_allclose(sparse_output.toarray(),
×
58
                            sparse_input.toarray())
59

60
        # Check function behavior when using masked array inputs and pandas
61
        # objects with missing data
62
        for input_value in [masked_array_input, missing_pandas_input]:
×
63
            func_output = func(input_value)
×
64
            assert isinstance(func_output, ma.core.MaskedArray)
×
65
            assert func_output.shape == input_value.shape
×
66
            npt.assert_allclose(func_output, masked_array_input)
×
67

68
        # Check function behavior with Theano graph variable
69
        theano_output = func(theano_graph_input)
×
70
        assert isinstance(theano_output, theano.gof.graph.Variable)
×
71
        assert theano_output.owner.inputs[0].name == input_name
×
72

73
        # Check function behavior with generator data
74
        generator_output = func(square_generator)
×
75

76
        # Output is wrapped with `pm.floatX`, and this unwraps
77
        wrapped = generator_output.owner.inputs[0]
×
78
        # Make sure the returned object has .set_gen and .set_default methods
79
        assert hasattr(wrapped, "set_gen")
×
80
        assert hasattr(wrapped, "set_default")
×
81
        # Make sure the returned object is a Theano TensorVariable
82
        assert isinstance(wrapped, tt.TensorVariable)
×
83

84
    def test_as_tensor(self):
×
85
        """
86
        Check returned values for `data` given known inputs to `as_tensor()`.
87

88
        Note that ndarrays should return a TensorConstant and sparse inputs
89
        should return a Sparse Theano object.
90
        """
91
        # Create the various inputs to the function
92
        input_name = 'testing_inputs'
×
93
        sparse_input = sps.csr_matrix(np.eye(3))
×
94
        dense_input = np.arange(9).reshape((3, 3))
×
95
        masked_array_input = ma.array(dense_input,
×
96
                                      mask=(np.mod(dense_input, 2) == 0))
97

98
        # Create a fake model and fake distribution to be used for the test
99
        fake_model = pm.Model()
×
100
        with fake_model:
×
101
            fake_distribution = pm.Normal.dist(mu=0, sd=1)
×
102
            # Create the testval attribute simply for the sake of model testing
103
            fake_distribution.testval = None
×
104

105
        # Alias the function to be tested
106
        func = pm.model.as_tensor
×
107

108
        # Check function behavior using the various inputs
109
        dense_output = func(dense_input,
×
110
                            input_name,
111
                            fake_model,
112
                            fake_distribution)
113
        sparse_output = func(sparse_input,
×
114
                             input_name,
115
                             fake_model,
116
                             fake_distribution)
117
        masked_output = func(masked_array_input,
×
118
                             input_name,
119
                             fake_model,
120
                             fake_distribution)
121

122
        # Ensure that the missing values are appropriately set to None
123
        for func_output in [dense_output, sparse_output]:
×
124
            assert func_output.missing_values is None
×
125

126
        # Ensure that the Theano variable names are correctly set.
127
        # Note that the output for masked inputs do not have their names set
128
        # to the passed value.
129
        for func_output in [dense_output, sparse_output]:
×
130
            assert func_output.name == input_name
×
131

132
        # Ensure the that returned functions are all of the correct type
133
        assert isinstance(dense_output, tt.TensorConstant)
×
134
        assert sparse.basic._is_sparse_variable(sparse_output)
×
135

136
        # Masked output is something weird. Just ensure it has missing values
137
        # self.assertIsInstance(masked_output, tt.TensorConstant)
138
        assert masked_output.missing_values is not None
×
139

140
        return None
×
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