Exemple #1
0
 def from_kspace_to_nc_kspace_and_traj(images, kspaces):
     if acq_type == 'radial':
         traj = get_radial_trajectory(image_size, **acq_kwargs)
     elif acq_type == 'cartesian':
         traj = get_debugging_cartesian_trajectory()
     elif acq_type == 'spiral':
         traj = get_spiral_trajectory(image_size, **acq_kwargs)
     else:
         raise NotImplementedError(f'{acq_type} dataset not implemented yet.')
     interpob = nfft_ob._extract_nufft_interpob()
     nufftob_back = kbnufft_adjoint(interpob, multiprocessing=multiprocessing)
     nufftob_forw = kbnufft_forward(interpob, multiprocessing=multiprocessing)
     dcomp = calculate_density_compensator(
         interpob,
         nufftob_forw,
         nufftob_back,
         traj[0],
     )
     traj = tf.repeat(traj, tf.shape(images)[0], axis=0)
     orig_image_channels = tf_ortho_ifft2d(kspaces)
     nc_kspace = nufft(nfft_ob, orig_image_channels, traj, image_size, multiprocessing=multiprocessing)
     nc_kspace_scaled = nc_kspace * scale_factor
     images_scaled = images * scale_factor
     images_channeled = images_scaled[..., None]
     nc_kspaces_channeled = nc_kspace_scaled[..., None]
     orig_shape = tf.ones([tf.shape(kspaces)[0]], dtype=tf.int32) * tf.shape(kspaces)[-1]
     dcomp = tf.ones([tf.shape(kspaces)[0], tf.shape(dcomp)[0]], dtype=dcomp.dtype) * dcomp[None, :]
     extra_args = (orig_shape, dcomp)
     smaps = non_cartesian_extract_smaps(nc_kspace, traj, dcomp, nufftob_back, orig_shape)
     return (nc_kspaces_channeled, traj, smaps, extra_args), images_channeled
Exemple #2
0
 def from_volume_to_nc_kspace_and_traj(volume):
     if acq_type == 'radial_stacks':
         traj = get_stacks_of_radial_trajectory(volume_size, **acq_kwargs)
     elif acq_type == 'spiral_stacks':
         traj = get_stacks_of_spiral_trajectory(volume_size, **acq_kwargs)
     else:
         raise NotImplementedError(
             f'{acq_type} dataset not implemented yet.')
     if compute_dcomp:
         interpob = nfft_ob._extract_nufft_interpob()
         nufftob_back = kbnufft_adjoint(interpob)
         nufftob_forw = kbnufft_forward(interpob)
         dcomp = calculate_density_compensator(
             interpob,
             nufftob_forw,
             nufftob_back,
             traj[0],
         )
     # need to add batch and coil dimension to the volume
     nc_kspace = nufft(nfft_ob, volume[None, None, ...], traj, volume_size)
     nc_kspace_scaled = nc_kspace * scale_factor
     volume_scaled = tf.abs(volume * scale_factor)
     volume_channeled = volume_scaled[None, ..., None]
     nc_kspaces_channeled = nc_kspace_scaled[..., None]
     orig_shape = tf.shape(volume)[None, ...]
     extra_args = (orig_shape, )
     if compute_dcomp:
         dcomp = tf.ones([1, tf.shape(dcomp)[0]],
                         dtype=dcomp.dtype) * dcomp[None, :]
         extra_args += (dcomp, )
     return (nc_kspaces_channeled, traj, extra_args), volume_channeled
Exemple #3
0
def test_density_compensators_tf():
    # This is a simple test to ensure that the code works only!
    # We still dont have a method to test if the results are correct
    ktraj, nufft_ob, torch_forward, torch_backward = setup()
    interpob = nufft_ob._extract_nufft_interpob()
    tf_ktraj = tf.convert_to_tensor(ktraj)
    nufftob_back = kbnufft_adjoint(interpob)
    nufftob_forw = kbnufft_forward(interpob)
    tf_dcomp = calculate_density_compensator(interpob, nufftob_forw,
                                             nufftob_back, tf_ktraj)
Exemple #4
0
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     interpob = nufft_ob._extract_nufft_interpob()
     self.nufftob_back = kbnufft_adjoint(interpob,
                                         multiprocessing=False)
     self.nufftob_forw = kbnufft_forward(interpob,
                                         multiprocessing=False)
     if acq_type == 'radial':
         self.traj = get_radial_trajectory(image_size, af=af)
     elif acq_type == 'cartesian':
         self.traj = get_debugging_cartesian_trajectory()
     elif acq_type == 'spiral':
         self.traj = get_spiral_trajectory(image_size, af=af)
     else:
         raise NotImplementedError(
             f'{acq_type} dataset not implemented yet.')
     self.dcomp = calculate_density_compensator(
         interpob,
         self.nufftob_forw,
         self.nufftob_back,
         self.traj[0],
     )
