def main(): """ Load the skeleton and other basic data from a worm HDF5 file, optionally animate it using matplotlib, and also create the features information by deriving them from the basic data, to annotate the animation with. """ # Force warnings to be errors warnings.simplefilter("error") # Load from file a normalized worm, as calculated by Schafer Lab code base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) schafer_nw_file_path = os.path.join(base_path, "example_video_norm_worm.mat") nw = mv.NormalizedWorm.from_schafer_file_factory(schafer_nw_file_path) # Placeholder for video metadata nw.video_info.video_name = "Example name" # We need to create WormFeatures to get the motion codes # (telling us in each frame if the worm is moving forward, backward, etc, # which is nice to have so we can annotate the plot with that info) wf = mv.WormFeatures(nw) motion_codes = wf.locomotion.motion_mode # Plot an animation of the worm and its motion codes wp = mv.NormalizedWormPlottable(nw, motion_codes) wp.show()
def main(): #warnings.filterwarnings('error') warnings.simplefilter(action='ignore', category=RuntimeWarning) # Set up the necessary file paths for file loading base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) matlab_generated_file_path = os.path.join( base_path, 'example_video_feature_file.mat') data_file_path = os.path.join(base_path, "example_video_norm_worm.mat") # Load the normalized worm from file nw = mv.NormalizedWorm.from_schafer_file_factory(data_file_path) # Generate the OpenWorm movement validation repo version of the features openworm_features = mv.WormFeatures(nw) # Load the Schafer Lab Matlab-code-generated features from disk matlab_worm_features = \ mv.WormFeatures.from_disk(matlab_generated_file_path) df = openworm_features.get_DataFrame() movement_df = openworm_features.get_movement_DataFrame() # investigating path dwelling. plt.plot(df.ix[0].data_array) plt.plot(np.sort(df.ix[0].data_array)) plt.show()
def example_virtual_worm_pipeline(data_file_path): """ This depicts an example of how the data would flow from the virtual worm to the features calculation and plotting This 'virtual' pipeline is simpler because there are no blocks to stitch and also we don't have to verify that our figures are the same as the Schafer figures This might be obsolete now [@Michael Currie, 22 Sep 2014] """ vw = movement_validation.BasicWormData(data_file_path) # NormalizedWorm can load either: # --> a 'VirtualWorm' file (wrapped in a class) or # --> a 'Schafer' file (wrapped in a class) nw = movement_validation.NormalizedWorm('VirtualWorm', vw) wf = movement_validation.WormFeatures(nw) wp = movement_validation.WormPlotter(wf) wp.show()
def video_to_features(video_path, output_path): """ Go from a path to a .avi file to features Parameters -------------------- vid_path: string Path to the video file. output_path: string Path to save the feature file to. Will overwrite if necessary. Returns -------------------- A StatisticsManager instance """ # The segmentation algorithm requires a temporary folder to generate # some intermediate files. The folder will be deleted at the end. tmp_root = os.path.join(os.path.abspath(output_path), 'TEMP_DATA') video = mv.VideoFile(video_path, tmp_root) frame_iterator = video.obtain_frame_iterator() worm_frame_list = [] for frame in frame_iterator: bool_frame = frame.process() segmented_worm_frame = mv.SegmentedWorm(bool_frame) worm_frame_list.append(segmented_worm_frame) worm_spec = mv.MinimalWormSpecification(worm_frame_list) worm_pre_features = mv.WormPreFeatures(worm_spec) nw = mv.NormalizedWorm(worm_spec.pre_features) if hasattr(video, 'video_info'): worm_features = mv.WormFeatures(nw, video.video_info) else: warnings.warn("VideoFile has not yet implemented video_info, " + \ "using default values.") #The frame rate is somewhere in the video info. Ideally this would all come #from the video parser eventually vi = mv.VideoInfo('Example Video File', 25.8398) worm_features = mv.WormFeatures(nw, vi) worm_features.write_to_disk(output_path)
def main(): # Set up the necessary file paths for file loading base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) data_file_path = os.path.join(base_path,"example_video_norm_worm.mat") # Load the normalized worm from file nw = mv.NormalizedWorm.from_schafer_file_factory(data_file_path) # Generate the OpenWorm movement validation repo version of the features fpo = mv.FeatureProcessingOptions() fpo.disable_feature_sections(['morphology']) openworm_features = mv.WormFeatures(nw, fpo) openworm_features.timer.summarize()
def main(): # TODO: #h_ventral_contour, h_dorsal_contour, video_info = \ # Kezhi_CV_Algorithm('test.avi') #experiment_info = mv.ExperimentInfo.from_CSV_factory('test.csv') #bw = BasicWorm.from_h_contour_factory(h_ventral_contour, h_dorsal_contour) #bw.video_info = video_info # TEMPORARY---------------------------- base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) schafer_bw_file_path = os.path.join( base_path, "example_contour_and_skeleton_info.mat") bw = mv.BasicWorm.from_schafer_file_factory(schafer_bw_file_path) # ------------------------------------- # TODO: get this line to work: #bw = example_worms() nw = mv.NormalizedWorm.from_BasicWorm_factory(bw) # DEBUG wp = mv.NormalizedWormPlottable(nw, interactive=False) wp.show() return wf = mv.WormFeatures(nw) base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) control_path = os.path.join(base_path, '30m_wait', 'R') experiment_files = [wf, wf] control_files = get_matlab_filepaths(control_path) # Compute histograms on our files experiment_histograms = mv.HistogramManager(experiment_files) control_histograms = mv.HistogramManager(control_files) experiment_histograms.plot_information() # Compute statistics stat = mv.StatisticsManager(experiment_histograms, control_histograms) stat[0].plot(ax=None, use_alternate_plot=True) print("Nonparametric p and q values are %.2f and %.2f, respectively." % (stat.min_p_wilcoxon, stat.min_q_wilcoxon))
def create_figure3(): # Load from file a normalized worm, as calculated by Schafer Lab code base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) schafer_nw_file_path = os.path.join(base_path, "example_video_norm_worm.mat") nw = mv.NormalizedWorm.from_schafer_file_factory(schafer_nw_file_path) # Placeholder for video metadata nw.video_info.video_name = "Example name" # We need to create WormFeatures to get the motion codes # (telling us in each frame if the worm is moving forward, backward, etc, # which is nice to have so we can annotate the plot with that info) wf = mv.WormFeatures(nw) motion_codes = wf.locomotion.motion_mode # Plot an animation of the worm and its motion codes wp = mv.NormalizedWormPlottable(nw, motion_codes)
def test_features(): """ Compare Schafer-generated features with our new code's generated features """ # Set up the necessary file paths for file loading #---------------------- base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) matlab_generated_file_path = os.path.join( base_path, 'example_video_feature_file.mat') data_file_path = os.path.join(base_path, "example_video_norm_worm.mat") # OPENWORM #---------------------- # Load the normalized worm from file nw = mv.NormalizedWorm.from_schafer_file_factory(data_file_path) # Generate the OpenWorm movement validation repo version of the features openworm_features = mv.WormFeatures(nw) # SCHAFER LAB #---------------------- # Load the Matlab codes generated features from disk matlab_worm_features = \ mv.WormFeatures.from_disk(matlab_generated_file_path) # COMPARISON #---------------------- # Show the results of the comparison print("\nComparison of computed features to those computed with " "old Matlab code:") categories = ['locomotion', 'posture', 'morphology', 'path'] # Compare each feature category to make sure they are equal for category in categories: is_test_passed = (getattr(matlab_worm_features, category) == getattr(openworm_features, category)) print(str.capitalize(category) + ": " + str(is_test_passed)) assert (is_test_passed) print("\nDone validating features")
def main(): # Create a normalized worm from a hardcoded example location #------------------------------------------------------------------- nw = example_nw() # movement_validation.NormalizedWorm # From the basic information in normalized_worm, # create an instance of WormFeatures, which contains all our features data. wf = movement_validation.WormFeatures(nw) # Plotting demonstration # movement_validation.plot_frame_codes(nw) # plt.tight_layout() # I just saved a plaintext file with the motioncodes extracted from # the features result file, by viewing the results file using HDFView #motion_codes = np.genfromtxt('motion_codes.txt', delimiter='\n') #wp = movement_validation.WormPlotter(nw, motion_codes, interactive=False) wp = movement_validation.WormPlotter(nw, interactive=False) wp.show()
def example_real_worm_pipeline(data_file_path, eigen_worm_file_path, other_data_file_path): """ This depicts an example of how the data would flow from the Schafer real worm data to the features calculation and plotting At two places, we verify that our figures are the same as the Schafer figures... This might be obsolete now [@Michael Currie, 22 Sep 2014] """ snw_blocks = movement_validation.SchaferNormalizedWormBlocks(data_file_path, eigen_worm_file_path) snw = snw_blocks.stitch() type(snw) # *** returns <class 'SchaferNormalizedWorm'> # NormalizedWorm can load either: # --> a 'VirtualWorm' file (wrapped in a class) or # --> a 'Schafer' file (wrapped in a class) nw = movement_validation.NormalizedWorm('Schafer', snw) nw.compare_with_schafer(snw) #*** returns True, hopefully! wf = movement_validation.WormFeatures(nw) sef = movement_validation.SchaferExperimentFile(other_data_file_path) wf.compare_with_schafer(sef) #*** returns True, hopefully! wp = movement_validation.WormPlotter(wf) wp.show() # show the plot