Beispiel #1
0
def const_potential(f):
    '''
    const_potential(f) yields f if f is a constant potential function; if f is a constant, yields
      a potential function that always yields f; otherwise raises an error.
    '''
    if is_const_potential(f): return f
    elif pimms.is_array(f, 'number'): return PotentialConstant(f)
    else: raise ValueError('Could not convert given value to potential constant: %s' % f)
Beispiel #2
0
def const(f):
    '''
    const(f) yields f.c if f is a constant potential function; if f is a constant, it yields f or
      the equivalent numpy array object; if f is a potential function that is not const, or is not
      a valid potential function constant, yields None.
    '''
    if is_const_potential(f): return f.c
    elif pimms.is_array(f, 'number'):
        if sps.issparse(f): return f
        else: return np.asarray(f)
    else: return None
Beispiel #3
0
 def entry_meta_data(mds):
     '''
     lblidx.entry_meta_data is lists of meta-data maps for each of the labels in the given label
       index object.
     '''
     if mds is None: return None
     if is_dataframe(mds):
         mds = {k:mds[k].values for k in mds.colums}
     elif pimms.is_map(mds):
         ks = list(mds.keys())
         mds = [{k:v for (k,v) in zip(ks,vs)} for vs in np.transpose(list(mds.values()))]
     elif not pimms.is_array(mds) or not all(pimms.is_map(u) for u in mds):
         raise ValueError('unbalanced or non-map entry meta-data')
     return pimms.persist(mds)
Beispiel #4
0
def to_potential(f):
    '''
    to_potential(f) yields f if f is a potential function; if f is not, but f can be converted to
      a potential function, that conversion is performed then the result is yielded.
    to_potential(Ellipsis) yields a potential function whose output is simply its input (i.e., the
      identity function).
    to_potential(None) is equivalent to to_potential(0).

    The following can be converted into potential functions:
      * Anything for which pimms.is_array(x, 'number') yields True (i.e., arrays of constants).
      * Any tuple (g, h) where g(x) yields a potential value and h(x) yields a jacobian matrix for
        the parameter vector x.
    '''
    if   is_potential(f): return f
    elif f is Ellipsis:   return identity
    elif pimms.is_array(f, 'number'): return const_potential(f)
    elif isinstance(f, tuple) and len(f) == 2: return PotentialLambda(f[0], f[1])
    else: raise ValueError('Could not convert object to potential function')
Beispiel #5
0
def is_image_array(arr):
    '''
    is_image_array(arr) yields True if arr is a valid image array and False otherwise; in order to
      be a valid array, it must be a 1D, 2D, 3D or 4D array.
    '''
    return pimms.is_array(arr, None, (1, 2, 3, 4))
