Пример #1
0
    def record(self, point, gradientDict, projectedGradFunc,
               normAfterProjection, modelsTable):
        if not self.doRecording:
            return 1

        assert len(point) == len(
            self.independentVars
        ), "Point length doesnt match # independent vars!"
        roundArray = frompyfunc(round, 2, 1)
        point = tuple(roundArray(point, 10))

        pointAsStr = str(point).replace(" ", "")

        gradDictAsStr = str(gradientDict).replace(" ", "")

        projGradFuncAsStr = str(tuple(projectedGradFunc)).replace(" ", "")

        queryDict = {
            self.independentVars[i]: point[i]
            for i in range(len(point))
        }

        cursor = self.dbConnection.cursor()

        print "Fetching point entry data from database '%s'  for recording" % modelsTable
        entryData = list(
            queryDBGivenParams("*", queryDict, cursor, modelsTable)[0])

        #Stupid table strings are recorded in type unicode and mess up upon re-insertion...
        for i, ent in enumerate(entryData):
            if isinstance(ent, unicode):
                entryData[i] = str(ent)

        entry = [
            self.pointNumber, pointAsStr, gradDictAsStr, projGradFuncAsStr,
            normAfterProjection
        ]
        entry = entry + entryData
        #print str(tuple(entry))

        existingPoint = queryDBGivenParams("pointNum",
                                           {'pointNum': self.pointNumber},
                                           cursor, self.trackTableName)
        if existingPoint:
            print "POINT NUM %s EXISTS IN RECORD TABLE %s" % (
                existingPoint[0], self.trackTableName)
        else:
            print "Inserting new point into record table '%s'" % self.trackTableName
            cursor.execute("INSERT INTO " + self.trackTableName + " VALUES " +
                           str(tuple(entry)))

        self.pointNumber += 1
        print
        self.dbConnection.commit()
        return
Пример #2
0
 def lookup(x, y):
     assert x >= 0., "Lookup only well defined for x >=0"
     assert y >= 0., "Lookup only well defined for y >=0"
     tolerance = 1.0001
     additionalConditions = []
     if not x == 0:
         additionalConditions.append(xname + ">" + str(x / tolerance))
         additionalConditions.append(xname + "<" + str(x * tolerance))
     else:
         additionalConditions.append(xname + "=" + str(x))
     if not y == 0:
         additionalConditions.append(yname + ">" + str(y / tolerance))
         additionalConditions.append(yname + "<" + str(y * tolerance))
     else:
         additionalConditions.append(yname + "=" + str(y))
     additionalConditions.append(condition)
     #print additionalConditions
     value = queryDBGivenParams(self.dependentVar, {}, cursor, "models",
                                tuple(additionalConditions))
     #print x,y, value
     if value:
         if len(value[0]) > 1:
             print "WARNING MORE THAN ONE VALUE FOUND!!!"
         return value[0][0]
     else:
         print "WARNING: no value"
         return 0.0
Пример #3
0
 def lookup(x,y):
     assert x >= 0.,  "Lookup only well defined for x >=0"
     assert y >= 0.,  "Lookup only well defined for y >=0"
     tolerance = 1.0001
     additionalConditions=[]
     if not x == 0:
         additionalConditions.append( xname + ">" + str(x/tolerance) )
         additionalConditions.append( xname + "<" + str(x*tolerance) )
     else:
         additionalConditions.append(xname + "=" + str(x))
     if not y == 0:
         additionalConditions.append(yname + ">" + str(y/tolerance))
         additionalConditions.append( yname + "<" + str(y*tolerance))
     else:
         additionalConditions.append(yname + "=" + str(y))
     additionalConditions.append(condition)
     #print additionalConditions
     value= queryDBGivenParams(self.dependentVar,
         {},
         cursor,"models", tuple( additionalConditions ))
     #print x,y, value
     if value:
         if len(value[0]) > 1 :
             print "WARNING MORE THAN ONE VALUE FOUND!!!"
         return value[0][0]
     else:
         print "WARNING: no value"
         return 0.0
Пример #4
0
    def getColumnData(self, colVars, colFunction, filters=()):

        rawData = queryDBGivenParams(colVars, [], self.dbConn.cursor(), 'models', filters,
                                     " ORDER BY " + self.orderByVar)
        #print rawData
        # light todo: add try catch to check num args
        rawData = [colFunction(*entry) for entry in rawData]
        #print rawData
        return rawData
