# refnx.reduce¶

class refnx.reduce.BatchReducer(filename, data_folder=None, verbose=True, persistent=True, **kwds)[source]

Bases: object

Batch reduction of reflectometry data based on spreadsheet metadata.

Example

>>> from refnx.reduce import BatchReducer
>>> data_folder = r'V:\data\current'
>>> b = BatchReducer('reduction.xls', data_folder=data_folder)
>>> b.reduce()


The spreadsheet must have columns:

reduce name scale refl1 refl2 refl3 dir1 dir2 dir3

Only rows where the value of the reduce column is 1 and where the sample name is set will be processed.

load_runs()[source]
reduce(show=True)[source]

Batch reduce data based on metadata from a spreadsheet

Parameters

show (bool (optional, default=True)) – display a summary table of the rows that were reduced

select_runs(all_runs)[source]
class refnx.reduce.Catalogue(h5d)[source]

Bases: object

Extract relevant parts of a NeXus file for reflectometry reduction

property datafile_number
class refnx.reduce.PlatypusNexus(h5data)[source]

Bases: refnx.reduce.platypusnexus.ReflectNexus

Processes Platypus NeXus files to produce an intensity vs wavelength spectrum

Parameters

h5data (HDF5 NeXus file or str) – An HDF5 NeXus file for Platypus, or a str containing the path to one

chod(omega=0.0, twotheta=0.0, scanpoint=0)[source]

Calculates the flight length of the neutrons in the Platypus instrument.

Parameters
• omega (float, optional) – Rough angle of incidence

• twotheta (float, optional) – Rough 2 theta angle

• scanpoint (int, optional) – Which dataset is being considered

Returns

chod, d_cx – Flight distance (mm), distance between chopper discs (mm)

Return type
phase_angle(scanpoint=0)[source]

Calculates the phase angle for a given scanpoint

Parameters

scanpoint (int) – The scanpoint you’re interested in

Returns

phase_angle, master_opening – The phase angle in degrees, and the angular opening of the master chopper

Return type

float

process(h5norm=None, lo_wavelength=2.5, hi_wavelength=19.0, background=True, direct=False, omega=None, twotheta=None, rebin_percent=1.0, wavelength_bins=None, normalise=True, integrate=-1, eventmode=None, event_folder=None, peak_pos=None, peak_pos_tol=0.0025, background_mask=None, normalise_bins=True, manual_beam_find=None, event_filter=None, **kwds)[source]

Processes the ProcessNexus object to produce a time of flight spectrum. The processed spectrum is stored in the processed_spectrum attribute. The specular spectrum is also returned from this function.

Parameters
• h5norm (str or HDF5 NeXus file) – If a str then h5norm is a path to the floodfield data, otherwise it is a hdf5 file handle containing the floodfield data.

• lo_wavelength (float) – The low wavelength cutoff for the rebinned data (A).

• hi_wavelength (float) – The high wavelength cutoff for the rebinned data (A).

• background (bool) – Should a background subtraction be carried out?

• direct (bool) – Is it a direct beam you measured? This is so a gravity correction can be applied.

• omega (float) – Expected angle of incidence of beam. If this is None, then the rough angle of incidence is obtained from the NeXus file.

• twotheta (float) – Expected two theta value of specular beam. If this is None then the rough angle of incidence is obtained from the NeXus file.

• rebin_percent (float) – Specifies the rebinning percentage for the spectrum. If rebin_percent is None, then no rebinning is done.

• wavelength_bins (array_like) – The wavelength bins for rebinning. If wavelength_bins is not None then the rebin_percent parameter is ignored.

• normalise (bool) – Normalise by the monitor counts.

• integrate (int) –

Specifies which scanpoints to use.

• integrate == -1 the spectrum is integrated over all the scanpoints.

• integrate >= 0 the individual spectra are calculated individually. If eventmode is not None, or event_filter is not None then integrate specifies which scanpoint to examine.

