Advertisement
AmCholadawan

Area_Cover_along_probability

Mar 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 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.  
  13. plot_title = "Area Covered 225 Robots Finite Food"
  14.  
  15. #list of string
  16. data_to_import = ['total_area_covered',
  17.                 'n_bots']
  18.  
  19. # ->
  20. # 0 don't draw error bars
  21. # 1 draw error bars
  22. drawError = 1
  23.  
  24. #nitialises an empty dictionary
  25. data = {}
  26. data_to_plot = {}
  27.  
  28. for _data in data_to_import:
  29.     data[_data] = []
  30.  
  31. path = []
  32.  
  33. nbots_array = ['1', '25', '81', '169', '225']
  34. levyP_array = ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100']
  35.  
  36. testNumber = 'simulation_results'
  37. cwd = '/Users/ammacpro1/simulation_results/self_energy_robot/' + testNumber
  38.  
  39. ##cwd = os.getcwd()
  40. ##cwd = cwd + "/" + testNumber
  41.  
  42. UTprefix = 'Levywalklike_P'
  43.  
  44. #UTfolder = ['No_Trophallaxis','Trophallaxis_generous', 'Th50','Th70','Th90',]
  45. UTfolder = [UTprefix + p for p in levyP_array]
  46.  
  47. trophFolder = 'FiniteFood'
  48.  
  49. for f in UTfolder:
  50.     path.append(cwd + '/' + f + '/' + trophFolder + '/Bot*/*Summary.csv')
  51.  
  52. def analyseFolders(folder, curr_p):
  53.     files = glob(folder)
  54.     for file in files:
  55.         with open(file, 'rt') as f:
  56.             reader = csv.reader(f, delimiter=',')
  57.             for row in reader:
  58.                 for key in data_to_import:
  59.                     if row[0] in [key]:
  60.                         data[key].append(row[1])
  61.    
  62.     sorted_data = dict.fromkeys(data.keys(),[])
  63.    
  64.     keys = [key for key, value in data.items()]
  65.     #print(keys)
  66.    
  67.     swarmSize = sorted(list(set(data["n_bots"])), key=int)
  68.    
  69.     for SSize in swarmSize:
  70.         for _data in [key for key, value in data.items()]:
  71.             data_to_plot[_data + "_levyP" + levyP_array[curr_p] + "_bots" + SSize] = \
  72.                     [float(i)
  73.                      for i, j in zip(
  74.                         data[_data],
  75.                         data["n_bots"])
  76.                      if j == SSize]
  77.  
  78. fig, ax = plt.subplots()
  79. colors = cm.rainbow(np.linspace(0.4, 1, len(nbots_array)))
  80. widths = np.linspace(2, 0.5, len(nbots_array))
  81. Markers = ['+','+','o','o','o','^','o','p','s']
  82. LineStyles = ['--','--','-','-','-','-.',':','--']
  83.  
  84. def plotData(b, drawError):
  85.     #print(data_to_plot)
  86.    
  87.     data_mean = []
  88.     data_sd = []
  89.    
  90.     for PSize in levyP_array:
  91.         c = colors[curr_b]
  92.         #w = widths[curr_b]
  93.         w = 2
  94.         vals = []
  95.         sd = []
  96.         vals = np.mean(data_to_plot["total_area_covered_levyP" + PSize + "_bots" + b])
  97.         if (drawError):
  98.             sd = np.std(data_to_plot["total_area_covered_levyP" + PSize + "_bots" + b])
  99.         else:
  100.             sd = 0
  101.         data_mean.append(vals)
  102.         data_sd.append(sd)
  103.    
  104.     levyP_bins = [int(x) for x in levyP_array]
  105.     ax.errorbar(levyP_bins,
  106.                 data_mean,
  107.                 data_sd,
  108.                 #sd, fmt = 'o',
  109.                 capsize = 2,
  110.                 linestyle = LineStyles[curr_b % len(LineStyles)],
  111.                 #linestyle = "None",
  112.                 linewidth = w,
  113.                 marker = Markers[curr_b % len(Markers)],
  114.                 #marker='o',
  115.                 color = c,
  116.                 #label = "Number of food patches = " + str(PSize))
  117.                 label = nbots_array[curr_b] + " Robots")
  118.    
  119.     ax.legend(frameon=False, loc="best", prop={'size': 10})
  120.     ax.set_xlabel("% Probability")
  121.     #if (drawY == 1):
  122.     ax.set_ylabel("Average Area Covered")
  123.    
  124.     ax.set_title(plot_title)
  125.    
  126.     #plotname = path + '/' + plot_title + "_" + _data
  127.     #fig.savefig(plotname + '.pdf', orientation='landscape')
  128.     #fig.savefig(plotname + '.png', orientation='landscape')
  129.  
  130.  
  131. curr_p = 0   #curr_path = current path
  132. for p in path: #p is the full path depending on which f is currently being used, f is a part of full path
  133.     analyseFolders(p, curr_p)
  134.     curr_p += 1
  135. curr_b = 0  #current bots group
  136. for b in nbots_array:
  137.     plotData(b, drawError)
  138.     curr_b += 1
  139.  
  140. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement