Page MenuHomec4science

trained_model_nearest_neighbor.py
No OneTemporary

File Metadata

Created
Wed, May 29, 11:31

trained_model_nearest_neighbor.py

# Copyright (C) 2018 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 numbers import Number
from rrompy.reduction_methods.base.trained_model import TrainedModel
from rrompy.utilities.base.types import Np1D, paramVal, paramList, sampList
from rrompy.utilities.base import verbosityManager as vbMng
from rrompy.parameter import checkParameterList
from rrompy.sampling import sampleList
__all__ = ['TrainedModelNearestNeighbor']
class TrainedModelNearestNeighbor(TrainedModel):
"""
ROM approximant evaluation for nearest neighbor approximant.
Attributes:
Data: dictionary with all that can be pickled.
"""
def centerNormalize(self, mu : paramList = [],
mu0 : paramVal = None) -> paramList:
"""
Compute normalized parameter to be plugged into approximant.
Args:
mu: Parameter(s) 1.
mu0: Parameter(s) 2. If None, set to self.data.mu0.
Returns:
Normalized parameter.
"""
mu = checkParameterList(mu, self.data.npar)[0]
if mu0 is None: mu0 = self.data.mu0
rad = ((mu ** self.data.rescalingExp - mu0 ** self.data.rescalingExp)
/ self.data.scaleFactor)
return rad
def getNearestNeighbor(self, mu : paramList = []) -> Np1D:
"""
Find nearest neighbor to arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = checkParameterList(mu, self.data.npar)[0]
vbMng(self, "INIT", "Finding nearest neighbor to mu = {}.".format(mu),
22)
if not hasattr(self.data, "musCentered"):
self.data.musCentered = self.centerNormalize(self.data.mus)
muTile = np.repeat(self.data.musCentered.data.reshape(
self.data.musCentered.shape + (1,)),
len(mu), axis = -1)
muCN = self.centerNormalize(mu).data.T
ids = np.argmin(np.linalg.norm(muTile - muCN, axis = 1), axis = 0)
vbMng(self, "DEL", "Done finding nearest neighbor.", 22)
return ids
def getApproxReduced(self, mu : paramList = []) -> sampList:
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = checkParameterList(mu, self.data.npar)[0]
if (not hasattr(self, "lastSolvedApproxReduced")
or self.lastSolvedApproxReduced != mu):
vbMng(self, "INIT",
"Evaluating approximant at mu = {}.".format(mu), 12)
ids = self.getNearestNeighbor(mu)
if isinstance(self.data.matR, Number):
matRed = np.zeros((self.data.projMat.shape[1], len(ids)))
matRed[ids, np.arange(len(ids))] = self.data.matR
else:
matRed = self.data.matR[:, ids]
self.uApproxReduced = sampleList(matRed)
vbMng(self, "DEL", "Done evaluating approximant.", 12)
self.lastSolvedApproxReduced = mu
return self.uApproxReduced
def getPoles(self, *args, **kwargs) -> Np1D:
"""Obtain approximant poles."""
return np.empty(0)

Event Timeline