예제 #1
0
def extract_pose_wrapper(video_fullpath,
                         view,
                         doOverwrite,
                         progress_bar_signal='',
                         verbose=0,
                         output_suffix='',
                         mars_opts={},
                         max_frames=999999):
    video_path = os.path.dirname(video_fullpath)
    video_name = os.path.basename(video_fullpath)
    output_folder = mof.get_mouse_output_dir(
        dir_output_should_be_in=video_path,
        video_name=video_name,
        output_suffix=output_suffix)

    extract_pose(video_fullpath=video_fullpath,
                 output_folder=output_folder,
                 output_suffix=output_suffix,
                 view=view,
                 doOverwrite=doOverwrite,
                 progress_bar_signal=progress_bar_signal,
                 mars_opts=mars_opts,
                 verbose=verbose,
                 max_frames=max_frames)
    return
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
예제 #3
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
예제 #4
0
def create_video_results_wrapper(top_video_fullpath,classifier_path,
                                 progress_bar_signal,
                                 view='top',
                                 doOverwrite=0,output_suffix=''):

    try:
        video_fullpath = top_video_fullpath
        video_path = os.path.dirname(video_fullpath)
        video_name = os.path.basename(video_fullpath)

        model_type = mof.get_clf_type(classifier_path=classifier_path)


        # 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)

        # Get the pose and feature files' names.
        pose_basename = mof.get_pose_no_ext(video_fullpath=top_video_fullpath,
                                            output_folder=output_folder,
                                            view='top',
                                            output_suffix=output_suffix)
        top_pose_fullpath = pose_basename + '.json'

        # Get the name of the text file we're going to save to.
        classifier_savename = mof.get_classifier_savename(video_fullpath=top_video_fullpath,
                                                          output_folder=output_folder,
                                                          view=view,
                                                          classifier_path=classifier_path,
                                                          output_suffix=output_suffix,model_type=model_type)

        # print(classifier_savename)
        predictions_exists=os.path.exists(classifier_savename)
        top_pose_exist = os.path.exists(top_pose_fullpath)
        video_savename = classifier_savename[:-3]+'mp4'
        video_exists=os.path.exists(video_savename)
        # print(video_savename)
        if not top_pose_exist:
            raise ValueError("No pose has been extracted for this video!")
        if not predictions_exists:
            raise ValueError("No behavior classified for this video!")
        if (not video_exists) | doOverwrite:
            pool = mp.Pool(1)
            manager = mp.Manager()
            queue_for_progress_bar = manager.Queue(20)
            result = pool.apply_async(create_mp4_prediction, (video_fullpath,top_pose_fullpath,classifier_savename,video_savename, queue_for_progress_bar))
            still_frames_left = True
            while still_frames_left:
                progress_bar_input = queue_for_progress_bar.get()
                if progress_bar_input:
                    still_frames_left = True
                    progress_bar_signal.emit(progress_bar_input[0], progress_bar_input[1])
                else:
                    still_frames_left = False
                    break

            pool.close()
            pool.join()
            result.get()
        else:
            print("4 - Video already exists")
            return

    except Exception as e:
        print(e)
        raise (e)
    return
def classify_actions_wrapper(top_video_fullpath,
                             front_video_fullpath,
                             doOverwrite,
                             view,
                             classifier_path='',
                             output_suffix=''):
    try:
        video_fullpath = top_video_fullpath
        video_path = os.path.dirname(video_fullpath)
        video_name = os.path.basename(video_fullpath)

        model_type = mof.get_clf_type(classifier_path=classifier_path)

        # 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)

        # Get the name of the features you should be loading.
        front_feat_basename = mof.get_feat_no_ext(
            video_fullpath=top_video_fullpath,
            output_folder=output_folder,
            view='front',
            output_suffix=output_suffix)

        top_feat_basename = mof.get_feat_no_ext(
            video_fullpath=top_video_fullpath,
            output_folder=output_folder,
            view='top',
            output_suffix=output_suffix)

        # Get the name of the text file we're going to save to.
        classifier_savename = mof.get_classifier_savename(
            video_fullpath=top_video_fullpath,
            output_folder=output_folder,
            view=view,
            classifier_path=classifier_path,
            output_suffix=output_suffix,
            model_type=model_type)

        # Make their matfile names.
        if 'pcf' in classifier_path:
            # more hiding pcf features shenanigans, hopefully the last time?
            # top_feat_basename = top_feat_basename[:-4] + 'pcf_'+top_feat_basename[-4:]
            front_feat_name = front_feat_basename + '_wnd.npz'
            top_feat_name = top_feat_basename + '_wnd.npz'
        else:
            front_feat_name = front_feat_basename + '_wnd.npz'
            top_feat_name = top_feat_basename + '_wnd.npz'

        # Check that features exist.
        top_feats_exist = os.path.exists(top_feat_name)
        front_feats_exist = os.path.exists(front_feat_name)
        classifier_savename_exist = os.path.exists(classifier_savename)
        # if False:
        if (not classifier_savename_exist) | doOverwrite:

            # If the proper features don't exist, raise an exception. Otherwise, load them.
            if (view == 'top') or (view == 'toppcf'):
                if top_feats_exist:
                    print("loading top features")
                    features = mcm.load_features_from_filename(
                        top_feat_name=top_feat_name)
                else:
                    print(
                        "Top features don't exist in the proper format/location. Aborting..."
                    )
                    raise ValueError(
                        top_feat_name.split('/')[-1] + " doesn't exist.")

            elif view == 'topfront':
                if not top_feats_exist:
                    print(
                        "Top features don't exist in the proper format/location. Aborting..."
                    )
                    raise ValueError(
                        top_feat_name.split('/')[-1] + " doesn't exist.")
                elif not front_feats_exist:
                    print(
                        "Front features don't exist in the proper format/location. Aborting..."
                    )
                    raise ValueError(
                        front_feat_name.split('/')[-1] + " doesn't exist.")
                else:  # Both featuresets exist.
                    print("loading top and front features")
                    features = mcm.load_features_from_filename(
                        top_feat_name=top_feat_name,
                        front_feat_name=front_feat_name)
            else:
                print("Classifier available for top or top and front view")
                raise ValueError('Classifier not available for only fron view')

            # Classify the actions (get the labels back).
            print("predicting labels")
            predicted_labels, predicted_labels_interaction = mcm.predict_labels(
                features, classifier_path)

            # Dump the labels into the Caltech Behavior Annotator format.
            mcm.dump_labels_CBA(predicted_labels, predicted_labels_interaction,
                                classifier_savename)
        else:
            print("3 - Predictions already exist")
            return

    except Exception as e:
        print(e)
        raise (e)
    return