Spectrum

class brixs.Spectrum(x=None, y=None, filepath=None, **kwargs)

Returns a Spectrum object.

Parameters:
  • x (list or array, optional) – x values (1D list/array).

  • y (list or array, optional) – y values (1D list/array).

  • filepath (str or Path, optional) – filepath.

  • **kwargs – kwargs are passed to Spectrum.load() function

Usage:
>>> s = br.Spectrum()
>>> s = br.Spectrum(x, y)
>>> s = br.Spectrum(x=x, y=y)
>>> s = br.Spectrum(y=y)
>>> s = br.Spectrum(filepath=<filepath>)
>>> s = br.Spectrum(filepath=<filepath>, delimiter=',')
>>>
>>> print(s.get_core_attrs()) # print list of core attrs
>>> print(s.get_attrs())      # print list of attrs
>>> print(s.get_methods())    # print list of methods available
get_core_attrs()

return a list of core attrs

get_attrs()

return a list of user defined attrs

get_methods()

return a list of methods available

remove_attrs()

Delete all user defined attrs.

copy_attrs_from(s)

Copy user defined attributes from another brixs object.

Parameters:

s (brixs object) – Either a Spectrum, Spectra, Image, or PhotonEvents to copy user defined attributes from.

Returns:

None

copy(limits=None)

Return a copy of the data within a range limits.

Usage:
>>> # full copy
>>> s2 = s1.copy()  # s2 is now a copy of s2
>>>
>>> # s2 will have only data between x=0 and 10 and between x=90 and 100
>>> s2 = s1.copy(limits=((0, 10), (90, 100)))
Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

Spectrum

save(filepath, only_data=False, check_overwrite=False, verbose=False, **kwargs)

Save data to a text file. Wrapper for numpy.savetxt().

Warning

Attrs are saved as comments if only_data is False. Saving attrs to file is not always reliable because requires converting variables to string. Only attrs that are of type: string, number, and list of number,

list of list of number and strings have been tested. Dictionaries are not saved.

Parameters:
  • filepath (string or path object, optional) – filepath or file handle. If the filename ends in .gz, the file is automatically saved in compressed gzip format.

  • only_data (bool, optional) – If True, header and footer are ignored and only data is saved to the file.

  • check_overwrite (bool, optional) – if True, it will check if file exists and ask if user wants to overwrite file.

  • verbose (bool, optional) – turn verbose on and off. Default is False.

  • *kwargs – kwargs are passed to np.savetxt() that saves the data.

If not specified, the following parameters are passed to numpy.savetxt():

Parameters:
  • fmt (string, or list, optional) – A single format (like %10.5f), or a sequence of formats. See numpy’s documentation for more information. If not specified, best fmt is calculated based on the number of decimal places of the data. Specifying fmt makes the code runs a little faster (not much tough, but it might make a difference if saving a lot of files).

  • delimiter (str, optional) – String or character separating columns. Use \\t for tab. Default is comma (”, “).

  • newline (str, optional) – String or character separating lines. Default is \n.

  • header (bool, optional) – String that will be written at the beginning of the file. Note that, object attributes will be saved at the beginning of the file. If only_data=True, header and footer is ignored.

  • comments (str, optional) – String that will be prepended to the header and footer strings, to mark them as comments. Default is “# “.

Returns:

None

See also

Spectrum.load()

load(filepath, only_data=False, verbose=False, **kwargs)

Load data from a text file. Wrapper for numpy.genfromtxt().

Warning

This a very simple loading function that works well with column text files (xy files). If file has more columns than two columns, the first two columns will be loaded by default. Use usecols to select columns with x and y data.

Note

If file was saved by br.Spectrum.save(), then the metadata (comments) can be recovered. If not, one might get better results by setting only_data = True.

Parameters:
  • filepath (string or path object, optional) – filepath or file handle. If the filename extension is .gz or .bz2, the file is first decompressed. Last used filepath is saved to an attr s.filepath.

  • only_data (bool, optional) – If True, header and footer are ignored and only data is loaded. Default is False.

  • verbose (book, optional) – Default is False. If True, it will print an warning when attributes cannot be loaded from the file.

  • **kwargs – kwargs are passed to numpy.genfromtxt() that loads the data.

If not specified, the following parameters are passed to numpy.genfromtxt():

Parameters:
  • delimiter (str, optional) – String or character separating columns. Use \t for tab. Default is comma (’, ‘).

  • comments (str, optional) – The character used to indicate the start of a comment. Default is #. Attributes picked up from the header will be loaded too.

  • usecols (tuple, optional) – Default is (0, 1).

