/ model / plot.py
plot.py
  1  #!/usr/bin/env python3
  2  project_name = "DefiOptions:DefiOptions-core"
  3  """
  4  plot.py: plot log of % system behavior
  5  """ % (project_name)
  6  
  7  import matplotlib.pyplot as plt
  8  
  9  def main():
 10      """
 11      Main function: plot the simulation.
 12      """
 13      
 14      # This will hold the headings for columns
 15      headings = []
 16      # This will hold each column, as a list
 17      columns = []
 18  
 19      log = open("./chain/log.tsv")
 20      for line in log:
 21          line = line.strip()
 22          if line == '':
 23              continue
 24          parts = line.split('\t')
 25          if parts[0].startswith('#'):
 26              # This is a header
 27              headings = parts
 28              headings[0] = headings[0][1:].strip()
 29          else:
 30              # This is data. Assume all columns are the same length
 31              for i, item in enumerate(parts):
 32                  if len(columns) <= i:
 33                      columns.append([])
 34                  columns[i].append(float(item))
 35                  
 36      # Now plot
 37      
 38      # Find what to plot against
 39      x_heading = "block"
 40      x_column_number = headings.index(x_heading)
 41      if x_column_number == -1:
 42          raise RuntimeError("No column: " + x_heading)
 43          
 44      fig, axes = plt.subplots(len(columns)+1, 1, sharex=True)
 45      fig.suptitle('%s Simulation Results' % (project_name))
 46  
 47      axis_cursor = 0
 48          
 49      for column_number in range(len(columns)):
 50  
 51          try:
 52              if headings[column_number] == 'epoch':
 53                  continue
 54              
 55              if column_number == x_column_number:
 56                  # Don't plot against self
 57                  continue
 58                  
 59              # Plot this column against the designated x
 60              ax = axes[axis_cursor]
 61              ax.plot(columns[x_column_number], columns[column_number], '-')
 62              ax.set_xlabel(headings[x_column_number])
 63              ax.set_ylabel(headings[column_number])
 64  
 65              #print(headings[column_number])
 66  
 67              if 'total SB' in headings[column_number]:
 68                  # plot diff 'total credit balance' 'total stablecoin balanace'
 69                  axis_cursor += 1
 70  
 71  
 72                  # Plot this column against the designated x
 73                  ax = axes[axis_cursor]
 74                  ncoldata = [(cB / 10**18) - (sB / 10**6) for (cB,sB) in zip(columns[column_number - 1], columns[column_number])]
 75                  ax.plot(columns[x_column_number], ncoldata, '-')
 76                  ax.set_xlabel(headings[x_column_number])
 77                  ax.set_ylabel('dCB_SB')
 78  
 79  
 80              if 'holding' in headings[column_number]:
 81                  # plot diff ('holding' - 'written') - (holding prev - written prev)
 82                  axis_cursor += 1
 83  
 84  
 85                  # Plot this column against the designated x
 86                  ax = axes[axis_cursor]
 87                  ncoldata = [(h - w) / 10.**18 for (h,w) in zip(columns[column_number], columns[column_number- 1])]
 88                  ncoldata1 = []
 89                  for nidx, x in enumerate(ncoldata):
 90                      if nidx > 0:
 91                          ncoldata1.append(x - ncoldata[nidx-1])
 92                      else:
 93                          ncoldata1.append(x)
 94  
 95                  ax.plot(columns[x_column_number], ncoldata1, '-')
 96                  ax.set_xlabel(headings[x_column_number])
 97                  ax.set_ylabel('# liquidated')
 98              
 99              # Make the next plot on the next axes
100              axis_cursor += 1
101          except Exception as inst:
102              print inst
103              pass
104              
105      # Show all the plots
106      plt.show()
107      
108  if __name__ == "__main__":
109      main()