Page MenuHomec4science

decorators.py
No OneTemporary

File Metadata

Created
Sun, Apr 28, 06:28

decorators.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/>.
#
from numpy import inf, random
from functools import wraps
__all__ = ['affine_construct', 'pivot_affine_construct', 'nonaffine_construct',
'get_is_affine', 'mu_independent', 'mupivot_independent',
'get_is_mu_independent', 'threshold', 'addWhiteNoise']
def affine_construct(method):
method.is_affine = 1
return method
def pivot_affine_construct(method):
method.is_affine = 1 / 2
return method
def nonaffine_construct(method):
method.is_affine = 0
return method
def get_is_affine(m):
if hasattr(m, "is_affine"): return m.is_affine
return 0
def mu_independent(method):
method.is_mu_independent = 1
return method
def mupivot_independent(method):
method.is_mu_independent = 1 / 2
return method
def get_is_mu_independent(m):
if hasattr(m, "is_mu_independent"): return m.is_mu_independent
return 0
def threshold(thresh = inf):
def method_with_thresh(method):
method.threshold = thresh
return method
return method_with_thresh
def addWhiteNoise(amplitude : float = 1., seed : int = None,
to_solve : bool = True):
def add_noise(engine):
try:
is_engine = "solve" in dir(engine)
except:
is_engine = False
if is_engine: # apply from outside class
if to_solve:
class engine_with_noise(engine):
def solve(self, *args, **kwargs):
if not hasattr(self, "rng"):
self.rng = random.default_rng(seed)
self.__class__.__name__ = (engine.__name__
+ "+WhiteNoise")
sol = super().solve(*args, **kwargs)
noise = self.rng.standard_normal(sol.shape)
if sol.dtype == complex:
noise = noise + 1.j * self.rng.standard_normal(
noise.shape)
return sol + amplitude * noise
else:
class engine_with_noise(engine):
def b(self, *args, **kwargs):
if not hasattr(self, "rng"):
self.rng = random.default_rng(seed)
self.__class__.__name__ = (engine.__name__
+ "+WhiteNoise")
bb = super().b(*args, **kwargs)
noise = self.rng.standard_normal(bb.shape)
if bb.dtype == complex:
noise = noise + 1.j * self.rng.standard_normal(
noise.shape)
return bb + amplitude * noise
return engine_with_noise
# apply from within class
@wraps(engine)
def solve_with_noise(self, *args, **kwargs):
if not hasattr(self, "rng"):
self.rng = random.default_rng(seed)
self.__class__.__name__ += "+WhiteNoise"
sol = engine(self, *args, **kwargs)
noise = self.rng.standard_normal(sol.shape)
if sol.dtype == complex:
noise = noise + 1.j * self.rng.standard_normal(noise.shape)
return sol + amplitude * noise
return solve_with_noise
return add_noise

Event Timeline