def test_params_to_affine(): # TODO: test with zooms and shears eps = np.finfo(np.float).eps params = np.array([.1, .5, -.6, .1, -4., 5.1, 1., 1., 1., 0., 0., 0.]) affine = np.eye(4) affine[:3, 3] = params[:3] pitch = np.array([[1., 0., 0.], [0., cos(.1), sin(.1)], [0., -sin(.1), cos(.1)]]) roll = np.array([[cos(-4.), 0., sin(-4.)], [0., 1., 0.], [-sin(-4.), 0., cos(-4.)]]) yaw = np.array([[cos(5.1), sin(5.1), 0.], [-sin(5.1), cos(5.1), 0.], [0., 0., 1.]]) affine[:3, :3] = pitch.dot(roll).dot(yaw) np.testing.assert_allclose(params_to_affine(params), affine) # Test params_to_affine is the inverse of affine_to_params affine2 = params_to_affine(affine_to_params(affine)) np.testing.assert_allclose(affine2, affine) # Test same result as spm_matrix function of spm np.testing.assert_allclose(affine, spm_matrix(params), atol=eps)
def test_params_to_affine(): # TODO: test with zooms and shears eps = np.finfo(np.float).eps params = np.array([0.1, 0.5, -0.6, 0.1, -4.0, 5.1, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0]) affine = np.eye(4) affine[:3, 3] = params[:3] pitch = np.array([[1.0, 0.0, 0.0], [0.0, cos(0.1), sin(0.1)], [0.0, -sin(0.1), cos(0.1)]]) roll = np.array([[cos(-4.0), 0.0, sin(-4.0)], [0.0, 1.0, 0.0], [-sin(-4.0), 0.0, cos(-4.0)]]) yaw = np.array([[cos(5.1), sin(5.1), 0.0], [-sin(5.1), cos(5.1), 0.0], [0.0, 0.0, 1.0]]) affine[:3, :3] = pitch.dot(roll).dot(yaw) np.testing.assert_allclose(spm_internals.params_to_affine(params), affine) # Test params_to_affine is the inverse of affine_to_params affine2 = spm_internals.params_to_affine(spm_internals.affine_to_params(affine)) np.testing.assert_allclose(affine2, affine)
def test_affine_to_params(): # TODO: test with zooms and shears eps = np.finfo(np.float).eps affine = np.eye(4) affine[:3, 3] = [1.1, 0.55, -.3] pitch = np.array([[1., 0., 0.], [0., 0., 1.], [0., -1., 0.]]) roll = np.array([[cos(-pi / 3.), 0., sin(-pi / 3.)], [0., 1., 0.], [-sin(-pi / 3.), 0., cos(-pi / 3.)]]) yaw = np.array([[cos(pi / 4.), sin(pi / 4.), 0.], [-sin(pi / 4.), cos(pi / 4.), 0.], [0., 0., 1.]]) affine[:3, :3] = pitch.dot(roll).dot(yaw) params = np.array([1.1, 0.55, -.3, pi / 2., -pi / 3., pi / 4., 1., 1., 1., 0., 0., 0.]) np.testing.assert_allclose(affine_to_params(affine), params, atol=eps) # Test affine_to_params is the inverse of params_to_affine params2 = affine_to_params(params_to_affine(params)) np.testing.assert_allclose(params2, params, atol=eps) # Test same result as spm_matrix function of spm np.testing.assert_allclose(params, spm_imatrix(affine), atol=1e4 * eps)
def _run_interface(self, runtime): # Set the realignement options realign = spm.Realign() realign.inputs.paths = self.inputs.paths realign.inputs.in_files = self.inputs.in_file realign.inputs.register_to_mean = self.inputs.register_to_mean realign.inputs.quality = 0.9 realign.inputs.fwhm = 5. realign.inputs.separation = 4 # TODO: understand this parameter realign.inputs.interp = 2 if self.inputs.correct_tagging: # Estimate the realignement parameters realign.inputs.jobtype = 'estimate' realign.run() parameters_file = realign.aggregate_outputs().get()[ 'realignment_parameters'] rea_parameters = np.loadtxt(parameters_file) # Correct for tagging: equal means for control and tag scans n_scans = len(rea_parameters) if self.inputs.control_scans: control_scans = self.inputs.control_scans else: control_scans = range(0, n_scans, 2) if self.inputs.tag_scans: tag_scans = self.inputs.tag_scans else: tag_scans = range(1, n_scans, 2) gap = np.mean(rea_parameters[control_scans], axis=0) -\ np.mean(rea_parameters[tag_scans], axis=0) rea_parameters[control_scans] -= gap / 2. rea_parameters[tag_scans] += gap / 2. # Save the corrected realignement parameters np.savetxt(parameters_file, rea_parameters, delimiter=' ') # Save the corrected transforms for each frame in spm compatible # .mat. This .mat serves as header for spm in case of 4D files affine = spm_affine(self.inputs.in_file) rea_affines = np.zeros((4, 4, n_scans)) for n_scan, param in enumerate(rea_parameters): rea_affines[..., n_scan] = params_to_affine(param).dot(affine) affines_file = os.path.splitext(self.inputs.in_file)[0] + '.mat' savemat(affines_file, dict(mat=rea_affines)) else: realign.inputs.jobtype = 'estimate' realign.inputs.register_to_mean = self.inputs.register_to_mean realign.run() # Reslice and save the aligned volumes # TODO: update Realign class in nipype.interfaces.spm.preprocess after # bug is corrected realign = spm.Realign() realign.inputs.paths = self.inputs.paths realign.inputs.in_files = self.inputs.in_file realign.inputs.jobtype = 'write' realign.run() return runtime
def _run_interface(self, runtime): # Set the realignement options realign = spm.Realign() realign.inputs.paths = self.inputs.paths realign.inputs.in_files = self.inputs.in_file realign.inputs.register_to_mean = self.inputs.register_to_mean realign.inputs.quality = 0.9 realign.inputs.fwhm = 5. realign.inputs.separation = 4 # TODO: understand this parameter realign.inputs.interp = 2 if self.inputs.correct_tagging: # Estimate the realignement parameters realign.inputs.jobtype = 'estimate' realign.run() parameters_file = realign.aggregate_outputs().get()[ 'realignment_parameters'] rea_parameters = np.loadtxt(parameters_file) # Correct for tagging: equal means for control and tag scans n_scans = len(rea_parameters) if self.inputs.control_scans: control_scans = self.inputs.control_scans else: control_scans = range(0, n_scans, 2) if self.inputs.tag_scans: tag_scans = self.inputs.tag_scans else: tag_scans = range(1, n_scans, 2) gap = np.mean(rea_parameters[control_scans], axis=0) -\ np.mean(rea_parameters[tag_scans], axis=0) rea_parameters[control_scans] -= gap / 2. rea_parameters[tag_scans] += gap / 2. # Save the corrected realignement parameters np.savetxt(parameters_file, rea_parameters, delimiter=' ') # Save the corrected transforms for each frame in spm compatible # .mat. This .mat serves as header for spm in case of 4D files affine = spm_affine(self.inputs.in_file) rea_affines = np.zeros((4, 4, n_scans)) for n_scan, param in enumerate(rea_parameters): rea_affines[..., n_scan] = params_to_affine(param).dot(affine) affines_file = os.path.splitext(self.inputs.in_file)[0] + '.mat' savemat(affines_file, dict(mat=rea_affines)) else: realign.inputs.jobtype = 'estimate' realign.inputs.register_to_mean = self.inputs.register_to_mean realign.run() # Reslice and save the aligned volumes realign = spm.Realign() realign.inputs.paths = self.inputs.paths realign.inputs.in_files = self.inputs.in_file realign.inputs.jobtype = 'write' realign.run() return runtime
def test_params_to_affine(): # TODO: test with zooms and shears eps = np.finfo(np.float).eps params = np.array([.1, .5, -.6, .1, -4., 5.1, 1., 1., 1., 0., 0., 0.]) affine = np.eye(4) affine[:3, 3] = params[:3] pitch = np.array([[1., 0., 0.], [0., cos(.1), sin(.1)], [0., -sin(.1), cos(.1)]]) roll = np.array([[cos(-4.), 0., sin(-4.)], [0., 1., 0.], [-sin(-4.), 0., cos(-4.)]]) yaw = np.array([[cos(5.1), sin(5.1), 0.], [-sin(5.1), cos(5.1), 0.], [0., 0., 1.]]) affine[:3, :3] = pitch.dot(roll).dot(yaw) np.testing.assert_allclose(spm_internals.params_to_affine(params), affine) # Test params_to_affine is the inverse of affine_to_params affine2 = spm_internals.params_to_affine( spm_internals.affine_to_params(affine)) np.testing.assert_allclose(affine2, affine)
def test_affine_to_params(): # TODO: test with zooms and shears eps = np.finfo(np.float).eps affine = np.eye(4) affine[:3, 3] = [1.1, 0.55, -0.3] pitch = np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, -1.0, 0.0]]) roll = np.array([[cos(-pi / 3.0), 0.0, sin(-pi / 3.0)], [0.0, 1.0, 0.0], [-sin(-pi / 3.0), 0.0, cos(-pi / 3.0)]]) yaw = np.array([[cos(pi / 4.0), sin(pi / 4.0), 0.0], [-sin(pi / 4.0), cos(pi / 4.0), 0.0], [0.0, 0.0, 1.0]]) affine[:3, :3] = pitch.dot(roll).dot(yaw) params = np.array([1.1, 0.55, -0.3, pi / 2.0, -pi / 3.0, pi / 4.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0]) np.testing.assert_allclose(spm_internals.affine_to_params(affine), params, atol=eps) # Test affine_to_params is the inverse of params_to_affine params2 = spm_internals.affine_to_params(spm_internals.params_to_affine(params)) np.testing.assert_allclose(params2, params, atol=eps)
def test_affine_to_params(): # TODO: test with zooms and shears eps = np.finfo(np.float).eps affine = np.eye(4) affine[:3, 3] = [1.1, 0.55, -.3] pitch = np.array([[1., 0., 0.], [0., 0., 1.], [0., -1., 0.]]) roll = np.array([[cos(-pi / 3.), 0., sin(-pi / 3.)], [0., 1., 0.], [-sin(-pi / 3.), 0., cos(-pi / 3.)]]) yaw = np.array([[cos(pi / 4.), sin(pi / 4.), 0.], [-sin(pi / 4.), cos(pi / 4.), 0.], [0., 0., 1.]]) affine[:3, :3] = pitch.dot(roll).dot(yaw) params = np.array([1.1, 0.55, -.3, pi / 2., -pi / 3., pi / 4., 1., 1., 1., 0., 0., 0.]) np.testing.assert_allclose(spm_internals.affine_to_params(affine), params, atol=eps) # Test affine_to_params is the inverse of params_to_affine params2 = spm_internals.affine_to_params( spm_internals.params_to_affine(params)) np.testing.assert_allclose(params2, params, atol=eps)