Exemple #1
0
 def XMLread(self,xmlNode,runInfoSkip = set(),xmlFilename=None):
   """
     parses the xml input file, instances the classes need to represent all objects in the simulation
     @ In, xmlNode, ElementTree.Element, xml node to read in
     @ In, runInfoSkip, set, optional, nodes to skip
     @ In, xmlFilename, string, optional, xml filename for relative directory
     @ Out, None
   """
   #TODO update syntax to note that we read InputTrees not XmlTrees
   unknownAttribs = utils.checkIfUnknowElementsinList(['printTimeStamps','verbosity','color','profile'],list(xmlNode.attrib.keys()))
   if len(unknownAttribs) > 0:
     errorMsg = 'The following attributes are unknown:'
     for element in unknownAttribs:
       errorMsg += ' ' + element
     self.raiseAnError(IOError,errorMsg)
   self.verbosity = xmlNode.attrib.get('verbosity','all').lower()
   if 'printTimeStamps' in xmlNode.attrib.keys():
     self.raiseADebug('Setting "printTimeStamps" to',xmlNode.attrib['printTimeStamps'])
     self.messageHandler.setTimePrint(xmlNode.attrib['printTimeStamps'])
   if 'color' in xmlNode.attrib.keys():
     self.raiseADebug('Setting color output mode to',xmlNode.attrib['color'])
     self.messageHandler.setColor(xmlNode.attrib['color'])
   if 'profile' in xmlNode.attrib.keys():
     thingsToProfile = list(p.strip().lower() for p in xmlNode.attrib['profile'].split(','))
     if 'jobs' in thingsToProfile:
       self.jobHandler.setProfileJobs(True)
   self.messageHandler.verbosity = self.verbosity
   runInfoNode = xmlNode.find('RunInfo')
   if runInfoNode is None:
     self.raiseAnError(IOError,'The RunInfo node is missing!')
   self.__readRunInfo(runInfoNode,runInfoSkip,xmlFilename)
   ### expand variable groups before continuing ###
   ## build variable groups ##
   varGroupNode = xmlNode.find('VariableGroups')
   # init, read XML for variable groups
   if varGroupNode is not None:
     varGroups = xmlUtils.readVariableGroups(varGroupNode,self.messageHandler,self)
   else:
     varGroups={}
   # read other nodes
   for child in xmlNode:
     if child.tag=='VariableGroups':
       continue #we did these before the for loop
     if child.tag in list(self.whichDict.keys()):
       self.raiseADebug('-'*2+' Reading the block: {0:15}'.format(str(child.tag))+2*'-')
       Class = child.tag
       if len(child.attrib.keys()) == 0:
         globalAttributes = {}
       else:
         globalAttributes = child.attrib
         #if 'verbosity' in globalAttributes.keys(): self.verbosity = globalAttributes['verbosity']
       if Class not in ['RunInfo','OutStreams'] and "returnInputParameter" in self.addWhatDict[Class].__dict__:
         paramInput = self.addWhatDict[Class].returnInputParameter()
         paramInput.parseNode(child)
         for childChild in paramInput.subparts:
           childName = childChild.getName()
           if "name" not in childChild.parameterValues:
             self.raiseAnError(IOError,'not found name attribute for '+childName +' in '+Class)
           name = childChild.parameterValues["name"]
           if "needsRunInfo" in self.addWhatDict[Class].__dict__:
             self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childName,self.runInfoDict,self)
           else:
             self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childName,self)
           self.whichDict[Class][name].handleInput(childChild, self.messageHandler, varGroups, globalAttributes=globalAttributes)
       elif Class != 'RunInfo':
         for childChild in child:
           subType = childChild.tag
           if 'name' in childChild.attrib.keys():
             name = childChild.attrib['name']
             self.raiseADebug('Reading type '+str(childChild.tag)+' with name '+name)
             #place the instance in the proper dictionary (self.whichDict[Type]) under his name as key,
             #the type is the general class (sampler, data, etc) while childChild.tag is the sub type
             #if name not in self.whichDict[Class].keys():  self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self)
             if Class != 'OutStreams':
               if name not in self.whichDict[Class].keys():
                 if "needsRunInfo" in self.addWhatDict[Class].__dict__:
                   self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self.runInfoDict,self)
                 else:
                   self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self)
               else:
                 self.raiseAnError(IOError,'Redundant naming in the input for class '+Class+' and name '+name)
             else:
               if name not in self.whichDict[Class][subType].keys():
                 self.whichDict[Class][subType][name] = self.addWhatDict[Class][subType].returnInstance(childChild.tag,self)
               else:
                 self.raiseAnError(IOError,'Redundant  naming in the input for class '+Class+' and sub Type'+subType+' and name '+name)
             #now we can read the info for this object
             #if globalAttributes and 'verbosity' in globalAttributes.keys(): localVerbosity = globalAttributes['verbosity']
             #else                                                      : localVerbosity = self.verbosity
             if Class != 'OutStreams':
               self.whichDict[Class][name].readXML(childChild, self.messageHandler, varGroups, globalAttributes=globalAttributes)
             else:
               self.whichDict[Class][subType][name].readXML(childChild, self.messageHandler, globalAttributes=globalAttributes)
           else:
             self.raiseAnError(IOError,'not found name attribute for one '+Class)
     else:
       #tag not in whichDict, check if it's a documentation tag
       if child.tag not in ['TestInfo']:
         self.raiseAnError(IOError,'<'+child.tag+'> is not among the known simulation components '+repr(child))
   # If requested, duplicate input
   # ###NOTE: All substitutions to the XML input tree should be done BEFORE this point!!
   if self.runInfoDict.get('printInput',False):
     fileName = os.path.join(self.runInfoDict['WorkingDir'],self.runInfoDict['printInput'])
     self.raiseAMessage('Writing duplicate input file:',fileName)
     outFile = open(fileName,'w')
     outFile.writelines(utils.toString(TreeStructure.tostring(xmlNode))+'\n') #\n for no-end-of-line issue
     outFile.close()
   if not set(self.stepSequenceList).issubset(set(self.stepsDict.keys())):
     self.raiseAnError(IOError,'The step list: '+str(self.stepSequenceList)+' contains steps that have not been declared: '+str(list(self.stepsDict.keys())))
