Advertisement
AmCholadawan

test

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