Python Graphical User Interface¶
A PyQT GUI has been created to make it easier to keep track of the loaded NeXus files and the results of any subsequent analysis. It is invoked from the command line by:
This assumes that the Python ‘bin’ directory is in your standard shell path.
The illustration shows the main features of the GUI:
- 1) Tree Pane
This contains the tree structure of NeXus files opened in the File menu, non-NeXus files that have been imported and converted into the NeXus format using one of the NeXus readers, and NXroot, NXentry, or NXdata groups added from the shell. Various actions on the data can be performed by right-clicking a tree item, include plotting, renaming, fitting and deleting the data.
- 2) Plot Pane
This contains plots produced by (a) the Data:Plot Data menu item, which operates on the NeXus data selected in the tree, (b) right-clicking on NeXus data in the tree, or (c) using NeXus data Plot methods from the shell. If an NXdata, NXmonitor, or NXlog group is plotted, the rank, dimensions, and plotting axes are determined automatically. If the rank of the data is greater than two, a two-dimensional slice is extracted from the data. The GUI allows the selection of alternative slices using one of the axis panels (see below). If an NXfield is selected, the axes can be chosen from other NXfields in the same group. It is possible to open other plot windows and switch between them using the Window menu or keyboard shortcuts (see below).
- 3) Shell Pane
This is an IPython shell, with NeXpy already imported (as * so no prefixes are necessary), along with NumPy (as np) and Pylab (as plt). Any assignments to items in the tree pane are automatically reflected in the tree pane, and new NXroot or NXentry objects can be added to the tree from the IPython shell. NeXus data plots commands from the shell will appear in the plot pane, and Matplotlib commands can be used to modify the plot characteristics. The shell has enhanced features such as autocompletion of NeXus dictionaries and attributes and tooltips containing module docstrings when you open the module parentheses.
- 4) Axis Panels
The tabbed panels below the plot can be used to modify the plots. The number of panels depends on the rank of the original data. The ‘signal’, ‘x’ and ‘y’ panels have text boxes and sliders for adjusting the plotting limits. For rank two or more, a projection panel allows the plotting of projections along different directions, using the current axis limits. For ranks greater than two, a ‘z’ panel allows the other dimensions to be varied. Finally, the ‘options’ panel provides access to the standard Matplotlib tools for modifying the plots.
- 5) Status Bar
The values and attributes of the currently selected item in the tree are displayed in the status bar.
- 6) Tooltips
The NeXus tree structure of an item in the tree pane will be displayed as a tooltip when the cursor hovers over it.
Adding NeXus Data to the Tree¶
NXroot groups that are displayed in the tree pane are all children of a group of class NXtree, known as ‘tree’. If you create a NeXus group dynamically in the IPython shell, it can be added to the tree pane using the tree’s add method:
>>> a=NXroot() >>> a.entry = NXentry() >>> nxtree.add(a)
If the group is an NXroot group, it will have the name used in the shell. If the group is not an NXroot group, the data will be wrapped automatically in an NXroot group and given a default name that doesn’t conflict with existing tree nodes, e.g., w4.
The NXroot class is still considered to be the root of the NeXus tree in shell commands. The NXtree group is only used by the GUI and cannot be saved to a file.
In Python, an object may be accessible within the shell with more than one name. NeXpy searches the shell dictionary for an object with the same ID as the added NeXus object and so may choose a different name. The object in the tree can be renamed.
Plotting NeXus Data¶
NXdata, NXmonitor, and NXlog data can be plotted by selecting a group on the tree and choosing “Plot Data” from the Data menu or by double-clicking the item on the tree (or right-clicking for over-plots). Below the plot pane, a series of tabs allow manipulation of the plot limits and parameters using text boxes and sliders.
The slider ranges are initially set by the data limits. You can redefine the slider ranges by editing their respective minimum and/or maximum text boxes. The original range can be restored by clicking on the Home button in the Options Tab or right-clicking within the plot.
The signal tab contains text boxes and sliders to adjust the intensity limits, a checkbox to plot the intensity on a log scale, and two dropdown menus to select a color palette and a 2D interpolation method.
The color palettes are divided into three sections, separating perceptually uniform palettes at the top, miscellaneous palettes, and diverging palettes at the bottom. See the Matplotlib documentation for more details.
If a diverging color scale is used, the signal is assumed to be symmetric about 0, so the minimum box and slider are disabled and their values set to the negative of the maximum values. If a log scale is chosen, a symmetric log plot is displayed, with threshold and scale parameters adjustable using the command-line symlog command (see below).
For a one-dimensional plot, there is no signal tab. The intensity is adjusted using the y-tab. There is also no signal tab for an RGB(A) image, since the colors are defined by the RGB(A) values.
The interpolation methods are the default options provided by Matplotlib, which are only available for 2D data with a regular grid.
If the astropy module is installed, the interpolation dropdown menu includes a convolve option. Strictly speaking, this is not an interpolation method, since it performs a Gaussian smoothing of the data, with a standard deviation set by the smooth option (see below). The default is 2 pixels.
The x and y-tabs contains text boxes and sliders to adjust the axis limits and a dropdown menu to select the axis to be plotted along x or y, respectively. The names correspond to the axis names in the NXdata group. A checkbox allows the direction of the axes to be flipped.
Flipping the axis directions does not flip the direction of the sliders.
The y-tab has three additions to the features in the x-tab:
Since multiple one-dimensional data sets can be plotted on the same figure, an additional pull-down menu is added on the left-hand side to select them.
Selecting the ‘smooth’ checkbox adds a line that smoothly interpolates one-dimensional data. This uses the SciPy interp1d function. This option is provided to add guides-to-the-eye, and should be used for numerical analysis with caution.
The ‘Fit’ button will open a panel for fitting the data using the LMFIT package.
If the data rank is three or more, the 2D plot vs x and y is a projection along the remaining axes. The z-tab sets the limits for those projections. It contains a dropdown menu for selecting the axis to be averaged or summed over and two text boxes for selecting the projection limits. When the data are first plotted, only the top slice if plotted, i.e., all the z-axis limits are set to their minimum value.
Projections are now averaged over the summed bins by default. To restore the previous behavior, click the ‘Sum’ checkbox in the Projection Tab.
When ‘Lock’ is checked, the difference between the limits of the selected z-axis is fixed. This allows successive images along the z-axis to be plotted by clicking the text-box arrows in increments of the difference between the two limits. If you use the text-box arrows or the terminal arrow keys to change the z-limits when they are locked together, the new plot is updated automatically. Otherwise, the data is only replotted when you force a replot using the toolbar (see below).
Make sure that the value of both limit boxes is entered, e.g., by pressing return after editing their values, before clicking on the ‘lock’ checkbox.
When stepping through the z-values, the ‘Autoscale’ checkbox determines whether the plot automatically scales the signal to the maximum intensity of the slice or is set to the current signal limits.
When ‘Autoscale’ is checked, it is not possible to adjust the limits in the Signal Tab.
The toolbar on the right provides further controls for replotting data as a function of z. The first button on the left forces a replot, e.g., when you have changed z-axis limits or turned on auto-scaling. The other buttons are for stepping through the z-values automatically, with ‘back’, ‘pause’, and ‘forward’ controls. The default speed is one frame per second, but after the first click on the play button, subsequent clicks will reduce the frame interval by a factor two.
The projection tab allows the data to be projected along one or two dimensions. The limits are set by the x, y, and z-tabs, while the projection axes are selected using the dropdown boxes. For a one-dimensional projection, select ‘None’ from the y box. This is a short-cut to making projections with the Projection Panel.
The options tab provides the standard Matplotlib toolbar. You can view with the addition of one extra button. From left to right, the buttons are:
Home - restores all plotting limits to their original values.
Arrows - cycles through the limits of previous plots.
Pan - enables panning mode (disabling zoom mode).
Zoom - enables zoom mode (disabling pan mode).
Aspect - toggles between setting the aspect ratio automatically to fill the available space or setting the x and y scales to be equal. This is only valid if the units of the x and y axes are identical.
Subplot - configures the spacing around the plot.
Edit - opens the Customize Panel to edit both image and point plots. Use this to change the title and axis labels, modify the image aspect ratio and skew angles, turn axis grids on or off and set their styles, modify the point plot markers and lines, scale or add an offset to 1D plots, and draw legends.
Save - saves plot to PNG file.
Export - exports plotted data to a NeXus file or, for one-dimensional data, a multi-column ASCII file.
Add - adds plotted data to the tree pane as an NXdata group within the scratch workspace ‘w0’.
On the far right of the toolbar, the data and axis values are dynamically updated to the values under the current mouse location.
See the Matplotlib documentation for more detailed descriptions of the standard toolbar, including keyboard shortcuts. The ‘Aspect’ and ‘Add’ buttons are unique to NeXpy.
The aspect ratio of a plot can also be set from the IPython shell. See below.
Command Line Options
It is possible to modify some of the plotting features from the IPython shell. The current plotting pane, the default Matplotlib axis instance, and the current image are exposed as
Before making any changes, make sure that you have selected the right plotting pane, either by selecting it in the Window menu or using one of the keyboard shortcuts, which are displayed in the menu, e.g., <Ctrl>+2 (⌘+2 on a Mac) to select Figure 2.
Set Aspect Ratio:
>>> plotview.aspect = <aspect>
<aspect>can be any of the values allowed by the Matplotlib set_aspect function, i.e., ‘auto’, ‘equal’, or the numerical value of the ratio between the height and the width (if the units are identical). The ‘Aspect’ button (see above) toggles between ‘auto’ and ‘equal’. This can also be set using the ‘Edit Parameters’ button on the Options tab.
Set Skew Angle:
>>> plotview.skew = <angle>
This sets the angle between the x and y-axes in degrees. If set to
None, the axes are plotted as orthogonal. If
plotview.aspectis currently set to ‘auto’, this command will automatically set it to 1.0 (equivalent to ‘equal’), i.e., assuming the units of the x and y-axes are the same. If they are not,
plotview.aspectshould be set to the ratio of their units. This can also be set using the ‘Edit Parameters’ button on the Options tab.
Set Smoothing Width:
>>> plotview.smooth = <stddev>
This sets the standard deviation in pixels for the Gaussian smoothing of the data performed when the ‘convolve’ option is selected in the Signal tab. The default value is 2.
>>> plotview.offsets = <True|False>
If the range of an axis is much smaller than the absolute values, the axis labels can overlap. Setting this option will determine whether Matplotlib converts the axis labels to differences from a fixed offset value or not. The default is
Select Color Map:
>>> plotview.cmap = <cmap>
This allows the color map of the currently displayed image to be changed. This can be useful if the map is not available in the Signal Tab. See the Matplotlib documentation for more details.
>>> plotview.vline(<x>, <ymin>, <ymax>) >>> plotview.hline(<y>, <xmin>, <xmax>) >>> plotview.vlines(<x-array>, <ymin>, <ymax>) >>> plotview.hlines(<y-array>, <xmin>, <xmax>) >>> plotview.crosshairs(<x>, <y>) >>> plotview.rectangle(<x>, <y>, <dx>, <dy>) >>> plotview.circle(<x>, <y>, <radius>) >>> plotview.ellipse(<x>, <y>, <dx>, <dy>)
These functions draw graphical primitives on the plot using the axis coordinates. In the case of the lines, the complete range of the plot will be used if the minimum and maximum values are omitted. The rectangle coordinates represent the lower left-hand corner but the circle and ellipse coordinates represent the shape center.
Since the arguments are in the units of the axes, the circle will only be truly circular if the x and y units are the same, and the aspect ratio of the plot is equal.
All of the functions will accept additional keyword arguments used in drawing Matplotlib shapes, e.g., to change the edge and fill colors, line properties, etc. See the Matplotlib documentation for more details.
Draws grid lines at the major tick values. Additional keyword arguments can be given to modify the color, linestyle, etc, using the standard Matplotlib conventions.
>>> plotview.legend(*items, *opts)
This draws a legend using the standard Matplotlib API, i.e., it is broadly equivalent to calling
plotview.ax.legend(). It is only intended to be used for one-dimensional plots. By default, the labels will contain the full path to each plotted field, but setting the keyword argument,
nameonly=Truewill restrict the label to the field name.
Legend labels, positions, and other attributes can be modified in the Customize Dialog.
Convert to Symmetric Log Plot:
>>> plotview.symlog(linthresh, linscale, vmax)
Plot the data using symmetric logarithms for both positive and negative data. The
linscaleparameters are used to define the linear region interpolating between the positive and negative log regions. See the Matplotlib documentation for more details. The maximum and minimum signal values are set to +/- vmax.
symlogwill set the
linscaleparameters for future plots. Call it without any parameters to set them to their default values,
There are a number of diverging color maps, such as
coolwarm, that are ideal for displaying symmetric log data. Some are available at the bottom of the color map dropdown menu in the Signal tab.
A number of keyboard shortcuts are defined when focus is on the plotting window. These can be used to switch between tabs or set various plotting options.
Keyboard focus can be switched to a particular plotting window by (a) clicking within the window, (b) using the Window menu, or (c) typing Ctrl+’n’ (⌘+’n’ on a Mac), where ‘n’ is the plot window number.
s - switch to the Signal tab.
x - switch to the X tab.
y - switch to the Y tab.
z - switch to the Z tab.
p - switch to the Projection tab.
o - switch to the Options tab.
l - toggle logarithmic signal scale (2D plots only).
g - toggle display of major and minor grid.
G - toggle display of major grid.
P - toggle panning mode (if enabled, zoom mode is disabled).
Z - toggle zoom mode (if enabled, pan mode is disabled).
E - toggle the aspect ratio between ‘equal’ and ‘automatic’.
S - save plot to a graphics file.
A - add plotted data to the tree pane.
O - open dialog to customize plots.
When NeXpy if first launched, a private directory is created in the home directory, ~/.nexpy/. This is used to store log files, backups, plugins, and scripts. A configuration file, ~/.nexpy/config.py, is created to contain Python commands that should be run at the start of every session.
By default, the configuration file contains a number of imports, including all the functions and classes defined by the nexusformat package.
import nexpy import nexusformat.nexus as nx from nexusformat.nexus import *
This file could also be used to change the default parameters used by the nexusformat package to define, e.g., memory limits, maximum loaded array sizes, file locking, default HDF5 compression, and default string encodings. See Python Interface to NeXus for more details.
For convenience, the configuration file also imports a number of other modules that are commonly used:
import sys import os import h5py as h5 import numpy as np import numpy.ma as ma import scipy as sp import matplotlib as mpl from matplotlib import pylab, mlab, pyplot plt = pyplot
If you require a different set of imports or prefer alternative abbreviations, edit the configuration file in ~/.nexpy/config.py.
Fitting NeXus Data¶
NeXpy makes it easy to fit one-dimensional data using the LMFIT package, with a ‘Fit’ button in the Y-Tab of every one-dimensional plot. Alternatively, choosing ‘Fit Data’ from the Data menu or using the keyboard shortcut Ctrl+Shift+F (⌘+⇧+F on a Mac), will fit data selected in the Tree Pane.
Either method opens a dialog window that allows multiple fit models to be combined, with the option of fixing or limiting parameters. To help in selecting a model, click on the pull-down menu and the model description will be displayed as a tooltip when you hover over it.
The fit can be plotted, along with the constituent models in the main plotting window and the fitting parameters displayed in a message window.
The fit is only performed over the range set by the X-axis limits
entered in the Fit Dialog. These values can be changed between
fits if required, or reset to the overall range of the data using the
Reset Limits button.
When the plotting window is selected, the keyboard shortcuts ‘l’ and ‘r’ can be used to set the X-axis limits in the fit dialog to the current cursor position in the canvas. Alternatively, the range can be selected by dragging with the right-mouse button (or with the Ctrl-key depressed).
Some of the LMFIT functions have an additional option that is selected with the ‘form’ keyword. At present, the default option is automatically selected in NeXpy.
Saving the Fit¶
The original data, the fitted data, constituent models, and the parameters can all be saved to an NXprocess group in the Tree Pane for subsequent plotting, refitting, or saving to a NeXus file. The group, named ‘f1’, ‘f2’, etc., is stored in the default scratch NXroot group, w0. If you choose to fit this entry again, it will load the models and parameters from the saved fit.
Defining a Model¶
NeXpy makes available any of the models currently supplied by the LMFIT package, as well as a couple of extra models added to the NeXpy package, the OrderParameterModel and the PDFdecayModel. If you wish to construct your own model, please refer to the LMFIT documentation for more details.
User-defined models can be added as separate files to their private models
~/.nexpy/models (new to v0.12.6). As an example, here is the
code for the OrderParameterModel that is distributed with NeXpy:
import numpy as np from lmfit.model import Model class OrderParameterModel(Model): r"""A model to describe the temperature dependence of an order parameter with three Parameters: ``amplitude``, ``Tc``, and ``beta``. .. math:: f(x; A, Tc, \beta) = A ((Tc - x[x<Tc])/ Tc)^\beta where the parameter ``amplitude`` corresponds to :math:`A`, ``Tc`` to :math:`Tc`, and ``beta`` to :math:`\beta`. """ def __init__(self, **kwargs): def op(x, amplitude=1.0, Tc=100.0, beta=0.5): v = np.zeros(x.shape) v[x<Tc] = amplitude * ((Tc - x[x<Tc])/ Tc)**beta v[x>=Tc] = 0.0 return v super().__init__(op, **kwargs) def guess(self, data, x=None, negative=False, **kwargs): """Estimate initial model parameter values from data.""" return self.make_params(amplitude=data.max(), Tc=x.mean(), beta=0.33)
Prior to v0.12.6, NeXpy defined its own system for generating fitting functions. This system is now deprecated, but legacy functions are still available at the end of the model list. If you have produced your own functions in the past, they will also be on this list. However, we recommend that all new functions now adhere to LMFIT model definitions. The following description of the old system is retained to help with debugging or migrating to the new system.
Defining a Function¶
User-defined functions can be added to their private functions directory in
~/.nexpy/functions. The file must define the name of the function, a list of
parameter names, and provide two modules to return the function values and
starting parameters, respectively.
As an example, here is the complete Gaussian function:
import numpy as np function_name = 'Gaussian' parameters = ['Integral', 'Sigma', 'Center'] factor = np.sqrt(2*np.pi) def values(x, p): integral, sigma, center = p return integral * np.exp(-(x-center)**2/(2*sigma**2)) / (sigma * factor) def guess(x, y): center = (x*y).sum()/y.sum() sigma = np.sqrt(abs(((x-center)**2*y).sum()/y.sum())) integral = y.max() * sigma * factor return integral, sigma, center
NeXpy uses the function’s ‘guess’ module to produce starting parameters automatically when the function is loaded. When each function is added to the model, the estimated y-values produced by that function will be subtracted from the data before the next function estimate. It is useful therefore to choose the order of adding functions carefully. For example, if a peak is sitting on a sloping background, the background function should be loaded first since it is estimated from the first and last data points. This guess will be subtracted before estimating the peak parameters. Obviously, the more functions that are added, the less reliable the guesses will be. Starting parameters will have to be entered manually before the fit in those cases.
If it is not possible to estimate starting parameters, just return values that do not trigger an exception.
The X-range used in ‘guessing’ the parameters can be adjusted by setting the X-axis limits in the Fit Dialog.
Importing NeXus Data¶
NeXpy can import data stored in a number of other formats, including SPEC files, TIFF images, and text files, using the File:Import menus. If a file format is not currently supported, the user can write their own. The following is an example of a module that reads the original format and returns NeXus data:
def get_data(filename): from libtiff import TIFF im = TIFF.open(filename) z = im.read_image() y = range(z.shape) x = range(z.shape) return NXentry(NXdata(z,(y,x)))
This could be run in the shell pane and then added to the tree using:
NeXpy is currently distributed with readers for the following format:
This reader will read a stack of images, which are readable by FabIO, e.g., TIFF or CBF, into a three-dimensional NXdata group. The image stack must be stored in separate files in a single directory, that are grouped with a common prefix followed by an integer defining the stack sequence.
This reader will read ASCII data stored in two or three columns, containing the x and y values, and, optionally, errors. One or more header lines can be skipped. A more flexible text importer, allowing the selection of data from multiple columns, is under development.
This reader will read multiple SPEC scans from a single SPEC log file, creating a separate NXentry for each scan. All the columns in each scan are read into the NXdata group, with the default signal defined by the last column. Mesh scans are converted to multi-dimensional data, with axes defined by the scan command. It is possible to plot different columns once the scans are imported.
This will read both the ASCII and binary (HDF5) versions of the neutron time-of-flight SPE intermediate format into standard-conforming NeXus files. The data is stored as S(phi,E), but, if the incident energy and (Q,E) bins are also defined, the data will will also be converted into S(Q,E). The current version does not read the ASCII PHX files used to define instrumental parameters, but there are plans to add that in the future.
Defining a Reader¶
It is possible to add a reader to the File:Import menu using the existing
samples as a guide in the nexpy.readers directory. User-defined import dialogs
can be added to their private readers directory in
Here is an example of an import dialog:
""" Module to read in a TIFF file and convert it to NeXus. Each importer needs to layout the GUI buttons necessary for defining the imported file and its attributes and a single module, get_data, which returns an NXroot or NXentry object. This will be added to the NeXpy tree. Two GUI elements are provided for convenience: ImportDialog.filebox: Contains a "Choose File" button and a text box. Both can be used to set the path to the imported file. This can be retrieved as a string using self.get_filename(). ImportDialog.close_buttons: Contains a "Cancel" and "OK" button to close the dialog. This should be placed at the bottom of all import dialogs. """ import numpy as np from nexusformat.nexus import * from nexpy.gui.importdialog import BaseImportDialog filetype = "TIFF Image" #Defines the Import Menu label class ImportDialog(BaseImportDialog): """Dialog to import a TIFF image""" def __init__(self, parent=None): super(ImportDialog, self).__init__(parent) self.set_layout(self.filebox(), self.close_buttons()) self.set_title("Import "+str(filetype)) def get_data(self): from libtiff import TIFF im = TIFF.open(self.get_filename()) z = NXfield(im.read_image(), name='z') y = NXfield(range(z.shape), name='y') x = NXfield(range(z.shape), name='x') return NXentry(NXdata(z,(y,x)))
nexpy.gui.importdialog.BaseImportDialog and its parent
nexpy.gui.importdialog.BaseDialog for other
It is possible to customize NeXpy by adding new menus to the main menu bar
with sub-menus that open dialog boxes for operations that are specific to a
particular domain. These will be automatically loaded from either the
nexpy.plugins directory within the installed NeXpy distribution or from the
The new menu should be defined as a Python package, i.e., by creating a
sub-directory within the plugins directory that contains
define the menu actions.
There is an example package,
chopper, in the
to show how plugins can work. It adds a top-level menu item,
has a couple of menu items to perform data analysis on the example file,
chopper.nxs, which is distributed with NeXpy.
Here is the
from . import get_ei, convert_qe def plugin_menu(): menu = 'Chopper' actions =  actions.append(('Get Incident Energy', get_ei.show_dialog)) actions.append(('Convert to Q-E', convert_qe.show_dialog)) return menu, actions
The actions define the menu text and the function that gets called when it
is selected. In the example, they are contained within the package as two files,
convert_qe.py, but they could also be in a separately
installed package in the Python path.
These files should open a dialog box and perform the required operations, after which the results can either be saved to a new NeXus file or saved as modifications to an existing tree item.
get_ei.py reads the monitor spectra contained within the
currently selected node on the tree, which should have been previously loaded.
It then calculates the difference between the peak positions of the two spectra,
calculates the incident energy, which is updated in both the dialog box and, if
Save button is pressed, in the loaded NeXus tree, ready for subsequent
In the simplest cases, no knowledge of PyQt is required. In the example below,
a grid defines a set of parameters, functions to read those parameters from
the PySide text boxes (here, they are decorated with
means that the function can be called without an argument), a couple of
buttons to activate different parts of the analysis, and finally the
nexpy.gui.datadialogs.BaseDialog for a list of
pre-defined dialog methods if the dialog uses it as the parent
Here is the code:
import numpy as np from nexpy.gui.datadialogs import BaseDialog, GridParameters from nexpy.gui.mainwindow import report_error from nexusformat.nexus import NeXusError def show_dialog(parent=None): try: dialog = EnergyDialog() dialog.show() except NeXusError as error: report_error("Getting Incident Energy", error) class EnergyDialog(BaseDialog): def __init__(self, parent=None): super(EnergyDialog, self).__init__(parent) self.select_entry() self.parameters = GridParameters() self.parameters.add('m1', self.entry['monitor1/distance'], 'Monitor 1 Distance') self.parameters.add('m2', self.entry['monitor2/distance'], 'Monitor 2 Distance') self.parameters.add('Ei', self.entry['instrument/monochromator/energy'], 'Incident Energy') self.parameters.add('mod', self.entry['instrument/source/distance'], 'Moderator Distance') action_buttons = self.action_buttons(('Get Ei', self.get_ei)) self.set_layout(self.entry_layout, self.parameters.grid(), action_buttons, self.close_buttons(save=True)) self.set_title('Get Incident Energy') self.m1 = self.entry['monitor1'] self.m2 = self.entry['monitor2'] @property def m1_distance(self): return self.parameters['m1'].value - self.moderator_distance @property def m2_distance(self): return self.parameters['m2'].value - self.moderator_distance @property def Ei(self): return self.parameters['Ei'].value @property def moderator_distance(self): return self.parameters['mod'].value def get_ei(self): t = 2286.26 * self.m1_distance / np.sqrt(self.Ei) m1_time = self.m1[t-200.0:t+200.0].moment() t = 2286.26 * self.m2_distance / np.sqrt(self.Ei) m2_time = self.m2[t-200.0:t+200.0].moment() self.parameters['Ei'].value = (2286.26 * (self.m2_distance - self.m1_distance) / (m2_time - m1_time))**2 def accept(self): try: self.parameters['Ei'].save() except NeXusError as error: report_error("Getting Incident Energy", error) super(EnergyDialog, self).accept()