def test_self_calibrating_reconstruction(self):
     """ Test all the registered transformations.
     """
     self.num_channels = 2
     print("Process test for SelfCalibratingReconstructor ::")
     for i in range(len(self.test_cases)):
         print("Test Case " + str(i) + " " + str(self.test_cases[i]))
         image, nb_scale, optimizer, recon_type, name = self.test_cases[i]
         image_multichannel = np.repeat(image.data[np.newaxis],
                                        self.num_channels,
                                        axis=0)
         if optimizer == 'condatvu':
             formulation = "analysis"
         else:
             formulation = "synthesis"
         if recon_type == 'cartesian':
             fourier = FFT(samples=convert_mask_to_locations(self.mask),
                           shape=image.shape,
                           n_coils=self.num_channels)
         else:
             fourier = NonCartesianFFT(samples=convert_mask_to_locations(
                 self.mask),
                                       shape=image.shape,
                                       n_coils=self.num_channels)
         kspace_data = fourier.op(image_multichannel)
         linear_op, regularizer_op = \
             self.get_linear_n_regularization_operator(
                 wavelet_name=name,
                 dimension=len(fourier.shape),
                 nb_scale=2,
                 n_coils=self.num_channels,
                 gradient_formulation=formulation,
             )
         # For self calibrating reconstruction the n_coils
         # for wavelet operation is 1
         linear_op.n_coils = 1
         reconstructor = SelfCalibrationReconstructor(
             fourier_op=fourier,
             linear_op=linear_op,
             regularizer_op=regularizer_op,
             gradient_formulation=formulation,
             verbose=0,
         )
         x_final, costs, _ = reconstructor.reconstruct(
             kspace_data=kspace_data,
             optimization_alg=optimizer,
             num_iterations=self.num_iter,
         )
         fourier_0 = FFT(
             samples=convert_mask_to_locations(self.mask),
             shape=image.shape,
             n_coils=self.num_channels,
         )
         recon = fourier_0.adj_op(fourier_0.op(image_multichannel))
         np.testing.assert_allclose(np.abs(x_final),
                                    np.sqrt(np.sum(np.abs(recon)**2,
                                                   axis=0)),
                                    rtol=1e-3)
 def test_check_asserts(self):
     # Tests to check for asserts
     image, nb_scale, optimizer, recon_type, name = self.test_cases[0]
     fourier = NonCartesianFFT(
         samples=convert_mask_to_locations(self.mask),
         shape=image.shape,
     )
     kspace_data = fourier.op(image.data)
     linear_op, regularizer_op = \
         self.get_linear_n_regularization_operator(
             wavelet_name=name,
             dimension=len(fourier.shape),
             nb_scale=2,
             gradient_formulation="synthesis",
         )
     reconstructor = CalibrationlessReconstructor(
         fourier_op=fourier,
         linear_op=linear_op,
         regularizer_op=regularizer_op,
         gradient_formulation="synthesis",
         verbose=1,
     )
     np.testing.assert_raises(
         ValueError,
         reconstructor.reconstruct,
         kspace_data=kspace_data,
         optimization_alg="test_fail",
         num_iterations=self.num_iter,
     )
     fourier.n_coils = 10
     reconstructor = SelfCalibrationReconstructor(
         fourier_op=fourier,
         linear_op=linear_op,
         regularizer_op=regularizer_op,
         gradient_formulation="synthesis",
         verbose=1,
     )
     np.testing.assert_raises(
         ValueError,
         reconstructor.reconstruct,
         kspace_data=kspace_data,
         optimization_alg=optimizer,
         num_iterations=self.num_iter,
     )
#############################################################################
# FISTA optimization
# ------------------
#
# We now want to refine the zero order solution using a FISTA optimization.
# The cost function is set to Proximity Cost + Gradient Cost

# Setup the operators
linear_op = WaveletN(
    wavelet_name='sym8',
    nb_scale=4,
)
regularizer_op = SparseThreshold(Identity(), 1.5e-8, thresh_type="soft")
# Setup Reconstructor
reconstructor = SelfCalibrationReconstructor(
    fourier_op=fourier_op,
    linear_op=linear_op,
    regularizer_op=regularizer_op,
    gradient_formulation='synthesis',
    kspace_portion=0.01,
    verbose=1,
)
x_final, costs, metrics = reconstructor.reconstruct(
    kspace_data=kspace_obs,
    optimization_alg='fista',
    num_iterations=10,
)
image_rec = pysap.Image(data=x_final)
recon_ssim = ssim(image_rec, image)
print('The Reconstruction SSIM is : ' + str(recon_ssim))
 def test_stack3d_self_calibration_recon(self):
     # This test carries out a self calibration recon using Stack3D
     self.num_channels = 2
     self.z_size = 10
     for i in range(len(self.test_cases)):
         image, nb_scale, optimizer, recon_type, name = self.test_cases[i]
         if recon_type == 'cartesian' or name == 24:
             continue
         # Make a dummy 3D image from 2D
         image = np.moveaxis(
             np.repeat(image.data[np.newaxis], self.z_size, axis=0), 0, 2)
         # Make dummy multichannel image
         image = np.repeat(image[np.newaxis], self.num_channels, axis=0)
         sampling_z = np.random.randint(2, size=image.shape[3])
         sampling_z[self.z_size // 2 - 3:self.z_size // 2 + 3] = 1
         Nz = sampling_z.sum()
         mask = convert_mask_to_locations(self.mask)
         z_locations = np.repeat(convert_mask_to_locations(sampling_z),
                                 mask.shape[0])
         z_locations = z_locations[:, np.newaxis]
         kspace_loc = np.hstack([np.tile(mask, (Nz, 1)), z_locations])
         fourier = Stacked3DNFFT(kspace_loc=kspace_loc,
                                 shape=image.shape[1:],
                                 implementation='cpu',
                                 n_coils=self.num_channels)
         kspace_obs = fourier.op(image)
         if optimizer == 'condatvu':
             formulation = "analysis"
         else:
             formulation = "synthesis"
         linear_op, regularizer_op = \
             self.get_linear_n_regularization_operator(
                 wavelet_name=name,
                 dimension=len(fourier.shape),
                 nb_scale=2,
                 n_coils=2,
                 n_jobs=2,
                 gradient_formulation=formulation,
             )
         # For self calibrating reconstruction the n_coils
         # for wavelet operation is 1
         linear_op.n_coils = 1
         reconstructor = SelfCalibrationReconstructor(
             fourier_op=fourier,
             linear_op=linear_op,
             regularizer_op=regularizer_op,
             gradient_formulation=formulation,
             num_check_lips=0,
             smaps_extraction_mode='Stack',
             verbose=1,
         )
         x_final, _, _, = reconstructor.reconstruct(
             kspace_data=kspace_obs,
             optimization_alg=optimizer,
             num_iterations=5,
         )
         fourier_0 = FFT(
             samples=kspace_loc,
             shape=image.shape[1:],
             n_coils=self.num_channels,
         )
         recon = fourier_0.adj_op(fourier_0.op(image))
         np.testing.assert_allclose(
             np.abs(x_final), np.sqrt(np.sum(np.abs(recon)**2, axis=0)),
             0.1)