From 415f878c6713b3c9d22477c70de5b77bf79ff430 Mon Sep 17 00:00:00 2001 From: Peter Ercius ncem-gauss jupyter Date: Thu, 3 Oct 2024 14:21:04 -0700 Subject: [PATCH 1/3] Revert "update counting docstring for electron_count. Its best to use a np.ndarray." I meant to add this to a feature branch. This reverts commit 6ec823e362e6e5c0b8b37d58a606435b6b1bad37. --- python/stempy/image/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index f96056f5..1909c89f 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -201,9 +201,9 @@ def electron_count(reader, darkreference=None, number_of_samples=40, :param reader: the file reader that has already opened the data. :type reader: stempy.io.reader - :param darkreference: the dark reference to subtract. For an empty dark - use np.zeros((Nx, Ny)). - :type darkreference: numpy.ndimage + :param darkreference: the dark reference to subtract, potentially generated + via stempy.image.calculate_average(). + :type darkreference: stempy.image.ImageArray or stempy::Image :param number_of_samples: the number of samples to take when calculating the thresholds. :type number_of_samples: int From 97b49731bcdffc235eec3dd0a1383ae0bbb70456 Mon Sep 17 00:00:00 2001 From: Peter Ercius ncem-gauss jupyter Date: Wed, 27 Aug 2025 15:49:34 -0700 Subject: [PATCH 2/3] Revert "Revert "update counting docstring for electron_count. Its best to use a np.ndarray." I meant to add this to a feature branch." This reverts commit 415f878c6713b3c9d22477c70de5b77bf79ff430. --- python/stempy/image/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index 1909c89f..f96056f5 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -201,9 +201,9 @@ def electron_count(reader, darkreference=None, number_of_samples=40, :param reader: the file reader that has already opened the data. :type reader: stempy.io.reader - :param darkreference: the dark reference to subtract, potentially generated - via stempy.image.calculate_average(). - :type darkreference: stempy.image.ImageArray or stempy::Image + :param darkreference: the dark reference to subtract. For an empty dark + use np.zeros((Nx, Ny)). + :type darkreference: numpy.ndimage :param number_of_samples: the number of samples to take when calculating the thresholds. :type number_of_samples: int From 658c7f87a5624b1391ffc2224a9facb5481d897b Mon Sep 17 00:00:00 2001 From: Peter Ercius ncem-gauss jupyter Date: Wed, 27 Aug 2025 16:25:54 -0700 Subject: [PATCH 3/3] Add a way to directly set the background threshold and xray threshold. This could be fsater than using the n_sigma method and/or more convenient in some cases. --- python/stempy/image/__init__.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index 6e6aefe4..c49285ca 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -194,6 +194,7 @@ def calculate_average(reader): def electron_count(reader, darkreference=None, number_of_samples=40, + background_threshold=None, xray_threshold=None, background_threshold_n_sigma=4, xray_threshold_n_sigma=10, threshold_num_blocks=1, scan_dimensions=(0, 0), verbose=False, gain=None, apply_row_dark=False, @@ -209,6 +210,14 @@ def electron_count(reader, darkreference=None, number_of_samples=40, :param number_of_samples: the number of samples to take when calculating the thresholds. :type number_of_samples: int + :param background_threshold: The value used as the background threshold. + If this is not set then the n_sigma is used + and the value is estimated from the data. + :type background_threshold: int + :param xray_threshold: The value used as the x-ray threshold. + If this is not set then the n_sigma is used + and the value is estimated from the data. + :type xray_threshold: int :param background_threshold_n_sigma: N-Sigma used for calculating the background threshold. :type background_threshold_n_sigma: int @@ -287,10 +296,15 @@ def electron_count(reader, darkreference=None, number_of_samples=40, if gain is not None: args.append(gain) - res = _image.calculate_thresholds(*args) - - background_threshold = res.background_threshold - xray_threshold = res.xray_threshold + # Determine the threshold. This is either directly set by the user + # or calculated from the data with a Gaussian fit to the noise and + # the threshold is a set number of sigma of that Gaussian. + if background_threshold is None or xray_threshold is None: + res = _image.calculate_thresholds(*args) + if background_threshold is None: + background_threshold = res.background_threshold + if xray_threshold is None: + xray_threshold = res.xray_threshold if verbose: print('****Statistics for calculating electron thresholds****')