Advertisement
AmCholadawan

off_plot

Apr 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.31 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. import matplotlib.cm as cm
  3. import numpy as np
  4. import os
  5. from glob import glob
  6. import csv
  7. from collections import OrderedDict
  8. import ast
  9. import re
  10. import fnmatch
  11.  
  12.  
  13.  
  14. #list of string
  15. data_to_import = ['total_steps_taken']
  16. #data_to_import = ['total_area_covered']
  17.  
  18. # ->
  19. # 0 don't draw error bars
  20. # 1 draw error bars
  21. drawError = 1
  22.  
  23. path = []
  24.  
  25. eff_array = ['0','40', '50','60','70', '80', '90', '100']
  26.  
  27. testNumber = 'FiniteFood'
  28. cwd = '/Users/ammacpro1/AnacondaProjects/Trophallaxis_Model_for_journal_ALR_2500_maxfood100_checkboardmap1_100runs/Simulation_results/' + testNumber
  29.  
  30. ##cwd = os.getcwd()
  31. ##cwd = cwd + "/" + testNumber
  32.  
  33. UTfolder_reference =  ['Random_No_Trophallaxis']  #'Levyrandom_No_Trophallaxis'
  34.  
  35. UTprefix = ['Random_Trophallaxis_Generous_eff',
  36.             'Random_Trophallaxis_UT70LT20average_eff',
  37.             'Levywalklike_Trophallaxis_Generous_eff',
  38.             'Levywalklike_Trophallaxis_UT70LT20average_eff',
  39.             #'Levyrandom_Trophallaxis_Generous_eff',
  40.             #'Levyrandom_Trophallaxis_UT70LT20average_eff',
  41.            ]
  42. UTprefixLegend = ['Random_Trophallaxis_Generous',
  43.             'Random_Trophallaxis_UT70LT20average',
  44.             'Levywalklike_Trophallaxis_Generous',
  45.             'Levywalklike_Trophallaxis_UT70LT20average',
  46.             #'Levyrandom_Trophallaxis_Generous',
  47.             #'Levyrandom_Trophallaxis_UT70LT20average',
  48.            ]
  49.  
  50. trophFolder = 'Bots225'
  51. plot_title = "Total Step Taken_" +str(trophFolder) + '_' +str(testNumber)
  52. #UTfolder = ['No_Trophallaxis','Trophallaxis_generous', 'Th50','Th70','Th90',]
  53. UTfolderP = []
  54. for p in UTprefix:
  55.     for n in range(len(eff_array)-1):
  56.         UTfolderP.append(p + eff_array[n+1])
  57. UTfolder = UTfolder_reference
  58. for pf in UTfolderP:
  59.     UTfolder.append(pf)
  60.  
  61. UTfolderP2 = UTfolder_reference
  62. for pf in UTprefixLegend:
  63.     UTfolderP2.append(pf)
  64.  
  65. for f in UTfolder:
  66.     path.append(cwd + '/' + f + '/' + trophFolder + '/Bot*/*Summary.csv')
  67. data_to_plot = {}
  68. def analyseFolders(folder, curr_p):
  69.     #nitialises an empty dictionary
  70.     data = {}
  71.     #data_to_plot = {}
  72.    
  73.     for _data in data_to_import:
  74.         data[_data] = []
  75.  
  76.     files = glob(folder)
  77.     for file in files:
  78.         with open(file, 'rt') as f:
  79.             reader = csv.reader(f, delimiter=',')
  80.             for row in reader:
  81.                 for key in data_to_import:
  82.                     if row[0] in [key]:
  83.                         data[key].append(row[1])
  84.    
  85.     sorted_data = dict.fromkeys(data.keys(),[])
  86.    
  87.     keys = [key for key, value in data.items()]
  88.     #print(keys)
  89.     #print('curr_p', curr_p)
  90.     for _data in [key for key, value in data.items()]:
  91.         #data_to_plot[_data + "_f" + UTfolderP2[curr_pref] + "_eff" + eff_array[curr_p]] = \
  92.         data_to_plot[_data + "_eff" + eff_array[curr_p]] = \
  93.                                                         [float(i) for i in data[_data]]
  94.     keys_plot = [key for key, value in data_to_plot.items()]
  95.     #print('keys_plot', keys_plot)
  96.    
  97. fig, ax = plt.subplots()
  98. colors = cm.rainbow(np.linspace(0.4, 1, len(UTfolderP2)))
  99. widths = np.linspace(2, 0.5, len(UTfolderP2))
  100. #Markers = ['+','+','o','o','o','^','o','p','s']
  101. #LineStyles = ['--','--','-','-','-','-.',':','--']
  102. Markers = ['+','+','+','o','o','s','s','^','s','s','s','o','^','o','p','s']
  103. LineStyles = ['-','-','--','-','--','-','--','-','--',':',':','-.','-.','-','-.',':','--']
  104.  
  105. def plotData():
  106.     #print(data_to_plot)
  107.     #print('len:', len(data_to_plot))
  108.     print('curr_pref:', curr_pref)
  109.    
  110.     data_mean = []
  111.     data_sd = []
  112.      
  113.     if curr_pref < len(UTfolder_reference):
  114.         eff_cycle = eff_array[curr_p]
  115.     else:
  116.         eff_cycle = eff_array[1:]
  117.         #eff_cycle = eff_array[len(UTfolder_reference):]
  118.     print('eff_array:',eff_array)
  119.     print('eff_cycle:',eff_cycle)    
  120.     for effSize in eff_cycle:
  121.         print('effSize:',effSize)
  122.         c = colors[curr_pref]
  123.         #w = widths[curr_b]
  124.         w = 2
  125.         vals = []
  126.         sd = []
  127.         #tmpArray = data_to_plot["total_area_covered_eff" + effSize]
  128.         tmpArray = data_to_plot["total_steps_taken_eff" + effSize]
  129.         vals = np.mean(tmpArray)
  130.         if (drawError):
  131.             sd = np.std(tmpArray)
  132.         else:
  133.             sd = 0
  134.         if effSize == '0':
  135.             for i in range(len(eff_array)-1):
  136.                 data_mean.append(vals)
  137.                 data_sd.append(sd)
  138.         else:
  139.             data_mean.append(vals)
  140.             data_sd.append(sd)
  141.    
  142.     eff_bins = [int(x) for x in eff_array]
  143.     eff_bins = eff_bins[1:]
  144.     ax.errorbar(eff_bins,
  145.                 data_mean,
  146.                 data_sd,
  147.                 #sd, fmt = 'o',
  148.                 capsize = 2,
  149.                 linestyle = LineStyles[curr_pref % len(LineStyles)],
  150.                 #linestyle = "None",
  151.                 linewidth = w,
  152.                 marker = Markers[curr_pref % len(Markers)],
  153.                 #marker='o',
  154.                 color = c,
  155.                 #label = "Number of food patches = " + str(effSize))
  156.                 label = UTfolderP2[curr_pref])
  157.    
  158.     ax.legend(frameon=False, loc="best", prop={'size': 10})
  159.     ax.set_xlabel("Efficiency of Energy transfer (%)")
  160.     ax.set_ylabel("Average Step Taken")
  161.    
  162.     ax.set_title(plot_title)
  163.    
  164.     #plotname = path + '/' + plot_title + "_" + _data
  165.     #fig.savefig(plotname + '.pdf', orientation='landscape')
  166.     #fig.savefig(plotname + '.png', orientation='landscape')
  167.  
  168.     plt.show(block=False)
  169.  
  170. ########## main
  171.  
  172. curr_p = 0   #curr_path = current path
  173. curr_pref = 0
  174.  
  175. for f in range(len(UTfolder_reference)):
  176.     data_to_plot = {}
  177.     analyseFolders(path[curr_p], curr_p)
  178.     plotData()
  179.     curr_p += 1
  180.     curr_pref += 1
  181.    
  182.  
  183. analyseFolders(path[curr_p], curr_p)
  184. plotData()
  185.  
  186. curr_pref += 1
  187. for pp in UTprefix:
  188.     UTfolderP = []
  189.     for n in range(len(eff_array)-1):
  190.         UTfolderP.append(pp + eff_array[n+1])
  191.     path = []
  192.     for f in UTfolderP:
  193.         path.append(cwd + '/' + f + '/' + trophFolder + '/Bot*/*Summary.csv')
  194.     #print('path', path)
  195.     #curr_p = 1
  196.     for p in path: #p is the full path depending on which f is currently being used, f is a part of full path
  197.         analyseFolders(p, curr_p)
  198.         curr_p += 1
  199.     plotData()
  200.     curr_pref += 1
  201.  
  202.  
  203. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement