def test_movement_annotation_head_correction(): """Test correct detection movement artifact and dev_head_t.""" raw = read_raw_fif(raw_fname, allow_maxshield='yes').load_data() pos = read_head_pos(pos_fname) # Check 5 rotation segments are detected annot_rot, [] = annotate_movement(raw, pos, rotation_velocity_limit=5) assert (annot_rot.duration.size == 5) # Check 2 translation vel. segments are detected annot_tra, [] = annotate_movement(raw, pos, translation_velocity_limit=.05) assert (annot_tra.duration.size == 2) # Check 1 movement distance segment is detected annot_dis, disp = annotate_movement(raw, pos, mean_distance_limit=.02) assert (annot_dis.duration.size == 1) # Check correct trans mat raw.set_annotations(annot_rot + annot_tra + annot_dis) dev_head_t = compute_average_dev_head_t(raw, pos) dev_head_t_ori = np.array( [[0.9957292, -0.08688804, 0.03120615, 0.00698271], [0.09020767, 0.9875856, -0.12859731, -0.0159098], [-0.01964518, 0.1308631, 0.99120578, 0.07258289], [0., 0., 0., 1.]]) assert_allclose(dev_head_t_ori, dev_head_t['trans'], rtol=1e-5, atol=0) # Smoke test skipping time due to previous annotations. raw.set_annotations(Annotations([raw.times[0]], 0.1, 'bad')) annot_dis, disp = annotate_movement(raw, pos, mean_distance_limit=.02) assert (annot_dis.duration.size == 1)
compute_average_dev_head_t(raw, head_pos)) fig = mne.viz.plot_head_positions(head_pos) for ax, val, val_ori in zip(fig.axes[::2], average_head_dev_t['trans'][:3, 3], original_head_dev_t['trans'][:3, 3]): ax.axhline(1000 * val, color='r') ax.axhline(1000 * val_ori, color='g') # The green horizontal lines represent the original head position, whereas the # red lines are the new head position averaged over all the time points. # %% # Plot raw data with annotated movement # ------------------------------------------------------------------ mean_distance_limit = .0015 # in meters annotation_movement, hpi_disp = annotate_movement( raw, head_pos, mean_distance_limit=mean_distance_limit) raw.set_annotations(annotation_movement) raw.plot(n_channels=100, duration=20) ############################################################################## # After checking the annotated movement artifacts, calculate the new transform # and plot it: new_dev_head_t = compute_average_dev_head_t(raw, head_pos) raw.info['dev_head_t'] = new_dev_head_t fig = mne.viz.plot_alignment(raw.info, show_axes=True, subject=subject, trans=trans_fname, subjects_dir=subjects_dir) mne.viz.set_3d_view(fig, azimuth=90, elevation=60)