<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import scipy as sp <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . patches as mpatches <EOL> import matplotlib as mpl <EOL> import seaborn as sns <EOL> import pandas as pd <EOL> from . . import utils <EOL> from . histogram import Histogram <EOL> class StatisticsManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exp_histogram_manager , ctl_histogram_manager ) : <EOL> """<STR_LIT>""" <EOL> assert ( len ( exp_histogram_manager ) == <EOL> len ( ctl_histogram_manager ) ) <EOL> num_features = len ( exp_histogram_manager ) <EOL> self . worm_statistics_objects = np . array ( [ None ] * num_features ) <EOL> for feature_index in range ( num_features ) : <EOL> self . worm_statistics_objects [ feature_index ] = WormStatistics ( <EOL> exp_histogram_manager [ feature_index ] , <EOL> ctl_histogram_manager [ feature_index ] ) <EOL> self . q_studentst_array = utils . compute_q_values ( self . p_studentst_array ) <EOL> self . q_wilcoxon_array = utils . compute_q_values ( self . p_wilcoxon_array ) <EOL> for feature_index in range ( num_features ) : <EOL> self . worm_statistics_objects [ feature_index ] . q_studentst = self . q_studentst_array [ feature_index ] <EOL> self . worm_statistics_objects [ feature_index ] . q_wilcoxon = self . q_wilcoxon_array [ feature_index ] <EOL> def __getitem__ ( self , index ) : <EOL> return self . worm_statistics_objects [ index ] <EOL> @ property <EOL> def p_studentst_array ( self ) : <EOL> return np . array ( [ x . p_studentst for x in self . worm_statistics_objects ] ) <EOL> @ property <EOL> def p_wilcoxon_array ( self ) : <EOL> return np . array ( [ x . p_wilcoxon for x in self . worm_statistics_objects ] ) <EOL> @ property <EOL> def valid_p_studentst_array ( self ) : <EOL> p_studentst_array = self . p_studentst_array <EOL> return p_studentst_array [ ~ np . isnan ( p_studentst_array ) ] <EOL> @ property <EOL> def valid_p_wilcoxon_array ( self ) : <EOL> p_wilcoxon_array = self . p_wilcoxon_array <EOL> return p_wilcoxon_array [ ~ np . isnan ( p_wilcoxon_array ) ] <EOL> @ property <EOL> def min_p_wilcoxon ( self ) : <EOL> return np . nanmin ( self . p_wilcoxon_array ) <EOL> @ property <EOL> def min_q_wilcoxon ( self ) : <EOL> return np . nanmin ( self . q_wilcoxon_array ) <EOL> def __repr__ ( self ) : <EOL> return utils . print_object ( self ) <EOL> def plot ( self ) : <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:12> , <NUM_LIT:9> ) ) <EOL> fig . suptitle ( "<STR_LIT>" ) <EOL> fig . text ( <NUM_LIT:1> , <NUM_LIT:1> , s = "<STR_LIT>" , fontdict = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT:8> } , <EOL> horizontalalignment = '<STR_LIT>' ) <EOL> rows = <NUM_LIT:5> <EOL> cols = <NUM_LIT:4> <EOL> for i in range ( rows * cols ) : <EOL> ax = plt . subplot2grid ( ( rows , cols ) , ( i // cols , i % cols ) ) <EOL> self . worm_statistics_objects [ i ] . plot ( ax ) <EOL> green_patch = mpatches . Patch ( color = '<STR_LIT:g>' , label = '<STR_LIT>' ) <EOL> grey_patch = mpatches . Patch ( color = '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> plt . legend ( handles = [ green_patch , grey_patch ] , <EOL> loc = '<STR_LIT>' , <EOL> fontsize = <NUM_LIT:12> , bbox_to_anchor = ( <NUM_LIT:0> , - <NUM_LIT:0.1> , <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> bbox_transform = plt . gcf ( ) . transFigure ) <EOL> plt . subplots_adjust ( <EOL> left = <NUM_LIT> , <EOL> right = <NUM_LIT> , <EOL> bottom = <NUM_LIT:0.1> , <EOL> top = <NUM_LIT> , <EOL> wspace = <NUM_LIT> , <EOL> hspace = <NUM_LIT> ) <EOL> class WormStatistics ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exp_histogram , ctl_histogram , USE_OLD_CODE = False ) : <EOL> """<STR_LIT>""" <EOL> if exp_histogram is None or ctl_histogram is None : <EOL> self . _z_score_experiment = np . NaN <EOL> self . _p_wilcoxon = np . NaN <EOL> self . _p_studentst = np . NaN <EOL> self . _t_statistic = np . NaN <EOL> self . _fisher_p = np . NaN <EOL> return <EOL> assert ( exp_histogram . specs . name == <EOL> ctl_histogram . specs . name ) <EOL> self . exp_histogram = exp_histogram <EOL> self . ctl_histogram = ctl_histogram <EOL> self . USE_OLD_CODE = USE_OLD_CODE <EOL> @ property <EOL> def z_score_experiment ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _z_score_experiment <EOL> except AttributeError : <EOL> USE_OLD_CODE = self . USE_OLD_CODE <EOL> if np . isnan ( self . exp_histogram . mean ) : <EOL> if ( ( USE_OLD_CODE and self . is_exclusive ) or ( <EOL> ~ USE_OLD_CODE and self . ctl_histogram . num_valid_videos > <NUM_LIT:1> ) ) : <EOL> self . _z_score_experiment = - np . Inf <EOL> else : <EOL> self . z_score_experiment = np . NaN <EOL> elif np . isnan ( self . ctl_histogram . mean ) : <EOL> if ( ( USE_OLD_CODE and self . is_exclusive ) or ( <EOL> ~ USE_OLD_CODE and self . exp_histogram . num_valid_videos > <NUM_LIT:1> ) ) : <EOL> self . _z_score_experiment = np . Inf <EOL> else : <EOL> self . _z_score_experiment = np . NaN <EOL> else : <EOL> self . _z_score_experiment = ( <EOL> ( self . exp_histogram . mean - self . ctl_histogram . mean ) / <EOL> self . ctl_histogram . std ) <EOL> return self . _z_score_experiment <EOL> @ property <EOL> def exp_p_normal ( self ) : <EOL> if self . exp_histogram is None : <EOL> return np . NaN <EOL> else : <EOL> return self . exp_histogram . p_normal <EOL> @ property <EOL> def ctl_p_normal ( self ) : <EOL> if self . ctl_histogram is None : <EOL> return np . NaN <EOL> else : <EOL> return self . ctl_histogram . p_normal <EOL> @ property <EOL> def p_studentst ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _p_studentst <EOL> except AttributeError : <EOL> if self . is_exclusive : <EOL> return self . fisher_p <EOL> else : <EOL> _ , self . _p_studentst = sp . stats . ttest_ind ( self . exp_histogram . valid_mean_per_video , <EOL> self . ctl_histogram . valid_mean_per_video ) <EOL> return self . _p_studentst <EOL> @ property <EOL> def p_wilcoxon ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _p_wilcoxon <EOL> except AttributeError : <EOL> if self . is_exclusive : <EOL> return self . fisher_p <EOL> elif not ( self . exp_histogram . no_valid_videos or <EOL> self . ctl_histogram . no_valid_videos ) : <EOL> _ , self . _p_wilcoxon = sp . stats . ranksums ( self . exp_histogram . valid_mean_per_video , <EOL> self . ctl_histogram . valid_mean_per_video ) <EOL> else : <EOL> self . _p_wilcoxon = np . NaN <EOL> return self . _p_wilcoxon <EOL> @ property <EOL> def specs ( self ) : <EOL> assert ( self . exp_histogram . specs . name == <EOL> self . ctl_histogram . specs . name ) <EOL> return self . exp_histogram . specs <EOL> @ property <EOL> def histogram_type ( self ) : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def motion_type ( self ) : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def data_type ( self ) : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def fisher_p ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _fisher_p <EOL> except AttributeError : <EOL> num_exp_videos = self . exp_histogram . num_videos <EOL> num_ctl_videos = self . ctl_histogram . num_videos <EOL> num_videos = num_exp_videos + num_ctl_videos <EOL> params = np . array ( [ num_exp_videos , num_videos , <EOL> num_exp_videos , num_exp_videos ] ) <EOL> _ , self . _fisher_p = sp . stats . fisher_exact ( params ) <EOL> return self . _fisher_p <EOL> @ property <EOL> def t_statistic ( self ) : <EOL> try : <EOL> return self . _t_statistic <EOL> except AttributeError : <EOL> self . _t_statistic , _ = sp . stats . ttest_ind ( self . exp_histogram . valid_mean_per_video , <EOL> self . ctl_histogram . valid_mean_per_video ) <EOL> return self . _t_statistic <EOL> @ property <EOL> def is_exclusive ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _is_exclusive <EOL> except AttributeError : <EOL> self . _is_exclusive = ( ( self . exp_histogram . no_valid_videos and <EOL> self . ctl_histogram . all_videos_valid ) or <EOL> ( self . ctl_histogram . no_valid_videos and <EOL> self . exp_histogram . all_videos_valid ) ) <EOL> return self . _is_exclusive <EOL> def __repr__ ( self ) : <EOL> return utils . print_object ( self ) <EOL> @ property <EOL> def plot_title ( self ) : <EOL> """<STR_LIT>""" <EOL> exp_histogram = self . exp_histogram <EOL> ctl_histogram = self . ctl_histogram <EOL> if self . data_type == '<STR_LIT:all>' : <EOL> data_type_string = '<STR_LIT>' <EOL> else : <EOL> data_type_string = '<STR_LIT>' . format ( self . data_type ) <EOL> title = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( self . specs . name . upper ( ) , <EOL> self . motion_type , <EOL> data_type_string , <EOL> self . histogram_type , <EOL> exp_histogram . num_videos , ctl_histogram . num_videos , <EOL> exp_histogram . num_samples , ctl_histogram . num_samples ) <EOL> title += ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( <EOL> exp_histogram . mean , exp_histogram . std , <EOL> ctl_histogram . mean , ctl_histogram . std , <EOL> self . p_wilcoxon , self . q_wilcoxon ) <EOL> return title <EOL> def plot ( self , ax , use_legend = False , use_alternate_plot = False ) : <EOL> """<STR_LIT>""" <EOL> ctl_bins = self . ctl_histogram . bin_midpoints <EOL> ctl_y_values = self . ctl_histogram . pdf <EOL> exp_bins = self . exp_histogram . bin_midpoints <EOL> exp_y_values = self . exp_histogram . pdf <EOL> min_x = min ( [ h [ <NUM_LIT:0> ] for h in [ ctl_bins , exp_bins ] ] ) <EOL> max_x = min ( [ h [ - <NUM_LIT:1> ] for h in [ ctl_bins , exp_bins ] ] ) <EOL> if self . q_wilcoxon <= <NUM_LIT> : <EOL> bgcolour = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> elif self . q_wilcoxon <= <NUM_LIT> : <EOL> bgcolour = ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) <EOL> elif self . q_wilcoxon <= <NUM_LIT> : <EOL> bgcolour = ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> ) <EOL> elif self . q_wilcoxon <= <NUM_LIT> : <EOL> bgcolour = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT> ) <EOL> else : <EOL> bgcolour = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> bgcolour = np . array ( bgcolour ) / <NUM_LIT:255> <EOL> if use_alternate_plot : <EOL> x = self . exp_histogram . data <EOL> y = self . ctl_histogram . data <EOL> truncated_length = min ( len ( x ) , len ( y ) ) <EOL> df = pd . DataFrame ( data = { '<STR_LIT>' : x [ : truncated_length ] , <EOL> '<STR_LIT>' : y [ : truncated_length ] } ) <EOL> g = sns . jointplot ( x = '<STR_LIT>' , y = '<STR_LIT>' , <EOL> data = df , kind = '<STR_LIT>' , stat_func = sp . stats . wilcoxon , <EOL> color = "<STR_LIT>" ) <EOL> g . fig . gca ( ) . set_title ( self . plot_title , fontsize = <NUM_LIT:10> ) <EOL> else : <EOL> plt . ticklabel_format ( style = '<STR_LIT>' , useOffset = True ) <EOL> h1 = ax . fill_between ( ctl_bins , ctl_y_values , alpha = <NUM_LIT:1> , color = '<STR_LIT>' , <EOL> label = '<STR_LIT>' ) <EOL> h2 = ax . fill_between ( exp_bins , exp_y_values , alpha = <NUM_LIT:0.5> , color = '<STR_LIT:g>' , <EOL> label = '<STR_LIT>' ) <EOL> ax . set_axis_bgcolor ( bgcolour ) <EOL> ax . set_xlabel ( self . exp_histogram . specs . units , fontsize = <NUM_LIT:10> ) <EOL> ax . set_ylabel ( '<STR_LIT>' , fontsize = <NUM_LIT:10> ) <EOL> ax . yaxis . set_ticklabels ( [ ] ) <EOL> ax . yaxis . set_ticks ( [ ] ) <EOL> ax . set_title ( self . plot_title , fontsize = <NUM_LIT:10> ) <EOL> ax . set_xlim ( min_x , max_x ) <EOL> ax . spines [ "<STR_LIT>" ] . set_visible ( False ) <EOL> ax . spines [ "<STR_LIT:right>" ] . set_visible ( False ) <EOL> ax . get_xaxis ( ) . tick_bottom ( ) <EOL> ax . get_yaxis ( ) . tick_left ( ) <EOL> if use_legend : <EOL> ax . legend ( handles = [ h1 , h2 ] , <EOL> labels = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> loc = '<STR_LIT>' , fontsize = <NUM_LIT:12> ) </s>