Пример #5
0
    def record(self,point,gradientDict,projectedGradFunc,normAfterProjection, modelsTable):
        if not self.doRecording:
            return 1

        assert len(point) == len(self.independentVars), "Point length doesnt match # independent vars!"
        roundArray=frompyfunc(round,2,1)
        point = tuple(roundArray(point,10))

        pointAsStr= str(point).replace(" ","")

        gradDictAsStr =str(gradientDict).replace(" ","")

        projGradFuncAsStr=str(tuple(projectedGradFunc)).replace(" ","")


        queryDict = {self.independentVars[i]:point[i] for i in range(len(point))}

        cursor=self.dbConnection.cursor()

        print "Fetching point entry data from database '%s'  for recording" % modelsTable
        entryData = list(queryDBGivenParams("*",queryDict,cursor,modelsTable)[0])

        #Stupid table strings are recorded in type unicode and mess up upon re-insertion...
        for i,ent in enumerate(entryData):
            if isinstance(ent,unicode):
                entryData[i]=str(ent)


        entry = [self.pointNumber, pointAsStr,gradDictAsStr,projGradFuncAsStr,normAfterProjection]
        entry = entry + entryData
        #print str(tuple(entry))

        existingPoint = queryDBGivenParams("pointNum",{'pointNum':self.pointNumber},
            cursor,self.trackTableName)
        if  existingPoint :
            print "POINT NUM %s EXISTS IN RECORD TABLE %s" % (existingPoint[0],self.trackTableName)
        else:
            print "Inserting new point into record table '%s'" % self.trackTableName
            cursor.execute("INSERT INTO "+self.trackTableName+" VALUES "+ str(tuple(entry)) )

        self.pointNumber+=1
        print
        self.dbConnection.commit()
        return
Пример #6
0
def edOfMaxMass(filters, connection, prescription=paramsDict):
    c=connection.cursor()
    answer={'max':-1.e10,'edMax':0.0,'RedMax':False, 'symbol': 'o'}
    values = queryDBGivenParams("gravMass,edMax,RedMax",(),c,"models",filters)
    for max,edMax,RedMax in values:
        if max > answer['max']:
            answer['max']=max
            answer['edMax']=edMax
            answer['RedMax']=RedMax
    return answer
Пример #7
0
    def getColumnData(self, colVars, colFunction, filters=()):

        rawData = queryDBGivenParams(colVars, [], self.dbConn.cursor(),
                                     'models', filters,
                                     " ORDER BY " + self.orderByVar)
        #print rawData
        # light todo: add try catch to check num args
        rawData = [colFunction(*entry) for entry in rawData]
        #print rawData
        return rawData
Пример #8
0
def edOfMaxMass(filters,connection):
    c=connection.cursor()
    answer={'max':-1.e10,'edMax':0.0,'RedMax':False}
    values = queryDBGivenParams("gravMass,edMax,RedMax,rpoe",(),c,"models",filters)
    for max,edMax,RedMax,rpoe in values:
        if max > answer['max']:
            answer['max']=max
            answer['edMax']=edMax
            answer['RedMax']=RedMax
            answer['rpoe'] =rpoe
    return answer
Пример #9
0
def edOfMaxMass(filters, connection):
    c = connection.cursor()
    answer = {'max': -1.e10, 'edMax': 0.0, 'RedMax': False}
    values = queryDBGivenParams("gravMass,edMax,RedMax", (), c, "models",
                                filters)
    for max, edMax, RedMax in values:
        if max > answer['max']:
            answer['max'] = max
            answer['edMax'] = edMax
            answer['RedMax'] = RedMax
    return answer
Пример #10
0
def sequencePlot(plotFields,
                 sqliteCursor,
                 filters=(),
                 colorBy=None,
                 tableName="models",
                 grid=True,
                 title="",
                 suppressShow=False,
                 orderBy=None,
                 forceColorbar=False,
                 **mplKwargs):
    """
    plotFields:        2-list, fields to plot
    sqliteCursor:      sqlite3.connection.cursor object for database
    filters:           list of strings for sqlite WHERE filters
    colorBy:           table field to color the points by
    tableName:         name of database table accessible from sqliteCursor
    mplKwargs:         keyword arguments passed on to matplotlib plotting function

    Note:  If colorBy is set, plots a scatter plot instead of regular plot!
           Remember to adjust mplKwargs accordingly in this case
    """
    getFields = plotFields[:]
    if colorBy:
        getFields.append(colorBy)
    if orderBy is None:
        orderBy = plotFields[0]
    points = queryDBGivenParams(getFields, [], sqliteCursor, tableName,
                                filters, " ORDER BY " + orderBy)

    #fig = mpl.figure()
    #axis = fig.add_subplot(111)
    mpl.title(title)
    mpl.grid(grid)
    if colorBy:
        mpl.scatter(*zip(*points)[:2], c=zip(*points)[-1], **mplKwargs)
    else:
        mpl.plot(*zip(*points)[:2], **mplKwargs)

    #axis.set_xlabel(getFields[0])
    #axis.set_ylabel(getFields[1])
    mpl.xlabel(getFields[0])
    mpl.ylabel(getFields[1])
    if colorBy and not suppressShow:
        colorLegend = mpl.colorbar()
        colorLegend.set_label(colorBy)
    if forceColorbar:
        colorLegend = mpl.colorbar()
        colorLegend.set_label(colorBy)
    print "Plotting %i entries" % len(points)
    if not suppressShow:
        mpl.show()
