Advertisement
AmCholadawan

AreaCovered_lastVersion

Dec 29th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. import numpy as np
  3. import os
  4. from glob import glob
  5. import csv
  6. import matplotlib.cm as cm
  7. from collections import OrderedDict
  8. import ast
  9. import re
  10. import fnmatch
  11.  
  12. # data decription for plot titles
  13. dataSetName = "ScavengeTroph2017-05-28_"
  14.  
  15. plot_title = "UT60LT30"
  16. '''
  17. # the path to the data from where the current file is saved
  18. dirname = os.path.abspath(os.path.join(
  19.    os.path.dirname( __file__ ),
  20.    '../../../..',
  21.    "self_energy_robot"
  22.    + "/" + "steptaken_infinitefood"
  23.    ))
  24.  
  25. path = dirname + "/" + dataSetName
  26. #print(path)
  27. extension   = "*Summary.csv"
  28. files = []
  29.  
  30. for dir,_,_ in os.walk(path):
  31.    files.extend(glob(os.path.join(dir,extension)))
  32. '''
  33.  
  34. data_to_import = ['n_food_cells_initial',
  35.                 'total_area_covered',
  36.                 'n_bots']
  37.  
  38. # ->
  39. # 1 for Default avg area plot
  40. # 2 for Normalised area plot (Avg / (food/bots))
  41. drawY = 2
  42.  
  43. # ->
  44. # 0 don't draw error bars
  45. # 1 draw error bars
  46. drawError = 1
  47.  
  48. data = {}
  49.  
  50. for _data in data_to_import:
  51.     data[_data] = []
  52.    
  53. #UTfolder = ['No_Trophallaxis','Trophallaxis_generous', 'Th50', 'Th70', 'Th90']
  54. #UTfolder = ['UT90LT40', 'UT90LT40_more']
  55.  
  56.  
  57. path = []
  58. #testNumber = 'Troph_Th_average'
  59. testNumber = 'test9'
  60.  
  61.  
  62. UTfolder = ['UT60LT30']
  63. #UTfolder = ['No_Trophallaxis','Trophallaxis_generous', 'Th50','Th70','Th90',]
  64.  
  65. trophFolder = 'test9FiniteTroph'
  66. #cwd='/Users/ammacpro1/AnacondaProjects/self_energy_robot/'+testNumber+'/'+UTfolder+'/'+trophFolder
  67. cwd='/Users/ammacpro1/simulation_results/self_energy_robot/'+testNumber
  68. #cwd='/Users/ammacpro1/AnacondaProjects/result_increase_initial_energy/'+testNumber
  69. #cwd = os.getcwd()
  70. for f in UTfolder:
  71.     path.append(cwd+'/'+f+'/'+trophFolder+'/Bot*/*Summary.csv')
  72. #init_food_per_groups = [32, 40, 48, 56, 64]
  73. #
  74.  
  75. fig, ax = plt.subplots()
  76. def analyseFolders(folder, curr_p, drawY, drawError):
  77.     files = glob(folder)
  78.     for file in files:
  79.         with open(file, 'rt') as f:
  80.             reader = csv.reader(f, delimiter=',')
  81.             for row in reader:
  82.                 for _data in data_to_import:
  83.                     if row[0] in [_data]:
  84.                         data[_data].append(row[1])
  85.    
  86.     sorted_data = dict.fromkeys(data.keys(),[])
  87.     data_to_plot = {}
  88.    
  89.     keys = [key for key, value in data.items()]
  90.     #print(keys)
  91.    
  92.     foodPatchSize = sorted(list(set(data["n_food_cells_initial"])), key=int)
  93.     swarmSize =     sorted(list(set(data["n_bots"])), key=int)
  94.    
  95.     for PSize in foodPatchSize:
  96.         for SSize in swarmSize:
  97.             for _data in [key for key, value in data.items()]:
  98.                 #print(PSize, _data)
  99.                 data_to_plot[_data + "_food" + PSize + "_bots" + SSize] = \
  100.                         [float(i)
  101.                          for i, j, k in zip(
  102.                             data[_data],
  103.                             data["n_bots"],
  104.                             data["n_food_cells_initial"])
  105.                          if j == SSize and k == PSize]
  106.    
  107.     #print(data_to_plot)
  108.     #colors = iter(cm.Greys(np.linspace(0.4, 1, len(foodPatchSize))))
  109.     colors = cm.rainbow(np.linspace(0.4, 1, len(UTfolder)))
  110.     widths = np.linspace(2, 0.5, len(UTfolder))
  111.    
  112.     #fig, ax = plt.subplots()
  113.     data_dict = {k:v for (k,v) in data_to_plot.items() if _data in k}
  114.    
  115.     Markers = ['+','+','o','o','o','^','o','p','s']
  116.     LineStyles = ['--','--','-','-','-','-.',':','--']
  117.     #MarkerIndex = 0;
  118.    
  119.     data_mean = []
  120.     data_sd = []
  121.    
  122.     for PSize in foodPatchSize:
  123.         c = colors[curr_p]
  124.         w = widths[curr_p]
  125.         food_dict = {k: v for (k, v) in data_dict.items() if PSize in k}
  126.         vals = []
  127.         sd = []
  128.         vals = np.mean(data_to_plot["total_area_covered_food" + PSize + "_bots" + SSize])
  129.         if (drawY == 2):
  130.             vals /= (int(PSize)/int(SSize))
  131.         if (drawError):
  132.             sd = np.std(data_to_plot["total_area_covered_food" + PSize + "_bots" + SSize])
  133.         else:
  134.             sd = 0
  135.         data_mean.append(vals)
  136.         data_sd.append(sd)
  137.    
  138.     foodPatchSize = [int(x) for x in foodPatchSize]
  139.     ax.errorbar(foodPatchSize,
  140.                 data_mean,
  141.                 data_sd,
  142.                 #sd, fmt = 'o',
  143.                 capsize = 2,
  144.                 linestyle = LineStyles[curr_p % len(LineStyles)],
  145.                 #linestyle = "None",
  146.                 linewidth = w,
  147.                 marker = Markers[curr_p % len(Markers)],
  148.                 #marker='o',
  149.                 color=c,
  150.                 #label="Number of food patches = " + str(PSize))
  151.                 label=UTfolder[curr_p])
  152.    
  153.     ax.legend(frameon=False, loc="best", prop={'size': 10})
  154.     ax.set_xlabel("Food Boundary Offset")
  155.     if (drawY == 1):
  156.         ax.set_ylabel("Average Area Covered")
  157.     elif (drawY == 2):
  158.         ax.set_ylabel("Average Covered/(food/bots)")
  159.    
  160.     ax.set_title(plot_title)
  161.    
  162.     #plotname = path + '/' + plot_title + "_" + _data
  163.     #fig.savefig(plotname + '.pdf', orientation='landscape')
  164.     #fig.savefig(plotname + '.png', orientation='landscape')
  165.  
  166.    
  167. curr_p = 0
  168. for p in path:
  169.     analyseFolders(p, curr_p, drawY, drawError)
  170.     curr_p += 1
  171.  
  172. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement