# refnx.dataset¶

class refnx.dataset.Data1D(data=None, mask=None, **kwds)[source]

Bases: object

A basic representation of a 1D dataset.

Parameters
• data (str, file-like or tuple of np.ndarray, optional) –

data can be a string or file-like object referring to a File to load the dataset from. The file should be plain text and have 2 to 4 columns separated by space, comma or tab. The columns represent x, y [y_err [, x_err]].

Alternatively it is a tuple containing the data from which the dataset will be constructed. The tuple should have between 2 and 4 members.

• data[0] - x

• data[1] - y

• data[2] - uncertainties on y, y_err

• data[3] - uncertainties on x, x_err

data must be at least two long, x and y. If the tuple is at least 3 long then the third member is y_err. If the tuple is 4 long then the fourth member is x_err. All arrays must have the same shape.

• mask (array-like) – Specifies which data points are (un)masked. Must be broadcastable to the y-data. Data1D.mask = None clears the mask. If a mask value equates to True, then the point is included, if a mask value equates to False it is excluded.

data

The data, (x, y, y_err, x_err)

Type

tuple of np.ndarray

finite_data

Data points that are finite

Type

tuple of np.ndarray

x

Type

np.ndarray

y

Type

np.ndarray

y_err

uncertainties on the y data (possibly masked)

Type

np.ndarray

x_err

uncertainties on the x data (possibly masked)

Type

np.ndarray

mask

Type

np.ndarray

filename

The file the data was read from

Type

str or None

weighted

Whether the y data has uncertainties

Type

bool

metadata

Information that should be retained with the dataset.

Type

dict

add_data(data_tuple, requires_splice=False, trim_trailing=True)[source]

Adds more data to the dataset.

Parameters
• data_tuple (tuple) – 2 to 4 member tuple containing the (x, y, y_err, x_err) data to add to the dataset. y_err and x_err are optional.

• requires_splice (bool, optional) – When the new data is added to the dataset do you want to scale it vertically so that it overlaps with the existing data? y and y_err in data_tuple are both multiplied by the scaling factor.

• trim_trailing (bool, optional) – When the new data is concatenated do you want to remove points from the existing data that are in the overlap region? This might be done because the datapoints in the data_tuple you are adding have have lower y_err than the preceding data.

Notes

Raises ValueError if there are no points in the overlap region and requires_splice was True. The added data is not masked.

property data

4-tuple containing the (x, y, y_err, x_err) data

property finite_data

4-tuple containing the (x, y, y_err, x_err) datapoints that are finite.

load(f)[source]

Loads a dataset from file. Must be 2 to 4 column ASCII.

Parameters

f (file-handle or string) – File to load the dataset from.

property mask
plot(fig=None)[source]

Plot the dataset.

Requires matplotlib be installed.

Parameters

fig (Figure instance, optional) – If fig is not supplied then a new figure is created. Otherwise the graph is created on the current axes on the supplied figure.

Returns

fig, axmatplotlib figure and axes objects.

Return type

matplotlib.Figure, matplotlib.Axes

refresh()[source]

save(f)[source]

Saves the data to file. Saves the data as 4 column ASCII.

Parameters

f (file-handle or string) – File to save the dataset to.

scale(scalefactor=1.0)[source]

Scales the y and y_err data by dividing by scalefactor.

Parameters

scalefactor (float) – The scalefactor to divide by.

sort()[source]

Sorts the data in ascending order

synthesise()[source]

Synthesise a new dataset by adding Gaussian noise onto each of the datapoints of the existing data.

Returns

dataset – A new synthesised dataset

Return type

refnx.dataset.Data1D

property unmasked_data

4-tuple containing unmasked (x, y, y_err, x_err) data

property x
property x_err
property y
property y_err
class refnx.dataset.ReflectDataset(data=None, **kwds)[source]

Bases: refnx.dataset.data1d.Data1D

A 1D Reflectivity dataset.

load(f)[source]

Load a dataset from file. Can either be 2-4 column ascii or XML file.

Parameters

f (str or file-like) – The file to load the spectrum from, or a str that specifies the file name

save_xml(f, start_time=0)[source]

Saves the reflectivity data to an XML file.

Parameters
• f (str or file-like) – The file to write the spectrum to, or a str that specifies the file name

• start_time (int, optional) – Epoch time specifying when the sample started