Exemple #2
0
 def initialize(self,runInfo,inputs,initDict=None):
   """
     Method to initialize this model class
     @ In, runInfo, dict, is the run info from the jobHandler
     @ In, inputs, list, is a list containing whatever is passed with an input role in the step
     @ In, initDict, dict, optional, dictionary of all objects available in the step is using this model
     @ Out, None
   """
   if isinstance(self.modelInstance, Models.Model):
     self.raiseAnError(IOError, "HybridModel has already been initialized, and it can not be initialized again!")
   self.modelInstance = self.retrieveObjectFromAssemblerDict('Model', self.modelInstance)
   if self.modelInstance.type == 'Code':
     codeInput = []
     for elem in inputs:
       if isinstance(elem, Files.File):
         codeInput.append(elem)
     self.modelInstance.initialize(runInfo, codeInput, initDict)
   self.cvInstance = self.retrieveObjectFromAssemblerDict('CV', self.cvInstance)
   self.cvInstance.initialize(runInfo, inputs, initDict)
   self.targetEvaluationInstance = self.retrieveObjectFromAssemblerDict('TargetEvaluation', self.targetEvaluationInstance)
   if len(self.targetEvaluationInstance):
     self.raiseAWarning("The provided TargetEvaluation data object is not empty, the existing data will also be used to train the ROMs!")
     self.existTrainSize = len(self.targetEvaluationInstance)
   self.tempTargetEvaluation = copy.deepcopy(self.targetEvaluationInstance)
   if self.modelInstance is None:
     self.raiseAnError(IOError,'Model XML block needs to be inputted!')
   if self.cvInstance is None:
     self.raiseAnError(IOError, 'CV XML block needs to be inputted!')
   if self.targetEvaluationInstance is None:
     self.raiseAnError(IOError, 'TargetEvaluation XML block needs to be inputted!')
   for romName, romInfo in self.romsDictionary.items():
     romInfo['Instance'] = self.retrieveObjectFromAssemblerDict('ROM', romName)
     if romInfo['Instance']  is None:
       self.raiseAnError(IOError, 'ROM XML block needs to be inputted!')
   modelInputs = self.targetEvaluationInstance.getVars("input")
   modelOutputs = self.targetEvaluationInstance.getVars("output")
   modelName = self.modelInstance.name
   totalRomOutputs = []
   for romInfo in self.romsDictionary.values():
     romIn = romInfo['Instance']
     if romIn.amITrained:
       self.raiseAWarning("The provided rom ", romIn.name, " is already trained, we will reset it!")
       romIn.reset()
     romInputs = romIn.getInitParams()['Features']
     romOutputs = romIn.getInitParams()['Target']
     totalRomOutputs.extend(romOutputs)
     unknownList = utils.checkIfUnknowElementsinList(modelInputs, romInputs)
     if unknownList:
       self.raiseAnError(IOError, 'Input Parameters: "', ','.join(str(e) for e in unknownList), '" used in ROM ', romIn.name, ' can not found in Model ', modelName)
     unknownList = utils.checkIfUnknowElementsinList(romInputs, modelInputs)
     if unknownList:
       self.raiseAnError(IOError, 'Input Parameters: "', ','.join(str(e) for e in unknownList), '" used in Model ', modelName, ', but not used in ROM ', romIn.name)
     unknownList = utils.checkIfUnknowElementsinList(modelOutputs, romOutputs)
     if unknownList:
       self.raiseAnError(IOError, 'Output Parameters: "', ','.join(str(e) for e in unknownList), '" used in ROM ', romIn.name, ' can not found in Model ', modelName)
     if romIn.amITrained:
       # Only untrained roms are allowed
       self.raiseAnError(IOError,'HybridModel only accepts untrained ROM, but rom "', romIn.name, '" is already trained')
   # check: we require that the union of ROMs outputs is the same as the paired model in order to use the ROM
   # to replace the paired model.
   if len(set(totalRomOutputs)) != len(totalRomOutputs):
     dup = []
     for elem in set(totalRomOutputs):
       if totalRomOutputs.count(elem) > 1:
         dup.append(elem)
     # we assume there is no duplicate outputs among the roms
     self.raiseAnError(IOError, 'The following outputs ', ','.join(str(e) for e in dup), "are found in the outputs of multiple roms!")
   unknownList = utils.checkIfUnknowElementsinList(totalRomOutputs,modelOutputs)
   if unknownList:
     self.raiseAnError(IOError, "The following outputs: ", ','.join(str(e) for e in unknownList), " used in Model: ", modelName, "but not used in the paired ROMs.")
   self.tempOutputs['uncollectedJobIds'] = []