amisc.interpolator
Provides interpolator classes. Interpolators manage training data and specify how to refine/gather more data.
Includes:
BaseInterpolator
: Abstract class providing basic structure of an interpolatorLagrangeInterpolator
: Concrete implementation for tensor-product barycentric Lagrange interpolation
BaseInterpolator(beta, x_vars, xi=None, yi=None, model=None, model_args=(), model_kwargs=None)
Bases: ABC
Base interpolator abstract class.
Setting the training data
You can leave the training data xi
, yi
empty; they can be iteratively refined later on.
Model specification
The model is a callable function of the form ret = model(x, *args, **kwargs)
. The return value is a dictionary
of the form ret = {'y': y, 'files': files, 'cost': cost}
. In the return dictionary, you specify the raw model
output y
as an np.ndarray
at a minimum. Optionally, you can specify paths to output files and the average
model cost (in units of seconds of cpu time), and anything else you want.
ATTRIBUTE | DESCRIPTION |
---|---|
beta |
specifies the refinement level of this interpolator as a set of natural number indices
TYPE:
|
x_vars |
list of variables that fully determines the input domain of interest for interpolation
TYPE:
|
xi |
TYPE:
|
yi |
TYPE:
|
_model |
stores a ref to the model or function that is to be interpolated, callable as
TYPE:
|
_model_args |
additional arguments to supply to the model
TYPE:
|
_model_kwargs |
additional keyword arguments to supply to the model
TYPE:
|
model_cost |
the average total cpu time (in seconds) for a single model evaluation call of one set of inputs
TYPE:
|
output_files |
tracks model output files corresponding to
TYPE:
|
logger |
a logging utility reference
TYPE:
|
Construct the interpolator.
PARAMETER | DESCRIPTION |
---|---|
beta |
refinement level indices
TYPE:
|
x_vars |
list of variables to specify input domain of interpolation |
xi |
DEFAULT:
|
yi |
DEFAULT:
|
model |
callable as {'y': y} = model(x), with
DEFAULT:
|
model_args |
optional args for the model
DEFAULT:
|
model_kwargs |
optional kwargs for the model
DEFAULT:
|
Source code in src/amisc/interpolator.py
__call__(x)
abstractmethod
Evaluate the interpolation at points x
.
PARAMETER | DESCRIPTION |
---|---|
x |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/interpolator.py
_fmt_input(x)
Helper function to make sure input x
is an ndarray of shape (..., xdim)
.
PARAMETER | DESCRIPTION |
---|---|
x |
if 1d-like as (n,), then converted to 2d as (1, n) if n==xdim or (n, 1) if xdim==1
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[bool, ndarray]
|
|
Source code in src/amisc/interpolator.py
grad(x, xi=None, yi=None)
abstractmethod
Evaluate the gradient/Jacobian at points x
using the interpolator.
PARAMETER | DESCRIPTION |
---|---|
x |
TYPE:
|
xi |
TYPE:
|
yi |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/interpolator.py
hessian(x, xi=None, yi=None)
abstractmethod
Evaluate the Hessian at points x
using the interpolator.
PARAMETER | DESCRIPTION |
---|---|
x |
TYPE:
|
xi |
TYPE:
|
yi |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/interpolator.py
refine(beta, auto=True)
abstractmethod
Return a new interpolator with one dimension refined by one level, as specified by beta
.
When you want to compute the model manually
You can set auto=False
, in which case the newly refined interpolation points x
will be returned to you
along with their indices, in the form idx, x, interp = refine(beta, auto=False)
. You might also want to
do this if you did not provide a model when constructing the Interpolator (so auto=True
won't work).
PARAMETER | DESCRIPTION |
---|---|
beta |
the new refinement level indices, should only refine one dimension by one level
TYPE:
|
auto |
whether to automatically compute and store model at refinement points (default is True)
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in src/amisc/interpolator.py
save_enabled()
Return whether the underlying model wants to save outputs to file.
Note
You can specify that a model wants to save outputs to file by providing an 'output_dir'
kwarg.
Source code in src/amisc/interpolator.py
set_yi(yi=None, model=None, x_new=())
Set the training data; if yi=None
, then compute from the model.
Warning
You would use x_new
if you wanted to compute the model at these specific locations and store the result.
This will ignore anything passed in for yi
, and it assumes a model is already specified (or passed in).
Info
You can pass in integer indices for x_new
or tuple indices. Integers will index into self.xi
. Tuples
provide extra flexibility for more complicated indexing, e.g. they might specify indices along different
coordinate directions in an N-dimensional grid. If you pass in a list of tuple indices for x_new
, the
resulting model outputs will be returned back to you in the form dict[str: np.ndarray]
. The keys are
string casts of the tuple indices, and the values are the corresponding model outputs.
PARAMETER | DESCRIPTION |
---|---|
yi |
TYPE:
|
model |
callable function, optionally overrides
TYPE:
|
x_new |
tuple of
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str:ndarray] | None
|
dict[str: np.ndarray] if |
Source code in src/amisc/interpolator.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
update_input_bds(idx, bds)
Update the input bounds at the given index.
PARAMETER | DESCRIPTION |
---|---|
idx |
the index of the input variable to update
TYPE:
|
bds |
the new bounds for the variable
TYPE:
|
xdim()
LagrangeInterpolator(beta, x_vars, init_grids=True, reduced=False, **kwargs)
Bases: BaseInterpolator
Tensor-product (multivariate) grid interpolator, based on barycentric Lagrange polynomials.
Info
The refinement level indices beta
are used in this class to specify anisotropic refinement along each
coordinate direction of the input domain, so x_dim = len(beta)
.
ATTRIBUTE | DESCRIPTION |
---|---|
x_grids |
univariate Leja sequence points in each 1d dimension
TYPE:
|
weights |
the barycentric weights corresponding to
TYPE:
|
reduced |
whether to store
TYPE:
|
Initialize a Lagrange tensor-product grid interpolator.
PARAMETER | DESCRIPTION |
---|---|
beta |
refinement level indices for each input dimension
TYPE:
|
x_vars |
list of variables specifying bounds/pdfs for each input x |
init_grids |
whether to compute 1d Leja sequences on initialization
DEFAULT:
|
reduced |
whether to store xi/yi matrices, e.g. set true if storing in external sparse grid structure
DEFAULT:
|
**kwargs |
other optional arguments (see
DEFAULT:
|
Source code in src/amisc/interpolator.py
__call__(x, xi=None, yi=None)
Evaluate the barycentric interpolation at points x
.
PARAMETER | DESCRIPTION |
---|---|
x |
TYPE:
|
xi |
TYPE:
|
yi |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/interpolator.py
get_grid_sizes(beta, k=2)
staticmethod
Compute number of grid points in each input dimension.
PARAMETER | DESCRIPTION |
---|---|
beta |
refinement level indices
TYPE:
|
k |
level-to-grid-size multiplier (probably just always
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[int]
|
list of grid sizes in each dimension |
Source code in src/amisc/interpolator.py
grad(x, xi=None, yi=None)
Evaluate the gradient/Jacobian at points x
using the interpolator.
PARAMETER | DESCRIPTION |
---|---|
x |
TYPE:
|
xi |
TYPE:
|
yi |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/interpolator.py
hessian(x, xi=None, yi=None)
Evaluate the Hessian at points x
using the interpolator.
PARAMETER | DESCRIPTION |
---|---|
x |
TYPE:
|
xi |
TYPE:
|
yi |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
|
Source code in src/amisc/interpolator.py
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
|
leja_1d(N, z_bds, z_pts=None, wt_fcn=None)
staticmethod
Find the next N
points in the Leja sequence of z_pts
.
PARAMETER | DESCRIPTION |
---|---|
N |
number of new points to add to the sequence
TYPE:
|
z_bds |
bounds on the 1d domain
TYPE:
|
z_pts |
current univariate Leja sequence
TYPE:
|
wt_fcn |
weighting function, uses a constant weight if
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
the Leja sequence |
Source code in src/amisc/interpolator.py
refine(beta, auto=True, x_refine=None)
Return a new interpolator with one dimension refined by one level, specified by beta
.
Note
If self.reduced=True
or auto=False
, then this function will return tuple indices idx
corresponding
to the new interpolation points x
. The tuple indices specify one index along each input dimension.
PARAMETER | DESCRIPTION |
---|---|
beta |
the new refinement level indices
|
auto |
whether to automatically compute model at refinement points
DEFAULT:
|
x_refine |
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in src/amisc/interpolator.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|