<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import scipy . io <EOL> import collections <EOL> import copy <EOL> import warnings <EOL> import os <EOL> import matplotlib . pyplot as plt <EOL> from . . import config , utils <EOL> from . basic_worm import WormPartition <EOL> from . basic_worm import BasicWorm <EOL> from . pre_features import WormParsing <EOL> from . pre_features_helpers import WormParserHelpers <EOL> from . video_info import VideoInfo <EOL> class NormalizedWorm ( WormPartition ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , other = None ) : <EOL> """<STR_LIT>""" <EOL> WormPartition . __init__ ( self ) <EOL> if other is None : <EOL> self . video_info = VideoInfo ( ) <EOL> else : <EOL> attributes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> for a in attributes : <EOL> setattr ( self , a , copy . deepcopy ( getattr ( other , a ) ) ) <EOL> @ classmethod <EOL> def from_BasicWorm_factory ( cls , basic_worm , frames_to_plot_widths = [ ] ) : <EOL> """<STR_LIT>""" <EOL> nw = cls ( ) <EOL> bw = basic_worm <EOL> nw . video_info = bw . video_info <EOL> if bw . h_ventral_contour is not None : <EOL> nw . widths , h_skeleton = WormParsing . compute_skeleton_and_widths ( bw . h_ventral_contour , <EOL> bw . h_dorsal_contour , <EOL> frames_to_plot_widths ) <EOL> nw . angles = WormParsing . compute_angles ( h_skeleton ) <EOL> nw . skeleton = WormParserHelpers . normalize_all_frames_xy ( h_skeleton , <EOL> config . N_POINTS_NORMALIZED ) <EOL> nw . widths = WormParserHelpers . normalize_all_frames ( nw . widths , h_skeleton , <EOL> config . N_POINTS_NORMALIZED ) <EOL> nw . ventral_contour = WormParserHelpers . normalize_all_frames_xy ( bw . h_ventral_contour , <EOL> config . N_POINTS_NORMALIZED ) <EOL> nw . dorsal_contour = WormParserHelpers . normalize_all_frames_xy ( bw . h_dorsal_contour , <EOL> config . N_POINTS_NORMALIZED ) <EOL> nw . area = WormParsing . compute_area ( nw . contour ) <EOL> else : <EOL> nw . angles = WormParsing . compute_angles ( bw . skeleton ) <EOL> nw . skeleton = WormParserHelpers . normalize_all_frames_xy ( bw . h_skeleton , <EOL> config . N_POINTS_NORMALIZED ) <EOL> nw . ventral_contour = None <EOL> nw . dorsal_contour = None <EOL> nw . area = None <EOL> nw . length = WormParsing . compute_skeleton_length ( nw . skeleton ) <EOL> return nw <EOL> @ classmethod <EOL> def from_schafer_file_factory ( cls , data_file_path ) : <EOL> """<STR_LIT>""" <EOL> nw = cls ( ) <EOL> nw . video_info = VideoInfo ( ) <EOL> if ( not os . path . isfile ( data_file_path ) ) : <EOL> raise Exception ( "<STR_LIT>" + data_file_path ) <EOL> else : <EOL> data_file = scipy . io . loadmat ( data_file_path , <EOL> squeeze_me = True , <EOL> struct_as_record = False ) <EOL> staging_data = data_file [ '<STR_LIT:s>' ] <EOL> data_keys = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:x>' , <EOL> '<STR_LIT:y>' ] <EOL> for key in data_keys : <EOL> setattr ( nw , key , getattr ( staging_data , key ) ) <EOL> del ( nw . EIGENWORM_PATH ) <EOL> del ( nw . x ) <EOL> del ( nw . y ) <EOL> del ( nw . in_out_touches ) <EOL> nw . skeleton = nw . skeletons <EOL> nw . ventral_contour = nw . vulva_contours <EOL> nw . dorsal_contour = nw . non_vulva_contours <EOL> del ( nw . skeletons ) <EOL> del ( nw . vulva_contours ) <EOL> del ( nw . non_vulva_contours ) <EOL> nw . length = nw . lengths <EOL> nw . head_area = nw . head_areas <EOL> nw . tail_area = nw . tail_areas <EOL> nw . vulva_area = nw . vulva_areas <EOL> nw . non_vulva_area = nw . non_vulva_areas <EOL> del ( nw . lengths ) <EOL> del ( nw . head_areas ) <EOL> del ( nw . tail_areas ) <EOL> del ( nw . vulva_areas ) <EOL> del ( nw . non_vulva_areas ) <EOL> nw . video_info . frame_code = nw . frame_codes <EOL> del ( nw . frame_codes ) <EOL> nw . area = nw . head_area + nw . tail_area + nw . vulva_area + nw . non_vulva_area <EOL> del ( nw . head_area ) <EOL> del ( nw . tail_area ) <EOL> del ( nw . vulva_area ) <EOL> del ( nw . non_vulva_area ) <EOL> return nw <EOL> def get_BasicWorm ( self ) : <EOL> """<STR_LIT>""" <EOL> bw = BasicWorm . from_contour_factory ( self . ventral_contour , <EOL> self . dorsal_contour ) <EOL> bw . video_info = self . video_info <EOL> return bw <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def rotated ( self , theta_d ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> @ property <EOL> def centre ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _centre <EOL> except AttributeError : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , category = RuntimeWarning ) <EOL> self . _centre = np . nanmean ( self . skeleton , <NUM_LIT:0> , keepdims = False ) <EOL> return self . _centre <EOL> @ property <EOL> def angle ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _angle <EOL> except AttributeError : <EOL> s = self . skeleton <EOL> v = s [ <NUM_LIT> , : , : ] - s [ <NUM_LIT:0> , : , : ] <EOL> self . _angle = np . arctan ( v [ <NUM_LIT:1> , : ] / v [ <NUM_LIT:0> , : ] ) * ( <NUM_LIT> / np . pi ) <EOL> return self . _angle <EOL> @ property <EOL> def dropped_frames_mask ( self ) : <EOL> """<STR_LIT>""" <EOL> return np . isnan ( self . skeleton [ <NUM_LIT:0> , <NUM_LIT:0> , : ] ) <EOL> @ property <EOL> def centred_skeleton ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _centred_skeleton <EOL> except AttributeError : <EOL> s = self . skeleton <EOL> if s . size != <NUM_LIT:0> : <EOL> s_mean = np . ones ( s . shape ) * self . centre <EOL> self . _centred_skeleton = s - s_mean <EOL> else : <EOL> self . _centred_skeleton = s <EOL> return self . _centred_skeleton <EOL> @ property <EOL> def orientation_free_skeleton ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _orientation_free_skeleton <EOL> except AttributeError : <EOL> orientation = self . angle <EOL> a = - orientation * ( np . pi / <NUM_LIT> ) <EOL> rot_matrix = np . array ( [ [ np . cos ( a ) , - np . sin ( a ) ] , <EOL> [ np . sin ( a ) , np . cos ( a ) ] ] ) <EOL> s1 = np . rollaxis ( self . centred_skeleton , <NUM_LIT:1> ) <EOL> s1_rotated = [ ] <EOL> for frame_index in range ( self . num_frames ) : <EOL> s1_rotated . append ( rot_matrix [ : , : , frame_index ] . dot <EOL> ( s1 [ : , : , frame_index ] ) ) <EOL> s1_rotated = np . array ( s1_rotated ) <EOL> self . _orientation_free_skeleton = np . rollaxis ( np . rollaxis ( s1_rotated , <NUM_LIT:0> , <NUM_LIT:3> ) , <NUM_LIT:1> ) <EOL> return self . _orientation_free_skeleton <EOL> @ property <EOL> def num_frames ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _num_frames <EOL> except AttributeError : <EOL> self . _num_frames = self . skeleton . shape [ <NUM_LIT:2> ] <EOL> return self . _num_frames <EOL> def position_limits ( self , dimension , measurement = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> d = getattr ( self , measurement ) <EOL> if ( len ( d . shape ) < <NUM_LIT:3> ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return ( np . nanmin ( d [ : , dimension , : ] ) , <EOL> np . nanmax ( d [ : , dimension , : ] ) ) <EOL> @ property <EOL> def contour ( self ) : <EOL> return self . get_contour ( keep_redundant_points = True ) <EOL> @ property <EOL> def contour_without_redundant_points ( self ) : <EOL> return self . get_contour ( keep_redundant_points = False ) <EOL> def get_contour ( self , keep_redundant_points = True ) : <EOL> """<STR_LIT>""" <EOL> if keep_redundant_points : <EOL> return np . concatenate ( ( self . ventral_contour , <EOL> self . dorsal_contour [ : : - <NUM_LIT:1> , : , : ] ) ) <EOL> else : <EOL> return np . concatenate ( ( self . ventral_contour , <EOL> self . dorsal_contour [ - <NUM_LIT:2> : <NUM_LIT:0> : - <NUM_LIT:1> , : , : ] ) ) <EOL> @ property <EOL> def contour_x ( self ) : <EOL> return self . contour [ : , <NUM_LIT:0> , : ] <EOL> @ property <EOL> def contour_y ( self ) : <EOL> return self . contour [ : , <NUM_LIT:1> , : ] <EOL> @ property <EOL> def skeleton_x ( self ) : <EOL> return self . skeleton [ : , <NUM_LIT:0> , : ] <EOL> @ property <EOL> def skeleton_y ( self ) : <EOL> return self . skeleton [ : , <NUM_LIT:1> , : ] <EOL> @ property <EOL> def ventral_contour_x ( self ) : <EOL> return self . ventral_contour [ : , <NUM_LIT:0> , : ] <EOL> @ property <EOL> def ventral_contour_y ( self ) : <EOL> return self . ventral_contour [ : , <NUM_LIT:1> , : ] <EOL> @ property <EOL> def dorsal_contour_x ( self ) : <EOL> return self . dorsal_contour [ : , <NUM_LIT:0> , : ] <EOL> @ property <EOL> def dorsal_contour_y ( self ) : <EOL> return self . dorsal_contour [ : , <NUM_LIT:1> , : ] <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> attribute_list = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> return utils . compare_attributes ( self , other , attribute_list , <EOL> high_corr_value = <NUM_LIT> , <EOL> merge_nans_list = [ '<STR_LIT>' ] ) <EOL> def __repr__ ( self ) : <EOL> return utils . print_object ( self ) <EOL> def plot_path ( self , posture_index ) : <EOL> """<STR_LIT>""" <EOL> vc = self . ventral_contour [ posture_index , : , : ] <EOL> nvc = self . dorsal_contour [ posture_index , : , : ] <EOL> skeleton_x = self . skeleton [ posture_index , <NUM_LIT:0> , : ] <EOL> skeleton_y = self . skeleton [ posture_index , <NUM_LIT:1> , : ] <EOL> plt . scatter ( vc [ <NUM_LIT:0> , : ] , vc [ <NUM_LIT:1> , : ] ) <EOL> plt . scatter ( nvc [ <NUM_LIT:0> , : ] , nvc [ <NUM_LIT:1> , : ] ) <EOL> plt . scatter ( skeleton_x , skeleton_y ) <EOL> plt . gca ( ) . set_aspect ( '<STR_LIT>' , adjustable = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> def plot_posture ( self , frame_index ) : <EOL> """<STR_LIT>""" <EOL> vc = self . ventral_contour [ : , : , frame_index ] <EOL> nvc = self . dorsal_contour [ : , : , frame_index ] <EOL> skeleton = self . skeleton [ : , : , frame_index ] <EOL> plt . scatter ( vc [ : , <NUM_LIT:0> ] , vc [ : , <NUM_LIT:1> ] , c = '<STR_LIT>' ) <EOL> plt . scatter ( nvc [ : , <NUM_LIT:0> ] , nvc [ : , <NUM_LIT:1> ] , c = '<STR_LIT>' ) <EOL> plt . scatter ( skeleton [ : , <NUM_LIT:0> ] , skeleton [ : , <NUM_LIT:1> ] , c = '<STR_LIT>' ) <EOL> plt . gca ( ) . set_aspect ( '<STR_LIT>' , adjustable = '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> def plot_contour ( self , frame_index ) : <EOL> NormalizedWorm . plot_contour_with_labels ( <EOL> self . contour [ : , : , frame_index ] ) <EOL> @ staticmethod <EOL> def plot_contour_with_labels ( contour , frame_index = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> contour_x = contour [ : , <NUM_LIT:0> , frame_index ] <EOL> contour_y = contour [ : , <NUM_LIT:1> , frame_index ] <EOL> plt . plot ( contour_x , contour_y , '<STR_LIT:r>' , lw = <NUM_LIT:3> ) <EOL> plt . scatter ( contour_x , contour_y , s = <NUM_LIT> ) <EOL> labels = list ( str ( l ) for l in range ( <NUM_LIT:0> , len ( contour_x ) ) ) <EOL> for label_index , ( label , x , y ) , in enumerate ( <EOL> zip ( labels , contour_x , contour_y ) ) : <EOL> if label_index <= len ( contour_x ) // <NUM_LIT:2> - <NUM_LIT:1> : <EOL> xytext = ( <NUM_LIT:20> , - <NUM_LIT:20> ) <EOL> else : <EOL> xytext = ( - <NUM_LIT:20> , <NUM_LIT:20> ) <EOL> plt . annotate ( <EOL> label , xy = ( <EOL> x , y ) , xytext = xytext , textcoords = '<STR_LIT>' , ha = '<STR_LIT:right>' , va = '<STR_LIT>' , bbox = dict ( <EOL> boxstyle = '<STR_LIT>' , fc = '<STR_LIT>' , alpha = <NUM_LIT:0.5> ) , arrowprops = dict ( <EOL> arrowstyle = '<STR_LIT>' , connectionstyle = '<STR_LIT>' ) ) </s>
