def test_dipole(): """Test dipole object.""" hnn_core_root = op.dirname(hnn_core.__file__) params_fname = op.join(hnn_core_root, 'param', 'default.json') dpl_out_fname = '/tmp/dpl1.txt' params = read_params(params_fname) times = np.random.random(6000) data = np.random.random((6000, 3)) dipole = Dipole(times, data) dipole.baseline_renormalize(params) dipole.convert_fAm_to_nAm() dipole.scale(params['dipole_scalefctr']) dipole.smooth(params['dipole_smooth_win'] / params['dt']) dipole.plot(show=False) viz.plot_dipole([dipole, dipole], show=False) dipole.write(dpl_out_fname) dipole_read = read_dipole(dpl_out_fname) assert_allclose(dipole_read.times, dipole.times, rtol=0, atol=0.00051) for dpl_key in dipole.data.keys(): assert_allclose(dipole_read.data[dpl_key], dipole.data[dpl_key], rtol=0, atol=0.000051) # average two identical dipole objects dipole_avg = average_dipoles([dipole, dipole_read]) for dpl_key in dipole_avg.data.keys(): assert_allclose(dipole_read.data[dpl_key], dipole_avg.data[dpl_key], rtol=0, atol=0.000051) with pytest.raises(ValueError, match="Dipole at index 0 was already an " "average of 2 trials"): dipole_avg = average_dipoles([dipole_avg, dipole_read])
def test_dipole(tmpdir, run_hnn_core_fixture): """Test dipole object.""" hnn_core_root = op.dirname(hnn_core.__file__) params_fname = op.join(hnn_core_root, 'param', 'default.json') dpl_out_fname = tmpdir.join('dpl1.txt') params = read_params(params_fname) times = np.random.random(6000) data = np.random.random((6000, 3)) dipole = Dipole(times, data) dipole.baseline_renormalize(params['N_pyr_x'], params['N_pyr_y']) dipole.convert_fAm_to_nAm() dipole.scale(params['dipole_scalefctr']) dipole.smooth(params['dipole_smooth_win'] / params['dt']) dipole.plot(show=False) plot_dipole([dipole, dipole], show=False) dipole.write(dpl_out_fname) dipole_read = read_dipole(dpl_out_fname) assert_allclose(dipole_read.times, dipole.times, rtol=0, atol=0.00051) for dpl_key in dipole.data.keys(): assert_allclose(dipole_read.data[dpl_key], dipole.data[dpl_key], rtol=0, atol=0.000051) # average two identical dipole objects dipole_avg = average_dipoles([dipole, dipole_read]) for dpl_key in dipole_avg.data.keys(): assert_allclose(dipole_read.data[dpl_key], dipole_avg.data[dpl_key], rtol=0, atol=0.000051) with pytest.raises(ValueError, match="Dipole at index 0 was already an " "average of 2 trials"): dipole_avg = average_dipoles([dipole_avg, dipole_read]) # test postproc dpls_raw, net = run_hnn_core_fixture(backend='joblib', n_jobs=1, reduced=True, record_isoma=True, record_vsoma=True, postproc=False) dpls, _ = run_hnn_core_fixture(backend='joblib', n_jobs=1, reduced=True, record_isoma=True, record_vsoma=True, postproc=True) with pytest.raises(AssertionError): assert_allclose(dpls[0].data['agg'], dpls_raw[0].data['agg']) dpls_raw[0].post_proc(net.params['N_pyr_x'], net.params['N_pyr_y'], net.params['dipole_smooth_win'] / net.params['dt'], net.params['dipole_scalefctr']) assert_allclose(dpls_raw[0].data['agg'], dpls[0].data['agg'])
dpl.scale(dpl_scalefctr) ############################################################################### # Finally, we plot the driving spike histogram, empirical and simulated median # nerve evoked response waveforms, and output spike histogram. fig, axes = plt.subplots(3, 1, sharex=True, figsize=(6, 6), constrained_layout=True) net.cell_response.plot_spikes_hist(ax=axes[0], spike_types=['evprox', 'evdist'], show=False) axes[1].axhline(0, c='k', ls=':', label='_nolegend_') axes[1].plot(1e3 * stc.times, dipole_tc, 'r--') average_dipoles(dpls).plot(ax=axes[1], show=False) axes[1].legend(['MNE label average', 'HNN simulation']) axes[1].set_ylabel('Current Dipole (nAm)') net.cell_response.plot_spikes_raster(ax=axes[2]) ############################################################################### # References # ---------- # .. [1] Jones, S. R., Pritchett, D. L., Stufflebeam, S. M., Hämäläinen, M. # & Moore, C. I. Neural correlates of tactile detection: a combined # magnetoencephalography and biophysically based computational modeling # study. J. Neurosci. 27, 10751–10764 (2007). # .. [2] Neymotin SA, Daniels DS, Caldwell B, McDougal RA, Carnevale NT, # Jas M, Moore CI, Hines ML, Hämäläinen M, Jones SR. Human Neocortical # Neurosolver (HNN), a new software tool for interpreting the cellular and # network origin of human MEG/EEG data. eLife 9, e51214 (2020).
def test_dipole(tmpdir, run_hnn_core_fixture): """Test dipole object.""" hnn_core_root = op.dirname(hnn_core.__file__) params_fname = op.join(hnn_core_root, 'param', 'default.json') dpl_out_fname = tmpdir.join('dpl1.txt') params = read_params(params_fname) times = np.arange(0, 6000 * params['dt'], params['dt']) data = np.random.random((6000, 3)) dipole = Dipole(times, data) dipole._baseline_renormalize(params['N_pyr_x'], params['N_pyr_y']) dipole._convert_fAm_to_nAm() # test smoothing and scaling dipole_raw = dipole.copy() dipole.scale(params['dipole_scalefctr']) dipole.smooth(window_len=params['dipole_smooth_win']) with pytest.raises(AssertionError): assert_allclose(dipole.data['agg'], dipole_raw.data['agg']) assert_allclose( dipole.data['agg'], (params['dipole_scalefctr'] * dipole_raw.smooth(params['dipole_smooth_win']).data['agg'])) dipole.plot(show=False) plot_dipole([dipole, dipole], show=False) # Test IO dipole.write(dpl_out_fname) dipole_read = read_dipole(dpl_out_fname) assert_allclose(dipole_read.times, dipole.times, rtol=0, atol=0.00051) for dpl_key in dipole.data.keys(): assert_allclose(dipole_read.data[dpl_key], dipole.data[dpl_key], rtol=0, atol=0.000051) # average two identical dipole objects dipole_avg = average_dipoles([dipole, dipole_read]) for dpl_key in dipole_avg.data.keys(): assert_allclose(dipole_read.data[dpl_key], dipole_avg.data[dpl_key], rtol=0, atol=0.000051) with pytest.raises(ValueError, match="Dipole at index 0 was already an " "average of 2 trials"): dipole_avg = average_dipoles([dipole_avg, dipole_read]) # average an n_of_1 dipole list single_dpl_avg = average_dipoles([dipole]) for dpl_key in single_dpl_avg.data.keys(): assert_allclose(dipole_read.data[dpl_key], single_dpl_avg.data[dpl_key], rtol=0, atol=0.000051) # average dipole list with one dipole object and a zero dipole object n_times = len(dipole_read.data['agg']) dpl_null = Dipole(np.zeros(n_times, ), np.zeros((n_times, 3))) dpl_1 = [dipole, dpl_null] dpl_avg = average_dipoles(dpl_1) for dpl_key in dpl_avg.data.keys(): assert_allclose(dpl_1[0].data[dpl_key] / 2., dpl_avg.data[dpl_key]) # Test experimental dipole dipole_exp = Dipole(times, data[:, 1]) dipole_exp.write(dpl_out_fname) dipole_exp_read = read_dipole(dpl_out_fname) assert_allclose(dipole_exp.data['agg'], dipole_exp_read.data['agg'], rtol=1e-2) dipole_exp_avg = average_dipoles([dipole_exp, dipole_exp]) assert_allclose(dipole_exp.data['agg'], dipole_exp_avg.data['agg']) # XXX all below to be deprecated in 0.3 dpls_raw, net = run_hnn_core_fixture(backend='joblib', n_jobs=1, reduced=True, record_isoma=True, record_vsoma=True) # test deprecation of postproc with pytest.warns(DeprecationWarning, match='The postproc-argument is deprecated'): dpls, _ = run_hnn_core_fixture(backend='joblib', n_jobs=1, reduced=True, record_isoma=True, record_vsoma=True, postproc=True) with pytest.raises(AssertionError): assert_allclose(dpls[0].data['agg'], dpls_raw[0].data['agg']) dpls_raw[0]._post_proc(net._params['dipole_smooth_win'], net._params['dipole_scalefctr']) assert_allclose(dpls_raw[0].data['agg'], dpls[0].data['agg'])