def pop(self): # Populate the main table mice_data = load_excel_file(mice_log) in_table = list(self.fetch("mouse_id")) for mouse in mice_data: if mouse['ID'] in in_table: continue key = dict(mouse_id=mouse['ID'].upper(), strain=mouse['MouseLine'].upper(), sex=mouse['Gender'].upper()) self.insert1(key) # Populate the surgeries tables surgery_data = load_yaml(surgeries_file) in_table = list(self.fetch("mouse_id")) for mouse in surgery_data.keys(): if mouse not in in_table: continue surgery = surgery_data[mouse] for inj in [k for k in list(surgery.keys()) if 'injection' in k]: part_key = dict(mouse_id=mouse, target=surgery[inj]['target'], compound=surgery[inj]['compound']) manual_insert_skip_duplicate(self.Injection, part_key)
def _make_tuples(self, key): session_fld = get_session_folder(**key) recs = load_yaml(metadatafile)['sessions'][key['mouse']][ key['sess_name']] for n, rec in enumerate(sorted(recs)): if rec != key['rec_name']: continue # Get files rec_files = [f for f in os.listdir(session_fld) if rec in f] tracking_file = [ f for f in rec_files if 'dlc' in f.lower() and f.endswith('.h5') ] if len(tracking_file) != 1: raise ValueError else: tracking_file = os.path.join(session_fld, tracking_file[0]) # Insert entry into main class self.insert1(key) # Load and clean tracking data bp_tracking = prepare_tracking_data(tracking_file, median_filter=True, likelihood_th=0.9, fisheye=False, common_coord=False, compute=True) bones_tracking = compute_body_segments(bp_tracking, self.bsegments) # Insert into the bodyparts tracking for bp, tracking in bp_tracking.items(): bp_key = key.copy() bp_key['bp'] = bp bp_key['x'] = tracking.x.values bp_key['y'] = tracking.y.values bp_key['speed'] = tracking.speed.values bp_key['dir_of_mvmt'] = tracking.direction_of_movement.values bp_key['angular_velocity'] = tracking.angular_velocity.values self.BodyPartTracking.insert1(bp_key) # Insert into the body segment data for bone, (bp1, bp2) in self.bsegments.items(): segment_key = key.copy() segment_key['bp1'] = bp1 segment_key['bp2'] = bp2 segment_key['orientation'] = bones_tracking[bone][ 'orientation'].values segment_key['angular_velocity'] = bones_tracking[bone][ 'angular_velocity'].values self.BodySegmentTracking.insert1(segment_key)
def _make_tuples(self, key): session_fld = get_session_folder(**key) recs = load_yaml(metadatafile)['sessions'][key['mouse']][ key['sess_name']] for n, rec in enumerate(sorted(recs)): # Get files rec_files = [f for f in os.listdir(session_fld) if rec in f] videos = [ f for f in rec_files if f.endswith(".mp4") or f.endswith(".avi") ] if len(videos) != 1: if len(set([get_file_name(f) for f in videos])) == 1: video = get_file_name(videos[0]) + ".mp4" else: raise ValueError else: video = videos[0] temp_fld = os.path.join(self.temp_files_fld, key['mouse']) temp_rec_files = [f for f in os.listdir(temp_fld) if rec in f] ais = [fl for fl in temp_rec_files if fl == f"{rec}_Fiji-tag.hdf5"] if not ais: continue if len(ais) != 1: raise ValueError(f'Found ais: {ais}') else: ai = ais[0] # Open video and get number of frames nframes, width, height, fps, _ = get_video_params( get_cap_from_file(os.path.join(session_fld, video))) # Open AI file and get number of samples f, keys, subkeys, allkeys = open_hdf(os.path.join(session_fld, ai)) n_samples = len(f['AI']['0'][()]) rkey = key.copy() rkey['rec_name'] = rec rkey['rec_n'] = n rkey['videofile'] = video rkey['aifile'] = ai rkey['n_frames'] = nframes rkey['fps_behav'] = fps rkey['n_samples'] = n_samples manual_insert_skip_duplicate(self, rkey)
def _make_tuples(self, key): session_fld = get_session_folder(**key) recs = load_yaml(metadatafile)['sessions'][key['mouse']][ key['sess_name']] # Get each session's recordin AI file for n, rec in enumerate(sorted(recs)): rec_files = [f for f in os.listdir(session_fld) if rec in f] ais = [fl for fl in rec_files if fl == f"{rec}.hdf5"] if len(ais) != 1: raise ValueError else: ai = ais[0] f, keys, subkeys, allkeys = open_hdf(os.path.join(session_fld, ai)) # Get stim start and protocol name try: protocol_names = dict( f['Audio Stimulation'])['Audio Stimuli Names'][( )].decode("utf-8") except KeyError as err: return protocol_names = protocol_names.replace("[", "").replace( "]", "").replace("'", "") protocol_names = protocol_names.split(", u") protocol_names = [pc.split("\\\\")[-1] for pc in protocol_names] start_frames = np.where( dict(f['Audio Stimulation'])['Audio Stimuli Start Indices'][ ()] > 0)[0] if len(start_frames) != len(protocol_names): raise ValueError for sf, pc in zip(start_frames, protocol_names): skey = key.copy() skey['frame'] = sf skey['protocol_name'] = pc skey['rec_name'] = rec manual_insert_skip_duplicate(self, skey)
from fcutils.file_io.io import load_yaml from fcutils.plotting.utils import save_figure import matplotlib.pyplot as plt import matplotlib.image as mpimg from pathlib import Path from rich import print from pyinspect._colors import mocassin, orange print(f'[{mocassin}]Generating [{orange}]figures[/{orange}][{mocassin}]...\n') metadata = load_yaml('figures/composed_figures.yml') figs_folder = Path('figures') for figname, figs in metadata.items(): if len(figs) < 2: ncols, nros = 1, 1 elif len(figs) < 3: ncols, nrows = 2, 1 elif len(figs) < 5: ncols, nrows = 2, 2 elif len(figs) < 7: ncols, nrows = 3, 2 f, axarr = plt.subplots(ncols=ncols, nrows=nrows, figsize=(18, 13)) f.suptitle(figname) for ax in axarr.flatten(): ax.axis('off') for ax, (title, fig) in zip(axarr.flatten(), figs): ax.imshow(mpimg.imread(figs_folder / fig)) ax.set(title=title)
def populate(self): mice = load_yaml(metadatafile)['mice'] for mouse in mice: manual_insert_skip_duplicate(self, {'mouse': mouse})
def pop(self): exps = load_yaml(experiments_file) for exp in exps.keys(): key = dict(exp_name=exp, arena=exps[exp]['arena']) manual_insert_skip_duplicate(self, key)
def pop(self): exps = load_yaml(experiments_file) for exp in exps.keys(): for subexp in exps[exp]['subexps']: key = dict(exp_name=exp, subname=list(subexp.keys())[0]) manual_insert_skip_duplicate(self, key)