• eventmode (None or array_like) – If eventmode is None then the integrated detector image is used. If eventmode is an array then the array specifies the integration times (in seconds) for the detector image, e.g. [0, 20, 30] would result in two spectra. The first would contain data for 0 s to 20s, the second would contain data for 20 s to 30 s. This option can only be used when integrate >= -1. If eventmode has zero length (e.g. []), then a single time interval for the entire acquisition is used, [0, acquisition_time]. This would source the image from the eventmode file, rather than the NeXUS file. The two approaches will probably not give identical results, because the eventmode method adjusts the total acquisition time and beam monitor counts to the frame number of the last event detected (which may be quite different if the count rate is very low). This parameter is disregarded if event_filter is provided.

• event_folder (None or str) – Specifies the path for the eventmode data. If event_folder is None then the eventmode data is assumed to reside in the same directory as the NeXUS file. If event_folder is a string, then the string specifies the path to the eventmode data.

• peak_pos (-1, None, or (float, float)) –

Options for finding specular peak position and peak standard deviation.

• -1

use manual_beam_find.

• None

use the automatic beam finder, falling back to manual_beam_find if it’s provided.

• (float, float)

specify the peak and peak standard deviation.

• peak_pos_tol (float) – Convergence tolerance for the beam position and width to be accepted from successive beam-finder calculations; see the tol parameter in the find_specular_ridge function.

• background_mask (array_like) – An array of bool that specifies which y-pixels to use for background subtraction. Should be the same length as the number of y pixels in the detector image. Otherwise an automatic mask is applied (if background is True).

• normalise_bins (bool) – Divides the intensity in each wavelength bin by the width of the bin. This allows one to compare spectra even if they were processed with different rebin percentages.

• manual_beam_find (callable, optional) – A function which allows the location of the specular ridge to be determined. Has the signature f(detector, detector_err) where detector and detector_err is the detector image and its uncertainty. detector and detector_err have shape (n, t, y) where n is the number of detector images, t is the number of time of flight bins and y is the number of y pixels. The function should return a tuple, (centre, centre_sd, lopx, hipx, background_pixels). centre, centre_sd, lopx, hipx should be arrays of shape (n, ), specifying the beam centre, beam width (standard deviation), lowest pixel of foreground region, highest pixel of foreground region. background_pixels is a list of length n. Each of the entries should contain arrays of pixel numbers that specify the background region for each of the detector images.

• event_filter (callable, optional) –

A function, that processes the event stream, returning a detector array, and a frame_count array. detector has shape (N, T, Y, X), where N is the number of detector images, T is the number of time bins (len(t_bins)), etc. frame_count has shape (N,) and contains the number of frames for each of the detector images. The frame_count is used to determine what fraction of the overall monitor counts should be ascribed to each detector image (by dividing by the total number of frames). The function has signature:

detector, frame_count = event_filter(loaded_events,

t_bins=None, y_bins=None, x_bins=None)

loaded_events is a 4-tuple of numpy arrays: (f_events, t_events, y_events, x_events), where f_events contains the frame number for each neutron, landing at position x_events, y_events on the detector, with time-of-flight t_events.

Notes

After processing this object contains the following the following attributes:

• path - path to the data file

• datafilename - name of the datafile

• datafile_number - datafile number.

• m_topandtail - the corrected 2D detector image, (n_spectra, TOF, Y)

• m_topandtail_sd - corresponding standard deviations

• n_spectra - number of spectra in processed data

• bm1_counts - beam montor counts, (n_spectra,)

• m_spec - specular intensity, (n_spectra, TOF)

• m_spec_sd - corresponding standard deviations

• m_beampos - beam_centre for each spectrum, (n_spectra, )

• m_lambda - wavelengths for each spectrum, (n_spectra, TOF)

• m_lambda_fwhm - corresponding FWHM of wavelength distribution

• m_lambda_hist - wavelength bins for each spectrum, (n_spectra, TOF)

• m_spec_tof - TOF for each wavelength bin, (n_spectra, TOF)

• mode - the Platypus mode, e.g. FOC/MT/POL/POLANAL/SB/DB

• detector_z - detector height, (n_spectra, )

• detector_y - sample-detector distance, (n_spectra, )

• domega - collimation uncertainty

• lopx - lowest extent of specular beam (in y pixels), (n_spectra, )

• hipx - highest extent of specular beam (in y pixels), (n_spectra, )

Returns

