Beispiel #1
0
 def Summary ( self, log = logFile, orderByMagnitude = False, total = None, totalKey = "Total" ):
     """Statistics summary."""
     def ExtractValue ( item ): return -item[1]
     if LogFileActive ( log ):
         # . Find total.
         if total is not None: localTotal = total
         else:                 localTotal = self[totalKey]
         localTotal = self.SummaryValue ( localTotal )
         # . Construct values to output.
         keyLength = 0
         keys      = self.keys ( )
         values    = []
         for ( key, value ) in self.iteritems ( ):
             keyLength  = max ( keyLength, len ( key ) )
             localValue = self.SummaryValue ( value )
             percentage = localValue / localTotal * 100.0
             values.append ( ( key, localValue, percentage ) )
         # . Sort.
         if orderByMagnitude: values.sort ( key = ExtractValue )
         else:                values.sort ( )
         # . Output.
         table = log.GetTable ( columns = [ max ( keyLength + 2, 20 ), 20, 15 ] )
         table.Start  ( )
         table.Title  ( self.SummaryTitle ( ) )
         table.Heading ( "Item"       )
         table.Heading ( "Time"       )
         table.Heading ( "Percentage" )
         for ( key, value, percentage ) in values:
             table.Entry (  key, alignment = "left"  )
             table.Entry ( "{:s}"   .format ( CPUTime.TimeToString ( value ) ) )
             table.Entry ( "{:8.3f}".format ( percentage                     ) )
         table.Stop ( )
 def Summary(self, log=logFile):
     """Summary."""
     if LogFileActive(log):
         # . Global data.
         summary = log.GetSummary()
         summary.Start("Regular Histogram Summary")
         summary.Entry("Bins", "{:d}".format(self.bins))
         summary.Entry("Counts", "{:d}".format(sum(self.counts)))
         summary.Stop()
         # . Dimension data.
         length = 0
         for dimension in self.dimensions:
             length = max(length, len(dimension.label))
         length = max(length + 2, 10)
         table = log.GetTable(columns=[length, 10] + 4 * [20])
         table.Start()
         table.Title("Regular Histogram Dimensions")
         table.Heading("Label")
         table.Heading("Bins")
         table.Heading("Bin Size")
         table.Heading("Lower")
         table.Heading("Upper")
         table.Heading("Period")
         for dimension in self.dimensions:
             table.Entry(dimension.label)
             table.Entry("{:d}".format(dimension.bins))
             table.Entry("{:.6g}".format(dimension.binSize))
             table.Entry("{:.6g}".format(dimension.lower))
             table.Entry("{:.6g}".format(dimension.upper))
             if dimension.isPeriodic:
                 table.Entry("{:.6g}".format(dimension.period))
             else:
                 table.Entry("")
         table.Stop()
Beispiel #3
0
 def Summary(self, log=logFile):
     """Summary."""
     if LogFileActive(log) and self.QPARSED:
         (head, tail) = os.path.split(self.name)
         summary = log.GetSummary()
         summary.Start(self.Label() + " Summary for " + "\"" + tail + "\"")
         summary.Entry("Lines Parsed", "{:d}".format(self.nlines))
         summary.Entry("Fatal Errors", "{:d}".format(self.nfatal))
         summary.Entry("Warnings",
                       "{:d}".format(self.nwarnings - self.nfatal))
         summary.Stop()
Beispiel #4
0
 def LogStart(self, state, log=logFile):
     """Start logging."""
     state.objectiveFunction.LogStart()
     if (self.logFrequency > 0) and LogFileActive(
             log, pushPriority=PrintPriority_Low):
         state.log = log
         state.table = log.GetTable(columns=[6, 6, 20, 20, 20])
         state.table.Start()
         state.table.Heading("Iteration", columnSpan=2)
         state.table.Heading("Function")
         state.table.Heading("Gradient")
         state.table.Heading("Variable")
 def LogStart(self, state, log=logFile):
     """Start logging."""
     state.objectiveFunction.LogStart()
     if (self.logFrequency > 0) and LogFileActive(
             log, pushPriority=PrintPriority_Low):
         state.log = log
         state.table = log.GetTable(
             columns=(len(state.accumulableAttributes) + 1) *
             [_TableColumnWidth])
         state.table.Start()
         state.table.Title(self.Label() + " Results")
         state.table.Heading("Time")
         for tag in state.accumulableLabels:
             state.table.Heading(tag)
