Exemplo n.º 1
0
def walk_current_folder(root_path, mars_opts):
    trials_to_run = []
    fullpaths = []
    for path, subdirs, filenames in os.walk(root_path):
        for numv, fname in enumerate(filenames):

            # start by looping over all movie files that have "Top" in their name
            cond1 = all(x not in fname
                        for x in ['.seq', '.avi', '.mpg', '.mp4'])
            cond2 = 'skipped' in path
            cond3 = 'Top' not in fname and '_t.seq' not in fname
            if cond1 | cond2 | cond3:
                continue

            if 'Top' in fname or '_t.seq' in fname:
                front_fname, top_fname, mouse_name = mof.get_names(fname)
                print(front_fname + ' ' + top_fname + ' ' + mouse_name)
                if top_fname != fname: continue
                fullpath_to_front = os.path.join(path, front_fname)
                fullpath_to_top = os.path.join(path, top_fname)
                cond1 = not (os.path.exists(fullpath_to_front))
                cond2 = mars_opts['doFront']
                cond3 = not mars_opts['doTop']
                cond4 = not mars_opts['doToppcf']
                # If the front video doesn't exist and we need it, continue.
                if (cond1 and cond2):  # |(cond3 and cond4):
                    continue
            elif 'Front' in fname:
                front_fname = fname
                front_fname, top_fname, mouse_name = mof.get_names(front_fname)
                # if front_fname != fname: continue
                fullpath_to_top = os.path.join(path, top_fname)
                fullpath_to_front = os.path.join(path, front_fname)
                cond1 = not (os.path.exists(fullpath_to_top))
                cond2 = mars_opts['doTop']
                cond3 = not mars_opts['doFront']
                cond4 = not mars_opts['doToppcf']

                if (cond1 and cond2 and cond4) | cond3:
                    continue
            else:
                # This is a movie file, but doesnt have "Top" or "Front" in it. Let's skip it.
                continue

            # Save the paths we want to use.
            mouse_trial = dict()
            mouse_trial['top'] = fullpath_to_top
            mouse_trial['front'] = fullpath_to_front
            trials_to_run.append(mouse_trial)
            fullpaths.append(fullpath_to_top)

    return trials_to_run, fullpaths
Exemplo n.º 2
0
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