Ancillary Functions¶
This script gathers central functions and classes for general applications
list and tuple flattening |
|
function for finding files/folders in folders and their subdirectories |
|
wrapper for multicore process execution |
|
Like |
|
return the smallest possible data type for a string or list of strings |
|
General function to select random sample indexes from arrays. |
- class spatialist.ancillary.HiddenPrints[source]¶
- Suppress console stdout prints, i.e. redirect them to a temporary string object.
Examples
>>> with HiddenPrints(): >>> print('foobar') >>> print('foobar')
- spatialist.ancillary.dissolve(inlist)[source]¶
list and tuple flattening
- Parameters:
inlist (
list[Any] |tuple[Any,...]) – the list with sub-lists or tuples to be flattened- Returns:
the flattened result
- Return type:
Examples
>>> dissolve([[1, 2], [3, 4]]) [1, 2, 3, 4]
>>> dissolve([(1, 2, (3, 4)), [5, (6, 7)]]) [1, 2, 3, 4, 5, 6, 7]
- spatialist.ancillary.finder(target, matchlist, foldermode=0, regex=False, recursive=True)[source]¶
function for finding files/folders in folders and their subdirectories
- Parameters:
target (
str|list[str]) – a directory, zip- or tar-archive or a list of them to be searchedfoldermode (
int) –0: only files
1: files and folders
2: only folders
regex (
bool) – are the search patterns in matchlist regular expressions or unix shell standard (default)?recursive (
bool) – search target recursively into all subdirectories or only in the top level? This is currently only implemented for parameter target being a directory.
- Returns:
the absolute names of files/folders matching the patterns
- Return type:
- spatialist.ancillary.multicore(function, cores, multiargs, pbar=False, **singleargs)[source]¶
wrapper for multicore process execution
- Parameters:
function (
Callable[...,Any]) – individual function to be applied to each process itemcores (
int) – the number of subprocesses started/CPUs used; this value is reduced in case the number of subprocesses is smallermultiargs (
dict[str,Any]) – a dictionary containing sub-function argument names as keys and lists of arguments to be distributed among the processes as valuespbar (
bool) – add a progress bar? Does not yet work on Windows.singleargs (
Any) – all remaining arguments which are invariant among the subprocesses
- Returns:
the return of the function for all subprocesses
- Return type:
Notes
all multiargs value lists must be of same length, i.e. all argument keys must be explicitly defined for each subprocess
all function arguments passed via singleargs must be provided with the full argument name and its value (i.e. argname=argval); default function args are not accepted
if the processes return anything else than None, this function will return a list of results
if all processes return None, this function will be of type void
Examples
>>> def add(x, y, z): >>> return x + y + z >>> multicore(add, cores=2, multiargs={'x': [1, 2]}, y=5, z=9) [15, 16] >>> multicore(add, cores=2, multiargs={'x': [1, 2], 'y': [5, 6]}, z=9) [15, 17]
See also
- spatialist.ancillary.parallel_apply_along_axis(func1d, axis, arr, cores=4, *args, **kwargs)[source]¶
Like
numpy.apply_along_axis()but using multiple threads. Adapted from here.
- spatialist.ancillary.parse_literal(x)[source]¶
return the smallest possible data type for a string or list of strings
- Parameters:
- Returns:
the parsing result
- Return type:
Examples
>>> isinstance(parse_literal('1.5'), float) True
>>> isinstance(parse_literal('1'), int) True
>>> isinstance(parse_literal('foobar'), str) True
- spatialist.ancillary.run(cmd, outdir=None, logfile=None, inlist=None, void=True, errorpass=False, env=None)[source]¶
- Wrapper for subprocess execution including logfile writing and command prompt piping.This is a convenience wrapper around the
subprocessmodule and calls its classPopeninternally.- Parameters:
outdir (
str|None) – the directory to execute the command ininlist (
list[Any] |None) – a list of arguments passed to stdin, i.e., arguments passed to interactive input of the programvoid (
bool) – return stdout and stderr?errorpass (
bool) – if False, asubprocess.CalledProcessErroris raised if the command failsenv (
dict[str,Any] |None) – the environment to be passed to the subprocess
- Return type:
- Returns:
a tuple of (returncode, stdout, stderr) if void=False otherwise None
- spatialist.ancillary.sampler(mask, samples=None, dim=1, replace=False, seed=42)[source]¶
General function to select random sample indexes from arrays. Adapted from package S1_ARD.
- Parameters:
mask (
ndarray[tuple[Any,...],dtype[bool]]) – A 2D boolean mask to limit the sample selection.samples (
int|None) – The number of samples to select. If None, the positions of all matching values are returned. If there are fewer values than required samples, the positions of all values are returned.dim (
Literal[1,2]) – The dimensions of the output array and its indexes. If 1, the returned array has one dimension and the indexes refer to the one-dimensional (i.e., flattened) representation of the input mask. If 2, the output array is of shape (2, samples) with two separate 2D arrays for y (index 0) and x respectively, which reference positions in the original 2D shape of the input array.replace (
bool) – Draw samples with or without replacement?seed (
int) – Seed used to initialize the pseudo-random number generator.
- Returns:
The index positions of the generated random samples as 1D or 2D array.
- Return type:
Examples
>>> import numpy as np >>> from spatialist.ancillary import sampler >>> array = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) >>> mask = array > 2 >>> s1d = sampler(mask=mask, samples=2, dim=1) >>> s2d = sampler(mask=mask, samples=2, dim=2) >>> print(s1d) [2 3] >>> print(s2d) [[1 1] [0 1]] >>> print(array.flatten()[s1d] == array[s2d[0], s2d[1]]) [ True True]
See also