def addGroup(self, rlz):
        """
      Function to add a group into the database
      @ In, groupName, string, group name
      @ In, attributes, dict, dictionary of attributes that must be added as metadata
      @ In, source, File object, data source (for example, csv file)
      @ Out, None
    """
        parentID = rlz.get("RAVEN_parentID", [None])[0]
        prefix = rlz.get("prefix")

        groupName = str(
            prefix if mathUtils.isSingleValued(prefix) else prefix[0])
        if parentID:
            #If Hierarchical structure, firstly add the root group
            if not self.firstRootGroup or parentID == "None":
                self.__addGroupRootLevel(groupName, rlz)
                self.firstRootGroup = True
                self.type = 'DET'
            else:
                # Add sub group in the Hierarchical structure
                self.__addSubGroup(groupName, rlz)
        else:
            # Parallel structure (always root level)
            self.__addGroupRootLevel(groupName, rlz)
            self.firstRootGroup = True
            self.type = 'MC'
        self.__updateFileLevelInfoDatasets()
        self.h5FileW.flush()
Exemple #2
0
def componentLifeCashflow(comp, cf, variables, lifetimeCashflows, v=100):
  """
    Calcualtes the annual lifetime-based cashflow for a cashflow of a component
    @ In, comp, CashFlows.Component, component whose cashflow is being analyzed
    @ In, cf, CashFlows.CashFlow, cashflow who is being analyzed
    @ In, variables, dict, RAVEN variables as name: value
    @ In, v, int, verbosity
    @ Out, lifeCashflow, np.array, array of cashflow values with length of component life
  """
  m = 'compLife'
  vprint(v, 1, m, "-"*75)
  print('DEBUGG comp:', comp.name, cf)
  vprint(v, 1, m, 'Computing LIFETIME cash flow for Component "{}" CashFlow "{}" ...'.format(comp.name, cf.name))
  paramText = '... {:^10.10s}: {: 1.9e}'
  # do cashflow
  results = cf.calculateCashflow(variables, lifetimeCashflows, comp.getLifetime()+1, v)
  lifeCashflow = results['result']

  if v < 1:
    # print out all of the parts of the cashflow calc
    for item, value in results.items():
      if item == 'result':
        continue
      if utils.isAFloatOrInt(value):
        vprint(v, 1, m, paramText.format(item, value))
      else:
        orig = cf.getParam(item)
        if utils.isSingleValued(orig):
          name = orig
        else:
          name = '(from input)'
        vprint(v, 1, m, '... {:^10.10s}: {}'.format(item, name))
        vprint(v, 1, m, '...           mean: {: 1.9e}'.format(value.mean()))
        vprint(v, 1, m, '...           std : {: 1.9e}'.format(value.std()))
        vprint(v, 1, m, '...           min : {: 1.9e}'.format(value.min()))
        vprint(v, 1, m, '...           max : {: 1.9e}'.format(value.max()))
        vprint(v, 1, m, '...           nonz: {:d}'.format(np.count_nonzero(value)))

    yx = max(len(str(len(lifeCashflow))),4)
    vprint(v, 0, m, 'LIFETIME cash flow summary by year:')
    vprint(v, 0, m, '    {y:^{yx}.{yx}s}, {a:^10.10s}, {d:^10.10s}, {c:^15.15s}'.format(y='year',
                                                                                        yx=yx,
                                                                                        a='alpha',
                                                                                        d='driver',
                                                                                        c='cashflow'))
    for y, cash in enumerate(lifeCashflow):
      if cf.type in ['Capex']:
        vprint(v, 1, m, '    {y:^{yx}d}, {a: 1.3e}, {d: 1.3e}, {c: 1.9e}'.format(y=y,
                                                                                 yx=yx,
                                                                                 a=results['alpha'][y],
                                                                                 d=results['driver'][y],
                                                                                 c=cash))
      elif cf.type == 'Recurring':
        vprint(v, 1, m, '    {y:^{yx}d}, -- N/A -- , -- N/A -- , {c: 1.9e}'.format(y=y,
                                                           yx=yx,
                                                           c=cash))
  return lifeCashflow
Exemple #3
0
 def _selectiveRealization(self, rlz):
     """
   Uses "options" parameters from input to select part of the collected data
   @ In, rlz, dict, {var:val} format (see addRealization)
   @ Out, rlz, dict, {var:val} modified
 """
     # TODO this could be much more efficient on the parallel (finalizeCodeOutput) than on serial
     # TODO someday needs to be implemented for when ND data is collected!  For now, use base class.
     # TODO externalize it in the DataObject base class
     toRemove = []
     for var, val in rlz.items():
         if var in self.protectedTags:
             continue
         # only modify it if it is not already scalar
         if not mathUtils.isSingleValued(val):
             # treat inputs, outputs differently TODO this should extend to per-variable someday
             ## inputs
             if var in self._inputs:
                 method, indic = self._selectInput
             # pivot variables are included here in "else"; remove them after they're used in operators
             else:
                 continue
             if method in ['inputRow']:
                 # zero-d xarrays give false behavior sometimes
                 # TODO formatting should not be necessary once standardized history,float realizations are established
                 if type(val) == list:
                     val = np.array(val)
                 elif type(val).__name__ == 'DataArray':
                     val = val.values
                 # FIXME this is largely a biproduct of old length-one-vector approaches in the deprecataed data objects
                 if val.size == 1:
                     rlz[var] = float(val)
                 else:
                     rlz[var] = float(val[indic])
             elif method in ['inputPivotValue']:
                 pivotParam = self.getDimensions(var)
                 assert (len(pivotParam) == 1
                         )  # TODO only handle History for now
                 pivotParam = pivotParam[var][0]
                 idx = (np.abs(rlz[pivotParam] - indic)).argmin()
                 rlz[var] = rlz[var][idx]
             elif method == 'operator':
                 if indic == 'max':
                     rlz[var] = float(val.max())
                 elif indic == 'min':
                     rlz[var] = float(val.min())
                 elif indic in ['mean', 'expectedValue', 'average']:
                     rlz[var] = float(val.mean())
         # otherwise, leave it alone
     return rlz
Exemple #4
0
 def _selectiveRealization(self, rlz):
     """
   Uses "options" parameters from input to select part of the collected data
   @ In, rlz, dict, {var:val} format (see addRealization)
   @ Out, rlz, dict, {var:val} modified
 """
     # TODO this could be much more efficient on the parallel (finalizeCodeOutput) than on serial
     # TODO costly for loop
     # TODO overwrites rlz by reference; is this okay?
     # data was previously formatted by _formatRealization
     # then select the point we want
     toRemove = []
     for var, val in rlz.items():
         if var in self.protectedTags:
             continue
         # only modify it if it is not already scalar
         if not mathUtils.isSingleValued(val):
             # treat inputs, outputs differently TODO this should extend to per-variable someday
             ## inputs
             if var in self._inputs:
                 method, indic = self._selectInput
             elif var in self._outputs or var in self._metavars:
                 # TODO where does metadata get picked from?  Seems like output fits best?
                 method, indic = self._selectOutput
             # pivot variables are included here in "else"; remove them after they're used in operators
             else:
                 toRemove.append(var)
                 continue
             if method in ['inputRow', 'outputRow']:
                 # zero-d xarrays give false behavior sometimes
                 # TODO formatting should not be necessary once standardized history,float realizations are established
                 if type(val) == list:
                     val = np.array(val)
                 elif type(val).__name__ == 'DataArray':
                     val = val.values
                 # FIXME this is largely a biproduct of old length-one-vector approaches in the deprecataed data objects
                 if val.size == 1:
                     rlz[var] = val[0]
                 else:
                     rlz[var] = val[indic]
             elif method in ['inputPivotValue', 'outputPivotValue']:
                 pivotParam = self.getDimensions(var)
                 assert (len(pivotParam) == 1
                         )  # TODO only handle History for now
                 pivotParam = pivotParam[var][0]
                 idx = (np.abs(rlz[pivotParam] - indic)).argmin()
                 rlz[var] = rlz[var][idx]
                 # if history is dataarray -> not currently possible, but keep for when it's needed
                 #if type(rlz[var]).__name__ == 'DataArray':
                 #  rlz[var] = float(val.sel(**{pivotParam:indic, 'method':b'nearest'})) #casting as str not unicode
                 # TODO allowing inexact matches; it's finding the nearest
             elif method == 'operator':
                 if indic == 'max':
                     rlz[var] = float(val.max())
                 elif indic == 'min':
                     rlz[var] = float(val.min())
                 elif indic in ['mean', 'expectedValue', 'average']:
                     rlz[var] = float(val.mean())
         # otherwise, leave it alone
     for var in toRemove:
         del rlz[var]
     return rlz
Exemple #5
0
n = np.inf
checkAnswer('InfDiff inf    - inf', mathUtils.diffWithInfinites(n, i), 0)
checkAnswer('InfDiff inf    - finite', mathUtils.diffWithInfinites(n, 0), i)
checkAnswer('InfDiff inf    - (-inf)', mathUtils.diffWithInfinites(n, -n), i)
checkAnswer('InfDiff finite - inf', mathUtils.diffWithInfinites(0, n), -i)
checkAnswer('InfDiff finite - finite', mathUtils.diffWithInfinites(3, 2), 1)
checkAnswer('InfDiff finite - (-inf)', mathUtils.diffWithInfinites(0, -n), i)
checkAnswer('InfDiff -inf   - inf', mathUtils.diffWithInfinites(-n, n), -i)
checkAnswer('InfDiff -inf   - finite', mathUtils.diffWithInfinites(-n, 0), -i)
checkAnswer('InfDiff -inf   - (-inf)', mathUtils.diffWithInfinites(-n, -n), 0)

##########################
#      TYPE CHECKING     #
##########################
# isSingleValued
checkAnswer('isSingleValued -1', mathUtils.isSingleValued(-1), True)
checkAnswer('isSingleValued 0', mathUtils.isSingleValued(0), True)
checkAnswer('isSingleValued 1', mathUtils.isSingleValued(1), True)
checkAnswer('isSingleValued 1e200', mathUtils.isSingleValued(1e200), True)
checkAnswer('isSingleValued 1e-200', mathUtils.isSingleValued(1e-200), True)
checkAnswer('isSingleValued -1e200', mathUtils.isSingleValued(-1e200), True)
checkAnswer('isSingleValued 3.14', mathUtils.isSingleValued(3.14), True)
checkAnswer('isSingleValued "hombre"', mathUtils.isSingleValued('hombre'),
            True)
checkAnswer('isSingleValued None', mathUtils.isSingleValued(None), True)
checkAnswer('isSingleValued True', mathUtils.isSingleValued(True), True)
checkAnswer('isSingleValued False', mathUtils.isSingleValued(False), True)
checkAnswer('isSingleValued long',
            mathUtils.isSingleValued(123456789012345678901234567890), True)

checkAnswer('isSingleValued inf notok',
Exemple #6
0
  def modifyOrAdd(self,modiDictionary={},save=True, allowAdd = False):
    """
      modiDictionary a dict of dictionaries of the required addition or modification
      {"variableToChange":value }
      @ In, modiDictionary, dict, dictionary of variables to modify
            syntax:
            {'Node|SubNode|SubSubNode:value1','Node|SubNode|SubSubNode@attribute:attributeValue|SubSubSubNode':value2
                      'Node|SubNode|SubSubNode@attribute':value3}
             TODO: handle added XML nodes
      @ In, save, bool, optional, True if the original tree needs to be saved
      @ In, allowAdd, bool, optional, True if the nodes that are not found should be added (additional piece of input)
      @ Out, returnElement, xml.etree.ElementTree.Element, the tree that got modified
    """
    if save:
      returnElement = copy.deepcopy(self.tree)            #make a copy if save is requested
    else:
      returnElement = self.tree                           #otherwise return the original modified

    for fullNode, val in modiDictionary.items():
      # might be comma-separated ("fully correlated") variables
      nodes = [x.strip() for x in fullNode.split(',')]
      for node in nodes:
        # make sure node is XML-tree-parsable
        if "|" not in node:
          raise IOError(self.printTag+' ERROR: the variable '+node.strip()+' does not contain "|" separator and can not be handled!!')
        changeTheNode = True
        allowAddNodes, allowAddNodesPath = [], OrderedDict()
        if "@" in node:
          # there are attributes that are needed to locate the node
          splittedComponents = node.split("|")
          # check the first
          pathNode = './'
          attribName = ''
          for cnt, subNode in enumerate(splittedComponents):
            splittedComp = subNode.split("@")
            component = splittedComp[0]
            attribPath = ""
            attribConstruct = OrderedDict()
            if "@" in subNode:
              # more than an attribute locator
              for attribComp in splittedComp[1:]:
                attribValue = None
                if ":" in attribComp.strip():
                  # it is a locator
                  attribName  = attribComp.split(":")[0].strip()
                  attribValue = attribComp.split(":")[1].strip()
                  attribPath +='[@'+attribName+('="'+attribValue+'"]')
                else:
                  # it is actually the attribute that needs to be changed
                  # check if it is the last component
                  if cnt+1 != len(splittedComponents):
                    raise IOError(self.printTag+' ERROR: the variable '+node.strip()+' follows the syntax "Node|SubNode|SubSubNode@attribute"'+
                                                ' but the attribute is not the last component. Please check your input!')
                  attribName = attribComp.strip()
                  attribPath +='[@'+attribName+']'
                if allowAdd:
                  attribConstruct[attribName]  = attribValue
            pathNode += "/" + component.strip()+attribPath
            if allowAdd:
              if len(returnElement.findall(pathNode)) > 0:
                allowAddNodes.append(pathNode)
              else:
                allowAddNodes.append(None)
              allowAddNodesPath[component.strip()] = attribConstruct
          if pathNode.endswith("]") and list(attribConstruct.values())[-1] is None:
            changeTheNode = False
          else:
            changeTheNode = True
        else:
          # there are no attributes that are needed to track down the node to change
          pathNode = './/' + node.replace("|","/").strip()
          if allowAdd:
            pathNodeTemp = './'
            for component in node.replace("|","/").split("/"):
              pathNodeTemp += '/'+component
              if len(returnElement.findall(pathNodeTemp)) > 0:
                allowAddNodes.append(pathNodeTemp)
              else:
                allowAddNodes.append(None)
              allowAddNodesPath[component.strip()] = None
        # look for the node with XPath directives
        foundNodes = returnElement.findall(pathNode)
        if len(foundNodes) > 1:
          raise IOError(self.printTag+' ERROR: multiple nodes have been found corresponding to path -> '+node.strip()+'. Please use the attribute identifier "@" to nail down to a specific node !!')
        if len(foundNodes) == 0 and not allowAdd:
          raise IOError(self.printTag+' ERROR: no node has been found corresponding to path -> '+node.strip()+'. Please check the input!!')
        if len(foundNodes) == 0:
          # this means that the allowAdd is true (=> no error message has been raised)
          indexFirstUnknownNode = allowAddNodes.index(None)
          if indexFirstUnknownNode == 0:
            raise IOError(self.printTag+' ERROR: at least the main XML node should be present in the RAVEN template input -> '+node.strip()+'. Please check the input!!')
          getFirstElement = returnElement.findall(allowAddNodes[indexFirstUnknownNode-1])[0]
          for i in range(indexFirstUnknownNode,len(allowAddNodes)):
            nodeWithAttributeName = list(allowAddNodesPath.keys())[i]
            if not allowAddNodesPath[nodeWithAttributeName]:
              subElement =  ET.Element(nodeWithAttributeName)
            else:
              subElement =  ET.Element(nodeWithAttributeName, attrib=allowAddNodesPath[nodeWithAttributeName])
            getFirstElement.append(subElement)
            getFirstElement = subElement
          # in the event of vector entries, handle those here
          if mathUtils.isSingleValued(val):
            val = str(val).strip()
          else:
            if len(val.shape) > 1:
              raise IOError(self.printTag+'ERROR: RAVEN interface is not prepared to handle matrix value passing yet!')
            val = ','.join(str(i) for i in val)
          if changeTheNode:
            subElement.text = val
          else:
            subElement.attrib[attribConstruct.keys()[-1]] = val

        else:
          nodeToChange = foundNodes[0]
          pathNode     = './/'
          # in the event of vector entries, handle those here
          if mathUtils.isSingleValued(val):
            val = str(val).strip()
          else:
            if len(val.shape) > 1:
              raise IOError(self.printTag+'ERROR: RAVEN interface is not prepared to handle matrix value passing yet!')
            val = ','.join(str(i) for i in val)
          if changeTheNode:
            nodeToChange.text = val
          else:
            nodeToChange.attrib[attribName] = val
    return returnElement