Ejemplo n.º 1
0
def ParseCommandLineArgs():

    theParameterManager = ParameterManager()

    # set input options
    parser = argparse.ArgumentParser(description='Command line input.')
    parser.add_argument("-i",
                        "--input",
                        action="store",
                        help="Load XML input file")
    parser.add_argument("-p",
                        "--param",
                        action="append",
                        help="Add user-defined parameter")

    rv = parser.parse_args()

    # strip out params
    if (rv.param):
        for param in rv.param:
            splt = string.split(param, "=", 1)
            if (len(splt) < 2):
                print(
                    "error - input params should be in the form -p key=value ")
                exit()
        name = splt[0].strip()
        paramString = splt[1]
        theParameterManager.SetParameter(name, paramString)

    # sanity check
    if (not rv.input):
        print("Warning - failed to provide input file")

    return rv
 def Run(self,problemManager):
     from Managers.ProblemManager import ProblemManager   # avoiding circular dependency
     
     # plotting
     doPlots = (problemManager.outputType == "plots")
     
     problemManager.theRegionalCalculationManager.type = self.type
     #problemManager.theRegionalCalculationManager.Run(problemManager)
     
     theParameterManager = ParameterManager()
     originalValue = theParameterManager.GetParameter(self.parameter)
     
     prevAction = problemManager.theActionManager.activeIndex - 1
     
     # reset params for max value
     theParameterManager.SetParameter(self.parameter,self.maxValue)
     
     # dummy manager based on run max value - nb may be able to use xml tree in prob manager rather than reloading...
     dummyProblemManager = ProblemManager.FromXMLFile(problemManager.inputFile)
     dummyProblemManager.Initialize()
     
     # rerun to this point
     dummyProblemManager.RunUntil(prevAction)
     
     # run regional calculation and record max value
     dummyProblemManager.theRegionalCalculationManager.type = self.type
     dummyProblemManager.theRegionalCalculationManager.saveResult = False
     maxValues = dummyProblemManager.theRegionalCalculationManager.Run(dummyProblemManager)
     
     # reset params for min value
     theParameterManager.SetParameter(self.parameter,self.minValue)
     dummyProblemManager = ProblemManager.FromXMLFile(problemManager.inputFile)
     
     # reset params
     dummyProblemManager.Initialize()
     
     # rerun to this point
     dummyProblemManager.RunUntil(prevAction)
     dummyProblemManager.theRegionalCalculationManager.type = self.type
     
     # run regional calculation and record min value
     minValues = dummyProblemManager.theRegionalCalculationManager.Run(dummyProblemManager)
     dummyProblemManager.theRegionalCalculationManager.saveResult = False
     
     diff = maxValues - minValues #np.abs(maxValues - minValues)
     if problemManager.theRegionalCalculationManager.type == 'NPV':
       diff *= 1e-6
     
     # fixme need to modify output control 
     filename = problemManager.outputPrefix+"_snstvty_"+self.parameter+"."+problemManager.outputType
     problemManager.theRegionalCalculationManager.type = self.parameter
     problemManager.theRegionalCalculationManager.SaveMap(filename, diff,problemManager.recordRange)
     problemManager.theRegionalCalculationManager.type = self.type
     
     theParameterManager.SetParameter(self.parameter,originalValue)
     
     if doPlots:
       pl.imshow(diff,origin="lower")
       pl.colorbar()
       pl.show()
