def test_vol_rms_diff(): # We make a fake 4D image shape_3d = (2, 3, 4) V = np.prod(shape_3d) T = 10 # The number of 3D volumes # Make a 2D array that we will reshape to 4D arr_2d = np.random.normal(size=(V, T)) differences = np.diff(arr_2d, axis=1) exp_rms = np.sqrt(np.mean(differences ** 2, axis=0)) # Reshape to 4D and run function arr_4d = np.reshape(arr_2d, shape_3d + (T,)) actual_rms = diagnostics.vol_rms_diff(arr_4d) assert_almost_equal(actual_rms, exp_rms)
plt.plot(vol_stds, label='vol std') plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.xlabel('Voxel') plt.ylabel('Volume standard deviation') plt.title('Volume Standard Deviation_' + i) plt.legend(fontsize=8) plt.savefig(i + '_vol_std.png') plt.close() # plot the RMS difference values for i in run_img_result.keys(): img = nib.load(run_img_result[i]) data = img.get_data() rms_dvals = dg.vol_rms_diff(data) rms_outliers, rms_thresholds = dg.iqr_outliers(rms_dvals) N = len(rms_dvals) x = np.arange(N) plt.plot(rms_dvals, label='vol RMS differences') plt.plot(x[rms_outliers], rms_dvals[rms_outliers], 'o', label='outliers') plt.plot([0, N], [rms_thresholds[0], rms_thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [rms_thresholds[1], rms_thresholds[1]], ':', label='IQR hi') plt.xlabel('Voxel') plt.ylabel('Volumn RMS difference values') plt.title('Volume RMS difference values_' + i) plt.legend(fontsize=8)
* Calculate the RMS difference values for the image data; * Use the ``iqr_outlier`` function to return indices of possible outliers in this RMS difference vector; On the same plot, plot the following: * The RMS vector; * The identified outlier points marked with an `o` marker; * A horizontal dashed line at the lower IRQ threshold; * A horizontal dashed line at the higher IRQ threshold; IMPORTANT - save this plot as ``vol_rms_outliers.png`` """ rmsd = diagnostics.vol_rms_diff(data) outliers_rms_index, thres_rms = diagnostics.iqr_outliers(rmsd) plt.plot(rmsd, 'r', label='rms differences values') for i in outliers_rms_index: plt.plot(i, rmsd[i], 'o', color='blue') plt.axhline(y=thres_rms[0], color='g', ls='dashed', label='low threshold') plt.axhline(y=thres_rms[1], color='black', ls='dashed', label='high threshold') plt.legend(loc=1) plt.xlabel('volume') plt.ylabel('rms difference') plt.savefig('vol_rms_outliers.png') plt.clf() """ Use the ``extend_diff_outliers`` to label outliers Use ``extend_diff_outliers`` on the output from ``iqr_outliers`` on the RMS
* Calculate the RMS difference values for the image data; * Use the ``iqr_outlier`` function to return indices of possible outliers in this RMS difference vector; On the same plot, plot the following: * The RMS vector; * The identified outlier points marked with an `o` marker; * A horizontal dashed line at the lower IRQ threshold; * A horizontal dashed line at the higher IRQ threshold; IMPORTANT - save this plot as ``vol_rms_outliers.png`` """ rmsd = diagnostics.vol_rms_diff(data) outliers_rms_index, thres_rms = diagnostics.iqr_outliers(rmsd) plt.plot(rmsd, "r", label="rms differences values") for i in outliers_rms_index: plt.plot(i, rmsd[i], "o", color="blue") plt.axhline(y=thres_rms[0], color="g", ls="dashed", label="low threshold") plt.axhline(y=thres_rms[1], color="black", ls="dashed", label="high threshold") plt.legend(loc=1) plt.xlabel("volume") plt.ylabel("rms difference") plt.savefig("vol_rms_outliers.png") plt.clf() """ Use the ``extend_diff_outliers`` to label outliers
+ str(name) + '.txt', outliers) N = len(vol_stds) x = np.arange(N) plt.plot(vol_stds, label='voxels std ' + str(name)) plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.title('voxels std ' + str(name)) plt.xlabel('time') plt.legend(fontsize=11, \ ncol=2, loc=9, borderaxespad=0.2) plt.savefig(project_path+'fig/outliers/%s_vol_std.png' %str(name)) plt.close() #RMS difference values rms_dvals = diagnostics.vol_rms_diff(data) rms_outliers, rms_thresholds = diagnostics.iqr_outliers(rms_dvals) #Plot N = len(rms_dvals) x = np.arange(N) plt.plot(rms_dvals, label='vol RMS differences ' + str(name)) plt.plot(x[rms_outliers], rms_dvals[rms_outliers], 'o', label='outliers') plt.plot([0, N], [rms_thresholds[0], rms_thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [rms_thresholds[1], rms_thresholds[1]], ':', label='IQR hi') plt.title('voxels rms difference ' + str(name)) plt.xlabel('time') plt.legend(fontsize=11, \ ncol=2, loc=9, borderaxespad=0.2) plt.savefig(project_path+'fig/outliers/%s_vol_rms_outliers.png'%str(name)) plt.close() #Label the outliers
+ str(name) + '.txt', outliers) N = len(vol_stds) x = np.arange(N) plt.plot(vol_stds, label='voxels std ' + str(name)) plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.title('voxels std ' + str(name)) plt.xlabel('time') plt.legend(fontsize=11, \ ncol=2, loc=9, borderaxespad=0.2) plt.savefig(project_path + 'fig/outliers/%s_vol_std.png' % str(name)) plt.close() #RMS difference values rms_dvals = diagnostics.vol_rms_diff(data) rms_outliers, rms_thresholds = diagnostics.iqr_outliers(rms_dvals) #Plot N = len(rms_dvals) x = np.arange(N) plt.plot(rms_dvals, label='vol RMS differences ' + str(name)) plt.plot(x[rms_outliers], rms_dvals[rms_outliers], 'o', label='outliers') plt.plot([0, N], [rms_thresholds[0], rms_thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [rms_thresholds[1], rms_thresholds[1]], ':', label='IQR hi') plt.title('voxels rms difference ' + str(name)) plt.xlabel('time') plt.legend(fontsize=11, \
plt.plot(vol_stds, label='vol std') plt.plot(x[outliers], vol_stds[outliers], 'o', label='outliers') plt.plot([0, N], [thresholds[0], thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [thresholds[1], thresholds[1]], ':', label='IQR hi') plt.xlabel('Voxel') plt.ylabel('Volume standard deviation') plt.title('Volume Standard Deviation_' + i) plt.legend(fontsize = 8) plt.savefig( i + '_vol_std.png') plt.close() # plot the RMS difference values for i in run_img_result.keys(): img = nib.load(run_img_result[i]) data = img.get_data() rms_dvals = dg.vol_rms_diff(data) rms_outliers, rms_thresholds = dg.iqr_outliers(rms_dvals) N = len(rms_dvals) x = np.arange(N) plt.plot(rms_dvals, label='vol RMS differences') plt.plot(x[rms_outliers], rms_dvals[rms_outliers], 'o', label='outliers') plt.plot([0, N], [rms_thresholds[0], rms_thresholds[0]], ':', label='IQR lo') plt.plot([0, N], [rms_thresholds[1], rms_thresholds[1]], ':', label='IQR hi') plt.xlabel('Voxel') plt.ylabel('Volumn RMS difference values') plt.title('Volume RMS difference values_' + i) plt.legend(fontsize = 8) plt.savefig( i + '_vol_rms_outliers.png') plt.close() # plot the extended_vol_rms_outliers