Пример #11
0
    def __init__(self, dbFilenames, trackTableName, trackVars):

        assert isinstance(trackVars, tuple)
        self.dbFilenames = dbFilenames
        #TODO: what if track tablenames differ between dbNames!
        self.trackTableName = trackTableName
        self.trackMetaTable = trackTableName + "Metadata"
        self.trackVars = trackVars

        for file in self.dbFilenames:
            thisFilesData = {key: [] for key in trackVars}
            connection = sqlite3.connect(file)

            metadataDict = self.readTrackMetadata(connection)
            thisFilesData.update(metadataDict)
            c = connection.cursor()
            rawData = queryDBGivenParams(
                ["pointNum", "point", "gradDict", "projGrad", "normProj"] +
                list(trackVars), {}, c, self.trackTableName, (),
                " ORDER BY pointNum")
            pointList = []
            gradDictList = []
            projGradList = []
            normProjList = []
            for entry in rawData:
                #print entry
                point = ast.literal_eval(entry[1])
                gradDict = ast.literal_eval(entry[2])
                projGrad = ast.literal_eval(entry[3])
                normProj = entry[4]
                gradDictList.append(gradDict)
                projGradList.append(projGrad)
                normProjList.append(normProj)
                for i, key in enumerate(trackVars):
                    thisFilesData[key].append(entry[
                        i +
                        5])  #since there are 5 variables before the trackVars

                pointList.append(point)
            thisFilesData.update({
                'points': pointList,
                'gradDicts': gradDictList,
                'projGrads': projGradList,
                'normProjs': normProjList
            })
            print thisFilesData
            self.trackData.append(thisFilesData)
Пример #12
0
    def checkIfRunExistsInDB(self, inputParams, cursor):
        myParams = inputParams.copy()
        myParams['eos'] = self.getEosName()
        for key in ('ye', 'rollMid', 'rollScale', 'eosTmin'):
            if key in self.eosPrescriptionDict:
                myParams[key] = self.eosPrescriptionDict[key]
            else:
                myParams[key] = str(None)
                if self.eosPrescriptionDict['prescriptionName'] == 'manual':
                    #HACK for kentaDataTofLogRhoFit2
                    if  self.eosPrescriptionDict['funcTofLogRho'] == 'kentaDataTofLogRhoFit2':
                        myParams['eosTmin'] = 5.0

        for key in ('quantity', 'target'):
            keyForDatabase = 'fixed' + key.capitalize()
            if key in self.eosPrescriptionDict:
                myParams[keyForDatabase] = self.eosPrescriptionDict[key]
            else:
                myParams[keyForDatabase] = str(None)
        if self.eosPrescriptionDict['prescriptionName'] == 'fixedQuantity' \
            or self.eosPrescriptionDict['prescriptionName'] == 'manual':
            myParams['T'] = str(None)

        #edMin not relevant for run type one model...
        #I think the following to do is fine now
        #TODO: modify checkIfRunExistsInDB to work properly with mass shed sequences!
        if self.runType == 30 and 'edMin' in myParams:
            del myParams['edMin']
        elif self.runType == 3 and 'edMin' in myParams:
            del myParams['edMin']
        elif self.runType == 3 and 'Nsteps' in myParams:
            del myParams['Nsteps']

        listResult = queryDBGivenParams('runID', myParams, cursor, self.tableName)

        if listResult:
            if len(listResult) > 1 or len(listResult[0]) > 1:
                print "Wow, this entry: "
                print myParams
                print "exists more than once in the database!"
                #raise AssertionError
            result = listResult[0][0]
            print " Parameters Have already been run in ID '%s': \n%s" % (result, myParams)

        return listResult
Пример #13
0
def sequencePlot(plotFields, sqliteCursor, filters=(), colorBy=None, tableName="models",
                 grid=True, title="", suppressShow=False, orderBy=None, forceColorbar=False,
                 **mplKwargs):
    """
    plotFields:        2-list, fields to plot
    sqliteCursor:      sqlite3.connection.cursor object for database
    filters:           list of strings for sqlite WHERE filters
    colorBy:           table field to color the points by
    tableName:         name of database table accessible from sqliteCursor
    mplKwargs:         keyword arguments passed on to matplotlib plotting function

    Note:  If colorBy is set, plots a scatter plot instead of regular plot!
           Remember to adjust mplKwargs accordingly in this case
    """
    getFields = plotFields[:]
    if colorBy:
        getFields.append(colorBy)
    if orderBy is None:
        orderBy = plotFields[0]
    points = queryDBGivenParams(getFields, [], sqliteCursor,
                                tableName, filters, " ORDER BY " + orderBy)

    #fig = mpl.figure()
    #axis = fig.add_subplot(111)
    mpl.title(title)
    mpl.grid(grid)
    if colorBy:
        mpl.scatter(*zip(*points)[:2], c=zip(*points)[-1], **mplKwargs)
    else:
        mpl.plot(*zip(*points)[:2], **mplKwargs)

    #axis.set_xlabel(getFields[0])
    #axis.set_ylabel(getFields[1])
    mpl.xlabel(getFields[0])
    mpl.ylabel(getFields[1])
    if colorBy and not suppressShow:
        colorLegend = mpl.colorbar()
        colorLegend.set_label(colorBy)
    if forceColorbar:
        colorLegend = mpl.colorbar()
        colorLegend.set_label(colorBy)
    print "Plotting %i entries" % len(points)
    if not suppressShow:
        mpl.show()