Returns:

None

See also

Spectrum.save()

check_step(max_error=0.1)

Checks vector uniformity of the x-coordinates.

If the step between two data points is the same through out the x vector, it sets s.step with the value of the step size.

Parameters:
  • max_error (number, optional) – percentage (of the x step) value

  • % (of the max error. Default is 0.1)

Note

Step uniformity is verified by the following equation:

(max(steps) - min(steps))/np.mean(steps) * 100 < max_error

Returns:

None

Raises:

ValueError – If x-coordinates are not uniform.

check_monotonicity()

Sets monotonicity attribute to ‘increasing’ or ‘decreasing’.

Raises:

ValueError if data is not monotonic.

Returns:

None

fix_monotonicity(mode='increasing')

Rearrange (x, y) such as x array is monotonically increasing or decreasing.

Parameters:

mode (str, optional) – increasing or decreasing.

Note

duplicated datapoints are averaged.

Returns:

None

set_shift(value)

Add value to x-coordinates.

Parameters:

value (float or int) – shift value.

Returns:

Spectrum

set_offset(value)

Add value to y-coordinates.

Parameters:

value (value) – offset value.

Returns:

Spectrum

set_factor(value)

Multiply y-coordinates by value.

Parameters:

value (number) – multiplicative factor

Raises:

AttributeError – if value is 0.

Returns:

Spectrum

set_calib(value)

Multiply x-coordinates by value.

Parameters:

value (number) – calibration value

Raises:

AttributeError – if value is 0.

Returns:

Spectrum

set_x_via_polyval(p)

Set x to np.polyval(p, x).

Parameters:

p (array) – 1D array of polynomial coefficients (including coefficients equal to zero) from highest degree to the constant term.

Returns:

Spectrum

set_x_via_function(f)

Set x to f(x).

Parameters:

f (function) – function where argument is x-coordinate elements

Returns:

Spectrum

set_roll(value)

Roll array elements for the x-coordinates

Note

Elements that roll beyond the last position are re-introduced at the

first. y-coordinates are fully preserved.

Parameters:

value (float or int) – roll value.

Returns:

Spectrum

flip_x()

Flip sign of x axis.

Returns:

Spectrum

flip_y()

Flip sign of y axis.

Returns:

Spectrum

floor(limits=None)

Sets zero value for y-coordinates.

Usage:
>>> # brings the avg of all data points to zero.
>>> s.floor()
>>>
>>> # Brings the avg between x=0 and 10 and between x=90 and 100 to zero
>>> s.floor(limits=((0, 10), (90, 100)))
Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

Spectrum

normalize(value=1, limits=None)

Set a factor such as the average y between limits is equal to value.

Parameters:
  • value (number) – value. Default is 1.

  • limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

Spectrum

interp(start=None, stop=None, num=None, step=None, x=None)

Interpolate data.

Parameters:
  • start (number, optional) – The starting value of the sequence. If None, the minium x value will be used.

  • stop (number, optional) – The end value of the sequence. If None, the maximum x value will be used.

  • num (int, optional) – Number of samples to generate.

  • step (number, optional) – Spacing between values. This overwrites num.

  • x (list or array, optional) – The x-coordinates at which to evaluate the interpolated values. This overwrites all other arguments.

None:

If none arguments is given, this function will adjust x to have regular spacing.

Returns:

Spectrum

derivative(order=1)

Returns the derivative of y-coordinates as a function of x-coordinates.

Parameters:

order (number, optional) – derivative order. Default is 1.

Returns:

Spectrum

moving_average(n)

Returns the moving average of the data.

Parameters:

n (int) – number of points to average.

Returns:

Spectrum of length given by (len(x)-n+1)

smooth(n, force_divisible=False)

Returns Spectrum which is the average over every n elements

Parameters:
  • n (int) – number of points to average.

  • force_divisible (bool, optional) – if True, the length of the data must be divisible by n. Default is False.

Warning

If force_divisible=False, the last data point might be averaged by less points then n.

Returns:

Spectrum of length given by (len(x)/n).

crop(start=None, stop=None)

Crop edges of the dataset.

Parameters:
  • start (number, optional) – start x value. If None, the minimum value of x will be used.

  • stop (number, optional) – final x value. If None, the maximum value of x will be used.

Returns:

Spectrum

switch_xy()

Switch x and y axis.

Returns:

Spectrum

remove(limits)

