From a887c5e6d6e46a2ddaaf65209a4f26b5ee767a60 Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 21 Jan 2024 15:40:15 -0500 Subject: [PATCH 1/5] Fix calculations for fold == True and also some problems for inputs with Qx or Qy < 0 --- sasdata/data_util/manipulations.py | 56 +++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/sasdata/data_util/manipulations.py b/sasdata/data_util/manipulations.py index 4212c20..e050adc 100644 --- a/sasdata/data_util/manipulations.py +++ b/sasdata/data_util/manipulations.py @@ -362,19 +362,45 @@ def _avg(self, data2D, maj): qy_data = data2D.qy_data[np.isfinite(data2D.data)] mask_data = data2D.mask[np.isfinite(data2D.data)] + # Was getting negative bin widths if whole box was below Qx = 0 for maj == 'x' or Qy = 0 for maj == 'y'. + # Should this raise error or correct bin width? + if self.bin_width < 0: + # raise RuntimeError("_Slab._avg: bin width can't be negative") + self.bin_width = abs(self.bin_width) + # Build array of Q intervals if maj == 'x': if self.fold: - x_min = 0 + # Set x_max based on which is further from Qx = 0 + x_max = max(abs(self.x_min),abs(self.x_max)) + # Set x_min based on which is closer to Qx = 0, but will have different limits depending on whether + # x_min and x_max are on the same size of Qx = 0 + if self.x_min*self.x_max >= 0: # If on same side + x_min = min(abs(self.x_min),abs(self.x_max)) + else: + x_min = 0 else: + x_max = self.x_max x_min = self.x_min - nbins = int(math.ceil((self.x_max - x_min) / self.bin_width)) + y_max = self.y_max + y_min = self.y_min + nbins = int(math.ceil((x_max - x_min) / self.bin_width)) elif maj == 'y': if self.fold: - y_min = 0 + # Set y_max based on which is further from Qy = 0 + y_max = max(abs(self.y_min), abs(self.y_max)) + # Set y_min based on which is closer to Qy = 0, but will have different limits depending on whether + # y_min and y_max are on the same size of Qy = 0 + if self.y_min * self.y_max >= 0: # If on same side + y_min = min(abs(self.y_min), abs(self.y_max)) + else: + y_min = 0 else: + y_max = self.y_max y_min = self.y_min - nbins = int(math.ceil((self.y_max - y_min) / self.bin_width)) + x_max = self.x_max + x_min = self.x_min + nbins = int(math.ceil((y_max - y_min) / self.bin_width)) else: raise RuntimeError("_Slab._avg: unrecognized axis %s" % str(maj)) @@ -392,10 +418,24 @@ def _avg(self, data2D, maj): frac_x = 0 frac_y = 0 # get ROI - if self.x_min <= qx_data[npts] and self.x_max > qx_data[npts]: - frac_x = 1 - if self.y_min <= qy_data[npts] and self.y_max > qy_data[npts]: - frac_y = 1 + if self.fold: + # If folded, need to satisfy absolute value of Q, but also make sure we're only pulling + # from data inside the box (an issue when the box is not centered on 0) + if maj == 'x': + if self.x_min <= qx_data[npts] < self.x_max and x_min <= abs(qx_data[npts]) < x_max: + frac_x = 1 + if self.y_min <= qy_data[npts] < self.y_max: + frac_y = 1 + elif maj == 'y': # The case where maj != 'x' or 'y' was handled earlier + if self.y_min <= qy_data[npts] < self.y_max and y_min <= abs(qy_data[npts]) < y_max: + frac_y = 1 + if self.x_min <= qx_data[npts] < self.x_max: + frac_x = 1 + else: + if self.x_min <= qx_data[npts] < self.x_max: + frac_x = 1 + if self.y_min <= qy_data[npts] < self.y_max: + frac_y = 1 frac = frac_x * frac_y if frac == 0: From 3802b8b47719e9d6abb0b1c0631a0397a46047b0 Mon Sep 17 00:00:00 2001 From: jack-rooks <106691816+jack-rooks@users.noreply.github.com> Date: Tue, 23 Jan 2024 09:05:12 -0500 Subject: [PATCH 2/5] Update sasdata/data_util/manipulations.py Co-authored-by: Jeff Krzywon --- sasdata/data_util/manipulations.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sasdata/data_util/manipulations.py b/sasdata/data_util/manipulations.py index e050adc..7bbaa01 100644 --- a/sasdata/data_util/manipulations.py +++ b/sasdata/data_util/manipulations.py @@ -364,9 +364,8 @@ def _avg(self, data2D, maj): # Was getting negative bin widths if whole box was below Qx = 0 for maj == 'x' or Qy = 0 for maj == 'y'. # Should this raise error or correct bin width? - if self.bin_width < 0: - # raise RuntimeError("_Slab._avg: bin width can't be negative") - self.bin_width = abs(self.bin_width) + # Bin width calculation returns negative values when either axis has no points above 0. + self.bin_width = abs(self.bin_width) # Build array of Q intervals if maj == 'x': From f36caba73d773eb266327fe1b6a533778538b996 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 23 Jan 2024 09:28:24 -0500 Subject: [PATCH 3/5] Fix typo in comment that came up in review --- sasdata/data_util/manipulations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sasdata/data_util/manipulations.py b/sasdata/data_util/manipulations.py index e050adc..037ec36 100644 --- a/sasdata/data_util/manipulations.py +++ b/sasdata/data_util/manipulations.py @@ -374,7 +374,7 @@ def _avg(self, data2D, maj): # Set x_max based on which is further from Qx = 0 x_max = max(abs(self.x_min),abs(self.x_max)) # Set x_min based on which is closer to Qx = 0, but will have different limits depending on whether - # x_min and x_max are on the same size of Qx = 0 + # x_min and x_max are on the same side of Qx = 0 if self.x_min*self.x_max >= 0: # If on same side x_min = min(abs(self.x_min),abs(self.x_max)) else: @@ -390,7 +390,7 @@ def _avg(self, data2D, maj): # Set y_max based on which is further from Qy = 0 y_max = max(abs(self.y_min), abs(self.y_max)) # Set y_min based on which is closer to Qy = 0, but will have different limits depending on whether - # y_min and y_max are on the same size of Qy = 0 + # y_min and y_max are on the same side of Qy = 0 if self.y_min * self.y_max >= 0: # If on same side y_min = min(abs(self.y_min), abs(self.y_max)) else: From c204e530eccf693c944b444df35df758c494f341 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 23 Jan 2024 09:31:42 -0500 Subject: [PATCH 4/5] Removed comment that was outdated after a commit suggested in review --- sasdata/data_util/manipulations.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sasdata/data_util/manipulations.py b/sasdata/data_util/manipulations.py index 6502e58..0284af4 100644 --- a/sasdata/data_util/manipulations.py +++ b/sasdata/data_util/manipulations.py @@ -362,8 +362,6 @@ def _avg(self, data2D, maj): qy_data = data2D.qy_data[np.isfinite(data2D.data)] mask_data = data2D.mask[np.isfinite(data2D.data)] - # Was getting negative bin widths if whole box was below Qx = 0 for maj == 'x' or Qy = 0 for maj == 'y'. - # Should this raise error or correct bin width? # Bin width calculation returns negative values when either axis has no points above 0. self.bin_width = abs(self.bin_width) From fe82d4871b2170635b8ca57210f3bdb1cdf80850 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 23 Jan 2024 09:37:19 -0500 Subject: [PATCH 5/5] Fix minor indentation issue I didn't catch in a commit suggestion --- sasdata/data_util/manipulations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sasdata/data_util/manipulations.py b/sasdata/data_util/manipulations.py index 0284af4..92cb7b6 100644 --- a/sasdata/data_util/manipulations.py +++ b/sasdata/data_util/manipulations.py @@ -362,8 +362,8 @@ def _avg(self, data2D, maj): qy_data = data2D.qy_data[np.isfinite(data2D.data)] mask_data = data2D.mask[np.isfinite(data2D.data)] - # Bin width calculation returns negative values when either axis has no points above 0. - self.bin_width = abs(self.bin_width) + # Bin width calculation returns negative values when either axis has no points above 0. + self.bin_width = abs(self.bin_width) # Build array of Q intervals if maj == 'x':