data

Module for data handling.

nexus.data.AutoFold(data, flip='left')

Folds a one dimensional data array. Uses autocorrelation to determine the correct folding point. The given data array is cut in half. Data of the second half are rolled and re-introduced on the other side to ensure equal intensities. Give an even number of data points. Returns the folded data and the fold index.

The central shift depends on the folding direction (left or right). The folding direction depends on the experimental setup. Measure the second order Doppler shift to determine the correct folding.

Changed in version 1.0.2.

Parameters:
  • data (list) – Data to be folded. Number of data points has to be even.

  • flip (string) –

    determines whether the left or right part of the spectrum is flipped.

    New in version 1.0.2.

Returns:

Folded data, fold_index

Return type:

ndarray, int

nexus.data.AutoFold_2D(data, axis='hor', flip='left')

Folds a two dimensional data array along the given axis. Uses autocorrelation on the data, which are integrated over the second axis, to determine the correct folding point. The given data array is cut in half. Data of the second half are rolled and re-introduced on the other side to ensure equal intensities. Give an even number of data points along the fold direction. Returns the folded data and the fold index.

The central shift depends on the folding direction (left or right). The folding direction depends on the experimental setup. Measure the second order Doppler shift to determine the correct folding.

Changed in version 1.0.2.

Parameters:
  • data (list) – Data to be folded. Number of data points has to be even along fold direction.

  • axis (int) – Either hor (horizontal) or vert (vertical) folding.

  • flip (string) –

    determines whether the left or right (upper/lower) part of the spectrum is flipped.

    New in version 1.0.2.

Returns:

Folded data, fold index

Return type:

ndarray, int

nexus.data.Baseline(data, lam, algorithm='aspls', max_iter=100, tol=0.001)

Apply various algorithms for baseline correction of the data. The correct lam value has to be specified by the user. A good procedure is to start with a low value (100) and then increase in an exponential manner (1e3, 1e4, …). At some point the curvature of the baseline changes. Then, reduce the values again and adjust.

Be aware, that baseline correction changes the intensity of the lines.

For more options and other algorithms use the pybaselines package.

New in version 1.0.4.

Parameters:
  • data (list) – Data to be corrected.

  • lam (float) – The smoothing parameter. Larger values will create smoother baselines.

  • algorithm (string) –

    Defines the used algorithm.

    • aspls Adaptive Smoothness Penalized Least Squares.

    • mpls Morphological Penalized Least Squares.

    • asls Asymmetric Least Squares.

    • drlps Doubly Reweighted Penalized Least Squares.

    • arpls Asymmetrically Reweighted Penalized Least Squares.

    • iarpls Improved Asymmetrically Reweighted Penalized Least Squares.

    • amormol Iteratively averaging morphological and mollified baseline fit.

    See pybaselines package documentaiton for more information.

  • max_iter (int) – Maximum number of iterations. Default is 100.

  • tol (float) – Tolerance value, defines the exit criteria. Default is 0.001.

Returns:

Returns the data corrected by the baseline and the baseline.

Return type:

ndarray, ndarray

nexus.data.BaselinePoly(data, left_point, right_point, poly_order=2)

Apply a polynomial baseline correction to the data. In order to correct the baseline, only parts of the spectrum should be used where no peaks are visible.

Be aware, that baseline correction changes the intensity of the lines.

For more options and other algorithms use the pybaselines package.

New in version 1.0.4.

Parameters:
  • data (list) – Data to be corrected.

  • left_point (int) – Index of the data array. Data from zero to this index are used.

  • left_point – Index of the data array. Data from this point to the end of the array are used.

  • poly_order (int) – Polynominal order for fitting the baseline. Default is 2.

Returns:

Returns the data corrected by the baseline and the baseline.

Return type:

ndarray, ndarray

nexus.data.Binning(data, bin=2, method='sum', endpoint=False)

Performs a binning/bucketing procedure on the data array with the given bin size. Either the sum or the mean of the binned data points can be calculated. If data array cannot be divided into equal sizes the endpoint can be discarded or binned with smaller size.

Parameters:
  • data (list) – Data to be folded.

  • bin (int) – Bin size, the number of points to be processed.

  • method (string) – Either sum or mean.

  • endpoint (bool) – Determines the endpoint treatment in case the array length is not integer dividable by bin. If False the last points are discarded. If True the bin of the last points equals the number of points left. It will result in non-equidistant points on the x-axis.

Returns:

Binned data.

Return type:

ndarray

nexus.data.CalibrateChannels(intensity, velocity, intensity_reference, velocity_reference, mode='constant', shift=0.0, center_shift=0.0)

Calibrates the velocity of the intensity data set to a given reference data set with known velocity. The intensity array is fit against the reference spectrum.

Changed in version 1.0.3: The velocity offset is now applied to the theory not to the experiment anymore.

Changed in version 1.0.4: Reverted change of version 1.0.3. Offset is applied to velocity again to properly account for drive offset.

Parameters:
  • intensity (list) – Data for which the velocity should be calibrated.

  • velocity (float or list) – If a float is given, the velocity is the assumed maximum velocity of the data set. If a list is provided, it is the assumed experimental velocity grid.

  • intensity_reference (list) – Array of intensity data used as reference.

  • velocity_reference (list) – Array of velocities used as reference.

  • mode (string) – Either constant acceleration mode (constant) or sinusoidal mode (sinus) of the Moessbauer drive unit. Default is constant.

  • shift (float) –

    Initial guess for shift of the spectrum in mm/s. Optional, default is zero.

    New in version 1.0.3.

  • center_shift (float) –

    Offset to the velocity calibration in mm/s. Optional, default is zero. For example, when a center shift is known between source and velocity reference sample.

    New in version 1.0.4.

Returns:

calibrated experimental velocity, velocity offset, velocity scaling factor, velocity used for the theoretical spectrum, theoretical spectrum

Return type:

ndarray, float, float, ndarray, ndarray

nexus.data.CalibrateChannelsAlphaFe(intensity, velocity, thickness, Bhf=33, B_fwhm=0.3, emission=False, mode='constant', shift=0.0, center_shift=0.0)

Calibrates the velocity of an isotropically distributed \(\\alpha\)-Fe spectrum. No beam polarization. The intensity array is fit against a theoretical sextet of \(\\alpha\)-Fe.

Changed in version 1.0.3: The velocity offset is now applied to the theory not to the experiment anymore.

Changed in version 1.0.4: Reverted change of version 1.0.3. Offset is applied to velocity again to properly account for drive offset.

Parameters:
  • intensity (list) – Data from an \(\\alpha\)-Fe sample.

  • velocity (float or list) – If a float is given, the velocity is the assumed maximum velocity of the data set. If a list is provided, it is the assumed experimental velocity grid.

  • thickness (float) – Thickness of the \(\\alpha\)-Fe sample (nm). Please note, that for sample thicknesses exceeding the typical escape length of the emitted probe (X-ray fluorescence or electrons) the escape length should be given.

  • Bhf (float) – The magnitude of the magnetic hyperfine field. Default is 33 T.

  • B_fwhm (float) – The FWHM of the magnetic hyperfine field distribution. Default is 0.3 T.

  • emission (bool) – If False (default), an absorption spectrum is calculated. If True an emission spectrum is calculated.

  • mode (string) – Either constant acceleration mode (constant) or sinusoidal mode (sinus) of the Moessbauer drive unit. Default is constant.

  • shift (float) –

    initial guess for shift of the spectrum in mm/s.

    New in version 1.0.3.

  • center_shift (float) –

    Offset to the velocity calibration in mm/s. Optional, default is zero. For example, when a center shift is known between source and velocity reference sample.

    New in version 1.0.4.

Returns:

calibrated experimental velocity, velocity offset, velocity scaling factor, velocity used for the theoretical spectrum, theoretical spectrum

Return type:

ndarray, float, float, ndarray, ndarray

nexus.data.CalibrateChannelsExperiment(intensity, velocity, experiment, emission=False, mode='constant', shift=0.0, center_shift=0.0)

Calibrates the velocity of a reference experiment. The theoretical model of the reference Experiment must be given. The intensity array is fit against the theoretical spectrum.

Changed in version 1.0.3: The velocity offset is now applied to the theory not to the experiment anymore.

Changed in version 1.0.4: Reverted change of version 1.0.3. Offset is applied to velocity again to properly account for drive offset.

Parameters:
  • intensity (list) – Data for which the velocity should be calibrated.

  • velocity (float or list) – If a float is given, the velocity is the assumed maximum velocity of the data set. If a list is provided, it is the assumed experimental velocity grid.

  • experiment (nx.Experiment) – An Experiment against which the experimental velocity is fit.

  • emission (bool) – If False (default), an absorption spectrum is calculated. If True an emission spectrum is calculated.

  • mode (string) – Either constant acceleration mode (constant) or sinusoidal mode (sinus) of the Moessbauer drive unit. Default is constant.

  • shift (float) –

    initial guess for shift of the spectrum in mm/s.

    New in version 1.0.3.

  • center_shift (float) –

    Offset to the velocity calibration in mm/s. Optional, default is zero. For example, when a center shift is known between source and velocity reference sample.

    New in version 1.0.4.

