Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Developer Notes

Matthew D Jones edited this page Jul 29, 2019 · 42 revisions

Running unit tests

Unit tests are written to use pytest. Once the dependencies have been installed, they can be run from a terminal in the project's root directory by running the command pytest.

Test coverage can be checked by running the following from the root of the repository:

pytest --cov=nexus_constructor

Linter

flake8 is used to check pep8 compliance. It is installed via pip and can be run as flake8 in the project's root directory.

Testing the UI

A script for testing the UI's functionality can be found at tests/UITests.md

Building a distributable version

A distributable version of the app, with the required python interpreter and libraries included can be built using cx_Freeze. It is included in the project's requirements file, and these must be installed in order to build the distributable. A build can be run with the following command in the projects root directory:

python setup.py build_exe

This will create the executable and copy it's required files to the build subdirectory

cx_Freeze is capable of building distributable versions for Windows, OS X, and Linux, but can only do so from a machine running that operating system.

Python and QML

The Developer Notes attempts to present a guide of the NeXus Constructor. This assumes you have some basic knowledge of how to create programs with Python + QML. If not, it might be best to examine the QML and Qt for Python sections of the Wiki first.

The data classes that internally model an instrument's geometry are found in the nexus_constructor.data_model module. The data that these classes contain is then exposed to the QML interface through a series of model classes defined in nexus_constructor.qml_models. These models create a set of named properties, or Roles, that can be read and edited through the QML UI.

For these custom models to be accessible in QML, they need to be registered to a module. This is done in nexus_constructor/application.py alongside the class that loads the QML files.

To use a custom model and its properties, import its qml module, create an instance, and use it as the model for a ListView component.

...
import MyModels 1.0

ListView {
    width: 100; height: 200

    model: InstrumentModel {}
    delegate: Text {
        text: name
    }
}

Methods in custom QML/python classes can be made callable using slot and signal annotations

JSON formats

The NeXus Constructor supports two different json formats, each with a different purpose.

Nexus Constructor json is designed to mirror the structure of the InstrumentModel and data_model classes. Thus it can be used to store and reload identical representations of the data in them for saving and returning to at a later time. Its format is defined in Instrument.schema.json

Nexus FileWriter json stores an instrument's data in the format required by ESS-DMSC's Nexus Filewriter to produce a standard compliant nexus file. As a result, certain transformations are made to the data before it goes into this format which cannot be reversed, making saving and reloading into the nexus constructor from this format slightly lossy. This is mostly due to floating point calculation inaccuracies, but also includes explicitly stating the dependent transform in a parent if the model was implicitly using the last one before exporting.

Each format has a package that contains methods for writing and parsing json. These packages expose the following methods:

  • generate_json(model: InstrumentModel) produces a json string from the given model
  • load_json_object_into_instrument_model(json_object: dict, model: InstrumentModel) populates a model with data from a json_object, which can be obtained from the load methods in python's json package

Custom Items in the NeXus Constructor

LabeledTextField

The LabeledTextField consists of a TextField and a Label bundled into one item.

Some of the fields in the TextField and Label can be accessed via aliases. Adding further aliases is quite simple.

Note: Setting readOnly to true is helpful in the case of things such as file selection where you only want the contents of the box to be changed by a FileDialog or something else in QML. Modifying the text can still be achieved programmatically in readOnly mode and is a safe bet for fields with "predictable" inputs.

ActiveScrollBar

The ActiveScrollBar is simply an ordinary ScrollBar plus a QML trick that causes the ScrollBar to always be visible if the contents of a Flickable are sufficiently long, and always be invisible if the contents of a Flickbable aren't sufficiently long.

By contrast, an ordinary ScrollBar with its policy set to ScrollBar.AsNeeded appears depending on whether the user's mouse is above the Flickable. This can result in a user being unaware that a list may be scrolled through unless they happen to move their mouse over it (something that can't be guaranteed). For this reason, ActiveScrollBar should be the preferred form of scroll bar that is used in the program.

Custom Validators in the NeXus Constructor

This section describes the custom validators used in the NeXus constructor and their purpose. Creating custom validators requires a combination of QML and Python which is discussed here. An overview of QML validators in general can be found here.

UnitValidator

The UnitValidator uses the pint library to check if a text input matches a valid unit of length. It will return QValidator.Intermediate and emit the ValidationFailed signal if:

  • the input could not be converted to a known unit
  • the input is a unit of time/volume/area/etc
  • the input has no units and simply consists of a number
  • the input has a magnitude other than 1 (e.g. "40 cm")

Some of these inputs are required because pint is flexible about what it regards as an acceptable physical quantity object. If given an empty string, pint will convert this to 1 dimensionless instead of throwing an Exception. This behaviour can then cause problems if a dimensionless object is allowed to propagate further into the program and be passed to functions that assume it is a length. In light of this, some care may be required when using pint for checking lengths as you might assume it will reject inputs that it is actually able to accept.

If the input passes all of the above checks then the UnitValidator will return QValidator.Acceptable and emit the ValidationSuccess signal.

Singletons

This section describes the Singletons that are used in the program. Information on how to create your own Singletons can be found here.

ValidUnits

The ValidUnits singleton contains variables that are used by QML when carrying out unit validation. These consist of

  • validMeshUnits - Defaults to False. Set to true if a user opens a mesh file and provides a valid unit of length.
  • validCylinderUnits - Defaults to False. Set to true if a user attempts to create a cylinder and provides a valid unit of length.
  • showCylinderUnitMessage - Defaults to False. Set to True if unit validation fails.
  • invalidUnitsText - Read-only string that contains the text displayed when unit validation fails.

These can then be used together through the following code:

 text: ValidUnits.showCylinderUnitMessage ? ValidUnits.invalidUnitsText : ""

In the above example, the text is either blank or contains a warning message depending on the state of showCylinderUnitMessage.

GeometryFileSelected

The GeometryFileSelected singleton contains a single boolean that defaults to false and is set to true if a user creates a mesh geometry and selects a file using a FileDialog. If a user attempts to ass a mesh without selecting a file then this is used to display a warning message.

The GeometryFileValidator performs the checks in Python that inform QML if a geometry file is valid. Unlike the QML Validators, GeometryFileValidator doesn't inherit from QValidator, it can't be used as the validator in a QML TextField, and it doesn't emit signals. It just happens to have "Validator" in the name and happens to aid QML in checking for acceptable input, but in a different way. These things should not be confused.

Clone this wiki locally