Remove datapoints within a range limits.

Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

Spectrum

calculate_area(limits=None)

Returns the calculated area under the curve. Wrapper for numpy.trapz().

Usage:
>>> s.calculate_area()                # returns the area for the whole dataset
>>> s.calculate_area(limits=(0, 10))  # returns the area between x=0 and 10

Warning

Calculate_area for broken limits, i.e., s.calculate_area(limits=((0, 10), (90, 100)))

works, but will

typically not return a desirable value, because the trapz() algorithm will consider the area between 10 and 90 as a rectangle. The correct approach in this case would be to calculated the area between 0 and 10 and between 90 and 100 separately.

>>> area = s.calculate_area(0, 10) + s.calculate_area(90, 100)

this behavior might change in the future.

Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

number

calculate_x_sum(limits=None)

Returns sum of x elements within a range.

Usage:
>>> s.calculate_x_sum()  # returns the x sum for the whole dataset
>>> s.calculate_x_sum((0, 10), (90, 100))  # returns the x sum from data between x=0 and 10 and between x=90 and 100
Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

number

calculate_y_sum(limits=None)

Returns sum of y elements within a range.

Usage:
>>> s.calculate_y_sum()  # returns the y sum for the whole dataset
>>> s.calculate_y_sum((0, 10), (90, 100))  # returns the y sum from data between x=0 and 10 and between x=90 and 100
Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

number

calculate_x_average(limits=None)

Returns the average value of x elements within a range.

Usage:
>>> s.calculate_x_average()  # returns the y average for the whole dataset
>>> s.calculate_x_average((0, 10), (90, 100))  # returns the y average from data between x=0 and 10 and between x=90 and 100
Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

number

calculate_y_average(limits=None)

Returns the average value of y elements within a range.

Usage:
>>> s.calculate_y_average()  # returns the y average for the whole dataset
>>> s.calculate_y_average((0, 10), (90, 100))  # returns the y average from data between x=0 and 10 and between x=90 and 100
Parameters:

limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

number

calculate_calib(values, mode='peaks', deg=1, limits=None, **kwargs)

return a calibration factor (spectral feature vs value).

Note

The calibration factor is the shift value (x-coord) as a function of

the values array. Assuming each spectral feature (i.e., peak) was collected based on a certain experimental condition (i.e, with a photon energy E), than one can find a calibration factor to multiply the x axis so that the x-coordinates reflect this experimental condition.

Parameters:
  • values (list) – value list for each spectral feature

  • mode (string, optional) –

    method used. Options are:

    1) ‘peaks’: Fit multiple peaks and calculate the distance between them (requires that brixs.addons.fitting is imported)

  • deg (int, optional) – a polynomial degree order used to fit the curve shift vs value.

  • limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

  • **kwargs (dict) – kwargs to be passed to ss.fit_peak() function when mode=’peak’

Returns:

Spectrum -> s

s.x = values s.y = calculated shifts s.popt = polynomial coeff. (highest power first.) that fit the calibration curve. s.model = function f(x) of the calibration curve. s.R2 = R2 factor of the fitting.

polyfit(deg, limits=None)

Fit data with a polynomial. Wrapper for numpy.polyfit().

Usage:
>>> fit, popt, R2, f = polyfit(deg)
Parameters:
  • deg (int) – degree of the fitting polynomial.

  • limits (None or list) – a pair of values (x_start, x_stop), a list of pairs ((xi_1, xf_1), (xi_2, xf_2), …), or None. If None, this function simply returns None. If pairs, each pair represents the start and stop of a data range from x. Limits are inclusive. Use x_start = None or x_stop = None to indicate the minimum or maximum x value of the data, respectively. If limits = [], i.e., an empty list, it assumes limits = (None, None).

Returns:

fit (spectrum), popt, R2, f(x)

fit (spectrum): polynomial fit spectrum with 100x more intepolated points

popt (np.array): 1D array of polynomial coefficients

(including coefficients equal to zero) from highest degree to the constant term.

R2 (number): R2 error

model (function): funcion f(x_centers)

index(x, closest=True)

Return the index value for a given x.

Parameters:
  • x (number) – x value.

  • closest (bool, optional) – if True, x does not have to be exact and the index which is closest to the x value is returned.

Returns:

int

x2y(x, closest=True)

Return the y value associated with a given x.

Parameters:
  • x (number) – x value.

  • closest (bool, optional) – if True, x does not have to be exact and the index which is closest to the x value is returned.

Returns:

y number