Exemple #1
0
 def buildprior(self, prior, mopt=None, extend=False):
     nprior = gv.BufferDict()
     if mopt is None:
         for k in [self.a, self.b]:
             k = gv.dictkey(prior, k)
             nprior[k] = prior[k]
     else:
         k = gv.dictkey(prior, self.a)
         nprior[k] = prior[k]
     self.mopt = mopt
     # use self.mopt to marginalize fitfcn
     return nprior
Exemple #2
0
 def buildprior(self, prior, mopt=None, extend=False):
     nprior = gv.BufferDict()
     k = gv.dictkey(prior, self.a)
     nprior[k] = prior[k]
     return nprior
Exemple #3
0
    def _buildprior(self, prior, mopt=None, nterm=None):
        """
        Based on corrfitter.py:698-804. Skips building the fixed parameters.
        """
        if nterm is None:
            nterm = mopt
        nterm = _parse_param(nterm, None)

        def resize_sym(Vii, ntermi):
            # N = size of Vii; ntermi is new max. dimension
            N = int(numpy.round((((8. * len(Vii) + 1.)**0.5 - 1.) / 2.)))
            if ntermi is None or N == ntermi:
                return Vii
            ans = []
            iterV = iter(Vii)
            for i in range(N):
                for j in range(i, N):
                    v = next(iterV)
                    if j < ntermi:
                        ans.append(v)
            return numpy.array(ans)

        ans = _gvar.BufferDict()

        # i,j range from n to o
        for i in range(2):
            for j in range(2):
                vij = self.V[i][j]
                if vij is None:
                    continue
                vij = _gvar.dictkey(prior, vij)
                if i == j and self.symmetric_V:
                    ans[vij] = (prior[vij] if nterm[i] is None else resize_sym(
                        prior[vij], nterm[i]))
                else:
                    if self.transpose_V:
                        ans[vij] = prior[vij][None:nterm[j], None:nterm[i]]
                    else:
                        ans[vij] = prior[vij][None:nterm[i], None:nterm[j]]

        # verify dimensions
        for ai, dEai in zip(self.a, self.dEa):
            if ai is None:
                continue
            ai, dEai = _gvar.get_dictkeys(self.pfixed, [ai, dEai])
            if len(self.pfixed[ai]) != len(self.pfixed[dEai]):
                raise ValueError('length mismatch between a and dEa for ' +
                                 str(self.datatag))
        for bj, dEbj in zip(self.b, self.dEb):
            if bj is None or dEbj is None:
                continue
            bj, dEbj = _gvar.get_dictkeys(self.pfixed, [bj, dEbj])
            if len(self.pfixed[bj]) != len(self.pfixed[dEbj]):
                raise ValueError('length mismatch between b and dEb for ' +
                                 str(self.datatag))
        for i in range(2):
            for j in range(2):
                Vij = self.V[i][j]
                if Vij is None:
                    continue
                ai, bj = _gvar.get_dictkeys(self.pfixed,
                                            [self.a[i], self.b[j]])
                Vij = _gvar.dictkey(prior, Vij)
                if i == j and self.symmetric_V:
                    N = self.pfixed[ai].shape[0]
                    if self.pfixed[bj].shape[0] != N:
                        raise ValueError(
                            'length mismatch between a, b, and V for ' +
                            str(self.datatag))
                    if len(ans[Vij].shape) != 1:
                        raise ValueError(
                            'symmetric_V=True => Vnn, Voo = 1-d arrays for ' +
                            str(self.datatag))
                    if ans[Vij].shape[0] != (N * (N + 1)) / 2:
                        raise ValueError(
                            'length mismatch between a, b, and V for ' +
                            str(self.datatag))
                else:
                    ai, bj = _gvar.get_dictkeys(self.pfixed,
                                                [self.a[i], self.b[j]])
                    Vij = _gvar.dictkey(prior, Vij)
                    Vij_shape = (ans[Vij].shape[::-1]
                                 if self.transpose_V else ans[Vij].shape)
                    if self.pfixed[ai].shape[0] != Vij_shape[0]:
                        raise ValueError(
                            'length mismatch between a and V for ' +
                            str(self.datatag))
                    elif self.pfixed[bj].shape[0] != Vij_shape[1]:
                        raise ValueError(
                            'length mismatch between b and V for ' +
                            str(self.datatag))
        return ans
