def test_median(): # simple tests x = np.arange(1, 11) m = pra.median(x) assert m == 5.5 x = np.arange(1, 12) m = pra.median(x) assert m == 6 # test dimensions x = np.random.rand(10, 9, 8) m, ci = pra.median(x, alpha=0.05) assert m.shape == (10, 9) assert ci.shape == (2, 10, 9) m, ci = pra.median(x, alpha=0.05, axis=1) assert m.shape == (10, 8) assert ci.shape == (2, 10, 8) m, ci = pra.median(x, alpha=0.05, axis=1, keepdims=True) assert m.shape == (10, 1, 8) assert ci.shape == (2, 10, 1, 8) # Now test statistical property of the confidence interval np.random.seed(0) # fix this for repeatable outcome R = 200 N = [10, 100, 1000] alpha = [0.05, 0.01] from scipy.stats import uniform dist = uniform() true_median = dist.median() for n in N: for a in alpha: failures = np.zeros(R, dtype=bool) for r in range(R): x = dist.rvs(size=n) m, ci = pra.median(x, alpha=a) if true_median < m + ci[0] or m + ci[1] < true_median: failures[r] = 1 assert sum( failures) / R <= 1.2 * a # allow some slack for realizations
def nice_plot(x, ylabel, bf_order=None): ''' Define a function to plot consistently the data ''' if bf_order is None: bf_order = beamformer_names ax1 = plt.gca() newmap = plt.get_cmap('gist_heat') from itertools import cycle # totally a hack to get the same line styles as Fig6/7 lines = ['-D','-v','->','-s','-o'] linecycler = cycle(lines) # totally a hack to get the same line styles as Fig6/7 map1 = [newmap( k ) for k in np.linspace(0.25,0.9,5)] map2 = [map1[3],map1[2],map1[4],map1[0],map1[1]] ax1.set_color_cycle(map2) # no clipping of the beautiful markers plt.setp(ax1,'clip_on',False) for bf in bf_order: i = bf_dict[bf] med, ci = pra.median(x, axis=0) p, = plt.plot(range(0, max_sources), #np.median(x[:,i,:], axis=0), med[i,:], next(linecycler), linewidth=1, markersize=4, markeredgewidth=.5, clip_on=False) # confidence interval for the median plt.fill_between(range(0, max_sources), med[i,:]+ci[0,i,:], med[i,:]+ci[1,i,:], #color='grey', color=p.get_color(), linewidth=0.05, edgecolor='k', alpha=0.3) # Hide right and top axes ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) ax1.spines['bottom'].set_position(('outward', 10)) ax1.spines['left'].set_position(('outward', 15)) ax1.yaxis.set_ticks_position('left') ax1.xaxis.set_ticks_position('bottom') # Make ticks nicer ax1.xaxis.set_tick_params(width=.3, length=3) ax1.yaxis.set_tick_params(width=.3, length=3) # Make axis lines thinner for axis in ['bottom','left']: ax1.spines[axis].set_linewidth(0.3) # Set ticks fontsize plt.xticks(size=9) plt.yticks(size=9) # Set labels plt.xlabel(r'Number of images $K$', fontsize=10) plt.ylabel(ylabel, fontsize=10) plt.legend(bf_order, fontsize=7, loc='upper left', frameon=False, labelspacing=0)
plt.xticks(size=9) plt.yticks(size=9) # Set labels plt.xlabel(r'Number of images $K$', fontsize=10) plt.ylabel(ylabel, fontsize=10) plt.legend(bf_order, fontsize=7, loc='upper left', frameon=False, labelspacing=0) # Here we plot the figure used in the paper (Fig. 10) plt.figure(figsize=(4,3)) nice_plot(opesq_bf[:,0,:,:], 'PESQ [MOS]', bf_order=['Rake Perceptual','Rake MVDR']) # plot input SNR med, ci = pra.median(ipesq[:,0]) o = np.ones(max_sources) p, = plt.plot(np.arange(max_sources), np.median(ipesq[:,0])*o) plt.text(5.0, 1.28, 'Input PESQ', fontsize=7) ''' # confidence interval for the median plt.fill_between(range(0, max_sources), (med+ci[0])*o, (med+ci[1])*o, #color='grey', color=p.get_color(), linewidth=0.05, edgecolor='k', alpha=0.3) '''
def nice_plot(x, ylabel, bf_order=None): ''' Define a function to plot consistently the data ''' if bf_order is None: bf_order = beamformer_names ax1 = plt.gca() newmap = plt.get_cmap('gist_heat') from itertools import cycle # totally a hack to get the same line styles as Fig6/7 lines = ['-D', '-v', '->', '-s', '-o'] linecycler = cycle(lines) # totally a hack to get the same line styles as Fig6/7 map1 = [newmap(k) for k in np.linspace(0.25, 0.9, 5)] map2 = [map1[3], map1[2], map1[4], map1[0], map1[1]] ax1.set_color_cycle(map2) # no clipping of the beautiful markers plt.setp(ax1, 'clip_on', False) for bf in bf_order: i = bf_dict[bf] med, ci = pra.median(x, axis=0) p, = plt.plot( range(0, max_sources), #np.median(x[:,i,:], axis=0), med[i, :], next(linecycler), linewidth=1, markersize=4, markeredgewidth=.5, clip_on=False) # confidence interval for the median plt.fill_between( range(0, max_sources), med[i, :] + ci[0, i, :], med[i, :] + ci[1, i, :], #color='grey', color=p.get_color(), linewidth=0.05, edgecolor='k', alpha=0.3) # Hide right and top axes ax1.spines['top'].set_visible(False) ax1.spines['right'].set_visible(False) ax1.spines['bottom'].set_position(('outward', 10)) ax1.spines['left'].set_position(('outward', 15)) ax1.yaxis.set_ticks_position('left') ax1.xaxis.set_ticks_position('bottom') # Make ticks nicer ax1.xaxis.set_tick_params(width=.3, length=3) ax1.yaxis.set_tick_params(width=.3, length=3) # Make axis lines thinner for axis in ['bottom', 'left']: ax1.spines[axis].set_linewidth(0.3) # Set ticks fontsize plt.xticks(size=9) plt.yticks(size=9) # Set labels plt.xlabel(r'Number of images $K$', fontsize=10) plt.ylabel(ylabel, fontsize=10) plt.legend(bf_order, fontsize=7, loc='upper left', frameon=False, labelspacing=0)
plt.legend(bf_order, fontsize=7, loc='upper left', frameon=False, labelspacing=0) # Here we plot the figure used in the paper (Fig. 10) plt.figure(figsize=(4, 3)) nice_plot(opesq_bf[:, 0, :, :], 'PESQ [MOS]', bf_order=['Rake Perceptual', 'Rake MVDR']) # plot input SNR med, ci = pra.median(ipesq[:, 0]) o = np.ones(max_sources) p, = plt.plot(np.arange(max_sources), np.median(ipesq[:, 0]) * o) plt.text(5.0, 1.28, 'Input PESQ', fontsize=7) ''' # confidence interval for the median plt.fill_between(range(0, max_sources), (med+ci[0])*o, (med+ci[1])*o, #color='grey', color=p.get_color(), linewidth=0.05, edgecolor='k', alpha=0.3) '''
def med_ci(slice_values): ''' This is used to compute median and confidence interval in dataframe ''' m, ci = median(np.array(slice_values['value']).flatten(), alpha=0.05) return pd.Series([m, ci[0], ci[1]], index=['value', 'ci_lo', 'ci_hi'])