Пример #14
0
    def __init__(self,dbFilenames,trackTableName,trackVars):

        assert isinstance(trackVars,tuple)
        self.dbFilenames=dbFilenames
        #TODO: what if track tablenames differ between dbNames!
        self.trackTableName=trackTableName
        self.trackMetaTable=trackTableName+"Metadata"
        self.trackVars=trackVars


        for file in self.dbFilenames:
            thisFilesData={key:[] for key in trackVars}
            connection=sqlite3.connect(file)

            metadataDict=self.readTrackMetadata(connection)
            thisFilesData.update(metadataDict)
            c=connection.cursor()
            rawData=queryDBGivenParams(["pointNum","point","gradDict","projGrad","normProj"]+list(trackVars),
                                       {},c,self.trackTableName,(), " ORDER BY pointNum" )
            pointList=[]
            gradDictList=[]
            projGradList=[]
            normProjList=[]
            for entry in rawData:
                #print entry
                point = ast.literal_eval( entry[1] )
                gradDict = ast.literal_eval( entry[2])
                projGrad = ast.literal_eval( entry[3])
                normProj = entry[4]
                gradDictList.append(gradDict)
                projGradList.append(projGrad)
                normProjList.append(normProj)
                for i,key in enumerate(trackVars):
                    thisFilesData[key].append( entry[ i + 5]) #since there are 5 variables before the trackVars

                pointList.append(point)
            thisFilesData.update({'points':pointList,'gradDicts':gradDictList,
                                  'projGrads':projGradList, 'normProjs':normProjList})
            print thisFilesData
            self.trackData.append(thisFilesData)