Exemple #4
0
 def test_get_prior_keys(self):
     prior = gv.BufferDict({'log(a)': 1., 'b': 2.})
     self.assertEqual(gv.get_dictkeys(prior, ['a', 'b', 'log(a)']),
                      ['log(a)', 'b', 'log(a)'])
     self.assertEqual([gv.dictkey(prior, k) for k in ['a', 'b', 'log(a)']],
                      ['log(a)', 'b', 'log(a)'])
Exemple #5
0
    def test_extension_mapping(self):
        " BufferDict extension and mapping properties  "
        p = BufferDict()
        p['a'] = 1.
        p['b'] = [2., 3.]
        p['log(c)'] = 0.
        p['sqrt(d)'] = [5., 6.]
        p['erfinv(e)'] = [[33.]]
        newp = BufferDict(p)
        for i in range(2):
            for k in p:
                assert np.all(p[k] == newp[k])
            assert newp['c'] == np.exp(newp['log(c)'])
            assert np.all(newp['d'] == np.square(newp['sqrt(d)']))
            assert np.all(newp['e'] == gv.erf(newp['erfinv(e)']))
            assert np.all(p.buf == newp.buf)
            p.buf[:] = [10., 20., 30., 1., 2., 3., 4.]
            newp.buf = np.array(p.buf.tolist())
        self.assertEqual(gv.get_dictkeys(p, ['c', 'a', 'log(c)', 'e', 'd']),
                         ['log(c)', 'a', 'log(c)', 'erfinv(e)', 'sqrt(d)'])
        self.assertEqual(
            [gv.dictkey(p, k) for k in ['c', 'a', 'log(c)', 'e', 'd']],
            ['log(c)', 'a', 'log(c)', 'erfinv(e)', 'sqrt(d)'])
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'a'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'b'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'c'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'd'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'e'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'log(c)'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'sqrt(d)'))
        self.assertTrue(gv.BufferDict.has_dictkey(p, 'erfinv(e)'))
        self.assertTrue(not gv.BufferDict.has_dictkey(p, 'log(a)'))
        self.assertTrue(not gv.BufferDict.has_dictkey(p, 'sqrt(b)'))
        self.assertEqual(list(p), ['a', 'b', 'log(c)', 'sqrt(d)', 'erfinv(e)'])
        np.testing.assert_equal(list(p.values()),
                                [10.0, [20., 30.], 1.0, [2., 3.], [[4.]]])
        self.assertEqual(p.get('c'), p['c'])

        # tracking?
        self.assertAlmostEqual(p['c'], np.exp(1))
        self.assertAlmostEqual(p['log(c)'], 1.)
        p['log(c)'] = 2.
        self.assertAlmostEqual(p['c'], np.exp(2))
        self.assertAlmostEqual(p['log(c)'], 2.)
        p['a'] = 12.
        self.assertAlmostEqual(p['c'], np.exp(2))
        self.assertAlmostEqual(p['log(c)'], 2.)
        self.assertEqual(
            list(p),
            ['a', 'b', 'log(c)', 'sqrt(d)', 'erfinv(e)'],
        )

        # the rest is not so important
        # trim redundant keys
        oldp = trim_redundant_keys(newp)
        assert 'c' not in oldp
        assert 'd' not in oldp
        assert np.all(oldp.buf == newp.buf)

        # nonredundant keys
        assert set(nonredundant_keys(newp.keys())) == set(p.keys())

        # stripkey
        for ks, f, k in [
            ('aa', np.exp, 'log(aa)'),
            ('aa', np.square, 'sqrt(aa)'),
        ]:
            assert (ks, f) == gv._bufferdict._stripkey(k)

        # addparentheses
        pvar = BufferDict()
        pvar['a'] = p['a']
        pvar['b'] = p['b']
        pvar['logc'] = p['log(c)']
        pvar['sqrtd'] = p['sqrt(d)']
        pvar['erfinv(e)'] = p['erfinv(e)']
        pvar = add_parameter_parentheses(pvar)
        for k in p:
            assert k in pvar
            assert np.all(p[k] == pvar[k])
        for k in pvar:
            assert k in p
        pvar = add_parameter_parentheses(pvar)
        for k in p:
            assert k in pvar
            assert np.all(p[k] == pvar[k])
        for k in pvar:
            assert k in p
        pvar['log(c(23))'] = 1.2
        pvar = BufferDict(pvar)
        assert 'c(23)' not in pvar
        assert 'log(c(23))' in pvar
        self.assertAlmostEqual(gv.exp(pvar['log(c(23))']), pvar['c(23)'])