def test_kuiper_anglerange(): alpha1 = np.asarray([ 0.291662278945, 5.899415544666, 5.402236718096, 3.728212505263, 5.303188109786, 3.737946900082, 3.850015526787, 4.902154536516, 3.631621444982, 5.341562525096 ]) alpha2 = np.asarray([ 0.613650458799, 2.109660249330, 3.617555161298, 6.196794760548, 1.856071575830, 2.991480015107, 1.789200626487, 4.835921843822, 2.767491245457, 1.744565591973 ]) alpha1b = alpha1 - np.pi alpha2b = alpha2 - np.pi with warnings.catch_warnings(): warnings.simplefilter("ignore") p, k = pycircstat.kuiper(alpha1, alpha2) pb, kb = pycircstat.kuiper(alpha1b, alpha2b) assert_allclose(p, pb, rtol=1e-4, atol=1e-4, err_msg="P-values are not equal") assert_allclose(k, kb, rtol=1e-4, atol=1e-4, err_msg="K statistics not equal")
def test_kuiper(): alpha1 = np.asarray([0.291662278945, 5.899415544666, 5.402236718096, 3.728212505263, 5.303188109786, 3.737946900082, 3.850015526787, 4.902154536516, 3.631621444982, 5.341562525096]) alpha2 = np.asarray([0.613650458799, 2.109660249330, 3.617555161298, 6.196794760548, 1.856071575830, 2.991480015107, 1.789200626487, 4.835921843822, 2.767491245457, 1.744565591973]) p0 = 0.1 k0 = 70 K0 = 67.395 with warnings.catch_warnings(): warnings.simplefilter("ignore") p, k = pycircstat.kuiper(alpha1, alpha2) assert_allclose(p, p0, rtol=1e-4, atol=1e-4, err_msg="Error in p-values.") assert_allclose( k, k0, rtol=1e-4, atol=1e-4, err_msg="Error in statistic k.")
def test_kuiper_warning(): alpha1 = np.asarray([0.291662278945, 5.899415544666, 5.402236718096, 3.728212505263, 5.303188109786, 3.737946900082, 3.850015526787, 4.902154536516, 3.631621444982, 5.341562525096]) alpha2 = np.asarray([0.613650458799, 2.109660249330, 3.617555161298, 6.196794760548, 1.856071575830, 2.991480015107, 1.789200626487, 4.835921843822, 2.767491245457, 1.744565591973]) p0 = 0.1 k0 = 70 with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") p, k = pycircstat.kuiper(alpha1, alpha2) assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) assert "N=10 not found in table" in str(w[-1].message)
def test_kuiper_anglerange(): alpha1 = np.asarray([0.291662278945, 5.899415544666, 5.402236718096, 3.728212505263, 5.303188109786, 3.737946900082, 3.850015526787, 4.902154536516, 3.631621444982, 5.341562525096]) alpha2 = np.asarray([0.613650458799, 2.109660249330, 3.617555161298, 6.196794760548, 1.856071575830, 2.991480015107, 1.789200626487, 4.835921843822, 2.767491245457, 1.744565591973]) alpha1b = alpha1 - np.pi alpha2b = alpha2 - np.pi with warnings.catch_warnings(): warnings.simplefilter("ignore") p, k = pycircstat.kuiper(alpha1, alpha2) pb, kb = pycircstat.kuiper(alpha1b, alpha2b) assert_allclose(p, pb, rtol=1e-4, atol=1e-4, err_msg="P-values are not equal" ) assert_allclose(k, kb, rtol=1e-4, atol=1e-4, err_msg="K statistics not equal" )
def test_kuiper_warning(): unknown_N = 26 alpha1 = np.random.rand(unknown_N)*2.*np.pi alpha2 = np.random.rand(unknown_N)*2.*np.pi with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") p, k = pycircstat.kuiper(alpha1, alpha2) assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) assert "N=%d not found in table" % unknown_N in str(w[-1].message)
def _compute_novel_rep_spike_stats(novel_phases, rep_phases): # compute rayleigh test for each condition p_novel, z_novel = pycircstat.rayleigh(novel_phases, axis=0) p_rep, z_rep = pycircstat.rayleigh(rep_phases, axis=0) # test whether the means are different ww_pvals, ww_tables = pycircstat.watson_williams(novel_phases, rep_phases, axis=0) ww_fstat = np.array([x.loc['Columns'].F for x in ww_tables]) # test whether the medians are different med_pvals, med_stat = pycircstat.cmtest(novel_phases, rep_phases, axis=0) # finall run kuiper test for difference in mean and/or dispersion p_kuiper, stat_kuiper = pycircstat.kuiper(novel_phases, rep_phases, axis=0) return p_novel, z_novel, p_rep, z_rep, ww_pvals, ww_fstat, med_pvals, med_stat, p_kuiper, stat_kuiper
def compute_phase_stats_with_shuffle(events, spike_rel_times, phase_data_hilbert, phase_bin_start, phase_bin_stop, do_permute=False, shuffle_type=1): spike_rel_times_tmp = spike_rel_times.copy() e_tmp = events.copy() if do_permute: if shuffle_type == 1: # permute the novel novel_events = np.where(events.isFirst.values)[0] perm_novel_events = np.random.permutation(novel_events) spike_rel_times_tmp[novel_events] = spike_rel_times_tmp[perm_novel_events] # and repeated separately rep_events = np.where(~events.isFirst.values)[0] perm_rep_events = np.random.permutation(rep_events) spike_rel_times_tmp[rep_events] = spike_rel_times_tmp[perm_rep_events] else: e_tmp['isFirst'] = np.random.permutation(e_tmp.isFirst) # get the phases at which the spikes occurred and bin into novel and repeated items for each hilbert band spike_phases_hilbert = _compute_spike_phase_by_freq(spike_rel_times_tmp, phase_bin_start, phase_bin_stop, phase_data_hilbert, events) # bin into repeated and novel phases novel_phases, rep_phases = _bin_phases_into_cond(spike_phases_hilbert, e_tmp) if (len(novel_phases) > 0) & (len(rep_phases) > 0): # test phase locking for all spikes comboined all_spikes_phases = np.vstack([novel_phases, rep_phases]) rayleigh_pval_all, rayleigh_z_all = pycircstat.rayleigh(all_spikes_phases, axis=0) rvl_all = pycircstat.resultant_vector_length(all_spikes_phases, axis=0) # rayleigh test for uniformity rvl_novel = pycircstat.resultant_vector_length(novel_phases, axis=0) rvl_rep = pycircstat.resultant_vector_length(rep_phases, axis=0) rvl_diff = rvl_novel - rvl_rep # compute rayleigh test for each condition rayleigh_pval_novel, rayleigh_z_novel = pycircstat.rayleigh(novel_phases, axis=0) rayleigh_pval_rep, rayleigh_z_rep = pycircstat.rayleigh(rep_phases, axis=0) rayleigh_diff = rayleigh_z_novel - rayleigh_z_rep # watson williams test for equal means ww_pval, ww_tables = pycircstat.watson_williams(novel_phases, rep_phases, axis=0) ww_fstat = np.array([x.loc['Columns'].F for x in ww_tables]) # kuiper test, to test for difference in dispersion (not mean, because I'm making them equal) kuiper_pval, stat_kuiper = pycircstat.kuiper(novel_phases - pycircstat.mean(novel_phases), rep_phases - pycircstat.mean(rep_phases), axis=0) return (rvl_novel, rvl_rep, rvl_diff, ww_fstat, stat_kuiper, rayleigh_z_novel, rayleigh_z_rep, rayleigh_diff, rayleigh_z_all, rvl_all), \ (rayleigh_pval_novel, rayleigh_pval_rep, ww_pval, kuiper_pval, rayleigh_pval_all), novel_phases, rep_phases else: return (np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2])), \ (np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2])), novel_phases, rep_phases
def compute_phase_stats_with_shuffle(events, spike_rel_times, phase_data_hilbert, phase_bin_start, phase_bin_stop, do_permute=False): spike_rel_times_tmp = spike_rel_times.copy() if do_permute: # permute the novel novel_events = np.where(events.isFirst.values)[0] perm_novel_events = np.random.permutation(novel_events) spike_rel_times_tmp[novel_events] = spike_rel_times_tmp[perm_novel_events] # and repeated separately rep_events = np.where(~events.isFirst.values)[0] perm_rep_events = np.random.permutation(rep_events) spike_rel_times_tmp[rep_events] = spike_rel_times_tmp[perm_rep_events] # get the phases at which the spikes occurred and bin into novel and repeated items for each hilbert band spike_phases_hilbert = _compute_spike_phase_by_freq(spike_rel_times_tmp, phase_bin_start, phase_bin_stop, phase_data_hilbert, events) # bin into repeated and novel phases novel_phases, rep_phases = _bin_phases_into_cond(spike_phases_hilbert, events) if (len(novel_phases) > 0) & (len(rep_phases) > 0): # rayleigh test for uniformity rvl_novel = pycircstat.resultant_vector_length(novel_phases, axis=0) rvl_rep = pycircstat.resultant_vector_length(rep_phases, axis=0) rvl_diff = rvl_novel - rvl_rep # compute rayleigh test for each condition rayleigh_pval_novel, rayleigh_z_novel = pycircstat.rayleigh(novel_phases, axis=0) rayleigh_pval_rep, rayleigh_z_rep = pycircstat.rayleigh(rep_phases, axis=0) rayleigh_diff = rayleigh_z_novel - rayleigh_z_rep # watson williams test for equal means ww_pval, ww_tables = pycircstat.watson_williams(novel_phases, rep_phases, axis=0) ww_fstat = np.array([x.loc['Columns'].F for x in ww_tables]) # kuiper test, to test for difference in dispersion (not mean, because I'm making them equal) kuiper_pval, stat_kuiper = pycircstat.kuiper(novel_phases - pycircstat.mean(novel_phases), rep_phases - pycircstat.mean(rep_phases), axis=0) return (rvl_novel, rvl_rep, rvl_diff, ww_fstat, stat_kuiper, rayleigh_z_novel, rayleigh_z_rep, rayleigh_diff), \ (rayleigh_pval_novel, rayleigh_pval_rep, ww_pval, kuiper_pval), novel_phases, rep_phases else: return (np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2])), \ (np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2]), np.array([np.nan] * phase_data_hilbert.shape[2])), novel_phases, rep_phases