def collect_data(self, session, min_quality='fair', alpha_color=False, tetrodes=None, plot_track=True): """Generate per-lap plots for each cluster of spikes on a trajectory """ if type(session) is tuple and len(session) == 3: from scanr.session import SessionData session = SessionData(rds=session) traj = session.trajectory ts, x, y = traj.ts, traj.x, traj.y self.out("Quality filter: at least %s" % min_quality) criteria = AND(PrincipalCellCriteria, get_min_quality_criterion(min_quality)) if tetrodes is not None: criteria = AND(criteria, get_tetrode_restriction_criterion(tetrodes)) for tc, data, lap, ax in self.get_plot(session, criteria): start, end = lap t, xy = time_slice_sample(ts, np.c_[x, y], start=start, end=end) lap_x, lap_y = xy.T t, xy = time_slice_sample(data.spikes, np.c_[data.x, data.y], start=start, end=end) spike_x, spike_y = xy.T ax.plot(lap_x, lap_y, **TRAJ_FMT) if len(spike_x): if alpha_color: alpha = xy_to_deg_vec(spike_x, spike_y) ax.scatter(spike_x, spike_y, c=alpha, vmin=0, vmax=360, **SPIKE_FMT) else: ax.scatter(spike_x, spike_y, **SPIKE_FMT) if plot_track: plot_track_underlay(ax, lw=0.5, ls='dotted') ax.axis('equal') ax.set_axis_off()
def compute_spatial_information(rds, which): data = SessionData(rds=rds) score = SpatialInformationScore(random=False, fixed_spacing=0.5, min_offset=15.0, reverse_for_shuffle=True, measure=which, x_bins=48) res = [] for tc in sorted(data.clusts.keys()): cell_res = dict(rds=rds, tc=tc) cluster = data.cluster_data(tc) cell_res['I'] = score.compute(data, cluster) cell_res['p'] = score.pval(data, cluster) cell_res['N_running'] = cluster.N_running res.append(cell_res) return res
def create_scan_cell_table(self, scan_phase='scan'): """For every scan–cell pair, compute the relative index of cell firing that occurred during the scan and previous cell firing on the track """ scan_table_description = { 'id': tb.UInt32Col(pos=1), 'scan_id': tb.UInt16Col(pos=2), 'rat': tb.UInt16Col(pos=3), 'day': tb.UInt16Col(pos=4), 'session': tb.UInt16Col(pos=5), 'session_start_angle': tb.FloatCol(pos=6), 'session_end_angle': tb.FloatCol(pos=7), 'tc': tb.StringCol(itemsize=8, pos=8), 'type': tb.StringCol(itemsize=4, pos=9), 'expt_type': tb.StringCol(itemsize=4, pos=10), 'area': tb.StringCol(itemsize=4, pos=11), 'subdiv': tb.StringCol(itemsize=4, pos=12), 'duration': tb.FloatCol(pos=13), 'magnitude': tb.FloatCol(pos=14), 'angle': tb.FloatCol(pos=15) } def add_scan_index_column_descriptors(descr): pos = 16 for name in ScanIndex.AllNames: descr[name] = tb.FloatCol(pos=pos) pos += 1 add_scan_index_column_descriptors(scan_table_description) data_file = self.get_data_file(mode='a') scan_cell_table = create_table(data_file, '/', 'scan_cell_info', scan_table_description, title='Metadata for Scan-Cell Pairs') scan_cell_table._v_attrs['scan_phase'] = scan_phase row = scan_cell_table.row row_id = 0 scans_table = get_node('/behavior', 'scans') sessions_table = get_node('/metadata', 'sessions') tetrodes_table = get_node('/metadata', 'tetrodes') cornu_ammonis_query = '(area=="CA1")|(area=="CA3")' hippocampal_datasets = unique_datasets('/metadata', 'tetrodes', condn=cornu_ammonis_query) quality_place_cells = AND(get_min_quality_criterion(self.min_quality), PlaceCellCriteria) index = ScanIndex(scan_phase=scan_phase) for dataset in hippocampal_datasets: dataset_query = '(rat==%d)&(day==%d)' % dataset hippocampal_tetrodes = unique_values( tetrodes_table, column='tt', condn='(%s)&(%s)' % (dataset_query, cornu_ammonis_query)) cluster_criteria = AND( quality_place_cells, get_tetrode_restriction_criterion(hippocampal_tetrodes)) for maze in get_maze_list(*dataset): rds = dataset + (maze, ) session = SessionData(rds=rds) place_cells = session.get_clusters(cluster_criteria) session_start_angle = np.median( session.trajectory.alpha_unwrapped[:5]) session_end_angle = np.median( session.trajectory.alpha_unwrapped[-5:]) self.out('Computing scan index for %s...' % session.data_group._v_pathname) for scan in scans_table.where(session.session_query): self.out.printf('|', color='cyan') for cell in place_cells: cluster = session.cluster_data(cell) tt, cl = parse_cell_name(cluster.name) tetrode = get_unique_row( tetrodes_table, '(rat==%d)&(day==%d)&(tt==%d)' % (rds[0], rds[1], tt)) row['id'] = row_id row['scan_id'] = scan['id'] row['rat'], row['day'], row['session'] = rds row['session_start_angle'] = session_start_angle row['session_end_angle'] = session_end_angle row['tc'] = cluster.name row['type'] = session.attrs['type'] row['expt_type'] = get_unique_row( sessions_table, session.session_query)['expt_type'] row['area'] = tetrode['area'] row['subdiv'] = tetrode['area'] + tetrode['subdiv'][:1] row['angle'] = session.F_('alpha_unwrapped')( session.T_(scan['start'])) row['duration'] = scan['duration'] row['magnitude'] = scan['magnitude'] for index_name in ScanIndex.AllNames: row[index_name] = index.compute( index_name, session, scan, cluster) self.out.printf('.', color='green') row_id += 1 row.append() if row_id % 100 == 0: scan_cell_table.flush() self.out.printf('\n') scan_cell_table.flush() self.out('Finished creating %s.' % scan_cell_table._v_pathname)
#!/usr/bin/env python from pylab import * import scanr.tracking from scanr.session import SessionData from ..tools.stats import KT_estimate from ..tools.filters import circular_blur import numpy as np reload(scanr.tracking) from scanr.tracking import SpatialInformationScore data = SessionData(rds=(97, 6, 2)) cluster = data.cluster_data((1, 2)) score = SpatialInformationScore(measure='olypher', x_bins=48, reverse_for_shuffle=True, random=False, fixed_spacing=0.5, min_offset=30.0) H_xk = score.H_xk(data, cluster) P_k = KT_estimate(H_xk.sum(axis=0)) P_k_x = (H_xk.astype('d') + 0.5) / (H_xk.sum(axis=1)[:, np.newaxis] + 0.5 * H_xk.shape[1]) I_pos = np.sum(P_k_x * np.log2(P_k_x / P_k), axis=1) I = circular_blur(I_pos, 360. / score.x_bins) print I.max()