Advertisement
AmCholadawan

one flat

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