Returns:

calibrated experimental velocity, velocity offset, velocity scaling factor, velocity used for the theoretical spectrum, theoretical spectrum

Return type:

ndarray, float, float, ndarray, ndarray

nexus.data.ChannelsToVelocity(num_channels, velocity, offset=0, mode='constant')

Converts a given number of channels to a velocity grid with possible offset for either sinusoidal mode or constant acceleration mode of a MOessbauer drive unit.

Parameters:
  • num_channels (int) – Number of channels.

  • velocity (float) – maximum velocity in mm/s. The velocity is set symmetrically around zero.

  • offset (float) – Offset to the symmetric velocity in mm/s. default is 0.

  • mode (string) – Either constant acceleration mode (constant) or sinusoidal mode (sinus) of the Moessbauer drive unit. Default is constant.

Returns:

Velocity of the channels in mm/s.

Return type:

ndarray

nexus.data.FindMaxima(x_data, y_data, n=None)

Returns the x and y values of maxima or of a single maximum found on the y data set.

New in version 1.0.2.

Parameters:
  • x_data (list) – x data.

  • y_data (list) – y data on which the minima are searched for.

  • n (int) – returns only the n-th minimum, optional parameter.

Returns:

minima value(s) on the x axis, minima value(s) on the y axis, indices of the minima

Return type:

ndarray or float, ndarray or float, ndarray or int

nexus.data.FindMinima(x_data, y_data, n=None)

Returns the x and y values of minima or of a single minimum found on the y data set.

New in version 1.0.2.

Parameters:
  • x_data (list) – x data.

  • y_data (list) – y data on which the minima are searched for.

  • n (int) – returns only the n-th minimum, optional parameter.

Returns:

minima value(s) on the x axis, minima value(s) on the y axis, indices of the minima

Return type:

ndarray or float, ndarray or float, ndarray or int

nexus.data.Fold(data, folding_point, flip='left')

Folds a one dimensional data array at the given folding point. The given array is cut in half. Data of the second half are rolled and re-introduced on the other side to ensure equal intensities. Give an even number of data points.

The central shift depends on the folding direction (left or right). The folding direction depends on the experimental setup. Measure the second order Doppler shift to determine the correct folding.

Changed in version 1.0.2.

Parameters:
  • data (list) – Data to be folded. Number of data points has to be even.

  • folding_point (int) – Folding point of the array.

  • flip (string) –

    determines whether the left or right part of the spectrum is flipped.

    New in version 1.0.2.

Returns:

Folded data.

Return type:

ndarray

nexus.data.HistogramStdDev(data)

Determines the standard deviation of histogram data, which is given by the square root of the measured histogram value.

For Poisson statistics the expectation value (here the one single measured value in a data set) and the variance are the same. Thus, the variance is \(Var = y = \sigma_y^2\), with the measured value \(y\). The standard deviation is \(\sigma_y = \sqrt{y}\).

nexus.data.Load(file_name, x_index, intensity_index, x_start=None, x_stop=None, intensity_threshold=None)

Load data arrays from columns in file.

Parameters:
  • file_name (string) – File to load (with relative path).

  • x_index (int) – Column index in file for x axis data.

  • intensity_index (int) – Column index in file for intensity data.

  • x_start (float) – x data smaller than x_start are not considered.

  • x_stop (float) – x data larger than x_stop are not considered.

  • intensity_threshold (float) – intensity data <= intensity_threshold are not considered.

nexus.data.Save(file_name, data_list)

Save data arrays as columns to file.

Parameters:
  • file_name (string) – File to load (with relative path).

  • data_list (list) – List containing the data to save column wise.

Examples

loading data

# load a time_spectrum
# column index from 0.
time_data, int_data = nx.data.load('data_folder\example_time_spectrum.dat',
                                    x_index = 1,  # time data are in column 1
                                    intensity_index = 2,  # intensity data in column 2
                                    x_start = 20,  # load only data with time data larger 20
                                    x_stop = 200,  # load only data with time data smaller 200
                                    intensity_threshold = None)  # not intensity threshold

saving data

# save some data arrays
num_points = 11

angles = np.linspace(0.1, 5, num_points)

intensity = np.random.rand(num_points)

nx.data.save('example_file.txt', [angles, intensity])

folding data

def gauss(x, offset):
  return np.exp(-1/2*(x-offset)**2/0.5*2)

arr = np.linspace(-8, 8, 100)

dat = gauss(arr, -4) + gauss(arr, 4.6)

folded_dat, shift_index = nx.data.Fold(dat)