def update_model(self, oldmodel, newmodel): """called whenever the model variable is set.""" # TODO: check for pre-connected traits on the new model # TODO: disconnect traits corresponding to old model (or leave them if the new model has the same ones?) # TODO: check for nested MMs? Is this a problem? # TODO: check for name collisions between MetaModel class traits and traits from model if newmodel is not None and not has_interface(newmodel, IComponent): self.raise_exception('model of type %s does not implement the IComponent interface' % type(newmodel).__name__, TypeError) if not self.surrogate: self.raise_exception("surrogate must be set before the model or any includes/excludes of variables", RuntimeError) new_model_traitnames = set() self._surrogate_input_names = [] self._taining_input_history = [] self._surrogate_info = {} # remove traits promoted from the old model for name in self._current_model_traitnames: if self.parent: self.parent.disconnect('.'.join([self.name,name])) self.remove_trait(name) if newmodel: # query for inputs traitdict = newmodel._alltraits(iotype='in') for name,trait in traitdict.items(): if self._eligible(name): self._surrogate_input_names.append(name) if name not in self._mm_class_traitnames: self.add_trait(name, _clone_trait(trait)) new_model_traitnames.add(name) setattr(self, name, getattr(newmodel, name)) # now outputs traitdict = newmodel._alltraits(iotype='out') for name,trait in traitdict.items(): if self._eligible(name): try: surrogate = self.surrogate[name] except KeyError: try: surrogate = self.surrogate['default'] except KeyError: self.raise_exception("No default surrogate model was" " specified. Either specify a default, or specify a " "surrogate model for all outputs",ValueError) trait_type = surrogate.get_uncertain_value(1.0).__class__() self.add(name, Slot(trait_type, iotype='out', desc=trait.desc)) self._surrogate_info[name] = (surrogate.__class__(), []) # (surrogate,output_history) new_model_traitnames.add(name) setattr(self, name, surrogate.get_uncertain_value(getattr(newmodel,name))) newmodel.parent = self newmodel.name = 'model' self._current_model_traitnames = new_model_traitnames
def _add_output(self, name): """Adds the specified output variable and its associated surrogate Slot.""" sur_name = __surrogate_prefix__+name self.add_trait(sur_name, Slot(ISurrogate, allow_none=True)) self.on_trait_change(self._surrogate_updated, sur_name) if self.default_surrogate is not None: surrogate = deepcopy(self.default_surrogate) self._default_surrogate_copies[sur_name] = surrogate self._add_var_for_surrogate(surrogate, name) else: self.add_trait(name, _clone_trait(self.model.trait(name))) self._training_data[name] = []
def _add_output(self, name): """Adds the specified output variable and its associated surrogate Slot.""" sur_name = __surrogate_prefix__ + name self.add_trait(sur_name, Slot(ISurrogate, allow_none=True)) self.on_trait_change(self._surrogate_updated, sur_name) if self.default_surrogate is not None: surrogate = deepcopy(self.default_surrogate) self._default_surrogate_copies[sur_name] = surrogate self._add_var_for_surrogate(surrogate, name) else: self.add_trait(name, _clone_trait(self.model.trait(name))) self._training_data[name] = []
def update_model(self, oldmodel, newmodel): """called whenever the model variable is set or when includes/excludes change.""" # TODO: check for pre-connected traits on the new model # TODO: disconnect traits corresponding to old model (or leave them if the new model has the same ones?) # TODO: check for nested MMs? Is this a problem? # TODO: check for name collisions between MetaModel class traits and traits from model if newmodel is not None and not has_interface(newmodel, IComponent): self.raise_exception( 'model of type %s does not implement the IComponent interface' % type(newmodel).__name__, TypeError) if not self.surrogate: self.raise_exception( "surrogate must be set before the model or any includes/excludes of variables", RuntimeError) new_model_traitnames = set() self._surrogate_input_names = [] self._training_input_history = [] self._surrogate_info = {} self._failed_training_msgs = [] # remove traits promoted from the old model for name in self._current_model_traitnames: if self.parent: self.parent.disconnect('.'.join([self.name, name])) self.remove_trait(name) if newmodel: # query for inputs traitdict = newmodel._alltraits(iotype='in') for name, trait in traitdict.items(): if self._eligible(name): self._surrogate_input_names.append(name) if name not in self._mm_class_traitnames: self.add_trait(name, _clone_trait(trait)) new_model_traitnames.add(name) setattr(self, name, getattr(newmodel, name)) # now outputs traitdict = newmodel._alltraits(iotype='out') for name, trait in traitdict.items(): if self._eligible(name): try: surrogate = self.surrogate[name] args = self.surrogate_args.get(name, []) except KeyError: try: surrogate = self.surrogate['default'] args = self.surrogate_args.get('default', []) except KeyError: self.raise_exception( "No default surrogate model was" " specified. Either specify a default, or specify a " "surrogate model for all outputs", ValueError) if isinstance(args, dict): kwargs = args args = [] elif isinstance(args, tuple): args = args[0] kwargs = args[1] else: kwargs = {} trait_type = surrogate.get_uncertain_value(1.0).__class__() self.add(name, Slot(trait_type, iotype='out', desc=trait.desc)) self._surrogate_info[name] = (surrogate.__class__( *args, **kwargs), []) # (surrogate,output_history) new_model_traitnames.add(name) setattr( self, name, surrogate.get_uncertain_value(getattr(newmodel, name))) newmodel.parent = self newmodel.name = 'model' self._current_model_traitnames = new_model_traitnames
def _add_input(self, name): """Adds the specified input variable.""" self.add_trait(name, _clone_trait(self.model.trait(name))) setattr(self, name, getattr(self.model, name))