Пример #1
0
    def read_weights(self, filename):

        fname = "%s_ww.csv" % filename
        f = df.DataFile(fname)
        self.ww = f.read()
        self.nInput = self.ww.shape[1]
        self.nHidden = self.ww.shape[0]

        fname = "%s_wv.csv" % filename
        f = df.DataFile(fname)
        self.wv = f.read()
        self.nOutput = self.wv.shape[0]

        fname = "%s_bh.csv" % filename
        f = df.DataFile(fname)
        self.bh = f.read()

        fname = "%s_bo.csv" % filename
        f = df.DataFile(fname)
        self.bo = f.read()

        fname = "%s_parms.csv" % filename
        f = df.DataFile(fname)
        parms = f.read()

        #eta is row 0
        self.eta = parms[0][1]
        self.tHidden = getTransfer(parms[1][0], parms[1][1])
        self.tOutput = getTransfer(parms[2][0], parms[2][1])

        #initialize outputs
        self.dOut = np.zeros(self.nOutput)
        self.dHidden = np.zeros(self.nHidden)
Пример #2
0
    def write_weights(self, filename):

        f = df.DataFile("%s_ww.csv" % filename)
        f.add(self.ww)
        f.write()

        f = df.DataFile("%s_wv.csv" % filename)
        f.add(self.wv)
        f.write()

        f = df.DataFile("%s_bh.csv" % filename)
        f.add(self.bh)
        f.write()

        f = df.DataFile("%s_bo.csv" % filename)
        f.add(self.bo)
        f.write()

        f = df.DataFile("%s_parms.csv" % filename)
        row = np.array([0, self.eta])
        f.add(row)
        row = np.array([self.tHidden.type(), self.tHidden.parm()])
        f.add(row)
        row = np.array([self.tOutput.type(), self.tOutput.parm()])
        f.add(row)
        f.write()
Пример #3
0
def printNorms(fits):
  """Basically a debugging print out
  """
  
  for fitSettings in fits:
    print "fitting ",fitSettings['input']
    
    #get input model to fit
    inputData=datafile.DataFile()
    inputData.readFile(fitSettings['input'])
    
    data=np.empty(inputData.fColumnValues.shape)
    for fitTo in fitSettings['fit-to']:
      
      print "to ",fitTo
      fileName="norms_DM_"+fitTo
      print "writting norms to \""+fileName+"\" ..."
      f=open(fileName,'w')
      #read in data to fit to
      fitData=datafile.DataFile()
      fitData.readFile(fitTo)
      
      #setup the fit with the data
      fit=Fit(inputData.fColumnValues,fitData.fColumnValues)
      
      numPoints=1000
      minDM=14.5
      maxDM=16.5
      minPS=-0.2
      maxPS=0.2
      deltaDM=(maxDM-minDM)/numPoints
      deltaPS=(maxPS-minPS)/numPoints
      PS=0.0
      norm2=fit.computeNormOfDifference(minDM-2*deltaDM,PS)
      norm1=fit.computeNormOfDifference(minDM-deltaDM,PS)
      f.write("DM norm0 norm1 norm2 DIVL DIVH DIV2")
      for i in range(0,numPoints):
        
        #Compute first and second derivatives in DM direction
        
        #get three points to evaluate first and second derivatives with
        DM=minDM+i*deltaDM
        norm0=fit.computeNormOfDifference(DM,PS)#at centre point
        DIVL=(norm1-norm2)/deltaDM
        DIVH=(norm0-norm1)/deltaDM
        DIV2=(DIVH-DIVL)/deltaDM
        f.write(str(DM)+" "+str(norm0)+" "+str(norm1)+" "+str(norm2)+" "+str(DIVL)+" "+str(DIVH)+" "+str(DIV2)+"\n")
        norm2=norm1
        norm1=norm0
      f.close()
Пример #4
0
def getPeriodRanges(baseFileName,start,end,options):
  #check that the averagePKE.txt file is there, and make sure it has all entries
  averagePKEFile=os.path.dirname(baseFileName)+"/averagePKE.txt"
  average_PKE.averagePKE(start,sys.maxint,baseFileName,options)
  
  #get periods in range indicated
  averagePKEFileData=datafile.DataFile()
  print __name__+": reading file \"",averagePKEFile,"\" ..."
  averagePKEFileData.readFile(averagePKEFile)
  periodCount=0
  periodRange=[]
  nSet=0
  time=[]
  for i in range(len(averagePKEFileData.fColumnValues)-1):
    if averagePKEFileData.fColumnValues[i][0]>=start and averagePKEFileData.fColumnValues[i][0]<end:
      if averagePKEFileData.fColumnValues[i][3]!=None and nSet==0:
        periodRange.append([averagePKEFileData.fColumnValues[i][0],None])
        nSet=1
      elif averagePKEFileData.fColumnValues[i][3]!=None and nSet==1:
        nSet=2
      elif averagePKEFileData.fColumnValues[i][3]!=None and nSet==2:
        if periodCount>0:
          periodRange.append([periodRange[periodCount-1][1],None])
        time.append(averagePKEFileData.fColumnValues[i][1])
        periodRange[periodCount][1]=averagePKEFileData.fColumnValues[i][0]
        nSet=1
        periodCount+=1
  return [periodRange,time]
Пример #5
0
  def __init__(self,element,options):
    """Initilizes the dataSet

    by setting baseFileName, start, end, and intilizing plots from an xml 
    element
    
    """
    
    #set some initial values
    self.axes=[]
    self.files={}
    
    #check for unrecognized attributes
    recogAttribs=[]
    notRecogAttribs=checkForRecogAttrib(element,recogAttribs)
    if len(notRecogAttribs)>0:
      raise Exception("attributes in dataset "+str(notRecogAttribs)
      +" are not in list of recognized dataset attributes "+str(recogAttribs))
    
    #load files
    fileElements=element.findall("file")
    for fileElement in fileElements:
      fileData=datafile.DataFile()
      fileData.readFile(fileElement.text)
      self.files[fileElement.get("name")]=fileData
      
    #add axes to dataset
    axisElements=element.findall("axis")
    for axisElement in axisElements:
      axis=Axis(axisElement,options)
      self.axes.append(axis)
Пример #6
0
def main():
    #parse command line options
    (options, args) = parseOptions()

    #get base file name
    [start, end, baseFileName] = disect_filename.disectFileName(args[0])

    #make sure that all the combined binary files have profiles made
    failedFiles = make_profiles.make_fileSet(args[0], options)

    #get and sort files
    extension = "_pro" + ".txt"
    filesExistProfiles = glob.glob(baseFileName + "*" + extension)
    files = []
    for file in filesExistProfiles:
        intOfFile = int(file[len(baseFileName):len(file) - len(extension)])
        if intOfFile >= start and intOfFile < end:
            files.append(file)

    files.sort()

    if len(files) < 1:
        print __name__+":"+main.__name__+": no files found matching \""+baseFileName\
          +"\" in the range "+str(start)+"-"+str(end)
        return False

    #make list of radial grid velocity, and time
    radialGridVelocity = []
    times = []
    nCount = 0
    for file in files:
        print "reading profile ", file, " ..."
        fileData = datafile.DataFile()
        fileData.readFile(file)
        nNumZones = len(fileData.fColumnValues)
        radialGridVelocity.append(
            fileData.fColumnValues[nNumZones - options.zone -
                                   1][options.column - 1])
        fileHeader = fileData.sHeader.split()
        times.append(float(fileHeader[1]))
        nCount = nCount + 1

    #print data to file
    print "writing to file ..."
    outputFileName = options.outputFile + ".txt"
    f = open(outputFileName, 'w')
    for i in range(0, len(radialGridVelocity) - 1):
        line = str(times[i]) + " " + str(radialGridVelocity[i]) + "\n"
        f.write(line)

    f.close()
    success = os.system(paths.SPHERLSanalPath + ' -tl ' + outputFileName)
    if success != 0:
        print "error computing fourier transform of \"" + outputFileName + "\""

    if __name__ == "__main__":  #keeps from redundently reporting errors
        #report failed files
        for file in failedFiles:
            print file
Пример #7
0
 def load(self,options):
   '''Loads the dataSet, this means that it sets, time, phases, and plots data'''
   
   #make sure that all the combined binary files within range of dataset have profiles made
   fileName=self.baseFileName+"["+str(self.start)+"-"+str(self.end)+"]"
   try:
     failedFiles=make_profiles.make_fileSet(fileName,options)
   except NoFilesFound as e:
     
     #it is OK if their aren't any combined binary files to make profiles from
     #so long as there are actually some profiles
     print e.message
     failedFiles=[]
   
   if len(failedFiles)>0:
     for failedFile in failedFiles:
       print failedFile
   
   #get and sort profiles within range of dataset
   extension="_pro"+".txt"
   filesExistProfiles=glob.glob(self.baseFileName+"*"+extension)
   filesExistProfiles.sort()
   files=[]
   print "found "+str(len(filesExistProfiles))+" profile files"
   for i in range(0,len(filesExistProfiles),options.frequency):
     file=filesExistProfiles[i]
     intOfFile=int(file[len(self.baseFileName):len(file)-len(extension)])
     if intOfFile>=self.start and intOfFile<self.end:
       files.append(file)
   if len(files)==0:
     raise Exception("no files found in range ["+str(self.start)+"-"
       +str(self.end)+"]")
   
   self.nNumFiles=len(files)
   
   #create time, phase and curve y's for dataset
   nFileCount=0
   for file in files:
     
     #get file index
     temp=file[file.find("_t")+2:]
     self.fileIndices.append(temp[:temp.find("_pro")])
     
     #read in profile
     print "reading in profile ",file,str(nFileCount+1)+"/"+str(self.nNumFiles)+" ..."
     fileData=datafile.DataFile()
     fileData.readFile(file)
     
     #load x-axis data and y-data
     for axis in self.axes:
       axis.load(fileData,options,self,nFileCount)
     nFileCount=nFileCount+1
Пример #8
0
def plot_heatmap(dataset, filename):

    ticks = Letters(dataset)
    rticks = range(len(ticks))

    # Plot heatmap (this works)
    fheatmap = datafile.DataFile(filename)
    h = fheatmap.read()
    nHidden = h.shape[1]

    plt.clf()
    plt.title('Hidden Layer Activations')
    plt.ylabel('Letter Class')
    plt.xlabel('Hidden Node')
    plt.yticks(rticks, ticks)
    plt.xticks(range(nHidden), range(nHidden))
    plt.margins(0.2)
    plt.imshow(h, cmap="bwr")
    plt.show()
    plt.savefig('heatmap_%d.png' % nHidden)
