Changed refereences to iteritems() to items() for Py3 compat

This commit is contained in:
Mike Croucher 2015-03-02 19:39:50 +00:00
parent 153a110a1d
commit 6aca7c2765
3 changed files with 70 additions and 32 deletions

View file

@ -63,15 +63,14 @@ class ParameterIndexOperations(object):
def __init__(self, constraints=None): def __init__(self, constraints=None):
self._properties = IntArrayDict() self._properties = IntArrayDict()
if constraints is not None: if constraints is not None:
for t, i in constraints.iteritems(): #python 3 fix
#for t, i in constraints.iteritems():
for t, i in constraints.items():
self.add(t, i) self.add(t, i)
def iteritems(self): #iteritems has gone in python 3
try: #def iteritems(self):
return self._properties.iteritems() # return self._properties.iteritems()
except AttributeError:
#Changed this from iteritems to items for Py3 compatibility. It didn't break the test suite.
return self._properties.items()
def items(self): def items(self):
return self._properties.items() return self._properties.items()
@ -159,14 +158,18 @@ class ParameterIndexOperations(object):
return numpy.array([]).astype(int) return numpy.array([]).astype(int)
def update(self, parameter_index_view, offset=0): def update(self, parameter_index_view, offset=0):
for i, v in parameter_index_view.iteritems(): #py3 fix
#for i, v in parameter_index_view.iteritems():
for i, v in parameter_index_view.items():
self.add(i, v+offset) self.add(i, v+offset)
def copy(self): def copy(self):
return self.__deepcopy__(None) return self.__deepcopy__(None)
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
return ParameterIndexOperations(dict(self.iteritems())) #py3 fix
#return ParameterIndexOperations(dict(self.iteritems()))
return ParameterIndexOperations(dict(self.items()))
def __getitem__(self, prop): def __getitem__(self, prop):
return self._properties[prop] return self._properties[prop]
@ -204,22 +207,25 @@ class ParameterIndexOperationsView(object):
def _filter_index(self, ind): def _filter_index(self, ind):
return ind[(ind >= self._offset) * (ind < (self._offset + self._size))] - self._offset return ind[(ind >= self._offset) * (ind < (self._offset + self._size))] - self._offset
#iteritems has gone in python 3. It has been renamed items()
def iteritems(self): def items(self):
for i, ind in self._param_index_ops.iteritems(): for i, ind in self._param_index_ops.items():
ind2 = self._filter_index(ind) ind2 = self._filter_index(ind)
if ind2.size > 0: if ind2.size > 0:
yield i, ind2 yield i, ind2
def items(self): #Python 3 items() is now implemented as per py2 iteritems
return [[i,v] for i,v in self.iteritems()] #def items(self):
# return [[i,v] for i,v in self.iteritems()]
def properties(self): def properties(self):
return [i for i in self.iterproperties()] return [i for i in self.iterproperties()]
def iterproperties(self): def iterproperties(self):
for i, _ in self.iteritems(): #py3 fix
#for i, _ in self.iteritems():
for i, _ in self.items():
yield i yield i
@ -239,7 +245,9 @@ class ParameterIndexOperationsView(object):
def iterindices(self): def iterindices(self):
for _, ind in self.iteritems(): #py3 fix
#for _, ind in self.iteritems():
for _, ind in self.items():
yield ind yield ind
@ -295,10 +303,14 @@ class ParameterIndexOperationsView(object):
def __str__(self, *args, **kwargs): def __str__(self, *args, **kwargs):
import pprint import pprint
return pprint.pformat(dict(self.iteritems())) #py3 fixes
#return pprint.pformat(dict(self.iteritems()))
return pprint.pformat(dict(self.items()))
def update(self, parameter_index_view, offset=0): def update(self, parameter_index_view, offset=0):
for i, v in parameter_index_view.iteritems(): #py3 fixes
#for i, v in parameter_index_view.iteritems():
for i, v in parameter_index_view.items():
self.add(i, v+offset) self.add(i, v+offset)
@ -306,6 +318,8 @@ class ParameterIndexOperationsView(object):
return self.__deepcopy__(None) return self.__deepcopy__(None)
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
return ParameterIndexOperations(dict(self.iteritems())) #py3 fix
#return ParameterIndexOperations(dict(self.iteritems()))
return ParameterIndexOperations(dict(self.items()))
pass pass

View file

@ -207,10 +207,14 @@ class Param(Parameterizable, ObsAr):
return 0 return 0
@property @property
def _constraints_str(self): def _constraints_str(self):
return [' '.join(map(lambda c: str(c[0]) if c[1].size == self._realsize_ else "{" + str(c[0]) + "}", self.constraints.iteritems()))] #py3 fix
#return [' '.join(map(lambda c: str(c[0]) if c[1].size == self._realsize_ else "{" + str(c[0]) + "}", self.constraints.iteritems()))]
return [' '.join(map(lambda c: str(c[0]) if c[1].size == self._realsize_ else "{" + str(c[0]) + "}", self.constraints.items()))]
@property @property
def _priors_str(self): def _priors_str(self):
return [' '.join(map(lambda c: str(c[0]) if c[1].size == self._realsize_ else "{" + str(c[0]) + "}", self.priors.iteritems()))] #py3 fix
#return [' '.join(map(lambda c: str(c[0]) if c[1].size == self._realsize_ else "{" + str(c[0]) + "}", self.priors.iteritems()))]
return [' '.join(map(lambda c: str(c[0]) if c[1].size == self._realsize_ else "{" + str(c[0]) + "}", self.priors.items()))]
@property @property
def _ties_str(self): def _ties_str(self):
return [''] return ['']
@ -336,7 +340,9 @@ class ParamConcatenation(object):
level += 1 level += 1
parent = parent._parent_ parent = parent._parent_
import operator import operator
self.parents = map(lambda x: x[0], sorted(parents.iteritems(), key=operator.itemgetter(1))) #py3 fix
#self.parents = map(lambda x: x[0], sorted(parents.iteritems(), key=operator.itemgetter(1)))
self.parents = map(lambda x: x[0], sorted(parents.tems(), key=operator.itemgetter(1)))
#=========================================================================== #===========================================================================
# Get/set items, enable broadcasting # Get/set items, enable broadcasting
#=========================================================================== #===========================================================================

View file

