def __init__(self, input): if isinstance(input, dict): input_to_file = input conservative_update(self, input_to_file) # self.__dict__.update(input_to_file) elif hasattr(input, '__iter__'): input_to_file = input else: # Modules, objects, etc. input_to_file = input.__dict__ conservative_update(self, input_to_file) # self.__dict__.update(input_to_file) dictpop = copy(self.__dict__) if dictpop.has_key('self'): dictpop.pop('self') self._dict_container = DictContainer(dictpop) file_items(self, input_to_file) self._value = copy(self) ContainerBase.__init__(self, input) self.OCValue = OCValue(self)
class ComponentBase(ContainerBase): """ Base class which other components inherit from. Significant portions are adapted from pymc.Container.ObjectContainer. (__init__, replace, value) """ _fits_abbrs = [] def __init__(self): dictpop = copy(self.__dict__) if 'self' in dictpop: dictpop.pop('self') self._dict_container = DictContainer(dictpop) file_items(self, dictpop) self._value = copy(self) super(ComponentBase, self).__init__(self) self.OCValue = OCValue(self) def update_trace_names(self, count=None): """ Set trace names based on component number, type, and attribute name Also add abbreviated 'fitsname' attribute to individual stochastics """ comptype = self.__class__.__name__ for attr in self.__dict__: newname = '{}_{}'.format(comptype, attr) fitsname = newname for longname, abbr in self.__class__._fits_abbrs: fitsname = fitsname.replace(longname, abbr) if count is not None: newname = '{:d}_{}'.format(count, newname) fitsname = '{:d}{}'.format(count, fitsname) try: self.__dict__[attr].__name__ = newname self.__dict__[attr].fitsname = fitsname except AttributeError: pass def replace(self, item, new_container, key): dict.__setitem__(self.__dict__, key, new_container) def _get_value(self): self.OCValue.run() return self._value value = property(fget=_get_value, doc='Copy of object with stochastics replaced by values')
def __init__(self, input): if isinstance(input, dict): input_to_file = input conservative_update(self, input_to_file) # self.__dict__.update(input_to_file) elif hasattr(input,'__iter__'): input_to_file = input else: # Modules, objects, etc. input_to_file = input.__dict__ conservative_update(self, input_to_file) # self.__dict__.update(input_to_file) dictpop = copy(self.__dict__) if dictpop.has_key('self'): dictpop.pop('self') self._dict_container = DictContainer(dictpop) file_items(self, input_to_file) self._value = copy(self) ContainerBase.__init__(self, input) self.OCValue = OCValue(self)
def __init__(self, input): if isinstance(input, dict): input_to_file = input self.__dict__.update(input_to_file) elif hasattr(input, '__iter__'): input_to_file = input else: # Modules, objects, etc. input_to_file = input.__dict__ self.__dict__.update(input_to_file) self._dict_container = DictContainer(self.__dict__) file_items(self, input_to_file) self._value = copy(self) ContainerBase.__init__(self, input) self.OCValue = OCValue(self)
def __init__(self): dictpop = copy(self.__dict__) if 'self' in dictpop: dictpop.pop('self') self._dict_container = DictContainer(dictpop) file_items(self, dictpop) self._value = copy(self) super(ComponentBase, self).__init__(self) self.OCValue = OCValue(self)
class ObjectContainer(ContainerBase): """ ObjectContainers wrap non-iterable objects. Contents of the input iterable, or attributes of the input object, are exposed as attributes of the object. :Parameters: iterable : dictionary or object with a __dict__. :Attributes: value : object A copy of self, with all variables replaced with their values. nodes : set All the stochastics, deterministics and potentials self contains. deterministics : set All the deterministics self contains. stochastics : set All the stochastics self contains with observed=False. potentials : set All the potentials self contains. observed_stochastics : set All the stochastics self contains with observed=True. containers : list All the containers self contains. :Note: - nodes, deterministics, etc. include all the objects in nested containers. - value replaces objects in nested containers. :SeeAlso: Container, ListContainer, DictContainer, ArrayContainer, SetContainer, TupleContainer """ register=False def __init__(self, input): if isinstance(input, dict): input_to_file = input conservative_update(self, input_to_file) # self.__dict__.update(input_to_file) elif hasattr(input,'__iter__'): input_to_file = input else: # Modules, objects, etc. input_to_file = input.__dict__ conservative_update(self, input_to_file) # self.__dict__.update(input_to_file) dictpop = copy(self.__dict__) if dictpop.has_key('self'): dictpop.pop('self') self._dict_container = DictContainer(dictpop) file_items(self, input_to_file) self._value = copy(self) ContainerBase.__init__(self, input) self.OCValue = OCValue(self) def replace(self, item, new_container, key): dict.__setitem__(self.__dict__, key, new_container) def _get_value(self): self.OCValue.run() return self._value value = property(fget = _get_value, doc=value_doc)
class ObjectContainer(ContainerBase): """ ObjectContainers wrap non-iterable objects. Contents of the input iterable, or attributes of the input object, are exposed as attributes of the object. :Parameters: iterable : dictionary or object with a __dict__. :Attributes: value : object A copy of self, with all variables replaced with their values. nodes : set All the stochastics, deterministics and potentials self contains. deterministics : set All the deterministics self contains. stochastics : set All the stochastics self contains with observed=False. potentials : set All the potentials self contains. observed_stochastics : set All the stochastics self contains with observed=True. containers : list All the containers self contains. :Note: - nodes, deterministics, etc. include all the objects in nested containers. - value replaces objects in nested containers. :SeeAlso: Container, ListContainer, DictContainer, ArrayContainer, SetContainer, TupleContainer """ register = False def __init__(self, input): if isinstance(input, dict): input_to_file = input self.__dict__.update(input_to_file) elif hasattr(input, '__iter__'): input_to_file = input else: # Modules, objects, etc. input_to_file = input.__dict__ self.__dict__.update(input_to_file) self._dict_container = DictContainer(self.__dict__) file_items(self, input_to_file) self._value = copy(self) ContainerBase.__init__(self, input) self.OCValue = OCValue(self) def replace(self, item, new_container, key): dict.__setitem__(self.__dict__, key, new_container) def _get_value(self): self.OCValue.run() return self._value value = property(fget=_get_value, doc=value_doc)