Пример #9
0
def ComputeAll(mm,
               dataset,
               filterMasks,
               outputCol,
               debug=False,
               bHidden=False):

    #get a count of the number of letters being tested
    #a letter may have multiple variants
    dd = pd.DataFrame(dataset)
    letters = dd[3]
    freq = pd.value_counts(letters)
    llist = list(freq.index)

    heatmap = np.zeros((mm.nOutput, mm.nHidden))

    #initialize dictionary
    #using a dictionary because test set may not have all letters
    SSE_Dict = {}
    for ll in llist:
        SSE_Dict[ll] = 0

    #loop through each training set
    for iTest in range(len(dataset)):

        #test sample
        trainingDataList = dataset[iTest]

        #input values
        trainingDataInputList = trainingDataList[1]
        inputDataArray = np.array(trainingDataInputList)

        letterNum = trainingDataList[2]
        letterChar = trainingDataList[3]
        letterClass = trainingDataList[4]
        outputArrayLength = mm.nOutput

        if debug == True:
            print ' '
            print '  Data Set Number', iTest, ' for letter ', letterChar, ' with letter number ', letterNum + 1

        desiredClass = trainingDataList[
            outputCol]  # identify the desired class
        desiredOutputArray = np.zeros(
            outputArrayLength)  # iniitalize the output array with 0's
        desiredOutputArray[
            desiredClass] = 1  # set the desired output for that class to 1

        #create filtered input data
        filteredInputDataArray = filterMasks.FilterLetter(inputDataArray)

        #use mlp class
        outputArray = mm.forward(inputDataArray, filteredInputDataArray)

        #calculate SSE
        errorArray = (desiredOutputArray - outputArray)**2
        newSSE = sum(errorArray)

        if debug == True:
            print ' '
            print ' The hidden node activations are:'
            print mm.Hidden()

            print ' '
            print ' The output node activations are:'
            print outputArray

            print ' '
            print ' The desired output array values are: '
            print desiredOutputArray

            print ' '
            print ' The error values are:'
            print(desiredOutputArray - outputArray)

            print 'New SSE = %.6f' % newSSE

        #save error in dictionary
        e = SSE_Dict[letterChar]
        e = e + newSSE
        SSE_Dict[letterChar] = e

        #build heatmap
        heatmap[desiredClass] = mm.Hidden()

    mseTotal = 0
    nTotal = 0

    #calculate average error of each SSE value
    for char in SSE_Dict:
        mseTotal = mseTotal + SSE_Dict[char]
        nTotal = nTotal + freq[char]

        e = SSE_Dict[char] / freq[char]
        SSE_Dict[char] = e

    mseTotal = mseTotal / nTotal

    if (bHidden == True):
        fheatmap = datafile.DataFile("heatmap_%d.csv" % mm.nHidden)
        fheatmap.add(heatmap)
        fheatmap.write()

    if debug == True:
        print SSE_Dict
        print 'Total MSE = ', mseTotal

    #return the errors
    return (SSE_Dict)
Пример #10
0
def read_datafile(file_path):
    df = datafile.DataFile(file_path)
    return df
Пример #11
0
def averagePKE(start,end,baseFileName,options):
  """Computes the average PKE from radial profiles."""
  
  nColumnUMax=14
  nColumnUMin=17
  nColumnU0=20
  nColumnTAve=49
  nColumnTMax=50
  nColumnTMin=53
  
  #get and sort files
  extension="_pro"+".txt"
  filesExistProfiles=glob.glob(baseFileName+"*"+extension)
  files=[]
  for file in filesExistProfiles:
    intOfFile=int(file[len(baseFileName):len(file)-len(extension)])
    if intOfFile>=start and intOfFile<end:
      files.append(file)
  
  files.sort()
  
  if len(files)<1:
    print __name__+":"+averagePKE.__name__+": no files found matching \""+baseFileName+"\" in range given"
    return False
    
  if len(files)<2:
    print __name__+":"+averagePKE.__name__+": need at least 2 files!"
    return False
  
  if options.bLconInsteadofKE:
    print __name__+":"+averagePKE.__name__+":finding max F_con in profiles ..."
  elif options.bumu0InsteadofKE:
    print __name__+":"+averagePKE.__name__+":finding max u-u_0 in profiles ..."
  elif options.bTmTaveInsteadofKE:
    print __name__+":"+averagePKE.__name__+":finding max T-<T> in profiles ..."
  else:
    print __name__+":"+averagePKE.__name__+":summing up kinetic energies in profiles ..."
  times=[]
  indexes=[]
  KE=[]
  #read in KE from average file, if file already exists
  lastIndex=-1
  directory=os.path.dirname(baseFileName)
  if directory=="":
    if options.bLconInsteadofKE:
      averagePKEFile="maxFcon.txt"
    elif options.bumu0InsteadofKE:
      averagePKEFile="maxumu0.txt"
    elif options.bTmTaveInsteadofKE:
      averagePKEFile="maxTmTave.txt"
    else:
      averagePKEFile="averagePKE.txt"
  else:
    if options.bLconInsteadofKE:
      averagePKEFile=directory+"/maxFcon.txt"
    elif options.bumu0InsteadofKE:
      averagePKEFile=directory+"/maxumu0.txt"
    elif options.bTmTaveInsteadofKE:
      averagePKEFile=directory+"/maxTmTave.txt"
    else:
      averagePKEFile=directory+"/averagePKE.txt"
    
  if os.path.exists(averagePKEFile):
    if not options.resum:
      if options.bLconInsteadofKE:
        print "  \"",averagePKEFile,"\" already exists, not recalculating F_con_max for entries"\
          +" already in file"
      elif options.bumu0InsteadofKE:
        print "  \"",averagePKEFile,"\" already exists, not recalculating (u-u_0)_max for entries"\
          +" already in file"
      elif options.bTmTaveInsteadofKE:
        print "  \"",averagePKEFile,"\" already exists, not recalculating (T-<T>)_max for entries"\
          +" already in file"
      else:
        print "  \"",averagePKEFile,"\" already exists, not re-summing KE for entries already in"\
          +" file"
      f=open(averagePKEFile,'r')
      f.readline() #skip header
      for line in f:
        lineParts=line.split()
        indexes.append(int(lineParts[0]))
        times.append(float(lineParts[1]))
        KE.append(float(lineParts[2]))
      lastIndex=int(lineParts[0])
      f.close()
    else:
      if options.bLconInsteadofKE:
        print __name__+":"+averagePKE.__name__+":  \"",averagePKEFile,"\" already exists, but "\
          +"re-sum set so reclaculating F_con_max entries in profiles"
      else:
        print __name__+":"+averagePKE.__name__+":  \"",averagePKEFile,"\" already exists, but "\
          +"re-sum set so resumming KE entries in profiles"
      
  if options.bLconInsteadofKE:
    nColumn=62#use F_con column
  else:
    nColumn=64#use kinetic energy column
  for file in files:
    fileIndex=int(file[len(file)-16:len(file)-8])
    if fileIndex>lastIndex:#make sure that file hasn't already been done
      if options.bLconInsteadofKE:
        print __name__+":"+averagePKE.__name__+": finding max of F_con of file \""+file+"\" ..."
      else:
        print __name__+":"+averagePKE.__name__+": summing KE of file \""+file+"\" ..."
      fileData=datafile.DataFile()
      fileData.readFile(file)
      fileHeader=fileData.sHeader.split()
      times.append(float(fileHeader[1]))
      
      #get file index
      indexes.append(int(fileIndex))
      
      #sum up all kinetic energies
      dKESum=0.0
      if options.bLconInsteadofKE:
        CLM=0.0
        for i in range(len(fileData.fColumnValues)-1):
          if fileData.fColumnValues[i][nColumn]!=None:
            if fileData.fColumnValues[i][nColumn]>CLM:
              CLM=fileData.fColumnValues[i][nColumn]
        KE.append(CLM)
      elif options.bumu0InsteadofKE:
        CLM=0.0
        for i in range(len(fileData.fColumnValues)-1):
          if fileData.fColumnValues[i][nColumn]!=None:
            if fileData.fColumnValues[i][nColumn]>CLM:
              CLM=fileData.fColumnValues[i][nColumn]
        KE.append(CLM)
      else:
        for i in range(len(fileData.fColumnValues)-1):
          dKESum=dKESum+fileData.fColumnValues[i][nColumn]
        KE.append(dKESum)
  
  bIncreasing=True
  peakKESum=0.0
  numInKESum=0
  numPeaksInAve=6
  halfPeriodAve=0.0
  totalNumPeaks=0
  timeOfLastPeak=0.0
  fracHalfPeriodAcceptPeaks=0.35
  weightOfCurrentPeriod=0.25
  valueOfLastPeak=0
  f=open(averagePKEFile,'w')
  
  #write header to file
  if options.bLconInsteadofKE:
    f.write("index(1) t[s](2) CLM(3) PCLM(4) 3_Period_Ave_PCLM(5)\n")
    print __name__+":"+averagePKE.__name__+":computing average Peak CLM ..."
  else:
    f.write("index(1) t[s](2) KE(3) PKE(4) 3_Period_Ave_PKE(5)\n")
    print __name__+":"+averagePKE.__name__+":computing average Peak KE ..."
  
  #find first two peaks
  bContinue=True
  bContinue1=True
  i=0
  if options.period==None:
    iOfLastPrint=-1
    while bContinue1 and bContinue:
      if KE[i+1]<KE[i] and bIncreasing:#stopped increasing
        bIncreasing=False
        if totalNumPeaks==0:#assume first peak is a peak to include in average
          
          #keep important info for peak finding
          timeOfLastPeak=times[i]
          totalNumPeaks=totalNumPeaks+1
          
          #add peak to summing
          peakKESum=peakKESum+KE[i]
          numInKESum=numInKESum+1
          
          #write first peak
          iOfLastPrint=i
          f.write(str(indexes[i])+" "+str(times[i])+" "+str(KE[i])+" "+str(KE[i])+" -\n")
        elif totalNumPeaks==1:#assume second peak is a peak to include in average
          
          #keep important info for peak finding
          halfPeriodAve=(times[i]-timeOfLastPeak)
          totalNumPeaks=totalNumPeaks+1
          timeOfLastPeak=times[i]
          
          #add peak to summing
          peakKESum=peakKESum+KE[i]
          numInKESum=numInKESum+1
          
          #write second peak
          iOfLastPrint=i
          f.write(str(indexes[i])+" "+str(times[i])+" "+str(KE[i])+" "+str(KE[i])+" -\n")
          bContinue1=False
      elif KE[i+1]>=KE[i]:
        bIncreasing=True
      if iOfLastPrint!=i:#if didn't print out a peak
        #write KE[i]
        iOfLastPrint=i
        f.write(str(indexes[i])+" "+str(times[i])+" "+str(KE[i])+" - -\n")
      
      i=i+1
      if i>=len(times)-1:#check for EOF
        bContinue=False
  else:
    iOfLastPrint=0
    if options.bLconInsteadofKE:
      halfPeriodAve=float(options.period)
    else:
      halfPeriodAve=float(options.period)*0.5
  
  
  #repeat search for peak until EOF reached
  while bContinue:
    
    #search expected range for maximum peak
    iOfMaxKEPeak=None
    KEMaxPeak=0.0
    lowerTime=timeOfLastPeak+halfPeriodAve-fracHalfPeriodAcceptPeaks*halfPeriodAve
    upperTime=timeOfLastPeak+halfPeriodAve+(fracHalfPeriodAcceptPeaks)*halfPeriodAve
    bIncreasing=True
    bContinue1=True
    while bContinue1 and bContinue:
      if KE[i+1]<KE[i] and bIncreasing:#stopped increasing
        #if in range of next expected peak
        if times[i]>=lowerTime and times[i]<=upperTime:
          if KE[i]>KEMaxPeak:
            KEMaxPeak=KE[i]
            iOfMaxKEPeak=i
        
        bIncreasing=False
      elif KE[i+1]>=KE[i]:
        bIncreasing=True
      
      if times[i]>upperTime:
        bContinue1=False
      
      #increament
      i=i+1
      if i>=len(times)-1:#if too large, stop everything
        bContinue=False
      
    #if no peaks found in expected range, use next peak found
    if iOfMaxKEPeak==None:
      bIncreasing=True
      bContinue1=True
      while bContinue1 and bContinue:
        
        if KE[i+1]<KE[i] and bIncreasing:#stopped increasing
          KEMaxPeak=KE[i]
          iOfMaxKEPeak=i
          bIncreasing=False
          bContinue1=False
        elif KE[i+1]>=KE[i]:
          bIncreasing=True
        
        i=i+1
        if i+1>=len(times)-1:#check for EOF
          bContinue=False
    
    #write out KE from but not including iOfLastPrint upto but not including iOfMaxKEPeak
    printStop=iOfMaxKEPeak
    if iOfMaxKEPeak==None and not bContinue:
      printStop=len(times)
    for j in range(iOfLastPrint+1,printStop):
      
      #write out KE[j]
      iOfLastPrint=j
      f.write(str(indexes[j])+" "+str(times[j])+" "+str(KE[j])+" - -\n")
    if iOfMaxKEPeak!=None:
      
      #we are garanteed to have a peak, either in the expected range or the next peak after it
      halfPeriod=(times[iOfMaxKEPeak]-timeOfLastPeak)
      halfPeriodAve=weightOfCurrentPeriod*halfPeriod+(1.0-weightOfCurrentPeriod)*halfPeriodAve  #compute a running average
      totalNumPeaks=totalNumPeaks+1
      timeOfLastPeak=times[iOfMaxKEPeak]
      if numInKESum==0:
        peakKESum=0.0
      if numInKESum==numPeaksInAve-1:#finish average
        numInKESum=numInKESum+1
        peakKESum=peakKESum+KEMaxPeak
        
        #write out average
        f.write(str(indexes[iOfMaxKEPeak])+" "+str(times[iOfMaxKEPeak])+" "+str(KE[iOfMaxKEPeak])
          +" "+str(KE[iOfMaxKEPeak])+" "+str(peakKESum/numInKESum)+"\n")
        iOfLastPrint=iOfLastPrint+1
        numInKESum=0
        peakKESum=0.0
      else:#keep averaging
        numInKESum=numInKESum+1
        peakKESum=peakKESum+KEMaxPeak
        #write out peak
        f.write(str(indexes[iOfMaxKEPeak])+" "+str(times[iOfMaxKEPeak])+" "+str(KE[iOfMaxKEPeak])
          +" "+str(KE[iOfMaxKEPeak])+" -\n")
        iOfLastPrint=iOfLastPrint+1
      i=i+1
      if i>=len(times)-2:#check for EOF
        bContinue=False
    
    #write out KE from but not including iOfLastPrint upto last value read in
    if not bContinue:
      printStop=len(times)
      for j in range(iOfLastPrint+1,printStop):
        
        #write out KE[j]
        iOfLastPrint=j
        f.write(str(indexes[j])+" "+str(times[j])+" "+str(KE[j])+" - -\n")
  f.close()
  if options.bLconInsteadofKE:
    print __name__+":"+averagePKE.__name__+":Peak Convective Luminosity data saved in \""+averagePKEFile+"\""
  else:
    print __name__+":"+averagePKE.__name__+":Peak Kinetic energy data saved in \""+averagePKEFile+"\""
def main():

    #parse command line options
    (options, args) = parseOptions()

    if len(args) != 1:
        raise Exception("need one argument")

    #set x/y columns for first curve
    data1XColumn = options.x1
    data1YColumn = options.y1

    #x,y columns of second curve
    data2XColumn = options.x2
    data2YColumn = options.y2

    #read in data
    print "reading in file \"" + args[0] + "\" ..."
    inputFile = datafile.DataFile()
    inputFile.readFile(args[0])

    #get first set of data, in our original case it was the model data
    x = inputFile.fColumnValues[:, data1XColumn]
    y = inputFile.fColumnValues[:, data1YColumn]
    data1 = np.append(x.reshape(x.shape[0], 1),
                      y.reshape(y.shape[0], 1),
                      axis=1)

    #get second set of data, in our original case this was the observations
    #x=inputFile[:,data2XColumn]
    #y=inputFile[:,data2YColumn]
    #data2=np.append(x.reshape(x.shape[0],1),y.reshape(y.shape[0],1),axis=1)

    #bin data and get centers, means and standard deviations, of bins
    #this is the model data
    binData = BinnedData()
    binData.addEvenBins(0.0, 0.04, 5)
    binData.addEvenBins(0.04, 0.89, 40)
    binData.addEvenBins(0.89, 1.0, 10)
    binData.binData(data1)
    centers = binData.getBinCenters()
    mean = binData.getMean()
    sigma = binData.getSTDD()

    # for bin in binData.bins:
    # print bin.points, bin.lowerBound, bin.upperBound
    # print

    # print centers
    # print
    # print mean
    # print
    # print sigma

    #calculate model points at observed points
    funcModel = DataFunction(centers, mean)
    modelX = []
    modelY = []
    diffY = []
    i = 0
    j = 0

    for x in inputFile.fColumnValues[:, data2XColumn]:

        if x <= funcModel.maxRange and x > funcModel.minRange:
            modelX.append(x)
            modelY.append(funcModel.getPointByLinearInt(x))
            diffY.append(inputFile.fColumnValues[i, data2YColumn] - modelY[j])
            j += 1
        i += 1

    #bin differences
    binDataRes = BinnedData()
    binDataRes.addEvenBins(0.0, 1.0, 20)
    data = np.transpose(np.array([modelX, diffY]))
    binDataRes.binData(data)
    centersRes = binDataRes.getBinCenters()
    meanRes = binDataRes.getMean()
    sigmaRes = binDataRes.getSTDD()

    #print len(binDataRes.bins)
    #print centersRes
    #print
    #print meanRes
    #print
    #print sigmaRes

    #write residuals of the fit to a file
    fileName = args[0][:len(args[0]) - 4] + "_residual.txt"
    print "writting residuals to \"" + fileName + "\" ..."
    f = open(fileName, 'w')
    header = "phase residual standard_deviation\n"
    f.write(header)
    for i in range(len(centersRes)):
        line = str(centersRes[i]) + " " + str(meanRes[i]) + " " + str(
            sigmaRes[i]) + "\n"
        f.write(line)
    for i in range(len(centersRes)):
        line = str(1 + centersRes[i]) + " " + str(meanRes[i]) + " " + str(
            sigmaRes[i]) + "\n"
        f.write(line)
    f.close()

    #plot stuff
    fig, axs = plt.subplots(nrows=2, ncols=1)
    #fig=plt.figure()
    ax = axs[0]
    #ax.plot(modelX,diffY,"o")
    ax.errorbar(centersRes, meanRes, yerr=sigmaRes)
    #ax.plot(centersRes,meanRes)
    ax.plot([0.0, 1.0], [0.0, 0.0])
    ax.plot([0.0, 1.0], [0.05, 0.05])
    ax.plot([0.0, 1.0], [-0.05, -0.05])
    ax = axs[1]
    ax.plot(modelX, modelY, "o")
    #ax.plot(inputFile.fColumnValues[:,0],inputFile.fColumnValues[:,1],"o")
    ax.plot(inputFile.fColumnValues[:, data2XColumn],
            inputFile.fColumnValues[:, data2YColumn], "ro")
    #ax.errorbar(centers,mean,yerr=sigma)
    plt.ylim((16.5, 14.5))
    plt.xlim((0, 1.0))
    #ax.plot(modelX,modelY,"o")

    plt.show()
Пример #13
0
def ComputeOutputsAcrossAllTrainingData (mm, dataset, outputCol, bHidden = False):

    numTrainingDataSets = len(dataset)       
    selectedTrainingDataSet = 0          
    SSE_Array = np.zeros(numTrainingDataSets+1)
    totalSSE = 0
    
    #used to build matrix for heatmap
    letterClass = 0      
    
    #heatmap of hidden layer activations for each output
    nHidden = mm.ww.shape[0]
    nOut = mm.wv.shape[0]
    heatmap = np.zeros((nOut,nHidden))
    
    #loop through each training set
    while selectedTrainingDataSet < numTrainingDataSets: 
        
        print ' '
        print ' the selected Training Data Set is ', selectedTrainingDataSet
        trainingDataList = dataset[selectedTrainingDataSet]
        
# Note: the trainingDataList is a list comprising several values:
#    - the 0th is the list number 
#    - the 1st is the actual list of the input training values
#    - etc. 

        trainingDataInputList = trainingDataList[1]      
        inputDataArray = np.array(trainingDataInputList)

        letterNum = trainingDataList[2]
        letterChar = trainingDataList[3]
        letterClass = trainingDataList[4]
        outputArrayLength = mm.nOutput
          
        print ' '
        print '  Data Set Number', selectedTrainingDataSet, ' for letter ', letterChar, ' with letter number ', letterNum 
        
        desiredOutputArray = np.zeros(outputArrayLength)    # iniitalize the output array with 0's
        desiredClass = trainingDataList[outputCol]                 # identify the desired class
        #letters are 1 - based, while classes are 0 based
        #if outputCol == 2:
        #    desiredClass = desiredClass-1
        desiredOutputArray[desiredClass] = 1                # set the desired output for that class to 1
        

        #use mlp class         
        outputArray = mm.forward(inputDataArray)
        errorArray = (desiredOutputArray-outputArray)**2
        newSSE = sum(errorArray)
        SSE_Array[selectedTrainingDataSet] = newSSE
        heatmap[desiredClass] = mm.Hidden()

        selectedTrainingDataSet = selectedTrainingDataSet +1 
        
    #keep MSE instead of total
    print 'SSE_Array:'
    print SSE_Array
    MSE = sum(SSE_Array)/numTrainingDataSets
    SSE_Array[numTrainingDataSets] = MSE
    print 'MSE = %.6f' % MSE 
    
    if(bHidden==True):
        fheatmap = datafile.DataFile("heatmap_%d.csv" % nHidden)
        fheatmap.add(heatmap)
        fheatmap.write()
    

    #return the errors
    return(SSE_Array)        
