amisc.utils
Provides some basic utilities for the package.
Includes:
to_model_dataset
— convert surrogate input/output dataset to a form usable by the true modelto_surrogate_dataset
— convert true model input/output dataset to a form usable by the surrogateconstrained_lls
— solve a constrained linear least squares problemsearch_for_file
— search for a file in the current working directory and additional search pathsformat_inputs
— broadcast and reshape all inputs to the same shapeformat_outputs
— reshape all outputs to a common loop shapeparse_function_string
— convert function-like strings to arguments and keyword-argumentsrelative_error
— compute the relative L2 error between two vectorsget_logger
— logging utility with nice formatting
constrained_lls(A, b, C, d)
Minimize \(||Ax-b||_2\), subject to \(Cx=d\), i.e. constrained linear least squares.
Note
See these lecture notes for more detail.
PARAMETER | DESCRIPTION |
---|---|
A |
TYPE:
|
b |
TYPE:
|
C |
TYPE:
|
d |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/utils.py
format_inputs(inputs, var_shape=None)
Broadcast and reshape all inputs to the same shape. Loop shape is inferred from broadcasting the leading dims of all input arrays. Input arrays are broadcast to this shape and then flattened.
Example
PARAMETER | DESCRIPTION |
---|---|
inputs |
TYPE:
|
var_shape |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Dataset, tuple[int, ...]]
|
the reshaped inputs and the common loop shape |
Source code in src/amisc/utils.py
format_outputs(outputs, loop_shape)
Reshape all outputs to the common loop shape. Loop shape is as obtained from a call to format_inputs
.
Assumes that all outputs are the same along the first dimension. This first dimension gets reshaped back into
the loop_shape
. Singleton outputs are squeezed along the last dimension. A singleton loop shape is squeezed
along the first dimension.
Example
PARAMETER | DESCRIPTION |
---|---|
outputs |
TYPE:
|
loop_shape |
the common leading dimensions to reshape the output arrays to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dataset
|
the reshaped outputs |
Source code in src/amisc/utils.py
get_logger(name, stdout=True, log_file=None, level=logging.INFO)
Return a file/stdout logger with the given name.
PARAMETER | DESCRIPTION |
---|---|
name |
the name of the logger to return
TYPE:
|
stdout |
whether to add a stdout stream handler to the logger
TYPE:
|
log_file |
add file logging to this file (optional)
TYPE:
|
level |
the logging level to set
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Logger
|
the logger |
Source code in src/amisc/utils.py
parse_function_string(call_string)
Convert a function signature like func(a, b, key=value)
to name, args, kwargs.
PARAMETER | DESCRIPTION |
---|---|
call_string |
a function-like string to parse
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[str, list, dict]
|
the function name, positional arguments, and keyword arguments |
Source code in src/amisc/utils.py
relative_error(pred, targ, axis=None)
Compute the relative L2 error between two vectors along the given axis.
PARAMETER | DESCRIPTION |
---|---|
pred |
the predicted values
|
targ |
the target values
|
axis |
the axis along which to compute the error
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
the relative L2 error |
Source code in src/amisc/utils.py
search_for_file(filename, search_paths=None)
Search for the given filename in the current working directory and any additional search paths provided.
PARAMETER | DESCRIPTION |
---|---|
filename |
the filename to search for
TYPE:
|
search_paths |
paths to try and find the file in
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
the full path to the file if found, otherwise the original |
Source code in src/amisc/utils.py
to_model_dataset(dataset, variables, del_latent=True, **field_coords)
Convert surrogate input/output dataset to a form usable by the true model. Primarily, reconstruct field quantities and denormalize.
PARAMETER | DESCRIPTION |
---|---|
dataset |
the dataset to convert
TYPE:
|
variables |
the
TYPE:
|
del_latent |
whether to delete the latent variables from the dataset after reconstruction
TYPE:
|
field_coords |
pass in extra field qty coords as f'{var}_coords' for reconstruction (optional)
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Dataset, Dataset]
|
the reconstructed/denormalized dataset and any field coordinates used during reconstruction |
Source code in src/amisc/utils.py
to_surrogate_dataset(dataset, variables, del_fields=True, **field_coords)
Convert true model input/output dataset to a form usable by the surrogate. Primarily, compress field quantities and normalize.
PARAMETER | DESCRIPTION |
---|---|
dataset |
the dataset to convert
TYPE:
|
variables |
the
TYPE:
|
del_fields |
whether to delete the original field quantities from the dataset after compression
TYPE:
|
field_coords |
pass in extra field qty coords as f'{var}_coords' for compression (optional)
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Dataset, list[str]]
|
the compressed/normalized dataset and a list of variable names to pass to surrogate |