def detectSeam(x, s=(.25, .25)): mx0 = zeros_like(x) mx1 = zeros_like(x) mn0 = zeros_like(x) mn1 = zeros_like(x) y = smooth(x, s[1], axis=1) y = smooth(y, s[0], axis=0) for i in range(1, x.shape[1] - 1): gtm = y[:, i] > y[:, i - 1] gtp = y[:, i] > y[:, i + 1] ltm = y[:, i] < y[:, i - 1] ltp = y[:, i] < y[:, i + 1] mx0[:, i] = logical_and(gtm, gtp) mn0[:, i] = logical_and(ltm, ltp) for i in range(1, x.shape[0] - 1): gtm = y[i, :] > y[i - 1, :] gtp = y[i, :] > y[i + 1, :] ltm = y[i, :] < y[i - 1, :] ltp = y[i, :] < y[i + 1, :] mx1[i, :] = logical_and(gtm, gtp) mn1[i, :] = logical_and(ltm, ltp) mn = logical_or(mn0, mn1) mx = logical_or(mx0, mx1) return (mn, mx)
def run_just_area_analysis(self): kernel = np.ones((3, 3), np.uint8) plt.figure() for label in self.firstactivelabels: if self.clips.shape[0] > 0: clip = self.clips[self.active_labels == label] if clip.shape[0] > 0: threshold = threshold_otsu(clip) testclip = np.zeros_like( self.clips[self.active_labels == label].reshape( 31, 31)) testclip[self.clips[self.active_labels == label].reshape( 31, 31) > (threshold + 0.3 * threshold)] = 1 testclip = cv2.normalize(src=testclip, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) opening = cv2.morphologyEx(testclip, cv2.MORPH_OPEN, kernel, iterations=2) #sure background area sure_bg = cv2.dilate(opening, kernel, iterations=3) #Find sure foreground area dist_transform = cv2.distanceTransform( opening, cv2.DIST_L2, 5) ret, sure_fg = cv2.threshold(dist_transform, 0.6 * dist_transform.max(), 255, 0) sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg, sure_fg) ret, markers = cv2.connectedComponents(sure_fg) markers = markers + 1 markers[unknown == 255] = 0 clip = clip.reshape(31, 31) clip = smooth(clip, sigma=1) print(clip.shape, clip) nclip = cv2.normalize(src=clip, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) gclip = cv2.cvtColor(nclip, cv2.COLOR_GRAY2BGR) markers = cv2.watershed(gclip, markers) print(np.argwhere(markers == 2)) '''gclip[markers == -1] = 255
def peak_deriv(samples, sampletimes, smth): smth = smth[0] smooth_deriv = smooth(np.diff(samples).astype(float), smth) / np.diff(sampletimes) declining = np.argwhere(smooth_deriv < 0)[0] return declining
def subtract_background(self, sigma=3): for key in self.firstintensitytrace.keys(): try: self.bg_sub_intensity_trace[key] = np.array( self.intensitytrace[key]) - self.bgintens self.second_bg_si_trace[key] = np.array( self.secondintensitytrace[key]) - self.bgintens self.filtered_intensity_trace[key] = smooth( self.bg_sub_intensity_trace[key], sigma) max_data = np.max(self.filtered_intensity_trace[key]) self.filtered_intensity_trace[ key] = self.filtered_intensity_trace[key] / max_data except KeyError: continue self.bg_sub_firstintensity_trace[key] = np.array( self.firstintensitytrace[key]) - self.bgintens initial_I = self.bg_sub_firstintensity_trace[key][0] initial_I = np.array([initial_I]) self.second_bg_fsi_trace[key] = np.array( self.firstsecondintensitytrace[key]) - self.bgintens # self.filtered_first_intensity_trace[key] = smooth( self.bg_sub_firstintensity_trace[key], sigma) self.filtered_first_intensity_trace[key] = smooth( self.firstintensitytrace[key], sigma) max_data = np.max(self.filtered_first_intensity_trace[key]) self.filtered_first_intensity_trace[ key] = self.filtered_first_intensity_trace[key] / max_data self.filtered_first_intensity_trace[key] = np.concatenate( (initial_I, self.filtered_first_intensity_trace[key])) print('This is the initial Area, ', self.filtered_first_intensity_trace[key][0])
def if_threshold_fails(self, centre, label, firstclip, testclip): rr, cc = circle(centre[0], centre[1], int(np.sqrt(self.firstareatrace[str(label)][-1])), shape=firstclip.shape) img = np.zeros_like(firstclip[0]) img[rr, cc] = firstclip[0][rr, cc] #eccentricity = self.get_eccentricity(centre,testclip) try: self.firstareatrace[str(label)].append(0) self.filtered_firstareatrace[str(label)] = smooth( self.firstareatrace[str(label)], 3) self.firstsecondintensitytrace[str(label)].append( np.average(img[img > 0])) except KeyError: self.firstareatrace[str(label)] = [0] self.filtered_firstareatrace[str(label)] = smooth( self.firstareatrace[str(label)], 3) self.firstsecondintensitytrace[str(label)] = [ np.average(img[img > 0]) ]
def extract_area(self, clip): # this function takes in a clip of the image, unbinarised kernel = np.ones((3, 3), np.uint8) #kernel is the 2D spreading function which dilates the edges in a process similar to convolution but where the convolution is performed conditionally on a foreground pixel not being completely surrounded by other foreground pixels clip = clip.reshape(31, 31) #testclip = cv2.normalize(src=testclip,dst=None,alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX,dtype = cv2.CV_8UC1) clip8 = (clip / 256).astype('uint8') ret, thresh = cv2.threshold(clip8, 0, 255, cv2.THRESH_OTSU) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) #sure background area sure_bg = cv2.dilate(opening, kernel, iterations=3) #Find sure foreground area dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5) ret, sure_fg = cv2.threshold(dist_transform, 0.6 * dist_transform.max(), 255, 0) sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg, sure_fg) ret, markers = cv2.connectedComponents(sure_fg, ltype=cv2.CV_32S) markers = markers + 1 markers[unknown == 255] = 0 clip8 = smooth(clip8, sigma=1) #plt.imshow(clip8) #plt.show() #nclip = cv2.normalize(src=clip,dst=None,alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX,dtype = cv2.CV_8UC1) gclip = cv2.cvtColor(clip8, cv2.COLOR_GRAY2RGB) markers = cv2.watershed(gclip, markers) #Update code here to decide sensibly which pixelarea = markers[markers == 2].shape[0] return pixelarea
def get_evts(rslt, a_params): """Return start and end times of candidate replay events.""" # get PC firing rates ## PC spks spks_pc = rslt.spks[:, :rslt.p['N_PC']] ## smoothed instantaneous firing rate avg'd over PCs fr_pc = smooth( spks_pc.sum(axis=1) / (rslt.dt * rslt.p['N_PC']), a_params['SMOOTH_FR']) # get start and end time idxs when PC FR is above threshold starts, ends = get_segments(fr_pc >= a_params['EVT_DTCN_TH']) # convert to time starts = starts.astype(float) * rslt.dt ends = ends.astype(float) * rslt.dt # remove too-short gaps btwn events if len(starts) > 0: starts, ends = remove_short_gaps(starts, ends, a_params['MIN_GAP_DUR']) # remove too-short events if len(starts) > 0: starts, ends = remove_short_evts(starts, ends, a_params['MIN_EVT_DUR']) # remove all events that start before min start time if len(starts): mask = starts > a_params['MIN_START'] starts = starts[mask] ends = ends[mask] # remove final event if it hits end of smln if len(ends) and ends[-1] >= rslt.ts[-1]: starts = starts[:-1] ends = ends[:-1] return starts, ends
def raster_with_pc_inh(rslt, xys, colors, cmap, nearest, epoch, trg_plt, y_lim, y_ticks, smoothness=1, n_t_ticks=None, fig_size=(15, 9), title=None): """ Make raster plots of PCs specified by place fields, along with full PC/INH rasters/rate traces. :param xys: list of (x, y) locs to plot spks from nearby cells for :param nearest: # of cells per (x, y) :param epoch: 'replay', 'wdw', 'trj', or 'full', specifying which epoch to make raster for (replay, detection window, trajectory, or full smln) """ fig, axs = plt.subplots(3, 1, figsize=fig_size, tight_layout=True) # get ordered idxs of PCs to plot ## get pfs pc_mask = rslt.ntwk.types_rcr == 'PC' pfxs = rslt.ntwk.pfxs[pc_mask] pfys = rslt.ntwk.pfys[pc_mask] ## loop through (x, y) pairs and add idxs of nearest PCs ### pc_c_dict_0 uses original pc idxs, pc_c_dict_1 uses simplified pc idxs pc_idxs, pc_c_dict_0, pc_c_dict_1 = get_idxs_nearest( xys, pfxs, pfys, nearest, colors) # get all spks for selected PCs spks_pc_chosen = rslt.spks[:, pc_idxs] # get desired time window if epoch == 'replay': start = 0 end = rslt.schedule['D_SMLN'] elif isinstance(epoch, tuple): start = epoch[0] end = epoch[1] t_mask = (start <= rslt.ts) & (rslt.ts < end) t_start = rslt.ts[t_mask][0] spk_t_idxs, pcs = spks_pc_chosen[t_mask].nonzero() spk_ts = spk_t_idxs * rslt.s_params['DT'] + t_start ## spks c = [pc_c_dict_1[pc] for pc in pcs] axs[0].scatter(spk_ts, pcs, c=c, s=30, vmin=0, vmax=1, cmap=cmap, lw=.5, edgecolor='k') ## replay trigger for trg, (y, marker) in zip(rslt.trg, trg_plt): axs[0].scatter(trg['T'], y, marker=marker, s=100, c='k') axs[0].set_xlim(start, end) axs[0].set_ylim(y_lim) axs[0].set_yticks(y_ticks) axs[0].set_xlabel('Time (s)') axs[0].set_ylabel('Neuron') axs[0].set_title('Spike sequences') if title is not None: axs[0].set_title(title) set_font_size(axs[0], 16) # PCs ## get spks spks_pc = rslt.spks[:, :rslt.p['N_PC']] ## raster t_idxs_spks_pc, nrn_spks_pc = spks_pc.nonzero() t_spks_pc = t_idxs_spks_pc * rslt.dt axs[1].scatter(t_spks_pc, nrn_spks_pc, s=5, c='k') # population firing rate axs[2].plot(rslt.ts, smooth( spks_pc.sum(axis=1) / (rslt.dt * rslt.p['N_PC']), smoothness), c='k', lw=3) axs[2].set_xlabel('Time (s)') axs[2].set_ylabel('PC spike rate (Hz)') axs[2].set_title('Population spike rates') # INHs # get spks spks_inh = rslt.spks[:, -rslt.p['N_INH']:] # raster t_idxs_spks_inh, nrn_spks_inh = spks_inh.nonzero() t_spks_inh = t_idxs_spks_inh * rslt.dt axs[1].scatter(t_spks_inh, -(1 + nrn_spks_inh), s=5, c='r') axs[1].set_yticks([-rslt.p['N_INH'] / 2, rslt.p['N_PC'] / 2]) axs[1].set_yticklabels(['INH', 'PC']) axs[1].set_xlabel('Time (s)') axs[1].set_title('Full raster') for tick_label, color in zip(axs[1].get_yticklabels(), ['r', 'k']): tick_label.set_color(color) # inh population average ax_2_twin = axs[2].twinx() ax_2_twin.plot(rslt.ts, smooth( spks_inh.sum(axis=1) / (rslt.dt * rslt.p['N_INH']), smoothness), c='r', lw=2) ax_2_twin.set_ylabel('INH spike rate (Hz)') axs[2].set_zorder(ax_2_twin.get_zorder() + 1) axs[2].patch.set_visible(False) set_colors(ax_2_twin, 'r') for ax in list(axs[1:]) + [ax_2_twin]: ax.set_xlim(0, rslt.ts[-1]) set_font_size(ax, 16) return fig, axs
def main(SIM_PREFIX=None, sim_ids=None, thresholds=None, trial_limit=None): if thresholds is None: thresholds = THRESHOLDS SCRIPTNOTES = ('Identify plume crossings for simulations with prefix "{}" ' 'using heading smoothing "{}" and thresholds "{}"'.format( SIM_PREFIX, HEADING_SMOOTHING, thresholds)) if sim_ids is None: SIM_SUFFIXES = [ 'fruitfly_0.3mps_checkerboard_floor_odor_on', 'fruitfly_0.3mps_checkerboard_floor_odor_none', 'fruitfly_0.3mps_checkerboard_floor_odor_afterodor', 'fruitfly_0.4mps_checkerboard_floor_odor_on', 'fruitfly_0.4mps_checkerboard_floor_odor_none', 'fruitfly_0.4mps_checkerboard_floor_odor_afterodor', 'fruitfly_0.6mps_checkerboard_floor_odor_on', 'fruitfly_0.6mps_checkerboard_floor_odor_none', 'fruitfly_0.6mps_checkerboard_floor_odor_afterodor', 'mosquito_0.4mps_checkerboard_floor_odor_on', 'mosquito_0.4mps_checkerboard_floor_odor_none', 'mosquito_0.4mps_checkerboard_floor_odor_afterodor',] sim_ids = [ '{}_{}'.format(SIM_PREFIX, sim_suffix) for sim_suffix in SIM_SUFFIXES ] # add script execution to database add_script_execution( SCRIPTID, session=session, multi_use=True, notes=SCRIPTNOTES) for sim_id in sim_ids: print('Identifying crossings from simulation: "{}"'.format(sim_id)) # get simulation sim = session.query(models.Simulation).filter_by(id=sim_id).first() # get all trials from this simulation trials = session.query(models.Trial).filter_by(simulation=sim).all() # make crossing group if 'fly' in sim_id: threshold = thresholds['fly'] elif 'mosq' in sim_id: threshold = thresholds['mosq'] cg_id = '{}_th_{}_hsmoothing_{}'.format( sim_id, threshold, HEADING_SMOOTHING) print('Storing in crossing group:') print(cg_id) cg = models.CrossingGroup( id=cg_id, simulation=sim, threshold=threshold, heading_smoothing=HEADING_SMOOTHING) session.add(cg) # loop through trials and identify crossings trial_ctr = 0 for trial in trials: if trial_limit and trial_ctr >= trial_limit: break # get relevant time-series odors = trial.timepoint_field(session, 'odor') xs = trial.timepoint_field(session, 'xidx') ys = trial.timepoint_field(session, 'yidx') zs = trial.timepoint_field(session, 'zidx') # get smoothed headings hs = smooth(trial.timepoint_field(session, 'hxyz'), HEADING_SMOOTHING) # identify crossings crossing_lists, peaks = time_series.segment_by_threshold( odors, threshold) tr_start = trial.start_timepoint_id # add crossings for c_ctr, (crossing_list, peak) in enumerate(zip(crossing_lists, peaks)): crossing = models.Crossing( trial=trial, crossing_number=c_ctr+1, crossing_group=cg, start_timepoint_id=crossing_list[0] + tr_start, entry_timepoint_id=crossing_list[1] + tr_start, peak_timepoint_id=crossing_list[2] + tr_start, exit_timepoint_id=crossing_list[3] + tr_start - 1, end_timepoint_id=crossing_list[4] + tr_start - 1, max_odor=peak,) session.add(crossing) # create this crossing's basic feature set crossing.feature_set_basic = models.CrossingFeatureSetBasic( position_x_entry=xs[crossing_list[1]], position_y_entry=ys[crossing_list[1]], position_z_entry=zs[crossing_list[1]], heading_xyz_entry=hs[crossing_list[1]], position_x_peak=xs[crossing_list[2]], position_y_peak=ys[crossing_list[2]], position_z_peak=zs[crossing_list[2]], heading_xyz_peak=hs[crossing_list[2]], position_x_exit=xs[crossing_list[3] - 1], position_y_exit=ys[crossing_list[3] - 1], position_z_exit=zs[crossing_list[3] - 1], heading_xyz_exit=hs[crossing_list[3] - 1], ) session.add(crossing) trial_ctr += 1 # commit after all crossings from all trials from a simulation have been added session.commit()
def findDescendingRegion(data, halfIntensityPoints,window = 1000): upperlimit = int(window/2) lowerlimit = int(window/2) avrates = [] lifetimes = [] for i in range(0,halfIntensityPoints.shape[0]): upperlimit = int(window/2) lowerlimit = int(window/2) plt.clf() if halfIntensityPoints[i] == data.shape[0]: continue if lowerlimit > halfIntensityPoints[i]: print(lowerlimit) lowerlimit = halfIntensityPoints[i] if upperlimit > data[:,i].shape[0] - halfIntensityPoints[i]: upperlimit = data[:,i].shape[0] - halfIntensityPoints[i] section = data[(halfIntensityPoints[i]-lowerlimit):(halfIntensityPoints[i]+upperlimit),i] if section.shape[0] == 0: continue smoothed_section = smooth(section,3) rates = np.gradient(section) if np.average(rates) > 0: print('This is the average gradient', np.average(rates)) continue #navigate to maximum maximum = np.argwhere(section == np.nanmax(section)) if maximum.shape[0] == 0: continue else: maximum = maximum[0][0] section = section[maximum:] start = np.argwhere(section < 0.95*section[0]) if start.shape[0] == 0: print('went wrong at start') continue else: start = start[0][0] if start == section.shape[0]: continue end = np.argwhere(section[start:] < 0.5*section[0]) if end.shape[0] == 0: end = section.shape[0] if end.shape[0] == 0: continue else: end = end[0][0] + start #backtrace = section[:end][::-1][section[:end][::-1] > 0.95] #if backtrace.shape[0] > 0: # start = end - np.argwhere(section[:end][::-1] == backtrace[0])[0][0] -1 # #fit exponential to this region of the intensity trace and then take time constant parameters, success = get_time_constant(section,start,end) if success ==1: time_constant = parameters[0][1] error = parameters[1][1,1] else: time_constant = end-start error = 0 avrates.append([time_constant,error]) lifetimes.append(halfIntensityPoints[i]) return avrates, lifetimes
#maxes = np.nanmax(intensity_traces,axis = 0) #mins = np.nanmin(intensity_traces,axis = 0) #intensity_traces =(intensity_traces - mins) #intensity_traces = intensity_traces/maxes for i in range(intensity_traces.shape[1]): maxe = np.nanmax(intensity_traces[:,i]) mine = np.nanmin(intensity_traces[:,i]) intensity_traces[:,i] = intensity_traces[:,i]/maxe for i in range(0,intensity_traces.shape[1]): intensity_traces[:,i] = smooth(intensity_traces[:,i],0.3) lifetimes = np.nansum(intensity_traces > 0.5,axis = 0) av_rates, finallifetimes = findDescendingRegion(intensity_traces,lifetimes) np.savetxt(dpath + outpath,np.array(av_rates),delimiter = ',') av_rates = np.array(av_rates) dictionary = {} dictionary['rates'] = av_rates[:,0] dictionary['error'] = av_rates[:,1]
def extract_intensity(self, label, counter, kernel): if self.clips.shape[0] > 0: clip = self.clips[self.active_labels == label] if clip.shape[0] > 0: threshold = threshold_otsu(clip) testclip = np.zeros_like( self.clips[self.active_labels == label].reshape(31, 31)) testclip[self.clips[self.active_labels == label].reshape( 31, 31) > threshold] = 1 try: self.secondareatrace[str(label)].append( len(testclip[testclip > 0])) except KeyError: self.secondareatrace[str(label)] = [ len(testclip[testclip > 0]) ] dt = distance_transform_edt(testclip) try: centre = list(peak_local_max(dt, threshold_rel=0.6)[0]) rr, cc = circle(centre[0], centre[1], int(dt[centre[0], centre[1]]), shape=dt.shape) img = np.zeros_like(dt) img[rr, cc] = self.clips[self.active_labels == label][0][rr, cc] try: #pixelcount = self.extract_area(clip) #self.firstareatrace[str(label)].append(pixelcount) #self.areatrace[str(label)].append(self.extract_area(testclip,kernel,clip)) #self.filtered_areatrace[str(label)] = smooth(self.firstareatrace[str(label)],3) self.secondintensitytrace[str(label)].append( np.average(img[img > 0])) except KeyError: #pixelcount = self.extract_area(clip) #self.firstareatrace[str(label)]=[pixelcount] #self.areatrace[str(label)] = [self.extract_area(testclip,kernel,clip)] #self.filtered_areatrace[str(label)]=smooth(self.firstareatrace[str(label)],3) self.secondintensitytrace[str(label)] = [ np.average(img[img > 0]) ] except IndexError: centre = [] try: self.missing_peaks[str(label)].append(counter) except KeyError: self.missing_peaks[str(label)] = [counter] if len(centre) > 0: x1 = 4 y1 = 4 lx = 31 ly = 31 #check the box can fit into clip centre = np.array(centre) dims_min = np.array([x1, y1]) dims_max = np.array([lx - x1, ly - y1]) margins_from_edge = np.vstack( ((centre - dims_min), (dims_max - centre))) if np.any(margins_from_edge.flatten() < 0): x1 += np.min(margins_from_edge) y1 += np.min(margins_from_edge) small_box_in_ves = self.clips[self.active_labels == label][0][centre[0] - y1:centre[0] + y1, centre[1] - x1:centre[1] + x1] av_intens = np.average(small_box_in_ves) try: self.intensitytrace[str(label)].append(av_intens) except KeyError: self.intensitytrace[str(label)] = [av_intens] try: self.centres[str(label)].append(centre) except KeyError: self.centres[str(label)] = [centre] firstclip = self.firstactiveclips[self.firstactivelabels == label] threshold = threshold_otsu(firstclip) testclip = np.zeros_like(firstclip.reshape(31, 31)) testclip[firstclip.reshape(31, 31) > threshold] = 1 try: self.firstsecondareatrace[str(label)].append( self.extract_area(firstclip)) except KeyError: self.firstsecondareatrace[str(label)] = [ self.extract_area(firstclip) ] dt = distance_transform_edt(testclip) try: #if thresholding has failed completely to find a foreground, no peak can be found. The return value assigned to #the variable 'centre' is not an array so cannot be indexed. An index error will be thrown and caught below centre = list(peak_local_max(dt, threshold_rel=0.6)[0]) rr, cc = circle(centre[0], centre[1], int(dt[centre[0], centre[1]]), shape=dt.shape) img = np.zeros_like(dt) img[rr, cc] = firstclip[0][rr, cc] try: pixelcount = self.extract_area(firstclip) self.firstareatrace[str(label)].append(pixelcount) self.areatrace[str(label)].append(self.extract_area(firstclip)) self.filtered_firstareatrace[str(label)] = smooth( self.firstareatrace[str(label)], 3) self.firstsecondintensitytrace[str(label)].append( np.average(img[img > 0])) except KeyError: pixelcount = self.extract_area(firstclip) self.areatrace[str(label)] = [self.extract_area(firstclip)] self.firstareatrace[str(label)] = [pixelcount] self.filtered_firstareatrace[str(label)] = smooth( self.firstareatrace[str(label)], 3) self.firstsecondintensitytrace[str(label)] = [ np.average(img[img > 0]) ] except IndexError: #If no centre position is found, we take the last centre position found and take the intensity value from this #If thresholding fails to find a foreground in the first frame, such that there is no previously recorded centre position #we do not record any intensity or area values for the contents of this box in this frame. try: centre = self.firstcentres[str(label)][-1] self.if_threshold_fails(centre, label, firstclip, testclip) except KeyError or IndexError: centre = [] ''' centre = [] try: self.missing_peaks[str(label)].append(counter) except KeyError: self.missing_peaks[str(label)] = [counter] ''' if len(centre) > 0: x1 = 4 y1 = 4 lx = 31 ly = 31 #check the box can fit into clip centre = np.array(centre) dims_min = np.array([x1, y1]) dims_max = np.array([lx - x1, ly - y1]) margins_from_edge = np.vstack( ((centre - dims_min), (dims_max - centre))) if np.any(margins_from_edge.flatten() < 0): x1 += np.min(margins_from_edge) y1 += np.min(margins_from_edge) small_box_in_ves = firstclip[0][centre[0] - y1:centre[0] + y1, centre[1] - x1:centre[1] + x1] av_intens = np.average(small_box_in_ves) try: self.firstintensitytrace[str(label)].append(av_intens) except KeyError: self.firstintensitytrace[str(label)] = [av_intens] try: self.firstcentres[str(label)].append(centre) except KeyError: self.firstcentres[str(label)] = [centre]
def main(trial_limit=None): # add script execution to database add_script_execution(SCRIPTID, session=session, multi_use=True, notes=SCRIPTNOTES) for sim_id in SIM_IDS: print('Identifying crossings from simulation: "{}"'.format(sim_id)) # get simulation sim = session.query(models.Simulation).filter_by(id=sim_id).first() # get all trials from this simulation trials = session.query(models.Trial).filter_by(simulation=sim).all() # make crossing group if 'fly' in sim_id: threshold = THRESHOLDS['fly'] elif 'mosq' in sim_id: threshold = THRESHOLDS['mosq'] cg_id = '{}_th_{}_hsmoothing_{}'.format(sim_id, threshold, HEADING_SMOOTHING) cg = models.CrossingGroup( id=cg_id, simulation=sim, threshold=threshold, heading_smoothing=HEADING_SMOOTHING) session.add(cg) # loop through trials and identify crossings trial_ctr = 0 for trial in trials: if trial_limit and trial_ctr >= trial_limit: break # get relevant time-series odors = trial.timepoint_field(session, 'odor') xs = trial.timepoint_field(session, 'xidx') ys = trial.timepoint_field(session, 'yidx') zs = trial.timepoint_field(session, 'zidx') # get smoothed headings hs = smooth(trial.timepoint_field(session, 'hxyz'), HEADING_SMOOTHING) # identify crossings crossing_lists, peaks = time_series.segment_by_threshold( odors, threshold) tr_start = trial.start_timepoint_id # add crossings for c_ctr, (crossing_list, peak) in enumerate(zip(crossing_lists, peaks)): crossing = models.Crossing( trial=trial, crossing_number=c_ctr+1, crossing_group=cg, start_timepoint_id=crossing_list[0] + tr_start, entry_timepoint_id=crossing_list[1] + tr_start, peak_timepoint_id=crossing_list[2] + tr_start, exit_timepoint_id=crossing_list[3] + tr_start - 1, end_timepoint_id=crossing_list[4] + tr_start - 1, max_odor=peak,) session.add(crossing) # create this crossing's basic feature set crossing.feature_set_basic = models.CrossingFeatureSetBasic( position_x_entry=xs[crossing_list[1]], position_y_entry=ys[crossing_list[1]], position_z_entry=zs[crossing_list[1]], heading_xyz_entry=hs[crossing_list[1]], position_x_peak=xs[crossing_list[2]], position_y_peak=ys[crossing_list[2]], position_z_peak=zs[crossing_list[2]], heading_xyz_peak=hs[crossing_list[2]], position_x_exit=xs[crossing_list[3] - 1], position_y_exit=ys[crossing_list[3] - 1], position_z_exit=zs[crossing_list[3] - 1], heading_xyz_exit=hs[crossing_list[3] - 1], ) session.add(crossing) trial_ctr += 1 # commit after all crossings from all trials from a simulation have been added session.commit()
dataRaw = np.loadtxt('RunMay17-1.csv', delimiter=',') #truncating data data = dataRaw[dataTruncStart:dataTruncStart + numTimeSteps] #calibrations for sensors that are slightly off data[:, 1] -= 1 data[:, 2] -= 1 data[:, 3] += 1 data[:, 4] -= 1 data[:, 5] += 1 time = (data[:, 0] - data[0][0]) / 1000 averageTimeStep = np.mean([time[i] - time[i - 1] for i in range(1, len(time))]) temp = np.array([voltageToTemp(data[:, i] * 5 / 1024) for i in range(1, 6)]) smoothTemp = np.array([smooth(temp[i], 6) for i in range(len(temp))]) smoothTemp = smoothTemp.transpose() temp = temp.transpose() # print(temp.shape) Tamb = np.mean(temp[0]) # K radius = 0.0254 / 2 dx = ROD_LENGTH / (NUM_POINTS) dt = averageTimeStep K = 200 # J/(s*m*K) Kc = 12 # W/m^2/K epsilon = 0.1 powerIn = 13 # W # residual, observed, model = compare(temp.transpose(), K, Kc, epsilon, powerIn)
def smooth_background(self, smoothing_length, check=False): """ Performs 1D boxcar background smoothing, after masking the Lya and OI geocoronal emission lines. Parameters ---------- smoothing_length : int Background smoothing length (pixels). check : bool Option to plot a comparison between the smoothed and unsmoothed background and provide an option to change the smoothing length. Notes ----- The background level is interpolated across geocoronal lines and set to zero in regions of bad data quality. """ OI = (self.wavelength.value > 1300) & (self.wavelength.value < 1307.5) Lya = (self.wavelength.value > 1213) & (self.wavelength.value < 1218) mask = OI | Lya | self.mask decision = 'n' while decision != 'y': smoothed_background = np.zeros_like(self.background.value) smoothed_background[~mask] = smooth( self.background.value[~mask], smoothing_length) if len(smoothed_background[~mask]) != 0: smoothed_background = np.interp( self.wavelength, self.wavelength[~mask], smoothed_background[~mask]) smoothed_background[self.mask] = 0.0 else: smoothed_background[:] = 0.0 if check: print('Plotting background...') import matplotlib.pyplot as pl pl.plot(self.wavelength.value, self.background.value, color='k', label='unsmoothed') pl.plot(self.wavelength.value, smoothed_background, color='r', label='smoothed') pl.legend(fancybox=True) pl.xlabel('Wavelength ($\\AA$)') pl.ylabel('Background (count s$^{-1}$)') pl.show() decision = raw_input('Adopt this backgound? y/n : ') if decision == 'y': self.background = smoothed_background / s else: smoothing_length = int(raw_input( 'Enter new smoothing length : ')) else: decision = 'y' self.background = smoothed_background / s
def smoothdata(data, sigma=3): data = data.astype(np.float) d2 = np.zeros_like(data) for i in range(data.shape[0]): d2[i] = smooth(data[i], sigma) return d2