Пример #15
0
def manyScriptSequencePlot(plotFields,
                           sqliteCursor,
                           filters=(),
                           colorBy=None,
                           maxPlot=None,
                           grid=False,
                           title="",
                           orderBy=None,
                           forceColorbar=False,
                           suppressShow=False,
                           legendList=[],
                           vmax=None,
                           vmin=None,
                           loc=0,
                           minRpoeOnly=False,
                           **mplKwargs):
    """
    Version of sequence plot for plotting huge amount of data
    assumes tablename is models
    if maxPlot plots maximum value of quantity for sequence only
    """
    orderBy = plotFields[0] + ',' + ','.join(
        tempPrescription.prescriptionParameters) + ",eos,ye,edMax"
    #orderBy = 'rpoe'+','+ ','.join(eosPrescription.prescriptionParameters) + ",eos,ye,edMax"
    #print orderBy
    size = 40
    plotLines = False
    #filters = ("ye=0.15",)
    filters += ("arealR < 100", )
    tableName = "models"
    if colorBy is None:
        colorBy = "arealR"
    getFields = plotFields[:]  # x-points 0, y-points 1
    getFields.append(colorBy)  # colorBy 2
    if orderBy is None:
        orderBy = plotFields[0]

    for key in tempPrescription.prescriptionParameters:
        getFields.append(key)  # script parameters 3-8
    getFields.append('RedMax')  # RedMax 9
    getFields.append('ye')  # ye 10
    getFields.append('eos')  # eos 11
    if maxPlot is not None:
        getFields.append(maxPlot)  # maxPlot 12
    if minRpoeOnly:
        getFields.append('runID')  # runIDcol
        runIDcol = len(getFields) - 1
        getFields.append('lineNum')  # lineNumcol
        lineNumcol = len(getFields) - 1
        orderBy = 'runID,lineNum,' + orderBy

    points = queryDBGivenParams(getFields, [], sqliteCursor, tableName,
                                filters, " ORDER BY " + orderBy)
    assert points, "No points returned for query with filters: %s" % str(
        filters)
    currentSymbol = ""
    previousSymbol = ""
    previousName = ""
    currentName = ""
    currentPoints = []
    currentToroidPoints = []
    currentEos = None
    currentYe = None
    previousEos = None
    previousYe = None
    previousPoint = None
    if vmax is None:
        vmax = max([point[2] for point in points])
    if vmin is None:
        vmin = min([point[2] for point in points])

    #print vmin, vmax
    for i, point in enumerate(points):
        #print point
        currentYe = point[10]
        currentEos = point[11]
        prescriptDict = dict()
        #print currentSymbol, previousSymbol
        for j, scriptParam in enumerate(point[3:3 + 6]):
            prescriptDict[
                tempPrescription.prescriptionParameters[j]] = scriptParam
        #print prescriptDict
        currentSymbol, currentName = symbolFromDBentry(prescriptDict)
        ax = plt.gca()
        handles, labels = ax.get_legend_handles_labels()
        #print currentName, labels
        if currentName in labels:
            currentName = None
            previousName = None
        if currentSymbol == previousSymbol and currentEos == previousEos and currentYe == previousYe:
            currentPoints.append(point)
            if point[9] > 0.0:
                #print "TOROIZZLE"
                currentToroidPoints.append(point)
        elif not previousSymbol == "" and not currentPoints == []:
            if minRpoeOnly:
                currentPoints = reduceToMinRpoeOnly(currentPoints, runIDcol,
                                                    lineNumcol)
            if maxPlot is not None:
                currentPoints = [getPointWithMaxOfCol(currentPoints, 12)]
                if currentPoints[0][9] == 0.0:
                    #print "antigotcha "
                    currentToroidPoints = []
                else:
                    currentToroidPoints = [
                        getPointWithMaxOfCol(currentToroidPoints, 12)
                    ]
            currentPoints = zip(*currentPoints)
            print currentPoints
            plt.scatter(*currentPoints[:2],
                        c=currentPoints[2],
                        marker=previousSymbol,
                        label=previousName,
                        vmax=vmax,
                        vmin=vmin,
                        s=size,
                        **mplKwargs)
            if plotLines:
                plt.plot(*currentPoints[:2],
                         ls=getLinestyle(previousEos, previousYe),
                         c='k',
                         lw=.5)
            currentPoints = []
            #legendList.append(currentName)
            if not currentToroidPoints == []:
                currentToroidPoints = zip(*currentToroidPoints)
                #below line sets circle color to black
                currentToroidPoints[2] = [(0., 0., 0.)
                                          for _ in currentToroidPoints[2]]
                plt.scatter(*currentToroidPoints[:2],
                            c=currentToroidPoints[2],
                            marker='o',
                            facecolor='none',
                            s=size * 2,
                            vmax=vmax,
                            vmin=vmin,
                            **mplKwargs)
                currentToroidPoints = []
        previousSymbol = currentSymbol
        previousEos = currentEos
        previousYe = currentYe
        previousName = currentName
        previousPoint = point
        #symbolList.append(symbolFromDBentry(prescriptDict))
    print "Done with loop"
    #Don't forget to plot the last set!
    #print getPointWithMaxOfCol(currentPoints, 12)
    if minRpoeOnly:
        currentPoints = reduceToMinRpoeOnly(currentPoints, runIDcol,
                                            lineNumcol)
    if maxPlot is not None:
        currentPoints = [getPointWithMaxOfCol(currentPoints, 12)]
        if currentPoints[0][9] == 0.0:
            currentToroidPoints = []
    currentPoints = zip(*currentPoints)
    plt.scatter(*currentPoints[:2],
                c=currentPoints[2],
                marker=previousSymbol,
                label=previousName,
                vmax=vmax,
                vmin=vmin,
                s=size,
                **mplKwargs)
    if plotLines:
        plt.plot(*currentPoints[:2],
                 ls=getLinestyle(previousEos, previousYe),
                 c='k',
                 lw=.5)
    #legendList.append(currentName)
    #Set colorbar before doing toroidal points because toroidal points have black coloring
    if colorBy and not suppressShow:
        colorLegend = plt.colorbar()
        colorLegend.set_label(latexField(colorBy))
    if forceColorbar:
        colorLegend = plt.colorbar()
        colorLegend.set_label(latexField(colorBy))
    if not currentToroidPoints == []:
        if maxPlot is not None:
            currentToroidPoints = [
                getPointWithMaxOfCol(currentToroidPoints, 12)
            ]
        currentToroidPoints = zip(*currentToroidPoints)
        #print "dur", currentToroidPoints
        #below line sets circle color to black
        currentToroidPoints[2] = [(0., 0., 0.) for _ in currentToroidPoints[2]]
        plt.scatter(*currentToroidPoints[:2],
                    c=currentToroidPoints[2],
                    marker='o',
                    facecolor='none',
                    s=size * 2,
                    vmax=vmax,
                    vmin=vmin,
                    **mplKwargs)

    plt.title(title)
    plt.grid(grid)

    #axis.set_xlabel(getFields[0])
    #axis.set_ylabel(getFields[1])
    plt.xlabel(latexField(getFields[0]))
    plt.ylabel(latexField(getFields[1]))
    print "Plotting %i entries" % len(points)

    if not suppressShow:
        #ax = plt.gca()
        #handles, labels = ax.get_legend_handles_labels()
        plt.legend(loc=loc)
        plt.show()
        legendList = []
Пример #16
0
def steepestDescent(funcName,
                    fixedNames,
                    inBasis,
                    firstDeriv,
                    p0,
                    deltas,
                    sqliteConnection,
                    modelGen,
                    stationaryParamsDict,
                    maxSteps,
                    changeBasis=False):
    assert isinstance(funcName, str)
    assert isinstance(fixedNames, tuple)
    if not len(fixedNames) == 0:
        assert isinstance(fixedNames[0], str)
    assert isinstance(inBasis, basis)
    assert isinstance(firstDeriv, deriv)
    assert isinstance(deltas, ndarray)
    assert inBasis.dim == len(
        deltas), "Dimension of your deltas must be same as input basis!"
    assert isinstance(sqliteConnection, sqlite3.Connection)
    assert isinstance(modelGen, modelGenerator)
    assert isinstance(p0, tuple)
    assert inBasis.dim == len(
        p0), "Dimension of your starting point must be same as input basis!"
    assert isinstance(stationaryParamsDict, dict)

    recorder = trackRecorder("track", sqliteConnection, inBasis.axesNames)
    recorder.recordTrackMetadata(inBasis.axesNames, funcName, fixedNames,
                                 firstDeriv.name, deltas, stationaryParamsDict,
                                 maxSteps, changeBasis)
    dim = inBasis.dim

    stencil = firstDeriv.stencil.indices[0]
    continueStepping = True
    step = 0
    currentBasis = deepcopy(inBasis)
    currentPoint = p0
    pointList = [p0]
    gradientDictList = []
    fixedSubspaceList = []
    projectedGradientList = []
    normAfterProjectionList = []
    basisList = []
    funcNamesList = {funcName: []}
    funcNamesList.update({key: [] for key in fixedNames})
    while continueStepping:
        step += 1
        if step > maxSteps:
            print "Maximum steps reached in steepestDescent"
            continueStepping = False
            break
        paramsDict = stationaryParamsDict.copy()

        paramsToRun = []
        paramsNeededForIthBasisVector = []
        for i in range(dim):
            ithPointsDesired = array(currentPoint) + [
                index * currentBasis.basis[i] * deltas for index in stencil
            ]
            ithPointsDesired = roundArray(ithPointsDesired, 10)
            ithParamsNeeded = []
            for j in ithPointsDesired:
                dictUpdate = {
                    currentBasis.axesNames[k]: j[k]
                    for k in range(dim)
                }
                paramsDict.update(dictUpdate)
                ithParamsNeeded.append(paramsDict.copy())
                if paramsDict in paramsToRun:
                    #print "ARRR MATEY, ALREAYD GOT THAT ONE"
                    #print paramsDict
                    pass
                else:
                    paramsToRun.append(paramsDict.copy())
            paramsNeededForIthBasisVector.append(ithParamsNeeded)
        modelGen.generateModels(modelGen.runRotNS, paramsToRun,
                                sqliteConnection)

        gradientDict = {funcName: []}
        gradientDict.update({key: [] for key in fixedNames})

        funcsDesired = [funcName] + list(fixedNames)
        sqliteConnection.commit()
        sqliteCursor = sqliteConnection.cursor()
        for i in range(dim):
            for j in paramsNeededForIthBasisVector[i]:
                funcs = queryDBGivenParams(funcsDesired,
                                           j,
                                           sqliteCursor,
                                           tableName="models")
                assert len(
                    funcs
                ), "QUERY RETURNED NO ENTRIES, MATCHING PARAMS: %s" % str(j)
                if len(funcs) > 1:
                    print "WWWARNING, MORE THAN ONE MODEL WITH PARAMS: ", j
                    print "results of models with those params in %s form: " % funcsDesired, funcs
                    last = funcs[0]
                    for k in range(len(funcs)):
                        if not last == funcs[k]:
                            print "OH WOW AND THEY HAVE DIFFERENT RESULTS... REALLY SHOULD ABORT"
                        last = funcs[k]

                newEntries = dict([
                    (funcsDesired[key], newVal) for key, newVal in enumerate(
                        queryDBGivenParams(
                            funcsDesired, j, sqliteCursor, tableName="models")
                        [0])
                ])
                j.update(newEntries)
            #print paramsNeededForIthBasisVector[i]

            #following yields the step for the derivative
            stepInIthDir = dot(currentBasis.basis[i], deltas)
            #here we save the function values and calculate their derivative
            #the function value at the point we are evaluating has stencil index0
            for func in funcsDesired:
                funcVals = [
                    paramsDict[func]
                    for paramsDict in paramsNeededForIthBasisVector[i]
                ]
                k = len(firstDeriv.coeffs) / 2  #default assume centered list
                for k, stencilIndex in enumerate(*firstDeriv.stencil.indices):
                    if stencilIndex == 0:
                        break
                if i == 0:  #only do this once
                    funcNamesList[func].append(funcVals[k])
                #print funcVals
                gradientDict[func].append(
                    dot(firstDeriv.coeffs, funcVals) / stepInIthDir)
        #print gradientDict
        gradientDictList.append(gradientDict)
        badSubspace = array(
            [gradientDict[fixedName] for fixedName in fixedNames])
        fixedSubspace = removeSubspace(currentBasis, badSubspace)
        #print fixedSubspace
        fixedSubspaceList.append(fixedSubspace)
        projectedGradFunc = zeros(dim)
        for projection in [
                project(gradientDict[funcName], vec) for vec in fixedSubspace
        ]:
            projectedGradFunc += projection
        normAfterProjectionList.append(norm(projectedGradFunc))
        projectedGradFunc = projectedGradFunc / norm(projectedGradFunc)
        #print projectedGradFunc
        projectedGradientList.append(projectedGradFunc)
        basisList.append(currentBasis.basis)
        if changeBasis:
            newBasisVectors = []
            for i in badSubspace:
                newBasisVectors.append(i)
            for i in fixedSubspace:
                newBasisVectors.append(i)

            currentBasis = basis(array(newBasisVectors),
                                 currentBasis.axesNames)

        print currentBasis.basis, currentBasis.isOrthogonal()
        recorder.record(currentPoint, gradientDict, projectedGradFunc,
                        normAfterProjectionList[step - 1], "models")
        currentPoint += -projectedGradFunc * norm(deltas) / 4.0
        pointList.append(deepcopy(currentPoint))
        print "-----------NEW POINT---------------"
        print currentPoint
        print
        print "   CURRENT POINT LIST         "
        print array(pointList)
        print "    CURRENT FUNCS LIST        "
        print {(key, value[step - 1]) for key, value in funcNamesList.items()}
        print "----------DONE THIS STEP-----------"
        print

    print "-----------------FINAL POINT LIST ---------------"
    print
    print array(pointList)
    print funcNamesList
    print
    print "-------------------------------------------------"
    return array(pointList), funcNamesList,gradientDictList,fixedSubspaceList,\
           projectedGradientList, normAfterProjectionList
