Skip to content

Commit

Permalink
Merge pull request #2773 from SasView/2100_2726_2752_Slicer-bugfixes
Browse files Browse the repository at this point in the history
2100 2726 2752 slicer bugfixes
  • Loading branch information
jack-rooks authored Jan 21, 2024
2 parents ebe6494 + 9703f46 commit 20b41de
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
75 changes: 71 additions & 4 deletions src/sas/qtgui/Plotting/Plotter2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def plot(self, data=None, marker=None, show_colorbar=True, update=False):
update=update)

self.updateCircularAverage()
self.updateSlicer()

def calculateDepth(self):
"""
Expand Down Expand Up @@ -338,8 +339,7 @@ def circularAverage(self):
else:
new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}")

new_plot.group_id = "2daverage" + self.data0.name
new_plot.id = "Circ avg " + self.data0.name
new_plot.id = "2daverage" + self.data0.name
new_plot.is_data = True

return new_plot
Expand Down Expand Up @@ -375,8 +375,8 @@ def updateCircularAverage(self):
# See if current item plots contain 2D average plot
has_plot = False
for plot in plots:
if plot.group_id is None: continue
if ca_caption in plot.group_id: has_plot = True
if plot.id is None: continue
if ca_caption in plot.id: has_plot = True
# return prematurely if no circular average plot found
if not has_plot: return

Expand All @@ -388,6 +388,32 @@ def updateCircularAverage(self):
# Show the new plot, if already visible
self.manager.communicator.plotUpdateSignal.emit([new_plot])

def updateSlicer(self):
"""
Update slicer plot on Data2D change
"""
if not hasattr(self, '_item'): return
item = self._item
if self._item.parent() is not None:
item = self._item.parent()

# Get all plots for current item
plots = GuiUtils.plotsFromModel("", item)
if plots is None: return
slicer_caption = 'Slicer' + self.data0.name
# See if current item plots contain slicer plot
has_plot = False
for plot in plots:
if not hasattr(plot, 'type_id') or plot.type_id is None: continue
if slicer_caption in plot.type_id: has_plot = True
# return prematurely if no slicer plot found
if not has_plot: return

# Now that we've identified the right plot, update the 2D data the slicer uses
self.slicer.data = self.data0
# Replot now that the 2D data is updated
self.slicer._post_data()

def setSlicer(self, slicer, reset=True):
"""
Clear the previous slicer and create a new one.
Expand All @@ -396,6 +422,47 @@ def setSlicer(self, slicer, reset=True):
# Clear current slicer
if self.slicer is not None:
self.slicer.clear()

# Clear the old slicer plots so they don't reappear later
if hasattr(self, '_item'):
item = self._item
if self._item.parent() is not None:
item = self._item.parent()

# Go through all items and see if they are a plot. The checks done here are not as thorough
# as GuiUtils.deleteRedundantPlots (which this takes a lot from). Will this cause problems?
# Primary concern is the check (plot_data.plot_role == DataRole.ROLE_DELETABLE) as I don't
# know what it does. The other checks seem to be related to keeping the new plots for that function
tempPlotsToRemove = []
slicer_type_id = 'Slicer' + self.data0.name
for itemIndex in range(item.rowCount()):
# GuiUtils.plotsFromModel tests if the data is of type Data1D or Data2D to determine
# if it is a plot, so let's try that
if isinstance(item.child(itemIndex).data(), (Data1D, Data2D)):
# First take care of this item, then we'll take care of its children
if hasattr(item.child(itemIndex).data(), 'type_id'):
if slicer_type_id in item.child(itemIndex).data().type_id:
# Store this plot to be removed later. Removing now
# will cause the next plot to be skipped
tempPlotsToRemove.append(item.child(itemIndex))
# It looks like the slicers are children of items that do not have data of instance Data1D or Data2D.
# Now do the children (1 level deep as is done in GuiUtils.plotsFromModel). Note that the slicers always
# seem to be the first entry (index2 == 0)
for itemIndex2 in range(item.child(itemIndex).rowCount()):
# Repeat what we did above (these if statements could probably be combined
# into one, but I'm not confident enough with how these work to say it wouldn't
# have issues if combined)
if isinstance(item.child(itemIndex).child(itemIndex2).data(), (Data1D, Data2D)):
if hasattr(item.child(itemIndex).child(itemIndex2).data(), 'type_id'):
if slicer_type_id in item.child(itemIndex).child(itemIndex2).data().type_id:
# Remove the parent since each slicer seems to generate a new entry in item
tempPlotsToRemove.append(item.child(itemIndex))
# Remove all the parent plots with matching criteria
for plot in tempPlotsToRemove:
item.removeRow(plot.row())
# Delete the temporary list of plots to remove
del tempPlotsToRemove