m_lambda, m_spec, m_spec_sd – Arrays containing the wavelength, specular intensity as a function of wavelength, standard deviation of specular intensity

Return type

np.ndarray

process_event_stream(t_bins=None, x_bins=None, y_bins=None, frame_bins=None, scanpoint=0, event_folder=None, event_filter=None)[source]

Processes the event mode dataset for the NeXUS file. Assumes that there is a event mode directory in the same directory as the NeXUS file, as specified by in ‘entry1/instrument/detector/daq_dirname’

Parameters
• t_bins (array_like, optional) – specifies the time bins required in the image

• x_bins (array_like, optional) – specifies the x bins required in the image

• y_bins (array_like, optional) – specifies the y bins required in the image

• scanpoint (int, optional) – Scanpoint you are interested in

• event_folder (None or str) – Specifies the path for the eventmode data. If event_folder is None then the eventmode data is assumed to reside in the same directory as the NeXUS file. If event_folder is a string, then the string specifies the path to the eventmode data.

• frame_bins (array_like, optional) – specifies the frame bins required in the image. If frame_bins = [5, 10, 120] you will get 2 images. The first starts at 5s and finishes at 10s. The second starts at 10s and finishes at 120s. If frame_bins has zero length, e.g. [], then a single interval consisting of the entire acquisition time is used: [0, acquisition_time]. If event_filter is provided then this parameter is ignored.

• event_filter (callable, optional) –

A function, that processes the event stream, returning a detector array, and a frame_count array. detector has shape (N, T, Y, X), where N is the number of detector images, T is the number of time bins (len(t_bins)), etc. frame_count has shape (N,) and contains the number of frames for each of the detector images. The frame_count is used to determine what fraction of the overall monitor counts should be ascribed to each detector image. The function has signature:

detector, frame_count = event_filter(loaded_events,

t_bins=None, y_bins=None, x_bins=None)

loaded_events is a 4-tuple of numpy arrays: (f_events, t_events, y_events, x_events), where f_events contains the frame number for each neutron, landing at position x_events, y_events on the detector, with time-of-flight t_events.

Returns

• detector, frame_count, bm1_counts (np.ndarray, np.ndarray, np.ndarray)

• Create a new detector image based on the t_bins, x_bins, y_bins and

• frame_bins you supply to the method (these should all be lists/numpy

• arrays specifying the edges of the required bins). If these are not

• specified, then the default bins are taken from the nexus file. This

• would essentially return the same detector image as the nexus file.

• However, you can specify the frame_bins list to generate detector

• images based on subdivided periods of the total acquisition.

• For example if frame_bins = [5, 10, 120] you will get 2 images. The

• first starts at 5s and finishes at 10s. The second starts at 10s

• and finishes at 120s. The frame_bins are clipped to the total

• acquisition time if necessary.

• frame_count is how many frames went into making each detector image.

class refnx.reduce.PlatypusReduce(direct, data_folder=None, **kwds)[source]

Bases: refnx.reduce.reduce.ReflectReduce

Reduces Platypus reflectometer data to give the specular reflectivity. Offspecular data maps are also produced.

Parameters
• direct (string, hdf5 file-handle or PlatypusNexus object) – A string containing the path to the direct beam hdf5 file, the hdf5 file itself, or a PlatypusNexus object.

• data_folder (str, optional) – Where is the raw data stored?

Examples

>>> from refnx.reduce import PlatypusReduce
>>> reducer = PlatypusReduce('PLP0000711.nx.hdf')
>>> datasets, reduced = reducer.reduce('PLP0000711.nx.hdf',
...                                    rebin_percent=2)

refnx.reduce.accumulate_HDF_files(files)[source]

Accumulates HDF files together, writing an accumulated file in the current directory. The accumulated datafile is written in the current directory (os.getcwd()) and has a filename based on the first file, prepended by ‘ADD_’. For example, if the first file is PLP0000708.nx.hdf then the accumulated file is ADD_PLP0000708.nx.hdf.

Parameters

files (list) – Strings specifying NeXUS filenames to be added together.

refnx.reduce.basename_datafile(pth)[source]

Given a NeXUS path return the basename minus the file extension. :param pth: :type pth: str

Returns

basename

Return type

str

Examples

>>> basename_datafile('a/b/c.nx.hdf')
'c'

refnx.reduce.catalogue(start, stop, data_folder=None)[source]

Extract interesting information from Platypus NeXUS files.

Parameters
• start (int) – start cataloguing from this run number.

• stop (int) – stop cataloguing at this run number

• data_folder (str, optional) – path specifying location of NeXUS files

Returns

catalog – Dataframe containing interesting parameters from Platypus Nexus files

Return type

pd.DataFrame

refnx.reduce.datafile_number(fname)[source]

From a filename figure out what the run number was

Parameters

fname (str) – The filename to be processed

Returns

run_number – The run number

Return type

int

Examples

>>> datafile_number('PLP0000708.nx.hdf')
708

refnx.reduce.gui()[source]
refnx.reduce.main()[source]
refnx.reduce.number_datafile(run_number, prefix='PLP')[source]

Given a run number figure out what the file name is. Given a file name, return the filename with the .nx.hdf extension

Parameters
• run_number (int or str) –

• prefix (str, optional) – The instrument prefix. Only used if run_number is an int

Returns

file_name

Return type

str

Examples

>>> number_datafile(708)
'PLP0000708.nx.hdf'
>>> number_datafile(708, prefix='QKK')
'QKK0000708.nx.hdf'
>>> number_datafile('PLP0000708.nx.hdf')
'PLP0000708.nx.hdf'

refnx.reduce.reduce_stitch(reflect_list, direct_list, background_list=None, norm_file_num=None, data_folder=None, prefix='PLP', trim_trailing=True, save=True, **kwds)[source]

Reduces a list of reflected beam run numbers and a list of corresponding direct beam run numbers from the Platypus reflectometer. If there are multiple reflectivity files they are spliced together.

Parameters
• reflect_list (list) – Reflected beam run numbers, e.g. [708, 709, 710] 708 corresponds to the file PLP0000708.nx.hdf.

• direct_list (list) – Direct beam run numbers, e.g. [711, 711, 711]

• background_list (list, optional) – List of bool to control whether background subtraction is used for each reduction, e.g. [False, True, True]. The default is to do a background subtraction on all runs.

• norm_file_num (int, optional) – The run number for the water flood field correction.

• data_folder (str, optional) – Where is the raw data stored?

• prefix (str, optional) – The instrument filename prefix.

• trim_trailing (bool, optional) – When datasets are spliced together do you want to remove points in the overlap region from the preceding dataset?

• save (bool, optional) – If True then the spliced file is written to a file (in the working directory) with a name like: c_PLP0000708.dat.

• kwds (dict, optional) – Options passed directly to refnx.reduce.platypusnexus.process, for processing of individual spectra. Look at that method docstring for specification of options.

Returns

combined_dataset, reduced_filename – The combined dataset and the file name of the reduced data, if it was saved. If it wasn’t saved reduced_filename is None.

Return type

Notes

If background is in the supplied kwds it is ignored. The prefix is used to specify the run numbers to a filename. For example a run number of 10, and a prefix of PLP resolves to a NeXus filename of ‘PLP0000010.nx.hdf’.

Examples

>>> from refnx.reduce import reduce_stitch
>>> dataset, fname = reduce_stitch([708, 709, 710],
...                                [711, 711, 711],
...                                 rebin_percent=2)

refnx.reduce.reduce_xrdml(f, bkg=None, scale=1, sample_length=None)[source]

Reduces a Panalytical XRDML file

Parameters
• f (file-like object or string) – The specular reflectivity (XRDML) file of interest

• bkg (list) – A list of file-like objects or strings that contain background measurements. The background is assumed to have the same number of points as the specular reflectivity curve. The backgrounds are averaged and subtracted from the specular reflectivity

• scale (float) – The direct beam intensity (cps)

• sample_length (None or float) – If None then no footprint correction is done. Otherwise the transverse footprint of the sample (mm).

Returns

specular_q, specular_r, specular_dr – The specular reflectivity as a function of momentum transfer, Q.

Return type

np.ndarray

refnx.reduce.test = <refnx._lib._testutils.PytestTester object>