def display_fruits(fruits=None, index=None, color='#FFFFFF', dim='', show=True, block=True): """ displays fruit trees Keyword Arguments: index {int} -- index of figure. If it is None no figure is created. {Default: None} fruits {Tree / List[Trees]} -- fruit/fruits to be displayed color {'hex' / [int,int,int]} -- color of fruit trees. {Default: '#FFFFFF'}] show {boolean} -- whether to immediately show the figure or not. {Default: True} block {boolean} -- whether the figure blocks the excution. {Default: True} dim {string} -- dimension proportionate to radius (default) or score ('score'). {Default: ''} Returns: {int} -- number of drawn figure. None if figure has been displayed and blocks """ # reset block plt.ioff() # check fruits if fruits is None: fruits = dp.Fruits() # create figure if requested if index is not None: plt.figure(index) # convert color to hex if not isinstance(color, str): color = rgb2hex(color[0], color[1], color[2]) # display fruits if fruits is not None: ax = plt.gca() if isinstance(fruits, list): for f in fruits: if dim == 'score': c = plt.Circle((f.x, f.y), radius=f.score, color=color) else: c = plt.Circle((f.x, f.y), radius=f.radius, color=color) ax.add_patch(c) else: if dim == 'score': c = plt.Circle((fruits.x, fruits.y), radius=fruits.score, color=color) else: c = plt.Circle((fruits.x, fruits.y), radius=fruits.radius, color=color) ax.add_patch(c) plt.axis('scaled') plt.draw() # optional if show: showfig(block) # return current figure number if not block: return plt.gcf().number else: return None
def readpath(filename, delimiter=',', quotechar='"', minlength=3, species=None): """ Returns datepath read from csv Arguments: filename {string} -- file name Keyword Arguments: skipheader boolean} -- if True return eader as first row (if present) delimiter {char} -- character used to separate fields. {Default ','} quotechar {char} -- character used to quote fields containing special characters, such as the delimiter \ or quotechar, or which contain new-line characters. {Default: '"'} minlength {int} -- minimum length for a path to be considered valid. {Default: 900} species {List[string]} -- list of accepeted species. If None take all. {Default: None} Returns: row """ # force minimum minlength to 3 if minlength < 3: minlength = 3 # init variables path = [] currmonkey = -1 currdate = None invalidcount = 0 # loop on every point for row in readcsvrow(filename, has_header=True, skipheader=True, delimiter=delimiter, quotechar=quotechar): try: # parse row specie = row[23] monkey = int(row[24]) [x, y] = _converttoXY(float(row[3]), float(row[4])) dtm = datetime.strptime(row[2], '%Y-%m-%d %H:%M:%S.%f') new = geo.Coordinates(x, y, 0, dtm) new.resetz() except Exception as e: # if row parsing failrs jump to next print('invalid row ' + str(invalidcount) + ': ' + str(row)) invalidcount += 1 continue if species is not None and specie not in species: continue # case same path if monkey == currmonkey and dtm.date() == currdate: path.append(new) else: # case new path # create and return new datepath if len(path) >= minlength: yield dtp.datepath(path, date=currdate, monkey=currmonkey, fruits=dp.Fruits()) # prepare for new path currdate = dtm.date() currmonkey = monkey path = [new] print('Overall found ' + str(invalidcount) + 'invalid rows') # return last hanging path if len(path) > MIN_VALID_LEN: return dtp.datepath(path, date=currdate, monkey=currmonkey, fruits=dp.Fruits())
import datepath as dtp import display as disp import monkeyexceptions as me import monkeyconstants as mc import geometry as geo import simulation as sim PATH_TO_GEN = 500 * 2 col = [255, 255, 255] dtp.datepath.Island = dp.Island() geo.Coordinates.Island = dp.Island() tries = 0 # Choose trees locs = np.random.randint(0, len(dp.Fruits()), 2) locs = [1000, 600] locs = [1000, 0] orig = dp.Fruits()[locs[0]] dest = dp.Fruits()[locs[1]] fruits = dp.Fruits() fruits.remove(orig) skipmemory = True # do not compute memory paths skipperception = False # do not compute perception paths # Creat path tot = 0 while tot < PATH_TO_GEN: # even loops while tot % 2 == 0 and tries < mc.MAX_TRIES:
dtp.datepath.Island = dp.Island() geo.Coordinates.Island = dp.Island() steps = int(NUM_TRIES / STEPSIZE) # RANDOM GENERATOR and DISPLAY rows = [] for j in range(START,steps): basemonkey = STEPSIZE * j i = 0 while i < STEPSIZE: try: rdm = np.random.uniform(0,1) start = time.process_time() if rdm < 0.5: print("working on memory trial " + str(i+basemonkey)) path = dtp.datepath.RandomMemory(dp.Fruits(), monkey=i+basemonkey, sortmethod=mc.SortScore) title = '{0:02d} - Memory'.format(i+basemonkey) else: print("working on view trial " + str(i+basemonkey)) path = dtp.datepath.RandomView(dp.Fruits(), monkey=i+basemonkey) title = '{0:02d} - Perception'.format(i+basemonkey) print("\tlength:" + str(path.length)) finish = time.process_time() # disp.display_datepath(path, index=i+basemonkey, title=title, fruit_dim='', show=True, block=True) row = path.getDataframeRow() rows.append(row) i += 1 except Exception as e: finish = time.process_time() print("\n") print("Error on trial " + str(i) + " - COMPUTATION ERROR - computation required " + str(finish-start) + "s")
rows = [] # read files availabe files = os.listdir(mc.REAL_PATH) files = files[:-2] # loop on files for i in range(START, len(files)): # Read file f = files[i] print("working on file #{0} - {1}".format(total, f)) splits = (f.split('.')[0]).split('_') monkey = int(splits[0]) dtm = datetime.strptime(splits[1], '%Y%m%d') path = dtp.datepath.FromCsv(mc.REAL_PATH + f, dp.Fruits(), date=dtm.date(), monkey=monkey, delimiter=',', quotechar='"', start=START_TIME, end=END_TIME) # disp.display_datepath(path, fruit_dim='') # quit() # get dataframe row row = path.getDataframeRow() rows.append(row) counter += 1 total += 1 # create and save dataframe every STEPSIZE loops if counter >= STEPSIZE: