Skip to content

Commit

Permalink
hyper: refactor vector plot from BasicImagePlot to ImagePlot
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Nov 26, 2024
1 parent 29e3d97 commit 4018ed3
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions orangecontrib/spectroscopy/widgets/owhyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ def color_palette_model(palettes, iconsize=QSize(64, 16)):
model.appendRow([item])
return model


def vector_color_model(colors):
model = QStandardItemModel()
for name, palette in colors:
Expand All @@ -321,11 +322,13 @@ def vector_color_model(colors):
model.appendRow([item])
return model


def circular_mean(degs):
sin = np.nansum(np.sin(np.radians(degs*2)))
cos = np.nansum(np.cos(np.radians(degs*2)))
return np.arctan2(sin, cos)/2


class VectorSettingMixin:
show_vector_plot = Setting(False)
vector_angle = ContextSetting(None)
Expand Down Expand Up @@ -477,6 +480,7 @@ def init_vector_plot(self, data):
else:
self.vector_magnitude = self.vector_angle = self.vcol_byval_feat = None


class VectorMixin:

def __init__(self):
Expand All @@ -487,6 +491,14 @@ def __init__(self):
self.new_ys = None
self.v_bin_change = 0

ci = self.plotview.centralWidget
self.vector_plot = VectorPlot()
self.vector_plot.hide()
self.plot.addItem(self.vector_plot)
self.vect_legend = ImageColorLegend()
self.vect_legend.setVisible(False)
ci.addItem(self.vect_legend)

def update_vectors(self):
v = self.get_vector_data()
if self.lsx is None: # image is not shown or is being computed
Expand Down Expand Up @@ -650,6 +662,7 @@ def update_binsize(self):
self.v_bin_change = 0
self.update_vectors()


class AxesSettingsMixin:

def __init__(self):
Expand Down Expand Up @@ -1129,6 +1142,7 @@ def colorbar(self):
def vcolorbar(self):
return self.master.vect_legend.axis


class VectorPlot(pg.GraphicsObject):

def __init__(self):
Expand Down Expand Up @@ -1212,11 +1226,11 @@ def boundingRect(self):

return self._boundingRect


class BasicImagePlot(QWidget, OWComponent, SelectionGroupMixin,
AxesSettingsMixin, ImageSelectionMixin,
ImageColorSettingMixin, ImageRGBSettingMixin,
ImageZoomMixin, ConcurrentMixin,
VectorSettingMixin, VectorMixin):
ImageZoomMixin, ConcurrentMixin):

gamma = Setting(0)

Expand All @@ -1232,8 +1246,6 @@ def __init__(self, parent):
ImageColorSettingMixin.__init__(self)
ImageZoomMixin.__init__(self)
ConcurrentMixin.__init__(self)
VectorSettingMixin.__init__(self)
VectorMixin.__init__(self)
self.parent = parent

self.parameter_setter = ImageParameterSetter(self)
Expand Down Expand Up @@ -1276,13 +1288,6 @@ def __init__(self, parent):
self.plot.vb.setAspectLocked()
self.plot.scene().sigMouseMoved.connect(self.plot.vb.mouseMovedEvent)

self.vector_plot = VectorPlot()
self.vector_plot.hide()
self.plot.addItem(self.vector_plot)
self.vect_legend = ImageColorLegend()
self.vect_legend.setVisible(False)
ci.addItem(self.vect_legend)

layout = QGridLayout()
self.plotview.setLayout(layout)
self.button = QPushButton("Menu", self.plotview)
Expand Down Expand Up @@ -1317,12 +1322,10 @@ def __init__(self, parent):
self.axes_settings_box = self.setup_axes_settings_box()
self.color_settings_box = self.setup_color_settings_box()
self.rgb_settings_box = self.setup_rgb_settings_box()
self.vector_settings_box = self.setup_vector_plot_controls()

box.layout().addWidget(self.axes_settings_box)
box.layout().addWidget(self.color_settings_box)
box.layout().addWidget(self.rgb_settings_box)
box.layout().addWidget(self.vector_settings_box)

choose_xy.setDefaultWidget(box)
view_menu.addAction(choose_xy)
Expand Down Expand Up @@ -1436,8 +1439,10 @@ def update_view(self):
self.data_valid_positions = None
self.xindex = None
self.yindex = None
self.update_binsize()
self.update_vectors() # clears the vector plot

if isinstance(self, VectorMixin):
self.update_binsize()
self.update_vectors() # clears the vector plot

self.start(self.compute_image, self.data, self.attr_x, self.attr_y,
self.parent.image_values(),
Expand Down Expand Up @@ -1545,8 +1550,9 @@ def draw(self, res, finished=False):
self.yindex = yindex
self.xindex = xindex

self.update_binsize()
self.update_vectors()
if isinstance(self, VectorMixin):
self.update_binsize()
self.update_vectors()

# shift centres of the pixels so that the axes are useful
shiftx = _shift(lsx)
Expand Down Expand Up @@ -1580,10 +1586,17 @@ def on_exception(self, ex: Exception):
raise ex


class ImagePlot(BasicImagePlot):
class ImagePlot(BasicImagePlot,
VectorSettingMixin, VectorMixin):

attr_x = ContextSetting(None, exclude_attributes=True)
attr_y = ContextSetting(None, exclude_attributes=True)

def __init__(self, parent):
BasicImagePlot.__init__(self, parent)
VectorSettingMixin.__init__(self)
VectorMixin.__init__(self)


class CurvePlotHyper(CurvePlot):
viewtype = Setting(AVERAGE) # average view by default
Expand Down Expand Up @@ -1762,7 +1775,7 @@ def __init__(self):
# add image settings to the main panne after ImagePlot.__init__
iabox.layout().addWidget(self.imageplot.axes_settings_box)
icbox.layout().addWidget(self.imageplot.color_settings_box)
ivbox.layout().addWidget(self.imageplot.vector_settings_box)
ivbox.layout().addWidget(self.imageplot.setup_vector_plot_controls())

self.data = None

Expand Down

0 comments on commit 4018ed3

Please sign in to comment.