Пример #14
0
def main():
    #parse command line options
    (options, args) = parseOptions()

    import matplotlib
    if not options.show:
        matplotlib.use('Agg')  #needed for saving figures

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec

    #get base file name
    fileName = args[0]
    parts0 = fileName.partition('[')
    baseFileName = parts0[0]
    if (parts0[1] == '' or parts0[2] == ''):
        print __name__ + ":" + main.__name__ + ": error parsing file range from argument \"" + fileName + "\""
        print "  expecting something like BASEFILENAME_t[STARTINDEX-ENDINDEX] ."
        return False

    #get interation range
    parts1 = parts0[2].partition(']')
    if (parts1[1] == ''):
        print __name__ + ":" + main.__name__ + ": error parsing file range from argument \"" + fileName + "\""
        print "  expecting something like BASEFILENAME_t[STARTINDEX-ENDINDEX] ."
        return False
    parts2 = parts1[0].partition('-')
    if (parts2[1] == '' or parts2[2] == ''):
        print __name__ + ":" + main.__name__ + ": error parsing file range from argument \"" + fileName + "\""
        print "  expecting something like BASEFILENAME_t[STARTINDEX-ENDINDEX] ."
        return False
    start = int(parts2[0])
    if parts2[2] == "*":
        end = sys.maxint
    else:
        end = int(parts2[2])

    #make sure that all the combined binary files have profiles made
    make_profiles.make_profiles(options.keep, fileName)

    #get and sort files
    extension = "_pro" + ".txt"
    filesExistProfiles = glob.glob(baseFileName + "*" + extension)
    files = []
    for file in filesExistProfiles:
        intOfFile = int(file[len(baseFileName):len(file) - len(extension)])
        if intOfFile >= start and intOfFile < end:
            files.append(file)

    files.sort()
    if len(files) == 0:
        print __name__ + ":" + main.__name__ + ": no files found matching " + baseFileName + "*" + extension
        return False

    #get surface velocity for all files in range, and find max/min of value to plotting
    L_LogTeff5mLogTeff45 = []
    L_LogTeff5mLogTeff5_t0 = []
    L_LogTeff45m3ZonesIn = []
    fileTimes = []
    nCount = 0
    max = -1e300
    min = 1e300
    L_at_LogTeff50 = None
    U0_3zonesin = []
    for file in files:
        fileData = datafile.DataFile()
        fileData.readFile(file)

        L_at_LogTeff5 = None
        L_at_LogTeff45 = None
        L_at_3ZonesIn = None
        for i in range(len(fileData.fColumnValues)):
            if fileData.fColumnValues[i][25] != None:

                #find L at Log(Teff)=5
                logTTest = math.log10(fileData.fColumnValues[i][25])
                if logTTest < 5.0 and L_at_LogTeff5 == None:
                    L_at_LogTeff5 = fileData.fColumnValues[i][31]
                    if L_at_LogTeff50 == None:
                        L_at_LogTeff50 = fileData.fColumnValues[i][31]

                #find L at Log(Teff)=4.5
                if logTTest < 4.5 and L_at_LogTeff45 == None:
                    L_at_LogTeff45 = fileData.fColumnValues[i][31]

        L_at_3ZonesIn = fileData.fColumnValues[len(fileData.fColumnValues) -
                                               4][31]
        L_LogTeff5mLogTeff45.append(L_at_LogTeff5 - L_at_LogTeff45)
        L_LogTeff45m3ZonesIn.append(L_at_LogTeff45 - L_at_3ZonesIn)
        L_LogTeff5mLogTeff5_t0.append(L_at_LogTeff5 - 50.0)
        U0_3zonesin.append(fileData.fColumnValues[len(fileData.fColumnValues) -
                                                  4][12])

        #get file times
        fileHeader = fileData.sHeader.split()
        fileTimes.append(float(fileHeader[1]))

    #make plot
    fig = plt.figure(figsize=(13, 8))
    if True:
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=2)
        ax2 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
    else:
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=3)
        #ax2 = plt.subplot2grid((3,3), (2,0),colspan=3)

    #turn on grid?
    if not options.noGrid:
        ax1.grid()
        ax2.grid()

    #creat plot string, (format of plot)
    plotString = '-'
    if options.noLines:
        plotString = 'o'
    if options.points:
        plotString = plotString + 'o'

    #set plot limits
    if options.ymin != None:
        min = options.ymin
    if options.ymax != None:
        max = options.ymax
    print "min=", min, " max=", max

    #make plot
    ax1.plot(fileTimes,
             L_LogTeff5mLogTeff45,
             plotString,
             label="L(LogTeff=5)-L(LogTeff=4.5)",
             markersize=6)
    ax1.plot(fileTimes,
             L_LogTeff45m3ZonesIn,
             plotString,
             label="L(LogTeff=45)-L(3 zones in)",
             markersize=4)
    ax1.plot(fileTimes,
             L_LogTeff5mLogTeff5_t0,
             plotString,
             label="L(LogTeff=5)-L(LogTeff=5)_t=0",
             markersize=2)
    ax1.legend()
    #plt.ylim(options.ymin,options.ymax)
    ax2.set_xlabel("t [s]")
    ax1.set_ylabel("L [L_sun]")
    ax2.set_ylabel("U0 3 zones in")
    ax2.plot(fileTimes, U0_3zonesin, '-')

    if options.show:
        plt.show()
    else:
        sOutFileName = options.outputFile + "." + options.format
        print __name__ + ":" + main.__name__ + ": creating plot \"" + sOutFileName + "\" from file " + file + "..."
        fig.savefig(sOutFileName, format=options.format,
                    transparent=False)  #save to file

    ax1.cla()  #clear plot
Пример #15
0
def makeFits(fits):
  """Creates fits
  """
  
  for fitSettings in fits:
    print "fitting ",fitSettings['input'],
    
    #get input model to fit
    inputData=datafile.DataFile()
    inputData.readFile(fitSettings['input'])
    
    x1_0=inputData.fColumnValues[:,0]
    y1_0=inputData.fColumnValues[:,1]
    data=np.empty(inputData.fColumnValues.shape)
    DMInit=fitSettings['DMInit']
    PSInit=fitSettings['PSInit']
    
    DM=DMInit#distance modulus, this must be a good estimate, radius of convergence is small
    PS=PSInit
    
    f=open(fitSettings['output'],'w')
    f.write("Fit norm DM PS count DNDDMC DNDPSC\n")
    for fitTo in fitSettings['fit-to']:
      print "to "+fitTo
      #read in data to fit to
      fitData=datafile.DataFile()
      fitData.readFile(fitTo)
      
      PS=PSInit#reset phase shift initialization every fit (they aren't related)
      DM=DMInit#reset phase shift initialization every fit (they aren't related)
      DeltaDM=1.0e-7
      DeltaPS=1.0e-5
      epsDM=1e-4
      epsPS=1e-2
      fracDM=0.8#fraction of correction to add
      fracPS=0.2
      #setup the fit with the data
      bContinue=True
      fitCreated=True
      fitFailedMessage=""
      try:
        fit=Fit(inputData.fColumnValues,fitData.fColumnValues)
      except Exception as e:
        fitFailedMessage=e
        fitCreated=False
        bContinue=False
      
      #this will be a loop until updates to DM and PS get small
      PSCorrection=epsPS+1.0
      DMCorrection=epsDM+1.0
      count=0
      maxIter=20
      while(bContinue):
        
        #Compute first and second derivatives in DM direction
        
        #get three points to evaluate first and second derivatives with
        norm=fit.computeNormOfDifferencePointByPoint(DM,PS)#at centre point
        normmDM=fit.computeNormOfDifferencePointByPoint(DM-DeltaDM,PS)#at DM-Delta
        normpDM=fit.computeNormOfDifferencePointByPoint(DM+DeltaDM,PS)#at DM+Delta

        #compute first derivatives
        DNDDMC=(normpDM-normmDM)/(2.0*DeltaDM)
        DNDDML=(norm-normmDM)/(DeltaDM)
        DNDDMH=(normpDM-norm)/(DeltaDM)
        DNDDM2=(DNDDMH-DNDDML)/(DeltaDM)
        
        #Compute first and second derivatives in PS direction
        
        #get three points to evaluate first and second derivatives with
        normmPS=fit.computeNormOfDifferencePointByPoint(DM,PS-DeltaPS)#at PS-Delta
        normpPS=fit.computeNormOfDifferencePointByPoint(DM,PS+DeltaPS)#at PS+Delta
        
        #compute first derivatives
        DNDPSC=(normpPS-normmPS)/(2.0*DeltaPS)
        DNDPSL=(norm-normmPS)/(DeltaPS)
        DNDPSH=(normpPS-norm)/(DeltaPS)
        DNDPS2=(DNDPSH-DNDPSL)/(DeltaPS)
        
        if(abs(DNDDM2)>epsDM):
          if(DNDDM2!=0.0):
            DMCorrection=-1.0*DNDDMC/(DNDDM2)
          else:
            DMCorrection=1e-10
        else:
          DMCorrection=0.0
          
        #only correct if needed
        if(abs(DNDPSC)>epsPS):
          if(DNDPS2!=0.0):
            PSCorrection=-1.0*DNDPSC/(DNDPS2)
          else:
            PSCorrection=1e-10
        else:
          PSCorrection=0.0
        
        DM=DM+DMCorrection*fracDM
        PS=PS+PSCorrection*fracPS
        if(PS>1.0):
          PS=int(PS)-PS
        elif(PS<-1.0):
          PS=int(PS)+PS
        print "    norm=",norm," count=",count
        print "    DM=",DM," DMCorrection=",DMCorrection," DNDDMC=",DNDDMC," DNDDM2=",DNDDM2," DNDDML=",DNDDML," DNDDMH=",DNDDMH
        print "    PS=",PS," PSCorrection=",PSCorrection," DNDPSC=",DNDPSC," DNDPS2=",DNDPS2," DNDPSL=",DNDPSL," DNDPSH=",DNDPSH
        if(abs(DNDDMC)<epsDM):
          #print "DNDDMC<eps"
          if(abs(DNDPSC)<epsPS):
            #print "DNDPSC<eps"
            bContinue=False
        if(count>=maxIter):
          print "  **FAILED TO CONVERGE**",
          bContinue=False
        count+=1
      
      if fitCreated:
        norm=fit.computeNormOfDifferencePointByPoint(DM,PS)#at centre point
        print "  "+str(fitTo)+" norm=",norm," DM=",DM," PS=",PS," count=",count," DNDDMC=",DNDDMC," DNDPSC=",DNDPSC
        f.write(str(fitTo)+" "+str(norm)+" "+str(DM)+" "+str(PS)+" "+str(count)+" "+str(DNDDMC)+" "+str(DNDPSC)+"\n")
      else:
        print str(fitFailedMessage)
        f.write(str(fitFailedMessage))
      
      #reset initial guesses as, we just failed to converge
      if(count>=maxIter):
        DM=DMInit
        PS=PSInit
    f.close()
Пример #16
0
def ComputeAll (mm, dataset, outputCol, bHidden=False):

    
    #get a count of the number of letters being tested
    #a letter may have multiple variants
    dd = pd.DataFrame(dataset)
    letters = dd[3]
    freq = pd.value_counts(letters)
    llist = list(freq.index)
    
    heatmap = np.zeros((mm.nOutput,mm.nHidden))
    
    #initialize dictionary
    #using a dictionary because test set may not have all letters
    SSE_Dict = {}
    for ll in llist:
        SSE_Dict[ll]=0
        
    #loop through each training set
    for iTest in range(len(dataset)):
        
        #test sample
        trainingDataList = dataset[iTest]
        
        #input values
        trainingDataInputList = trainingDataList[1]      
        inputDataArray = np.array(trainingDataInputList)

        letterNum = trainingDataList[2]
        letterChar = trainingDataList[3]
        letterClass = trainingDataList[4]
        outputArrayLength = mm.nOutput
          
        desiredClass = trainingDataList[outputCol]                 # identify the desired class
        #letters are 1 - based, while classes are 0 based
        #if outputCol == 2:
        #    desiredClass = desiredClass-1
        desiredOutputArray = np.zeros(outputArrayLength)    # iniitalize the output array with 0's
        desiredOutputArray[desiredClass] = 1                # set the desired output for that class to 1
        
        #use mlp class         
        outputArray = mm.forward(inputDataArray)
        
        #calculate SSE
        errorArray = (desiredOutputArray-outputArray)**2
        newSSE = sum(errorArray)
        
        #save error in dictionary
        e = SSE_Dict[letterChar]
        e = e + newSSE
        SSE_Dict[letterChar] = e
        
        #build heatmap
        heatmap[desiredClass] = mm.Hidden()
        
        
    #calculate average error of each SSE value
    for char in SSE_Dict:
        e = SSE_Dict[char]/freq[char]
        SSE_Dict[char] = e
    
    if(bHidden==True):
        fheatmap = datafile.DataFile("heatmap_%d.csv" % mm.nHidden)
        fheatmap.add(heatmap)
        fheatmap.write()

    #return the errors
    return(SSE_Dict)        
Пример #17
0
def main():
  
  #parse command line options
  (options,args)=parseOptions()
  
  #get XML settings
  currentSettings=Settings()
  currentSettings.parseXML(args[0])
  
  #get base file name
  [start,end,baseFileName]=disect_filename.disectFileName(currentSettings.files)
  
  #make sure that all the combined binary files have profiles made
  failedFiles=make_profiles.make_profiles(options.keep,currentSettings.files,options.remake,False)
  
  #get period ranges and times for each period from PKE file 
  [periodRange,time]=getPeriodRanges(baseFileName,start,end,options)
  
  dWSum=[]
  dWSumError=[]
  dClosestToTurn=[]
  
  #get all possible file names
  extension="_pro"+".txt"
  filesExistProfiles=glob.glob(baseFileName+"*"+extension)
  filesExistProfiles.sort()
  
  #check for the t=0 model
  if currentSettings.workPlotSettings.temperatureProfileFile!=None:#if set use user set value
    firstFile=currentSettings.workPlotSettings.temperatureProfileFile
  else:
    firstFile=baseFileName+"00000000"
  failedFiles2=make_profiles.make_profiles(options.keep,firstFile,options.remake,False)
  if not os.path.isfile(firstFile+extension):
    warnings.warn("didn't find profile or dump at \""+firstFile+extension+
      "\" using, \""+filesExistProfiles[0]+"\" instead.")
    firstFile=filesExistProfiles[0]
  else:
    firstFile+=extension
  
  fileData=datafile.DataFile()
  fileData.readFile(firstFile)
  Log10T=fileData.fColumnValues[0:len(fileData.fColumnValues)-1,currentSettings.tColumn]
  Log10T=np.array(Log10T,dtype=np.float64)#force double precision
  Log10T=np.log10(Log10T)
  
  #find out how close to the surface to go
  nEndZone=len(fileData.fColumnValues)-1
  for j in range(len(fileData.fColumnValues)-1):#position
    if Log10T[j]<currentSettings.workPlotSettings.minTemp:
      nEndZone=j
      break
  
  for n in range(len(periodRange)):
    
    #get and sort files
    files=[]
    for file in filesExistProfiles:
      intOfFile=int(file[len(baseFileName):len(file)-len(extension)])
      if intOfFile>=periodRange[n][0] and intOfFile<periodRange[n][1]:
        files.append(file)
    
    #check to make sure we have a start and end for the period
    if periodRange[n][1]==None:
      raise Exception("file range index range "+str(start)+"-"+str(end)+" should contain at least"
        +" one period as indicated by PKE peaks, but does not.")
    
    if len(files)<3:
      raise Exception("need more than 3 files to compute the work, likely alot more with file"
        +" indices in the range  ("+str(periodRange[n][0])+","+str(periodRange[n][1])+")")
    
    #for first model dump
    fileData=datafile.DataFile()
    print "reading file ",files[0]," ..."
    fileData.readFile(files[0])
    
    #read in p, and 1/rho for first file
    p=np.zeros( (len(fileData.fColumnValues)-1,len(files)+1) )
    rhoInvert=np.zeros( (len(fileData.fColumnValues)-1,len(files)+1) )
    deltaM=np.zeros( (len(fileData.fColumnValues)-1,len(files)+1) )
    temperature=np.zeros( (len(fileData.fColumnValues)-1,len(files)+1) )
    maxP=np.empty(len(fileData.fColumnValues)-1)
    maxP.fill(-1.0*sys.float_info.max)
    minP=np.empty(len(fileData.fColumnValues)-1)
    minP.fill(sys.float_info.max)
    for i in range(len(fileData.fColumnValues)-1):
      p[i][0]=fileData.fColumnValues[i][currentSettings.pColumn]
      p[i][len(files)]=fileData.fColumnValues[i][currentSettings.pColumn]
      if currentSettings.AV:
        p[i][0]+=fileData.fColumnValues[i][currentSettings.QColumn]
        p[i][len(files)]+=fileData.fColumnValues[i][currentSettings.QColumn]
        
      #get max P
      if p[i][0]>=maxP[i]:
        maxP[i]=p[i][0]
      
      #get min P
      if p[i][0]<=minP[i]:
        maxP[i]=p[i][0]
      
      rhoInvert[i][0]=1.0/fileData.fColumnValues[i][currentSettings.rhoColumn]
      rhoInvert[i][len(files)]=1.0/fileData.fColumnValues[i][currentSettings.rhoColumn]
      deltaM[i][0]=fileData.fColumnValues[i][currentSettings.deltaMColumn]
      deltaM[i][len(files)]=fileData.fColumnValues[i][currentSettings.deltaMColumn]
      temperature[i][0]=fileData.fColumnValues[i][currentSettings.tColumn]
      temperature[i][len(files)]=fileData.fColumnValues[i][currentSettings.tColumn]
    
    #read all files in for current period
    for i in range(1,len(files)):#for each dump
      
      print "reading file ",files[i]," ..."
      
      fileData.readFile(files[i])
      
      #check headers for used columns
      if fileData.sColumnNames[currentSettings.pColumn]!=currentSettings.pColumnHeader:
        warings.warn("file \""+files[i]+" has pressure column header as \""\
          +fileData.sColumnNames[currentSettings.pColumn]+" expected something like \""\
          +currentSettings.pColumnHeader+"\".")
      if currentSettings.AV:
        if fileData.sColumnNames[currentSettings.QColumn]!=currentSettings.QColumnHeader:
          warnings.warn("file \""+files[i]+" has A.V. column header as \""\
            +fileData.sColumnNames[currentSettings.QColumn]+" expected something like \""\
            +currentSettings.QColumnHeader+"\".")
      if fileData.sColumnNames[currentSettings.rhoColumn]!=currentSettings.rhoColumnHeader:
        warnings.warn("file \""+files[i]+" has density column header as \""\
          +fileData.sColumnNames[currentSettings.rhoColumn]+" expected something like \""\
          +currentSettings.rhoColumnHeader+"\".")
      if fileData.sColumnNames[currentSettings.deltaMColumn]!=currentSettings.deltaMColumnHeader:
        warnings.warn("file \""+files[i]+" has delta M_r column header as \""\
          +fileData.sColumnNames[currentSettings.deltaMColumn]+" expected something like \""\
          +currentSettings.deltaMColumnHeader+"\".")
      
      for j in range(len(fileData.fColumnValues)-1):#for each zone
        p[j][i]=fileData.fColumnValues[j][currentSettings.pColumn]
        if currentSettings.AV:
          p[j][i]+=fileData.fColumnValues[j][currentSettings.QColumn]
          
        #get max P
        if p[j][i]>=maxP[j]:
          maxP[j]=p[j][i]
        
        #get min P
        if p[j][i]<=minP[j]:
          maxP[j]=p[j][i]
        
        deltaM[j][i]=fileData.fColumnValues[j][currentSettings.deltaMColumn]
        rhoInvert[j][i]=1.0/fileData.fColumnValues[j][currentSettings.rhoColumn]
    
    #compute work
    dW=np.zeros(len(fileData.fColumnValues)-1)
    for i in range(1,len(files)+1):#time
      for j in range(0,nEndZone):#position
        
        dW[j]+=(0.5*(rhoInvert[j][i]-rhoInvert[j][i-1])*(p[j][i]+p[j][i-1]))*deltaM[j][i]
    
    dWMax=0.0
    dWMin=0.0
    dWError=np.zeros(len(fileData.fColumnValues)-1)
    dClosenessToEdge=np.zeros(len(fileData.fColumnValues)-1)
    dWSum.append(0.0)
    dClosestToTurnThisPeriod=sys.float_info.max
    for j in range(0,nEndZone):#position
      dWSum[n]+=dW[j]
      
      #
      y1=p[j][len(files)]
      y0=p[j][len(files)-1]
      ym1=p[j][len(files)-2]
      x1=rhoInvert[j][len(files)]
      x0=rhoInvert[j][len(files)-1]
      xm1=rhoInvert[j][len(files)-2]
      deltaX=(x1-x0)
      y1py0=y1+y0
      tiny=1e-300#fixes the case where things don't move, e.g. near the core
      m=(y0-ym1)/((x0-xm1)+tiny)
      y1p=y0+m*deltaX
      y1ppy0=y1p+y0
      #how far off is our guess from assuming a linear extrapolation
      dW1=0.5*deltaX*y1py0#work from setting first point equal to last point
      dW1p=0.5*deltaX*y1ppy0#work from linear extrapolation
      dWError[j]=abs(dW1-dW1p)*deltaM[j][len(files)]
      dWMax+=dW[j]+dWError[j]
      dWMin+=dW[j]-dWError[j]
      
      #how close to a turn is the start/end position
      dtotal=maxP[j]-minP[i]
      dmax=max(abs(p[j][0]-maxP[j]),abs(p[j][0]-minP[j]))
      distanceFromEdge=dmax/dtotal#will be 1/2 if near middle, will be near 0 if near edge
      
      #keep value of closest start/end position to turn
      if distanceFromEdge<dClosestToTurnThisPeriod:
        dClosestToTurnThisPeriod=distanceFromEdge
    dClosestToTurn.append(dClosestToTurnThisPeriod)
    dWSumError.append((dWMax-dWMin)*0.5)
    
    #make plots of P and 1/rho for each zone
    fig=plt.figure(figsize=(13,8))
    ax1 = plt.subplot2grid((3,3), (0,0),colspan=3,rowspan=3)
    if currentSettings.plotPdVCurves:
      for j in range(currentSettings.PdVPlotSettings.startZone,len(fileData.fColumnValues)-1):
        #print rhoInvert[j]
        make_PdV_plot(rhoInvert[j],p[j],j,n,fig,ax1,currentSettings.PdVPlotSettings)
    
    #make work plot
    make_Work_plot(Log10T,dW,dWError,fig,ax1
      ,currentSettings.workPlotSettings,n,time[n],"Log10(T)")
    make_Work_plot(range(len(fileData.fColumnValues)-1),dW,dWError,fig,ax1
      ,currentSettings.workPlotSettings,n,time[n],"zone #")
    
  #print out total work done by model
  f=open("work_per_period.txt",'w')
  f.write("period time[s]      work[ergs] work_uncertianty[ergs] relative_distance_from_turn\n")
  for i in range(len(dWSum)):
    f.write(str(i)+" "+str(time[i])+" "+str(dWSum[i])+" "+str(dWSumError[i])+" "
      +str(dClosestToTurn[i])+"\n")
  f.close()
