AdvancedPhotonSource / tike

Repository for ptychography software
http://tike.readthedocs.io
Other
29 stars 15 forks source link

NEW: Expose FFT Scaling option #298

Closed carterbox closed 9 months ago

carterbox commented 10 months ago

Purpose

Allow to the user to choose a different scaling for the FFT operation in the ptychography forward operator. This will cause the probe to converge to a different magnitude depending on which scaling the user picks.

Approach

Mostly just passed through the norm option from the FFT function. In my tests, there doesn't seem to be a qualitative difference between the test reconstructions for each of the three normalization options. In order of smallest probe magnitude to largest, the options are 'backward', 'ortho', 'forward'.

The default scaling option is 'ortho' because that one is correct for the adjoint operation tests and it is the current behavior. Which every option keeps the probe magnitude small is technically the most "precise" because floating point numbers are more precise nearer zero.

Pre-Merge Checklists

Submitter

Reviewer

pep8speaks commented 10 months ago

Hello @carterbox! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 106:70: W291 trailing whitespace Line 107:69: E202 whitespace before ')'

Comment last updated at 2024-01-09 19:45:52 UTC
carterbox commented 9 months ago

@a4894z, Did you have more comments?

a4894z commented 9 months ago

@a4894z, Did you have more comments?

Yeah, check out the attached pdf...am I using this feature correctly?

For "ortho" and "forward" I'm getting something that looks qualitatively similar but for "backward" I'm getting something totally different?

tike_2024Jan10_v0.pdf

carterbox commented 9 months ago

Those are interesting results! I expected the results to be different because changing the FFT normalization affects the magnitude of the gradient, but I haven't observed such a drastic difference on any dataset that I have tested.

Looks like using "backward" normalization convergence is less noisy. Could be because using "backward" normalization causes smaller step sizes.

Is one of those reconstructions better than the others? Can you use the same y-axis for those log plots?

a4894z commented 9 months ago

Those are interesting results! I expected the results to be different because changing the FFT normalization affects the magnitude of the gradient, but I haven't observed such a drastic difference on any dataset that I have tested.

Looks like using "backward" normalization convergence is less noisy. Could be because using "backward" normalization causes smaller step sizes.

Is one of those reconstructions better than the others? Can you use the same y-axis for those log plots?

Here's similar behavior using data from the benchmark repo (velo_19c2_Jun_IC_fly145)

I guess now we need to figure out what we're doing differently?

tike_2024Jan11_v0.pdf

carterbox commented 9 months ago

The MATLAB built-in ffts are "backward" normalized (which is the default for numpy).

We can compare using the interactive interpreter to confirm.

NumPy

>>> x = [0, 0, 0, 1, 0, 0, 0]
>>> y_ortho = np.fft.fft(x, norm='ortho')
>>> y_back = np.fft.fft(x, norm='backward')
>>> y_fore = np.fft.fft(x, norm='forward')
>>> np.round(y_ortho, 3)
array([ 0.378+0.j   , -0.341-0.164j,  0.236+0.296j, -0.084-0.368j,
       -0.084+0.368j,  0.236-0.296j, -0.341+0.164j])
>>> np.round(y_back, 3)
array([ 1.   +0.j   , -0.901-0.434j,  0.623+0.782j, -0.223-0.975j,
       -0.223+0.975j,  0.623-0.782j, -0.901+0.434j])
>>> np.round(y_fore, 3)
array([ 0.143+0.j   , -0.129-0.062j,  0.089+0.112j, -0.032-0.139j,
       -0.032+0.139j,  0.089-0.112j, -0.129+0.062j])

MATLAB

>> x = [0, 0, 0, 1, 0, 0, 0]

x =

     0     0     0     1     0     0     0

>> fft(x)

ans =

   1.0000 + 0.0000i  -0.9010 - 0.4339i   0.6235 + 0.7818i  -0.2225 - 0.9749i  -0.2225 + 0.9749i   0.6235 - 0.7818i  -0.9010 + 0.4339i