Beispiel #6
0
    def test_predicates(self):
        '''
        test_predicates ensures that the various pimms functions of the form is_<type>(obj) are
        working properly.
        '''
        # some arbitrary values to use in testing
        qr = pimms.quant([1.5, 3.3, 9.5, 10.4, 6.2, 0.1], 'mm')
        qi = pimms.quant([1, 3, 9, 10, 6, 0], 'seconds')
        mr = np.random.rand(10, 3)
        vi = np.random.randint(0, 5000, size=12)
        sr = np.array(10.0)
        si = np.array(2) * pimms.units.deg
        l = [1, 2.0, 'abc']
        lx = [[1, 1], [2.0, 2.0], [4, 7.7]]
        u = u'a unicode string of stuff'
        b = b'a byte string of stuff'
        f0 = lambda: np.linspace(0, 100, 117)
        f1 = lambda x: x**2 + 1
        d = {
            'a': 12,
            'b': None,
            'c': f0,
            'd': f1,
            'e': lambda: 'some string',
            'f': lambda: None
        }
        pm = pyr.pmap(d)
        lm = pimms.lazy_map(d)

        # a function for testing predicates
        def tpred(p, tvals, fvals):
            for s in tvals:
                self.assertTrue(p(s))
            for s in fvals:
                self.assertFalse(p(s))

        # Map types
        tpred(pimms.is_lazy_map, [lm],
              [qr, qi, mr, vi, sr, si, l, u, b, f0, f1, d, pm])
        tpred(pimms.is_map, [lm, d, pm],
              [qr, qi, mr, vi, sr, si, l, u, b, f0, f1])
        tpred(pimms.is_pmap, [lm, pm],
              [qr, qi, mr, vi, sr, si, l, u, b, f0, f1, d])
        # Numpy types require a little attention due to their optional arguments and the
        # complexities of the type relationships
        tpred(pimms.is_nparray, [qr, qi, mr, vi, sr],
              [l, lx, u, b, f0, f1, d, pm, lm])
        self.assertTrue(pimms.is_nparray(qr, 'real'))
        self.assertTrue(pimms.is_nparray(qr, 'any', 1))
        self.assertFalse(pimms.is_nparray(qr, 'any', 2))
        self.assertFalse(pimms.is_nparray(qi, 'string'))
        self.assertTrue(pimms.is_nparray(qi, ('real', 'int'), (1, 3)))
        self.assertFalse(pimms.is_nparray(qi, ('real', 'int'), 2))
        self.assertFalse(
            pimms.is_nparray(qr, ('string', 'bool', 'bytes'), (2, 3)))
        self.assertTrue(pimms.is_nparray(mr, None, 2))
        self.assertFalse(pimms.is_nparray(mr, None, 1))
        self.assertFalse(pimms.is_nparray(vi, 'int', 2))
        tpred(pimms.is_npscalar, [sr],
              [qr, qi, mr, vi, l, lx, u, b, f0, f1, d, pm, lm])
        self.assertTrue(pimms.is_npscalar(np.array(12.0), 'real'))
        self.assertFalse(pimms.is_npscalar(np.array(12.0), 'string'))
        self.assertTrue(pimms.is_npscalar(np.array(12.0), ('real', 'complex')))
        tpred(pimms.is_npmatrix, [mr, pimms.quant(mr, 'm/s')],
              [sr, si, qr, qi, vi, l, lx, u, b, f0, f1, d, pm, lm])
        self.assertTrue(pimms.is_npmatrix(mr, ('int', 'real', 'string')))
        self.assertTrue(pimms.is_npmatrix(mr, 'number'))
        self.assertFalse(pimms.is_npmatrix(mr, ('bool', 'string')))
        tpred(pimms.is_npvector, [qr, qi, vi, vi * pimms.units.mol, qr, qi],
              [sr, si, mr, l, lx, u, b, f0, f1, d, pm, lm])
        self.assertTrue(pimms.is_npvector(vi, 'real'))
        self.assertTrue(pimms.is_npvector(qi, 'int'))
        self.assertFalse(pimms.is_npvector(qr, ('bool', 'string')))
        self.assertTrue(pimms.is_npvalue('abc', 'string'))
        self.assertTrue(pimms.is_npvalue(u'abc', ('unicode', 'real')))
        self.assertFalse(pimms.is_npvalue(np.array(5.6), ('unicode', 'real')))
        self.assertFalse(pimms.is_npvalue(np.array(5.6), ('unicode', 'bool')))
        self.assertFalse(pimms.is_npvalue(np.array([5.6]),
                                          ('unicode', 'real')))
        # Also the un-nump'ified versions
        tpred(pimms.is_array,
              [qr, qi, vi, sr, si, mr, qr, qi, l, lx, u, b, f0, f1],
              [d, pm, lm])
        self.assertTrue(pimms.is_array(qr, 'real'))
        self.assertTrue(pimms.is_array(qr, 'any', 1))
        self.assertTrue(pimms.is_array(qr, 'any', 1))
        self.assertFalse(pimms.is_array(qi, 'string'))
        self.assertTrue(pimms.is_array(qi, ('real', 'int'), (1, 3)))
        self.assertFalse(pimms.is_array(qi, ('real', 'int'), 2))
        self.assertFalse(
            pimms.is_array(qr, ('string', 'bool', 'bytes'), (2, 3)))
        self.assertTrue(pimms.is_array(mr, None, 2))
        self.assertFalse(pimms.is_array(mr, None, 1))
        self.assertFalse(pimms.is_array(vi, 'int', 2))
        self.assertFalse(pimms.is_array(l, 'number', 1))
        self.assertTrue(pimms.is_array(lx, 'any', (1, 2)))
        tpred(pimms.is_scalar, [u, b, f0, f1, sr, si],
              [qr, qi, vi, mr, l, d, pm, lm, lx])
        tpred(pimms.is_int, [vi[0], si, 1, 10],
              [u, b, f0, f1, d, pm, lm, sr, qr, mr])
        tpred(pimms.is_real, [vi[0], si, 1, 10, sr],
              [4j, u, b, f0, f1, d, pm, lm, lx, mr, qr])
        tpred(pimms.is_complex, [vi[0], si, 1, 10, sr, 4j],
              [u, b, f0, f1, d, pm, lm, lx, mr, qr])
        tpred(pimms.is_number, [vi[0], si, 1, 10, sr],
              [u, b, f0, f1, d, pm, lm, lx, mr, qr])
        tpred(pimms.is_str, ['abc'],
              [vi, si, 1, 10, sr, qr, f0, f1, d, pm, lm, lx, mr])
        tpred(pimms.is_class, [str, int],
              [vi, si, 1, 10, sr, qr, u, b, f0, f1, d, pm, lm, lx, mr])
        tpred(pimms.is_quantity, [qr, qi, si],
              [vi, 10, sr, u, b, f0, f1, d, pm, lm, lx, mr])
        tpred(pimms.is_unit, ('seconds', 's', 'mm', 'deg', pimms.units.seconds,
                              pimms.units.s, pimms.units.mm, pimms.units.deg),
              (1, 10.0, np.asarray([10]), None, 'nonunitstring', qr))
        self.assertTrue(pimms.is_nparray(mr, np.inexact))
        self.assertFalse(pimms.is_nparray(vi, np.inexact))