@ -164,7 +164,9 @@ class Pickleable(object):
'_Cacher_wrap__cachers', # never pickle cachers '_Cacher_wrap__cachers', # never pickle cachers
] ]
dc = dict() dc = dict()
for k,v in self.__dict__.iteritems(): #py3 fix
#for k,v in self.__dict__.iteritems():
for k,v in self.__dict__.items():
if k not in ignore_list: if k not in ignore_list:
dc[k] = v dc[k] = v
return dc return dc
@ -427,7 +429,9 @@ class Indexable(Nameable, Updateable):
"""evaluate the prior""" """evaluate the prior"""
if self.priors.size > 0: if self.priors.size > 0:
x = self.param_array x = self.param_array
return reduce(lambda a, b: a + b, (p.lnpdf(x[ind]).sum() for p, ind in self.priors.iteritems()), 0) #py3 fix
#return reduce(lambda a, b: a + b, (p.lnpdf(x[ind]).sum() for p, ind in self.priors.iteritems()), 0)
return reduce(lambda a, b: a + b, (p.lnpdf(x[ind]).sum() for p, ind in self.priors.items()), 0)
return 0. return 0.
def _log_prior_gradients(self): def _log_prior_gradients(self):
@ -435,7 +439,9 @@ class Indexable(Nameable, Updateable):
if self.priors.size > 0: if self.priors.size > 0:
x = self.param_array x = self.param_array
ret = np.zeros(x.size) ret = np.zeros(x.size)
[np.put(ret, ind, p.lnpdf_grad(x[ind])) for p, ind in self.priors.iteritems()] #py3 fix
#[np.put(ret, ind, p.lnpdf_grad(x[ind])) for p, ind in self.priors.iteritems()]
[np.put(ret, ind, p.lnpdf_grad(x[ind])) for p, ind in self.priors.items()]
return ret return ret
return 0. return 0.
@ -613,7 +619,9 @@ class OptimizationHandlable(Indexable):
if not self._optimizer_copy_transformed: if not self._optimizer_copy_transformed:
self._optimizer_copy_.flat = self.param_array.flat self._optimizer_copy_.flat = self.param_array.flat
[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__] #py3 fix
#[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__]
[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.items() if c != __fixed__]
if self.has_parent() and (self.constraints[__fixed__].size != 0 or self._has_ties()): if self.has_parent() and (self.constraints[__fixed__].size != 0 or self._has_ties()):
fixes = np.ones(self.size).astype(bool) fixes = np.ones(self.size).astype(bool)
fixes[self.constraints[__fixed__]] = FIXED fixes[self.constraints[__fixed__]] = FIXED
@ -642,11 +650,15 @@ class OptimizationHandlable(Indexable):
if f is None: if f is None:
self.param_array.flat = p self.param_array.flat = p
[np.put(self.param_array, ind, c.f(self.param_array.flat[ind])) [np.put(self.param_array, ind, c.f(self.param_array.flat[ind]))
for c, ind in self.constraints.iteritems() if c != __fixed__] #py3 fix
#for c, ind in self.constraints.iteritems() if c != __fixed__]
for c, ind in self.constraints.items() if c != __fixed__]
else: else:
self.param_array.flat[f] = p self.param_array.flat[f] = p
[np.put(self.param_array, ind[f[ind]], c.f(self.param_array.flat[ind[f[ind]]])) [np.put(self.param_array, ind[f[ind]], c.f(self.param_array.flat[ind[f[ind]]]))
for c, ind in self.constraints.iteritems() if c != __fixed__] #py3 fix
#for c, ind in self.constraints.iteritems() if c != __fixed__]
for c, ind in self.constraints.items() if c != __fixed__]
#self._highest_parent_.tie.propagate_val() #self._highest_parent_.tie.propagate_val()
self._optimizer_copy_transformed = False self._optimizer_copy_transformed = False
@ -681,7 +693,9 @@ class OptimizationHandlable(Indexable):
constraint to it. constraint to it.
""" """
self._highest_parent_.tie.collate_gradient() self._highest_parent_.tie.collate_gradient()
[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__] #py3 fix
#[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.items() if c != __fixed__]
if self._has_fixes(): return g[self._fixes_] if self._has_fixes(): return g[self._fixes_]
return g return g
@ -691,6 +705,8 @@ class OptimizationHandlable(Indexable):
constraint to it. constraint to it.
""" """
self._highest_parent_.tie.collate_gradient() self._highest_parent_.tie.collate_gradient()
#py3 fix
#[np.put(g, i, c.gradfactor_non_natural(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
[np.put(g, i, c.gradfactor_non_natural(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__] [np.put(g, i, c.gradfactor_non_natural(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__]
if self._has_fixes(): return g[self._fixes_] if self._has_fixes(): return g[self._fixes_]
return g return g
@ -751,7 +767,9 @@ class OptimizationHandlable(Indexable):
self.optimizer_array = x # makes sure all of the tied parameters get the same init (since there's only one prior object...) self.optimizer_array = x # makes sure all of the tied parameters get the same init (since there's only one prior object...)
# now draw from prior where possible # now draw from prior where possible
x = self.param_array.copy() x = self.param_array.copy()
[np.put(x, ind, p.rvs(ind.size)) for p, ind in self.priors.iteritems() if not p is None] #Py3 fix
#[np.put(x, ind, p.rvs(ind.size)) for p, ind in self.priors.iteritems() if not p is None]
[np.put(x, ind, p.rvs(ind.size)) for p, ind in self.priors.items() if not p is None]
unfixlist = np.ones((self.size,),dtype=np.bool) unfixlist = np.ones((self.size,),dtype=np.bool)
unfixlist[self.constraints[__fixed__]] = False unfixlist[self.constraints[__fixed__]] = False
self.param_array.flat[unfixlist] = x.view(np.ndarray).ravel()[unfixlist] self.param_array.flat[unfixlist] = x.view(np.ndarray).ravel()[unfixlist]