Page MenuHomec4science

trained_model_nearest_neighbor.py
No OneTemporary

File Metadata

Created
Wed, May 8, 20:01

trained_model_nearest_neighbor.py

# Copyright (C) 2018-2020 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from rrompy.reduction_methods.base.trained_model.trained_model import (
TrainedModel)
from rrompy.utilities.numerical.compress_matrix import compressMatrix
from rrompy.utilities.base.types import Np1D, paramList, sampList
from rrompy.utilities.base import verbosityManager as vbMng
from rrompy.utilities.exception_manager import RROMPyWarning
from rrompy.sampling import emptySampleList
__all__ = ['TrainedModelNearestNeighbor']
class TrainedModelNearestNeighbor(TrainedModel):
"""
ROM approximant evaluation for nearest neighbor approximant.
Attributes:
Data: dictionary with all that can be pickled.
"""
def compress(self, collapse : bool = False, tol : float = 0., *args,
**kwargs):
if not collapse and tol <= 0.: return
RMat = self.data.projMat
if not collapse:
if hasattr(self.data, "_compressTol"):
RROMPyWarning(("Recompressing already compressed model is "
"ineffective. Aborting."))
return
self.data.projMat, RMat, _ = compressMatrix(RMat, tol, *args,
**kwargs)
for j, suppj in enumerate(self.data.supp):
self.data.vals[j] = RMat[:, suppj : suppj + len(self.data.vals[j])
].dot(self.data.vals[j])
self.data.supp[j] = [0]
super().compress(collapse, tol)
def getNearestNeighbor(self, mu : paramList = []) -> Np1D:
"""
Find nearest neighbor to arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = self.checkParameterList(mu)
idxUnique, idxmap = np.unique(self.data.NN(mu), return_inverse = True)
idxUnique = np.array(idxUnique, dtype = int)
vbMng(self, "INIT", "Finding nearest neighbor to mu = {}.".format(mu),
22)
nn = emptySampleList()
for i, iM in enumerate(idxUnique):
idx = np.where(idxmap == i)[0]
val, supp = self.data.vals[iM], self.data.supp[iM]
if i == 0:
if hasattr(self.data.projMat, "shape"):
nnlen = self.data.projMat.shape[1]
else:
nnlen = len(val)
nn.reset((nnlen, len(mu)), dtype = val.dtype)
nn.data[:] = 0.
for i in idx: nn.data[supp : supp + len(val), i] = val
vbMng(self, "DEL", "Done finding nearest neighbor.", 22)
return nn
def getApproxReduced(self, mu : paramList = []) -> sampList:
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = self.checkParameterList(mu)
if (not hasattr(self, "lastSolvedApproxReduced")
or self.lastSolvedApproxReduced != mu):
vbMng(self, "INIT",
"Evaluating approximant at mu = {}.".format(mu), 12)
self.uApproxReduced = self.getNearestNeighbor(mu)
vbMng(self, "DEL", "Done evaluating approximant.", 12)
self.lastSolvedApproxReduced = mu
return self.uApproxReduced

Event Timeline