From d97164153c5b6e3a13d33a759a1ebc66ff65d0b1 Mon Sep 17 00:00:00 2001 From: ellisrae Date: Wed, 18 Oct 2023 14:19:14 -0700 Subject: [PATCH 1/2] Added realspace mask for radial statistics in polardatacube --- py4DSTEM/process/polar/polar_analysis.py | 29 +++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/py4DSTEM/process/polar/polar_analysis.py b/py4DSTEM/process/polar/polar_analysis.py index 8d2d92585..e5b604bcd 100644 --- a/py4DSTEM/process/polar/polar_analysis.py +++ b/py4DSTEM/process/polar/polar_analysis.py @@ -10,6 +10,7 @@ def calculate_radial_statistics( self, + mask_realspace=None, plot_results_mean=False, plot_results_var=False, figsize=(8, 4), @@ -89,16 +90,28 @@ def calculate_radial_statistics( unit=" probe positions", disable=not progress_bar, ): - self.radial_all[rx, ry] = np.mean(self.data[rx, ry], axis=0) - self.radial_all_std[rx, ry] = np.sqrt( - np.mean((self.data[rx, ry] - self.radial_all[rx, ry][None]) ** 2, axis=0) + if mask_realspace is None or mask_realspace[rx,ry]: + self.radial_all[rx, ry] = np.mean(self.data[rx, ry], axis=0) + self.radial_all_std[rx, ry] = np.sqrt( + np.mean((self.data[rx, ry] - self.radial_all[rx, ry][None]) ** 2, axis=0) + ) + + if mask_realspace is None: + self.radial_mean = np.mean(self.radial_all, axis=(0, 1)) + self.radial_var = np.mean( + (self.radial_all - self.radial_mean[None, None]) ** 2, axis=(0, 1) ) - self.radial_mean = np.mean(self.radial_all, axis=(0, 1)) - self.radial_var = np.mean( - (self.radial_all - self.radial_mean[None, None]) ** 2, axis=(0, 1) - ) - + else: + self.radial_mean = np.sum(self.radial_all, axis=(0, 1)) / np.sum(mask_realspace) + self.radial_var = np.zeros_like(self.radial_mean) + for rx in range(self._datacube.shape[0]): + for ry in range(self._datacube.shape[1]): + if mask_realspace[rx,ry]: + self.radial_var += (self.radial_all[rx,ry] - self.radial_mean) ** 2 + self.radial_var /= np.sum(mask_realspace) + + # Compute normalized variance self.radial_var_norm = np.copy(self.radial_var) sub = self.radial_mean > 0.0 self.radial_var_norm[sub] /= self.radial_mean[sub] ** 2 From 827b31430dd77f49f4f425b922f6fba15eaca6d3 Mon Sep 17 00:00:00 2001 From: alex-rakowski Date: Thu, 9 Nov 2023 21:33:25 -0800 Subject: [PATCH 2/2] black --- py4DSTEM/process/polar/polar_analysis.py | 71 ++++++++++-------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/py4DSTEM/process/polar/polar_analysis.py b/py4DSTEM/process/polar/polar_analysis.py index e5b604bcd..8062a098d 100644 --- a/py4DSTEM/process/polar/polar_analysis.py +++ b/py4DSTEM/process/polar/polar_analysis.py @@ -90,10 +90,12 @@ def calculate_radial_statistics( unit=" probe positions", disable=not progress_bar, ): - if mask_realspace is None or mask_realspace[rx,ry]: + if mask_realspace is None or mask_realspace[rx, ry]: self.radial_all[rx, ry] = np.mean(self.data[rx, ry], axis=0) self.radial_all_std[rx, ry] = np.sqrt( - np.mean((self.data[rx, ry] - self.radial_all[rx, ry][None]) ** 2, axis=0) + np.mean( + (self.data[rx, ry] - self.radial_all[rx, ry][None]) ** 2, axis=0 + ) ) if mask_realspace is None: @@ -106,9 +108,9 @@ def calculate_radial_statistics( self.radial_mean = np.sum(self.radial_all, axis=(0, 1)) / np.sum(mask_realspace) self.radial_var = np.zeros_like(self.radial_mean) for rx in range(self._datacube.shape[0]): - for ry in range(self._datacube.shape[1]): - if mask_realspace[rx,ry]: - self.radial_var += (self.radial_all[rx,ry] - self.radial_mean) ** 2 + for ry in range(self._datacube.shape[1]): + if mask_realspace[rx, ry]: + self.radial_var += (self.radial_all[rx, ry] - self.radial_mean) ** 2 self.radial_var /= np.sum(mask_realspace) # Compute normalized variance @@ -131,7 +133,7 @@ def calculate_radial_statistics( returnfig=True, ) if returnfig: - ans.append((fig,ax)) + ans.append((fig, ax)) if plot_results_var: fig, ax = plot_radial_var_norm( self, @@ -139,7 +141,7 @@ def calculate_radial_statistics( returnfig=True, ) if returnfig: - ans.append((fig,ax)) + ans.append((fig, ax)) # return return ans @@ -355,8 +357,8 @@ def calculate_pair_dist_function( coefs[3] *= int_mean # Calculate the mean atomic form factor without a constant offset - #coefs_fk = (0.0, coefs[1], coefs[2], coefs[3], coefs[4]) - #fk = scattering_model(k2, coefs_fk) + # coefs_fk = (0.0, coefs[1], coefs[2], coefs[3], coefs[4]) + # fk = scattering_model(k2, coefs_fk) bg = scattering_model(k2, coefs) fk = bg - coefs[0] @@ -432,7 +434,6 @@ def calculate_pair_dist_function( # store results self.pdf = pdf - # prepare answer if density is None: return_values = self.pdf_r, self.pdf_reduced @@ -443,39 +444,26 @@ def calculate_pair_dist_function( else: ans = None if not returnfig else [] - # Plots if plot_background_fits: - fig,ax = self.plot_background_fits( - figsize = figsize, - returnfig = True - ) + fig, ax = self.plot_background_fits(figsize=figsize, returnfig=True) if returnfig: - ans.append((fig,ax)) + ans.append((fig, ax)) if plot_sf_estimate: - fig,ax = self.plot_sf_estimate( - figsize = figsize, - returnfig = True - ) + fig, ax = self.plot_sf_estimate(figsize=figsize, returnfig=True) if returnfig: - ans.append((fig,ax)) + ans.append((fig, ax)) if plot_reduced_pdf: - fig,ax = self.plot_reduced_pdf( - figsize = figsize, - returnfig = True - ) + fig, ax = self.plot_reduced_pdf(figsize=figsize, returnfig=True) if returnfig: - ans.append((fig,ax)) + ans.append((fig, ax)) if plot_pdf: - fig,ax = self.plot_pdf( - figsize = figsize, - returnfig = True - ) + fig, ax = self.plot_pdf(figsize=figsize, returnfig=True) if returnfig: - ans.append((fig,ax)) + ans.append((fig, ax)) # return return ans @@ -485,7 +473,7 @@ def plot_background_fits( self, figsize=(8, 4), returnfig=False, - ): +): """ TODO """ @@ -513,14 +501,15 @@ def plot_background_fits( ) ax.set_yscale("log") if returnfig: - return fig,ax + return fig, ax plt.show() + def plot_sf_estimate( self, figsize=(8, 4), returnfig=False, - ): +): """ TODO """ @@ -540,7 +529,7 @@ def plot_sf_estimate( ax.set_xlabel("Scattering Vector [A^-1]") ax.set_ylabel("Reduced Structure Factor") if returnfig: - return fig,ax + return fig, ax plt.show() @@ -548,7 +537,7 @@ def plot_reduced_pdf( self, figsize=(8, 4), returnfig=False, - ): +): """ TODO """ @@ -561,14 +550,15 @@ def plot_reduced_pdf( ax.set_xlabel("Radius [A]") ax.set_ylabel("Reduced Pair Distribution Function") if returnfig: - return fig,ax + return fig, ax plt.show() + def plot_pdf( self, figsize=(8, 4), returnfig=False, - ): +): """ TODO """ @@ -581,11 +571,9 @@ def plot_pdf( ax.set_xlabel("Radius [A]") ax.set_ylabel("Pair Distribution Function") if returnfig: - return fig,ax + return fig, ax plt.show() - - # functions for inverting from reduced PDF back to S(k) # # invert @@ -666,4 +654,3 @@ def scattering_model(k2, *coefs): # int1*np.exp(k2/(-2*sigma1**2)) return int_model -