Пример #17
0
    fig.set_size_inches(24, 13.5)

    sequencePlot([xaxis, "ToverW"],
                 c,
                 filters,
                 colorBy=colorByField,
                 suppressShow=True,
                 forceColorbar=True)
    fig.savefig("plots/cb-" + colorByField + "-" + xaxis + "VS-a" + str(a) +
                "-T" + str(T) + ".png")
    fig.clf()

exit()

filters = ("RedMax=0", ) + filtersT
valuesS = queryDBGivenParams("a,edMax,rpoe,RedMax", (), c, "models", filters)
print "Got spheroids, ", len(valuesS)
filters = ("RedMax>0", ) + filtersT
valuesT = queryDBGivenParams("a,edMax,rpoe,RedMax", (), c, "models", filters)
print "Got toroids, ", len(valuesT)

#print valuesT[:10]
#print valuesS[:10]

valuesS = zip(*valuesS)
valuesT = zip(*valuesT)

#print valuesT[0]

fig = mpl.figure()
ax = fig.add_subplot(111, projection='3d')
def manyScriptSequencePlot(plotFields, sqliteCursor, filters=(), colorBy=None, maxPlot=None,
                           grid=False, title="", orderBy=None, forceColorbar=False,
                           suppressShow=False, legendList=[], vmax=None, vmin=None,
                           loc=0, minRpoeOnly=False,
                           **mplKwargs):
    """
    Version of sequence plot for plotting huge amount of data
    assumes tablename is models
    if maxPlot plots maximum value of quantity for sequence only
    """
    orderBy = plotFields[0] + ',' + ','.join(tempPrescription.prescriptionParameters) + ",eos,ye,edMax"
    #orderBy = 'rpoe'+','+ ','.join(eosPrescription.prescriptionParameters) + ",eos,ye,edMax"
