Skip to content

Commit

Permalink
Add a new Rotate Plot icon to the Options Tab
Browse files Browse the repository at this point in the history
  • Loading branch information
rayosborn committed Jun 18, 2024
1 parent 6a973dc commit 5820a08
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
86 changes: 86 additions & 0 deletions src/nexpy/gui/datadialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,92 @@ def apply(self):
self.plotview.draw()


class RotationDialog(NXPanel):

def __init__(self, parent=None):
super().__init__('Rotation', title='Rotation Panel', parent=parent)
self.tab_class = RotationTab
self.plotview_sort = True


class RotationTab(NXTab):

def __init__(self, label, parent=None):
super().__init__(label, parent=parent)

self.plotview = self.active_plotview
self.data = self.plotview.plotdata

self.parameters = GridParameters()
self.parameters.add('title', self.plotview.title, 'Title')
self.parameters.add('xlabel', self.plotview.xaxis.label,
'X-Axis Label')
self.parameters.add('ylabel', self.plotview.yaxis.label,
'Y-Axis Label')
self.parameters.add('angle', 0.0, 'Rotation Angle (degrees)')

self.set_layout(self.parameters.grid(header=False, width=200),
self.checkboxes(('reshape', 'Reshape Plot', True)),
self.action_buttons(('Plot', self.rotate_plot),
('Reset', self.reset_plot)))
self.parameters['title'].box.setFocus()

@property
def title(self):
return self.parameters['title'].value

@property
def xlabel(self):
return self.parameters['xlabel'].value

@property
def ylabel(self):
return self.parameters['ylabel'].value

@property
def angle(self):
return float(self.parameters['angle'].value)

@property
def reshape(self):
return self.checkbox['reshape'].isChecked()

@property
def pv(self):
if 'Rotation' in self.plotviews:
return self.plotviews['Rotation']
else:
from .plotview import NXPlotView
return NXPlotView('Rotation')

def rotate_plot(self):
from scipy.ndimage import rotate
signal = self.data.nxsignal
zr = NXfield(rotate(np.nan_to_num(signal, 0.0), angle=self.angle,
mode='constant', reshape=self.reshape),
name=signal.nxname)
if 'long_name' in signal.attrs:
zr.attrs['long_name'] = signal.attrs['long_name']
y, x = self.data.nxaxes
dy = (y[1] - y[0]) * np.sin(np.radians(self.angle))
dx = (x[1] - x[0]) * np.cos(np.radians(self.angle))

ny = zr.shape[0]
yr = NXfield([(-dy * (ny // 2) + i*dy) for i in range(ny)],
name=self.ylabel)
nx = zr.shape[1]
xr = NXfield([(-dx * (nx // 2) + i*dx) for i in range(nx)],
name=self.xlabel)

self.pv.plot(NXdata(zr, (yr, xr), title=self.title))

def reset_plot(self):
self.pv.plot(self.data)

def apply(self):
pass


class StyleDialog(NXPanel):

def __init__(self, parent=None):
Expand Down
13 changes: 12 additions & 1 deletion src/nexpy/gui/plotview.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@

from .. import __version__
from .datadialogs import (CustomizeDialog, ExportDialog, LimitDialog,
ProjectionDialog, ScanDialog, StyleDialog)
ProjectionDialog, RotationDialog, ScanDialog,
StyleDialog)
from .utils import (boundaries, centers, divgray_map, find_nearest,
fix_projection, get_color, in_dark_mode, iterable,
keep_data, parula_map, report_error, report_exception,
Expand Down Expand Up @@ -3863,6 +3864,7 @@ class NXNavigationToolbar(NavigationToolbar2QT, QtWidgets.QToolBar):
('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
(None, None, None, None),
('Aspect', 'Set aspect ratio to equal', 'equal', 'set_aspect'),
('Rotate', 'Rotate plot', 'rotate-plot', 'rotate_plot'),
('Customize', 'Customize plot', 'customize', 'edit_parameters'),
('Style', 'Modify style', 'modify-style', 'modify_style'),
(None, None, None, None),
Expand Down Expand Up @@ -3943,6 +3945,15 @@ def home(self, autoscale=True):
if self.plotview.skew:
self.plotview.grid(self.plotview._grid, self.plotview._minorgrid)

def rotate_plot(self):
"""Launch the Rotation Panel."""
self.plotview.make_active()
if not self.plotview.mainwindow.panel_is_running('Rotation'):
self.plotview.panels['Rotation'] = RotationDialog()
self.plotview.panels['Rotation'].activate(self.plotview.label)
self.plotview.panels['Rotation'].setVisible(True)
self.plotview.panels['Rotation'].raise_()

def edit_parameters(self):
"""Launch the Customize Panel."""
self.plotview.make_active()
Expand Down
Binary file added src/nexpy/gui/resources/rotate-plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5820a08

Please sign in to comment.