data
Module for data handling.
- nexus.data.AutoFold(data, flip='left', factor=1, method='linear', extra_data=[])
Folds a one dimensional data array. Uses autocorrelation to determine the correct shift between the spectra. The given data array is cut in half. Data of the flipped half are rolled (and so re-introduced on the other side) to ensure equal intensities. Give an even number of data points. Returns the folded data and the lag, which describes how much the two parts are shifted.
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.2.0: now returns the lag instead of the folding point.
- 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.
Added in version 1.0.2.
factor (int) –
the interpolation factor. For 1 the original data are used. For higher values the data are interpolated by a times factor finer grid than the input data.
Added in version 1.2.0.
method (string) –
interpolation method.
linear
: Linear interpolation.cubic
: Cubic spline interpolation.PChip
: PCHIP 1-D monotonic cubic interpolation.Akima
: Akima interpolation.
Added in version 1.2.0.
extra_data (list) –
List of data sets that are folded in the same way as the provided
data
for which the folding is performed.Added in version 2.0.0.
- Returns:
folded data, lag, list
- Return type:
ndarray, int, list of folded extra_data
- 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.
Added 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.
Added 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.
Added 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.
right_point (int) – 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
ormean
.endpoint (bool) – Determines the endpoint treatment in case the array length is not integer dividable by bin. If
False
the last points are discarded. IfTrue
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 isconstant
.shift (float) –
Initial guess for shift of the spectrum in mm/s. Optional, default is zero.
Added 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.
Added 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.0, 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. IfTrue
an emission spectrum is calculated.mode (string) – Either constant acceleration mode (
constant
) or sinusoidal mode (sinus
) of the Moessbauer drive unit. Default isconstant
.shift (float) –
initial guess for shift of the spectrum in mm/s.
Added 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.
Added 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. IfTrue
an emission spectrum is calculated.mode (string) – Either constant acceleration mode (
constant
) or sinusoidal mode (sinus
) of the Moessbauer drive unit. Default isconstant
.shift (float) –
initial guess for shift of the spectrum in mm/s.
Added 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.
Added 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.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 isconstant
.
- 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.
Added in version 1.0.2.
- Parameters:
x_data (list) – x data.
y_data (list) – y data on which the maxima are searched for.
n (int) – returns only the n-th maximum, optional parameter.
- Returns:
maxima position on the x axis, maxima values of the y axis, indices of the maxima
- 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.
Added 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 position on the x axis, minima values on the y axis, indices of the minima
- Return type:
ndarray or float, ndarray or float, ndarray or int
- nexus.data.FindPeaks(x, y, n=6, neg=True)
Returns the x position and y values of a number of expected extrema on the y data.
Added in version 1.2.0.
- Parameters:
x_data (list) – x data.
y_data (list) – y data on which the minima are searched for.
n (int) – number of peaks to find.
neg (bool) – If “True”, negative peaks are searched for. Default is “True”.
- Returns:
extrema position on the x axis, extrema on the y axis, indices of the extrema, number of extrema found
- Return type:
ndarray or float, ndarray or float, ndarray or int, 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.
Added in version 1.0.2.
- Returns:
Folded data
- Return type:
ndarray
- nexus.data.Fold_Interpolate(data, flip='left', factor=1, method='linear', lag=0.0)
Folds a one dimensional data array. The given data array is cut in half. Data of the flipped half are rolled (and so re-introduced on the other side) to ensure equal intensities. Give an even number of data points. The lag between the two points defines how far the two arrays are shifted in terms of channels. The precission must match the factor. So X.X1 must have a factor of 100.
Added in version 2.0.0.
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.
- 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.
factor (int) – the interpolation factor. For 1 the original data are used. For higher values the data are interpolated by a times factor finer grid than the input data.
method (string) –
interpolation method.
linear
: Linear interpolation.cubic
: Cubic spline interpolation.PChip
: PCHIP 1-D monotonic cubic interpolation.Akima
: Akima interpolation.
lag (float): Number of channels to be shifted.
- Returns:
Folded data.
- Return type:
ndarray
- nexus.data.GetAreas(velocity, intensity, n=6, neg=True, norm=None)
Returns the sum of the peak areas found in the fit from smallest to highest detuning values and their ratios on the total area.
Added in version 1.2.0.
- Parameters:
velocity (list) – velocity data.
intensity (list) – intensity data.
n (int) – number of peaks to find.
neg (bool) – If “True”, negative peaks are searched for. Default is “True”.
norm (string) – Determines the normalization factor. None, “sum”, “mean”, “min” or “max”.
- Returns:
areas values, area ratio
- Return type:
ndarray, ndarray
- nexus.data.GetLinewidths(velocity, intensity, n=6, neg=True)
Returns the widths in units of velocity values of the peaks found. The number of peaks needs to be specified. Helpful to get the instrumental function from a reference spectrum.
Added in version 1.2.0.
- Parameters:
velocity (list) – velocity data.
intensity (list) – intensity data.
n (int) – number of peaks to find.
neg (bool) – If “True”, negative peaks are searched for. Default is “True”.
- Returns:
width (FWHM) values in units of the velocity
- Return type:
ndarray
- nexus.data.GetLorentzian(velocity, intensity, n=6, neg=True, baseline=None)
Returns the widths in units of velocity values of the peaks found. The number of peaks needs to be specified.
Added in version 1.2.0.
- Parameters:
velocity (list) – velocity data.
intensity (list) – intensity data.
n (int) – number of peaks to find.
neg (bool) – If “True”, negative peaks are searched for. Default is “True”.
- Returns:
number of peaks, indices of peaks, velocity values of peaks, intensity values of peaks, width (FWHM) values in units of the velocity, area values, baseline of fit, fitted spectrum.
- Return type:
int, ndarry, ndarray, ndarray,ndarray, ndarray, float, 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.
- Returns:
x_data, intensity_data.
- Return type:
ndarray, ndarray
- nexus.data.LoadFile(file_name, skip_rows=2)
Load a file with data saved in a 1D or 2D format and outputs to a 1D data set.
Added in version 1.2.0.
- Parameters:
file_name (string) – mos file to load (with relative path).
skip_rows (int) – number of rows to skip. Default is 2.
- Returns:
1D data
- Return type:
ndarray
- nexus.data.Normalize(data, method='baseline', value=0, left_point=0, right_point=0, poly_order=0)
Returns the normalized data.
Added in version 1.2.0.
- Parameters:
data (list) – Data to be normalized.
method (string) –
Can be
”value”: Normalize by given value.
”max”: Normalize by maximum of data.
”min”: Normalize by minimum of data.
”sum”: Normalize by sum of all data points.
”mean”: Normalize by mean of all data points.
”baseline”: Normalize by a polynominal baseline fit.
value (float) – Initial guess for shift of the spectrum in mm/s. Optional, default is zero.
left_point (int) – Index of the data array. Data from zero to this index are used for the baseline fit. If zero it is automatically selected.
right_point (int) – Index of the data array. Data from this point to the end of the array are used for the baseline fit. If zero it is automatically selected.
poly_order (int) – Polynominal order for fitting the baseline.
0
for flat baseline and1
for tilted line correction. Default is0
.
- Returns:
normalized intensity, normalization factor
- Return type:
ndarray, float
- 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)