def rename(path, ver_old): ver = mof.get_version_suffix() ver_old = 'v' + ver_old for dir, subdirs, filenames in os.walk(path): if ver_old in dir: old_dir = dir new_dir = dir.replace(ver_old, ver) if not os.path.exists(new_dir): os.rename(old_dir, new_dir) else: new_dir = dir for numv, fname in enumerate(filenames): if ver_old in fname: oldf = fname newf = fname.replace(ver_old, ver) if os.path.exists(os.path.join(new_dir, oldf)): os.rename(os.path.join(new_dir, oldf), os.path.join(new_dir, newf))
def __init__(self, parent=None): super(MainWindow, self).__init__(parent) #widnow setup resolution = QDesktopWidget().screenGeometry() self.screen_w = resolution.width() self.screen_h = resolution.height() self.setGeometry(0, 0, 650, 200) self.setWindowTitle('bento dumper' + mof.get_version_suffix()) self.setWindowIcon(QIcon('icons/run.png')) #center window qr = self.frameGeometry() cp = QtGui.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) #adjust size self.resize(self.screen_w / 2, self.screen_h / 16) self.Menu() self.Layout() central_widget = QtGui.QWidget() central_widget.setLayout(self.main_layout) self.setCentralWidget(central_widget)
def dump_bento(video_fullpath, output_suffix='', pose_file='', basepath=''): if not output_suffix: # Default suffix is just the version number. output_suffix = mof.get_version_suffix() video_path = os.path.dirname(video_fullpath) video_name = os.path.basename(video_fullpath) # Get the output folder for this specific mouse. output_folder = mof.get_mouse_output_dir( dir_output_should_be_in=video_path, video_name=video_name, output_suffix=output_suffix) mouse_name = output_folder.split('/')[-1] # if not movie_file: # movie_name = mouse_name + '_Top_J85.seq' # # movie_location = output_folder # movie_location = os.path.split(movie_location)[0] # movie_location = os.path.split(movie_location)[0] # # movie_file = os.path.join(movie_location, movie_name) # else: # movie_file = os.path.basename(movie_file) if not pose_file: pose_basename = mof.get_pose_no_ext(video_fullpath=video_fullpath, output_folder=output_folder, view='top', output_suffix=output_suffix) top_pose_fullpath = pose_basename + '.mat' # else: # pose_file = os.path.basename(pose_file) """ This function writes an xls with information for bento in it.""" wb = xlwt.Workbook(encoding='utf-8') ws1 = wb.add_sheet('Sheet1', cell_overwrite_ok=True) ws1.write(0, 0, basepath) # A1 ws1.write(0, 1, 'Ca framerate:') # B1 ws1.write(0, 2, 0) # C1 ws1.write(0, 3, 'Annot framerate:') # D1 ws1.write(0, 4, 30) # E1 ws1.write(0, 5, 'Multiple trials/Ca file:') # F1 ws1.write(0, 6, 0) # G1 ws1.write(0, 7, 'Multiple trails/annot file') # H1 ws1.write(0, 8, 0) # I1 ws1.write(0, 9, 'Includes behavior movies:') # J1 ws1.write(0, 10, 1) # K1 ws1.write( 0, 11, 'Offset (in seconds; positive values = annot starts before Ca):') # L1 ws1.write(0, 12, 0) # M1 ws1.write(1, 0, 'Mouse') # A2 ws1.write(1, 1, 'Sessn') # B2 ws1.write(1, 2, 'Trial') # C2 ws1.write(1, 3, 'Stim') # D2 ws1.write(1, 4, 'Calcium imaging file') # E2 ws1.write(1, 5, 'Start Ca') # F2 ws1.write(1, 6, 'Stop Ca') # G2 ws1.write(1, 7, 'FR Ca') # H2 ws1.write(1, 8, 'Alignments') # I2 ws1.write(1, 9, 'Annotation file') # J2 ws1.write(1, 10, 'Start Anno') # K2 ws1.write(1, 11, 'Stop Anno') # L2 ws1.write(1, 12, 'FR Anno') # M2 ws1.write(1, 13, 'Offset') # N2 ws1.write(1, 14, 'Behavior movie') # O2 ws1.write(1, 15, 'Tracking') # P2 ws1.write(2, 0, 1) # A2 ws1.write(2, 1, 1) # B2 ws1.write(2, 2, 1) # C2 ws1.write(2, 3, '') # D2 ws1.write(2, 4, '') # E2 ws1.write(2, 5, '') # F2 ws1.write(2, 6, '') # G2 ws1.write(2, 7, '') # H2 ws1.write(2, 8, '') # I2 ann = [ os.path.join(output_folder, f) for f in os.listdir(output_folder) if is_gt_annotation(f) | ('pred' in f) ] ann = sorted(ann) ann = [get_rel_path(annot_path, basepath) for annot_path in ann] ws1.write(2, 9, ';'.join(ann)) # J2 ws1.write(2, 10, '') # K2 ws1.write(2, 11, '') # L2 ws1.write(2, 12, '') # M2 ws1.write(2, 13, '') # N2 ws1.write(2, 14, get_rel_path(video_fullpath, basepath)) # O2 ws1.write(2, 15, get_rel_path(top_pose_fullpath, basepath)) # P2 bento_name = 'bento_' + output_suffix + '.xls' wb.save(os.path.join(output_folder, bento_name)) return 1
def dump_bento_across_dir(root_path): ''' This function makes a bento file for a specific directory.''' wb = xlwt.Workbook(encoding='utf-8') ws1 = wb.add_sheet('Sheet1', cell_overwrite_ok=True) ws1.write(0, 0, os.path.abspath(root_path)) # A1 ws1.write(0, 1, 'Ca framerate:') # B1 ws1.write(0, 2, 0) # C1 ws1.write(0, 3, 'Annot framerate:') # D1 ws1.write(0, 4, 30) # E1 ws1.write(0, 5, 'Multiple trials/Ca file:') # F1 ws1.write(0, 6, 0) # G1 ws1.write(0, 7, 'Multiple trails/annot file') # H1 ws1.write(0, 8, 0) # I1 ws1.write(0, 9, 'Includes behavior movies:') # J1 ws1.write(0, 10, 1) # K1 ws1.write( 0, 11, 'Offset (in seconds; positive values = annot starts before Ca):') # L1 ws1.write(0, 12, 0) # M1 ws1.write(0, 13, 'Includes tracking data:') ws1.write(0, 14, 0) ws1.write(0, 15, 'Includes audio files:') ws1.write(0, 16, 0) ws1.write(1, 0, 'Mouse') # A2 mouse_col_num = 0 session_col_num = 1 trial_col_num = 2 ws1.write(1, 1, 'Sessn') # B2 ws1.write(1, 2, 'Trial') # C2 ws1.write(1, 3, 'Stim') # D2 ws1.write(1, 4, 'Calcium imaging file') # E2 ws1.write(1, 5, 'Start Ca') # F2 ws1.write(1, 6, 'Stop Ca') # G2 ws1.write(1, 7, 'FR Ca') # H2 ws1.write(1, 8, 'Alignments') # I2 ws1.write(1, 9, 'Annotation file') # J2 annot_file_col_num = 9 ws1.write(1, 10, 'Start Anno') # K2 ws1.write(1, 11, 'Stop Anno') # L2 ws1.write(1, 12, 'FR Anno') # M2 ws1.write(1, 13, 'Offset') # N2 ws1.write(1, 14, 'Behavior movie') # O2 behavior_movie_col_num = 14 ws1.write(1, 15, 'Tracking') # P2 tracking_file_col_num = 15 ws1.write(1, 16, 'Audio file') audio_file_col_num = 16 ws1.write(1, 17, 'tSNE') # ws1.write(2, 0, 1) # A2 # ws1.write(2, 1, 1) # B2 # ws1.write(2, 2, 1) # C2 # ws1.write(2, 3, '') # D2 # ws1.write(2, 4, '') # E2 # ws1.write(2, 5, '') # F2 # ws1.write(2, 6, '') # G2 # ws1.write(2, 7, '') # H2 # ws1.write(2, 8, '') # I2 mouse_number = 0 row_num = 2 # Going through everything in this directory. audio_filenames = [] add_audio_count = 0 nonaudio_filenames = [] for path, subdirs, filenames in os.walk(root_path): for fname in sorted(filenames): fname = os.path.join(path, fname) if fname.endswith('.wav'): audio_filenames.append(fname) else: nonaudio_filenames.append(fname) audio_filenames = sorted(audio_filenames) nonaudio_filenames = sorted(nonaudio_filenames) for fname in nonaudio_filenames: try: cond1 = '.seq' not in fname cond2 = 'skipped' in path if (cond1) | cond2: continue if 'Top' in fname: front_fname, top_fname, mouse_name = mof.get_names(fname) fullpath_to_front = os.path.join(path, front_fname) fullpath_to_top = os.path.join(path, top_fname) else: # This is a seq file, but doesnt have "Top" or "Front" in it. Let's skip it. continue # Add their info to the bento file at the appropriate level. video_fullpath = fullpath_to_top output_suffix = '' video_path = os.path.dirname(video_fullpath) video_name = os.path.basename(video_fullpath) # Get the output folder for this specific mouse. output_folder = mof.get_mouse_output_dir( dir_output_should_be_in=video_path, video_name=video_name, output_suffix=output_suffix) _, _, mouse_name = mof.get_names(video_name=video_name) pose_basename = mof.get_pose_no_ext(video_fullpath=video_fullpath, output_folder=output_folder, view='top', output_suffix=output_suffix) top_pose_fullpath = pose_basename + '.json' same_path_ann = [ os.path.join(root_path, f) for f in os.listdir(root_path) if is_annotation_file(f, mouse_name) ] ann = [ os.path.join(output_folder, f) for f in os.listdir(output_folder) if is_annotation_file(f, mouse_name) ] ann = sorted(ann) ann = [get_normrel_path(f, root_path) for f in ann] pose_cond = os.path.exists(top_pose_fullpath) video_cond = os.path.exists(video_fullpath) should_write = (pose_cond and video_cond) if should_write: old_mouse_number = mouse_number mouse_number = get_mouse_number(video_fullpath) mouse_cond = (old_mouse_number == mouse_number) # TODO: Session condition sess_cond = (True) if mouse_cond and sess_cond: trial_count += 1 else: trial_count = 1 ws1.write(row_num, mouse_col_num, mouse_number) # A2 ws1.write(row_num, session_col_num, 1) # B2 ws1.write(row_num, trial_col_num, trial_count) # C2 ws1.write(row_num, annot_file_col_num, ';'.join(ann)) # J2 ws1.write(row_num, 10, '') # K2 ws1.write(row_num, 11, '') # L2 ws1.write(row_num, 12, '') # M2 ws1.write(row_num, 13, '') # N2 track_file = get_normrel_path(top_pose_fullpath, root_path) ws1.write(row_num, behavior_movie_col_num, get_normrel_path(fullpath_to_top, root_path)) # O2 ws1.write(row_num, tracking_file_col_num, track_file) # P2 row_num += 1 except Exception as e: print(e) error_msg = 'ERROR: ' + fname + ' has failed. ' + str(e) continue # End of try-except block # End of particular fname # End of the particular root_path last_row = row_num row_num = 2 for audio_file_count, audio_file in enumerate(audio_filenames): # Write the files in order. ws1.write(row_num + audio_file_count, audio_file_col_num + 2, get_normrel_path(audio_file, root_path)) bento_name = 'bento_' + mof.get_version_suffix() + '.xls' wb.save(os.path.join(root_path, bento_name)) return