Ejemplo n.º 3
0
def ParseCommandLineArgs():
    """ Interpret the command line arguments provided to bluecap, record parameters and return the remainder."""

    theParameterManager = ParameterManager()

    # set input options
    parser = argparse.ArgumentParser(description='Command line input.')
    parser.add_argument("-i",
                        "--input",
                        action="store",
                        help="Load XML input file")
    parser.add_argument("-p",
                        "--param",
                        action="append",
                        help="Add user-defined parameter")

    rv = parser.parse_args()

    # strip out params
    if (rv.param):
        for param in rv.param:
            splt = string.split(param, "=", 1)
            if (len(splt) < 2):
                raise BluecapError(
                    "Error: Input parameters should be in the form -p key=value "
                )
            name = splt[0].strip()
            paramString = splt[1]
            theParameterManager.SetParameter(name, paramString)

    # sanity checks
    if (not rv.input):
        print("Warning - failed to provide input file")
        #raise  IFDError("Input error","Failed to provided input file")

    return rv
    def Run(self,problemManager):
        """Run the ComparativeSensitivityAction action."""
        
        from Managers.ProblemManager import ProblemManager   # avoiding circular dependency
        
        problemManager.theRegionalCalculationManager.type = self.type
        #problemManager.theRegionalCalculationManager.Run(problemManager)
        
        diffs = []
        for i in range(3):
            theParameterManager = ParameterManager()
            originalValue = theParameterManager.GetParameter(self.parameters[i])
        
            prevAction = problemManager.theActionManager.activeIndex - 1
        
            # reset params for max value
            theParameterManager.SetParameter(self.parameters[i],self.maxValues[i])
        
            # dummy manager based on run max value - nb may be able to use xml tree in prob manager rather than reloading...
            dummyProblemManager = ProblemManager.FromXMLFile(problemManager.inputFile)
            dummyProblemManager.Initialize()

            # rerun to this point
            dummyProblemManager.RunUntil(prevAction)
        
            # run regional calculation and record max value
            dummyProblemManager.theRegionalCalculationManager.type = self.type
            dummyProblemManager.theRegionalCalculationManager.saveResult = False
            maxValues = dummyProblemManager.theRegionalCalculationManager.Run(dummyProblemManager)
        
            # reset params for min value
            theParameterManager.SetParameter(self.parameters[i],self.minValues[i])
            dummyProblemManager = ProblemManager.FromXMLFile(problemManager.inputFile)

            # reset params
            dummyProblemManager.Initialize()

            # rerun to this point
            dummyProblemManager.RunUntil(prevAction)
            dummyProblemManager.theRegionalCalculationManager.type = self.type
        
            # run regional calculation and record min value
            minValues = dummyProblemManager.theRegionalCalculationManager.Run(dummyProblemManager)
            dummyProblemManager.theRegionalCalculationManager.saveResult = False
        
            diffs.append( maxValues - minValues )
        
            ## fixme need to modify output control
            self.sens_keys.append(self.parameters[i])
            filename = problemManager.outputPrefix+"_snstvty_"+self.parameters[i]+"."+problemManager.outputType
            if problemManager.theRegionalCalculationManager.type == 'NPV':
              data = (maxValues - minValues)*1e-6
            else:
              data = (maxValues - minValues)
            problemManager.theRegionalCalculationManager.type = self.paramlist[self.sens_keys.index(self.parameters[i])]
            problemManager.theRegionalCalculationManager.SaveMap(filename, data, problemManager.recordRange)
            problemManager.theRegionalCalculationManager.type = self.type
        
            theParameterManager.SetParameter(self.parameters[i],originalValue)
        
        diffs = np.array(diffs)
        
        # reshape to image configuration [3,M,N] -> [M,N,3]
        diffs = np.moveaxis(diffs,0,-1)
        
        # pl.imshow(diffs,origin="lower")
        
        # pl.figure()
        # pl.imshow(diffs[:,:,0],origin="lower")
        # pl.colorbar()
        
        # pl.figure()
        # pl.imshow(diffs[:,:,1],origin="lower")
        # pl.colorbar()
        
        # pl.figure()
        # pl.imshow(diffs[:,:,2],origin="lower")
        # pl.colorbar()
        
        # pl.figure()
        # # trying hsv
        
        # np.save("diffs.npy",diffs)
        
        # from matplotlib.colors import hsv_to_rgb
        # x = 0.5*(diffs[:,:,1] - diffs[:,:,2])
        # y =  diffs[:,:,0] - 0.8660254037844386*(diffs[:,:,1] + diffs[:,:,2])
        # h = np.arctan2(y,x)/(2*np.pi)+0.5
        # # print "max h", np.nanmax(h)
        # s = np.sqrt(x**2 +  y**2)
        # v = np.sum(diffs,2)/3.
        # hsv = np.array([h,s,v])
        # hsv = np.moveaxis(hsv,0,-1)
        # hh = hsv_to_rgb(hsv)
        # pl.imshow(hh,origin="lower")
        
        # pl.show()
        
        if self.abs:
          diffs = np.abs(diffs)
        diffScale = np.nanmax(diffs) +1e-64 # np.nanmean(diffs)+2*np.nanstd(diffs)+1e-64  # np.nanmax(diffs)
        #print "diffScale", diffScale
        diffs = diffs/diffScale
        
        filename = problemManager.outputPrefix+"_snstvty_RGB."+problemManager.outputType
        # Process per-pixel diff values for better hue contrast
        diffs = diffs/diffs.sum(2).reshape((diffs.shape[0],diffs.shape[1],1))
        diffs = diffs/diffs.mean(2).reshape((diffs.shape[0],diffs.shape[1],1))
        diffs[diffs>1.] = 1.
        # Add transparency layer
        trans = (np.isnan(diffs[:,:,0]) & np.isnan(diffs[:,:,1]) & np.isnan(diffs[:,:,2])) == False
        diffs = np.concatenate((diffs, trans.reshape((trans.shape[0],trans.shape[1],1))), 2)
        # Adjust calculation manager type for appropriate plotting
        problemManager.theRegionalCalculationManager.type = "sensitivity"
        problemManager.theRegionalCalculationManager.SaveMap(filename, diffs, problemManager.recordRange)
        problemManager.theRegionalCalculationManager.type = self.type
        # Save parameter order
        filename = 'parameters.txt'
        with open(filename, 'w') as F:
            F.write(' '.join(self.parameters))
Ejemplo n.º 5
0
def GetAttributeString(node,attribute):
  """ Gets the attribute string."""
  rv = node.attrib[attribute]
  theParameterManager = ParameterManager()
  rv = theParameterManager.ReplaceParams(rv)
  return rv