Wasatch.PY
Python application driver for Wasatch Photonics spectrometers
Loading...
Searching...
No Matches
wasatch.utils Namespace Reference

Functions

 all_same (a)
 
 apply_boxcar (a, half_width)
 apply a boxcar convolution of the given half_width to input array 'a'
 
 area_under_peak (spectrum, x_axis, x_index, boxcar_half_width=0)
 integrate the 'area under the curve' for the given spectrum and x_axis, using the peak centered on x_axis, with optional smoothing given the boxcar width.
 
 ascii_spectrum (spectrum, rows, cols, x_axis, x_unit)
 render a spectrum as ASCII-art
 
 check_admin ()
 
 clamp_to_int16 (n)
 
 clean_nan (a)
 
 coeffs_look_valid (coeffs, count=None)
 Can be used as a sanity-check for any set of coefficients.
 
 dict_get_norm (d, keys)
 Similar to dict.get(), but case-insensitive and normalizes-out spaces, underscores, periods and hyphens.
 
 dict_get_path (d, keys)
 Similar to dict.get(), but takes a list of keys to be traversed in sequence.
 
 dump (foo, indent=0)
 similar to Perl's Data.Dumper
 
 find_local_maxima (a, x_axis, center, tolerance=0)
 Given a spectrum (array 'a'), with an x_axis, a 'center' along that x_axis, and an allowed 'tolerance' (in same units as the x_axis), find the local maxima within 'tolerance' of 'center'.
 
 find_nearest_index (L, value)
 
 find_nearest_value (L, value)
 
 find_peak_feet_indices (spectrum, x_axis, x_index, boxcar_half_width=0)
 Given a spectrum and an x_axis, find the indexes of the left and right 'feet' of the peak centered on x_index.
 
 generate_excitation (wavelengths, wavenumbers)
 If we've loaded a CSV that had wavelength and wavenumber columns, but no metadata, use this to infer the excitation wavelength.
 
 generate_wavelengths (pixels, coeffs)
 expand 3rd-order wavelength polynomial into array of wavelengths
 
 generate_wavelengths_from_wavenumbers (excitation, wavenumbers)
 
 generate_wavenumbers (excitation, wavelengths, wavenumber_correction=0)
 convert wavelengths into Raman shifts in 1/cm wavenumbers from the given excitation wavelength
 
 get_pathnames_from_directory (rootdir, pattern=None, recursive=False)
 iterate down a directory, returning pathnames that match the given pattern
 
 interpolate_array (spectrum, old_axis, new_axis)
 Interpolate the passed spectrum over a fixed x-axis (e.g.
 
 interpolate_value (spectrum, old_axis, x)
 I might be making this more difficult than it needs to be.
 
 load_json (pathname)
 convenience wrapper to load a JSON file
 
 parabolic_approximation (pixel, x, y)
 Given an array of doubles and a peak index, use the peak and its two neighbors to form a parabola and return the interpolated maximum height of the parabola.
 
 peak_height_above_background (spectrum, x_axis, x_index, boxcar_half_width=0)
 
 pixel_to_wavelength (int x, list[float] coeffs)
 
 remove_unicode (s)
 convert unicode string to ascii
 
 resize_file (path, nbytes, ensure_no_overwrite=False)
 
 stomp_first (a, count)
 "Stomps" the first "count" elements with the first non-stomped value.
 
 stomp_last (a, count)
 "stomps" the last "count" elements with the last non-stomped value
 
 timestamp ()
 
 to_bool (value)
 
 truthy (flag)
 
 twos_complement (val, bits)
 
 uint16_to_little_endian (values)
 
 update_obj_from_dict (obj, d)
 given a destination object and a source dictionary, update any existing attributes of the destination object from like-named keys in the source dictionary
 
 vercmp (a, b, delim=".")
 
 wavelength_to_wavenumber (wavelength, excitation)
 convert a single wavelength to wavenumber
 
 wavenumber_to_wavelength (excitation, wavenumber)
 convert a single (uncorrected) wavenumber to wavelength
 

Variables

 log = logging.getLogger(__name__)
 

Function Documentation

◆ all_same()

wasatch.utils.all_same ( a)

◆ apply_boxcar()

wasatch.utils.apply_boxcar ( a,
half_width )

apply a boxcar convolution of the given half_width to input array 'a'

◆ area_under_peak()

wasatch.utils.area_under_peak ( spectrum,
x_axis,
x_index,
boxcar_half_width = 0 )

integrate the 'area under the curve' for the given spectrum and x_axis, using the peak centered on x_axis, with optional smoothing given the boxcar width.

◆ ascii_spectrum()

wasatch.utils.ascii_spectrum ( spectrum,
rows,
cols,
x_axis,
x_unit )

render a spectrum as ASCII-art

◆ check_admin()

wasatch.utils.check_admin ( )

◆ clamp_to_int16()

wasatch.utils.clamp_to_int16 ( n)

◆ clean_nan()

wasatch.utils.clean_nan ( a)

◆ coeffs_look_valid()

wasatch.utils.coeffs_look_valid ( coeffs,
count = None )

Can be used as a sanity-check for any set of coefficients.

Checks that coeffs:

  • are not None
  • have no NaN
  • are not all the same (zeros, -1 etc)
  • are not [0, 1, 0, 0]
  • checks count if provided

◆ dict_get_norm()

wasatch.utils.dict_get_norm ( d,
keys )

Similar to dict.get(), but case-insensitive and normalizes-out spaces, underscores, periods and hyphens.

Parameters
d(input) dictionary
k(input) case-insensitive key (can be prioritized list)

Note that this function does not distiguish between the dictionary not having a key, and the value of the key being None.

◆ dict_get_path()

wasatch.utils.dict_get_path ( d,
keys )

Similar to dict.get(), but takes a list of keys to be traversed in sequence.

Parameters
d(input) dictionary
keys(input) list of case-insensitive keys

◆ dump()

wasatch.utils.dump ( foo,
indent = 0 )

similar to Perl's Data.Dumper

◆ find_local_maxima()

wasatch.utils.find_local_maxima ( a,
x_axis,
center,
tolerance = 0 )

Given a spectrum (array 'a'), with an x_axis, a 'center' along that x_axis, and an allowed 'tolerance' (in same units as the x_axis), find the local maxima within 'tolerance' of 'center'.

Note
probably a numpy shortcut for this

◆ find_nearest_index()

wasatch.utils.find_nearest_index ( L,
value )

◆ find_nearest_value()

wasatch.utils.find_nearest_value ( L,
value )

◆ find_peak_feet_indices()

wasatch.utils.find_peak_feet_indices ( spectrum,
x_axis,
x_index,
boxcar_half_width = 0 )

Given a spectrum and an x_axis, find the indexes of the left and right 'feet' of the peak centered on x_index.

Internally apply then given boxcar for added smoothing.

◆ generate_excitation()

wasatch.utils.generate_excitation ( wavelengths,
wavenumbers )

If we've loaded a CSV that had wavelength and wavenumber columns, but no metadata, use this to infer the excitation wavelength.

Useful for interpolation.

◆ generate_wavelengths()

wasatch.utils.generate_wavelengths ( pixels,
coeffs )

expand 3rd-order wavelength polynomial into array of wavelengths

◆ generate_wavelengths_from_wavenumbers()

wasatch.utils.generate_wavelengths_from_wavenumbers ( excitation,
wavenumbers )

◆ generate_wavenumbers()

wasatch.utils.generate_wavenumbers ( excitation,
wavelengths,
wavenumber_correction = 0 )

convert wavelengths into Raman shifts in 1/cm wavenumbers from the given excitation wavelength

◆ get_pathnames_from_directory()

wasatch.utils.get_pathnames_from_directory ( rootdir,
pattern = None,
recursive = False )

iterate down a directory, returning pathnames that match the given pattern

◆ interpolate_array()

wasatch.utils.interpolate_array ( spectrum,
old_axis,
new_axis )

Interpolate the passed spectrum over a fixed x-axis (e.g.

integral wavelengths or wavenumbers).

◆ interpolate_value()

wasatch.utils.interpolate_value ( spectrum,
old_axis,
x )

I might be making this more difficult than it needs to be.

◆ load_json()

wasatch.utils.load_json ( pathname)

convenience wrapper to load a JSON file

◆ parabolic_approximation()

wasatch.utils.parabolic_approximation ( pixel,
x,
y )

Given an array of doubles and a peak index, use the peak and its two neighbors to form a parabola and return the interpolated maximum height of the parabola.

"pixel" is ideally the array index of the pinnacle of a previously- identified peak within the spectrum, although though this will technically generate a parabola through any pixel and its two neighbors.

Parameters
pixelindex of a point on the spectrum
xx-axis (wavelengths or wavenumbers)
yy-axis (intensity)
See also
https://stackoverflow.com/a/717833
Returns
a point representing the interpolated vertex of a parabola drawn through the specified pixel and its two neighbors (in x-axis space)

◆ peak_height_above_background()

wasatch.utils.peak_height_above_background ( spectrum,
x_axis,
x_index,
boxcar_half_width = 0 )

◆ pixel_to_wavelength()

wasatch.utils.pixel_to_wavelength ( int x,
list[float] coeffs )

◆ remove_unicode()

wasatch.utils.remove_unicode ( s)

convert unicode string to ascii

◆ resize_file()

wasatch.utils.resize_file ( path,
nbytes,
ensure_no_overwrite = False )
Keep only the last `nbytes` of a text file specified by `path`.

This function is encoding (utf-8, utf-16, ascii) agnostic because it uses the line-
continuation character '\n' as a boundary. It is system independent because it slices
before that character. This was written to keep our default log file within a reasonable
filesize, suitable for emailing.

Call this on a file that is not yet opened. Expect the file to be closed and flushed
by the time the function returns.

returns True if the file was changed.

◆ stomp_first()

wasatch.utils.stomp_first ( a,
count )

"Stomps" the first "count" elements with the first non-stomped value.

Parameters
a(Input) array to modify
count(Input) HOW MANY leading elements to stomp, so the index of the first GOOD pixel should be one more than this

◆ stomp_last()

wasatch.utils.stomp_last ( a,
count )

"stomps" the last "count" elements with the last non-stomped value

◆ timestamp()

wasatch.utils.timestamp ( )

◆ to_bool()

wasatch.utils.to_bool ( value)

◆ truthy()

wasatch.utils.truthy ( flag)

◆ twos_complement()

wasatch.utils.twos_complement ( val,
bits )

◆ uint16_to_little_endian()

wasatch.utils.uint16_to_little_endian ( values)

◆ update_obj_from_dict()

wasatch.utils.update_obj_from_dict ( obj,
d )

given a destination object and a source dictionary, update any existing attributes of the destination object from like-named keys in the source dictionary

Parameters
obj(In/Out) the object whose attributes to update
d(Input) the dictionary whose keys should be treated as attributes

◆ vercmp()

wasatch.utils.vercmp ( a,
b,
delim = "." )
 vercmp("1.2.3.4", "1.2.4.3") -> -1 

◆ wavelength_to_wavenumber()

wasatch.utils.wavelength_to_wavenumber ( wavelength,
excitation )

convert a single wavelength to wavenumber

◆ wavenumber_to_wavelength()

wasatch.utils.wavenumber_to_wavelength ( excitation,
wavenumber )

convert a single (uncorrected) wavenumber to wavelength

Variable Documentation

◆ log

wasatch.utils.log = logging.getLogger(__name__)