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 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 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 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:") feature_correlations = {} # Compare each of the 93 features to make sure they are equal for feature in openworm_features.features: f_matlab = matlab_worm_features.get_features(feature.name) f_openworm = openworm_features.get_features(feature.name) cur_corr = mv.utils.correlation(f_matlab.value, f_openworm.value, feature.name) # Handle the case where we did not pass arrays to correlation and # therefore got back None if not cur_corr: cur_corr = 0 # DEBUG: suppress this assertion since it is not passing # As of June 2017, however, we've validated that the differences seen # are acceptable for various reasons. #assert(cur_corr >= 0.99) feature_correlations[feature.name] = cur_corr print(feature.name, ": %.2f" % cur_corr) print("\nDone validating features")
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 version of the features fpo = mv.FeatureProcessingOptions() fpo.disable_feature_sections(['morphology']) openworm_features = mv.WormFeatures(nw, fpo) openworm_features.timer.summarize()
def getFeatStats(worm, wStats): if not isinstance(wStats, WormStats): wStats = WormStats() worm_openworm = worm.to_open_worm() assert worm_openworm.skeleton.shape[1] == 2 worm_features = mv.WormFeatures(worm_openworm) def _get_worm_stat(func): # calculate the mean value of each feature worm_stat = wStats.getWormStats(worm_features, func) for field in wStats.extra_fields: worm_stat[field] = getattr(worm, field) return worm_stat worm_stats = {stat: _get_worm_stat(FUNC_FOR_DIV[stat]) for stat in FUNC_FOR_DIV} return worm_features, worm_stats
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 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 getFeaturesOpenWorm(worm, wStats=[]): if not isinstance(wStats, WormStatsClass): wStats = WormStatsClass() #let's make a copy of the skeletons before chaning axis skeletons = worm.skeleton.copy() #IMPORTANT change axis to an openworm format before calculating features assert worm.skeleton.shape[2] == 2 worm.changeAxis() #OpenWorm feature calculation assert worm.skeleton.shape[1] == 2 worm_features = mv.WormFeatures(worm) #convert the timeseries features into a recarray tot_frames = worm.timestamp.size timeseries_data = np.full(tot_frames, np.nan, wStats.feat_timeseries_dtype) timeseries_data['timestamp'] = worm.timestamp timeseries_data['worm_index'] = worm.worm_index timeseries_data['motion_modes'] = worm_features._features[ 'locomotion.motion_mode'].value for feat in wStats.feat_timeseries: feat_obj = wStats.features_info.loc[feat, 'feat_name_obj'] timeseries_data[feat] = worm_features._features[feat_obj].value #convert the events features into a dictionary events_data = {} for feat in wStats.feat_events: feat_obj = wStats.features_info.loc[feat, 'feat_name_obj'] events_data[feat] = worm_features._features[feat_obj].value #calculate the mean value of each feature worm_stats = wStats.getWormStats(worm_features, np.mean) worm_stats['n_frames'] = worm.n_frames worm_stats['worm_index'] = worm.worm_index worm_stats['n_valid_skel'] = worm.n_valid_skel return timeseries_data, events_data, worm_stats, skeletons
def main(): """ 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 version of the features print('Computing example features from normalized worm') openworm_features = mv.WormFeatures(nw) # SCHAFER LAB #---------------------- print('Loading example features from disk') matlab_worm_features = mv.WormFeatures.from_disk( matlab_generated_file_path) # COMPARISON #---------------------- # TODO: I think we should add an iteration method for worm_features for feature in openworm_features: other_feature = matlab_worm_features.get_features(feature.name) is_same = feature == other_feature if not is_same: print('Feature mismatch: %s' % feature.name) #import pdb # pdb.set_trace() print("\nDone validating features")
def main(): """ 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) 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) specs = mv.get_feature_specs() # '^locomotion\.' => string that starts with 'locomotion.' loco_specs = specs[specs['feature_name'].str.contains('^locomotion\.')] #loco_names = loco_specs['feature_name'] # Generate the OpenWorm movement validation repo version of the features print('Computing example features from normalized worm') openworm_features = mv.WormFeatures(nw, specs=loco_specs) f = openworm_features.features feature_names = [x.name for x in f] # We might want to expose an interface that returns a list of features # that optionally allows temporary features and/or even non-requested # features d = openworm_features._features all_feature_names = [d[x].name for x in d] # all_feature_names contains morphology.length which was not requested # Not sure what test to run ... # Let's marvel at the name filtering!!!! print('All done with test_spec_filtering.py')
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 #feature_spec = mv.WormFeatures.get_feature_spec(extended=True) VERBOSE = True RUN_FULL = False #If true the normalized worm is computed from the basic worm input #If false it is preloaded (much faster) # 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") base_path = os.path.abspath(mv.user_config.EXAMPLE_DATA_PATH) control_path = os.path.join(base_path, '30m_wait', 'R') control_files = get_matlab_filepaths(control_path) if len(control_files) == 0: raise Exception( 'Missing the example files in the "<example_root>/30m_wait/R" diretory' ) nw_file_path = os.path.join(base_path, "example_video_norm_worm.mat") #example_video_norm_worm.mat # ------------------------------------- # TODO: get this line to work: #bw = example_worms() #TODO - we could support tierpsy calls which are probably better ... if RUN_FULL: print('Converting bacic worm to normalized worm') bw = mv.BasicWorm.from_schafer_file_factory(schafer_bw_file_path) nw = mv.NormalizedWorm.from_BasicWorm_factory(bw) else: nw = mv.NormalizedWorm.from_schafer_file_factory(nw_file_path) # DEBUG #wp = mv.NormalizedWormPlottable(nw, interactive=False) # wp.show() # return print('Extracting features from normalized worm') wf = mv.WormFeatures(nw) experiment_features = [wf, wf] # Compute histograms on our files experiment_histograms = mv.HistogramManager(experiment_features, verbose=VERBOSE) #Note this doesn't allow feature expansion since we are loading in histograms #We would need to load first, maniuplate, then pass in to histogram creator control_histograms = mv.HistogramManager(control_files, verbose=VERBOSE) experiment_histograms.plot_information() print('Computing statistics on histograms') # Compute statistics stat = mv.StatisticsManager(experiment_histograms, control_histograms) #print(stat[0]) stat[0].plot(ax=None, use_alternate_plot=True) print("Nonparametric p and q values are %.5f and %.5f, respectively." % (stat.min_p_wilcoxon, stat.min_q_wilcoxon))
for file in files: if file.endswith(".mat"): print(file) file_ind = file_ind + 1 # Load a "basic" worm from a file #bw = mv.BasicWorm.from_schafer_file_factory("example_contour_and_skeleton_info.mat") bw = mv.BasicWorm.from_schafer_file_factory(root + file) # Normalize the basic worm nw = mv.NormalizedWorm.from_BasicWorm_factory(bw) ## Plot this normalized worm #wp = mv.NormalizedWormPlottable(nw, interactive=False) #wp.show() # Obtain features wf = mv.WormFeatures(nw) wf_ess = {} # if read None data, use 0 instead for ind in range(fea_no): wf_ess[feature_field[ind]] = wf._features[ feature_field[ind]].value if (wf_ess[feature_field[ind]]) is None: wf_ess[feature_field[ind]] = 0 # save the essential features to hdf5 file with h5py.File(root + file[:-9] + '_esFea.h5', 'w') as hf: for k, v in wf_ess.items(): hf.create_dataset(k, data=v) # plt.plot(wf_ess[feature_field[21]])