def HIO_mode(fftmagnitude, g_k, mask, beta, steps, mode, measure=False, parameters=[100, 30, 1.5, 0.9, 1]): print( 'Running Phase-Retrieval iterations using Hybrid Input-Output method with options' ) xp = cp.get_array_module(fftmagnitude) t = time.time() error = xp.zeros(steps) normalization = 1 / (fftmagnitude.size * xp.linalg.norm(fftmagnitude)) epsilon = parameters[4] if epsilon == 0: print( 'WARNING: you are actually running ER method, if you want to get back to HIO set parammeters[4]=1' ) if mode == 'normal': print('Normal HIO implementation with epsilon = ' + str(epsilon)) if mode == 'shrink-wrap': counter = -1 maskupdate = parameters[0] nupdates = -int(-steps // maskupdate) start_sigma = parameters[1] stop_sigma = parameters[2] print('The mask evolves using shrink-wrap rules every ' + str(maskupdate) + ' steps') print('Starting smoothing with sigma: ' + str(start_sigma)) print('Ending smoothing with sigma: ' + str(stop_sigma)) sigma = xp.linspace(start_sigma, stop_sigma, nupdates) # mask = pf.autocorrelation_maskND(g_k, 0.04) # g_k = pf.fouriermod2autocorrelation(fftmagnitude) if mode == 'sparsity': counter = -1 maskupdate = parameters[0] nupdates = -int(-steps // maskupdate) # mask = pf.autocorrelation_maskND(g_k, 0.04) start_sparsity = xp.sum(mask) / mask.size end_sparsity = 0.1 print('The mask evolves using sparsity rules every ' + str(maskupdate) + ' steps') print('Starting sparsity: ' + str(start_sparsity)) print('End sparsity: ' + str(end_sparsity)) sparsity = xp.linspace(start_sparsity, end_sparsity, nupdates) if mode == 'exponential-average': alpha = parameters[3] g_exp = g_k print( 'The solution evolves using exponentially weighted average with alpha ' + str(alpha)) print('... it means the exponential average is done on past ' + str(1 / (1 - alpha)) + 'estimates') # iteration starts here for k in range(0, steps): t = algorithmStatus(t, k, steps) # phase retrieval four-iterations, this sequence minimize memory usage gp_k = xp.fft.rfftn(g_k) # alias for G_k gp_k = xp.angle(gp_k) # alias for Phi_k gp_k = xp.exp(1j * gp_k) # alias for Gp_k gp_k = fftmagnitude * gp_k # alias for Gp_k gp_k = xp.fft.irfftn(gp_k) # alias for gp_k # 4th step - updates for elements that violate object domain constraints index = xp.logical_and(gp_k > 0, mask) g_k[index] = gp_k[index] index = xp.logical_not(index) g_k[index] = epsilon * (g_k[index] - (beta * gp_k[index])) # 5th step - Shrink-wrap implementation if mode == 'shrink-wrap': if (k + 1) % maskupdate == 0: counter = counter + 1 print("smoothed mask with sigma = ", sigma[counter]) gp_k = pf.my_gaussblur(g_k, sigma[counter]) mask = pf.threshold_maskND(gp_k, 0.01) # 5th step - Shrink-wrap implementation if mode == 'sparsity': if (k + 1) % maskupdate == 0: counter = counter + 1 print("smoothed mask with sigma = ", sparsity[counter]) mask = pf.sparsity_maskND(g_k, 0.1) # update process following exponential average rules if mode == 'exponential-average': g_exp = alpha * g_exp + (1 - alpha) * g_k # measure the solution distance if measure == True: gp_k = xp.fft.rfftn(g_k) # alias for G_k gp_k = xp.abs(gp_k) # alias for Phi_k # error[k] = xp.linalg.norm(fftmagnitude - gp_k) * normalization error[k] = snrIntensity_db( fftmagnitude / fftmagnitude.sum(), xp.abs(fftmagnitude / fftmagnitude.sum() - gp_k / gp_k.sum())) if mode == 'exponential-average': g_k = g_exp return (g_k, mask, error)
def anchorUpdateSK(signal, kernel, signal_deconv=np.float32(0), iterations=10, measure=True, clip=False, verbose=True): # for code agnosticity between Numpy/Cupy xp = cp.get_array_module(signal) xps = cupyx.scipy.get_array_module(signal) # for performance evaluation start_time = time.time() if iterations<100: breakcheck = iterations else: breakcheck = 100 # normalization signal /= signal.sum() epsilon = 1e-7 # starting guess with a flat image if signal_deconv.any()==0: # xp.random.seed(0) signal_deconv = xp.full(signal.shape,0.5) + 0.01*xp.random.rand(*signal.shape) # signal_deconv = signal.copy() else: signal_deconv = signal_deconv #+ 0.1*prior.max()*xp.random.rand(*signal.shape) # normalization signal_deconv = signal_deconv/signal_deconv.sum() # to measure the distance between the guess convolved and the signal error = None if measure == True: error = xp.zeros(iterations) for i in range(iterations): # I use this property to make computation faster kernel_update = xps.ndimage.gaussian_filter(signal_deconv, sigma) # kernel_update = xps.ndimage.fourier_gaussian(signal_deconv, sigma) kernel_mirror = (kernel_update) relative_blur = my_correlation(signal_deconv, kernel_update) # compute the measured distance metric if given if measure==True: # error[i] = xp.linalg.norm(signal/signal.sum()-relative_blur/relative_blur.sum()) error[i] = snrIntensity_db(signal/signal.sum(), xp.abs(signal/signal.sum()-relative_blur/relative_blur.sum())) if (error[i] < error[i-breakcheck]) and i > breakcheck: break if verbose==True and (i % 100)==0 and measure==False: print('Iteration ' + str(i)) elif verbose==True and (i % 100)==0 and measure==True: print('Iteration ' + str(i) + ' - noise level: ' + str(error[i])) relative_blur = signal / relative_blur # avoid errors due to division by zero or inf relative_blur[xp.isinf(relative_blur)] = epsilon relative_blur = xp.nan_to_num(relative_blur) # multiplicative update, for the full model signal_deconv *= 0.5 * (my_convolution(relative_blur, kernel_mirror) + my_correlation(axisflip(relative_blur), kernel_mirror)) # signal_deconv *= (my_convolution(relative_blur, kernel_mirror) + my_correlation(relative_blur,kernel_mirror)) # multiplicative update, for the Anchor Update approximation # signal_deconv *= my_convolution(kernel_mirror, relative_blur) # multiplicative update, remaining term. This gives wrong reconstructions # signal_deconv *= my_correlation(axisflip(relative_blur), kernel_mirror) if clip: signal_deconv[signal_deconv > +1] = +1 signal_deconv[signal_deconv < -1] = -1 print("\n\n Algorithm finished. Performance:") print("--- %s seconds ----" % (time.time() - start_time)) print("--- %s sec/step ---" % ((time.time() - start_time)/iterations)) return signal_deconv, error #,kernel_update
def schulzSnyder(correlation, prior=np.float32(0), iterations=10, measure=True, clip=False, verbose=True): """ De-AutoCorrelation protocol implemented by Schultz-Snyder. It needs to be checked to assess the working procedure. Parameters ---------- correlation : TYPE DESCRIPTION. prior : TYPE, optional DESCRIPTION. The default is np.float32(0). iterations : TYPE, optional DESCRIPTION. The default is 10. measure : TYPE, optional DESCRIPTION. The default is True. clip : TYPE, optional DESCRIPTION. The default is True. verbose : TYPE, optional DESCRIPTION. The default is True. Returns ------- signal_decorr : TYPE DESCRIPTION. error : TYPE DESCRIPTION. """ xp = pyb.get_array_module(correlation) # for performance evaluation start_time = time.time() epsilon = 1e-7 if iterations < 10: breakcheck = iterations else: breakcheck = 10 # starting guess with a flat image if prior.any() == 0: signal_decorr = xp.full( correlation.shape, 0.5) + 0.01 * xp.random.rand(*correlation.shape) else: signal_decorr = prior.copy( ) #+ 0.1*prior.max()*xp.random.rand(*signal.shape) R_0 = signal_decorr.sum() signal_decorr = signal_decorr / R_0 relative_corr = xp.zeros_like(signal_decorr) # to measure the distance between the guess convolved and the signal error = None if measure == True: error = xp.zeros(iterations) for i in range(iterations): relative_corr = my_correlation(signal_decorr, signal_decorr) if measure == True: # error[i] = xp.linalg.norm(correlation/correlation.sum()-relative_corr/relative_corr.sum()) error[i] = snrIntensity_db( correlation / correlation.sum(), xp.abs(correlation / correlation.sum() - relative_corr / relative_corr.sum())) if (error[i] < error[i - breakcheck]) and i > breakcheck: break if verbose == True and (i % 100) == 0 and measure == False: print('Iteration ' + str(i)) elif verbose == True and (i % 100) == 0 and measure == True: print('Iteration ' + str(i) + ' - noise level: ' + str(error[i])) # relative_corr = 0.5*(correlation + axisflip(correlation)) / relative_corr relative_corr = (correlation) / relative_corr # avoid errors due to division by zero or inf relative_corr[xp.isinf(relative_corr)] = epsilon relative_corr = xp.nan_to_num(relative_corr) # multiplicative update # signal_decorr *= my_correlation(axisflip(signal_decorr), (relative_corr)) / R_0 # signal_decorr *= my_correlation((relative_corr), (signal_decorr)) / R_0 # signal_decorr *= (my_correlation(relative_corr, signal_decorr) + my_correlation(relative_corr, axisflip(signal_decorr))) / R_0 signal_decorr *= (my_correlation(relative_corr, signal_decorr) + my_convolution(relative_corr, signal_decorr)) / R_0 if clip: signal_decorr[signal_decorr > +1] = +1 signal_decorr[signal_decorr < -1] = -1 print("\n\n Algorithm finished. Performance:") print("--- %s seconds ----" % (time.time() - start_time)) print("--- %s sec/step ---" % ((time.time() - start_time) / iterations)) return signal_decorr, error
def anchorUpdateZ(signal, kernel, signal_deconv=np.float32(0), kerneltype='B', iterations=10, measure=True, clip=False, verbose=True): """ Reconstruction of signal_deconv from its auto-correlation signal, via a RichardsonLucy-like multiplicative procedure. At the same time, the kernel psf is deconvolved from the reconstruction so that the iteration converges corr(conv(signal_deconv, kernel), conv(signal_deconv, kernel),) -> signal. Parameters ---------- signal : ndarray, either numpy or cupy. The auto-correlation to be inverted kernel : ndarray, either numpy or cupy. Point spread function that blurred the signal. It must be signal.shape == kernel.shape. signal_deconv : ndarray, either numpy or cupy or 0. It must be signal.shape == signal_deconv.shape. The de-autocorrelated signal deconvolved with kernel at ith iteration. The default is np.float32(0). kerneltype : string. Type of kernel update used for the computation choosing from blurring directly the autocorrelation 'A', blurring the signal that is then autocorrelated 'B' and the window applied in fourier domain 'C'. The default is 'B'. iterations : int, optional Number of iteration to be done. The default is 10. measure : boolean, optional If true computes the euclidean distance between signal and the auto-correlation of signal_deconv. The default is True. clip : boolean, optional Clip the results within the range -1 to 1. Useless for the moment. The default is False. verbose : boolean, optional Print current step value. The default is True. Returns ------- signal_deconv : ndarray, either numpy or cupy. The de-autocorrelated signal deconvolved with kernel at ith iteration.. error : vector. Euclidean distance between signal and the auto-correlation of signal_deconv. Last implementation returns the SNR instead of euclidean distance. """ # for code agnosticity between Numpy/Cupy xp = pyb.get_array_module(signal) # for performance evaluation start_time = time.time() if iterations < 100: breakcheck = iterations else: breakcheck = 100 # normalization signal /= signal.sum() kernel /= kernel.sum() epsilon = 1e-7 # starting guess with a flat image if signal_deconv.any() == 0: # xp.random.seed(0) signal_deconv = xp.full(signal.shape, 0.5) + 0.01 * xp.random.rand(*signal.shape) # signal_deconv = signal.copy() else: signal_deconv = signal_deconv #+ 0.1*prior.max()*xp.random.rand(*signal.shape) # normalization signal_deconv = signal_deconv / signal_deconv.sum() # to measure the distance between the guess convolved and the signal error = None if measure == True: error = xp.zeros(iterations) for i in range(iterations): # I use this property to make computation faster K = my_convolution(signal_deconv, my_correlation(kernel, kernel)) relative_blur = my_correlation(K, signal_deconv) # compute the measured distance metric if given if measure == True: #error[i] = xp.linalg.norm(signal/signal.sum()-relative_blur/relative_blur.sum()) error[i] = snrIntensity_db( signal / signal.sum(), xp.abs(signal / signal.sum() - relative_blur / relative_blur.sum())) if (error[i] < error[i - breakcheck]) and i > breakcheck: break if verbose == True and (i % 100) == 0 and measure == False: print('Iteration ' + str(i)) elif verbose == True and (i % 100) == 0 and measure == True: print('Iteration ' + str(i) + ' - noise level: ' + str(error[i])) relative_blur = signal / relative_blur # avoid errors due to division by zero or inf relative_blur[xp.isinf(relative_blur)] = epsilon relative_blur = xp.nan_to_num(relative_blur) # multiplicative update, for the full model # signal_deconv *= 0.5 * (my_convolution(relative_blur, kernel_mirror) + my_correlation(axisflip(relative_blur), kernel_mirror)) # signal_deconv *= (my_convolution(kernel_mirror,relative_blur) + my_correlation(relative_blur, kernel_mirror)) # multiplicative update, for the Anchor Update approximation signal_deconv *= my_correlation((relative_blur), (K)) # signal_deconv *= (my_correlation(relative_blur, K) + my_convolution(relative_blur, K)) # multiplicative update, remaining term. This gives wrong reconstructions # signal_deconv *= my_correlation(axisflip(relative_blur), kernel_mirror) if clip: signal_deconv[signal_deconv > +1] = +1 signal_deconv[signal_deconv < -1] = -1 print("\n\n Algorithm finished. Performance:") print("--- %s seconds ----" % (time.time() - start_time)) print("--- %s sec/step ---" % ((time.time() - start_time) / iterations)) return signal_deconv, error #,kernel_update
def richardsonLucy(signal, kernel, prior=np.float32(0), iterations=10, measure=True, clip=False, verbose=True): """ Deconvolution using the Richardson Lucy algorithm. Parameters ---------- signal : ndarray, either numpy or cupy. The signal to be deblurred. kernel : ndarray, either numpy or cupy. Point spread function that blurred the signal. It must be signal.shape == kernel.shape. prior : ndarray, either numpy or cupy, optional the prior information to start the reconstruction. The default is np.float32(0). iterations : integer, optional Number of iteration to be done. The default is 10. measure : boolean, optional If true computes the euclidean distance between signal and the auto-correlation of signal_deconv. The default is True. clip : boolean, optional Clip the results within the range -1 to 1. The default is False. verbose : boolean, optional Print current step value. The default is True. Returns ------- signal_deconv : ndarray, either numpy or cupy. The deconvolved signal with respect the given kernel at ith iteration. error : one dimensional ndarray. Euclidean distance between signal and the auto-correlation of signal_deconv. """ xp = pyb.get_array_module(signal) start_time = time.time() if iterations < 100: breakcheck = iterations else: breakcheck = 100 epsilon = 1e-7 # starting guess with a flat image if prior.any() == 0: signal_deconv = xp.full(signal.shape, 0.5) + 0.01 * xp.random.rand(*signal.shape) else: signal_deconv = prior #+ 0.1*prior.max()*xp.random.rand(*signal.shape) kernel_mirror = axisflip(kernel) error = None if measure == True: error = xp.zeros(iterations) for i in range(iterations): if verbose == True and (i % 100) == 0: print('Iteration ' + str(i)) relative_blur = my_convolution(signal_deconv, kernel) if measure == True: # error[i] = xp.linalg.norm(signal/signal.sum()-relative_blur/relative_blur.sum()) error[i] = snrIntensity_db( signal / signal.sum(), xp.abs(signal / signal.sum() - relative_blur / relative_blur.sum())) if (error[i] < error[i - breakcheck]) and i > breakcheck: break relative_blur = signal / relative_blur # avoid errors due to division by zero or inf relative_blur[xp.isinf(relative_blur)] = epsilon relative_blur = xp.nan_to_num(relative_blur) # multiplicative update signal_deconv *= my_convolution(relative_blur, kernel_mirror) if clip: signal_deconv[signal_deconv > +1] = +1 signal_deconv[signal_deconv < -1] = -1 print("\n\n Algorithm finished. Performance:") print("--- %s seconds ----" % (time.time() - start_time)) print("--- %s sec/step ---" % ((time.time() - start_time) / iterations)) return signal_deconv, error