scico.util¶
General utility functions.
Functions
|
Check if positional arguments to func are jax tracers. |
|
Get a string describing the specified device. |
|
Flexible partial function creation. |
|
Recursive version of |
|
Recursive version of |
|
Get content of a file via a URL. |
Classes
|
A wrapper class for |
|
Timer class supporting multiple independent labeled timers. |
- scico.util.rgetattr(obj, name, default=None)[source]¶
Recursive version of
getattr
.- Parameters:
- Return type:
- Returns:
Attribute value of default if attribute does not exist.
- scico.util.partial(func, indices, *fixargs, **fixkwargs)[source]¶
Flexible partial function creation.
This function is similar to
functools.partial
, but allows fixing of arbitrary positional arguments rather than just some number of trailing positional arguments.- Parameters:
func (
Callable
) – Function from which partial function is to be derived.indices (
Sequence
) – Tuple of indices of positional args of func that are to be fixed to the values specified in fixargs.*fixargs (
Any
) – Fixed values for specified positional arguments.**fixkwargs (
Any
) – Fixed values for keyword arguments.
- Return type:
- Returns:
The partial function with fixed arguments.
- scico.util.check_for_tracer(func)[source]¶
Check if positional arguments to func are jax tracers.
This is intended to be used as a decorator for functions that call external code from within SCICO. At present, external functions cannot be jit-ed or vmap/pmaped. This decorator checks for signs of jit/vmap/pmap and raises an appropriate exception.
- Return type:
- scico.util.url_get(url, maxtry=3, timeout=10)[source]¶
Get content of a file via a URL.
- Parameters:
- Return type:
- Returns:
Buffered I/O stream.
- Raises:
ValueError – If the maxtry parameter is not greater than zero.
urllib.error.URLError – If the file cannot be downloaded.
- class scico.util.Timer(labels=None, default_label='main', all_label='all')[source]¶
Bases:
object
Timer class supporting multiple independent labeled timers.
The timer is based on the relative time returned by
timeit.default_timer
.- Parameters:
- elapsed(label=None, total=True)[source]¶
Get elapsed time since timer start.
- Parameters:
label (
Optional
[str
]) – Label of the timer for which the elapsed time is required. If it isNone
, the default timer with label specified by the default_label parameter of__init__
is selected.total (
bool
) – IfTrue
return the total elapsed time since the first call ofstart
for the selected timer, otherwise return the elapsed time since the most recent call ofstart
for which there has not been a corresponding call tostop
.
- Return type:
- Returns:
Elapsed time.
- class scico.util.ContextTimer(timer=None, label=None, action='StartStop')[source]¶
Bases:
object
A wrapper class for
Timer
that enables its use as a context manager.For example, instead of
>>> t = Timer() >>> t.start() >>> x = sum(range(1000)) >>> t.stop() >>> elapsed = t.elapsed()
one can use
>>> t = Timer() >>> with ContextTimer(t): ... x = sum(range(1000)) >>> elapsed = t.elapsed()
- Parameters:
timer (
Optional
[Timer
]) – Timer object to be used as a context manager. IfNone
, a new class:Timer object is constructed.label (
Optional
[str
]) – Label of the timer to be used. If it isNone
, start the default timer.action (
str
) – Actions to be taken on context entry and exit. If the value is ‘StartStop’, start the timer on entry and stop on exit; if it is ‘StopStart’, stop the timer on entry and start it on exit.