def test_jointJ_window_analysis(self): sts1 = self.sts1_neo sts2 = self.sts2_neo data = np.vstack((sts1, sts2)).T winsize = 100 * pq.ms binsize = 5 * pq.ms winstep = 20 * pq.ms pattern_hash = [3] UE_dic = ue.jointJ_window_analysis(data, binsize, winsize, winstep, pattern_hash) expected_Js = np.array([ 0.57953708, 0.47348757, 0.1729669, 0.01883295, -0.21934742, -0.80608759 ]) expected_n_emp = np.array([9., 9., 7., 7., 6., 6.]) expected_n_exp = np.array([6.5, 6.85, 6.05, 6.6, 6.45, 8.7]) expected_rate = np.array( [[0.02166667, 0.01861111], [0.02277778, 0.01777778], [0.02111111, 0.01777778], [0.02277778, 0.01888889], [0.02305556, 0.01722222], [0.02388889, 0.02055556]]) * pq.kHz expected_indecis_tril26 = [4., 4.] expected_indecis_tril4 = [1.] self.assertTrue(np.allclose(UE_dic['Js'], expected_Js)) self.assertTrue(np.allclose(UE_dic['n_emp'], expected_n_emp)) self.assertTrue(np.allclose(UE_dic['n_exp'], expected_n_exp)) self.assertTrue( np.allclose(UE_dic['rate_avg'].rescale('Hz').magnitude, expected_rate.rescale('Hz').magnitude)) self.assertTrue( np.allclose(UE_dic['indices']['trial26'], expected_indecis_tril26)) self.assertTrue( np.allclose(UE_dic['indices']['trial4'], expected_indecis_tril4))
def test_jointJ_window_analysis(self): sts1 = self.sts1_neo sts2 = self.sts2_neo data = np.vstack((sts1,sts2)).T winsize = 100*pq.ms binsize = 5*pq.ms winstep = 20*pq.ms pattern_hash = [3] UE_dic = ue.jointJ_window_analysis(data, binsize, winsize, winstep, pattern_hash) expected_Js = np.array( [ 0.57953708, 0.47348757, 0.1729669 , 0.01883295, -0.21934742,-0.80608759]) expected_n_emp = np.array( [ 9., 9., 7., 7., 6., 6.]) expected_n_exp = np.array( [ 6.5 , 6.85, 6.05, 6.6 , 6.45, 8.7 ]) expected_rate = np.array( [[ 0.02166667, 0.01861111], [ 0.02277778, 0.01777778], [ 0.02111111, 0.01777778], [ 0.02277778, 0.01888889], [ 0.02305556, 0.01722222], [ 0.02388889, 0.02055556]])*pq.kHz expected_indecis_tril26 = [ 4., 4.] expected_indecis_tril4 = [ 1.] self.assertTrue(np.allclose(UE_dic['Js'] ,expected_Js)) self.assertTrue(np.allclose(UE_dic['n_emp'] ,expected_n_emp)) self.assertTrue(np.allclose(UE_dic['n_exp'] ,expected_n_exp)) self.assertTrue(np.allclose( UE_dic['rate_avg'].rescale('Hz').magnitude , expected_rate.rescale('Hz').magnitude)) self.assertTrue(np.allclose( UE_dic['indices']['trial26'],expected_indecis_tril26)) self.assertTrue(np.allclose( UE_dic['indices']['trial4'],expected_indecis_tril4))
def test_Riehle_et_al_97_UE(self): url = "http://raw.githubusercontent.com/ReScience-Archives/Rostami-" \ "Ito-Denker-Gruen-2017/master/data" files_to_download = (("extracted_data.npy", "c4903666ce8a8a31274d6b11238a5ac3"), ("winny131_23.gdf", "cc2958f7b4fb14dbab71e17bba49bd10")) for filename, checksum in files_to_download: # The files will be downloaded to ELEPHANT_TMP_DIR download(url=f"{url}/{filename}", checksum=checksum) # load spike data of figure 2 of Riehle et al 1997 spiketrain = self.load_gdf2Neo(ELEPHANT_TMP_DIR / "winny131_23.gdf", trigger='RS_4', t_pre=1799 * pq.ms, t_post=300 * pq.ms) # calculating UE ... winsize = 100 * pq.ms bin_size = 5 * pq.ms winstep = 5 * pq.ms pattern_hash = [3] t_start = spiketrain[0][0].t_start t_stop = spiketrain[0][0].t_stop t_winpos = ue._winpos(t_start, t_stop, winsize, winstep) significance_level = 0.05 UE = ue.jointJ_window_analysis(spiketrain, pattern_hash=pattern_hash, bin_size=bin_size, win_size=winsize, win_step=winstep, method='analytic_TrialAverage') # load extracted data from figure 2 of Riehle et al 1997 extracted_data = np.load(ELEPHANT_TMP_DIR / 'extracted_data.npy', encoding='latin1', allow_pickle=True).item() Js_sig = ue.jointJ(significance_level) sig_idx_win = np.where(UE['Js'] >= Js_sig)[0] diff_UE_rep = [] y_cnt = 0 for trial_id in range(len(spiketrain)): trial_id_str = "trial{}".format(trial_id) indices_unique = np.unique(UE['indices'][trial_id_str]) if len(indices_unique) > 0: # choose only the significant coincidences indices_unique_significant = [] for j in sig_idx_win: significant = indices_unique[np.where( (indices_unique * bin_size >= t_winpos[j]) & (indices_unique * bin_size < t_winpos[j] + winsize))] indices_unique_significant.extend(significant) x_tmp = np.unique(indices_unique_significant) * \ bin_size.magnitude if len(x_tmp) > 0: ue_trial = np.sort(extracted_data['ue'][y_cnt]) diff_UE_rep = np.append(diff_UE_rep, x_tmp - ue_trial) y_cnt += +1 np.testing.assert_array_less(np.abs(diff_UE_rep), 0.3)
def test_multiple_neurons(self): np.random.seed(12) spiketrains = [[ homogeneous_poisson_process(rate=50 * pq.Hz, t_stop=1 * pq.s) for _ in range(5) ] for neuron in range(3)] spiketrains = np.stack(spiketrains, axis=1) UE_dic = ue.jointJ_window_analysis(spiketrains, bin_size=5 * pq.ms, win_size=300 * pq.ms, win_step=100 * pq.ms) js_expected = [[0.6081138], [0.17796665], [-1.2601125], [-0.2790147], [0.07804556], [0.7861176], [0.23452221], [0.11624397]] indices_expected = { 'trial2': [20, 30, 20, 30, 104, 104, 104], 'trial3': [21, 21, 65, 65, 65, 128, 128, 128], 'trial4': [8, 172, 172], 'trial0': [104, 106, 104, 106, 104, 106], 'trial1': [158, 158, 158, 188] } n_emp_expected = [[4.], [4.], [1.], [4.], [4.], [5.], [3.], [3.]] n_exp_expected = [[2.2858334], [3.2066667], [2.955], [4.485833], [3.4622223], [2.723611], [2.166111], [2.4122221]] rate_expected = [[[0.04666667, 0.03266666, 0.04333333]], [[0.04733333, 0.03666667, 0.044]], [[0.04533333, 0.03466666, 0.046]], [[0.04933333, 0.04466667, 0.04933333]], [[0.04466667, 0.04266667, 0.046]], [[0.04133333, 0.04466667, 0.044]], [[0.04133333, 0.03666667, 0.04266667]], [[0.03933334, 0.03866667, 0.04666667]]] * 1 / pq.ms input_parameters_expected = { 'pattern_hash': [7], 'bin_size': 5 * pq.ms, 'win_size': 300 * pq.ms, 'win_step': 100 * pq.ms, 'method': 'analytic_TrialByTrial', 't_start': 0 * pq.s, 't_stop': 1 * pq.s, 'n_surrogates': 100 } assert_array_almost_equal(UE_dic['Js'], js_expected) assert_array_almost_equal(UE_dic['n_emp'], n_emp_expected) assert_array_almost_equal(UE_dic['n_exp'], n_exp_expected) assert_array_almost_equal(UE_dic['rate_avg'], rate_expected) self.assertEqual(sorted(UE_dic['indices'].keys()), sorted(indices_expected.keys())) for trial_key in indices_expected.keys(): assert_array_equal(indices_expected[trial_key], UE_dic['indices'][trial_key]) self.assertEqual(UE_dic['input_parameters'], input_parameters_expected)
def test_multiple_neurons(self): np.random.seed(12) spiketrains = \ [StationaryPoissonProcess( rate=50 * pq.Hz, t_stop=1 * pq.s).generate_n_spiketrains(5) for neuron in range(3)] spiketrains = np.stack(spiketrains, axis=1) UE_dic = ue.jointJ_window_analysis(spiketrains, bin_size=5 * pq.ms, win_size=300 * pq.ms, win_step=100 * pq.ms) js_expected = [[0.3978179], [0.08131966], [-1.4239882], [-0.9377029], [-0.3374434], [-0.2043383], [-1.001536], [-np.inf]] indices_expected = \ {'trial3': [12, 27, 31, 34, 27, 31, 34, 136, 136, 136], 'trial4': [4, 60, 60, 60, 117, 117, 117]} n_emp_expected = [[5.], [4.], [1.], [2.], [2.], [2.], [1.], [0.]] n_exp_expected = [[3.5591667], [3.4536111], [3.3158333], [3.8466666], [2.370278], [2.0811112], [2.4011111], [3.0533333]] rate_expected = [[[0.042, 0.03933334, 0.048]], [[0.04533333, 0.038, 0.05]], [[0.046, 0.04, 0.04666667]], [[0.05066667, 0.042, 0.046]], [[0.04466667, 0.03666667, 0.04066667]], [[0.04066667, 0.03533333, 0.04333333]], [[0.03933334, 0.038, 0.038]], [[0.04066667, 0.04866667, 0.03666667]]] * (1. / pq.ms) input_parameters_expected = { 'pattern_hash': [7], 'bin_size': 5 * pq.ms, 'win_size': 300 * pq.ms, 'win_step': 100 * pq.ms, 'method': 'analytic_TrialByTrial', 't_start': 0 * pq.s, 't_stop': 1 * pq.s, 'n_surrogates': 100 } assert_array_almost_equal(UE_dic['Js'], js_expected) assert_array_almost_equal(UE_dic['n_emp'], n_emp_expected) assert_array_almost_equal(UE_dic['n_exp'], n_exp_expected) assert_array_almost_equal(UE_dic['rate_avg'], rate_expected) self.assertEqual(sorted(UE_dic['indices'].keys()), sorted(indices_expected.keys())) for trial_key in indices_expected.keys(): assert_array_equal(indices_expected[trial_key], UE_dic['indices'][trial_key]) self.assertEqual(UE_dic['input_parameters'], input_parameters_expected)
def test_jointJ_window_analysis(self): sts1 = self.sts1_neo sts2 = self.sts2_neo data = np.vstack((sts1, sts2)).T win_size = 100 * pq.ms bin_size = 5 * pq.ms win_step = 20 * pq.ms pattern_hash = [3] UE_dic = ue.jointJ_window_analysis(spiketrains=data, pattern_hash=pattern_hash, bin_size=bin_size, win_size=win_size, win_step=win_step) expected_Js = np.array( [0.57953708, 0.47348757, 0.1729669, 0.01883295, -0.21934742, -0.80608759]) expected_n_emp = np.array( [9., 9., 7., 7., 6., 6.]) expected_n_exp = np.array( [6.5, 6.85, 6.05, 6.6, 6.45, 8.7]) expected_rate = np.array( [[0.02166667, 0.01861111], [0.02277778, 0.01777778], [0.02111111, 0.01777778], [0.02277778, 0.01888889], [0.02305556, 0.01722222], [0.02388889, 0.02055556]]) * pq.kHz expected_indecis_tril26 = [4., 4.] expected_indecis_tril4 = [1.] assert_array_almost_equal(UE_dic['Js'].squeeze(), expected_Js) assert_array_almost_equal(UE_dic['n_emp'].squeeze(), expected_n_emp) assert_array_almost_equal(UE_dic['n_exp'].squeeze(), expected_n_exp) assert_array_almost_equal(UE_dic['rate_avg'].squeeze(), expected_rate) assert_array_almost_equal(UE_dic['indices']['trial26'], expected_indecis_tril26) assert_array_almost_equal(UE_dic['indices']['trial4'], expected_indecis_tril4) # check the input parameters input_params = UE_dic['input_parameters'] self.assertEqual(input_params['pattern_hash'], pattern_hash) self.assertEqual(input_params['bin_size'], bin_size) self.assertEqual(input_params['win_size'], win_size) self.assertEqual(input_params['win_step'], win_step) self.assertEqual(input_params['method'], 'analytic_TrialByTrial') self.assertEqual(input_params['t_start'], 0 * pq.s) self.assertEqual(input_params['t_stop'], 200 * pq.ms)
def setUpClass(cls): # Download data filepath = TEST_DATA_DIR / Path(UE_DATASET_URL).name if not filepath.exists(): urlretrieve(UE_DATASET_URL, filename=filepath) check_integrity(filepath, md5="0219a243be3b452cf0537dc0a333fb2e") # Load data and extract spiketrains block = neo.io.NeoHdf5IO(filepath).read_block() sts1 = block.segments[0].spiketrains sts2 = block.segments[1].spiketrains cls.spiketrains = np.vstack((sts1, sts2)).T cls.UE = ue.jointJ_window_analysis(cls.spiketrains, binsize=5 * pq.ms, winsize=100 * pq.ms, winstep=10 * pq.ms, pattern_hash=[3])
def test_Riehle_et_al_97_UE(self): url = "http://raw.githubusercontent.com/ReScience-Archives/Rostami-" \ "Ito-Denker-Gruen-2017/master/data" shortname = "unitary_event_analysis_test_data" local_test_dir = create_local_temp_dir(shortname) files_to_download = ["extracted_data.npy", "winny131_23.gdf"] context = ssl._create_unverified_context() for filename in files_to_download: url_file = "{url}/{filename}".format(url=url, filename=filename) dist = urlopen(url_file, context=context) localfile = os.path.join(local_test_dir, filename) with open(localfile, 'wb') as f: f.write(dist.read()) # load spike data of figure 2 of Riehle et al 1997 spiketrain = self.load_gdf2Neo(os.path.join(local_test_dir, "winny131_23.gdf"), trigger='RS_4', t_pre=1799 * pq.ms, t_post=300 * pq.ms) # calculating UE ... winsize = 100 * pq.ms binsize = 5 * pq.ms winstep = 5 * pq.ms pattern_hash = [3] t_start = spiketrain[0][0].t_start t_stop = spiketrain[0][0].t_stop t_winpos = ue._winpos(t_start, t_stop, winsize, winstep) significance_level = 0.05 UE = ue.jointJ_window_analysis(spiketrain, binsize, winsize, winstep, pattern_hash, method='analytic_TrialAverage') # load extracted data from figure 2 of Riehle et al 1997 extracted_data = np.load(os.path.join(local_test_dir, 'extracted_data.npy'), encoding='latin1', allow_pickle=True).item() Js_sig = ue.jointJ(significance_level) sig_idx_win = np.where(UE['Js'] >= Js_sig)[0] diff_UE_rep = [] y_cnt = 0 for trial_id in range(len(spiketrain)): trial_id_str = "trial{}".format(trial_id) indices_unique = np.unique(UE['indices'][trial_id_str]) if len(indices_unique) > 0: # choose only the significant coincidences indices_unique_significant = [] for j in sig_idx_win: significant = indices_unique[np.where( (indices_unique * binsize >= t_winpos[j]) & (indices_unique * binsize < t_winpos[j] + winsize))] indices_unique_significant.extend(significant) x_tmp = np.unique(indices_unique_significant) * \ binsize.magnitude if len(x_tmp) > 0: ue_trial = np.sort(extracted_data['ue'][y_cnt]) diff_UE_rep = np.append(diff_UE_rep, x_tmp - ue_trial) y_cnt += +1 shutil.rmtree(local_test_dir) np.testing.assert_array_less(np.abs(diff_UE_rep), 0.3)
for trial in range(10): st_list = test_data.test_data(size=2, corr=.1, t_stop=10000 * ms, rate=10 * Hz, assembly_sizes=[2], method="CPP", bkgr_corr=.00) data += [st_list] # plotting.rasterplot(data) # plt.show() Js_dict = ue.jointJ_window_analysis( data=data, binsize=5 * ms, winsize=100 * ms, winstep=20 * ms, pattern_hash=[ue.hash_from_pattern([1, 1], 2)]) print Js_dict # Why not save settings in Js_dict? dict_args = { 'events': { 'SO': [10 * ms] }, 'save_fig': True, 'path_filename_format': 'UE1.pdf', 'showfig': True, 'suptitle': True,
# with the trial alignment on the PS. # load and cut the data file_name = 'winny131_23.gdf' trigger = 'PS_4' t_pre = 300 * pq.ms t_post = 1800 * pq.ms spiketrain = load_gdf2Neo(data_path + file_name, trigger, t_pre, t_post) N = len(spiketrain.T) # calculating UE ... UE = ue.jointJ_window_analysis(spiketrain, binsize, winsize, winstep, pattern_hash, method=method) # parameters for plotting plot_params = { 'events': { '0\nPS': [300 * pq.ms], '600\nES1': [900 * pq.ms], 'ES2': [1200 * pq.ms], 'ES3': [1500 * pq.ms], '1500\nRS': [1800 * pq.ms] }, 'path_filename_format': '/figure1.eps', }
# with the trial alignment on the PS. # load and cut the data file_name = 'winny131_23.gdf' trigger = 'PS_4' t_pre = 300 * pq.ms t_post = 1800 * pq.ms spiketrain = load_gdf2Neo(data_path + file_name, trigger, t_pre, t_post) N = len(spiketrain.T) # calculating UE ... UE = ue.jointJ_window_analysis(spiketrain, binsize, winsize, winstep, pattern_hash, method=method) # parameters for plotting plot_params = { 'events': { '0\nPS': [300 * pq.ms], '600\nES1': [900 * pq.ms], 'ES2': [1200 * pq.ms], 'ES3': [1500 * pq.ms], '1500\nRS': [1800 * pq.ms] }, 'path_filename_format': '../article/figure1.eps', }
# This is the first attempt to reproduce Figure 2 # of the original article after cutting the data # with the trial alignment on the PS. # load and cut the data file_name = 'winny131_23.gdf' trigger = 'PS_4' t_pre = 300 * pq.ms t_post = 1800 * pq.ms spiketrain = load_gdf2Neo(data_path + file_name, trigger, t_pre, t_post) N = len(spiketrain.T) # calculating UE ... UE = ue.jointJ_window_analysis( spiketrain, binsize, winsize, winstep, pattern_hash, method=method) # parameters for plotting plot_params = { 'events': {'0\nPS': [300 * pq.ms], '600\nES1': [900 * pq.ms], 'ES2': [1200 * pq.ms], 'ES3': [1500 * pq.ms], '1500\nRS': [1800 * pq.ms]}, 'path_filename_format': '/figure1.eps', } print('plotting Figure 1 with trigger: ', trigger, '...') plot_UE(spiketrain, UE, significance_level, binsize, winsize, winstep, pattern_hash, N, plot_params)
def test_Riehle_et_al_97_UE(self): from neo.rawio.tests.tools import (download_test_file, create_local_temp_dir, make_all_directories) from neo.test.iotest.tools import (cleanup_test_file) url = [ "https://raw.githubusercontent.com/ReScience-Archives/" + "Rostami-Ito-Denker-Gruen-2017/master/data", "https://raw.githubusercontent.com/ReScience-Archives/" + "Rostami-Ito-Denker-Gruen-2017/master/data"] shortname = "unitary_event_analysis_test_data" local_test_dir = create_local_temp_dir( shortname, os.environ.get("ELEPHANT_TEST_FILE_DIR")) files_to_download = ["extracted_data.npy", "winny131_23.gdf"] make_all_directories(files_to_download, local_test_dir) for f_cnt, f in enumerate(files_to_download): download_test_file(f, local_test_dir, url[f_cnt]) # load spike data of figure 2 of Riehle et al 1997 sys.path.append(local_test_dir) file_name = '/winny131_23.gdf' trigger = 'RS_4' t_pre = 1799 * pq.ms t_post = 300 * pq.ms spiketrain = self.load_gdf2Neo(local_test_dir + file_name, trigger, t_pre, t_post) # calculating UE ... winsize = 100 * pq.ms binsize = 5 * pq.ms winstep = 5 * pq.ms pattern_hash = [3] method = 'analytic_TrialAverage' t_start = spiketrain[0][0].t_start t_stop = spiketrain[0][0].t_stop t_winpos = ue._winpos(t_start, t_stop, winsize, winstep) significance_level = 0.05 UE = ue.jointJ_window_analysis( spiketrain, binsize, winsize, winstep, pattern_hash, method=method) # load extracted data from figure 2 of Riehle et al 1997 try: extracted_data = np.load( local_test_dir + '/extracted_data.npy').item() except UnicodeError: extracted_data = np.load( local_test_dir + '/extracted_data.npy', encoding='latin1').item() Js_sig = ue.jointJ(significance_level) sig_idx_win = np.where(UE['Js'] >= Js_sig)[0] diff_UE_rep = [] y_cnt = 0 for tr in range(len(spiketrain)): x_idx = np.sort( np.unique(UE['indices']['trial' + str(tr)], return_index=True)[1]) x = UE['indices']['trial' + str(tr)][x_idx] if len(x) > 0: # choose only the significant coincidences xx = [] for j in sig_idx_win: xx = np.append(xx, x[np.where( (x * binsize >= t_winpos[j]) & (x * binsize < t_winpos[j] + winsize))]) x_tmp = np.unique(xx) * binsize.magnitude if len(x_tmp) > 0: ue_trial = np.sort(extracted_data['ue'][y_cnt]) diff_UE_rep = np.append( diff_UE_rep, x_tmp - ue_trial) y_cnt += +1 np.testing.assert_array_less(np.abs(diff_UE_rep), 0.3) cleanup_test_file('dir', local_test_dir)