#print orderBy
    size = 40
    plotLines = False
    #filters = ("ye=0.15",)
    filters += ("arealR < 100",)
    tableName = "models"
    if colorBy is None:
        colorBy = "arealR"
    getFields = plotFields[:]  # x-points 0, y-points 1
    getFields.append(colorBy)  # colorBy 2
    if orderBy is None:
        orderBy = plotFields[0]

    for key in tempPrescription.prescriptionParameters:
        getFields.append(key)   # script parameters 3-8
    getFields.append('RedMax')  # RedMax 9
    getFields.append('ye')      # ye 10
    getFields.append('eos')     # eos 11
    if maxPlot is not None:
        getFields.append(maxPlot)  # maxPlot 12
    if minRpoeOnly:
        getFields.append('runID')    # runIDcol
        runIDcol = len(getFields) - 1
        getFields.append('lineNum')  # lineNumcol
        lineNumcol = len(getFields) - 1
        orderBy = 'runID,lineNum,' + orderBy

    points = queryDBGivenParams(getFields, [], sqliteCursor,
                                tableName, filters, " ORDER BY " + orderBy)
    assert points, "No points returned for query with filters: %s" % str(filters)
    currentSymbol = ""
    previousSymbol = ""
    previousName = ""
    currentName = ""
    currentPoints = []
    currentToroidPoints = []
    currentEos = None
    currentYe = None
    previousEos = None
    previousYe = None
    previousPoint = None
    if vmax is None:
        vmax = max([point[2] for point in points])
    if vmin is None:
        vmin = min([point[2] for point in points])

    #print vmin, vmax
    for i, point in enumerate(points):
        #print point
        currentYe = point[10]
        currentEos = point[11]
        prescriptDict = dict()
        #print currentSymbol, previousSymbol
        for j, scriptParam in enumerate(point[3:3 + 6 ]):
            prescriptDict[tempPrescription.prescriptionParameters[j]] = scriptParam
        #print prescriptDict
        currentSymbol, currentName = symbolFromDBentry(prescriptDict)
        ax = plt.gca()
        handles, labels = ax.get_legend_handles_labels()
        #print currentName, labels
        if currentName in labels:
            currentName = None
            previousName = None
        if currentSymbol == previousSymbol and currentEos == previousEos and currentYe == previousYe:
            currentPoints.append(point)
            if point[9] > 0.0:
                #print "TOROIZZLE"
                currentToroidPoints.append(point)
        elif not previousSymbol == "" and not currentPoints == []:
            if minRpoeOnly:
                currentPoints = reduceToMinRpoeOnly(currentPoints, runIDcol, lineNumcol)
            if maxPlot is not None:
                currentPoints = [getPointWithMaxOfCol(currentPoints, 12)]
                if currentPoints[0][9] == 0.0:
                    #print "antigotcha "
                    currentToroidPoints = []
                else:
                    currentToroidPoints = [getPointWithMaxOfCol(currentToroidPoints, 12)]
            currentPoints = zip(*currentPoints)
            print currentPoints
            plt.scatter(*currentPoints[:2], c=currentPoints[2], marker=previousSymbol, label=previousName,
                        vmax=vmax, vmin=vmin, s=size, **mplKwargs)
            if plotLines:
                plt.plot(*currentPoints[:2], ls=getLinestyle(previousEos, previousYe), c='k', lw=.5)
            currentPoints = []
            #legendList.append(currentName)
            if not currentToroidPoints == []:
                currentToroidPoints = zip(*currentToroidPoints)
                #below line sets circle color to black
                currentToroidPoints[2] = [(0., 0., 0.) for _ in currentToroidPoints[2]]
                plt.scatter(*currentToroidPoints[:2], c=currentToroidPoints[2],
                            marker='o', facecolor='none', s=size * 2,
                            vmax=vmax, vmin=vmin, **mplKwargs)
                currentToroidPoints = []
        previousSymbol = currentSymbol
        previousEos = currentEos
        previousYe = currentYe
        previousName = currentName
        previousPoint = point
        #symbolList.append(symbolFromDBentry(prescriptDict))
    print "Done with loop"
    #Don't forget to plot the last set!
    #print getPointWithMaxOfCol(currentPoints, 12)
    if minRpoeOnly:
        currentPoints = reduceToMinRpoeOnly(currentPoints, runIDcol, lineNumcol)
    if maxPlot is not None:
        currentPoints = [getPointWithMaxOfCol(currentPoints, 12)]
        if currentPoints[0][9] == 0.0:
            currentToroidPoints = []
    currentPoints = zip(*currentPoints)
    plt.scatter(*currentPoints[:2], c=currentPoints[2], marker=previousSymbol, label=previousName,
                vmax=vmax, vmin=vmin, s=size, **mplKwargs)
    if plotLines:
        plt.plot(*currentPoints[:2], ls=getLinestyle(previousEos, previousYe), c='k', lw=.5)
    #legendList.append(currentName)
    #Set colorbar before doing toroidal points because toroidal points have black coloring
    if colorBy and not suppressShow:
        colorLegend = plt.colorbar()
        colorLegend.set_label(latexField(colorBy))
    if forceColorbar:
        colorLegend = plt.colorbar()
        colorLegend.set_label(latexField(colorBy))
    if not currentToroidPoints == []:
        if maxPlot is not None:
            currentToroidPoints = [getPointWithMaxOfCol(currentToroidPoints, 12)]
        currentToroidPoints = zip(*currentToroidPoints)
        #print "dur", currentToroidPoints
        #below line sets circle color to black
        currentToroidPoints[2] = [(0., 0., 0.) for _ in currentToroidPoints[2]]
        plt.scatter(*currentToroidPoints[:2], c=currentToroidPoints[2],
                    marker='o', facecolor='none', s=size * 2,
                    vmax=vmax, vmin=vmin, **mplKwargs)


    plt.title(title)
    plt.grid(grid)

    #axis.set_xlabel(getFields[0])
    #axis.set_ylabel(getFields[1])
    plt.xlabel(latexField(getFields[0]))
    plt.ylabel(latexField(getFields[1]))
    print "Plotting %i entries" % len(points)

    if not suppressShow:
        #ax = plt.gca()
        #handles, labels = ax.get_legend_handles_labels()
        plt.legend(loc=loc)
        plt.show()
        legendList = []