Beispiel #6
0
 def TestHessian(self, delta=1.0e-4, log=logFile, tolerance=1.0e-4):
     """Compare analytical and numerical hessian."""
     maxDiff = 0.0
     nvariables = self.NumberOfVariables()
     if LogFileActive(log) and (nvariables > 0):
         # . Allocate arrays.
         g = Real1DArray.WithExtent(nvariables)
         ha = SymmetricMatrix.WithExtent(nvariables)
         x = self.VariablesAllocate()
         # . Analytical calculation.
         self.VariablesGet(x)
         f = self.FunctionGradientsHessian(x, g, ha)
         # . Numerical calculation.
         hn = self.NumericalHessian(x, delta=delta)
         if (ha is not None) and (hn is not None):
             # . Compute the maximum absolute difference between the gradients.
             for i in range(nvariables):
                 for j in range(i + 1):
                     diff = math.fabs(ha[i, j] - hn[i, j])
                     maxDiff = max(maxDiff, diff)
             # . Output only those differences which are bigger than tolerance.
             if maxDiff > tolerance:
                 table = log.GetTable(columns=[10, 10, 20, 20, 20])
                 table.Start()
                 table.Title("Analytical and Numerical Hessians")
                 table.Heading("Variables", columnSpan=2)
                 table.Heading("Analytical")
                 table.Heading("Numerical")
                 table.Heading("|Difference|")
                 for i in range(nvariables):
                     for j in range(i + 1):
                         diff = math.fabs(ha[i, j] - hn[i, j])
                         if diff > tolerance:
                             table.Entry("{:d}".format(i))
                             table.Entry("{:d}".format(j))
                             table.Entry("{:20.6f}".format(ha[i, j]))
                             table.Entry("{:20.6f}".format(hn[i, j]))
                             table.Entry("{:20.6f}".format(diff))
                 table.Stop()
             # . Output the biggest difference.
             log.Paragraph(
                 "Maximum absolute hessian difference = {:.6f}".format(
                     maxDiff))
     return maxDiff
Beispiel #7
0
    def Parse(self, log=logFile):
        """Parsing."""
        if not self.QPARSED:
            # . Initialization.
            if LogFileActive(log): self.log = log
            # . Open the file.
            self.Open()
            # . Parse all the lines.
            try:
                while True:
                    line = self.GetLine()
                    if line is None: break
            except EOFError:
                pass
# . Close the file.
            self.WarningStop()
            self.Close()
            # . Set the parsed flag and some other options.
            self.log = None
            self.QPARSED = True
Beispiel #8
0
 def TestGradients(self, delta=1.0e-4, log=logFile, tolerance=1.0e-4):
     """Compare analytical and numerical derivatives."""
     maxDiff = 0.0
     nvariables = self.NumberOfVariables()
     if LogFileActive(log) and (nvariables > 0):
         # . Allocate arrays.
         ga = Real1DArray.WithExtent(nvariables)
         ga.Set(0.0)
         x = self.VariablesAllocate()
         # . Analytical calculation.
         self.VariablesGet(x)
         f = self.FunctionGradients(x, ga)
         # . Numerical calculation.
         gn = self.NumericalGradients(x, delta=delta)
         if (ga is not None) and (gn is not None):
             # . Compute the maximum absolute difference between the gradients.
             for i in range(nvariables):
                 diff = math.fabs(ga[i] - gn[i])
                 maxDiff = max(maxDiff, diff)
             # . Output only those differences which are bigger than tolerance.
             if maxDiff > tolerance:
                 table = log.GetTable(columns=[10, 20, 20, 20])
                 table.Start()
                 table.Title("Analytical and Numerical Gradients")
                 table.Heading("Variable")
                 table.Heading("Analytical")
                 table.Heading("Numerical")
                 table.Heading("|Difference|")
                 for i in range(nvariables):
                     diff = math.fabs(ga[i] - gn[i])
                     if diff > tolerance:
                         table.Entry("{:d}".format(i))
                         table.Entry("{:20.6f}".format(ga[i]))
                         table.Entry("{:20.6f}".format(gn[i]))
                         table.Entry("{:20.6f}".format(diff))
                 table.Stop()
             # . Output the biggest difference.
             log.Paragraph(
                 "Maximum absolute gradient difference = {:.6f}".format(
                     maxDiff))
     return maxDiff
Beispiel #9
0
 def TestHessian(self, delta=1.0e-4, log=logFile, tolerance=1.0e-4):
     """Compare analytical and numerical hessian."""
     maxDiff = 0.0
     if LogFileActive(log):
         # . Analytical and numerical calculation.
         (f, gA, hA) = self.FunctionGradientHessian(self.variable)
         hN = self.NumericalHessian(self.variable, delta=delta)
         maxDiff = math.fabs(hA - hN)
         # . Output the difference only if it is bigger than tolerance.
         if maxDiff > tolerance:
             table = log.GetTable(columns=[20, 20])
             table.Start()
             table.Title("Analytical and Numerical Hessian")
             table.Entry("Analytical")
             table.Entry("{:20.6f}".format(hA))
             table.Entry("Numerical")
             table.Entry("{:20.6f}".format(hN))
             table.Entry("|Difference|")
             table.Entry("{:20.6f}".format(maxDiff))
             table.Stop()
     return maxDiff
Beispiel #10
0
 def Summary(self, log=logFile, pageWidth=100, valueWidth=14):
     """Summary."""
     if LogFileActive(log):
         summary = log.GetSummary(pageWidth=pageWidth,
                                  valueWidth=valueWidth)
         summary.Start(self.Label() + " Options")
         keys = self.__class__.defaultAttributeNames.keys()
         keys.sort()
         for key in keys:
             value = getattr(self,
                             self.__class__.defaultAttributeNames[key])
             if isinstance(value, bool):
                 if value: valuestring = "True"
                 else: valuestring = "False"
             elif isinstance(value, float):
                 valuestring = "{:g}".format(value)
             elif isinstance(value, basestring):
                 valuestring = value
             else:
                 valuestring = str(value)
             summary.Entry(key, valuestring)
         summary.Stop()
Beispiel #11
0
 def LogStart(self, state, log=logFile):
     """Start logging."""
     if (self.logFrequency > 0) and LogFileActive(
             log, pushPriority=PrintPriority_Low):
         state.log = log