Page MenuHomec4science

parameter_list.py
No OneTemporary

File Metadata

Created
Mon, May 6, 11:22

parameter_list.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/>.
#
from copy import deepcopy as copy
from .parameter import parameter
from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert
from rrompy.utilities.base.types import List, TupleAny
__all__ = ['parameterList', 'emptyParameterList', 'checkParameter',
'checkParameterList']
def checkParameter(mu, npar = None, deep = False):
if isinstance(mu, (parameterList, list,)):
raise RROMPyException(("Parameter list not allowed here. Single "
"parameter required."))
if not isinstance(mu, (parameter,)):
mu = parameter(mu, npar)
elif npar is not None:
RROMPyAssert(len(mu), npar, "Number of parameters")
return copy(mu)
def checkParameterList(mu, npar = None, deep = False):
wasParameter = False
if not isinstance(mu, (parameterList,)):
mu = parameterList(mu, npar)
if len(mu) == 1:
wasParameter = True
elif npar is not None and mu.shape[1] is not None:
RROMPyAssert(mu.shape[1], npar, "Number of parameters")
return copy(mu), wasParameter
class parameterList:
"""HERE"""
def __init__(self, data:List[TupleAny], lengthCheck : int = None):
if (isinstance(data, (tuple, parameter,))
or not hasattr(data, "__len__")):
data = [data]
self.data = [None] * len(data)
for j, par in enumerate(data):
self[j] = parameter(par)
if j == 0 and lengthCheck is None:
lengthCheck = self.shape[1]
RROMPyAssert(len(self[j]), lengthCheck, "Number of parameters")
def __len__(self):
return len(self.data)
def __str__(self):
if len(self) <= 3:
selfstr = str(self.data)
else:
selfstr = "[{} ..({}).. {}]".format(self[0], len(self) - 2,
self[-1])
return selfstr
def __repr__(self):
return repr(self.data)
@property
def shape(self):
if len(self) == 0:
return (0, None)
return (len(self), len(self.data[0]))
@property
def re(self):
return parameterList([x.re for x in self.data])
@property
def im(self):
return parameterList([x.im for x in self.data])
@property
def abs(self):
return parameterList([x.abs for x in self.data])
@property
def angle(self):
return parameterList([x.angle for x in self.data])
@property
def conj(self):
return parameter([x.conj for x in self.data])
@property
def dtype(self):
return self.data[0].dtype
def __getitem__(self, key):
if isinstance(key, (tuple,)):
return tuple(self[list(key)])
if isinstance(key, (list,)):
return [self[k] for k in key]
return self.data[key]
def __call__(self, dim):
return [x(dim) for x in self.data]
def __setitem__(self, key, value):
if isinstance(key, (tuple, list,)):
RROMPyAssert(len(key), len(value), "Slice length")
for k, val in zip(key, value):
self[k] = val
else:
self.data[key] = value
def __contains__(self, item):
return item in self.data
def __iter__(self):
return self.data.__iter__()
def __eq__(self, other):
if not hasattr(other, "shape") or self.shape != other.shape:
return False
if isinstance(other, self.__class__):
fac = other.data
else:
fac = other
return (isinstance(fac, (list,))
and all([self[j] == fac[j] for j in range(len(self))]))
def __ne__(self, other):
return not self == other
def __copy__(self):
return parameterList(self.data)
def __deepcopy__(self, memo):
return parameterList(copy(self.data, memo))
def __add__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(self.shape, other.shape, "Number of parameters")
fac = other.data
else:
fac = [other] * len(self)
data = [self[j] + fac[j] for j in range(len(self))]
return parameterList(data)
def __iadd__(self, other):
self.data = (self + other).data
return self
def __sub__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(self.shape, other.shape, "Number of parameters")
fac = other.data
else:
fac = [other] * len(self)
data = [self[j] - fac[j] for j in range(len(self))]
return parameterList(data)
def __isub__(self, other):
self.data = (self - other).data
return self
def __mul__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(self.shape, other.shape, "Number of parameters")
fac = other.data
else:
fac = [other] * len(self)
data = [self[j] * fac[j] for j in range(len(self))]
return parameterList(data)
def __imul__(self, other):
self.data = (self * other).data
return self
def __truediv__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(self.shape, other.shape, "Number of parameters")
fac = other.data
else:
fac = [other] * len(self)
data = [self[j] / fac[j] for j in range(len(self))]
return parameterList(data)
def __idiv__(self, other):
self.data = (self / other).data
return self
def __pow__(self, other):
if isinstance(other, self.__class__):
RROMPyAssert(self.shape, other.shape, "Number of parameters")
fac = other.data
else:
fac = [other] * len(self)
data = [self[j] ** fac[j] for j in range(len(self))]
return parameterList(data)
def __ipow__(self, other):
self.data = (self ** other).data
return self
def __neg__(self):
return parameterList([-x for x in self.data])
def __pos__(self):
return parameterList(self.data)
def reset(self, length):
self.data = [None] * length
def append(self, items):
if isinstance(items, self.__class__):
fac = items.data
elif isinstance(items, (parameter,)):
fac = [items]
self.data += [checkParameter(x, self.shape[1], True) for x in fac]
def pop(self, idx = -1):
self.data.pop(idx)
def find(self, item):
return next((j for j in range(len(self)) if self[j] == item), None)
def findall(self, item):
return [j for j in range(len(self)) if self[j] == item]
def flatten(self, idx = 0):
return [y(idx) for y in self]
class emptyParameterList(parameterList):
def __init__(self):
super().__init__([])

Event Timeline