Utils sub-package
- py:
__init__.py
utilities.py
folder_tree.py
- namespace:
luxpy.utils
- luxpy.utils.np2d(data)[source]
Make a tuple, list or numpy array at least a 2D numpy array.
- Args:
- data:
- tuple, list, ndarray
- Returns:
- returns:
- ndarray with .ndim >= 2
- luxpy.utils.np3d(data)[source]
Make a tuple, list or numpy array at least a 3d numpy array.
- Args:
- data:
- tuple, list, ndarray
- Returns:
- returns:
- ndarray with .ndim >= 3
- luxpy.utils.np2dT(data)[source]
Make a tuple, list or numpy array at least a 2D numpy array and transpose.
- Args:
- data:
- tuple, list, ndarray
- Returns:
- returns:
- ndarray with .ndim >= 2 and with transposed axes.
- luxpy.utils.np3dT(data)[source]
Make a tuple, list or numpy array at least a 3d numpy array and transposed first 2 axes.
- Args:
- data:
- tuple, list, ndarray
- Returns:
- returns:
- ndarray with .ndim >= 3 and with first two axestransposed (axis=3 is kept the same).
- luxpy.utils.put_args_in_db(db, args)[source]
Takes the args with not-None input values of a function and overwrites the values of the corresponding keys in dict db. | (args are collected with the built-in function locals(), | See example usage below)
- Args:
- db:
- dict
- Returns:
- returns:
- dict with the values of specific keys overwritten by thenot-None values of corresponding args of a function fcn.
- Example usage:
- _db = {‘c’ : ‘c1’, ‘d’ : 10, ‘e’ : {‘e1’:’hello’, ‘e2’:1000}}def test_put_args_in_db(a, b, db = None, c = None,d = None,e = None):args = locals().copy() # get dict with keyword input arguments to# function ‘test_put_args_in_db’db = put_args_in_db(db,args) # overwrite non-None args in db copy.if db is not None: # unpack db for further usec,d,e = [db[x] for x in sorted(db.keys())]print(’ a : {}’.format(a))print(’ b : {}’.format(b))print(’ db: {}’.format(db))print(’ c : {}’.format(c))print(’ d : {}’.format(d))print(’ e : {}’.format(e))print(‘_db: {}’.format(_db))
- luxpy.utils.vec_to_dict(vec=None, dic={}, vsize=None, keys=None)[source]
Convert dict to vec and vice versa.
- Args:
- vec:
- list or vector array, optional
- dic:
- dict, optional
- vsize:
- list or vector array with size of values of dict, optional
- keys:
- list or vector array with keys in dict (must be provided).
- Returns:
- returns:
- x, vsizex is an array, if vec is Nonex is a dict, if vec is not None
- luxpy.utils.loadtxt(filename, header=None, sep=', ', dtype=<class 'float'>, missing_values=nan)[source]
Load data from text file.
- Args:
- filename:
- String with filename [+path]
- header:
- None, optionalNone: no header present, ‘infer’ get from file.
- sep:
- ‘,’, optionalDelimiter (‘,’ -> csv file)
- dtype:
- float, optionalTry casting output array to this datatype.
- missing_values:
- np.nan, optionalReplace missing values with this.
- Returns:
- ndarray:
- loaded data in ndarray of type dtype or object (in case of mixed types)
- luxpy.utils.savetxt(filename, X, header=None, sep=',', fmt=':1.18f', aw='w')[source]
Save data to text file.
- Args:
- filename:
- String with filename [+path]
- X:
- ndarray with data
- header:
- None or list, optionalNone: no header present.
- sep:
- ‘,’, optionalDelimiter (‘,’ -> csv file)
- fmt:
- ‘:1.18f’, optionalFormat string for numerical data output.Can be tuple/list for different output formats per column.
- aw:
- ‘w’, optionaloptions: ‘w’ -> write or ‘a’ -> append to file
- luxpy.utils.getdata(data, dtype=<class 'float'>, header=None, sep=', ', datatype='S', copy=True, verbosity=False, missing_values=nan)[source]
Get data from csv-file.
- Args:
- data:
- - str with path to file containing data- ndarray with data
- dtype:
- float, optionaldtype of elements in ndarray data arrayIf None: mixture of datatypes is expected->dtype of output will be object
- header:
- None, optional- None: no header in file- ‘infer’: infer headers from file
- sep:
- ‘,’ or ‘ ‘ or other char, optionalColumn separator in data file
- datatype’:
- ‘S’,optionalSpecifies a type of data.Is used when creating column headers (:column: is None).-‘S’: light source spectrum-‘R’: reflectance spectrumor other.
- copy:
- True, optionalReturn a copy of ndarray
- verbosity:
- True, False, optionalPrint warning when inferring headers from file.
- Returns:
- returns:
- data as ndarray
- luxpy.utils.dictkv(keys=None, values=None, ordered=True)[source]
Easy input of of keys and values into dict.
- Args:
- keys:
- iterable list[str,…] of keys
- values:
- iterable list[…,…, ] of values
- ordered:
- True, False, optionalTrue: creates an ordered dict using ‘collections.OrderedDict()’
- Returns:
- returns:
- (ordered) dict
- luxpy.utils.meshblock(x, y)[source]
Create a meshed block from x and y.
(Similar to meshgrid, but axis = 0 is retained).To enable fast blockwise calculation.- Args:
- x:
- ndarray with ndim == 2
- y:
- ndarray with ndim == 2
- Returns:
- X,Y:
- 2 ndarrays with ndim == 3X.shape = (x.shape[0],y.shape[0],x.shape[1])Y.shape = (x.shape[0],y.shape[0],y.shape[1])
- luxpy.utils.asplit(data)[source]
Split data on last axis
- Args:
- data:
- ndarray
- Returns:
- returns:
- ndarray, ndarray, …(number of returns is equal data.shape[-1])
- luxpy.utils.ajoin(data)[source]
Join data on last axis.
- Args:
- data:
- tuple (ndarray, ndarray, …)
- Returns:
- returns:
- ndarray (shape[-1] is equal to tuple length)
- luxpy.utils.broadcast_shape(data, target_shape=None, expand_2d_to_3d=None, axis0_repeats=None, axis1_repeats=None)[source]
Broadcasts shapes of data to a target_shape.
Useful for block/vector calc. when numpy fails to broadcast correctly.- Args:
- data:
- ndarray
- target_shape:
- None or tuple with requested shape, optional- None: returns unchanged :data:
- expand_2d_to_3d:
- None (do nothing) or …, optionalIf ndim == 2, expand from 2 to 3 dimensions
- axis0_repeats:
- None or number of times to repeat axis=0, optional- None: keep axis=0 same size
- axis1_repeats:
- None or number of times to repeat axis=1, optional- None: keep axis=1 same size
- Returns:
- returns:
- reshaped ndarray
- luxpy.utils.todim(x, tshape, add_axis=1, equal_shape=False)[source]
Expand x to dims that are broadcast-compatable with shape of another array.
- Args:
- x:
- ndarray
- tshape:
- tuple with target shape
- add_axis:
- 1, optionalDetermines where in x.shape an axis should be added
- equal_shape:
- False or True, optionalTrue: expand :x: to identical dimensions (speficied by :tshape:)
- Returns:
- returns:
- ndarray broadcast-compatable with tshape.
- luxpy.utils.read_excel(filename, sheet_name=None, cell_range=None, dtype=<class 'float'>, force_dictoutput=False, out='X')[source]
Read excel file using openpyxl.
- Args:
- filename:
- string with [path/]filename of Excel file.
- sheet_name:
- None, optionalIf None: read all sheetsIf string or tuple/list of strings: read these sheets.
- cell_range:
- None, optionalRead all data on sheet(s).If string range (e.g. ‘B2:C4’) or tuple/list of cell_ranges: read this range.If tuple/list: then length must match that of the list of sheet_names!
- dtype:
- float, optionalTry to cast the output data array(s) to this type. In case of failure,data type will be ‘object’.
- force_dictoutput:
- False, optionalIf True: output will always be a dictionary (sheet_names are keys)with the requested data arrays.If False: in case only a single sheet_name is supplied or only a singlesheet is present, then the output will be an ndarray!
- out:
- ‘X’, optionalString specifying requested output (eg. ‘X’ or ‘X,wb’ with wb the loaded workbook)
- Returns:
- X:
- dict or ndarray (single sheet and force_dictoutput==False)with data in requested ranges.
- wb:
- If in :out: the loaded workbook is also output.
- luxpy.utils.write_excel(filename, X, sheet_name=None, cell_range=None)[source]
Write data to an excel file using openpyxl.
- Args:
- filename:
- string with [path/]filename of Excel file.
- sheet_name:
- None, optionalIf None: use first one (or the keys in :X: when it is a dictionary)If string: use this sheet.If tuple/list of strings: use these to write the data in :X: (if :X: is a list/tuple of ndarrays)
- X:
- ndarray, list/tuple or dictIf ndarray/list/tuple: sheet_names must be supplied explicitely in :sheet_names:If dict: keys must be sheet_names
- cell_range:
- None, optionalRead all data on sheet(s).If string range (e.g. ‘B2:C4’) or tuple/list of cell_ranges: read this range.If tuple/list: then length must match that of the list of sheet_names!
- luxpy.utils.show_luxpy_tree(omit=['.pyc', '__pycache__', '.txt', '.dat', '.csv', '.npz', '.png', '.jpg', '.md', '.pdf', '.ini', '.log', '.rar', 'drivers', 'SDK_', 'dll', 'bak'])[source]
Show luxpy foler tree.
- Args:
- omit:
- List of folders and file-extensions to omit.
- Returns:
None
- luxpy.utils.is_importable(string, pip_string=None, try_pip_install=False)[source]
Check if string is importable/loadable. If it doesn’t then try to ‘pip install’ it using subprocess. Returns None if succesful, otherwise throws and error or outputs False.
- Args:
- string:
- string with package or module name
- pip_string:
- string with package or module name as known by pipIf None: use the import string
- try_pip_install:
- False, optionalTrue: try pip installing it using subprocess
- Returns:
- success:
- True if importable, False if not.
- luxpy.utils.get_function_kwargs(f)[source]
Get dictionary of a function’s keyword arguments and their default values.
- Args:
- f:
- function name
- Returns:
- dict:
- Dict with the function’s keyword arguments and their default valuesIs empty if there are no defaults (i.e. f.__defaults__ or f.__kwdefaults__ are None).
- luxpy.utils.profile_fcn(fcn, profile=True, sort_stats='tottime', output_file=None)[source]
Profile or time a function fcn.
- Args:
- fcn:
- function to be profiled or timed (using time.time() difference)
- profile:
- True, optionalProfile the function, otherwise only time it.
- sort_stats:
- ‘tottime’, optionalSort profile results according to sort_stats (‘tottime’, ‘cumtime’,…)
- output_file:
- None, optionalIf not None: output result to output_file.
- Return:
- ps:
- Profiler output
- luxpy.utils.unique(array, sort=True)[source]
Get unique elements from array.
- Args:
- array:
- array to get unique elements from.
- sort:
- True, optionalIf True: get sorted unique elements.
- Returns:
- unique_array:
- ndarray with (sorted) unique elements.
- luxpy.utils.save_pkl(filename, obj, compresslevel=0)[source]
Save an object in a (gzipped) pickle file.
- Args:
- filename:
- str with filename of pickle file.
- obj:
- python object to save
- compresslevel:
- 0, optionalIf > 0: use gzip to compress pkl file.
- Returns:
- None:
- luxpy.utils.load_pkl(filename, gzipped=False)[source]
Load the object in a (gzipped) pickle file.
- Args:
- filename:
- str with filename of pickle file.
- gzipped:
- False, optionalIf True: ‘.gz’ will be added to filename before opening.
- Returns:
- obj:
- loaded python object