WrightTools.kit module

A collection of small, general purpose objects and methods.

class WrightTools.kit.FileSlicer(path, skip_headers=True, header_charachter='#')[source]

Bases: object

Access groups of lines from a file quickly, without loading the entire file into memory.

Mostly a convinient wrapper around the standard library linecache module.

__init__(path, skip_headers=True, header_charachter='#')[source]

Create a FileSlicer object.

Parameters:
  • path (string) – Path to the file.
  • skip_headers (bool (optional)) – Toggle to skip headers at beginning of file. Default is True.
  • header_charachter (string (optional)) – Charachter that appears at the beginning of header lines for this file. Default is ‘#’.
close()[source]

Clear the cache, attempting to free as much memory as possible.

get(line_count)[source]

Get the next group of lines from the file.

Parameters:line_count (int) – The number of lines to read from the file.
Returns:List of lines as strings.
Return type:list
skip(line_count)[source]

Skip the next group of lines from the file.

Parameters:line_count (int) – The number of lines to skip.
class WrightTools.kit.INI(filepath)[source]

Bases: object

Handle communication with an INI file.

__init__(filepath)[source]

Create an INI handler object.

Parameters:filepath (string) – Filepath.
add_section(section)[source]

Add section.

Parameters:section (string) – Section to add.
clear()[source]

Remove all contents from file. Use with extreme caution.

Warning

This is a destructive action.

dictionary

Get a python dictionary of contents.

get_options(section)[source]

List the options in a section.

Parameters:section (string) – The section to investigate.
Returns:The options within the given section.
Return type:list of strings
has_option(section, option)[source]

Test if file has option.

Parameters:
Returns:

Return type:

boolean

has_section(section)[source]

Test if file has section.

Parameters:section (string) – Section.
Returns:
Return type:boolean
read(section, option)[source]

Read from file.

Parameters:
Returns:

Value.

Return type:

string

sections

List of sections.

write(section, option, value)[source]

Write to file.

Parameters:
class WrightTools.kit.Spline(xi, yi, k=3, s=1000, ignore_nans=True)[source]

Bases: object

Spline.

__init__(xi, yi, k=3, s=1000, ignore_nans=True)[source]

Initialize.

Parameters:
  • xi (1D array) – x points.
  • yi (1D array) – y points.
  • k (integer (optional)) – Degree of smoothing. Must be between 1 and 5 (inclusive). Default is 3.
  • s (integer (optional)) –

    Positive smoothing factor used to choose the number of knots. Number of knots will be increased until the smoothing condition is satisfied:

    ``sum((w[i] * (y[i]-spl(x[i])))**2, axis=0) <= s``
    

    If 0, spline will interpolate through all data points. Default is 1000.

  • ignore_nans (boolean (optional)) – Toggle removle of nans. Default is True.

Note

Use k=1 and s=0 for a linear interplation.

class WrightTools.kit.TimeStamp(at=None, timezone='local')[source]

Bases: object

Class for representing a moment in time.

RFC3339

RFC3339.

RFC5322

RFC5322.

__init__(at=None, timezone='local')[source]

Create a TimeStamp object.

Parameters:
  • at (float (optional)) – Seconds since epoch (unix time). If None, current time will be used. Default is None.
  • timezone (string or integer (optional)) – String (one in {‘local’, ‘utc’} or seconds offset from UTC. Default is local.
unix

float – Seconds since epoch (unix time).

date

string – Date.

hms

string – Hours, minutes, seconds.

human

string – Representation of the timestamp meant to be human readable.

legacy

string – Legacy WrightTools timestamp representation.

RFC3339

stringRFC3339 representation (recommended for most applications).

RFC5322

stringRFC5322 representation.

path

string – Representation of the timestamp meant for inclusion in filepaths.

date

year-month-day.

hms

Get time formated.

HH:MM:SS

human

Human-readable timestamp.

legacy

Legacy timestamp format.

path

Timestamp for placing into filepaths.

class WrightTools.kit.Timer(verbose=True)[source]

Bases: object

Context manager for timing code.

>>> with Timer():
...     your_code()
__init__(verbose=True)[source]

init.

WrightTools.kit.array2string(array, sep='\t')[source]

Generate a string from an array with useful formatting.

Great for writing arrays into single lines in files.

See also

string2array()

WrightTools.kit.closest_pair(arr, give='indicies')[source]

Find the pair of indices corresponding to the closest elements in an array.

If multiple pairs are equally close, both pairs of indicies are returned. Optionally returns the closest distance itself.

I am sure that this could be written as a cheaper operation. I wrote this as a quick and dirty method because I need it now to use on some relatively small arrays. Feel free to refactor if you need this operation done as fast as possible. - Blaise 2016.02.07

Parameters:
  • arr (numpy.ndarray) – The array to search.
  • give ({'indicies', 'distance'} (optional)) – Toggle return behavior. If ‘distance’, returns a single float - the closest distance itself. Default is indicies.
Returns:

List containing lists of two tuples: indicies the nearest pair in the array.

>>> arr = np.array([0, 1, 2, 3, 3, 4, 5, 6, 1])
>>> closest_pair(arr)
[[(1,), (8,)], [(3,), (4,)]]

Return type:

list of lists of two tuples

WrightTools.kit.diff(xi, yi, order=1)[source]

Take the numerical derivative of a 1D array.

Output is mapped onto the original coordinates using linear interpolation.

Parameters:
  • xi (1D array-like) – Coordinates.
  • yi (1D array-like) – Values.
  • order (positive integer (optional)) – Order of differentiation.
Returns:

Numerical derivative. Has the same shape as the input arrays.

Return type:

1D numpy array

WrightTools.kit.fft(xi, yi, axis=0)[source]

Take the 1D FFT of an N-dimensional array and return “sensible” properly shifted arrays.

Parameters:
  • xi (numpy.ndarray) – 1D array over which the points to be FFT’ed are defined
  • yi (numpy.ndarray) – ND array with values to FFT
  • axis (int) – axis of yi to perform FFT over
Returns:

  • xi (1D numpy.ndarray) – 1D array. Conjugate to input xi. Example: if input xi is in the time domain, output xi is in frequency domain.
  • yi (ND numpy.ndarray) – FFT. Has the same shape as the input array (yi).

WrightTools.kit.file_len(fname)[source]

Cheaply get the number of lines in a file.

File is not entirely loaded into memory at once.

WrightTools.kit.filename_parse(fstr)[source]

Parse a filepath string into it’s path, name, and suffix.

WrightTools.kit.flatten_list(items, seqtypes=(<class 'list'>, <class 'tuple'>), in_place=True)[source]

Flatten an irregular sequence.

Works generally but may be slower than it could be if you can make assumptions about your list.

Source

Parameters:
  • items (iterable) – The irregular sequence to flatten.
  • seqtypes (iterable of types (optional)) – Types to flatten. Default is (list, tuple).
  • in_place (boolean (optional)) – Toggle in_place flattening. Default is True.
Returns:

Flattened list.

Return type:

list

Examples

>>> l = [[[1, 2, 3], [4, 5]], 6]
>>> wt.kit.flatten_list(l)
[1, 2, 3, 4, 5, 6]
WrightTools.kit.get_index(lis, argument)[source]

Find the index of an item, given either the item or index as an argument.

Particularly useful as a wrapper for arguments like channel or axis.

Parameters:
  • lis (list) – List to parse.
  • argument (int or object) – Argument.
Returns:

Index of chosen object.

Return type:

int

WrightTools.kit.get_methods(the_class, class_only=False, instance_only=False, exclude_internal=True)[source]

Get a list of strings corresponding to the names of the methods of an object.

WrightTools.kit.get_path_matching(name)[source]

Get path matching a name.

Parameters:name (string) – Name to search for.
Returns:Full filepath.
Return type:string
WrightTools.kit.get_timestamp(style='RFC3339', at=None, hms=True, frac=False, timezone='here', filename_compatible=False)[source]

Get the current time as a string.

Parameters:
  • style ({'RFC3339', 'short', 'display', 'legacy'} (optional)) – The format of the returned string. legacy is the old WrightTools format. Default is RFC3339. All other arguments control RFC3339 behavior.
  • at (local seconds since epoch (optional)) – Time at-which to generate timestamp. If None, use current time. Default is None. Use time.time() to get seconds.
  • hms (bool (optional)) – Toggle inclusion of current time (hours:minutes:seconds) in returned string. Default is True. Does not effect legacy timestamp.
  • frac (bool (optional)) – Toggle inclusion of fractional seconds in returned string. Default is False. Does not effect legacy timestamp. Only appears if hms is present.
  • timezone ({'here', 'utc'}) – Timezone. Default is here.
  • filename_compatible (bool) – Remove special charachters. Default is False.
WrightTools.kit.glob_handler(extension, folder=None, identifier=None)[source]

Return a list of all files matching specified inputs.

Parameters:
WrightTools.kit.intersperse(lst, item)[source]

Put item between each existing item in list.

Source

WrightTools.kit.item2string(item, sep='\t')[source]

Generate string from item.

Parameters:
  • item (object) – Item.
  • sep (string (optional)) – Separator. Default is ‘t’.
Returns:

Return type:

string

See also

string2item()

WrightTools.kit.mono_resolution(grooves_per_mm, slit_width, focal_length, output_color, output_units='wn')[source]

Calculate the resolution of a monochromator.

Parameters:
  • grooves_per_mm (number) – Grooves per millimeter.
  • slit_width (number) – Slit width in microns.
  • focal_length (number) – Focal length in mm.
  • output_color (number) – Output color in nm.
  • output_units (string (optional)) – Output units. Default is wn.
Returns:

Resolution.

Return type:

float

WrightTools.kit.nm_width(center, width, units='wn')[source]

Given a center and width, in energy units, get back a width in nm.

Parameters:
  • center (number) – Center (in energy units).
  • width (number) – Width (in energy units).
  • units (string (optional)) – Input units. Default is wn.
Returns:

Width in nm.

Return type:

number

WrightTools.kit.parse_identity(string)[source]

Parse an identity string into its components.

Returns:(names, operators)
Return type:tuple of lists
WrightTools.kit.read_data_column(path, name)[source]

Read a named column of a PyCMDS data file as a single array.

Parameters:
  • path (string) – Path of PyCMDS data file.
  • name (string) – Name of column to read.
Returns:

The column of the data file, as an array.

Return type:

1D numpy.ndarray

WrightTools.kit.read_h5(filepath)[source]

Read from a HDF5 file, returning the data within as a python dictionary.

Returns:Dictionary containing data from HDF5 file.
Return type:OrderedDict

See also

kit.write_h5()

WrightTools.kit.read_headers(filepath)[source]

Read ‘WrightTools formatted’ headers from given path.

Parameters:filepath (str) – Path of file.
Returns:Dictionary containing header information.
Return type:OrderedDict

See also

kit.write_headers()

WrightTools.kit.remove_nans_1D(arrs)[source]

Remove nans in a list of 1D arrays.

Removes indicies in all arrays if any array is nan at that index. All input arrays must have the same size.

Parameters:arrs (list of 1D arrays) – The arrays to remove nans from
Returns:List of 1D arrays in same order as given, with nan indicies removed.
Return type:list
WrightTools.kit.share_nans(arrs1)[source]

Take a list of nD arrays and return a new list of nD arrays.

The new list is in the same order as the old list. If one indexed element in an old array is nan then every element for that index in all new arrays in the list is then nan.

Parameters:arrs1 (list of nD arrays) – The arrays to syncronize nans from
Returns:List of nD arrays in same order as given, with nan indicies syncronized.
Return type:list
WrightTools.kit.smooth_1D(arr, n=10)[source]

Smooth 1D data by ‘running average’.

Parameters:n (int) – number of points to average
WrightTools.kit.string2array(string, sep='\t')[source]

Generate an array from a string created using array2string.

See also

array2string()

WrightTools.kit.string2identifier(s)[source]

Turn a string into a valid python identifier.

Parameters:s (string) – string to convert
Returns:valid python identifier.
Return type:str
WrightTools.kit.string2item(string, sep='\t')[source]

Turn a string into a python object.

Parameters:
  • string (string) – String.
  • sep (string (optional)) – Seperator. Default is ‘t’.
Returns:

Return type:

object

See also

item2string()

class WrightTools.kit.suppress_stdout_stderr[source]

Bases: object

Context manager for doing a “deep suppression” of stdout and stderr in Python.

i.e. will suppress all print, even if the print originates in a compiled C/Fortran sub-function.

This will not suppress raised exceptions, since exceptions are printed to stderr just before a script exits, and after the context manager has exited (at least, I think that is why it lets exceptions through).

Source

>>> with wt.kit.suppress_stdout_stderr():
...     rogue_function()
__init__()[source]

init.

WrightTools.kit.symmetric_sqrt(x, out=None)[source]

Compute the ‘symmetric’ square root: sign(x) * sqrt(abs(x)).

Parameters:
  • x (array_like or number) – Input array.
  • out (ndarray, None, or tuple of ndarray and None (optional)) – A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
Returns:

Symmetric square root of arr.

Return type:

np.ndarray

WrightTools.kit.timestamp_from_RFC3339(RFC3339)[source]

Generate a Timestamp object from a RFC3339 formatted string.

Parameters:RFC3339 (string) – RFC3339 formatted string.
Returns:
Return type:WrightTools.kit.TimeStamp
WrightTools.kit.unique(arr, tolerance=1e-06)[source]

Return unique elements in 1D array, within tolerance.

Parameters:
  • arr (array_like) – Input array. This will be flattened if it is not already 1D.
  • tolerance (number (optional)) – The tolerance for uniqueness.
Returns:

The sorted unique values.

Return type:

array

WrightTools.kit.update_progress(progress, carriage_return=False, length=50)[source]

Print a pretty progress bar to the console.

accepts ‘progress’ as a percentage bool carriage_return toggles overwrite behavior

WrightTools.kit.write_h5(filepath, dictionary)[source]

Save a python dictionary into an HDF5 file.

Right now it only works to store numpy arrays of numbers.

Parameters:
  • filepath (str) – Filepath to HDF5 file to create. The .hdf5 extension will be appended to the filename if it is not already there.
  • dictionary (python dictionary-like) – The content to store to the HDF5 file.
Returns:

The full filepath to the created HDF5 file.

Return type:

str

See also

kit.read_h5()

WrightTools.kit.write_headers(filepath, dictionary)[source]

Write ‘WrightTooTools formatted’ headers to given file.

Headers written can be read again using read_headers.

Parameters:
  • filepath (str) – Path of file. File must not exist.
  • dictionary (dict or OrderedDict) – Dictionary of header items.
Returns:

Filepath of file.

Return type:

str

See also

kit.read_headers()

WrightTools.kit.zoom2D(xi, yi, zi, xi_zoom=3.0, yi_zoom=3.0, order=3, mode='nearest', cval=0.0)[source]

Zoom a 2D array, with axes.

Parameters:
  • xi (1D array) – x axis points.
  • yi (1D array) – y axis points.
  • zi (2D array) – array values. Shape of (x, y).
  • xi_zoom (float (optional)) – Zoom factor along x axis. Default is 3.
  • yi_zoom (float (optional)) – Zoom factor along y axis. Default is 3.
  • order (int (optional)) – The order of the spline interpolation, between 0 and 5. Default is 3.
  • mode ({'constant', 'nearest', 'reflect', or 'wrap'}) – Points outside the boundaries of the input are filled according to the given mode. Default is constant.
  • cval (Value used for) –