Пример #18
0
def main():
    #parse command line options
    (options, args) = parseOptions()

    import matplotlib
    if not options.show:
        matplotlib.use('Agg')  #needed for saving figures

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec

    #get base file name
    fileName = args[0]
    [start, end, baseFileName] = disect_filename.disectFileName(fileName)

    #make sure that all the combined binary files have profiles made
    make_profiles.make_profiles(options.keep, fileName, options.remake,
                                options.remakeBins)

    #get and sort files
    extension = "_pro" + ".txt"
    filesExistProfiles = glob.glob(baseFileName + "*" + extension)
    files = []
    for file in filesExistProfiles:
        intOfFile = int(file[len(baseFileName):len(file) - len(extension)])
        if intOfFile >= start and intOfFile < end:
            files.append(file)

    files.sort()

    #get surface velocity for all files in range, and find max/min of value to plotting
    surfaceLuminosity = []
    fileTimes = []
    nCount = 0
    max = -1e60
    min = 1e60
    nColumn = 55
    nZonesFromSurface = 3
    for file in files:

        print "reading in profile ", file, " ..."
        fileData = datafile.DataFile()
        fileData.readFile(file)
        surfaceLuminosity.append(
            fileData.fColumnValues[len(fileData.fColumnValues) -
                                   nZonesFromSurface - 1][nColumn])
        fileHeader = fileData.sHeader.split()
        fileTimes.append(float(fileHeader[1]))
        nCount = nCount + 1

        if max < fileData.fColumnValues[
                len(fileData.fColumnValues) - nZonesFromSurface -
                1][nColumn] and fileData.fColumnValues[
                    len(fileData.fColumnValues) - nZonesFromSurface -
                    1][nColumn] != None:
            max = fileData.fColumnValues[len(fileData.fColumnValues) -
                                         nZonesFromSurface - 1][nColumn]
        if min > fileData.fColumnValues[
                len(fileData.fColumnValues) - nZonesFromSurface -
                1][nColumn] and fileData.fColumnValues[
                    len(fileData.fColumnValues) - nZonesFromSurface -
                    1][nColumn] != None:
            min = fileData.fColumnValues[len(fileData.fColumnValues) -
                                         nZonesFromSurface - 1][nColumn]

    #make plots
    nCount = 0
    fig = plt.figure(figsize=(13, 8))
    ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=3)

    if not options.noGrid:
        ax1.grid()
    plotString = '-'
    if options.noLines:
        plotString = 'o'
    if options.points:
        plotString = plotString + 'o'

    if options.ymin != None:
        min = options.ymin
    if options.ymax != None:
        max = options.ymax
    print "min=", min, " max=", max
    ax1.plot(fileTimes, surfaceLuminosity, plotString)
    plt.ylim(min, max)
    ax1.set_xlabel("t [s]")
    ax1.set_ylabel("L [L_sun]")
    ax1.set_title("Ligh Curve")

    if options.show:
        plt.show()
    else:
        sOutFileName = options.outputFile + "." + options.format
        print __name__ + ":" + main.__name__ + ": creating plot \"" + sOutFileName + "\" from file " + file + "..."
        fig.savefig(sOutFileName, format=options.format,
                    transparent=False)  #save to file

    ax1.cla()  #clear plot
Пример #19
0
def run_test(dataset,
             filterMasks,
             gb_base,
             tTransferH,
             alpha,
             eta,
             seed,
             nHidden=6,
             bOutputByClass=True,
             bHidden=False,
             bWeights=False):

    # Define the global variables
    global inputArrayLength
    global hiddenArrayLength
    global outputArrayLength
    global gridWidth
    global gridHeight
    global eGH  # expandedGridHeight, defined in function expandLetterBoundaries
    global eGW  # expandedGridWidth defined in function expandLetterBoundaries

    #noise factor
    #(no noise)
    gamma = 0.0

    ####################################################################################################
    # Obtain unit array size in terms of array_length (M) and layers (N)
    ####################################################################################################

    # This calls the procedure 'welcome,' which just prints out a welcoming message.
    # All procedures need an argument list.
    # This procedure has a list, but it is an empty list; welcome().

    welcome()

    # Right now, for simplicity, we're going to hard-code the numbers of layers that we have in our
    #   multilayer Perceptron (MLP) neural network.
    # We will have an input layer (I), an output layer (O), and a single hidden layer (H).

    # Define the variable arraySizeList, which is a list. It is initially an empty list.
    # Its purpose is to store the size of the array.

    arraySizeList = list()  # empty list

    # Obtain the actual sizes for each layer of the network
    arraySizeList = obtainNeuralNetworkSizeSpecs(dataset, filterMasks, nHidden,
                                                 bOutputByClass)

    # Unpack the list; ascribe the various elements of the list to the sizes of different network layers
    # Note: A word on Python encoding ... the actually length of the array, in each of these three cases,
    #       will be xArrayLength. For example, the inputArrayLength for the 9x9 pixel array is 81.
    #       These values are passed to various procedures. They start filling in actual array values,
    #       where the array values start their count at element 0. However, when filling them in using a
    #       "for node in range[limit]" statement, the "for" loop fills from 0 up to limit-1. Thus, the
    #       original xArrayLength size is preserved.
    inputArrayLength = arraySizeList[0]
    hiddenArrayLength = arraySizeList[1]
    outputArrayLength = arraySizeList[2]
    outputClassColumn = arraySizeList[3]

    print ' '
    print ' inputArrayLength = ', inputArrayLength
    print ' hiddenArrayLength = ', hiddenArrayLength
    print ' outputArrayLength = ', outputArrayLength
    print ' outputClassColumn = ', outputClassColumn

    #load greybox mlp from previous weeks
    #todo: number of hidden nodes for both gb and cnn
    #gb = mlp.mlp.read('from_week4/weights_%d' % nHidden)
    gb = mlp.mlp.read(gb_base)

    #init new cnn
    mm = cnn.cnn.init(gb, inputArrayLength, hiddenArrayLength, tTransferH,
                      outputArrayLength, mlp.TransferSigmoid(alpha), eta)

    # Parameter definitions for backpropagation, to be replaced with user inputs
    #alpha = 1.0
    #eta = 0.5
    maxNumIterations = MAX_ITERATIONS  # temporarily set to 10 for testing
    epsilon = 0.01
    iteration = 0
    SSE = 0.0
    numTrainingDataSets = len(dataset)
    dd = pd.DataFrame(dataset)

    ####################################################################################################
    # Initialize the weight arrays for two sets of weights; w: input-to-hidden, and v: hidden-to-output
    ####################################################################################################

    #randomize initial weights
    mm.randomize(seed)

    ####################################################################################################
    # Before we start training, get a baseline set of outputs, errors, and SSE
    ####################################################################################################

    print ' '
    print '  Before training:'

    SSE_Dict = ComputeAll(mm,
                          dataset,
                          filterMasks,
                          outputClassColumn,
                          bHidden=False)

    ####################################################################################################
    # Next step - Obtain a single set of randomly-selected training values for alpha-classification
    ####################################################################################################

    while iteration < maxNumIterations:

        # Increment the iteration count
        iteration = iteration + 1

        ####################################################################################################
        # While training - STEP 1: Obtain a set of training data; inputs and desired outputs
        ####################################################################################################

        # Randomly select one of 26 traing sets. number returned is 0 <= n <= numTrainingDataSets-1
        #        index = random.randint(0, numTrainingDataSets-1)
        index = random.randint(1, numTrainingDataSets)
        index = index - 1

        # We return the list from the function, with values placed inside the list.
        #        print ' in main, about to call obtainSelected'
        trainingDataList = dataset[index]

        # Optional print/debug
        #        printLetter(trainingDataList)

        ####################################################################################################
        # While training - STEP 2: Create an input array based on the input training data list
        ####################################################################################################

        # The trainning inputs are drawn from the first element (starting count at 0) in the training data list

        thisTrainingDataList = trainingDataList[1]
        inputDataArray = np.array(thisTrainingDataList)

        #add random noise to input data
        inputDataLen = len(inputDataArray)

        #need some un-noisy samples?
        noise = np.zeros(inputDataLen)
        if addNoise == True:
            if random.random() < .5:
                noise = np.random.random(inputDataLen) < gamma
        noisyInputDataArray = abs(inputDataArray - noise)

        # The desired outputs are drawn from the fourth element (starting count at 0) in the training data list
        #   This represents the "big shape class" which we are training towards in GB1

        desiredOutputArray = np.zeros(
            outputArrayLength)  # iniitalize the output array with 0's
        desiredClass = trainingDataList[
            outputClassColumn]  # identify the desired class
        desiredOutputArray[
            desiredClass] = 1  # set the desired output for that class to 1

        ####################################################################################################
        # Compute a single feed-forward pass and obtain the Actual Outputs
        ####################################################################################################

        #run input through filter
        filteredInputDataArray = filterMasks.FilterLetter(noisyInputDataArray)

        ## use mlp class
        outputArray = mm.forward(noisyInputDataArray, filteredInputDataArray)

        ####################################################################################################
        # Perform backpropagation
        ####################################################################################################

        ### use mlp class
        mm.backprop(filteredInputDataArray, desiredOutputArray)

        # Compute a forward pass, test the new SSE
        outputArray = mm.forward(noisyInputDataArray, filteredInputDataArray)

        #not keeping track of all outputs any more
        #keeping average SSE across all letter variants
        #so do a ComputeAll every 100 iterations

        # Determine the error between actual and desired outputs
        maxSSE = epsilon + 1
        if iteration % 100 == 0:

            maxSSE = 0
            SSE_Dict = ComputeAll(mm,
                                  dataset,
                                  filterMasks,
                                  outputClassColumn,
                                  bHidden=False)
            numvals = len(SSE_Dict.keys())

            #add up all the SSEs
            for k in SSE_Dict:
                sse = SSE_Dict[k]
                if (sse > maxSSE):
                    maxSSE = sse

        #break out when worst SSE is less than threshold
        if maxSSE < epsilon:
            break

    print 'Out of while loop at iteration ', iteration

    ####################################################################################################
    # After training, get a new comparative set of outputs, errors, and SSE
    ####################################################################################################

    print ' '
    print '  After training:'

    #    SSE_Array = ComputeOutputsAcrossAllTrainingData (mm, dataset, outputClassColumn, bHidden=True)
    SSE_Dict = ComputeAll(mm,
                          dataset,
                          filterMasks,
                          outputClassColumn,
                          debug=True,
                          bHidden=True)
    SSE_Array = FlattenDict(SSE_Dict)
    MSE = sum(SSE_Array) / len(SSE_Array)

    #save weight array
    if (bWeights == True):

        mm.write_weights('weights_%d' % nHidden)

    #MSE and iterations by epoch
    if (bHidden == True):
        f = datafile.DataFile("sse_%d.csv" % nHidden)
        f.add(SSE_Array)
        f.write()

    print('iterations = %d' % iteration)
    print('MSE = %.6f' % MSE)

    return (iteration, MSE)
Пример #20
0
    ax1.set_title("P(B1) vs. Assurance Monitor output")
    ax1.legend(loc="best")
    ax1.set_ylim([0.0, np.max(y_sig_max_likelihood/avg_prob) + 0.1])
    plt.show()


if __name__ == "__main__":
    # DATA_FILE = "example_data/no_fault.csv"
    # DATA_FILE = "estimation_data/c_0.0_p_0.0_d_0.0/fm0.csv"
    # data_files = [DATA_FILE]

    DATA_DIR = "estimation_data"
    data_file_names = glob.glob(os.path.join(DATA_DIR, "**", "*.csv"), recursive=True)

    # Construct DataFile objects
    datafiles = []
    for file in data_file_names:
        df = datafile.DataFile()
        df.load_csv(file)
        df.calc_metrics()
        datafiles.append(df)

    # Plot each datafile
    for df in datafiles:
        plot_data_file(df)

    calculate_statistics(datafiles)
    print("\n")

    plot_outcomes(datafiles)
Пример #21
0
    def readProfiles(self, options):
        '''Reads the needed data to create the light curve from the radial profile files'''

        #make sure needed radial profiles are made
        fileName = self.baseFileName + "[" + str(self.start) + "-" + str(
            self.end) + "]"
        failedFiles = make_profiles.make_fileSet(fileName,
                                                 options)  #default is to make
        #a profile set

        #let user know if there were any failed files
        if len(failedFiles) > 0:
            for failedFile in failedFiles:
                print failedFile

        #get and sort profiles within range of dataset
        extension = "_pro" + ".txt"
        filesExistProfiles = glob.glob(self.baseFileName + "*" + extension)
        filesExistProfiles.sort()
        files = []
        for i in range(0, len(filesExistProfiles), self.frequency):
            file = filesExistProfiles[i]
            intOfFile = int(file[len(self.baseFileName):len(file) -
                                 len(extension)])
            if intOfFile >= self.start and intOfFile < self.end:
                files.append(file)
        if len(files) == 0:
            raise Exception("no files found in range")

        self.nNumFiles = len(files)

        #get time, Luminosity, Teff, M_r and grid velocity
        self.luminosity = []
        self.temperature = []
        self.interiorMass = []
        self.time = []
        self.gridVelocity = []
        self.radius = []
        count = 1

        for file in files:

            #read data file
            print "reading in \"" + file + "\" " + str(count) + "/" + str(
                self.nNumFiles) + " ..."
            fileData = datafile.DataFile()
            fileData.readFile(file)

            #get time
            #these are required in case the time isn't space seperated
            fileHeader = fileData.sHeader.split("=")
            indexBracket = fileHeader[1].find("[s]")
            self.time.append(float(fileHeader[1][0:indexBracket]))

            #get index for surface zone, minus zoneFromSurf
            rowIndex = len(fileData.fColumnValues) - 1 - self.zonesFromSurf

            #get luminosity
            self.luminosity.append(
                fileData.fColumnValues[rowIndex][luminosityIndex])

            #get Teff, of outer interface
            temp = (fileData.fColumnValues[rowIndex -
                                           1][tempIndex]) * (2**(0.25))
            self.temperature.append(temp)

            #get M_r
            self.interiorMass.append(
                fileData.fColumnValues[rowIndex][massIndex])

            #get radius
            self.radius.append(fileData.fColumnValues[rowIndex][radiusIndex])

            #get grid velocity
            self.gridVelocity.append(
                fileData.fColumnValues[rowIndex][gridUIndex])

            #increment file counter
            count = count + 1
Пример #22
0
def test_hidden(dataset, filterMasks, gb_base, tTransferH, alpha, eta,
                nHidden):

    #just keep running tests with random seeds
    #keep track of which had the lowest iteration count
    epoch = 0
    best_iterations = 1e6
    best_seed = 1e6
    best_sse = 1e6
    best_epoch = 0

    #start a data file
    #this is overall mse and iterations
    f = datafile.DataFile("training_%d.csv" % nHidden)

    while epoch < 25:
        seed = random.randint(1, MAXINT)
        print '###################'
        print 'epoch = %d' % epoch
        print 'seed = %d' % seed
        print '###################'

        #use previously found best alpha and eta
        rc = run_test(dataset,
                      filterMasks,
                      gb_base,
                      tTransferH,
                      alpha,
                      eta,
                      seed=seed,
                      nHidden=nHidden,
                      bOutputByClass=True,
                      bHidden=True,
                      bWeights=True)
        iterations = rc[0]
        sse = rc[1]
        #        if iterations < best_iterations:
        if sse < best_sse:
            best_iterations = iterations
            best_sse = sse
            best_seed = seed
            best_epoch = epoch

        #print after every loop
        print '*******'
        print('best iterations = %d' % best_iterations)
        print('seed = %d' % best_seed)
        print('MSE = %.4f' % best_sse)
        print '*******'

        row = np.array([iterations, sse])
        f.add(row)

        #keep running average of sse
        fsseavg = datafile.DataFile("sse_avg_%d.csv" % nHidden)
        fsse = datafile.DataFile("sse_%d.csv" % nHidden)
        fsse.read()
        if epoch == 0:
            #don't include total mse
            fsseavg.add(fsse.array())
        else:
            fsseavg.read()
            a1 = fsseavg.array()
            a2 = fsse.array()
            aa = a1 + a2
            #dont include total mse
            fsseavg.clear()
            fsseavg.add(aa)

        fsseavg.write()

        #next loop
        epoch = epoch + 1

    #save the training SSE results
    f.write()

    #averages
    fsseavg = datafile.DataFile("sse_avg_%d.csv" % nHidden)
    fsseavg.read()
    a_sse = fsseavg.array()
    a_sse = [x / epoch for x in a_sse]
    fsseavg.clear()
    fsseavg.add(a_sse)
    fsseavg.write()

    #create plots

    #iterations by runs
    df = DataFrame(f.array())
    plt.figure(1)
    plt.subplot(211)
    plt.plot(df[0], 'b-')
    plt.plot(best_epoch,
             best_iterations,
             color='black',
             marker='.',
             markersize=10)
    plt.xlabel('epoch')
    plt.ylabel('Iterations')
    plt.title('Number of Iterations')

    #mse by runs
    plt.subplot(212)
    plt.plot(df[1], 'r-')
    plt.plot(best_epoch, best_sse, color='black', marker='.', markersize=10)
    plt.xlabel('epoch')
    plt.ylabel('MSE')
    plt.title('MSE')

    plt.subplots_adjust(hspace=.5, wspace=0.35)
    plt.show()
    plt.savefig('training_%d.png' % nHidden)

    #average sse by letter
    plt.clf()
    plt.title('Average SSE By Letter, %d Hidden Nodes, %d Runs' %
              (nHidden, epoch))
    plt.ylabel('SSE')
    plt.xlabel('Letter')
    #ticks = [x+ord('A') for x in range(26)]
    #ticks = [chr(x) for x in ticks]
    #plt.xticks(range(26),ticks)
    ticks = Letters(dataset)
    rticks = range(len(ticks))
    plt.xticks(rticks, ticks)
    plt.margins(0.2)
    #plt.plot(sse)
    y_pos = np.arange(26)
    #last item is mse, don't plot it
    plt.bar(y_pos, a_sse, align='center')
    plt.show()
    plt.savefig('sseavg_%d.png' % nHidden)

    #idea for plot - number of iterations to converge? maybe they all will?
    #find quickest convergence

    #rerun using best_seed, save the hidden outputs
    rc = run_test(dataset,
                  filterMasks,
                  gb_base,
                  tTransferH,
                  alpha,
                  eta,
                  seed=best_seed,
                  nHidden=nHidden,
                  bOutputByClass=True,
                  bHidden=True,
                  bWeights=True)
    #rc = run_test(dataset, tTransferH, alpha, eta=1.5,seed=seed,nHidden=nHidden, bOutputByClass = True, bHidden=True, bWeights=True)
    iterations = rc[0]
    sse = rc[1]
    print '*******'
    print('best iterations = %d' % iterations)
    print('seed = %d' % best_seed)
    print('mse = %.4f' % best_sse)
    print '*******'

    # Plot heatmap (this works)
    filename = "heatmap_%d.csv" % nHidden
    plot_heatmap(dataset, filename)

    #plot SSE
    fsse = datafile.DataFile("sse_%d.csv" % nHidden)
    sse = fsse.read()
    ticks = Letters(dataset)
    rticks = range(len(ticks))

    plt.clf()
    plt.title('MSE By Letter, %d Hidden Nodes' % nHidden)
    plt.ylabel('MSE')
    plt.xlabel('Letter')

    plt.xticks(rticks, ticks)
    #ticks = [chr(x+ord('A')) for x in range(26)]
    #plt.xticks(range(26),ticks)

    plt.margins(0.2)
    #plt.plot(sse)
    y_pos = np.arange(len(ticks))
    #last item is mse, don't plot it
    plt.bar(y_pos, sse, align='center')
    plt.show()
    plt.savefig('ssebyletter_%d.png' % nHidden)
