def prepare_data(path, arena, smooth, medfilt, only_laser, gts): LASER_THORAX_MAP = {True:THORAX,False:HEAD} #PROCESS SCORE FILES: pooldf = pd.DataFrame() for csvfile in sorted(glob.glob(path + "/*.csv")): #don't waste time smoothing files not in out genotype list _,_,_,_genotype,_laser,_ = flymad_analysis.extract_metadata_from_filename(csvfile) if _laser != only_laser: print "\tskipping laser", _laser, "!=", only_laser continue if _genotype not in gts: print "\tskipping genotype", _genotype, "!=", gts continue csvfilefn = os.path.basename(csvfile) cache_args = csvfilefn, arena, smoothstr cache_fname = csvfile+'.madplot-cache' results = madplot.load_bagfile_cache(cache_args, cache_fname) if results is None: results = flymad_analysis.load_and_smooth_csv(csvfile, arena, smooth) if results is not None: #update the cache madplot.save_bagfile_cache(results, cache_args, cache_fname) else: print "skipping", csvfile continue df,dt,experimentID,date,time,genotype,laser,repID = results duration = (df.index[-1] - df.index[0]).total_seconds() if duration < EXPERIMENT_DURATION: print "\tmissing data", csvfilefn, duration continue print "\t%ss experiment" % duration #we use zx to rotate by pi df['zx'][df['zx'] > 0] = math.pi #ROTATE by pi if orientation is east df['orientation'] = df['theta'] + df['zx'] #ROTATE by pi if orientation is north/south (plusminus 0.25pi) and hemisphere does not match scoring: smask = df[df['as'] == 1] smask = smask[smask['orientation'] < 0.75*(math.pi)] smask = smask[smask['orientation'] > 0.25*(math.pi)] amask = df[df['as'] == 0] amask1 = amask[amask['orientation'] > -0.5*(math.pi)] amask1 = amask1[amask1['orientation'] < -0.25*(math.pi)] amask2 = amask[amask['orientation'] > 1.25*(math.pi)] amask2 = amask2[amask2['orientation'] < 1.5*(math.pi)] df['as'] = 0 df['as'][smask.index] = math.pi df['as'][amask1.index] = math.pi df['as'][amask2.index] = math.pi df['orientation'] = df['orientation'] - df['as'] df['orientation'] = df['orientation'].astype(float) df['orientation'][np.isfinite(df['orientation'])] = np.unwrap(df['orientation'][np.isfinite(df['orientation'])]) #MAXIMUM SPEED = 300: df['v'][df['v'] >= 300] = np.nan #CALCULATE FORWARD VELOCITY df['Vtheta'] = np.arctan2(df['vy'], df['vx']) df['Vfwd'] = (np.cos(df['orientation'] - df['Vtheta'])) * df['v'] df['Afwd'] = np.gradient(df['Vfwd'].values) / dt df['dorientation'] = np.gradient(df['orientation'].values) / dt try: df = flymad_analysis.align_t_by_laser_on( df, min_experiment_duration=EXPERIMENT_DURATION, align_first_only=False, t_range=(-1,6), min_num_ranges=5) except flymad_analysis.AlignError, err: print "\talign error %s (%s)" % (csvfilefn, err) continue #median filter if medfilt: df['Vfwd'] = scipy.signal.medfilt(df['Vfwd'].values, medfilt) df['obj_id'] = flymad_analysis.create_object_id(date,time) df['Genotype'] = genotype df['lasergroup'] = laser df['RepID'] = repID pooldf = pd.concat([pooldf, df])
calibration_file = None d1_arena = madplot.Arena( 'mm', **flymad_analysis.get_arena_conf(calibration_file=calibration_file)) cache_fname = os.path.join(path,'moonwalker_d1.madplot-cache') cache_args = (path, DAY_1_GENOTYPES, LASER_POWERS, smoothstr, d1_arena) d1_data = None if args.only_plot: d1_data = madplot.load_bagfile_cache(cache_args, cache_fname) if d1_data is None: #these loops are braindead inefficient and the wrong way, #however, we have limited time, and I cache the intermediate #representation anyway... d1_data = {k:dict() for k in DAY_1_GENOTYPES} for gt in DAY_1_GENOTYPES: for lp in LASER_POWERS: try: d1_data[gt][lp] = prepare_data(path, d1_arena, args.smooth, medfilt, lp, [gt]) except KeyError: #this laser power and genotype combination was not tested pass madplot.save_bagfile_cache(d1_data, cache_args, cache_fname) all_data = d1_data note = "%s %s\nd1arena:%r\nmedfilt %s" % (d1_arena.unit, smoothstr, d1_arena, medfilt) plot_all_data(path, all_data, d1_arena, note) if args.show: plt.show()
smoothstr = '%s' % {True: 'smooth', False: 'nosmooth'}[args.smooth] arena = madplot.Arena('mm') note = "%s %s\n%r\nmedfilt %s" % (arena.unit, smoothstr, arena, medfilt) cache_fname = os.path.join(os.path.dirname(path), 'speed.madplot-cache') cache_args = ([os.path.basename(b) for b in bags], smoothstr, args.smooth, medfilt, genotypes, args.min_experiment_duration) data = None if args.only_plot: data = madplot.load_bagfile_cache(cache_args, cache_fname) if data is None: data = prepare_data(bags, arena, smoothstr, args.smooth, medfilt, genotypes, args.min_experiment_duration) madplot.save_bagfile_cache(data, cache_args, cache_fname) if args.stats: fname_prefix = flymad_plot.get_plotpath(path, 'csv_speed') madplot.view_pairwise_stats_plotly( data, genotypes, fname_prefix, align_colname='t', stat_colname='v', ) plot_data(path, data, arena, note) if args.show: plt.show()
def prepare_data(path, arena, smooth, medfilt, only_laser, gts): LASER_THORAX_MAP = {True: THORAX, False: HEAD} #PROCESS SCORE FILES: pooldf = pd.DataFrame() for csvfile in sorted(glob.glob(path + "/*.csv")): #don't waste time smoothing files not in out genotype list _, _, _, _genotype, _laser, _ = flymad_analysis.extract_metadata_from_filename( csvfile) if _laser != only_laser: print "\tskipping laser", _laser, "!=", only_laser continue if _genotype not in gts: print "\tskipping genotype", _genotype, "!=", gts continue csvfilefn = os.path.basename(csvfile) cache_args = csvfilefn, arena, smoothstr cache_fname = csvfile + '.madplot-cache' results = madplot.load_bagfile_cache(cache_args, cache_fname) if results is None: results = flymad_analysis.load_and_smooth_csv( csvfile, arena, smooth) if results is not None: #update the cache madplot.save_bagfile_cache(results, cache_args, cache_fname) else: print "skipping", csvfile continue df, dt, experimentID, date, time, genotype, laser, repID = results duration = (df.index[-1] - df.index[0]).total_seconds() if duration < EXPERIMENT_DURATION: print "\tmissing data", csvfilefn continue print "\t%ss experiment" % duration #we use zx to rotate by pi df['zx'][df['zx'] > 0] = math.pi #ROTATE by pi if orientation is east df['orientation'] = df['theta'] + df['zx'] #ROTATE by pi if orientation is north/south (plusminus 0.25pi) and hemisphere does not match scoring: smask = df[df['as'] == 1] smask = smask[smask['orientation'] < 0.75 * (math.pi)] smask = smask[smask['orientation'] > 0.25 * (math.pi)] amask = df[df['as'] == 0] amask1 = amask[amask['orientation'] > -0.5 * (math.pi)] amask1 = amask1[amask1['orientation'] < -0.25 * (math.pi)] amask2 = amask[amask['orientation'] > 1.25 * (math.pi)] amask2 = amask2[amask2['orientation'] < 1.5 * (math.pi)] df['as'] = 0 df['as'][smask.index] = math.pi df['as'][amask1.index] = math.pi df['as'][amask2.index] = math.pi df['orientation'] = df['orientation'] - df['as'] df['orientation'] = df['orientation'].astype(float) df['orientation'][np.isfinite(df['orientation'])] = np.unwrap( df['orientation'][np.isfinite(df['orientation'])]) #MAXIMUM SPEED = 300: df['v'][df['v'] >= 300] = np.nan #CALCULATE FORWARD VELOCITY df['Vtheta'] = np.arctan2(df['vy'], df['vx']) df['Vfwd'] = (np.cos(df['orientation'] - df['Vtheta'])) * df['v'] df['Afwd'] = np.gradient(df['Vfwd'].values) / dt df['dorientation'] = np.gradient(df['orientation'].values) / dt try: df = flymad_analysis.align_t_by_laser_on( df, min_experiment_duration=EXPERIMENT_DURATION, align_first_only=False, t_range=(-1, 9), min_num_ranges=5) except flymad_analysis.AlignError, err: print "\talign error %s (%s)" % (csvfilefn, err) continue #median filter if medfilt: df['Vfwd'] = scipy.signal.medfilt(df['Vfwd'].values, medfilt) df['obj_id'] = flymad_analysis.create_object_id(date, time) df['Genotype'] = genotype df['lasergroup'] = laser df['RepID'] = repID pooldf = pd.concat([pooldf, df])
def prepare_data(path, arena, smoothstr, smooth, medfilt, gts): pooldf = DataFrame() for csvfile in sorted(glob.glob(path + "/*.csv")): cache_args = os.path.basename(csvfile), arena, smoothstr cache_fname = csvfile+'.madplot-cache' results = madplot.load_bagfile_cache(cache_args, cache_fname) if results is None: results = flymad_analysis.load_and_smooth_csv(csvfile, arena, smooth) if results is not None: #update the cache madplot.save_bagfile_cache(results, cache_args, cache_fname) else: print "skipping", csvfile continue df,dt,experimentID,date,time,genotype,laser,repID = results #we plot head v thorax v nolaser (so for the same of plotting, consider #these the genotypes genotype = genotype + '-' + laser if genotype not in gts: print "\tskipping genotype", genotype continue if 0: fig = plt.figure() fig.suptitle(os.path.basename(csvfile)) ax = fig.add_subplot(1,1,1) df['experiment'] = 1 df['tobj_id'] = 1 madplot.plot_tracked_trajectory(ax, df, arena, debug_plot=False, color='k', ) ax.add_patch(arena.get_patch(color='k', alpha=0.1)) duration = (df.index[-1] - df.index[0]).total_seconds() if duration < EXPERIMENT_DURATION: print "\tmissing data", csvfilefn continue print "\t%ss experiment" % duration #MAXIMUM SPEED = 300: df['v'][df['v'] >= 300] = np.nan df['v'] = df['v'].fillna(method='ffill') try: df = flymad_analysis.align_t_by_laser_on( df, min_experiment_duration=EXPERIMENT_DURATION, align_first_only=True, exact_num_ranges=1) except flymad_analysis.AlignError, err: print "\talign error %s (%s)" % (csvfile, err) continue #median filter if medfilt: df['v'] = scipy.signal.medfilt(df['v'].values, medfilt) df['obj_id'] = flymad_analysis.create_object_id(date,time) df['Genotype'] = genotype df['lasergroup'] = laser pooldf = pd.concat([pooldf, df])
def prepare_data(path, arena, smoothstr, smooth, medfilt, gts): pooldf = DataFrame() for csvfile in sorted(glob.glob(path + "/*.csv")): cache_args = os.path.basename(csvfile), arena, smoothstr cache_fname = csvfile + '.madplot-cache' results = madplot.load_bagfile_cache(cache_args, cache_fname) if results is None: results = flymad_analysis.load_and_smooth_csv( csvfile, arena, smooth) if results is not None: #update the cache madplot.save_bagfile_cache(results, cache_args, cache_fname) else: print "skipping", csvfile continue df, dt, experimentID, date, time, genotype, laser, repID = results #we plot head v thorax v nolaser (so for the same of plotting, consider #these the genotypes genotype = genotype + '-' + laser if genotype not in gts: print "\tskipping genotype", genotype continue if 0: fig = plt.figure() fig.suptitle(os.path.basename(csvfile)) ax = fig.add_subplot(1, 1, 1) df['experiment'] = 1 df['tobj_id'] = 1 madplot.plot_tracked_trajectory( ax, df, arena, debug_plot=False, color='k', ) ax.add_patch(arena.get_patch(color='k', alpha=0.1)) duration = (df.index[-1] - df.index[0]).total_seconds() if duration < EXPERIMENT_DURATION: print "\tmissing data", csvfilefn continue print "\t%ss experiment" % duration #MAXIMUM SPEED = 300: df['v'][df['v'] >= 300] = np.nan df['v'] = df['v'].fillna(method='ffill') try: df = flymad_analysis.align_t_by_laser_on( df, min_experiment_duration=EXPERIMENT_DURATION, align_first_only=True, exact_num_ranges=1) except flymad_analysis.AlignError, err: print "\talign error %s (%s)" % (csvfile, err) continue #median filter if medfilt: df['v'] = scipy.signal.medfilt(df['v'].values, medfilt) df['obj_id'] = flymad_analysis.create_object_id(date, time) df['Genotype'] = genotype df['lasergroup'] = laser pooldf = pd.concat([pooldf, df])