In [34]:
import hashlib, struct
import cPickle as pickle
import numpy as np
from pisa.utils.jsons import dumps
from pisa.utils.hash import hash_obj

In [35]:
len(hashlib.md5(dumps([0,1,2])).hexdigest())


Out[35]:
32

Old version that was developed here (now probably out of sync with official version in PISA...):

def hash_obj(obj):
    hash_val, = struct.unpack('<q', hashlib.md5(
            pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
        ).digest()[:8])
    return hash_val

In [38]:
print hash_obj([0, 1, 2])


5808667850487776704

In [39]:
bits = 64*2
n_elements = 200
np.log10(2*2**bits/(n_elements*(n_elements-1)))


Out[39]:
34.232986368579887

Testing out a container class but that:

  • c[0] = 1 assigns 1 to the object that lives at position 0
  • c['aeff_scale'] = 1 has the same effect
  • c returns values for all objects in c
  • c.values does the same
  • c.values[2:8] = [6, 1, 8, 9, 5] assigns those values to the values properties of the third through seventh entities in c
  • c.priors returns priors for all objects in c

In [40]:
l = list(['zero', 'one', 'two'])
l.__getitem__(0)


Out[40]:
'zero'

In [41]:
[x for x in l.__iter__()]


Out[41]:
['zero', 'one', 'two']

In [42]:
l.__setitem__(1, 1)
print l


['zero', 1, 'two']

In [43]:
l.__getslice__(1,3)


Out[43]:
[1, 'two']

In [44]:
l.__setslice__(1, 3, ('b', 'c'))
print l


['zero', 'b', 'c']

In [45]:
from functools import total_ordering
from collections import OrderedDict

In [46]:
from pisa.core.param import Param, ParamSet

In [47]:
p0 = Param(name='c', value=1.5, prior=None, range=[1,2],
           is_fixed=False, is_discrete=False, tex=r'\int{\rm c}')
p1 = Param(name='a', value=2.5, prior=None, range=[1,5],
           is_fixed=False, is_discrete=False, tex=r'{\rm a}')
p2 = Param(name='b', value=1.5, prior=None, range=[1,2],
           is_fixed=False, is_discrete=False, tex=r'{\rm b}')
c = ParamSet(p0, p1, p2)
print c.values
print c[0]
c[0].value = 1
print c.values
print c.tex


(2.5, 1.5, 1.5)
a=2.5; prior=None, range=(1, 5), is_fixed=False, is_discrete=False; help=""
(1, 1.5, 1.5)
{\rm a}=1,\,{\rm b}=1.5,\,\int{\rm c}=1.5

In [48]:
c.values = [3, 2, 1]
print c.values
print c.values[0]
print c[0].value


(3, 2, 1)
3
3

In [49]:
print 'priors:', c.priors
print 'names:', c.names


priors: (None, None, None)
names: ('a', 'b', 'c')

In [50]:
print c['a']
print c['a'].value
c['a'].value = 33
print c['a'].value


a=3; prior=None, range=(1, 5), is_fixed=False, is_discrete=False; help=""
3
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-50-39f1ce1c49d9> in <module>()
      1 print c['a']
      2 print c['a'].value
----> 3 c['a'].value = 33
      4 print c['a'].value

/home/justin/cowen/code/pisa/pisa/core/param.pyc in __setattr__(self, attr, val)
    111         if attr not in self._slots:
    112             raise AttributeError('Invalid attribute: %s' % (attr,))
--> 113         object.__setattr__(self, attr, val)
    114 
    115     def __str__(self):

/home/justin/cowen/code/pisa/pisa/core/param.pyc in value(self, val)
    139                         'Passed values must have units if the param has units'
    140                 val = val.to(self._value.units)
--> 141             self.validate_value(val)
    142         self._value = val
    143         if hasattr(self._value, 'units'):

/home/justin/cowen/code/pisa/pisa/core/param.pyc in validate_value(self, value)
    126                 assert value >= min(self.range) and \
    127                         value <= max(self.range), \
--> 128                         'value=' + str(value) + '; range=' + str(self.range)
    129 
    130     @property

AssertionError: value=33; range=(1, 5)

In [51]:
print c['c'].is_fixed
c['c'].is_fixed = True
print c['c'].is_fixed
print c.are_fixed
c.fix('c')
print c.are_fixed
c.unfix('a')
print c.are_fixed
c.unfix([0,1,2])
print c.are_fixed


False
True
(False, False, True)
(False, False, True)
(False, False, True)
(False, False, False)

In [52]:
fixed_params = c.fixed
print fixed_params.are_fixed
free_params = c.free
print free_params.are_fixed
print c.free.values


()
(False, False, False)
(3, 2, 1)

In [53]:
print c.values_hash
print c.fixed.values_hash
print c.free.values_hash


6727196129584638508
-1196596804347985876
6727196129584638508

In [54]:
print c[0].state
print c.state_hash
print c.fixed.state_hash
print c.free.state_hash


OrderedDict([('name', 'a'), ('_value', 3), ('prior', None), ('range', (1, 5)), ('is_fixed', False), ('is_discrete', False), ('nominal_value', 2.5), ('tex', '{\\rm a}=3'), ('help', '')])
-2877000503915889431
-1196596804347985876
-2877000503915889431

In [55]:
print 'fixed:', c.fixed.names
print 'fixed, discrete:', c.fixed.discrete.names
print 'fixed, continuous:', c.fixed.continuous.names
print 'free:', c.free.names
print 'free, discrete:', c.free.discrete.names
print 'free, continuous:', c.free.continuous.names
print 'continuous, free:', c.continuous.free.names
print 'free, continuous hash:', c.free.continuous.values_hash
print 'continuous, free hash:', c.continuous.free.values_hash


fixed: ()
fixed, discrete: ()
fixed, continuous: ()
free: ('a', 'b', 'c')
free, discrete: ()
free, continuous: ('a', 'b', 'c')
continuous, free: ('a', 'b', 'c')
free, continuous hash: 6727196129584638508
continuous, free hash: 6727196129584638508

In [56]:
print c.b.prior
print c.priors_llh


None
0