Exemple #5
0
 def preprocessing(self, image, kspace):
     traj = self.generate_trajectory()
     interpob = self.nufft_obj._extract_nufft_interpob()
     nufftob_forw = kbnufft_forward(interpob, multiprocessing=True)
     nufftob_back = kbnufft_adjoint(interpob, multiprocessing=True)
     if self.dcomp:
         dcomp = calculate_density_compensator(
             interpob,
             nufftob_forw,
             nufftob_back,
             traj[0],
         )
     traj = tf.repeat(traj, tf.shape(image)[0], axis=0)
     orig_image_channels = ortho_ifft2d(kspace)
     if self.crop_image_data:
         image = adjust_image_size(image, self.image_size)
     nc_kspace = nufft(self.nufft_obj, orig_image_channels, traj, self.image_size, multicoil=self.multicoil)
     nc_kspace, image = scale_tensors(nc_kspace, image, scale_factor=self.scale_factor)
     image = image[..., None]
     nc_kspaces_channeled = nc_kspace[..., None]
     orig_shape = tf.ones([tf.shape(kspace)[0]], dtype=tf.int32) * self.image_size[-1]
     if not self.crop_image_data:
         output_shape = tf.shape(image)[1:][None, :]
         output_shape = tf.tile(output_shape, [tf.shape(image)[0], 1])
     extra_args = (orig_shape,)
     if self.dcomp:
         dcomp = tf.ones(
             [tf.shape(kspace)[0], tf.shape(dcomp)[0]],
             dtype=dcomp.dtype,
         ) * dcomp[None, :]
         extra_args += (dcomp,)
     model_inputs = (nc_kspaces_channeled, traj)
     if self.multicoil:
         smaps = non_cartesian_extract_smaps(nc_kspace, traj, dcomp, nufftob_back, self.image_size)
         model_inputs += (smaps,)
     if not self.crop_image_data:
         model_inputs += (output_shape,)
     model_inputs += (extra_args,)
     return model_inputs, image
Exemple #6
0
 def from_kspace_to_nc_kspace_and_traj(images, kspaces):
     if acq_type == 'radial':
         traj = get_radial_trajectory(image_size, **acq_kwargs)
     elif acq_type == 'cartesian':
         traj = get_debugging_cartesian_trajectory()
     elif acq_type == 'spiral':
         traj = get_spiral_trajectory(image_size, **acq_kwargs)
     else:
         raise NotImplementedError(f'{acq_type} dataset not implemented yet.')
     if compute_dcomp:
         interpob = nfft_ob._extract_nufft_interpob()
         nufftob_back = kbnufft_adjoint(interpob, multiprocessing=True)
         nufftob_forw = kbnufft_forward(interpob, multiprocessing=True)
         dcomp = calculate_density_compensator(
             interpob,
             nufftob_forw,
             nufftob_back,
             traj[0],
         )
     traj = tf.repeat(traj, tf.shape(images)[0], axis=0)
     orig_image = tf_unmasked_adj_op(kspaces[..., None])
     nc_kspace = nufft(nfft_ob, orig_image[:, None, ..., 0], traj, image_size)
     nc_kspace_scaled = nc_kspace * scale_factor
     images_scaled = images * scale_factor
     # Here implement gridding
     if gridding:
         pi = tf.constant(math.pi)
         def tf_grid_nc(nc_kspace_traj):
             nc_kspace, traj = nc_kspace_traj
             X_grid, Y_grid = tf.meshgrid(
                 tf.range(-pi, pi, 2*pi / image_size[0]),
                 tf.range(-pi, pi, 2*pi / image_size[1]),
             )
             nc_kspace = tf.numpy_function(
                 grid_non_cartesian,
                 [traj, nc_kspace, X_grid, Y_grid],
                 tf.complex64,
             )
             return nc_kspace
         nc_kspace_scaled = tf.nest.map_structure(tf.stop_gradient, tf.map_fn(
             tf_grid_nc,
             (nc_kspace_scaled, traj),
             fn_output_signature=tf.complex64,
             parallel_iterations=multiprocessing.cpu_count(),
         ))
         nc_kspace_scaled.set_shape([
             None,
             *image_size,
         ])
         traj = tf.ones_like(nc_kspace_scaled)
     images_channeled = images_scaled[..., None]
     nc_kspaces_channeled = nc_kspace_scaled[..., None]
     orig_shape = tf.ones([tf.shape(kspaces)[0]], dtype=tf.int32) * tf.shape(kspaces)[-1]
     extra_args = (orig_shape,)
     if compute_dcomp:
         dcomp = tf.ones([tf.shape(kspaces)[0], tf.shape(dcomp)[0]], dtype=dcomp.dtype) * dcomp[None, :]
         extra_args += (dcomp,)
     if gridding:
         return (nc_kspaces_channeled, traj), images_channeled
     else:
         return (nc_kspaces_channeled, traj, extra_args), images_channeled