sample

Handle data associated with a sample.

class sample.Sample(input_arr, **kwargs)

Handle the measured data along with metadata.

This class is a subclass of the numpy.ndarray class with additional methods and attributes that are specific to small-angle X-ray scattering experiments on biological samples.

It can handle various operations such as addition and subtraction of sample data or numpy array, scaling by a scalar or an array, indexing, broadcasting, reshaping, binning, sliding average or data cleaning.

Parameters
  • input_arr (np.ndarray, list, tuple or scalar) – Input array corresponding to sample scattering data.

  • kwargs (dict (optional)) –

    Additional keyword arguments either for np.asarray() or for sample metadata. The metadata are:

    • filename, the name of the file used to extract the data.

    • errors, the errors associated with scattering data.

    • time, the experimental time.

    • elution_volume, if available, the elution volume of the HPLC.

    • I0, the fitted intensity at q = 0.

    • I0_std, the uncertainty on I0 value(s).

    • rg, the gyration radius.

    • rg_std, the uncertainty on Rg value(s).

    • wavelength, the wavelength of the X-ray beam.

    • name, the name for the sample.

    • temperature, the temperature(s) used experimentally.

    • concentration, the concentration of the sample.

    • pressure, the pressure used experimentally.

    • buffer, a description of the buffer used experimentally.

    • q, the values for the momentum transfer q.

    • detector, the detector used.

    • beamline, the name of the beamline used.

    • flow_rate, the flow rate inside the capillary.

    • observable, for 2D dataset the name of the attribute

      corresponding to the first axis.

Note

The errors metadata is special as it is updated for various operations that are performed on the data array such as indexing or for the use of universal functions. For instance, indexing of the data will be performed on errors as well if its shape is the same as for the data. Also, addition, subtraction and other universal functions will lead to automatic error propagation. Some other metadata might change as well, like q, but only for the use of methods specific of the Sample class and not for methods inherited from numpy.

Examples

A sample can be created using the following:

>>> s1 = Sample(
...     np.arange(5),
...     dtype='float32',
...     errors=np.array([0.1, 0.2, 0.12, 0.14, 0.15])
... )
>>> buffer = Sample(
...     [0., 0.2, 0.4, 0.3, 0.1],
...     dtype='float32',
...     errors=np.array([0.1, 0.2, 0.05, 0.1, 0.2])
... )

where my_data, my_errors and q_values are numpy arrays. A buffer subtraction can be performed using:

>>> s1 = s1 - buffer
Sample([0. , 0.80000001, 1.60000002, 2.70000005, 3.9000001], dtype=float32)

where buffer1 is another instance of Sample. The error propagation is automatically performed and the other attributes are taken from the first operand (here s1). Other operations such as scaling can be performed using:

>>> s1 = 0.8 * s1
Sample([0. , 0.80000001, 1.60000002, 2.4000001, 3.20000005], dtype=float32)

You can transform another Sample instance into a column vector and look how broadcasting and error propagation work:

>>> s2 = Sample(
...     np.arange(5, 10),
...     dtype='float32',
...     errors=np.array([0.1, 0.3, 0.05, 0.1, 0.2])
... )
>>> s2 = s2[:, np.newaxis]
>>> res = s1 * s2
>>> res.errors
array([[0.5       , 1.00498756, 0.63245553, 0.76157731, 0.85      ],
       [0.6       , 1.23693169, 0.93722996, 1.23109707, 1.5       ],
       [0.7       , 1.40089257, 0.84593144, 0.99141313, 1.06887792],
       [0.8       , 1.60312195, 0.98061205, 1.15948264, 1.26491106],
       [0.9       , 1.81107703, 1.1516944 , 1.3955644 , 1.56923548]])
property T

The transposed array.

Same as self.transpose().

Examples

>>> x = np.array([[1.,2.],[3.,4.]])
>>> x
array([[ 1.,  2.],
       [ 3.,  4.]])
>>> x.T
array([[ 1.,  3.],
       [ 2.,  4.]])
>>> x = np.array([1.,2.,3.,4.])
>>> x
array([ 1.,  2.,  3.,  4.])
>>> x.T
array([ 1.,  2.,  3.,  4.])

See also

`transpose`_

bin(bin_size, *metadata, axis=0)

Bin data with the given bin size along specified axis.

Parameters
  • bin_size (int) – The size of the bin (in number of data points).

  • metadata (strings) – List of metadata names that should be binned as well.

  • axis (int, optional) – The axis over which the binning is to be performed. (default, 0)

Returns

out_arr – A binned instance of Sample with the same metadata except for errors, which are binned as well and possibly other user provided metadata names.

Return type

Sample

get_q_range(qmin, qmax)

Helper function to select a specific momentum transfer range.

The function assumes that q values correspond to the last dimension of the data set.

Parameters
  • qmin (int) – The minimum value for the momentum transfer q range.

  • qmax (int) – The maximum value for the momentum transfer q range.

Returns

out – A new instance of the class with the selected q range.

Return type

Sample

get_time_range(tmin, tmax)

Helper function to select a specific time range.

The function assumes that time values correspond to the first dimension of the data set.

Parameters
  • tmin (int) – The minimum value for time.

  • tmax (int) – The maximum value for time.

Returns

out – A new instance of the class with the selected time range.

Return type

Sample

plot(plot_type='standard', axis=0, xlabel=None, ylabel='I(q)', new_fig=False, max_lines=10, colormap='jet')

Helper function for quick plotting.

Parameters
  • plot_type (str) –

    Type of plot to be generated (for q on x-axis). Possible options are:

    • ’standard’, I(q) vs. q

    • ’q2’, I(q) vs. q ** 2

    • ’log’, log[I(q)] vs. q

    • ’guinier’, log[I(q)] vs. q ** 2

    • ’kratky’, q**2 * I(q) vs. q

  • axis (int) – The axis along which to plot the data. If xlabel is None, then for 1D data, 0 is assumed to be q values, and for 2D data, 0 is assumed to correspond to time and 1 to q values.

  • xlabel (str) – The label for the x-axis. (default None)

  • ylabel (str) – The label for the y-axis. (default ‘log(I(q))’)

  • new_fig (bool) – If true, create a new figure instead of plotting on the existing one.

  • max_lines (int) – For 2D data, maximum number of lines to be plotted.

  • colormap (str) – The colormap to be used for 2D data.

range_selector(axis=0, sel_data=None, **kwargs)

Interactive plot to manually select a data range.

Parameters
  • axis (int, optional) – The axis along which to plot the data. If xlabel is None, then for 1D data, 0 is assumed to be q values, and for 2D data, 0 is assumed to correspond to time and 1 to q values. (default, 0)

  • sel_data (Sample, optional) – An instance of Sample class that will be used to perform the range selection. If None, the instance from which the method was called will be used. (default, None)

  • kwargs (dict, keywords arguments) – Additional arguments to be passed to the Sample.plot() method.

sliding_average(window_size, *metadata, axis=0)

Performs a sliding average of data and errors along given axis.

Parameters
  • window_size (int) – Size of the window for the sliding average.

  • metadata (strings) – List of metadata names that should be binned as well.

  • axis (int, optional) – The axis over which the average is to be performed. (default, 0)

Returns

out_arr – An averaged instance of Sample with the same metadata except for errors, which are processed as well.

Return type

Sample

write_csv(filename, header=None, comments='# ', footer=None)

Write the data in text format.

By default, the text contains three columns, the first one corresponding to q values, the second to I(q) and the third to the associated errors.

If sample data are 2D, the data will be averaged over the first dimension before writing the file.

Parameters
  • filename (str) – The name of the file to be written.

  • header (str, optional) – A string defining the header of the file.

  • comments (str, optional) – The string to be prepend to header lines to indicate a comment.

  • footer (str, optional) – A string defining the footer of the file.