# Create a new slicer
self.slicer_z += 1
self.slicer = slicer(self, self.ax, item=self._item, zorder=self.slicer_z)
Expand Down
2 changes: 1 addition & 1 deletion src/sas/qtgui/Plotting/Slicers/AnnulusSlicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def _post_data(self, nbins=None):
new_plot.ytransform = 'y'
new_plot.yaxis("\\rm{Residuals} ", "/")

new_plot.group_id = "AnnulusPhi" + self.data.name
new_plot.id = "AnnulusPhi" + self.data.name
new_plot.type_id = "Slicer" + self.data.name # Used to remove plots after changing slicer so they don't keep showing up after closed
new_plot.is_data = True
new_plot.xtransform = "x"
new_plot.ytransform = "y"
Expand Down
8 changes: 4 additions & 4 deletions src/sas/qtgui/Plotting/Slicers/BoxSlicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, base, axes, item=None, color='black', zorder=3, direction=Non
center_x=self.center_x,
center_y=self.center_y)

# draw the rectangle and plost the data 1D resulting
# draw the rectangle and plot the data 1D resulting
# of averaging data2D
self.update()
self._post_data()
Expand Down Expand Up @@ -245,7 +245,7 @@ def _post_data(self, new_slab=None, nbins=None, direction=None):

#new_plot. = "2daverage" + self.data.name
new_plot.id = (self.averager.__name__) + self.data.name
new_plot.group_id = new_plot.id
new_plot.type_id = "Slicer" + self.data.name # Used to remove plots after changing slicer so they don't keep showing up after closed
new_plot.is_data = True
item = self._item
if self._item.parent() is not None:
Expand Down Expand Up @@ -448,7 +448,7 @@ def __init__(self, base, axes, color='black', zorder=5, x=0.5, y=0.5,
# Center coordinates
self.center_x = center_x
self.center_y = center_y
# defined end points vertical lignes and their saved values
# defined end points vertical lines and their saved values
self.y1 = y + self.center_y
self.save_y1 = self.y1

Expand Down Expand Up @@ -757,7 +757,7 @@ def move(self, x, y, ev):
self.y2 = self.center_y - delta
self.half_height = numpy.fabs(self.y1) - self.center_y
self.has_move = True
self.base.base.update()
self.base.update()

def setCursor(self, x, y):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/sas/qtgui/Plotting/Slicers/SectorSlicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ def _post_data(self, nbins=None):
new_plot.ytransform = 'y'
new_plot.yaxis("\\rm{Residuals} ", "/")

new_plot.group_id = "2daverage" + self.data.name
new_plot.id = "SectorQ" + self.data.name
new_plot.type_id = "Slicer" + self.data.name # Used to remove plots after changing slicer so they don't keep showing up after closed
new_plot.is_data = True
item = self._item
if self._item.parent() is not None:
Expand Down Expand Up @@ -385,13 +385,13 @@ def update(self, phi=None, delta=None, mline=None,
else:
self.phi = numpy.fabs(self.phi)
if side:
self.theta = mline.alpha + self.phi
self.theta = mline.theta + self.phi

if mline is not None:
if delta != 0:
self.theta2 = mline + delta
else:
self.theta2 = mline.alpha
self.theta2 = mline.theta
if delta == 0:
theta3 = self.theta + delta
else:
Expand Down
2 changes: 1 addition & 1 deletion src/sas/qtgui/Plotting/Slicers/WedgeSlicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _post_data(self, new_sector=None, nbins=None):
new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}")

new_plot.id = str(self.averager.__name__) + self.data.name
new_plot.group_id = new_plot.id
new_plot.type_id = "Slicer" + self.data.name # Used to remove plots after changing slicer so they don't keep showing up after closed
new_plot.is_data = True
item = self._item
if self._item.parent() is not None:
Expand Down

0 comments on commit 20b41de

Please sign in to comment.