Пример #23
0
def main():

    #parse command line options
    (options, args) = parseOptions()

    import matplotlib
    if not options.show:
        matplotlib.use('Agg')  #needed for saving figures

    import matplotlib.pyplot as plt

    #need a file name
    if len(args) != 1:
        parser.error("need one and only one argument, INPUTFILE")

    fileName = args[0]

    #get base file name
    parts0 = fileName.partition('[')
    baseFileName = parts0[0]
    if (parts0[1] == '' or parts0[2] == ''):
        print __name__ + ":" + main.__name__ + ": error parsing file range from argument \"" + fileName + "\""
        print "  expecting something like BASEFILENAME_t[STARTINDEX-ENDINDEX] ."
        return False

    #get interation range
    parts1 = parts0[2].partition(']')
    if (parts1[1] == ''):
        print __name__ + ":" + main.__name__ + ": error parsing file range from argument \"" + fileName + "\""
        print "  expecting something like BASEFILENAME_t[STARTINDEX-ENDINDEX] ."
        return False
    parts2 = parts1[0].partition('-')
    if (parts2[1] == '' or parts2[2] == ''):
        print __name__ + ":" + main.__name__ + ": error parsing file range from argument \"" + fileName + "\""
        print "  expecting something like BASEFILENAME_t[STARTINDEX-ENDINDEX] ."
        return False
    start = int(parts2[0])
    if parts2[2] == "*":
        end = sys.maxint
    else:
        end = int(parts2[2])

    #make sure that all the combined binary files in range have profiles
    make_profiles.make_profiles(options.keep, fileName, options.remake, False)

    #get and sort files
    extension = "_pro.txt"
    filesExistSlices = glob.glob(baseFileName + "*" + extension)
    files = []
    for file in filesExistSlices:
        intOfFile = int(file[len(baseFileName):len(file) - len(extension)])
        if intOfFile >= start and intOfFile < end:
            files.append(file)

    files.sort()

    if len(files) == 0:
        print "no files matching \"" + baseFileName + "\" in the range specified"
        return False

    #lists to hold data
    times = []
    phase = []
    vThetaMaxs = []
    vThetaMaxs_j = []
    vThetaMaxs_i = []

    vU_U0Maxs = []
    maxTempVariations = []
    surfaceGridVelocities = []

    radialProfile = datafile.DataFile()
    for file in files:

        print "reading in profile \"" + file + "\" ..."

        #read in profile
        radialProfile.readFile(file)

        #get time
        parts = radialProfile.sHeader.split()
        times.append(float(parts[1]))
        if options.period != None:
            phase.append((float(parts[1]) - times[0]) / options.period)

        #get maximum theta velocity of entire model
        vThetaMaxTemp = math.fabs(
            radialProfile.fColumnValues[0][22])  #used first value
        vThetaMaxs_jTemp = -1
        vThetaMaxs_iTemp = -1
        for i in range(len(radialProfile.fColumnValues) - 1):

            #check v_max
            if math.fabs(radialProfile.fColumnValues[i][22]) > vThetaMaxTemp:
                vThetaMaxTemp = math.fabs(radialProfile.fColumnValues[i][22])
                vThetaMaxs_iTemp = i
                vThetaMaxs_jTemp = radialProfile.fColumnValues[i][23]

            #check v_min
            if math.fabs(radialProfile.fColumnValues[i][25]) > vThetaMaxTemp:
                vThetaMaxTemp = math.fabs(radialProfile.fColumnValues[i][25])
                vThetaMaxs_iTemp = i
                vThetaMaxs_jTemp = radialProfile.fColumnValues[i][26]

        if options.unscaleVel:
            vThetaMaxs.append(vThetaMaxTemp)
        else:
            vThetaMaxs.append(vThetaMaxTemp / 1.0e5)
        vThetaMaxs_j.append(vThetaMaxs_jTemp)
        vThetaMaxs_i.append(vThetaMaxs_iTemp)

        #get maximum U-U0 velocity of entire model
        vU_U0MaxsTemp = math.fabs(
            radialProfile.fColumnValues[0][14] -
            radialProfile.fColumnValues[0][20])  #used first value
        for i in range(len(radialProfile.fColumnValues) - 1):
            vU_U0MaxsTemp1 = max(
                math.fabs(radialProfile.fColumnValues[i][14] -
                          radialProfile.fColumnValues[i][20]),
                math.fabs(radialProfile.fColumnValues[i][17] -
                          radialProfile.fColumnValues[i][20]))
            vU_U0MaxsTemp = max(vU_U0MaxsTemp, vU_U0MaxsTemp1)
        if options.unscaleVel:
            vU_U0Maxs.append(vU_U0MaxsTemp)
        else:
            vU_U0Maxs.append(vU_U0MaxsTemp / 1.0e5)

        #get maximum temperature vairation of entire model
        tempVariance = (radialProfile.fColumnValues[0][46] -
                        radialProfile.fColumnValues[0][49])
        #/radialProfile.fColumnValues[0][25]
        for i in range(len(radialProfile.fColumnValues) - 1):

            tempVariance2 = (radialProfile.fColumnValues[i][46] -
                             radialProfile.fColumnValues[i][49])
            #/radialProfile.fColumnValues[i][25]

            tempVariance = max(tempVariance, tempVariance2)
        maxTempVariations.append(tempVariance)

        #get surface grid velocity
        if options.unscaleVel:
            surfaceGridVelocities.append(
                radialProfile.fColumnValues[len(radialProfile.fColumnValues) -
                                            4][20])
        else:
            surfaceGridVelocities.append(
                radialProfile.fColumnValues[len(radialProfile.fColumnValues) -
                                            4][20] / 1.0e5)
    '''
  #write to a file
  f=open('variation.txt','w')
  f.write("time[s] MaxTheta[cm/s] MaxRelHorTempVar U0[cm/s]\n")
  for i in range(len(times)):
    line= str(times[i])+" "+str(vThetaMaxs[i])+" "+str(maxTempVariations[i])+" "+str(surfaceGridVelocities[i])+"\n"
    f.write(line)
  f.close()'''

    #plt.subplots_adjust(hspace=0.001)
    #fig=plt.figure(figsize=(6,8))
    #ax=plt.subplot(1,1,1)
    #ax.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(nbins=6,steps=None,trim=True,integer=False,symmetric=False,prune='both'))
    #ax.yaxis.major.formatter.set_powerlimits((0,0))
    #y_max=max(watchZones[1][index])
    #y_min=min(watchZones[1][index])
    #y_max_abs=max([abs(y_max),abs(y_min)])
    #exponent=int(math.log10(y_max_abs))
    #if exponent>5:
    #  exponent=5
    #scale=10.0**float(-1.0*exponent)
    #y1=list(y*scale for y in watchZones[1][index])
    #line1=plt.plot(phase1,y1,'-',label="period "+str(period1))
    #line2=plt.plot(phase2,y1,'--',label="period "+str(period2))
    #line3=plt.plot(phase3,y1,':',label="period "+str(period3))
    #plt.title(options.title)
    #plt.annotate(shells[1],(0.88,0.2),xycoords='axes fraction',va="top",ha="center")
    #plt.setp(ax.get_xticklabels(), visible=False)
    #plt.ylabel(" 1e"+str(exponent))

    print "generating plot ..."
    plotString = '-'
    if options.noLines:
        plotString = 'o'
    if options.points and not options.noLines:
        plotString = plotString + 'o'
    pointSize = 3
    fig = plt.figure(figsize=(13, 8))
    fig.subplots_adjust(hspace=0.0000)

    #plot surface velocity
    ax1 = plt.subplot(4, 1, 1)
    if not options.noGrid:
        ax1.grid()
    if options.period != None:
        plt.plot(phase,
                 surfaceGridVelocities,
                 plotString,
                 markersize=pointSize)
    else:
        plt.plot(times,
                 surfaceGridVelocities,
                 plotString,
                 markersize=pointSize)
    if options.unscaleVel:
        ax1.set_ylabel("u_0 [cm/s]")
    else:
        ax1.set_ylabel("u_0 [km/s]")

    #plot v_max
    ax2 = plt.subplot(4, 1, 2)
    if not options.noGrid:
        ax2.grid()
    if options.period != None:
        plt.plot(phase, vThetaMaxs, plotString, markersize=pointSize)
    else:
        plt.plot(times, vThetaMaxs, plotString, markersize=pointSize)
    if options.unscaleVel:
        ax2.set_ylabel("|v| max [cm/s]")
    else:
        ax2.set_ylabel("|v| max [km/s]")

    #plot i of v_max
    ax3 = plt.subplot(4, 1, 3)
    if not options.noGrid:
        ax3.grid()
    if options.period != None:
        plt.plot(phase, vThetaMaxs_i, plotString, markersize=pointSize)
    else:
        plt.plot(times, vThetaMaxs_i, plotString, markersize=pointSize)
    ax3.set_ylabel("i of v_max")

    #plot j of v_max
    ax4 = plt.subplot(4, 1, 4)
    if not options.noGrid:
        ax4.grid()
    if options.period != None:
        plt.plot(phase, vThetaMaxs_j, plotString, markersize=pointSize)
    else:
        plt.plot(times, vThetaMaxs_j, plotString, markersize=pointSize)
    if options.unscaleVel:
        ax4.set_ylabel("j of v_max")
    else:
        ax4.set_ylabel("j of v_max")
    if options.period != None:
        ax4.set_xlabel("phase")
    else:
        ax4.set_xlabel("t [s]")

    ax1.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(prune='both'))
    ax2.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(prune='both'))
    ax3.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(prune='both'))
    ax4.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(prune='both'))
    xticklables = ax1.get_xticklabels() + ax2.get_xticklabels(
    ) + ax3.get_xticklabels()
    plt.setp(xticklables, visible=False)
    ax1.set_title(options.title)
    ax1.set_xlim(options.xMin, options.xMax)
    ax2.set_xlim(options.xMin, options.xMax)
    ax3.set_xlim(options.xMin, options.xMax)
    ax4.set_xlim(options.xMin, options.xMax)
    if options.show:
        plt.show()
    else:
        print __name__ + ":" + main.__name__ + ": saving figure to file \"" + options.outputFile + "." + options.format
        fig.savefig(options.outputFile + "." + options.format,
                    format=options.format,
                    transparent=False)  #save to file