def load_translated_model(self):#, translated): # if not translated: # return self._prism_model_str = '' if self.model.PRISM_model != '': try: with read(self.model.PRISM_model_) as f: self._prism_model_str = f.read() # logger.debug(self._prism_model_str) except IOError, e: # logger.error(e) pass
def object_temporal_formulas_changed(self, info): if not os.path.isfile(info.object.temporal_formulas_): return with read(info.object.temporal_formulas_) as f: lines = f.readlines() if len(lines) == 0 or lines[0].strip() != 'Formulas:': return del self.temporal_formulas[:] # clear list for line in lines[1:]: line = line.strip() # skip empty lines if len(line) == 0: continue # extract formula first = line.find('"') second = line.find('"', first + 1) formula = line[first + 1:second] # extract parameters parameters_start = line.find('{', second + 1) parameters_end = line.find('}', parameters_start + 1) parameters = [] for parameter in line[parameters_start + 1:parameters_end].split(','): name_and_values = parameter.split('=') name = name_and_values[0].strip() values = name_and_values[1].split(':') lower = values[0] step = values[1] upper = values[2] # create parameter objects parameters.append(TemporalFormulaParameter(name=name, lower=float(lower), step=float(step), upper=float(upper))) # create formula object and add to list temporal_formula = TemporalFormula( formula=formula, parameters=parameters, params_handler=self, ) self.temporal_formulas.append(temporal_formula)
def _directory_changed(self): model_parameters_file = os.path.join(self.directory, "modelParameters.xml") try: with read(model_parameters_file) as f: parser = sax.make_parser() handler = ModelParametersXMLReader() parser.setContentHandler(handler) try: parser.parse(f) # read parameters from file into dictionary except sax._exceptions.SAXParseException, e: print e self.modelVariables = handler.modelVariables self.ruleConstants = handler.ruleConstants self.moleculeConstants = handler.moleculeConstants self.rewardConstants = handler.rewardConstants self.rewardConstant_descriptions = [ rewardConstant.description.replace("A", "a") for rewardConstant in handler.rewardConstants ] self.rewardConstant = self.rewardConstant_descriptions[0] except IOError, e: # print e, 'ModelParameters._directory_changed()' # raise e pass
def load(self, file, force=False): ''' Reads parameters file, resets traits, sets traits to new parameters, Returns True if a params file was successfully loaded. ''' if self._dirty and not force: if self._interaction_mode == 'terminal': answer = raw_input('Save current parameters before continuing? [Y/n] ') if len(answer.strip()) == 0 or answer.upper().startswith('Y'): self.save() # will prompt for file name with self._params_file as default elif self._interaction_mode == 'script': logger.warn('Overwriting unsaved parameters: %s' % ','.join(['%s=%s' % (name, value) for name, value in self._dirty_parameters.items()])) # open and parse params file with ParamsXMLReader # reporting errors or responding to success with read(file, 'rb') as fh: parser = sax.make_parser() parameters_dictionary = {} # keep reference to dictionary that will be filled by handler handler = ParamsXMLReader(parameters_dictionary, self._parameter_set_name) parser.setContentHandler(handler) error = None try: # read parameters from file into dictionary parser.parse(fh) except sax._exceptions.SAXParseException: error = "'%s' is not a well-formed XML file." % file # check for other errors if not hasattr(handler, 'parameter_set_name'): error = "'%s' is not a parameters file." % file # elif not handler.has_expected_parameter_set_name: # will also fail without changing anything, also good. # error = "Incorrect parameter set: '%s' when '%s' expected." % (handler.parameter_set_name, handler.expected_parameter_set_name) elif len(parameters_dictionary) == 0: error = "No parameters in file '%s'." % file if error is not None: logger.error(error) # auto_close_message(message=error) #TODO is this desirable when scripting?...might not happen, as with ProgressDialog return False # read parameters ok # reset traits so we don't carry over parameters that are missing from # the file we are loading self._unresetable = self.reset(self.parameter_names()) #FIXME reverts to default values not preferences! if len(self._unresetable) > 0: logger.warn("Some parameters were not reset: %s", ', '.join(self._unresetable)) # change directory here so that relative paths don't trigger TraitError if not os.path.isabs(file): file = os.path.abspath(file) # prepends os.getcwd() to relative file names self.directory = os.path.dirname(file) # set parameters from dictionary, passing if values are not suitable for name, value in parameters_dictionary.iteritems(): try: set_trait_value_from_parameter_value(self, name, value) except TraitError: pass # success! # logger.debug("Loaded '%s'." % file) self._params_file = file self._dirty = False