def gradientsAtPoint(self, quantities, pointDict, gradVars=('edMax', 'rpoe'), tol=1e-4): """ Return a list of gradients of input 'quantities' at the point defined by 'pointDict' with respect to gradVars """ pointDict = pointDict.copy() indVars = ('a', 'edMax', 'rpoe') assert all([key in indVars for key in pointDict.keys()]), \ "Not all keys in pointDict are proper indVars!" assert isinstance(quantities, list), 'quantities must be a list!' cursor = self.dbConn.cursor() pointFilters = equalsFiltersFromDict(pointDict, tolerance=tol) query = "WHERE " + " AND ".join(pointFilters) fetchQuantities = ",".join(gradVars) + "," + ",".join(quantities) cursor.execute("SELECT " + fetchQuantities + " FROM models " + query) point = cursor.fetchall() assert point, "Query for %s found no matching entries for dataset %s" \ % (pointDict, self.name) assert len(point) < 2, "Query for %s returned more than one entry for dataset %s \n" \ " Try lowering the search tolerance: %s" \ % (pointDict, self.name, tol) #print point gradients = [] for i, derivVar in enumerate(gradVars): # get the point directly preceding in derivVar prevPointDict = self.getClosestPointToIndVar( pointDict, derivVar, tol) prevPointFilters = equalsFiltersFromDict(prevPointDict, tolerance=tol) query = "WHERE " + " AND ".join(prevPointFilters) cursor.execute("SELECT " + fetchQuantities + " FROM models " + query) prevPoint = cursor.fetchall() diffs = numpy.array(point[0]) - numpy.array(prevPoint[0]) #print diffs / diffs[i] dquantitiesDderivVar = (diffs / diffs[i])[-len(quantities):] #print dquantitiesDderivVar gradients.append(dquantitiesDderivVar) gradients = numpy.array(gradients).transpose() return gradients
def getSecInstabilitySeq(self, a, rpoe, edMin, edMax): """ Do stupid solve between edMin and edMax for zero of grad(J) x grad(M_b) returns ed of zero """ cursor = self.dbConn.cursor() pointDict = {'a': a, 'rpoe': rpoe} pointFilters = equalsFiltersFromDict(pointDict) query = "WHERE " + " AND ".join(pointFilters) query += " AND edMax>%s AND edMax<%s " % (edMin, edMax) cursor.execute("SELECT edMax FROM models " + query + " ORDER BY edMax") eds = cursor.fetchall() answer = None edList = [] previousTpFunc = 1.0 for ed in eds[1:]: ed = ed[0] edList.append(ed) tempDict = pointDict.copy() tempDict.update({'edMax': ed}) tpFunc = numpy.linalg.det(self.gradientsAtPoint(['J', 'baryMass'], tempDict)) #print ed, tpFunc if tpFunc * previousTpFunc < 0: answer = ed previousTpFunc = tpFunc print answer return answer
def getClosestPointToIndVar(self, pointDict, indVar, tolerance, lessThanFlag=True): """ Finds point in sequence directly less (greater if lessThanFlag is false) than the pointDict in indVar. Returns pointDict for prevPoint """ maxOrMin = "MAX" op = '<' if not lessThanFlag: maxOrMin = "MIN" op = '>' pointDict = pointDict.copy() value = pointDict[indVar] del pointDict[indVar] cursor = self.dbConn.cursor() pointFilters = equalsFiltersFromDict(pointDict, tolerance) query = "WHERE " + " AND ".join(pointFilters) + " AND " + indVar + op + str(value) cursor.execute("SELECT " + maxOrMin + "(" + indVar + ") FROM models " + query) answer = cursor.fetchall() assert answer, "No point %s %s for %s=%s " % (op, indVar, indVar, value) pointDict.update({indVar: answer[0][0]}) return pointDict.copy()
def getSecInstabilitySeq(self, a, rpoe, edMin, edMax): """ Do stupid solve between edMin and edMax for zero of grad(J) x grad(M_b) returns ed of zero """ cursor = self.dbConn.cursor() pointDict = {'a': a, 'rpoe': rpoe} pointFilters = equalsFiltersFromDict(pointDict) query = "WHERE " + " AND ".join(pointFilters) query += " AND edMax>%s AND edMax<%s " % (edMin, edMax) cursor.execute("SELECT edMax FROM models " + query + " ORDER BY edMax") eds = cursor.fetchall() answer = None edList = [] previousTpFunc = 1.0 for ed in eds[1:]: ed = ed[0] edList.append(ed) tempDict = pointDict.copy() tempDict.update({'edMax': ed}) tpFunc = numpy.linalg.det( self.gradientsAtPoint(['J', 'baryMass'], tempDict)) #print ed, tpFunc if tpFunc * previousTpFunc < 0: answer = ed previousTpFunc = tpFunc print answer return answer
def getClosestPointToIndVar(self, pointDict, indVar, tolerance, lessThanFlag=True): """ Finds point in sequence directly less (greater if lessThanFlag is false) than the pointDict in indVar. Returns pointDict for prevPoint """ maxOrMin = "MAX" op = '<' if not lessThanFlag: maxOrMin = "MIN" op = '>' pointDict = pointDict.copy() value = pointDict[indVar] del pointDict[indVar] cursor = self.dbConn.cursor() pointFilters = equalsFiltersFromDict(pointDict, tolerance) query = "WHERE " + " AND ".join( pointFilters) + " AND " + indVar + op + str(value) cursor.execute("SELECT " + maxOrMin + "(" + indVar + ") FROM models " + query) answer = cursor.fetchall() assert answer, "No point %s %s for %s=%s " % (op, indVar, indVar, value) pointDict.update({indVar: answer[0][0]}) return pointDict.copy()
def gradientsAtPoint(self, quantities, pointDict, gradVars=('edMax', 'rpoe'), tol=1e-4): """ Return a list of gradients of input 'quantities' at the point defined by 'pointDict' with respect to gradVars """ pointDict = pointDict.copy() indVars = ('a', 'edMax', 'rpoe') assert all([key in indVars for key in pointDict.keys()]), \ "Not all keys in pointDict are proper indVars!" assert isinstance(quantities, list), 'quantities must be a list!' cursor = self.dbConn.cursor() pointFilters = equalsFiltersFromDict(pointDict, tolerance=tol) query = "WHERE " + " AND ".join(pointFilters) fetchQuantities = ",".join(gradVars) + "," + ",".join(quantities) cursor.execute("SELECT " + fetchQuantities + " FROM models " + query) point = cursor.fetchall() assert point, "Query for %s found no matching entries for dataset %s" \ % (pointDict, self.name) assert len(point) < 2, "Query for %s returned more than one entry for dataset %s \n" \ " Try lowering the search tolerance: %s" \ % (pointDict, self.name, tol) #print point gradients = [] for i, derivVar in enumerate(gradVars): # get the point directly preceding in derivVar prevPointDict = self.getClosestPointToIndVar(pointDict, derivVar, tol) prevPointFilters = equalsFiltersFromDict(prevPointDict, tolerance=tol) query = "WHERE " + " AND ".join(prevPointFilters) cursor.execute("SELECT " + fetchQuantities + " FROM models " + query) prevPoint = cursor.fetchall() diffs = numpy.array(point[0]) - numpy.array(prevPoint[0]) #print diffs / diffs[i] dquantitiesDderivVar = (diffs / diffs[i])[-len(quantities):] #print dquantitiesDderivVar gradients.append(dquantitiesDderivVar) gradients = numpy.array(gradients).transpose() return gradients
# del coldTovSet for script in scriptsList: xFunc = lambda x: theEos.rhobFromEnergyDensityWithTofRho(x, ye, tempFuncsDict[script]) xFunc = lambda x: x thisSet = cstDataset(script, eosName, ye, sourceDb) thisSeq = cstSequence(thisSet, theMaxRotSlice, filters) #cursor = thisSeq.dbConn.cursor() #cursor.execute("SELECT rpoe FROM models") #print cursor.fetchall() eds = [] plotVars = [] for rpoe in numpy.arange(0.64, 1.0, 0.04): ed = thisSet.getSecInstabilitySeq(a, rpoe, 1e15, 3e15) setCursor = thisSet.dbConn.cursor() ptDict = {'a': a, 'rpoe': rpoe, 'edMax': ed} pointFilters = equalsFiltersFromDict(ptDict) query = "WHERE " + " AND ".join(pointFilters) setCursor.execute("SELECT " + ','.join(yVars) + " FROM models " + query) answer = yFunc(*setCursor.fetchall()[0]) eds.append(xFunc(ed)) plotVars.append(answer) plt.scatter(eds, plotVars, marker=symbols[script], c=colors[script], s=50) theEos.resetCachedBetaEqYeVsRhobs(tempFuncsDict[script], 13.5, 16.0) thisPlot = thisSeq.getSeqPlot([xVar], yVars, filters, \ xcolFunc=xFunc, ycolFunc=yFunc) plt.plot(*thisPlot, c=colors[script], label=script) plotList.append([thisPlot, colors[script], '-']) del thisSet
for script in scriptsList: xFunc = lambda x: theEos.rhobFromEnergyDensityWithTofRho( x, ye, tempFuncsDict[script]) xFunc = lambda x: x thisSet = cstDataset(script, eosName, ye, sourceDb) thisSeq = cstSequence(thisSet, theMaxRotSlice, filters) #cursor = thisSeq.dbConn.cursor() #cursor.execute("SELECT rpoe FROM models") #print cursor.fetchall() eds = [] plotVars = [] for rpoe in numpy.arange(0.64, 1.0, 0.04): ed = thisSet.getSecInstabilitySeq(a, rpoe, 1e15, 3e15) setCursor = thisSet.dbConn.cursor() ptDict = {'a': a, 'rpoe': rpoe, 'edMax': ed} pointFilters = equalsFiltersFromDict(ptDict) query = "WHERE " + " AND ".join(pointFilters) setCursor.execute("SELECT " + ','.join(yVars) + " FROM models " + query) answer = yFunc(*setCursor.fetchall()[0]) eds.append(xFunc(ed)) plotVars.append(answer) plt.scatter(eds, plotVars, marker=symbols[script], c=colors[script], s=50) theEos.resetCachedBetaEqYeVsRhobs(tempFuncsDict[script], 13.5, 16.0) thisPlot = thisSeq.getSeqPlot([xVar], yVars, filters, \ xcolFunc=xFunc, ycolFunc=yFunc) plt.plot(*thisPlot, c=colors[script], label=script) plotList.append([thisPlot, colors[script], '-']) del thisSet
def __init__(self, inputCstDataset, fixedIndVarsDict, filters=()): """ Takes fixedIndVarsDict, and sets the parameter to the indVar not in the dict. filters only used for special case of selecting min rpoe """ assert isinstance(inputCstDataset, cstDataset) assert all([key in self.indVars for key in fixedIndVarsDict.keys()]), \ "Not all keys in fixedIndVarsDict are proper indVars" assert len(fixedIndVarsDict) == 2, "Must fix 2 indVars!" self.orderByVar = [indVar for indVar in self.indVars if indVar not in fixedIndVarsDict][0] copyOfInputDict = fixedIndVarsDict.copy() #Handle special case of selecting rpoe min restrictToRpoeMin = False if 'rpoe' in copyOfInputDict: if copyOfInputDict['rpoe'] == 'min': #delete it so we filter out nothing; do the rpoe min filtering later del copyOfInputDict['rpoe'] restrictToRpoeMin = True sliceFilters = equalsFiltersFromDict(copyOfInputDict, tolerance=3e-3) query = "WHERE " + " AND ".join(sliceFilters) + " ORDER BY " + self.orderByVar self.dbConn = sqlite3.connect(':memory:') cursor = self.dbConn.cursor() cursor.execute("CREATE TABLE models " + parseFiles.columnsString) cursor.execute("ATTACH '" + inputCstDataset.dbName + "' AS toMerge") cursor.execute("INSERT INTO models SELECT DISTINCT * FROM toMerge.models " + query) self.dbConn.commit() if restrictToRpoeMin: # min rpoe occurs at the max run number for a given runID # method: for each runID, find the max lineNum (satisfying the filters!) # then, for that runID, delete entries that don't have that lineNum cursor.execute("SELECT DISTINCT runID FROM models") runIDs = cursor.fetchall() #print len(runIDs), runIDs filtersString = "" if filters: filtersString = " AND " + " AND ".join(filters) #print filtersString for runID in runIDs: runID = runID[0] cursor.execute("SELECT MAX(lineNum) FROM models WHERE runID='" + runID + "'" + filtersString) maxLineNum = cursor.fetchone()[0] #print runID, maxLineNum #if filters have filtered out all values, maxLineNum is None, # and the following query will fail, so lets just move on if maxLineNum is None: continue # runType 3 means mass-shed sequence, so all rpoes are min! # so don't delete anything cursor.execute("DELETE FROM models WHERE runID='" + runID + "' AND lineNum!=" + str(maxLineNum) + " AND runType!=3") #for i in cursor.execute("SELECT * FROM models"): # print i cursor.execute("SELECT count(*) FROM models") print "Sliced %s entries into cstSequence '%s' from cstDataset '%s'"\ % (cursor.fetchone()[0], str(fixedIndVarsDict), inputCstDataset.name)
def __init__(self, inputCstDataset, fixedIndVarsDict, filters=()): """ Takes fixedIndVarsDict, and sets the parameter to the indVar not in the dict. filters only used for special case of selecting min rpoe """ assert isinstance(inputCstDataset, cstDataset) assert all([key in self.indVars for key in fixedIndVarsDict.keys()]), \ "Not all keys in fixedIndVarsDict are proper indVars" assert len(fixedIndVarsDict) == 2, "Must fix 2 indVars!" self.orderByVar = [ indVar for indVar in self.indVars if indVar not in fixedIndVarsDict ][0] copyOfInputDict = fixedIndVarsDict.copy() #Handle special case of selecting rpoe min restrictToRpoeMin = False if 'rpoe' in copyOfInputDict: if copyOfInputDict['rpoe'] == 'min': #delete it so we filter out nothing; do the rpoe min filtering later del copyOfInputDict['rpoe'] restrictToRpoeMin = True sliceFilters = equalsFiltersFromDict(copyOfInputDict, tolerance=3e-3) query = "WHERE " + " AND ".join( sliceFilters) + " ORDER BY " + self.orderByVar self.dbConn = sqlite3.connect(':memory:') cursor = self.dbConn.cursor() cursor.execute("CREATE TABLE models " + parseFiles.columnsString) cursor.execute("ATTACH '" + inputCstDataset.dbName + "' AS toMerge") cursor.execute( "INSERT INTO models SELECT DISTINCT * FROM toMerge.models " + query) self.dbConn.commit() if restrictToRpoeMin: # min rpoe occurs at the max run number for a given runID # method: for each runID, find the max lineNum (satisfying the filters!) # then, for that runID, delete entries that don't have that lineNum cursor.execute("SELECT DISTINCT runID FROM models") runIDs = cursor.fetchall() #print len(runIDs), runIDs filtersString = "" if filters: filtersString = " AND " + " AND ".join(filters) #print filtersString for runID in runIDs: runID = runID[0] cursor.execute( "SELECT MAX(lineNum) FROM models WHERE runID='" + runID + "'" + filtersString) maxLineNum = cursor.fetchone()[0] #print runID, maxLineNum #if filters have filtered out all values, maxLineNum is None, # and the following query will fail, so lets just move on if maxLineNum is None: continue # runType 3 means mass-shed sequence, so all rpoes are min! # so don't delete anything cursor.execute("DELETE FROM models WHERE runID='" + runID + "' AND lineNum!=" + str(maxLineNum) + " AND runType!=3") #for i in cursor.execute("SELECT * FROM models"): # print i cursor.execute("SELECT count(*) FROM models") print "Sliced %s entries into cstSequence '%s' from cstDataset '%s'"\ % (cursor.fetchone()[0], str(fixedIndVarsDict), inputCstDataset.name)