Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thankfully these phase_contrast changes are as easy as pie #577

Merged
merged 10 commits into from
Nov 22, 2023

Conversation

gvarnavi
Copy link
Member

Various small fixes and features to merge before MRS:

  • Real-space positions mask
  • Fourier probe residual aberrations plotting
  • Force rotation flag for parallax

@gvarnavi gvarnavi changed the title Thankfully this phase_contrast changes are as easy as pie Thankfully these phase_contrast changes are as easy as pie Nov 21, 2023
Comment on lines +1220 to 1221
if not positions_mask[rx, ry]:
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this skipping posistions where posistions_mask[rx,ry] == False if so is the following correct and more readable"

if posistion_mask[rx,ry] is False:
    continue 

Is it worth splitting this into two for loops so that posistion_mask is Not None, is checked once, and not per probe position?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this is skipping positions where the mask is false. Not sure I understand the for loop suggestion, this has to be checked per probe position.

Re: style - PEP8 explicitly says comparing boolean values to True/False with == or is is bad form.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. One day I'll learn to not try and refactor numpy Boolean masks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the for loop, this checks if posistion_mask is not None at every probe position. Would it be better to do ~:

 if positions_mask is None:
     intensities = get_shifted_ar(
                    diffraction_intensities[rx, ry],
                    -com_fitted_x[rx, ry],
                    -com_fitted_y[rx, ry],
                    bilinear=True,
                    device="cpu",
                )

                if crop_patterns:
                    intensities = intensities[crop_mask].reshape(
                        region_of_interest_shape
                    )

                mean_intensity += np.sum(intensities)
                amplitudes[counter] = np.sqrt(np.maximum(intensities, 0))
                counter += 1
else:
    for rx in range(diffraction_intensities.shape[0]):
            for ry in range(diffraction_intensities.shape[1]):
                  if not positions_mask[rx, ry]:
                      continue
              intensities = get_shifted_ar(
                  diffraction_intensities[rx, ry],
                  -com_fitted_x[rx, ry],
                  -com_fitted_y[rx, ry],
                  bilinear=True,
                  device="cpu",
              )

              if crop_patterns:
                  intensities = intensities[crop_mask].reshape(
                      region_of_interest_shape
                  )

                mean_intensity += np.sum(intensities)
                amplitudes[counter] = np.sqrt(np.maximum(intensities, 0))
                counter += 1

Comment on lines 2504 to 2510
def show_fourier_probe(
self,
probe=None,
remove_initial_probe_aberrations=False,
cbar=True,
scalebar=True,
pixelsize=None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of trivial, but it would be nice if these were type hinted

Comment on lines 2545 to +2546
figsize = kwargs.pop("figsize", (6, 6))
chroma_boost = kwargs.pop("chroma_boost", 2)
chroma_boost = kwargs.pop("chroma_boost", 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cool I didn't know you could pop from a dict

Comment on lines 2765 to 2771
plot_fourier_probe: bool, optional
If true, the reconstructed complex Fourier probe is displayed
remove_initial_probe_aberrations: bool, optional
If true, when plotting fourier probe, removes initial probe
to visualize changes
padding : int, optional
Pixels to pad by post rotating-cropping object
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are optional as there's no deafult value. If wanted to be treated as optional, it should be remove_initial_probe_aberrations: Optional[bool] = None, or remove_initial_probe_aberrations: bool | None = None (this needs from __future__ import annotations as the first import)

chroma_boost = kwargs.pop("chroma_boost", 2)
else:
chroma_boost = kwargs.pop("chroma_boost", 1)
chroma_boost = kwargs.pop("chroma_boost", 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is removing default for chroma_boost = 2 a preference thing?

Comment on lines +3053 to +3060
probe = list(
asnumpy(
self._return_fourier_probe(
probe,
remove_initial_probe_aberrations=remove_initial_probe_aberrations,
)
)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trivial, but I think literal [] is prefered to list

@@ -3052,6 +3092,7 @@ def show_fourier_probe(
def show_transmitted_probe(
self,
plot_fourier_probe: bool = False,
remove_initial_probe_aberrations=False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs docstring added

Comment on lines +1872 to +1874
if remove_initial_probe_aberrations:
probe_array = self.probe_fourier_residual
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same self.probe_fourier_residual[0] comment

Comment on lines +1971 to +1972
remove_initial_probe_aberrations: bool, optional
If true, when plotting fourier probe, removes initial probe
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same optional

@@ -2158,6 +2165,7 @@ def visualize(
plot_convergence: bool = True,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same implicit

Comment on lines +1220 to 1221
if not positions_mask[rx, ry]:
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. One day I'll learn to not try and refactor numpy Boolean masks

Comment on lines +1220 to 1221
if not positions_mask[rx, ry]:
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the for loop, this checks if posistion_mask is not None at every probe position. Would it be better to do ~:

 if positions_mask is None:
     intensities = get_shifted_ar(
                    diffraction_intensities[rx, ry],
                    -com_fitted_x[rx, ry],
                    -com_fitted_y[rx, ry],
                    bilinear=True,
                    device="cpu",
                )

                if crop_patterns:
                    intensities = intensities[crop_mask].reshape(
                        region_of_interest_shape
                    )

                mean_intensity += np.sum(intensities)
                amplitudes[counter] = np.sqrt(np.maximum(intensities, 0))
                counter += 1
else:
    for rx in range(diffraction_intensities.shape[0]):
            for ry in range(diffraction_intensities.shape[1]):
                  if not positions_mask[rx, ry]:
                      continue
              intensities = get_shifted_ar(
                  diffraction_intensities[rx, ry],
                  -com_fitted_x[rx, ry],
                  -com_fitted_y[rx, ry],
                  bilinear=True,
                  device="cpu",
              )

              if crop_patterns:
                  intensities = intensities[crop_mask].reshape(
                      region_of_interest_shape
                  )

                mean_intensity += np.sum(intensities)
                amplitudes[counter] = np.sqrt(np.maximum(intensities, 0))
                counter += 1

@alex-rakowski
Copy link
Collaborator

The decision was to accept these with the formatting issues, and we will address them in a subsequent PR.

@bsavitzky bsavitzky merged commit 3397349 into dev Nov 22, 2023
10 checks passed
bsavitzky added a commit to bsavitzky/py4DSTEM that referenced this pull request Mar 12, 2024
Thankfully these phase_contrast changes are as easy as pie

Former-commit-id: 3397349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants