API Reference#
immlib is a library of tools for manipulating immutable scientific data.
The immlib library is designed to enable immutable data structures and lazy
computation in a scientific context, and it works primarily via a collection of
utility functions and through the use of decorators, which are generally
applied to classes and their members to declare how an immutable
data-structure’s members are related. Taken together, these utilities form a
DSL-like system for declaring workflows and immutable data-structures with full
inheritance support.
- units#
The registry for units tracked by
immlib. Theimmlib.unitsobject is a globalpint-module unit registry that can be used as a single global place for tracking units. Immlib functions that interact with units generally take an argumenturegthat can be used to modify this registry. Additionally, the default registry (this object,immlib.units) can be temporarily changed in a local block usingwith immlib.default_ureg(ureg): ....- Type:
pint.UnitRegistry
- version#
A representation of the
immlibversion. The version string may be obtained viaimmlib.version.string; major, minor, and micro numbers (when present) may be obtained viaimmlib.version.major,immlib.version.minor, andimmlib.version.micro(when not provided the are set toNone), and a stage tag (a string), if given, can be obtained viaimmlib.version.stage.- Type:
- submodules#
A tuple of strings, each of which is the name of one of the submodules in
immlib. The modules are listed in load-order and allimmlibsubmodules, including private submodules, are included.- Type:
tuple of str
- docproc#
This object is used to process all of the doc-strings in the
immliblibrary; it should be used only with theimmlib.docwrapdecorator, which can safely be applied anywhere in a sequence of decorators and which correctly applies thewrapsdecorator to its argument. Function documentation is always processed using thesections=('Parameters', 'Returns', 'Raises', 'Examples', 'Inputs', 'Outputs')parameter and thewith_indent(4)decorator. The base-name for the functionfisf.__module__ + '.' + f.__name__.- Type:
docrep.DocstringProcessor object
- class ArrayIndex(array, freeze=True)#
A type that indexes the elements of an array for easy searching.
The
ArrayIndexclass is a class that stores a (typically read-only) numpy array whose elements must all be unique and that creates an index of that array’s elements.ArrayIndexobjects primarily support a afindmethod that can be used to look up object indices.ArrayIndexobjects require that the arrays they are given contain unique objects that are sortable and hashable.Examples
>>> from immlib import ArrayIndex >>> labels = [['r1c1', 'r1c2'], ['r2c1', 'r2c2']] >>> index = ArrayIndex(labels) >>> index.find('r1c1') (0, 0) >>> index.find(['r2c1', 'r1c2']) (array([1, 0]), array([0, 1])) >>> index.find(['r2c2', 'r2c1', 'r1c2'], ravel=True) array([3, 1, 2])
- find(ids, *, ravel=False, **kw)[source]#
Finds and returns the indices of the given identities.
index.find(id)returns the index, in the original array on whichindexis based, of the identityid. Ifidis not in the original array, then aKeyErroris raised.- Parameters:
ids (array-like) – The identity or identities to look up in the index.
ravel (boolean, optional) – Whether the return value should be an array representing a raveled index into the flattened version of the original indexed array (
True) or a tuple representing an unraveled multi-index into the original indexed array (False). The default isFalse.default (object, optional) – If default is not given, then an error is raised when an identity is not found. If default is given, however, the default value is inserted into the place of any missing indices and no error is raised.
- Returns:
The indices, into the original indexed array, of the given identities, ids.
- Return type:
indices
- property flatdata#
Returns a named tuple containing the flattened data used by the
ArrayIndextype to lookup identities.index.flatdatareturns a named 2-tuple with keysidentandindex. Theidentelement is a read-only numpy array containing the sorted and flattened identities represented in the original array. Theindexelement is a read-only numpy array containing the argsort of the flattened original array object.
- class Immutable(*args, **kw)#
A type that becomes immutable immediately after initialization.
Any class that inherits from
Immutableshould implement an__init__method, within which it is allowed to change the attributes of theselfobject normally. After the__init__method terminates, the object becomes read-only and can no longer be updated.
- class ImmutableType(name, bases, attrs, **kwargs)#
A meta-class for types that are immutable.
When this metaclass is used in a class, objects of the type become immutable immediately after the
__init__method is run. Such types should not overload the__new__classmethod and instead should see to their initialization in__init__as usual. Once the__init__method has finished, the__setattr__,__delattr__,__setitem__, and__delitem__methods will raise aTypeError.
- class Load(template=None)#
Loads a Python object from a stream or path then returns the object.
immlib.load(path, format) loads a Python object from the given path using the named file format and returns the loaded object or rasies an error on failure. If the format can be deduced from the path suffix, then it may be omitted.
immlib.load(stream, format) reads a Python object from the given stream object using the named format and returns the object. The format argument cannot be omitted when the first argument is a stream.
In fact, immlib.load is an object of the immlib.pathlib.Load type that primarily behaves like a function. The immlib.load.register and immlib.load.unregister methods can be used to add understood formats.
- Parameters:
source (path-like or stream) – The input source from which the object is to be loaded. This may be either a path-like object or a readable IOBase stream.
format (str or None, optional) – If provided, format must be a string that names a format that has been previously registered with load using the load.register method. If format is not provided or is None (the default), then an attempt is made to deduce the format using the suffix of the source argument, assuming that source is a path and not a stream. If source is a stream or if the suffix does not indicate a specific format, then a ValueError is raised.
**kwargs – Any additional parameters are passed to the registered export function.
- Returns:
The object that was loaded from the given stream or path.
- Return type:
object
- Raises:
TypeError – If the input source is not a stream or path-like object or if the format is not a string.
ValueError – If the format is not recognized or if it cannot be deduced from the source.
- static from_dir(src, filter=None)[source]#
Loads a nested dictionary structure of a directory.
Load.from_dir(path) returns path if path refers to a file. Alternatively, if path refers to a directory, this function returns a lazy dictionary whose keys are the names of the entries in the directory and whose values are the result of calling Load.from_dir on their paths.
- class MetaObject(*args, **kwargs)#
Base planobject type for objects that keep track of metadata.
- Parameters:
metadata (None or Mapping, optional) – The dictionary of metadata that is to be attached to the object. If the argument
Noneis provided (the default), then the empty dictionary is used.
- metadata#
A lazy dictionary of the metadata tracked by the object.
- Type:
ldict
- dropmeta(*args)[source]#
Returns a duplicate object with given metadata keys cleared.
The arguments must be keys, which are dropped from the metadata of the duplicate object.
- plan = plan(<1 calcs>, <1 params>)#
- class OSFClient(project='xxxxx', storage='osfstorage', file_cache_mode=None, local_cache_dir=None, content_type_method=<function guess_type>, pagesize=100, mkdir_mode=509)#
Client class for the OSF.
- class OSFPath(cloud_path: CloudPathT, *args: Any, **kwargs: Any)#
- class OSFPath(cloud_path: str | CloudPath, *args: Any, **kwargs: Any)
Class for representing and operating on OSF repositories.
OSFPath(path)returns a path object representing an OSF path; OSF paths use the formatosf://<project-id>/<path>. The project ID is derived from the OSF tag; i.e., the websitehttps://osf.io/<project-id>is the primary website of the OSF project. The project-ID may be followed by a colon and an OSF storage name (the project ID by itself is equivalent toosf://<project-ID>:osfstorage/).For example, the project found at the OSF website
https://osf.io/bw9ec/has the URLosf://bw9ec/.- Parameters:
cloud_path (str or path-like) – The OSF path that the created
OSFPathobject is to represent.client (OSFClient or None, optional) – The
OSFClientobject to use. TheOSFClientis responsible primarily for the caching of data locally. IfOSFClientisNone, then anOSFClientobject is created for the project using a temporary cache directory.local_cache_dir (str or path-like, optional) – The local directory in which cache files should be stored. This option is ignored if
clientis notNone; otherwise it is passed to the created client object. The cache directory is the root cache directory for the entire OSF project.file_cache_mode (cloudpathlib.enums.FileCacheMode, optional) – How often to clear the file cache; see [cloudpathlib’s caching docs](https://cloudpathlib.drivendata.org/stable/caching/) for more information about the options in
cloudpathlib.enums.FileCacheMode.mkdir_mode (int, optional) – The mode to use when making directories in the cache. By default this is
0o775. This option is ignored if theclientoption is notNone.pagesize (int, optional) – The number of items to include in a single page when paging directory contents from the OSF server. The default is 100. This option is ignored if the
clientoption is notNone.
- property drive#
The drive prefix (letter or UNC path), if any. _(Docstring copied from pathlib.Path)_
- is_file()[source]#
Whether this path is a regular file (also True for symlinks pointing to regular files).
_(Docstring copied from pathlib.Path)_
- mkdir(parents=False, exist_ok=False)[source]#
- Create a new directory at this given path.
_(Docstring copied from pathlib.Path)_
- class Save(template=None)#
Saves a Python object to a stream or path then returns the stream/path.
immlib.save(path, object, format) saves the given object to the given path using the named file format and returns the path on success or rasies an error on failure. If the format can be deduced from the path suffix, then it may be omitted.
immlib.save(stream, object, format) writes the given object to the given stream object using the named format and returns the stream.
In fact, immlib.save is an object of the immlib.pathlib.Save type that primarily behaves like a function. The immlib.save.register and immlib.save.unregister methods can be used to add understood formats.
- Parameters:
dest (path-like or stream) – The output destination to which the object is to be saved. This may be either a path-like object or a writeable IOBase stream.
obj (object) – Any object that can be saved in the given format.
format (str or None, optional) – If provided, format must be a string that names a format that has been previously registered with save using the save.register method. If format is not provided or is None (the default), then an attempt is made to deduce the format using the suffix of the dest argument, assuming that dest is a path and not a stream. If dest is a stream or if the suffix does not indicate a specific format, then a ValueError is raised.
**kwargs – Any additional parameters are passed to the registered export function.
- Returns:
The destination stream or path. If the dest argument is a path name, then a path object is returned instead of the path name.
- Return type:
path-like or stream
- Raises:
TypeError – If the destination is not a stream or path-like object or if the format is not a string.
ValueError – If the format is not recognized or if it cannot be deduced from the destination object.
- class Version(string=None, /, *, package_name=None, pyproject_path=None, on_error='warn', tag_prefixes=('rc', 'a', 'b'))#
A type that represents a Python package version.
Python packages are represented simultaneously as version strings, version tuples, and by the version components major, minor, micro, and stage.
- Parameters:
string (str or None, optional) – The version string to be represented. If this argument is not provided, then one or both of the package_name and pyproject_path options must be provided so that the version string can be obtained via the package version or the pyproject.toml file.
package_name (str or None, optional) – If the first argument (string) is provided, then this argument is ignored; otherwise, the version string is first searched for by this package name using the importlib or importlib_metadata packages. If found, then this version string is represented in the Version object.
pyproject_path (path-like or None, optional) – If the first argument (string) is not given and the package_name is not given, then the version is searched for in the pyproject.toml file given by this path. In order for such a file to be valid, it must contain a line that, when stripped of whitespace, begins with the string ‘version=’ followed by a string representation (e.g., ‘version=”1.12.5”’). If such a line is found in the [project] section of the TOM: file pointed to by this argument, then it is represented as the version string in the Version object.
on_error ({'warn' | 'ignore' | 'raise'}, optional) – How to handle failures to deduce or parse the version number. If ‘raise’ is given, then the errors are allowed to be raised. If ‘warn’, then a warning is raised and a null version is returned. If ‘ignore’, then errors are ignored and a null version is returned. The default is ‘raise’.
tag_prefixes (tuple of str, optional) – An optional tuple of strings that can appear as the prefixes of stage tagss at the end of the version string. By default, this is (‘rc’, ‘a’, ‘b’), so version strings like ‘1.1.12a6’ and ‘1.1.12rc6’ are valid but ‘1.1.12c6’ is not.
- string#
The string representing the package version. For example “1.2.15” or “0.2.2.dev1”.
- Type:
str
- tuple#
The components of the version string, for example, (1, 2, 15) or (0, 2, 2, ‘dev1’). Any missing component is excluded.
- Type:
tuple of int and str
- major#
The major version number, typically indicates major API version.
- Type:
int
- minor#
The minor version number, typically indicates minor API version.
- Type:
int
- micro#
The micro version number, typically indicates patch increment number.
- Type:
int
- stage#
The development stage of the version. For example ‘dev1’ or ‘rc2’.
- Type:
str
- getstring(pyproject_path=None)[source]#
Returns the current version string for the given package name.
Version.getstring(package_name) returns the version string of the package with the given package name.
Version.getstring(pyproject_path=path) returns the version string found in the pyproject.toml file found at the given path.
Version.getstring(package_name, path) returns Version.getstring(package_name) if the given package_name is found, otherwise returns Version.getstring(pyproject_path=path).
- alike_units(a, b, /, *, ureg=None)#
Returns
Trueif the arguments are alike units, otherwiseFalse.alike_units(a, b)returnsTrueif a and b can be cast to each other in terms of units andFalseotherwise. Both a and b can either be units, unit names, or quantities with units. If either a or b is neither a unit nor a quantity, then it is considered equivalent to having units ofNone, i.e., no units.- Parameters:
a (unit-like) – A unit object or the name of a unit or a quantity.
b (unit-like) – A unit object or the name of a unit or a quantity.
ureg (pint.UnitRegistry, None, Ellipsis, optional) – The
pint.UnitRegistryobject to use. IfEllipsis, then theimmlib.unitsregistry is used. IfNone, then the registry of object a is used if available or that of object b if not. If neither a nor b has an available registry, thenimmlib.unitsis used.
- Returns:
Trueif the units a and b are alike andFalseotherwise.- Return type:
bool
- argfilter(fn=None, /, **kwargs)#
A decorator that creates decorators that filter function arguments.
A function decorated with
@argfilteris turned into a an argument filter function, which itself can be used to decorate functions whose arguments need to be filtered.In the definition of the filter function, the names of arguments must match those of the arguments they will be filtering on other functions. The filter function must return a tuple of the filtered values in the order they are defined in the function’s argument list. The arguments may be given in any order, but a
*in the arguments list indicates that any of the arguments following the*are not themselves being filtered and thus will not be returned from the filter function.When a filter function is used to decorate another function, the decorator can optionally be given named arguments where the name corresponds to one of the arguments to the origional filter definition and the value corresponds to the name that is used for this parameter in the decorated functions. In this way, the parameter names don’t have to match exactly those of the filter function and can instead be specified in the decoration.
Examples
>>> @argfilter ... def fix_angle(angle, *, unit): ... angle = np.asarray(angle) ... if unit == 'degrees': ... angle = np.pi / 180 * angle ... elif unit != 'radians': ... raise ValueError(f'unrecognized unit: {unit}') ... return (angle,) ... @fix_angle ... def cos_halfangle(angle, unit='radians'): ... return np.cos(angle / 2)
>>> cos_halfangle([0, 360], 'degrees') array([1., -1.])
>>> @fix_angle(angle='theta') ... def sin_halfangle(theta, unit='radians'): ... return np.sin(theta / 2)
>>> sin_halfangle([0, 360], 'degrees') array([0., 0.])
- class args(*args, **kwargs)#
An object type that represents a set of function arguments.
args(x1, x2 ... k1=v1, k2=v2 ...)yields anargsobject that represents the positional argumentsx1, x2 ...and the named argumentsk1=v1,k2=v2, etc.If
ais an instance ofargsandfis a function, then the arguments inacan be applied tofusing either of the following .. method:: -f @ a- - ``a.passto(f)``
Note that if
fis an object that defines the__matmul__method, then the former syntax will call that method instead of the__rmatmul__method of theargsobjectaand thus won’t work.
- array_args(fn=None, /, *args)#
Converts arguments of the decorated function into NumPy arrays.
The decorator
@array_args, when applied to a function, will convert all of that function’s arguments into NumPy arrays prior to invoking the function.array_argsconsiderspint.Quantityobjects whose magnitudes are arrays to be arrays and will convert arguments that are quantitites whose magnitudes are not arrays into new quantities with array magnitudes.If a function is decorated with
@array_args('arg1', 'arg2' ...)then only the arguments whose names are given (arg1,arg2, …) are converted into arrays.
- assoc(d, /, *args, **kwargs)#
Returns a copy of the given dictionary with additional key-value pairs.
assoc(d, key, val)returns a copy of the dictionary d with the given key-value pair associated in the new copy. The return value is always the same type as the argument d but is always an updated copy. The argument d is never mutated.assoc(d, key1, val1, key2, val2 ...)associates all the given keys to the given values in the returned copy.assoc(d, key1=val1, key2=val2 ...)uses the keyword arguments as the arguments that are to be associated. These may be mixed with positional key-value pairs.assoc(d)returns a copy of d.- Parameters:
d (dict-like) – A dictionary that is to be copied and updated with the following arguments.
args – Sequential pairs of keys and values (i.e.,
len(args)must be even) that should be updated in the returned dictionary.kwargs – Additional key-value pairs to be updated in the returned dictionary.
- Returns:
A copy of d with updated keys and values.
- Return type:
dict-like
- azpath(obj, *args, **kwargs)#
Creates and returns an
AzureBlobPathrepresenting an Azure repository.azpath(p)creates and returns anAzureBlobPathobject, which is a type ofcloudpathlib.CloudPathobject, from the path or path-stringp. Ifpis anAzureBlobPath, then it is returned as-is. Otherwisepathstr(p)is converted into anAzureBlobPath;pathstr(p)may start with'az://'(not case-sensitive) or, if it does not have a scheme specifier,'az://'will be prepended to it.azpath(p, a1, a2...)convertspinto anAzureBlobPaththen joins thea1,a2, etc. values to the end of the path and returns the joined path.The
azpathfunction accepts all the optional arguments of theAzureBlobClienttype fromcloudpathlibas well as theclientoption. If theclientoption is given along with additional optional arguments, then the optional arguments are ignored.Additionally,
azpathparses the optioncache_path, which is not normally accepted byAzureBlobPath, which instead requires the optionlocal_cache_dir. Any time that alocal_cache_diris given, it overrides thecache_path; however, iflocal_cache_diris not given andcache_pathis, then the directoryos.path.join(cache_path,"az")is given as thelocal_cache_diroption.
- class calc(*args, name=None, lazy=True, lrucache=0, pathcache=None, indent=None)#
Decorator type that represents a single calculation in a calc-plan.
The
calcclass encapsulates data regarding the calculation of a single set of output values from a separate set of input values: a calculation component that can be fit together with other such components to make a calculation plan.@calcby itself can be used as a decorator to indicate that the function that follows is a calculation component; calculation components can be combined to formplanobjects, which can encapsulate a flexible workflow of Python computations. When@calcis used as a decorator by itself, then the calc is considered to have a single output value whose name is the same as that of the function it decorates.@calc(names...)accepts a string or strings that name the output values of the calc function. In this case, the decorated function must return either a tuple of thes values in the order they are given or a dictionary in which the keys are the same as the given names.@calc(None)is a special instance which indicates that the lazy argument is to be ignored (it is forced to beFalse), no output values are to be produced by the function, and the calculation must always run when the input parameters are updated.The
calcclass parses its inputs and outputs through theimmlib.docwrapfunction in order to collect documentation (see theinput_docsandoutput_docsattributes, below). The'Inputs'and'Outputs'sections are tracked as the documentation of the parameters, and are required to be formatted using [NumPy’s documentation style](https://numpydoc.readthedocs.io/en/latest/format.html) in order for the parameter documentation to be properly extracted. Users of calculation objects should decorate their functions usingdocwrapmanually themselves, however (if desired), because decorating a function withcalcalone does not cause the function’s documentation to be available to other functions that use@docwrapto format their docstrings.Caching for calculations requires some care. First, the
calc- andplan-based workflow system inimmlibis designed to work best withcalcobjects that are pure functions. A functionf(*args, **kw)is pure if it has no side-effects and iff(*args1, **kw1) == f(*args2, **kw2)is true wheneverargs1 == args2 and kw1 == kw2. That is,falways produces the same outputs when given the same inputs. Plans that contain unpure functions can work fine in many contexts, but unpurecalcobjects will break caching because the return value of a cached unpure calculation will always be the same value. (In other words, the value that is calculated and cached by the function the first time it is called.)Second, the
calctype has an option,pathcache, which can be set to an explicit path to which all calculations run by the createdcalcobject will be cached and later uncached if re-requested. This is occasionally appropriate for a particular compute environment, but a better approach is typically to grant control of caching and cache paths to the user who creates theplandictobject downstream of the creation of thecalcobjects. To enable this behavior, one should instead use the optionpathcache=True, which enables caching of calculations to a specific cache path when provided by the user during the creation of theplandict(the default isFalse, which disables path caching for the calculation).- Parameters:
outputs (strings) – The positional arguments to
@calc()provide the names of the output variables. The names must all be valid variable names (seeimmlib.strisvar).name (None or str, optional) – The name of the function. The default,
None, usesfn.__name__.lazy (bool, optional) – Whether the calculation unit should be calculated lazily (
True) or eagerly (False) when a plandict is created. The default isTrue.lrucache (int, optional) – The number of recently calculated results to cache. If this value is 0, then no memoization is done (the default). If
lrucacheis an integer greater than 0, then an LRU cache is used with a maximum size of lrucache. Iflrucacheisinf, then all values are cached indefinitely. Note that this cache is performed at the level of the calculation using Python’sfunctoolscaching decorators.pathcache (None, bool, or path-like, optional) – If
pathcacheis a path-like object (typically apathlib.Pathorstring) that references a directory, then the results are cached in files in the given directory whenever possible. Thepathcacheoption may also a 2-tuple containing a path followed by options to thejoblib.Memoryconstructor; seeimmlib.util.to_pathcachefor more information.indent (int or None, optional) – The indentation level of the function’s docstring. The default is
None, which indicates that the indentation level should be deduced from the docstring itself.
- name#
The name of the calculation function.
- Type:
str
- base_function#
The original function, prior to decoration for caching.
- Type:
callable
- lrucache#
The in-memory cache being used. If this value is
None``then no in-memory cache is being used. If it is an integer, this indicates the number of least recently used objects being stored in the cache. Otherwise, ``lrucachewill be a function used to wrap thebase_functionof the calculation for caching. Thelrucacheparameter is filtered by theimmlib.util.to_lrucachefunction in order to convert it into a validfunctools.lru_cacheobject.- Type:
None or lrucache-like
- pathcache#
The file-system-based cache being used. If this value is
NoneorFalse, then no filesystem cache is being used by the calculation directly. If this value is a path object, then that path is the directory in which cache files are saved/loaded. Ifpathcacheis ajoblib.Memoryobject, then this object handles the caching for the calculation. Otherwise, the value will beTrue, indicating that caching should be performed automatically using thecache_pathinput to the calc. Ifcache_pathwas not already one of the inputs, it is added as an input with the default valueNone. When automatic caching is performed, thecache_pathis automatically converted into ajoblib.Memoryobject using theimmlib.util.to_pathcachefunction.- Type:
None or pathcache-like
- function#
The function itself.
- Type:
callable
- signature#
The signature of
fn, as returned frominspect.signature(fn).- Type:
inspect.Signature
- inputs#
The names of the input parameters for the calculation.
- Type:
pcollections.pset of str
- outputs#
The names of the output values of the calculation.
- Type:
tuple of str
- defaults#
A persistent dictionary whose keys are input parameter names and whose values are the default values for the associated parameters.
- Type:
pcollections.pdict
- lazy#
Whether the calculation is intended as a lazy (
True) or eager (False) calculation.- Type:
bool
- input_docs#
A
pdictobject whose keys are input names and whose values are the documentation for the associated input parameters.- Type:
pcollections.pdict
- output_docs#
A
pdictobject whose keys are output names and whose values are the documentation for the associated output values.- Type:
pcollections.pdict
- call(*args, **kwargs)[source]#
Calls the calculation and returns the results dictionary.
c.call(...)is an alias forc(...).See also
calc.mapcall,calc.eager_call, andcalc.lazy_call.
- eager_call(*args, **kwargs)[source]#
Eagerly calls the given calculation using the arguments.
c.eager_call(...)returns the result of calling the calculationc(...)directly. Using theeager_callmethod is different from calling the__call__method only in that theeager_callmethod ignores thelazymember and always returns the direct results of calling the calculation; using the__call__method will result ineager_callbeing run if the calculation is not lazy and inlazy_callbeing run if the calculation is lazy.See also
- eager_mapcall(*args, **kwargs)[source]#
Calls the given calculation using the parameters in mappings.
c.eager_mapcall(map1, map2..., key1=val1, key2=val2...)returns the result of calling the calculationc(...)using the parameters found in the provided mappings and key-value pairs. All arguments ofmapcallare merged left-to-right usingimmlib.mergethen passed toc.functionas required by it.
- lazy_call(*args, **kwargs)[source]#
Returns a lazy-dict of the results of calling the calculation.
calc.lazy_call(...)is equivalent tocalc(...)except that thelazydictthat it returns encapsulates the running of the calculation itself, so thatcalc(...)is not run until one of the lazy values is requested.See also
- lazy_mapcall(*args, **kwargs)[source]#
Calls the given calculation lazily using the parameters in mappings.
c.lazy_mapcall(map1, map2..., key1=val1, key2=val2...)returns the result of calling the calculationc(...)using the parameters found in the provided mappings and key-value pairs. All arguments ofmapcallare merged left-to-right usingimmlib.mergethen passed toc.functionas required by it.The only difference between
calc.mapcallandcalc.lazy_mapcallis that the lazydict returned by the latter method encapsulates the calling of the calculation itself, so no call to the calculation is made until one of the values of the lazydict is requested.See also
- mapcall(*args, **kwargs)[source]#
Calls the calculation and returns the results dictionary.
c.mapcall(map1, map2..., key1=val1, key2=val2...)returns the result of calling the calculationc(...)using the parameters found in the provided mappings and key-value pairs. All arguments ofmapcallare merged left-to-right usingimmlib.mergethen passed toc.functionas required by it.See also
- rename_keys(*args, **kwargs)[source]#
Returns a copy of the calculation with inputs and outputs renamed.
calc.rename_keys(...)returns a copy ofcalcin which the input and output values of the function have been translated. The translation is found from merging the list of 0 or more dict-like arguments given left-to-right followed by the keyword arguments into a single dictionary. The keys of this dictionary are translated into their associated values in the returned dictionary.If any of the values of the merged dictionary are 2-tuples, then they are interpreted as
(input_tr, output_tr). In this case, then the key must be associated with a name that appears in both the calculation’s input list and its output list, and the two names are translated differently.
- update_function(fn)[source]#
Updates the function and its calc object.
On occasion, a function decorated with
@calcis later decorated with another feature, such as a decorator that causes its inputs to be promoted. Such a decorator, when it comes after the@calcdecorator (i.e., on a line prior to the@calc), will not update the calculation object and thus the calculation object, when invoked, will not call the fully decorated version of its function. To fix this, anycalcobject whosebase_functionmember variable is identical to the function given to aplanobject (i.e.,f is not to_calc(f).base_function), then this method is called to return acalcobject whosebase_functionhas been updated. If possible, it also updates the fn argument to use the newcalcobject.In general, this function should not be called directly by the user; rather, it gets run automatically when a calc is added to a new
planorplanobject.
- can_hash(obj)#
Returns
Trueif obj is safe to hash andFalseotherwise.can_hash(obj)is equivalent tohashsafe(obj) is not None. This differs fromis_ahashable(obj)in thatis_ahashableonly checks whether obj is an instance ofHashablewhilehashsafe(obj)attempts to hash obj and returnsNonewhen aTypeErroris raised.Note
A fairly reliable test of whether an object is immutable or not in Python is whether it can be hashed.
See also
- can_iter(obj)#
Returns
Trueif obj is safe to iterate andFalseotherwise.can_iter(obj)is equivalent toitersafe(obj) is not None. This differs fromis_aiterable(obj)in thatis_aiterableonly checks whether obj is an instance ofIterable;itersafetries to runiter(obj)and returnsNonewhen aTypeErroris raised.See also
- class default_docproc(docproc)#
Context manager for setting the default
immlib.docprocdocument processing object.The following code-block can be used to evaluate the code represented by
...using thedocrep.DocstringProcessorobjectdocprocas the defaultimmlib.docprocprocessor:with immlib.default_docproc(docproc): ...
If the
immlib.docprocvalue has accidentally been corrupted, then it can be reset using the following:immlib.default_docproc.reset()
- Parameters:
docproc (docrep.DocstringProcessor object) – The docstring processing object to use as the default in
immlibin the contextualized code.
See also
docwrapdecorator that simplifies the use of the
docreplibrary.
- class default_ureg(ureg)#
Context manager for setting the default
immlibunit registry.The following code-block can be used to evaluate the code represented by
...using the unit-registryuregas the defaultimmlib.unitsregistry:with immlib.default_ureg(ureg): ...
- dictmap(f, keys, /, *args, **kw)#
Returns a dict with the given keys and the values
map(f, keys).dictmap(f, keys)returns a dict object whose keys are the elements ofiter(keys)and whose values are the elements ofmap(f, keys).dictmap(f, keys, *args, **kw)returns a dict object whose keys are the elements ofiter(keys)and whose values are the elements of[f(k, *args, **kw) for k in iter(keys)].- Parameters:
f (function) – The function used to create the values in the new dictionary; it must accept one arguments (
f(k)) plus any additional arguments provided in*argsand**kwargs.keys (iterable) – An iterable object whose values are to become the keys of the new dictionary.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
A dictionary of the given keys with each key
kmapped tof(k).- Return type:
dict
- dissoc(d, /, *args)#
Returns a copy of the given dictionary with certain keys removed.
dissoc(d, key)returns a copy of the dictionary d with the givenkeydisssociated in the new copy. The return value is always the same type as the argument d.dissoc(d, key1, key2 ...)dissociates all the given keys from their values in the returned copy.dissoc(d)returns a copy of d.- Parameters:
d (dict-like) – A dictionary that is to be copied and updated according to the following arguments.
args – Keys that should be removed from the copy of d that is returned.
- Returns:
A copy of d with the given keys removed.
- Return type:
dict-like
- docwrap(f=None, /, *, indent=None, proc=Ellipsis)#
Applies standard doc-string processing to the decorated function.
The
immlib.docwrapdecorator applies a standard set of pre-processing to the docstring of the function that follows it. This processing amounts to using thedocrepmodule’sDocstringProcessoras a filter on the documentation of the function. The function’s documentation is always placed in the base-name equal to its fully-qualified namespace name.When called as
@docwrap(name)for a stringname, the documentation for the decorated function is instead placed under the base-namename.- Parameters:
f (function or str or None, optional) –
The function to be decorated, when
@docwrapis used alone as a decorator, or when used as a decorator with only the other options given, such as@docwrap(indent=8). If a string is given, as in@docwrap('immlib.dictmap')then the given string is used as the function’s name instead of its__module__plus its__name__. This is mostly useful when using@docwrapwith a function defined in a private submodule; for exampleimmlib.dictmapis defined inimmlib.util._corebut is imported into a reclaimed by theimmlibcore namespace, so it is typically considered to belong to that namespace.Typically this argument does not need to be provided as it is given after the decorator line; the exception to this is when a string is given.
indent (None or int, optional) – The number of spaces that are used as indentation before lines in the docstring. This is mostly useful when decorated functions appear in indented contexts and thus the default indentation of 4 is inappropriate. If
Noneis given, then the decorator finds the non-empty line, not including the first line, with the smallest indentation and uses that.proc (docrep.DocstringProcessor or Ellipsis, optional) – The proc option provides the document processor object from the
docreplibrary that should be used to process the decorated object. Because these objects can be specifically configured to enable different docstring formats, this option is provided to the user. The default value isEllipsis, in which case theimmlib.docprocobject is used. Thedocprocobject has been configured to work with theInputandOutputsections that are used with calculations and plans. Theimmlib.with_docprocfunction can be used to change theimmlib.docprocobject that is used in a local code-block.
- Returns:
The decorated function or object, after its docstring has been parsed.
- Return type:
object
See also
default_docprocRun a code-block with a specific default docstring processor.
- filepath(p, *args)#
Returns a local
Pathobject for the given path if possible.The
filepathfunction is intended to coerce remote paths (such as the S3 or OSF paths managed through thecloudpathlib.CloudPathclass) into paths representing their local caches. If a local file is requested, then it is always downloaded before thePathis returned. For directories, the cache directory itself will always exist, but no such guarantee is made about its contents.If the argument to
filepathis a string and not a path object, then it is converted into a path via theimmlib.pathfunction.- Parameters:
p (path-like) – The object whose local cache path is to be returned after it has been downloaded.
- Returns:
If the input path p is already a local path, then it is returned. If p is a remote path, then it is downloaded and its cache path is returned. If there is no local cache or if the file does not exist, an error is raised.
- Return type:
Path
- freezearray(arr)#
Freezes a NumPy array or SciPy sparse array in-place.
freezearray(x)sets the'WRITEABLE'bit on the numpy arrayxor onx.dataifxis a SciPy sparse array. Ifxis neither a NumPy array nor a SciPy sparse array, then aTypeErroris raised. No value is returned.freezearray(q)is equivalent tofreezearray(q.m)ifqis apint.Quantityobject.Warning
This function mutates its argument in-place.
See also
- frozenarray(obj, /, dtype=None, *, copy=False, **kwargs)#
Roughly equivalent to
numpy.arraybut returns read-only arrays.frozenarray(obj)is equivalent tonumpy.array(obj)with a small number of exceptions:Primarily, the returned object is always a frozen array (i.e., an array with the
'WRITEABLE'flag set toFalse).The default value of the
copyoption isFalse, meaning that a copy of the array will only be made if required by the other parameters or if the array is not already read-only. If you wish to make an array read-only rather than obtaining a read-only copy of it, use thefreezearray()function.SciPy sparse arrays are also handled by setting the write flag on the
obj.datamember.If
objis apint.Quantityobject, then an equivalent quantity with the magnitude made read-only is returned.
If a PyTorch tensor is passed to
frozenarray, it will be converted into a frozen NumPy array; PyTorch tensors themselves cannot be frozen, however.See also
numpy.arrayCreate an array that is not frozen.
freezearrayConvert an argument to a frozen array in-place.
- get(d, k, /, *args, **kwargs)#
Returns a value from either a mapping or a sequence.
The
getfunction is essentially a function version of thegetmethod that works for bothMappingandSequencetypes (e.g.,dict,list,tuple, and related types that implement their abstract bases).get(d, k)extracts element k from object d and returns it. If k is not a valid index of d (i.e., k is not a key of d, if d is a mapping, or is not an integer index of d if d is a sequence), then the optional valuedefaultis returned. Ifdetaultis not explicitly provided, then an error is raised. Note that if a non-integer key is provided for a sequence, this is treated as a missing index.Note
The default value may be expressed as either a third positional argument or a named argument (
default).- Parameters:
d (object) – The dict-like or list-like object from which an element is being extracted.
k (object) – The key or index into d whose value is to be extracted.
args – The default value to be returned if an item is not found. The default value may be specified as a third positional argument.
kwargs – The default value to be returned if an item is not found. The default value may be specified as a named argument with the name
"default".
- Returns:
The object
d[k], if the key k is found in the collection d. Otherwise,defaultis returned.- Return type:
object
- Raises:
KeyError – If the key or index k is not found in the collection d and no
defaultoption is given.
- gspath(obj, *args, **kwargs)#
Creates and returns an
GSPathrepresenting a Google Storage repository.gspath(p)creates and returns aGSPathobject, which is a type ofcloudpathlib.CloudPathobject, from the path or path-stringp. Ifpis aGSPath, then it is returned as-is. Otherwisepathstr(p)is converted into anGSPath;pathstr(p)may start with'gs://'(not case-sensitive) or, if it does not have a scheme specifier,'gs://'will be prepended to it.gspath(p, a1, a2...)convertspinto anGSPaththen joins thea1,a2, etc. values to the end of the path and returns the joined path.The
gspathfunction accepts all the optional arguments of theGSClienttype fromcloudpathlibas well as theclientoption. If theclientoption is given along with additional optional arguments, then the optional arguments are ignored.Additionally,
gspathparses the optioncache_path, which is not normally accepted byGSPath, which instead requires the optionlocal_cache_dir. Any time that alocal_cache_diris given, it overrides thecache_path; however, iflocal_cache_diris not given andcache_pathis, then the directoryos.path.join(cache_path, "gs")is given as thelocal_cache_diroption.
- hashsafe(obj)#
Returns
hash(obj)if obj is hashable, otherwise returnsNone.This function attempts to hash an object and returns
Nonewhen doing so raises aTypeError.Note
A fairly reliable test of whether an object is immutable or not in Python is whether it can be hashed.
- Parameters:
obj (object) – The object to be hashed.
- Returns:
If the object is hashable, returns the hashcode; otherwise, returns
None.- Return type:
int or None
See also
- is_abytes(obj)#
Returns
Trueif an object is a byte-string, otherwiseFalse.is_abytes(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.ByteStringtype.- Parameters:
obj (object) – The object whose quality as an
ByteStringobject is to be assessed.- Returns:
Trueif obj is an instance ofByteString, otherwiseFalse.- Return type:
bool
- is_acoll(obj)#
Returns
Trueif an object is a collection (a sized iterable container).is_acoll(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Collectiontype.- Parameters:
obj (object) – The object whose quality as an
Collectionobject is to be assessed.- Returns:
Trueif obj is an instance ofCollection, otherwiseFalse.- Return type:
bool
- is_acontainer(obj)#
Returns
Trueif an object implements__contains__, otherwiseFalse.is_acontainer(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Containertype.- Parameters:
obj (object) – The object whose quality as a
Containerobject is to be assessed.- Returns:
Trueif obj is an instance ofContainer, otherwiseFalse.- Return type:
bool
- is_ahashable(obj)#
Returns
Trueif an object is a hashable object, otherwiseFalse.is_ahashable(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Hashabletype. This differs from thecan_hashfunction, which checks whehter callinghashon an object raises an exception.- Parameters:
obj (object) – The object whose quality as an
Hashableobject is to be assessed.- Returns:
Trueif obj is an instance ofHashable, otherwiseFalse.- Return type:
boolean
See also
- is_aiterable(obj)#
Returns
Trueif an object implements__iter__, otherwiseFalse.is_aiterable(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Iterabletype.- Parameters:
obj (object) – The object whose quality as an
Iterableobject is to be assessed.- Returns:
Trueif obj is an instance ofIterable, otherwiseFalse.- Return type:
bool
- is_aiterator(obj)#
Returns
Trueif an object is an instance ofcollections.abc.Iterator.is_aiterable(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Iteratortype.- Parameters:
obj (object) – The object whose quality as an
Iteratorobject is to be assessed.- Returns:
Trueif obj is an instance ofIterator, otherwiseFalse.- Return type:
bool
- is_amap(obj)#
Returns
Trueif an object is an abstract mapping, otherwiseFalse.is_amap(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Mappingtype.- Parameters:
obj (object) – The object whose quality as an
Mappingobject is to be assessed.- Returns:
Trueif obj is an instance ofMapping, otherwiseFalse.- Return type:
bool
- is_ammap(obj)#
Returns
Trueif an object is a mutable mapping, otherwiseFalse.is_ammap(obj)returnsTrueif the given objectobjis an instance of the abstractcollections.abc.MutableMappingtype.- Parameters:
obj (object) – The object whose quality as an
MutableMappingobject is to be assessed.- Returns:
Trueif obj is an instance ofMutableMapping, otherwiseFalse.- Return type:
bool
- is_amseq(obj)#
Returns
Trueif an object is a mutable sequence, otherwiseFalse.is_amseq(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.MutableSequencetype.- Parameters:
obj (object) – The object whose quality as an
MutableSequenceobject is to be assessed.- Returns:
Trueif obj is an instance ofMutableSequence, otherwiseFalse.- Return type:
bool
- is_amset(obj)#
Returns
Trueif an object is a mutable set, otherwiseFalse.is_amset(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.MutableSettype.- Parameters:
obj (object) – The object whose quality as an
MutableSetobject is to be assessed.- Returns:
Trueif obj is an instance ofMutableSet, otherwiseFalse.- Return type:
bool
- is_apmap(obj)#
Returns
Trueif an object is a persistent mapping, otherwiseFalse.is_apmap(obj)returnsTrueif the given object obj is an instance of the abstractpcollections.abc.PersistentMappingtype.- Parameters:
obj (object) – The object whose quality as an
PersistentMappingobject is to be assessed.- Returns:
Trueif obj is an instance ofPersistentMapping, otherwiseFalse.- Return type:
bool
- is_apseq(obj)#
Returns
Trueif an object is a persistent sequence, otherwiseFalse.is_apseq(obj)returnsTrueif the given object obj is an instance of the abstractpcollections.abc.PersistentSequencetype.- Parameters:
obj (object) – The object whose quality as an
PersistentSequenceobject is to be assessed.- Returns:
Trueif obj is an instance ofPersistentSequence, otherwiseFalse.- Return type:
bool
- is_apset(obj)#
Returns
Trueif an object is a persistent set, otherwiseFalse.is_apset(obj)returnsTrueif the given object obj is an instance of the abstractpcollections.abc.PersistentSettype.- Parameters:
obj (object) – The object whose quality as an
PersistentSetobject is to be assessed.- Returns:
Trueif obj is an instance ofPersistentSet, otherwiseFalse.- Return type:
bool
- is_areversible(obj)#
Returns
Trueif an object is an instance ofReversible.is_areversible(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Reversibletype.- Parameters:
obj (object) – The object whose quality as an
Reversibleobject is to be assessed.- Returns:
Trueif obj is an instance ofReversible, otherwiseFalse.- Return type:
bool
- is_array(obj, /, *, dtype=None, shape=None, ndim=None, numel=None, frozen=None, sparse=None, quant=None, unit=Ellipsis, ureg=None)#
Returns
Trueif an object is anumpy.ndarrayobject, otherwise returnsFalse.is_array(obj)returnsTrueif the given object obj is an instance of thenumpy.ndarrayclass or is ascipy.sparsearray, or if obj is apint.Quantityobject whose magnitude is one of these. Additional constraints may be placed on the object via the optional argments.Note that to
immlib, bothnumpy.ndarrayarrays andscipy.sparsearrays are considered “arrays”. This behavior can be changed with thesparseparameter.- Parameters:
obj (object) – The object whose quality as a NumPy array object is to be assessed.
dtype (dtype-like or None, optional) – The NumPy dtype that is required of the obj in order to be considered a valid
ndarray. Theobj.dtypematches the given dtype parameter if either dtype isNone(the default) or ifobj.dtypeis a sub-dtype of dtype according tonumpy.issubdtype. Alternately, dtype can be a tuple, in which case, obj is considered valid if its dtype is any of the dtypes in dtype. Note that in the case of a tuple, the dtype of obj must appear exactly in the tuple rather than be a subtype of one of the objects in the tuple.ndim (int, tuple or ints, or None, optional) – The number of dimensions that the object must have in order to be considered a valid numpy array. If
None, then any number of dimensions is acceptable (this is the default). If this is an integer, then the number of dimensions must be exactly that integer. If this is a list or tuple of integers, then the dimensionality must be one of these numbers.shape (int, tuple of ints, or None, optional) – If the
shapeparameter is notNone, then the given obj must have a shape that matches the parameter value. The value shape must be a tuple that is equal to the obj’s shape tuple with the following additional rules: a-1value in theshapetuple will match any value in the obj’s shape tuple, and a singleEllipsismay appear in shape, which matches any number of values in the obj’s shape tuple. The default value ofNoneindicates that no restriction should be applied to the obj’s shape.numel (int, tuple of ints, or None, optional) – If the numel parameter is not
None, then the given obj must have the same number of elements as given by numel. If numel is a tuple, then the number of elements in obj must be in the numel tuple. The number of elements is the product of its shape.frozen (bool or None, optional) – If
None, then no restrictions are placed on the'WRITEABLE'flag of obj. IfTrue, then the data in obj must be read-only in order for obj to be considered a valid array. IfFalse, then the data in obj must not be read-only.sparse (boolean or False, optional) – If the sparse` parameter is
None, then no requirements are placed on the sparsity of obj for it to be considered a valid array. If sparse isTrueorFalse, then obj must either be sparse or not be sparse, respectively, for obj to be considered valid. Ifsparseis a string, then it must be either'coo','lil','csr', or'csr', indicating the required sparse array type. Onlyscipy.sparsematrices are considered valid sparse arrays.quant (bool, optional) – Whether
Quantityobjects should be considered valid arrays or not. Ifquant=Truethen obj is considered a valid array only whenobjis a quantity object with anumpyarray as the magnitude. IfFalse, then obj must be anumpyarray itself and not aQuantityto be considered valid. IfNone(the default), then either quantities ornumpyarrays are considered valid arrays.unit (unit-like, Ellipsis, or None, optional) – A unit with which the object obj’s unit must be compatible in order for obj to be considered a valid array. An obj that is not a quantity is considered to have a unit of
None, which is not the same as being a quantity with a dimensionless unit. In other words,is_array(array, quant=None)will returnTruefor a numpy array whileis_array(arary, quant='dimensionless')will returnFalse. Ifunit=Ellipsis(the default), then the object’s unit is ignored.ureg (pint.UnitRegistry, None, or Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then the registry of obj is used if obj is a quantity, andimmlib.unitsis used if not.
- Returns:
Trueif obj is a valid numpy array, otherwiseFalse.- Return type:
bool
See also
- is_aseq(obj)#
Returns
Trueif an object is a sequence, otherwiseFalse.is_aseq(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Sequencetype.- Parameters:
obj (object) – The object whose quality as an
Sequenceobject is to be assessed.- Returns:
Trueif obj is an instance ofSequence, otherwiseFalse.- Return type:
bool
- is_aset(obj)#
Returns
Trueif an object is a set type, otherwiseFalse.is_aset(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Settype.- Parameters:
obj (object) – The object whose quality as an
Setobject is to be assessed.- Returns:
Trueif obj is an instance ofSet, otherwiseFalse.- Return type:
bool
- is_asized(obj)#
Returns
Trueif an object implementslen(), otherwiseFalse.is_asized(obj)returnsTrueif the given object obj is an instance of the abstractcollections.abc.Sizedtype.- Parameters:
obj (object) – The object whose quality as a
Sizedobject is to be assessed.- Returns:
Trueif obj is an instance ofSized, otherwiseFalse.- Return type:
bool
- is_azpath(obj)#
Detects whether the input is an
AzureBlobPathobject.is_azpath(obj)returnsTrueif obj is an instance of theAzureBlobPathclass andFalseotherwise.See also:
like_azpath- Parameters:
obj (object) – The object whose membership in the
AzureBlobPathclass is to be determined.- Returns:
Trueif obj is an instance ofAzureBlobPathandFalseotherwise.- Return type:
boolean
- is_bool(obj, /)#
Determines whether the argument is a scalar boolean or not.
is_bool(obj)returnsTrueif obj is a scalar boolean andFalseotherwise.See also
is_scalar,is_booldata
- is_booldata(obj, /)#
Returns
Trueif an object is a boolean, otherwiseFalse.is_booldata(obj)returnsTrueif the given object obj is an instance of thebooltype or if it is an instance of a boolean NumPy array or PyTorch tensor.- Parameters:
obj (object) – The object whose quality as a
boolobject or boolean array or tensor is to be assessed.- Returns:
Trueif obj is an instance ofboolor is a boolean array or tensor, otherwiseFalse.- Return type:
boolean
See also
- is_bytes(obj)#
Returns
Trueif an object is abytesobject, otherwiseFalse.is_bytes(obj)returnsTrueif the given object obj is an instance of thebytestype and returnsFalseotherwise.- Parameters:
obj (object) – The object whose quality as an
bytesobject is to be assessed.- Returns:
Trueif obj is an instance ofbytes, otherwiseFalse.- Return type:
bool
- is_calcfn(obj, /)#
Determines if an object is function that was decorated by
@calc.is_calcfn(obj)returnsTrueif obj is a function that was decorated with an@calcdecorator or if obj is acalcobject, and it returnsFalseotherwise.Functions decorated with
@calcare not changed but rather are given some metadata, which is stored in the member fieldcalc. For such functions, this field contains an object of typecalc.See also
calc,to_calc,is_calc
- is_complex(obj, /)#
Determines whether the argument is a scalar complex number or not.
is_complex(obj)returnsTrueif obj is a scalar complex number andFalseotherwise. Note that booleans, integers, and real numbers are all considered valid complex numbers.See also
- is_complexdata(obj)#
Returns
Trueif an object is a complex number, otherwiseFalse.is_complexdata(obj)returnsTrueif the given object obj is an instance of thenumbers.Complextype or an instance of a complex-valued NumPy array or PyTorch tensor.- Parameters:
obj (object) – The object whose quality as a
Complexobject is to be assessed.- Returns:
Trueif obj is an instance ofComplex, otherwiseFalse.- Return type:
boolean
See also
- is_ddict(obj)#
Returns
Trueif an object is adefaultdictobject.is_ddict(obj)returnsTrueif the given object obj is an instance of thecollections.defaultdicttype.- Parameters:
obj (object) – The object whose quality as a
defaultdictobject is to be assessed.- Returns:
Trueif obj is an instance ofdefaultdict, otherwiseFalse.- Return type:
bool
- is_dense(obj, /, dtype=None, *, shape=None, ndim=None, numel=None, quant=None, ureg=None, unit=Ellipsis)#
Returns
Trueif an object is a dense NumPy array or PyTorch tensor.is_dense(obj)returnsTrueif the given object obj is an instance of one of the NumPyndarrayclasses, is a dense PyTorch tensor, or is a quantity whose magnintude is one of theese. Additional constraints may be placed on the object via the optional argments.- Parameters:
obj (object) – The object whose quality as a dense numerical object is to be assessed.
dtype (dtype-like or None, optional) – The NumPy or PyTorch dtype or dtype-like object that is required to match that of the obj in order to be considered valid. The
obj.dtypematches the given dtype parameter if either dtype isNone(the default) or ifobj.dtypeis equivalent to dtype. Alternately, dtype can be a tuple, in which case, obj is considered valid if its dtype is any of the dtypes in dtype.ndim (int, tuple or ints, or None, optional) – The number of dimensions that the object must have in order to be considered valid. If ndim is
None, then any number of dimensions is acceptable (this is the default). If it is an integer, then the number of dimensions must be exactly that integer. If this is a list or tuple of integers, then the dimensionality must be one of the listedn numbers.shape (int, tuple of ints, or None, optional) – If the shape parameter is not
None, then the given obj must have a shape that matches the parameter value. The value of shape must be a tuple that is equal to the shape of obj with the following additional rules: a-1value in the shape tuple will match any value in the shape of obj, and a singleEllipsismay appear in shape, which matches any number of values in the shape tuple of obj. The default value ofNoneindicates that no restriction should be applied to the shape of obj.numel (int, tuple of ints, or None, optional) – If the numel parameter is not
None, then the given obj must have the same number of elements as given by numel. If numel is a tuple, then the number of elements in obj must be in the numel tuple. The number of elements is the product of its shape.quant (bool, optional) – Whether
pint.Quantityobjects should be considered valid or not. Ifquant=Truethen obj is considered a valid numerical object only when obj is a quantity object with a valid numerical object as the magnitude. Ifquant=False, then obj must be a numerical object itself and not apint.Quantityto be considered valid. Ifquant=None(the default), then either quantities or numerical objects are considered valid.ureg (UnitRegistry, None, or Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then the registry of obj is used if obj is a quantity, andimmlib.unitsis used if not.unit (unit-like or Ellipsis, optional) – A unit with which the unit of obj must be compatible in order for obj to be considered a valid numerical object. An obj that is not a quantity is considered to have dimensionless units. If
unit=Ellipsis(the default), then the object’s unit is ignored.
- Returns:
Trueif obj is a valid dense numerical object, otherwiseFalse.- Return type:
bool
- is_dict(obj)#
Returns
Trueif an object is adictobject.is_dict(obj)returnsTrueif the given object obj is an instance of thedicttype.- Parameters:
obj (object) – The object whose quality as an
dictobject is to be assessed.- Returns:
Trueif obj is an instance ofdict, otherwiseFalse.- Return type:
bool
- is_filepath(p)#
Detects whether an object is a filesystem
Pathobject.Any object that inherits from the
Pathtype is considered a filesystem path. File-paths correspond to URLs that begin with'file://'.- Parameters:
p (path-like) – The object whose quality as a path is to be assessed.
- Returns:
Trueif p is an instance of thePathtype andFalseotherwise.- Return type:
boolean
- is_frozenset(obj)#
Returns
Trueif an object is afrozensetobject.is_frozenset(obj)returnsTrueif the given object obj is an instance of thefrozensettype.- Parameters:
obj (object) – The object whose quality as an
frozensetobject is to be assessed.- Returns:
Trueif obj is an instance offrozenset, otherwiseFalse.- Return type:
bool
- is_gspath(obj)#
Detects whether the input is an
GSPathobject.is_gspath(obj)returnsTrueif obj is an instance of theGSPathclass andFalseotherwise.See also:
like_gspath- Parameters:
obj (object) – The object whose membership in the
GSPathclass is to be determined.- Returns:
Trueif obj is an instance ofGSPathandFalseotherwise.- Return type:
boolean
- is_intdata(obj, /)#
Returns
Trueif an object is a Python integer, otherwiseFalse.is_intdata(obj)returnsTrueif the given object obj is an instance of thenumbers.Integraltype or if it is an instance of a numeric NumPy array or PyTorch tensor whose dtype is an integer type.- Parameters:
obj (object) – The object whose quality as a
Integralobject or integer-valued array or tensor is to be assessed.- Returns:
Trueif obj is an instance ofIntegralor is an integer numpy array, otherwiseFalse.- Return type:
boolean
See also
- is_integer(obj, /)#
Determines whether the argument is a scalar integer or not.
is_integer(obj)returnsTrueif obj is a scalar integer andFalseotherwise. Note that booleans are considered integers.See also
is_scalar,is_intdata
- is_lambda(obj)#
Returns
Trueif an object is a lambda function, otherwiseFalse.is_lambda(obj)returnsTrueif the given object obj is an instance of thetypes.LambdaTypetype.- Parameters:
obj (object) – The object whose quality as a
LambdaTypeobject is to be assessed.- Returns:
Trueif obj is an instance ofLambdaType, otherwiseFalse.- Return type:
bool
- is_ldict(obj)#
Returns
Trueif an object is a persistent lazy dictionary object.is_ldict(obj)returnsTrueif the given object obj is an instance of thepcollections.ldicttype andFalseotherwise.- Parameters:
obj (object) – The object whose quality as a
ldictobject is to be assessed.- Returns:
Trueif obj is an instance ofldict, otherwiseFalse.- Return type:
bool
- is_list(obj)#
Returns
Trueif an object is alistobject.is_list(obj)returnsTrueif the given object obj is an instance of thelisttype.- Parameters:
obj (object) – The object whose quality as an
listobject is to be assessed.- Returns:
Trueif obj is an instance oflist, otherwiseFalse.- Return type:
bool
- is_llist(obj)#
Returns
Trueif an object is a persistent lazy list object.is_llist(obj)returnsTrueif the given object obj is an instance of thepcollections.llisttype andFalseotherwise.- Parameters:
obj (object) – The object whose quality as a
llistobject is to be assessed.- Returns:
Trueif obj is an instance ofllist, otherwiseFalse.- Return type:
bool
- is_mcoll(obj)#
Returns
Trueif an object is a mutablelist,set, ordict.is_mcoll(obj)returnsTrueif the given object obj is an instance of thedict,set, orlisttypes, all of which are mutable collections. Otherwise,Falseis returned.- Parameters:
obj (object) – The object whose quality as a mutable collection is to be assessed.
- Returns:
Trueif obj is alist,set, ordictandFalseotherwise.- Return type:
bool
- is_number(obj, /, dtype=None)#
Determines whether the argument is a scalar number or not.
is_number(obj)returnsTrueif obj is a scalar number andFalseotherwise. The following are considered scalar numbers:Any instances of
numbers.Number,Any numpy array
xwhose shape is()such thatx.item()is a scalar.
- Parameters:
obj (object) – The object whose quality as a scalar number is to be tested.
dtype (bool, int, float, complex, or None, optional) – The type of the scalar. If this is
None(the default)``, then the type of the scalar must be a number but it needn’t be any particular number. Otherwise, it must match the given type.
- Returns:
Trueif obj is a scalar number value andFalseotherwise.- Return type:
bool
See also
like_number,is_numberdata,is_numeric,is_bool,is_integer,is_real,is_complex
- is_numberdata(obj, /)#
Returns
Trueif an object is a Python number, otherwiseFalse.is_numberdata(obj)returnsTrueif the given objectobjis an instance of thenumbers.Numbertype or if it is an instance of a numeric NumPy array or PyTorch tensor.Except in special cases,
is_numberdata(x)is equivalent tois_complexdata(x).is_numberdatais related to the functionis_numeric: ifis_numeric(x)isTruethenis_numberdata(x)is alsoTrue. However,is_numberdata(10)isTruewhileis_numeric(10)is not.is_numberdatais designed for determining whether an object represents numbers, whereasis_numericis designed for querying the properties of NumPy arrays and PyTorch tensors such as their shapes and data types.- Parameters:
obj (object) – The object whose quality as a
Numberobject or numerical array or tensor is to be assessed.- Returns:
Trueif obj is an instance ofNumberor is a numerical array or tensor, otherwiseFalse.- Return type:
boolean
See also
- is_numeric(obj, /, dtype=None, *, shape=None, ndim=None, numel=None, sparse=None, quant=None, unit=Ellipsis, ureg=None)#
Returns
Trueif an object is a numerical collection type andFalseotherwise.is_numeric(obj)returnsTrueif the given object obj is an instance of thetorch.Tensorclass, thenumpy.ndarrayclass, one one of thescipy.sparsearray classes, or is apint.Quantityobject whose magnitude is an instance of one of these types. Additional constraints may be placed on the object via the optional argments.Note
The
is_numericfunction is similar to theis_arrayandis_tensorfunctions butis agnostic about whether its argument is a PyTorch tensor, a NumPy array, or an object that can be converted into one of these types.- Parameters:
obj (object) – The object whose quality as a numeric object is to be assessed.
dtype (dtype-like or None, optional) – The NumPy or PyTorch dtype or dtype-like object that is required to match that of the obj in order to be considered valid. The
obj.dtypematches the given dtype parameter if either dtype isNone(the default) or ifobj.dtypeis equivalent to dtype. Alternately, dtype can be a tuple, in which case, obj is considered valid if its dtype is any of the dtypes in dtype.ndim (int, tuple or ints, or None, optional) – The number of dimensions that the object must have in order to be considered valid. If ndim is
None, then any number of dimensions is acceptable (this is the default). If it is an integer, then the number of dimensions must be exactly that integer. If this is a list or tuple of integers, then the dimensionality must be one of the listedn numbers.shape (int, tuple of ints, or None, optional) – If the shape parameter is not
None, then the given obj must have a shape that matches the parameter value. The value of shape must be a tuple that is equal to the shape of obj with the following additional rules: a-1value in the shape tuple will match any value in the shape of obj, and a singleEllipsismay appear in shape, which matches any number of values in the shape tuple of obj. The default value ofNoneindicates that no restriction should be applied to the shape of obj.sparse (bool or False, optional) – If the
sparseparameter isNone, then no requirements are placed on the sparsity of obj for it to be considered valid. If sparse isTrueorFalse, then obj must either be sparse or not be sparse, respectively, for obj to be considered valid. If sparse is a string, then it must be a valid sparse array type that matches the type of obj for obj to be considered valid.numel (int, tuple of ints, or None, optional) – If the numel parameter is not
None, then the given obj must have the same number of elements as given by numel. If numel is a tuple, then the number of elements in obj must be in the numel tuple. The number of elements is the product of its shape.quant (bool, optional) – Whether
pint.Quantityobjects should be considered valid or not. Ifquant=Truethen obj is considered a valid numerical object only when obj is a quantity object with a valid numerical object as the magnitude. Ifquant=False, then obj must be a numerical object itself and not apint.Quantityto be considered valid. Ifquant=None(the default), then either quantities or numerical objects are considered valid.unit (unit-like or Ellipsis, optional) – A unit with which the unit of obj must be compatible in order for obj to be considered a valid numerical object. An obj that is not a quantity is considered to have dimensionless units. If
unit=Ellipsis(the default), then the object’s unit is ignored.ureg (UnitRegistry, None, or Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then the registry of obj is used if obj is a quantity, andimmlib.unitsis used if not.
- Returns:
Trueif obj is a valid numerical object, otherwiseFalse.- Return type:
bool
- is_odict(obj)#
Returns
Trueif an object is anOrderedDictobject.is_odict(obj)returnsTrueif the given object obj is an instance of thecollections.OrderedDicttype.- Parameters:
obj (object) – The object whose quality as an
OrderedDictobject is to be assessed.- Returns:
Trueif obj is an instance ofOrderedDict, otherwiseFalse.- Return type:
bool
- is_osfpath(obj)#
Detects whether the input is an
OSFPathobject.is_osfpath(obj)returnsTrueifobjis an instance of theOSFPathclass andFalseotherwise.See also:
like_osfpath- Parameters:
obj (object) – The object whose membership in the
OSFPathclass is to be determined.- Returns:
Trueifobjis an instance ofOSFPathandFalseotherwise.- Return type:
boolean
- is_path(p)#
Detects whether an object is either a
Pathor aCloudPathobject.Both
PathandCloudPathobjects abstractly represent paths, but they do not share a subclass.is_pathtests whether an object’s type is a subclass of any of path types recognized byimmlib. Additional path types can be registered by addingimmlib.paths.PathTypeRecordinstances to theimmlib.pathtypesdictionary. The key for such a record should be the string prefix for the path type (such as's3'for anS3Pathtype).- Parameters:
p (path-like) – The object whose quality as a path is to be assessed.
- Returns:
Trueif p has a type that is recognized byimmlibas a path type andFalseotherwise.- Return type:
boolean
- is_pcoll(obj)#
Detects if an object is a
plist,pset,pdict,llistorldict.is_pcoll(obj)returnsTrueif the given object obj is an instance of the persistent collection typesplist,pset,pdict,llist, orldict. Otherwise,Falseis returned.Note that this function tests against a specific set of concrete types; instances of objects whose types are subclasses of these types will be treated as instances of the base types; however other immutable types not defined in
immliborpcollectionswill not be recognized by this function.- Parameters:
obj (object) – The object whose quality as a persistent collection is to be assessed.
- Returns:
Trueif obj is a persistent collection andFalseotherwise.- Return type:
bool
- is_pdict(obj)#
Returns
Trueif an object is a persistent dictionary object.is_pdict(obj)returnsTrueif the given object obj is an instance of thepcollections.pdicttype andFalseotherwise.Note
The
ldicttype is a subtype ofpdict, so foris_pdict(ldict())returnsTrue.- Parameters:
obj (object) – The object whose quality as a
pdictobject is to be assessed.- Returns:
Trueif obj is an instance ofpdict, otherwiseFalse.- Return type:
bool
- is_plan(arg)#
Determines if an object is a
planinstance.is_plan(x)returnsTrueifxis a calculationplanandFalseotherwise.
- is_plandict(arg)#
Determines if an object is a
plandictinstance.is_plandict(x)returnsTrueifxis aplandictobject andFalseotherwise.
- is_planobject(obj)#
Determines if an object is an instance of a
immlib.plantypeobject.is_planobject(obj)returnsTrueifobjis an instance of aimmlib.plantypeclass andFalseotherwise.See also:
plantype,is_plantype
- is_plantype(obj)#
Determines if an object is a
immlib.plantype.is_plantype(obj)returnsTrueifobjis aimmlibplantypeclass andFalseotherwise. Note that this works for the type but not instances of the type, for which you should useis_planobject.See also:
is_planobject,plantype
- is_plist(obj)#
Returns
Trueif an object is a persistent list object.is_plist(obj)returnsTrueif the given object obj is an instance of thepcollections.plisttype andFalseotherwise.- Parameters:
obj (object) – The object whose quality as a
plistobject is to be assessed.- Returns:
Trueif obj is an instance ofplist, otherwiseFalse.- Return type:
bool
- is_pset(obj)#
Returns
Trueif an object is a persistent set object.is_pset(obj)returnsTrueif the given object obj is an instance of thepcollections.psettype andFalseotherwise.- Parameters:
obj (object) – The object whose quality as a
psetobject is to be assessed.- Returns:
Trueif obj is an instance ofpset, otherwiseFalse.- Return type:
bool
See also
is_set,is_aset,is_amset,is_apset,is_tset,is_frozenset
- is_quant(obj, /, unit=Ellipsis, *, ureg=None)#
Returns
Trueif given apint.Quantityobject andFalseotherwise.is_quant(obj)returnsTrueif obj is apint.Quantityobject andFalseotherwise. The optional parameter unit may additionally specify a unit that obj must be compatible with.Note
The parameter value
unit=Nonetype indicates a scalar without a unit (i.e., an object that is not a quantity), and so, whileNoneis a valid value, this function will always returnFalsewhen it is passed.- Parameters:
obj (object) – The object whose quality as a
pint.Quantityobject is to be assessed.unit (unit-like or None, optional) – The unit that the object must have in order to be considered valid. This may be a
pint.Unitor unit-name (see alsoimmlib.unit), a list or tuple of such units/unit-names, orNone. IfEllipsisis given (the default), then the object must be apint.Quantityobject, but it doesn’t matter what the unit of the object is. Otherwise, the object must have a unit equivalent to the unit or to one of the units given (unit may be a tuple of possible units). Thepint.UnitRegistryobjects for the units given via this parameter are ignored; only the ureg parameter influences thepint.UnitRegistryrequirements.ureg (pint.UnitRegistry, Ellipsis, None, optional) – The
pint.UnitRegistryobject to use for units. IfEllipsis, then valueimmlib.unitsis used. If ureg isNone(the default), then a specific unit registry is not checked.
- Returns:
Trueif obj is apint.Quantitywhose unit is compatible with the requested unit andFalseotherwise.- Return type:
bool
- Raises:
TypeError – If the
uregparameter is not apint.UnitRegistry,Ellipsis, orNone.
- is_real(obj, /)#
Determines whether the argument is a scalar real number or not.
is_real(obj)returnsTrueif obj is a scalar real number andFalseotherwise. Note that booleans and integers are considered real numbers.See also
is_scalar,is_realdata
- is_realdata(obj, /)#
Returns
Trueif an object is a Python number, otherwiseFalse.is_realdata(obj)returnsTrueif the given object obj is an instance of thenumbers.Realtype or of a real-valued NumPy array or PyTorch tensor.- Parameters:
obj (object) – The object whose quality as a
Realobject or real-values NumPy array ot PyTorch tensor is to be assessed.- Returns:
Trueif obj is an instance ofRealor is a real-valued array or tensor, otherwiseFalse.- Return type:
bool
See also
- is_s3path(obj)#
Detects whether the input is an
S3Pathobject.is_s3path(obj)returnsTrueifobjis an instance of theS3Pathclass andFalseotherwise.See also:
like_s3path- Parameters:
obj (object) – The object whose membership in the
S3Pathclass is to be determined.- Returns:
Trueifobjis an instance ofS3PathandFalseotherwise.- Return type:
boolean
- is_set(obj)#
Returns
Trueif an object is asetobject.is_set(obj)returnsTrueif the given object obj is an instance of thesettype. Note that this is not the same asis_asetwhich determines whether the object is of thecollections.abc.Setabstract type.- Parameters:
obj (object) – The object whose quality as an
setobject is to be assessed.- Returns:
Trueif obj is an instance ofset, otherwiseFalse.- Return type:
bool
See also
is_aset,is_amset,is_apset,is_pset,is_tset,is_frozenset
- is_sparse(obj, /, dtype=None, *, shape=None, ndim=None, numel=None, quant=None, ureg=None, unit=Ellipsis)#
Returns
Trueif an object is a sparse SciPy array or a sparse PyTorch tensor.is_sparse(obj)returnsTrueif the given object obj is an instance of one of the SciPy sparse array classes, is a sparse PyTorch tensor, or is apint.Quantitywhose magnintude is one of theese. Additional constraints may be placed on the object via the optional argments.- Parameters:
obj (object) – The object whose quality as a sparse numerical object is to be assessed.
dtype (dtype-like or None, optional) – The NumPy or PyTorch dtype or dtype-like object that is required to match that of the obj in order to be considered valid. The
obj.dtypematches the given dtype parameter if either dtype isNone(the default) or ifobj.dtypeis equivalent to dtype. Alternately, dtype can be a tuple, in which case, obj is considered valid if its dtype is any of the dtypes in dtype.ndim (int, tuple or ints, or None, optional) – The number of dimensions that the object must have in order to be considered valid. If ndim is
None, then any number of dimensions is acceptable (this is the default). If it is an integer, then the number of dimensions must be exactly that integer. If this is a list or tuple of integers, then the dimensionality must be one of the listedn numbers.shape (int, tuple of ints, or None, optional) – If the shape parameter is not
None, then the given obj must have a shape that matches the parameter value. The value of shape must be a tuple that is equal to the shape of obj with the following additional rules: a-1value in the shape tuple will match any value in the shape of obj, and a singleEllipsismay appear in shape, which matches any number of values in the shape tuple of obj. The default value ofNoneindicates that no restriction should be applied to the shape of obj.numel (int, tuple of ints, or None, optional) – If the numel parameter is not
None, then the given obj must have the same number of elements as given by numel. If numel is a tuple, then the number of elements in obj must be in the numel tuple. The number of elements is the product of its shape.quant (bool, optional) – Whether
pint.Quantityobjects should be considered valid or not. Ifquant=Truethen obj is considered a valid numerical object only when obj is a quantity object with a valid numerical object as the magnitude. Ifquant=False, then obj must be a numerical object itself and not apint.Quantityto be considered valid. Ifquant=None(the default), then either quantities or numerical objects are considered valid.ureg (UnitRegistry, None, or Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then the registry of obj is used if obj is a quantity, andimmlib.unitsis used if not.unit (unit-like or Ellipsis, optional) – A unit with which the unit of obj must be compatible in order for obj to be considered a valid numerical object. An obj that is not a quantity is considered to have dimensionless units. If
unit=Ellipsis(the default), then the object’s unit is ignored.sparsetype ('matrix', 'array', or None, optional) – The kind of sparse array to accept: either
'matrix'for the scipy sparse matrix types (e.g.,scipy.sparse.csr_matrix) or'array'for the sparse array types (e.g.,scipy.sparse.csr_array). If the value isNone(the default) then either is accepted.
- Returns:
Trueif obj is a valid sparse numerical object, otherwiseFalse.- Return type:
bool
- is_str(obj)#
Returns
Trueif an object is a string andFalseotherwise.is_str(obj)returnsTrueif the given object obj is an instance of thestrtype andFalseotherwise.- Parameters:
obj (object) – The object whose quality as a string object is to be assessed.
- Returns:
True if obj is a string, otherwise False.
- Return type:
bool
- is_tcoll(obj)#
Returns
Trueif an object is a transienttlist,tset, ortdict.is_tcoll(obj)returnsTrueif the given object obj is an instance of thetdict,tset, ortlisttypes, all of which are transient collections. Otherwise,Falseis returned.- Parameters:
obj (object) – The object whose quality as a transient collection is to be assessed.
- Returns:
Trueif obj is atlist,tset, ortdictandFalseotherwise.- Return type:
bool
- is_tensor(obj, /, dtype=None, *, shape=None, ndim=None, numel=None, device=None, requires_grad=None, sparse=None, quant=None, unit=Ellipsis, ureg=None)#
Returns
Trueif the argument is atorch.tensorobject, otherwise returnsFalse.is_tensor(obj)returnsTrueif the given object obj is an instance of thetorch.Tensorclass or is apint.Quantityobject whose magnitude is an instance oftorch.Tensor. Additional constraints may be placed on the object via the optional argments.- Parameters:
obj (object) – The object whose quality as a PyTorch tensor object is to be assessed.
dtype (dtype-like or None, optional) – The PyTorch dtype or a dtype-like object that is required to match that of the obj in order to be considered a valid tensor. The
obj.dtypematches the given dtype parameter if either dtype isNone(the default) or ifobj.dtypeis equal to the PyTorch equivalent ot dtype. Alternately, dtype can be a tuple, in which case, obj is considered valid if its dtype is any of the dtypes in dtype. ndim : int or tuple or ints or None, optional The number of dimensions that the object must have in order to be considered a valid tensor. IfNone, then any number of dimensions is acceptable (this is the default). If this is an integer, then the number of dimensions must be exactly that integer. If this is a list or tuple of integers, then the dimensionality must be one of these numbers.shape (int, tuple of ints, None, optional) – If the shape parameter is not
None, then the given obj must have a shape shape that matches the parameter value. The value of shape must be a tuple that is equal to obj’s shape tuple with the following additional rules: a-1value in the shape tuple will match any value in the obj’s shape tuple, and a singleEllipsismay appear in shape, which matches any number of values in the obj’s shape tuple. The default value ofNoneindicates that no restriction should be applied to the obj’s shape.numel (int, tuple of ints, or None, optional) – If the numel parameter is not
None, then the given obj must have the same number of elements as given by numel. If numel is a tuple, then the number of elements in obj must be in the numel tuple. The number of elements is the product of its shape.device (device-name or None, optional) – If device is
None, then a tensor with any device field is considered valid; otherwise, the device parameter must equalobj.devicefor obj to be considered a valid tensor. The default value isNone.requires_grad (bool or None, optional) – If
None, then a tensor with any requires_grad field is considered valid; otherwise, the requires_grad parameter must equalobj.requires_gradfor obj to be considered a valid tensor. The default value isNone.sparse (bool or None, optional) – If the sparse parameter is
None, then no requirements are placed on the sparsity of obj for it to be considered a valid tensor. If sparse isTrueorFalse, then obj must either be sparse or not be sparse, respectively, for obj to be considered valid. If sparse is a string, then it must be either'coo'or'csr', indicating the required sparse array type.quant (bool, optional) – Whether
pint.Quantityobjects should be considered valid tensors or not. If quant isTruethen obj is considered a valid array only when obj is a quantity object with atorchtensor as the magnitude. If quant isFalse, then obj must be atorchtensor itself and not aQuantityto be considered valid. If quant isNone(the default), then either quantities ortorchtensors are considered valid.unit (unit-like, None, Ellipsis, optional) – A unit with which the object obj’s unit must be compatible in order for obj to be considered a valid tensor. An obj that is not a quantity is considered to have a unit of
None. Ifunit=Ellipsis(the default), then the object’s unit is ignored.ureg (UnitRegistry, None, or Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then the registry of obj is used if obj is a quantity, andimmlib.unitsis used if not.
- Returns:
Trueif obj is a valid PyTorch tensor whose properties match the requirements spelled out by the optional parameters, otherwiseFalse.- Return type:
boolean
See also
- is_tplandict(arg)#
Determines if an object is a
tplandictinstance.is_tplandict(x)returnsTrueifxis atplandictobject andFalseotherwise.
- is_tuple(obj)#
Returns
Trueif an object is atupleobject.is_tuple(obj)returnsTrueif the given object obj is an instance of thetupletype.- Parameters:
obj (object) – The object whose quality as an
tupleobject is to be assessed.- Returns:
Trueif obj is an instance oftuple, otherwiseFalse.- Return type:
bool
- is_unit(q, /, *, ureg=None)#
Returns
Trueif q is apint.Unitobject andFalseotherwise.is_unit(q)returnsTrueif q is a unit object (of typepint.Unit) andFalseotherwise.- Parameters:
q (object) – The object whose quality as a
pint.Unitis to be assessed.ureg (UnitRegistry, Ellipsis, or None, optional) – The
pint.UnitRegistryobject that the given unit object must belong to. IfNone(the default), then any unit registry is allowed. IfEllipsis, then theimmlib.unitsregistry is used. Otherwise, this must be a specificpint.UnitRegistryobject.
- Returns:
Trueif q is apint.Unitobject andFalseotherwise.- Return type:
bool
- Raises:
TypeError – If the
uregparameter is not apint.UnitRegistry,Ellipsis, orNone.
- is_ureg(obj)#
Returns
Trueif an object is aping.UnitRegistryobject.is_ureg(obj)returnsTrueif the given object obj is an instance of thepint.UnitRegistrytype.- Parameters:
obj (object) – The object whose quality as an
UnitRegistryobject is to be assessed.- Returns:
Trueif obj is an instance ofUnitRegistry, otherwiseFalse.- Return type:
bool
- is_url(url, /)#
Returns
Trueif given a valid URL string andFalseotherwise.is_url(url)returnsTrueif and only if the given URL is a valid URL string that includes the URL scheme and the netloc unless the scheme is'file', in which case the netloc is optional. Whether the URL can be requested or not does not make a difference;is_urloperates on the given URL string alone.See also
can_download_url
- itemmap(f, d, /, *args, **kwargs)#
Returns a dictionary object whose values are a function of a given dictionary’s items.
itemmap(f, d)returns a dict whose keys are the same as those of the given mapping object d and whose values, for each keykaref(k, d[k]).itemmap(f, d, *args, **kw)additionally passes the given arguments to the function f, such that in the resulting map, each keykis mapped tof(k, d[k], *args, **kw).Unlike
lazyitemmap, this function returns either adict, apdict, or anldictdepending on the input argument d. If d is anldict, then anldictis returned; if d is apdict, apdictis returned, and otherwise, adictis returnd.- Parameters:
f (function) – The function used to create the values in the new dictionary; it must accept two arguments (
f(k, d[k])) plus any additional arguments provided in*argsand**kwargs.d (collections.abc.Mapping) – A mapping whose keys are to be preserved and remapped to a function of their values.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
This function always returns a dictionary object whose type is either
pcollections.pdict,pcollections.ldict, ordict, depending on the type of d.- Return type:
pcollections.pdict or pcollections.ldict or dict
- itersafe(obj)#
Returns an iterator of the given object or
Noneif it is not iterable.itersafe(obj)is equivalent toiter(obj)with the exception that, if obj is not iterable, it returnsNoneinstead of raising an exception.- Parameters:
obj (object) – The object to be iterated.
- Returns:
If obj is iterable, returns
iter(obj); otherwise, returnsNone.- Return type:
iterator or None
See also
- keymap(f, d, /, *args, **kwargs)#
Returns a dict object whose values are a function of a dict’s keys.
keymap(f, d)returns a dict whose keys are the same as those of the given dict object and whose values, for each keykaref(k).keymap(f, d, *args, **kw)additionally passes the given arguments to the function f, such that in the resulting map, each keykis mapped tof(k, *args, **kw).This function returns either a
dictor apdict. Ifdis apdict, apdictis returned, and otherwise, adictis returnd. Unlike thevalmapfunction, anldictis never returned because the lazy values of such a dictionary are not accessed bykeymap; if a lazy dictionary is required, then the functionlazykeymapshould be used instead.- Parameters:
f (function) – The function used to create the values in the new dictionary.
d (collections.abc.Mapping or iterable) – A mapping whose keys are to be preserved and remapped to a function of themselves. Alternatively, this may be an iterable of the keys instead of a dict with matching keys.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
This function always returns a dictionary object whose type is either
pcollections.pdictordict, depending on the type of d.- Return type:
collections.abc.Mapping object
- lambdadict(*args, **kwargs)#
Builds and returns a
ldictwith lambda functions calculated lazily.lambdadict(args...)is equivalent tomerge(args...)except that always returns an object of typepcollections.ldictand that any lambda function in the values provided by the merged arguments is made into a lazy partial function whose inputs come from the lambda-function variable names in the same resultingldict.Warning
This function will gladly return an
ldictthat encapsulates an infinite loop if you are not careful. For example, the following lambdadict will infinitely loop when either key is requested:ld = lambdadict(a=lambda b:b, b=lambda a:a).Examples
>>> d = lambdadict(a=1, b=2, c=lambda a,b: a + b) >>> d.is_lazy('c') True
>>> d.is_ready('c') False
>>> d['c'] 3
>>> d {|'a': 1, 'b': 2, 'c': 3|}
- lazyitemmap(f, d, /, *args, **kwargs)#
Returns an
ldictobject whose values are a function of a dict’s items.lazyitemmap(f, d)yields anldictwhose keys are the same as those of the given dict object and whose values, for each keyk, are lazily computed asf(k, d[k]).itemmap(f, d, *args, **kw)additionally passes the given arguments to the function f, such that in the resulting map, each keykis mapped tof(k, d[k], *args, **kw).- Parameters:
f (function) – The function used to create the values in the new dictionary; it must accept two arguments (
f(k, d[k])) plus any additional arguments provided in*argsand**kwargs.d (collections.abc.Mapping) – A mapping whose items are to be preserved and remapped to a function of their keys and values.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
This function always returns a lazy dictionary object of type
pcollections.ldict.- Return type:
pcollections.ldict
- lazykeymap(f, d, /, *args, **kwargs)#
Returns a object of type
pcollections.ldictwhose values are a function of the keys of the mapping d.keymap(f, d)returns a dict whose keys are the same as those of the given dict object and whose values, for each keykaref(k). If d is a sequence or iterable, then it is treated as a sequence of keys.keymap(f, d, *args, **kw)additionally passes the given arguments to the function f, such that in the resulting map, each keykis mapped tof(k, *args, **kw).- Parameters:
f (function) – The function used to create the values in the new dictionary.
d (collections.abc.Mapping or iterable) – A mapping whose keys are to be preserved and remapped to a function of themselves. Alternatively, this may be an iterable of the keys instead of a dict with matching keys.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
This function always returns an object of type
ldictwhose values are lazy.- Return type:
pcollections.ldict
- lazyvalmap(f, d, /, *args, **kwargs)#
Returns a dict object whose values are transformed by a function.
lazyvalmap(f, d)returns a dict whose keys are the same as those of the given dict object and whose values, for each keykaref(d[k]). All values are created lazily.lazyvalmap(f, d, *args, **kw)additionally passes the given arguments to the function f, such that in the resulting map, each keykis mapped tof(d[k], *args, **kw).- Parameters:
f (function) – The function used to create the values in the new dictionary.
d (collections.abc.Mapping) – A mapping whose keys are to be preserved and remapped to a function of their values.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
This function always returns a lazy dictionary object of type
pcollections.ldict.- Return type:
pcollections.ldict
- ldictmap(f, keys, *args, **kw)#
Returns a lazy dictionary with the given keys and the values
map(f, keys).lazydictmap(f, keys)returns apcollections.ldictobject whose keys are the elements ofiter(keys)and whose values are the elements ofmap(f, keys). All values are lazy.lazydictmap(f, keys, *args, **kw)returns a pcollections.ldict object whose keys are the elements ofiter(keys)and whose values are the elements of[f(k, *args, **kw) for k in iter(keys)], lazily calculated.- Parameters:
f (function) – The function used to create the values in the new dictionary; it must accept one arguments (
f(k)) plus any additional arguments provided in*argsand**kwargs.keys (iterable) – An iterable object whose values are to become the keys of the new dictionary.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
A persistent lazy dictionary of the given keys with each key
kmapped tof(k).- Return type:
pcollections.ldict
- like_azpath(obj)#
Detects whether an input can be converted into an
AzureBlobPathobject.like_azpath(obj)returnsTrueif obj is an instance of theAzureBlobPathclass or is a string that forms a valid Azure path, andFalseotherwise.See also:
is_azpath- Parameters:
obj (object) – The object whose ability to be converted into an
AzureBlobPathinstance is to be determined.- Returns:
Trueif obj is an instance ofAzureBlobPathor is a string that could be converted into anAzureBlobPathandFalseotherwise.- Return type:
boolean
- like_filepath(obj)#
Detects whether an input can be converted into a
Pathobject.like_filepath(obj)returnsTrueif obj is an instance of thePathclass or is a string that forms a valid path, andFalseotherwise. Most strings are at least theoretically valid paths, but any string that starts with a sheme followed by'://'must have the'file'scheme.See also:
is_filepath- Parameters:
obj (object) – The object whose ability to be converted into a
Pathinstance is to be determined.- Returns:
Trueif obj is an instance ofAzureBlobPathor is a string that could be converted into anAzureBlobPathandFalseotherwise.- Return type:
boolean
- like_gspath(obj)#
Detects whether the input can be converted into an
GSPathobject.like_gspath(obj)returnsTrueif obj is an instance of theGSPathclass or is a string that forms a valid GS path, andFalseotherwise.See also:
is_gspath- Parameters:
obj (object) – The object whose ability to be converted into an
GSPathinstance is to be determined.- Returns:
Trueif obj is an instance ofGSPathor is a string that could be converted into anGSPathandFalseotherwise.- Return type:
boolean
- like_number(obj, /)#
Determines whether the argument holds a scalar number value or not.
like_number(x)returnsTrueifxis already a scalar number, ifxis a single-element numpy array or tensor, or ifxis a sequence or set that has only one numerical element; otherwise, it returnsFalse.If
like_number(x)returnsTrue, thento_number(x)will always return a valid Python number (i.e., an object of typenumbers.Number).
- like_osfpath(obj)#
Detects whether the input can be converted into an
OSFPathobject.like_osfpath(obj)returnsTrueifobjis an instance of theOSFPathclass or is a string that forms a valid OSF path, andFalseotherwise.See also:
is_osfpath- Parameters:
obj (object) – The object whose ability to be converted into an
OSFPathinstance is to be determined.- Returns:
Trueifobjis an instance ofOSFPathor is a string that could be converted into anOSFPathandFalseotherwise.- Return type:
boolean
- like_path(p)#
Detects whether an object is either like a
PathorCloudPathobject.Both
PathandCloudPathobject abstractly represent paths, but they do not share a subclass.like_pathtests whether an object’s type is a subclass of any of path types recognized byimmlibor is a string or bytes object that could be converted into a path. Additional path types can be registered by addingimmlib.pathlib.PathTypeRecordinstances to theimmlib.pathlib.pathtypesdictionary. The key for such a record should be the string prefix for the path type (such as's3'for anS3Pathtype).- Parameters:
p (object) – The object whose quality as a path-like object is to be assessed.
- Returns:
Trueif p has a type that is recognized byimmlibas a path type or is an object that can be converted into a path type andFalseotherwise.- Return type:
boolean
- like_s3path(obj)#
Detects whether the input can be converted into an
S3Pathobject.like_s3path(obj)returnsTrueifobjis an instance of theS3Pathclass or is a string that forms a valid S3 path, andFalseotherwise.See also:
is_s3path- Parameters:
obj (object) – The object whose ability to be converted into an
S3Pathinstance is to be determined.- Returns:
Trueifobjis an instance ofS3Pathor is a string that could be converted into anS3PathandFalseotherwise.- Return type:
boolean
- like_unit(obj, /, *, ureg=Ellipsis)#
Returns
Trueif obj is or names apint.UnitandFalseotherwise.like_unit(obj)returnsTrueif obj is apint.Unitobject or a string that names apint.UnitandFalseotherwise.- Parameters:
obj (object) – The object whose quality as a
pint.Unitis to be assessed.ureg (pint.UnitRegistry, Ellipsis, or None, optional) – The
pint.UnitRegistryobject to use. IfNone, then any registry is allowed but an exception is raised if obj is a string because there is no registry in which to look it up. IfEllipsis(the default), then theimmlib.unitsregistry is required.
- Returns:
Trueif obj is apint.Unitor a string naming such a unit andFalseotherwise.- Return type:
bool
- mag(obj, /, unit=Ellipsis, *, strict=False)#
Returns the magnitude of the given object.
mag(quantity)returns the magnitude of the given quantity, regardless of the quantity’s unit.mag(obj), for a non-quantity object obj, simply returns obj.mag(arg, unit)returnsarg.m_as(unit)ifargis a quantity and returnsargitself ifargis not a quantity.mag(arg, Ellipsis)is equivalent tomag(arg).mag(obj, None)returns obj if it is not apint.Quantityand raises an exception if obj is apint.Quantity.If
mag(quantity, unit)is given a quantity not compatible with the given unit, then an error is raised.Note that if the first argument to
mag()is not a quantity, then the unit argument is always ignored, and the first argument is returned as-is. This behavior can be changed using the strict option.- Parameters:
obj (object) – The object that is to be converted into a magnitude.
unit (unit-like, None, or Ellipsis, optional) – The unit in which the magnitude of the argument obj should be returned. The default argument of
Ellipsisindicates that the value’s native unit, if any, should be used. A value ofNoneindicates that the obj must have no units (i.e., not be a quantity), otherwise an exception is raised.strict (bool, optional) – Whether strict matching of the unit is performed. If
False(the default), then a non-quantity (such as a plain NumPy array) is treated as a quantity whose unit is the type passed in the unit parameter; ifTrue, then obj must be compatible with the unit parameter or an error is raised.
- Returns:
The magnitude of obj in the requested unit, if obj is a quantity, or obj itself, if it is not a quantity.
- Return type:
object
- Raises:
DimensionalityError – If the given obj is a quantity whose unit is not compatible with the unit parameter.
ValueError – If unit is None but obj is a quantity or if a unit is requested of a non-quantity with the strict option enabled.
- merge(*args, **kwargs)#
Merges dict-like objects left-to-right. See also
rmerge.merge(...)collapses all arguments, which must beMappingobjects of some kind (dict,pdict,ldict, or a similar type), into a single mapping from left-to-right (i.e., with values in dictionaries to the right in the argument list overwriting values to the left in the argument list). The mapping that is returned depends on the inputs: if any of the input mappings areldictobjects, then anldictis returned (and the laziness of arguments is respected); otherwise, apdictobject is retuend.Named arguments may be passed after the dictionaries; these are collectively considered equivalent to one additional dictionary argument to the right of the positional mapping arguments.
- Parameters:
args – A sequence of
collections.abc.Mappingobjects such asdictobjects.kwargs – Additional key-value pairs that are merged into the result last.
- Returns:
A dictionary that represents the merger of all given dictionaries and key-value pairs. If any of the arguments are lazy dictionaries (
pcollections.ldict) then the return value is also lazy in order to respect the laziness of the arguments.- Return type:
pcollections.pdict or pcollections.ldict
See also
rmergeMerges dictionaries from right to left.
- nestget(d, /, *args, **kwargs)#
Returns a value from a data structure of nested mappings and sequences.
The
nestgetfunction is essentially a nested version of thegetfunction that works for bothMappingandSequencetypes (e.g.,dict,list,tuple, and related types that implement their abstract bases).nestget(data, k1, k2, k3...)extracts elementk1fromdatathen elementk2from that value, then elementk3from that value, etc., until there are no more keys; the final value is returned. If any of the values are missing, then the optional valuedefaultis returned if it is provided and an error is raised if it is not. Note that the provided keys may be integer indices for list-like objects that may be included in the nesting. If a string key is given for a list-like container, then this is treated as a missing key, not an error.This function raises a
KeyError` when a key or index is not found in the relevant container, but this behavior can be changed by passing the optional named parameter ``default. Ifdefaultis provided, then this value is returned if any keys are missing.- Parameters:
d (object) – The dict-like or list-like object from which an element is being extracted.
args – The list of keys and indices to be extracted.
kwargs – The default value to be returned if an item is not found can be specified using the named option
default. If this option is not provided, then an error is raised should the item not be found.
- Returns:
The object found at the given nested position in the data structure d. If one of the provided keys does not exist in the associated sub-collection of d, then the
defaultoption is returned.- Return type:
object
- Raises:
KeyError – If the given sequence of keys cannot be found in the nested data structure and no
defaultoption is provided.
- class numapi(fn)#
An interface for defining functions that expect all arguments to be either numpy arrays or pytorch tensors.
A function decorated with
@numapiis a placeholder for two subfunctions: one that is called when any of the arguments are pytorch tensors (all of whose arguments, when possible, are converted into pytorch tensors), and a version called otherwise, all of whose arguments are converted into numpy arrays when possible. The body of the decorated function is usuallypass, but, if desired, it can return either the pytorch or the numpy modules to indicate that a particular version of the function should be called (if necessary, tensors are converted into numpy arrays for this).Once a function has been decorated with
@numapi, that function should be used to decorate two other functions. If, for example, the functionfis decorated with@numapi, then@f.arrayshould be used to decorate the version of the function that accepts numpy arrays and@f.tensorshould be used to decorate the version of the function that accepts pytorch tensors.Examples
>>> @numapi ... def l2_distance(pt1, pt2): ... "Calculates the L2 distance between two points." ... pass
>>> @l2_distance.array ... def _(pt1, pt2): ... return np.sqrt(np.sum((pt1 - pt2)**2, axis=0))
>>> @l2_distance.tensor ... def _(pt1, pt2): ... return torch.sqrt(torch.sum((pt1 - pt2)**2, axis=0))
>>> l2_distance(torch.tensor([0,0]), [0,1]) tensor(1.)
>>> l2_distance([0,0], torch.tensor([0,1])) tensor(1.)
>>> l2_distance([0,0], [0,1]) 1.0
- numeric_args(fn=None, /, *args)#
Converts arguments of the decorated function into either NumPy arrays or PyTorch tensors.
The decorator
@numeric_args, when applied to a function, will convert all of that function’s arguments into numeric collections–either PyTorch tensors or NumPy arrays–prior to invoking the function. Either all arguments are converted into either NumPy arrays or all arguments are converted into PyTorch tensors; the former only occurs when no PyTorch tensors occur in the argument list.numeric_argsconsiderspint.Quantityobjects whose magnitudes are numeric collections to be numeric collections and will convert arguments that are quantitites into new quantities with numeric magnitudes if necessary.If a function is decorated with
@numeric_args('arg1', 'arg2' ...)then only the arguments whose names are given (arg1,arg2, …) are converted into numeric collections.When arguments are converted into PyTorch tensors, the first object in the argument list that is already a tensor is found and its device is used as the device for all converted objects. If no such object is found, then
Noneis used for the device.
- osfpath(obj, *args, client=None, cache_path=Ellipsis, file_cache_mode=Ellipsis, mkdir_mode=Ellipsis, pagesize=Ellipsis, local_cache_dir=None)#
Creates and returns an
OSFPathrepresenting an OSF.io repository.osfpath(p)creates and returns anOSFPathobject, which is a type ofcloudpathlib.CloudPathobject, from the path or path-stringp. Ifpis anOSFPath, then it is returned as-is. Otherwisestr(p)is converted into anOSFPath;str(p)may start with'osf://'(not case-sensitive) or, if it does not have a scheme specifier,'osf://'will be prepended to it.osfpath(p, a1, a2...)convertspinto anOSFPaththen joins thea1,a2, etc. values to the end of the path and returns the joined path.OSF paths take the format
'osf://<project-ID>:<storage>/<path>'where the storage is optional (defaulting to'osfstorage') and an empty path refers to the project storage’s root. The project-ID is the code used to find the project online. For example, the webpage reached at the websitehttps://osf.io/tery8/is the project page for the project whose ID istery8.If any of the optional keyword arguments are given, then the returned path will always use the specified options; a new path is returned with updated options if necessary; this is done before joining paths if multiple positional arguments are given. If the
clientkeyword is given, then it is modified by the keyword options before being used in the path. All optional keyword arguments have a default value ofEllipsis, which indicates that the value of theclientfor that option should be used.- Parameters:
obj (path-like) – The path or path-like object to convert into an OSFPath, typically a string.
client (OSFClient or None, optional) – The
OSFClientobject to use. TheOSFClientis responsible primarily for the caching of data locally. IfOSFClientisNone, then anOSFClientobject is created for the project using a temporary cache directory.cache_path (path-like or None, optional) – The local directory in which cache files should be stored. This option is ignored if
clientis notNone; otherwise it is passed to the created client object. The cache directory is the root cache directory for the entire OSF project.file_cache_mode (cloudpathlib.enums.FileCacheMode, optional) – How often to clear the file cache; see [cloudpathlib’s caching docs](https://cloudpathlib.drivendata.org/stable/caching/) for more information about the options in
cloudpathlib.enums.FileCacheMode.mkdir_mode (int, optional) – The mode to use when making directories in the cache. By default this is
0o775. This option is ignored if theclientoption is notNone.pagesize (int, optional) – The number of items to include in a single page when paging directory contents from the OSF server. The default is 100. This option is ignored if the
clientoption is notNone.
- path(arg0, *args, **kwargs)#
Convenience function for instantiating
Pathobjects.path(arg)returns aPath-like object that references the path given by the argumentarg. Theargis converted into a string prior to conversion into a path if it is not a path already.path(arg, *args)joins the list of arguments inargsto the path created from thearg.Optional keyword arguments may be given as well,
- pathdict(arg, all=False, filter=None, ondir=None, onfile=None)#
Returns a lazy dictionary of the nested paths beginning at the argument.
Searches a directory and all its subdirectories, lazily, for all contents, uniting them in a single nested lazy dictionary. The dictionaries in the nest represent directories whose keys are the filenames of their contents. The value of a key is another lazy dictionaries or a path object if the file is not a directory.
- Parameters:
arg (path-like) – The path from which to begin the search.
all (boolean, optional) – Whether to include hidden files (
True) or not (False) in the directory lists. The default isFalse.filter (None or function, optional) – A filter that must return either
True(indicating that the path should be included in the pathdict) orFalse(indicating that the path should not be included in the pathdict) for each path that is scanned. The default isNone, meaning that no filter is applied.ondir (function, optional) – A function to run on any path encountered during the
pathdictsearch that is a directory. Whenpathdictis given a path that is a directory, it returns a lazy dictionary whose keys are the filenames of the contents of that directory. For subdirectories, their filenames are mapped to the return value ofondir(path)wherepathis the path object for the subdirectory. By default this ispathdictitself, resulting in a nested structure for subdirectories. Theall,filter,ondir, andonfileparameters are all passed to this function.onfile (function, optional) – A function to run on any path encountered during the
pathdictsearch that is a file. Whenpathdictis given a path that is a directory, it returns a lazy dictionary whose keys are the filenames of the contents of that directory. For files in the directory, their filenames are mapped to the return value ofonfile(path)wherepathis the path object for the file. By default this isNone, indicating that the path itself should be returned.
- Returns:
A lazy dictionary of the contents of the argument.
- Return type:
ldict
- pathstr(obj)#
Returns a string or bytes representation of a path.
pathstr(obj)returnsobjitself ifobjis either astrorbytesobject. Ifobjis aCloudPathobject, thenstr(obj)is returned. Otherwise, Ifobjis aPathLikeobject, thenos.fspath(obj)is returned.
- pdictmap(f, keys, /, *args, **kw)#
Returns a
pdictwith the given keys and the valuesmap(f, keys).pdictmap(f, keys)returns apdictobject whose keys are the elements ofiter(keys)and whose values are the elements ofmap(f, keys).pdictmap(f, keys, *args, **kw)returns a dict object whose keys are the elements ofiter(keys)and whose values are the elements of[f(k, *args, **kw) for k in iter(keys)].- Parameters:
f (function) – The function used to create the values in the new dictionary; it must accept one arguments (
f(k)) plus any additional arguments provided in*argsand**kwargs.keys (iterable) – An iterable object whose values are to become the keys of the new dictionary.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
A persistent dictionary of the given keys with each key
kmapped tof(k).- Return type:
pcollections.pdict
- class plan(*args, **kwargs)#
Represents a directed acyclic graph of calculations.
The
planclass encapsulates individual functions that require parameters as inputs and produce outputs in the form of named values. Plan objects can be called as functions with a dictionary and/or a keyword arguments providing the plan’s parameters; they always return a type of lazy dictionary called aplandictof the values they calculate, even if they calculate only a single value.Superficially, a
planis apdictobject whose values must all becalcobjects. However, under the hood, everyplanobject maintains a directed acyclic graph of dependencies of the inputs and outputs of the calculation objects such that it can createplandictobjects that reify the outputs of the various calculations lazily.The keys that are used in a plan must be strings but are not otherwise restricted.
For a plan
p = plan(calc_key1=calc1, calc_key2=calc2, ...), aplandictcan be instantiated using the following syntax:pd = p(param1=val1, param2=val2, ...)
This
plandictis an enhancedldictthat evaluates components of the plan as requested based on laziness requirements of the calculations in the plan and on dictionary lookups of plan outputs. (ldictis the lazy dictionary type from thepcollectionslibrary.)All plans implicitly contains the parameter
'cache_path'with the default value ofNone. This parameter is used by the plan’splandictobjects, to cache the outputs of calculations that were constructed with the optionpathcache=True.- inputs#
A pset of the input parameter names, as defined by the plan’s calculations. Note that the union of the inputs and the outputs is equivalent to the keys in any plan-dictionary.
- Type:
pset of strs
- outputs#
A pset of the output parameter names, as defined by the plan’s calculations. Note that the union of the inputs and the outputs is equivalent to the keys in any plan-dictionary.
- Type:
pset of strs
- defaults#
A dictionary whose keys consist of a subset of the inputs to the plan and whose values are the default values those parameters should take if they are not provided explicitly to the plan.
- Type:
pdict
- calcs#
A persistent dictionary whose keys are the names of the various calculations in the plan and whose values are the calculation objects themselves.
- Type:
pdict
- input_docs#
A dictionary whose keys are input parameter names and whose values are the combined documentation for the associated parameter across all calculations in the plan.
- Type:
pdict
- output_docs#
A dictionary whose keys are output value names and whose values are the combined documentation for the associated outputs across all calculations in the plan.
- Type:
pdict
- requirements#
A
psetof the names of the required calculations of the plan (i.e., those with optionlazy=False).- Type:
pset
- __doc__#
Every
planobject is given a set of documentation which includes sections for the inputs and outputs as well as a listing of all the calculation steps.- Type:
str
- class CalcData(names, calcs, args, sources, index)#
- args#
Alias for field number 2
- calcs#
Alias for field number 1
- index#
Alias for field number 4
- names#
Alias for field number 0
- sources#
Alias for field number 3
- filtercall(*args, **kwargs)[source]#
Calls the plan object, but filters out args that aren’t in the plan.
plan_obj.filtercall(dict1, dict2, ..., k1=v1, k2=v2, ...)is equivalent toplan_obj(dict1, dict2, ..., k1=v1, k2=v2, ...)except that any keys in the argument list tofiltercallthat aren’t in the parameter list ofplan_objare automatically filtered out.
- class plandict(*args, **kwargs)#
A persistent dict type that manages the outputs of executing a plan.
plandict(plan, params)instantiates a plan object with the given dict-like object of parameters,params.plandict(plan, params, k1=v1, k2=v2, ...)additional merges all keyword arguments into parameters.plandict(plan, k1=v1, k2=v2, ...)uses only the keyword arguments as the plan parameters.Note that
plandict(plan, args...)is equivalent toplan(args...).plandictis a subclass oflazydict, but it has some unique behavior, primarily in that only the parameters of aplandictmay be updated; the rest of the items are consequences of the plan and parameter.- Parameters:
plan (plan) – The
planobject that is to be instantiated.*params (dict-like, optional) – The dict-like object of the parameters of the
plan. All and onlyplanparameters must be provided, after theparamsargument is merged with thekwargsoptions. This may be alazydict, and this dict’s laziness is respected as much as possible.**kwargs (optional keywords) – Optional keywords that are merged into
paramsto form the set of parameters for the plan.
- plan#
The plan object on which this plandict is based or alternatively a
plandictor object to copy.- Type:
- inputs#
The parameters that fulfill the plan. Note that these are the only keys in the
plandictthat can be updated using methods likesetandsetdefault.- Type:
pdict
- delete(k)[source]#
Returns a copy of the mersistent mapping the excludes the given key.
If the key is not found in the mapping, a KeyError is raised.
- class planobject(*args, **kwargs)#
Base class for objects that are based on lazy calculation plans.
planobjectis the base-class for all objects that useimmlib.planobjects as their base type. Objects that inherit fromplanobject(which uses metaclassplantype) are defined in the same way that calculation plans are defined. Any attributes of the class (including those inherited from base classes) that are calculations (seeimmlib.calcandimmlib.plan) are turned into a plan. The inputs of the plan are the required parameters for the class, and the outputs of the plan become the attributes of the class, which are resolved lazily like inplandicts.The
__init__function of aplanobjectis special. During the__init__function only, the parameters of aplanobjectfunction can be set using the usualsetattrinterface. Allplanobject``s are immutable once they have been initialized, however. At the end of the ``__init__function, the object must have all of its parameters set, otherwise an error is raised. If no__init__function is defined, then theplanobjectdefault init function callsmergeon its arguments and keywords; the resulting dict must be a dictionary of the class’s parameters.planobjecttypes must not overload the following methods, as they are used by theplanobject/plantypesystem. These are: *__new__*__setattr__*__getattr__*__dir__
- class plantype(name, bases, attrs, **kwargs)#
A metaclass that allows one to create lazy types from calculation plans.
The
plantypemetaclass handles classes with the base-classplanobject. In general, one should create a plan-object by inheriting fromplanobject, not by providing theplantypemetaclass, but passingplantypehas the same effect (all classes created with metaclassplantypewill inherit fromplanobject).See
planobjectfor more information.- class planobject_base(*args, **kwargs)[source]#
The base-class for the
immlib.planobjectclass.plantype.planobject_baseis a simple class that implements the basic features of theplanobjectclass. The separation for certain methods from theplanobjecttype itself is required due to details of how theplanobjectclass, which has theplantypemeta-class, gets initialized while theplantype.__new__method depends on methods in theplanobjectclass (which hasn’t been initialized/defined at the time that theplanobject.__new__method is called. This class shouldn’t be used directly and shouldn’t be inherited. Use theplanobjectclass instead.
- promote(*args, ureg=None)#
Promotes all arguments into quantities with compatible magnitudes.
promote(a, b, c...)converts all of the passed arguments into numerical quantity objects and returns them as a list. The returned arguments will all have compatible (promoted) types or magnitude types.Promotion is determined based on the object type. If any of the objects are PyTorch tensors or quantities with tensor magnitudes, then all of the returned quantities will have for their magnitudes PyTorch tensors with the same profile (e.g, device) as the tensor argument(s). Otherwise, the returned quantities will be converted into array types. The purpose of this promotion is to ensure that all arguments can be combined into an expression (PyTorch tensor operations generally require that all arguments are tensors).
- Parameters:
args – The arguments that are to be promoted.
ureg (pint.UnitRegistry or None, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to apint.UnitRegistryis performed.
- Returns:
A list of the arguments after each has been promoted.
- Return type:
list of tensors or arrays
- quant(mag, /, unit=Ellipsis, *, ureg=None)#
Returns a
pint.Quantityobject with the given magnitude and unit.quant(mag, unit)returns apint.Quantityobject with the given magnitude mag and unit. If mag is alreaady apint.Quantity, then it is converted into the given units and returned (a copy of mag is made only if necessary); if the units of mag in this case are not compatible with unit, then an error is raised. If mag is not a quantity, then the given unit is used to create the quantity.quant(mag)is equivalent toquant(mag, Ellipsis). Both return mag if mag is already apint.Quantityobject; otherwise they return a quantity with dimensionless units.Warning
The value
unit=Noneis not equivalent tounit='dimensionless'; rather,unit=Noneis used throughout immlib to indicate a non-quantity such as a plain PyTorch tensor or a NumPy array. Accordingly, an exception is raised whenunit=Noneis given.- Parameters:
mag (object) – The magnitude to be given a unit.
unit (unit-like, Ellipsis, optional) – The units to use in the returned quantity. If
Ellipsisis given (the default), then dimensionless units are assumed unless the mag argument already is a quantity with its own units. IfNoneis given, then an exception is raised.ureg (pint.UnitRegistry, None, Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to apint.UnitRegistryis performed, andimmlib.unitsis used when a unit name is given.
- Returns:
A quantity object representing the given magnitude and unit.
- Return type:
pint.Quantity
- Raises:
ValueError – If unit is
None.
- reload_immlib()[source]#
Reload and return the entire
immlibpackage.immlib.reload_immlib()reloads every submodule in theimmlibpackage then reloadsimmlibitself, and returns the reloaded package.Warning
This function exists primarily for debugging purposes; its use is not generally needed or advised by users of the library.
- Returns:
The newly reloaded
immlibmodule.- Return type:
module
Examples
>>> import immlib as il >>> il.units = None # This will break parts of the library. >>> il = il.reload_immlib() # But this resets it. >>> il.units is not None True
- rmerge(*args, **kwargs)#
Merges dictionary objects right-to-left. See also
merge.rmerge(...)collapses all arguments, which must be pythonMappingobjects of some kind, into a single mapping from right-to-left. The mapping that is returned depends on the inputs: if any of the input mappings are lazydict objects, then a lazydict is returned (and the laziness of arguments is respected); otherwise, a frozendict object is retuend.Named arguments may be passed after the dictionaries; these are collectively considered equivalent to one additional dictionary argument to the right of the positional mapping arguments.
Note
The
rmergefunction is identical to themergefunction but with reversed arguments. In other words,merge(*args, **kw)is equivalent tormerge(kw, **reversed(args)).- Parameters:
args – A sequence of
collections.abc.Mappingobjects such asdictobjectss.kwargs – Additional key-value pairs that are merged into the result first.
- Returns:
A dictionary that represents the merger of all given dictionaries and key-value pairs. If any of the arguments are lazy dictionaries (
pcollections.ldict) then the return value is also lazy in order to respect the laziness of the arguments.- Return type:
pcollections.pdict or pcollections.ldict
See also
mergeMerges dictionaries from left to right.
- s3path(obj, *args, **kwargs)#
Creates and returns an
S3Pathrepresenting an AWS S3 repository.s3path(p)creates and returns anS3Pathobject, which is a type ofcloudpathlib.CloudPathobject, from the path or path-stringp. Ifpis anS3Path, then it is returned as-is. Otherwisepathstr(p)is converted into anS3Path;pathstr(p)may start with's3://'(not case-sensitive) or, if it does not have a scheme specifier,'s3://'will be prepended to it.s3path(p, a1, a2...)convertspinto anS3Paththen joins thea1,a2, etc. values to the end of the path and returns the joined path.The
s3pathfunction accepts all the optional arguments of theS3Clienttype fromcloudpathlibas well as theclientoption. If theclientoption is given along with additional optional arguments, then the optional arguments are ignored.Additionally,
s3pathparses the optioncache_path, which is not normally accepted byS3Path, which instead requires the optionlocal_cache_dir. Any time that alocal_cache_diris given, it overrides thecache_path; however, iflocal_cache_diris not given andcache_pathis, then the directoryos.path.join(cache_path, "s3")is given as thelocal_cache_diroption.
- sparse_find(arr, /)#
Returns the indices and values of nonzero elements of a sparse object.
sparse_find(sp_array)is equivalent toscipy.sparse.find(sp_array)for a sparse arraysp_array.sparse_find(sp_tensor)is equivalent tos.indices() + (s.values(),)for a sparse PyTorch tensorsp_tensorand a version of it that has been coalesced,s = sp_tensor.coalesce(). Note that thes.values()tensor is cloned and detached before being returned.sparse_find(q)for a quantityqreturns the equivalent ofsparse_find(q.m)except that the returned value array will have the same magnitude asq.- Raises:
TypeError – If arr is not a sparse array or sparse tensor.
See also
sparse_data,sparse_indices
- strcmp(a, b, /, case=True, *, unicode=None, strip=False, split=False)#
Determines if the given objects are strings and compares them if so.
strcmp(a, b)returnsNoneif either a or b is not a string; otherwise, it returns-1,0, or1if a is less than, equal to, or greater than b, respectively, subject to the constraints of the parameters.- Parameters:
a (object) – The first argument.
b (object) – The second argument.
case (bool, optional) – Whether to perform case-sensitive (
case=True) or case-insensitive (case=False) string comparison. The default isFalse.unicode (bool or None, optional) – Whether to run unicode normalization on a and b prior to comparison. By default, this is
None, which is interpreted as a True value when case isFalseand asFalsevalue when case isTrue. In other words, unicode normalization is performed when case-insensitive comparison is being performed but not when standard string comparison is being performed. Unicode normalization is always performed both before and after casefolding. Unicode normalization is performed using theunicodedatapackage’snormalize(unicode, string)function. If this argument is a string, it is instead passed to thenormalizefunction as the first argument. When unicode is not a string but normalization is performed, them the default string is'NFD'.strip (bool, optional) – If set to
True, thena.strip()andb.strip()are used in place of a and b. If set toFalse(the default), then no stripping is performed. If a non-boolean value is given, then it is passed as an argument to thestrip()method.split (bool, optional) – If set to
True, thena.split()andb.split()are used in place of a and b. The lists of strings that result froma.split()andb.split()are rejoined with no separator prior to comparison. If this option is set to False (the default), then no splitting is performed. If a non-boolean value is given, then it is passed as an argument to thesplit()method.
- Returns:
Noneif either a is not a string or b is not a string; otherwise,-1if a is lexicographically less than b,0ifa == b, and1if a is lexicographically greater than b, subject to the constraints of the optional parameters.- Return type:
bool or None
- strends(a, b, /, case=True, *, unicode=None, strip=False)#
Determines whether or not the string a ends with the string b.
strends(a, b)returnsTrueif a and b are both strings and if a ends with b, subject to the constraints of the parameters.- Parameters:
case (bool, optional) – Whether to perform case-sensitive (
case=True) or case-insensitive (case=False) string comparison. The default isFalse.unicode (bool or None, optional) – Whether to run unicode normalization on a and b prior to comparison. By default, this is
None, which is interpreted as a True value when case isFalseand asFalsevalue when case isTrue. In other words, unicode normalization is performed when case-insensitive comparison is being performed but not when standard string comparison is being performed. Unicode normalization is always performed both before and after casefolding. Unicode normalization is performed using theunicodedatapackage’snormalize(unicode, string)function. If this argument is a string, it is instead passed to thenormalizefunction as the first argument. When unicode is not a string but normalization is performed, them the default string is'NFD'.strip (bool, optional) – If set to
True, thena.strip()andb.strip()are used in place of a and b. If set toFalse(the default), then no stripping is performed. If a non-boolean value is given, then it is passed as an argument to thestrip()method.
- Returns:
If a and b are both strings then
Trueis returned if a ends with b andFalseis returned otherwise. If either a or b is not a string, thenNoneis returned.- Return type:
bool or None
- streq(a, b, /, case=True, *, unicode=None, strip=False, split=False)#
Determines if the given objects are equal strings or not.
streq(a, b)returnsTrueif a and b are both strings and are equal to each other, subject to the constraints of the options.- Parameters:
a (object) – The first argument.
b (object) – The second argument.
case (bool, optional) – Whether to perform case-sensitive (
case=True) or case-insensitive (case=False) string comparison. The default isFalse.unicode (bool or None, optional) – Whether to run unicode normalization on a and b prior to comparison. By default, this is
None, which is interpreted as a True value when case isFalseand asFalsevalue when case isTrue. In other words, unicode normalization is performed when case-insensitive comparison is being performed but not when standard string comparison is being performed. Unicode normalization is always performed both before and after casefolding. Unicode normalization is performed using theunicodedatapackage’snormalize(unicode, string)function. If this argument is a string, it is instead passed to thenormalizefunction as the first argument. When unicode is not a string but normalization is performed, them the default string is'NFD'.strip (bool, optional) – If set to
True, thena.strip()andb.strip()are used in place of a and b. If set toFalse(the default), then no stripping is performed. If a non-boolean value is given, then it is passed as an argument to thestrip()method.split (bool, optional) – If set to
True, thena.split()andb.split()are used in place of a and b. The lists of strings that result froma.split()andb.split()are rejoined with no separator prior to comparison. If this option is set to False (the default), then no splitting is performed. If a non-boolean value is given, then it is passed as an argument to thesplit()method.
- Returns:
If a and b are both strings then
Trueis returned if a equals b andFalseis returned otherwise. If either a or b is not a string, thenNoneis returned.- Return type:
bool or None
- striskey(s)#
Determines if the given string is a valid keyword.
strissym(s)returnsTrueif s is both a string and a valid keyword (such as'if'or'while'). Otherwise, it returnsFalseif s is a string andNoneif not.
- strissym(s)#
Determines if the given string is a valid symbol (identifier).
strissym(s)returnsTrueif s is both a string and a valid identifier. Otherwise, it returnsFalseif s is a string andNoneif not.
- strisvar(s)#
Determines if the given string is a valid variable name.
strissym(s)returnsTrueif s is both a string and a valid name (i.e., a symbol but not a keyword). Otherwise, it returnsFalseif s is a string andNoneif not.
- strnorm(s, /, case=False, *, unicode=True)#
Normalizes a string using the
unicodedatapackage.strnorm(s)returns a version of s that has been unicode-normalized using theunicodedata.normalize(s)function. Case-normalization can also be requested via the case option.- Parameters:
s (object) – The string to be normalized.
case (bool, optional) – Whether to perform case-normalization (
case=True) or not (case=False, the default). If two strings are case-normalized, then an equality comparison will reveal whether the original (unnormalized strings) were equal up to the case of the characters. Case normalization is performed using thestr.casefold()method.unicode (bool or str, optional) – Whether to perform unicode normalization via the
unicodedata.normalizefunction. The default behavior (unicode=True) is to perform normalization, but this can be disabled withunicode=False. Alternatively, a string may be given, in which case it is passed to theunicodedata.normalizefunction as the first argument; when unicode isTrue, the string used is'NFD'.
- Returns:
A normalized version of s.
- Return type:
str
- strstarts(a, b, /, case=True, *, unicode=None, strip=False)#
Determines whether or not the string a starts with the string b.
strstarts(a, b)returnsTrueif a and b are both strings and if a starts with b, subject to the constraints of the parameters.- Parameters:
case (bool, optional) – Whether to perform case-sensitive (
case=True) or case-insensitive (case=False) string comparison. The default isFalse.unicode (bool or None, optional) – Whether to run unicode normalization on a and b prior to comparison. By default, this is
None, which is interpreted as a True value when case isFalseand asFalsevalue when case isTrue. In other words, unicode normalization is performed when case-insensitive comparison is being performed but not when standard string comparison is being performed. Unicode normalization is always performed both before and after casefolding. Unicode normalization is performed using theunicodedatapackage’snormalize(unicode, string)function. If this argument is a string, it is instead passed to thenormalizefunction as the first argument. When unicode is not a string but normalization is performed, them the default string is'NFD'.strip (bool, optional) – If set to
True, thena.strip()andb.strip()are used in place of a and b. If set toFalse(the default), then no stripping is performed. If a non-boolean value is given, then it is passed as an argument to thestrip()method.
- Returns:
True` if `a` and `b` are both strings and if `a` startss with `b`, subject to the constraints of the optional parameters. If either `a` or `b` is not a string, then ``Noneis returned.- Return type:
bool or None
- tensor_args(fn=None, /, *args, keep_arrays=False)#
Converts arguments of the decorated function into PyTorch tensors.
The decorator
@tensor_args, when applied to a function, will convert all of that function’s arguments into PyTorch tensors prior to invoking the function.tensor_argsconsiderspint.Quantityobjects whose magnitudes are tensors to be tensors and will convert arguments that are quantitites into new quantities with tensor magnitudes.If a function is decorated with
@tensor_args('arg1', 'arg2' ...)then only the arguments whose names are given (arg1,arg2, …) are converted into tensors.When arguments are converted into PyTorch tensors, the first object in the argument list that is already a tensor is found and its device is used as the device for all converted objects. If no such object is found, then
Noneis used for the device.The optional argument keep_arrays (default:
False) can be set toTrueto indicate that the function should convert tensor return values back into NumPy arrays if none of the arguments to the function were originally tensors. This allows a function to be written using one numerical interface (PyTorch) but to work for either PyTorch tensors or NumPy arrays while returning values whose types match the input types.
- to_array(obj, /, dtype=None, *, order=None, copy=False, sparse=None, frozen=None, quant=None, ureg=None, unit=Ellipsis, detach=True)#
Reinterprets obj as a NumPy array or quantity with an array magnitude.
immlib.to_arrayis roughly equivalent to thenumpy.asarrayfunction with a few exceptions:to_array(obj)allows quantities for obj and, in such a case, will return a quantity whose magnitude has been reinterpreted as an array, though this behavior can be altered with the quant parameter;to_array(obj)can extract thenumpyarray fromtorchtensor objects.
- Parameters:
obj (object) – The object that is to be reinterpreted as, or if necessary covnerted to, a NumPy array object.
dtype (data-type, optional) – The dtype that is passed to
numpy.asarray().order ({'C', 'F'}, optional) – The array order that is passed to
numpy.asarray().copy (boolean, optional) – Whether to copy the data in obj or not. If
False, then obj is only copied if doing so is required by the optional parameters. IfTrue, then obj is always copied if possible.sparse (bool, 'csr', 'coo', or None, optional) – If
None, then the sparsity of obj is the same as the sparsity of the array that is returned. Otherwise, the return value will always be either ascipy.spasematrix (sparse=True) or anumpy.ndarray(sparse=False) based on the given value of sparse. The sparse parameter may also be set to'bsr','coo','csc','csr','dia', or'dok'to return specific sparse matrix types.frozen (bool or None, optional) – Whether the return value should be read-only or not. If
None, then no changes are made to the return value; if a new array is allocated in theto_array()function call, then it is returned in a writeable form. Iffrozen=True, then the return value is always a read-only array; if obj is not already read-only, then a copy of obj is always returned in this case. Iffrozen=False, then the return-value is never read-only.quant (bool or None, optional) – Whether the return value should be a
Quantityobject wrapping the array (quant=True) or the array itself (quant=False). If quant isNone(the default) then the return value is a quantity if either obj is a quantity or an explicit unit parameter is given and is not a quantity if obj is not a quantity.ureg (pint.UnitRegistry or None, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to aUnitRegistryis performed (i.e., the same quantity class is returned).unit (unit-like, bool, or Ellipsis, optional) – The unit that should be used in the return value. When the return value of this function is a
Quantity(see the quant parameter), the returned quantity always has a unit matching the unit parameter; if the provided obj is not a quantity, then its unit is presumed to be that requested by unit. When the return value of this function is not aQuantityobject and is instead is a NumPy array object, then when obj is not a quantity the unit parameter is ignored, and when obj is a quantity, its magnitude is returned after conversion into unit. The default value of unit,Ellipsis, indicates that, if obj is a quantity, its unit should be used, and unit should be considered dimensionless otherwise.detach (bool, optional) – If the argument is a PyTorch tensor that requires gradient tracking, then it must be detached from the gradient tracking system before it can be turned into an array. If detach is
True(the default), then this detachment is performed automatically. Otherwise, an error is raised if a tensor would need to be detached.
- Returns:
Either a NumPy array equivalent to obj or a
Quantitywhose magnitude is a NumPy array equivalent to obj.- Return type:
numpy.ndarray or pint.Quantity
- Raises:
ValueError – If invalid parameter values are given or if the parameters conflict.
See also
- to_dense(obj, /, dtype=None, *, quant=None, ureg=None, unit=Ellipsis)#
Returns a dense version of the numerical object obj.
to_dense(obj)returns obj if it is already a PyTorch dense tensor or a NumPyndarrayor a quantity whose magnitude is one of these. Otherwise, it converts obj into a dense representation and returns this. Additional requirements on the output format of the return value can be added using the optional parameters.- Parameters:
obj (object) – The object that is to be converted into a dense representation.
dtype (dtype-like or None, optional) – The dtype that is passed to
torch.as_tensor(obj)ornp.asarray(obj).quant (bool or None, optional) – Whether the return value should be a
pint.Quantityobject wrapping wrapping the object (quant=True) or the object itself (quant=False). If quant isNone(the default) then the return value is a quantity if obj is a quantity and is not a quantity if obj is not a quantity.ureg (pint.UnitRegistry or None, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to apint.UnitRegistryis performed (i.e., the same quantity class is returned).unit (unit-like, bool, None, or Ellipsis, optional) – The unit that should be used in the return value. When the return value of this function is a
pint.Quantity(see the quant parameter), the returned quantity always has a unit matching the unit parameter; if the provided obj is not a quantity, then its unit is presumed to be those requested by unit. When the return value of this function is not apint.Quantityobject and is instead a numeric object, then when obj is not a quantity the unit parameter is ignored, and when obj is a quantity, its magnitude is returned after conversion into unit. The default value of unit,Ellipsis, indicates that, if obj is a quantity, its unit should be used, and unit should be considered to beNoneotherwise.
- Returns:
A dense version of the argument obj.
- Return type:
dense tensor, dense array, or quantity with a dense magnitude
- to_mcoll(obj, /, copy=True)#
Returns a mutable copy of obj.
to_mcoll(obj)returns a mutable copy of the given collection obj. If obj is already a mutable collection, then a duplicate is returned. If obj is not a collection that can be converted into a mutable collection, then an error is raised.A mutable collection, according to this function, is a Python
list,set, ordict, depending on the type of obj. When obj is aSequence, the result is alist; when obj is aSet, the result is aset; and when obj is aMapping, the result is adict.- Parameters:
obj (collection) – An object that is to be converted into a persistent collection.
copy (boolean, optional) – If obj is already a mutable collection, then a copy is made if and only if
copyisTrue; otherwise, obj is returned as-is when it is already a mutable type. The default isTrue.
- Returns:
A mutable version of obj; the return value’s type will always be one of
list,set, ordict.- Return type:
object
- Raises:
TypeError – If obj cannot be converted into a mutable collection.
- to_number(obj, /, unit=Ellipsis, *, ureg=None)#
Converts the argument into a simple Python number.
to_number(obj)returns a simple Python number representation of obj (in other words, obj will be a subtype of Python’snumbers.Numbertype). Any number, any NumPy array with only one element, and any PyTorch tensor with only one element can be converted into a scalar. If obj is apint.Quantitythen the return value is a quantity with the same unit as obj and whose magnitude isto_number(obj.m).- Parameters:
obj (object) – The object that is to be converted into a scalar number.
unit (unit-like, bool, or None, optional) – If obj is a
pint.Quantityobject, the unit parameter determines how it is handled byto_number. If unit isNoneand obj is a quantity, then an error will be raised. If unit is a validpint.Unitor the an object that can be converted into a unit vis thatimmlib.unitfunction. then an error is raised if obj is not a quantity with alike units. If unit isEllipsis(the default value), then the behavior depends on whether obj is a quantity: if obj is a quantity, theto_numberfunction is run on its magnitude and a quantity with the same unit is returned; if obj is not a quantity, then the a non-quantity is returned.ureg (pint.UnitRegistry, None, or Ellipsis, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then the registry of obj is used if obj is a quantity, andimmlib.unitsis used if not.
- Returns:
A scalar number that is an object whose class is a subtype of
numbers.Numberor apint.Quantityobject whose magnitude is such a number.- Return type:
number or pint.Quantity
- Raises:
TypeError – If the argument is not like a scalar number.
- to_numeric(obj, /, dtype=None, *, copy=False, sparse=None, quant=None, ureg=None, unit=Ellipsis)#
Reinterprets obj as a numeric type or quantity with such a magnitude.
immlib.to_numericis roughly equivalent to thetorch.as_tensorornumpy.asarrayfunction with a few exceptions:to_numeric(obj)allows quantities for obj and, in such a case, will return a quantity whose magnitude has been reinterpreted as a numeric object, though this behavior can be altered with thequantparameter;to_numeric(obj)correctly handles SciPy sparse matrices, NumPy arrays, and PyTorch tensors.
If the object obj passed to
immlib.to_numeric(obj)is a PyTorch tensor or a quantity whose magnitude is a PyTorch tensor, then a PyTorch tensor or a quantity with a PyTorch tensor magnitude is returned. Otherwise, a NumPy array, SciPy sparse matrix, or quantity with a magnitude matching one of these types is returned.- Parameters:
obj (object) – The object that is to be reinterpreted as, or if necessary covnerted to, a numeric object.
dtype (dtype-like or None, optional) – The dtype that is passed to
torch.as_tensor(obj)ornp.asarray(obj).copy (bool, optional) – Whether to copy the data in obj or not. If
False, then obj is only copied if doing so is required by the optional parameters. IfTrue, then obj is always copied if possible.sparse (bool, {'csr','csc','bsr','bsc','coo'}, or None, optional) – If
None, then the sparsity of obj is the same as the sparsity of the object that is returned. Otherwise, the return value will always be either a spase object (sparse=True) or a dense object (sparse=False) based on the given value of sparse. The sparse parameter may also be set to'coo','csr', or other sparse matrix names to return specific sparse layouts.quant (bool or None, optional) – Whether the return value should be a
pint.Quantityobject wrapping wrapping the object (quant=True) or the object itself (quant=False). If quant isNone(the default) then the return value is a quantity if obj is a quantity and is not a quantity if obj is not a quantity.ureg (pint.UnitRegistry or None, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to apint.UnitRegistryis performed (i.e., the same quantity class is returned).unit (unit-like, bool, None, or Ellipsis, optional) – The unit that should be used in the return value. When the return value of this function is a
pint.Quantity(see the quant parameter), the returned quantity always has a unit matching the unit parameter; if the provided obj is not a quantity, then its unit is presumed to be those requested by unit. When the return value of this function is not apint.Quantityobject and is instead a numeric object, then when obj is not a quantity the unit parameter is ignored, and when obj is a quantity, its magnitude is returned after conversion into unit. The default value of unit,Ellipsis, indicates that, if obj is a quantity, its unit should be used, and unit should be considered to beNoneotherwise.
- Returns:
Either a NumPy array or PyTorch tensor equivalent to obj or a
pint.Quantitywhose magnitude is such an object.- Return type:
NumPy array or PyTorch tensor or Quantity
- Raises:
ValueError – If invalid parameter values are given or if the parameters conflict.
- to_pcoll(obj)#
Returns a persistent copy of obj.
to_pcoll(obj)returns obj itself if obj is a persistent collection; otherwise, it returns a persistent copy of obj. If obj is not a collection that can be converted into a persistent collection, then an error is raised.- Parameters:
obj (collection) – An object that is to be converted into a persistent collection.
- Returns:
A persistent version of obj.
- Return type:
object
- Raises:
TypeError – If obj cannot be converted into a persistent collection.
- to_sparse(obj, /, dtype=None, *, quant=None, ureg=None, unit=Ellipsis)#
Returns a sparse version of the numerical object obj.
to_sparse(obj)returns obj if it is already a PyTorch sparse tensor or a SciPy sparse matrix or a quantity whose magnitude is one of these. Otherwise, it converts obj into a sparse representation and returns this. Additional requirements on the output format of the return value can be added using the optional parameters.- Parameters:
obj (object) – The object that is to be converted into a sparse representation.
dtype (dtype-like or None, optional) – The dtype that is passed to
torch.as_tensor(obj)ornp.asarray(obj).quant (bool or None, optional) – Whether the return value should be a
pint.Quantityobject wrapping wrapping the object (quant=True) or the object itself (quant=False). If quant isNone(the default) then the return value is a quantity if obj is a quantity and is not a quantity if obj is not a quantity.ureg (pint.UnitRegistry or None, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to apint.UnitRegistryis performed (i.e., the same quantity class is returned).unit (unit-like, bool, None, or Ellipsis, optional) – The unit that should be used in the return value. When the return value of this function is a
pint.Quantity(see the quant parameter), the returned quantity always has a unit matching the unit parameter; if the provided obj is not a quantity, then its unit is presumed to be those requested by unit. When the return value of this function is not apint.Quantityobject and is instead a numeric object, then when obj is not a quantity the unit parameter is ignored, and when obj is a quantity, its magnitude is returned after conversion into unit. The default value of unit,Ellipsis, indicates that, if obj is a quantity, its unit should be used, and unit should be considered to beNoneotherwise.
- Returns:
A sparse version of the argument obj.
- Return type:
sparse tensor, sparse array, or quantity with a sparse magnitude
- to_tcoll(obj, /, copy=True)#
Returns a transient copy of obj.
to_tcoll(obj)returns a copy of obj as a transient collection. If obj is not a collection that can be converted into a transient collection, then an error is raised.Note
If obj is already a transient collection, then a copy of obj is returned.
Note
If
to_tcollis given a lazy dict (pcollections.ldict) or a lazi list (pcollections.llist), the resulting transient dictionary is made usingobj.transient()and so respects the laziness of the elements.- Parameters:
obj (collection) – An object that is to be converted into a persistent collection.
copy (boolean, optional) – If obj is already a transient collection, then a copy is made if and only if
copyisTrue; otherwise, obj is returned as-is when it is already a transient type. The default isTrue.
- Returns:
A transient version of obj. The returned value is always either a
tlist,tset, ortdictobject.- Return type:
object
- Raises:
TypeError – If obj cannot be converted into a transient collection.
- to_tensor(obj, /, dtype=None, *, device=None, requires_grad=None, copy=False, sparse=None, quant=None, ureg=None, unit=Ellipsis)#
Reinterprets obj as a PyTorch tensor or as a
pintquantity with a tensor magnitude.immlib.to_tensoris roughly equivalent to thetorch.as_tensorfunction with a few exceptions:to_tensor(obj)allows quantities for obj and, in such a case, will return a quantity whose magnitude has been reinterpreted as a tensor, though this behavior can be altered with the quant parameter;to_tensor(obj)can convet a SciPy sparse matrix into a sparse tensor.
- Parameters:
obj (object) – The object that is to be reinterpreted as or covnerted to, a PyTorch tensor object.
dtype (dtype-like, optional) – The dtype that is passed to
torch.as_tensor(obj).device (device name or None, optional) – The device parameter that is passed to
torch.as_tensor(obj),Noneby default.requires_grad (bool or None, optional) – Whether the returned tensor should require gradient calculations or not. If
None(the default), then the objecct obj is not changed from its current gradient settings, if obj is a tensor, and obj is not made to track its gradient if it is converted into a tensor. If the requires_grad parameter does not match the given tensor’s requires_grad field, then a copy is always returned.copy (bool, optional) – Whether to copy the data in obj or not. If
False, then obj is only copied if doing so is required by the optional parameters. IfTrue, then obj is always copied if possible.sparse (bool, {'csr','csc','bsr','bsc','coo'}, or None, optional) – If
None, then the sparsity of obj is the same as the sparsity of the tensor that is returned. Otherwise, the return value will always be either a spase tensor (sparse=True) or a dense tensor (sparse=False) based on the given value ofsparse. Thesparseparameter may also be set to the name of a sparse layout in order to convert the object into that layout. Possible sparse layouts include'coo','csr','csc','bsr', and'bsc'.quant (bool or None, optional) – Whether the return value should be a
Quantityobject wrapping the array (quant=True) or the tensor itself (quant=False). If quant isNone(the default) then the return value is a quantity if either obj is a quantity or an explicit unit parameter is given and is not a quantity if obj is not a quantity.ureg (pint.UnitRegistry or None, optional) – The
pint.UnitRegistryobject to use for units. If ureg isEllipsis, thenimmlib.unitsis used. If ureg isNone(the default), then no specific coersion to apint.UnitRegistryis performed (i.e., the specific subclass ofpint.Quantityused by obj is not changed).unit (unit-like, bool, None, or Ellipsis, optional) – The unit that should be used in the return value. When the return value of this function is a
pint.Quantity(see the quant parameter), the returned quantity always has units matching the unit parameter; if the provided obj is not a quantity, then its unit is presumed to be that requested by unit. When the return value of this function is not apint.Quantityobject and is instead a PyTorch tensor object, then when obj is not a quantity the unit parameter is ignored, and when obj is a quantity, its magnitude is returned after conversion into unit. The default value of unit,Ellipsis, indicates that, if obj is a quantity, its unit should be used, and unit should be considered dimensionless otherwise.
- Returns:
Either a PyTorch tensor equivalent to obj or a
pint.Quantitywhose magnitude is a PyTorch tensor equivalent to obj.- Return type:
torch.Tensor or pint.Quantity
- Raises:
ValueError – If invalid parameter values are given or if the parameters conflict.
See also
- unit(obj, /, ureg=None)#
Converts the argument into a a
pint.Unitobject.unit(obj)returns theimmlib-library unit object for the given unit object obj (which may be from a separatepint.UnitRegistryinstance).unit(unitname)returns the unit object for the given unit name stringunitname.unit(q)returns the unit of the given quantity objectq.Note
immlibconsiders an object to be “unit-like” ifunit(obj)returns a validpint.Unitobject.- Parameters:
obj (object) – The object that is to be converted to a unit.
ureg (ping.UnitRegistry, None, or Ellipsis, optional) – The unit registry to convert the object into. If
Ellipsis, thenimmlib.unitsis used. IfNone(the default), then the unit registry for obj is used if obj is a quantity or unit already, andimmlib.unitsis used if not. Otherwise, must be a unit registry.
- Returns:
The
Unitobject associated with the given argument.- Return type:
pint.Unit
- Raises:
TypeError – When the argument cannot be converted to a
pint.Unitobject.
- url_download(url, /, destpath=None, *, mkdirs=True, mkdir_mode=509, expanduser=True)#
Returns the contents of the given URL as a byte-string.
url_download(url)returns the contents of the given url as a byte-string.url_download(url, destpath)downloads the given url to the given destination path,destpath, and returns that path on success.- Parameters:
url (str or URL) – The URL to be downloaded.
destpath (PathLike or None, optional) – A string,
pathlib.Pathobject, or any object that can be converted into aPath, which details the local destination path to which the URL should be saved. The default,None, indicates that the file should not be downloaded to a path but should instead just be returned as a byte string.mkdirs (boolean, optional) – Whether to make directories that do not exist in order to save the URL to the path destpath. The default is
True.mkdir_mode (int, optional) – The mode to give any directory created by this function. The default is
0o775. If mkdirs is set toFalse, then this option is ignored.expanduser (bool, optional) – Whether to expand the
~character into the user’s directory in the destination path. The default isTrue.
- Returns:
If destpath is
None, then abytesobject containing the URL contents is returned; otherwise, thepathlib.Pathobject representing the downloaded file is returned.- Return type:
bytes or Path
- valmap(f, d, /, *args, **kwargs)#
Returns a dictionary object whose values are transformed by a function.
valmap(f, d)returns a dict whose keys are the same as those of the given dict object and whose values, for each keykaref(d[k]).valmap(f, d, *args, **kw)additionally passes the given arguments to the function f, such that in the resulting map, each keykis mapped tof(d[k], *args, **kw).Unlike
lazyvalmap, this function returns either adict, apdict, or anldictdepending on the input argument d. If d is a (lazy)ldict, then anldictis returned; if d is apdict, apdictis returned, and otherwise, adictis returnd.- Parameters:
f (function) – The function used to create the values in the new dictionary.
d (collections.abc.Mapping) – A mapping whose keys are to be preserved and remapped to a function of their values.
args – Additional positional arguments to pass to f.
kwargs – Additional named arguments to pass to f.
- Returns:
This function always returns a dictionary object whose type is either
pcollections.pdict,pcollections.ldict, ordict, depending on the type of d.- Return type:
collections.abc.Mapping object