Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F61199207
parameter_list.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, May 5, 04:10
Size
8 KB
Mime Type
text/x-python
Expires
Tue, May 7, 04:10 (2 d)
Engine
blob
Format
Raw Data
Handle
17479173
Attached To
R6746 RationalROMPy
parameter_list.py
View Options
# 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
collections.abc
import
Iterable
from
itertools
import
product
as
iterprod
from
copy
import
deepcopy
as
copy
from
rrompy.utilities.exception_manager
import
RROMPyException
,
RROMPyAssert
from
rrompy.utilities.base.types
import
Np2D
__all__
=
[
'parameterList'
,
'emptyParameterList'
,
'checkParameterList'
]
def
checkParameterList
(
mu
,
npar
=
None
,
check_if_single
:
bool
=
False
,
return_data
:
bool
=
False
):
if
not
isinstance
(
mu
,
(
parameterList
,)):
mu
=
parameterList
(
mu
,
npar
)
else
:
if
npar
is
not
None
:
RROMPyAssert
(
mu
.
shape
[
1
],
npar
,
"Number of parameters"
)
mu
=
copy
(
mu
)
if
npar
==
0
:
mu
.
reset
((
1
,
0
),
mu
.
dtype
)
if
return_data
:
mu
=
mu
.
data
if
check_if_single
:
return
mu
,
len
(
mu
)
<=
1
return
mu
def
checkParameter
(
mu
,
npar
=
None
,
return_data
:
bool
=
False
):
muL
,
wasPar
=
checkParameterList
(
mu
,
npar
,
True
,
return_data
)
if
not
wasPar
:
muL
,
wasPar
=
checkParameterList
([
mu
],
npar
,
True
,
return_data
)
if
not
wasPar
:
raise
RROMPyException
((
"Only single parameter allowed. No "
"parameter lists here."
))
return
muL
def
emptyParameterList
():
return
parameterList
([[]])
def
addMemberFromNumpyArray
(
self
,
fieldName
):
def
objFunc
(
self
,
other
):
if
not
isinstance
(
other
,
(
self
.
__class__
,)):
other
=
parameterList
(
other
,
self
.
shape
[
1
])
return
parameterList
(
getattr
(
np
.
ndarray
,
fieldName
)(
self
.
data
,
other
.
data
))
setattr
(
self
.
__class__
,
fieldName
,
objFunc
)
def
objIFunc
(
self
,
other
):
self
.
data
=
getattr
(
self
.
__class__
,
fieldName
)(
self
,
other
)
.
data
setattr
(
self
.
__class__
,
"__i"
+
fieldName
[
2
:],
objIFunc
)
class
parameterList
:
__all__
+=
[
pre
+
post
for
pre
,
post
in
iterprod
([
"__"
,
"__i"
],
[
"add__"
,
"sub__"
,
"mul__"
,
"div__"
,
"truediv__"
,
"floordiv__"
,
"pow__"
])]
def
__init__
(
self
,
data
:
Np2D
,
lengthCheck
:
int
=
None
):
if
not
isinstance
(
data
,
Iterable
):
data
=
[
data
]
elif
isinstance
(
data
,
(
self
.
__class__
,)):
data
=
data
.
data
elif
isinstance
(
data
,
(
tuple
,)):
data
=
list
(
data
)
if
(
isinstance
(
data
,
(
list
,))
and
len
(
data
)
>
0
and
isinstance
(
data
[
0
],
(
tuple
,))):
data
=
[
list
(
x
)
for
x
in
data
]
self
.
data
=
np
.
array
(
data
,
ndmin
=
1
,
copy
=
1
)
if
self
.
data
.
ndim
==
1
:
self
.
data
=
self
.
data
[:,
None
]
if
np
.
size
(
self
.
data
)
>
0
:
self
.
data
=
self
.
data
.
reshape
((
len
(
self
),
-
1
))
if
self
.
shape
[
0
]
*
self
.
shape
[
1
]
==
0
:
lenEff
=
0
if
lengthCheck
is
None
else
lengthCheck
self
.
reset
((
0
,
lenEff
),
self
.
dtype
)
if
lengthCheck
is
not
None
:
if
lengthCheck
!=
1
and
self
.
shape
==
(
lengthCheck
,
1
):
self
.
data
=
self
.
data
.
T
RROMPyAssert
(
self
.
shape
[
1
],
lengthCheck
,
"Number of parameters"
)
for
fieldName
in
[
"__add__"
,
"__sub__"
,
"__mul__"
,
"__div__"
,
"__truediv__"
,
"__floordiv__"
,
"__pow__"
]:
addMemberFromNumpyArray
(
self
,
fieldName
)
def
__len__
(
self
):
return
self
.
shape
[
0
]
def
__str__
(
self
):
if
len
(
self
)
==
0
:
selfstr
=
"[]"
elif
len
(
self
)
<=
3
:
selfstr
=
"[{}]"
.
format
(
" "
.
join
([
str
(
x
)
for
x
in
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
):
return
self
.
data
.
shape
@property
def
size
(
self
):
return
self
.
data
.
size
@property
def
re
(
self
):
return
parameterList
(
np
.
real
(
self
.
data
))
@property
def
im
(
self
):
return
parameterList
(
np
.
imag
(
self
.
data
))
@property
def
abs
(
self
):
return
parameterList
(
np
.
abs
(
self
.
data
))
@property
def
angle
(
self
):
return
parameterList
(
np
.
angle
(
self
.
data
))
@property
def
conj
(
self
):
return
parameterList
(
np
.
conj
(
self
.
data
))
@property
def
dtype
(
self
):
return
self
.
data
.
dtype
def
__getitem__
(
self
,
key
):
return
self
.
data
[
key
]
def
__call__
(
self
,
key
,
idx
=
None
):
if
idx
is
None
:
return
self
.
data
[:,
key
]
return
self
[
key
,
idx
]
def
__setitem__
(
self
,
key
,
value
):
if
isinstance
(
key
,
(
tuple
,
list
,
np
.
ndarray
)):
RROMPyAssert
(
len
(
key
),
len
(
value
),
"Slice length"
)
for
k
,
val
in
zip
(
key
,
value
):
self
[
k
]
=
val
else
:
self
.
data
[
key
]
=
value
def
__eq__
(
self
,
other
):
if
not
hasattr
(
other
,
"shape"
)
or
self
.
shape
!=
other
.
shape
:
return
False
if
isinstance
(
other
,
self
.
__class__
):
other
=
other
.
data
return
np
.
allclose
(
self
.
data
,
other
)
def
__contains__
(
self
,
item
):
return
next
((
x
for
x
in
self
if
np
.
allclose
(
x
[
0
],
item
)),
-
1
)
!=
-
1
def
__iter__
(
self
):
return
iter
([
parameterList
([
x
])
for
x
in
self
.
data
])
def
__copy__
(
self
):
return
parameterList
(
self
.
data
)
def
__deepcopy__
(
self
,
memo
):
return
parameterList
(
copy
(
self
.
data
,
memo
))
def
__neg__
(
self
):
return
parameterList
(
-
self
.
data
)
def
__pos__
(
self
):
return
copy
(
self
)
def
reset
(
self
,
size
,
dtype
=
complex
):
self
.
data
=
np
.
empty
(
size
,
dtype
=
dtype
)
self
.
data
[:]
=
np
.
nan
def
insert
(
self
,
items
,
idx
=
None
):
if
isinstance
(
items
,
self
.
__class__
):
items
=
items
.
data
else
:
items
=
np
.
array
(
items
,
ndmin
=
2
)
if
len
(
self
)
==
0
:
self
.
data
=
parameterList
(
items
)
.
data
elif
idx
is
None
:
self
.
data
=
np
.
append
(
self
.
data
,
items
,
axis
=
0
)
else
:
self
.
data
=
np
.
insert
(
self
.
data
,
idx
,
items
,
axis
=
0
)
def
append
(
self
,
items
):
self
.
insert
(
items
)
def
pop
(
self
,
idx
=
-
1
):
self
.
data
=
np
.
delete
(
self
.
data
,
idx
,
axis
=
0
)
def
find
(
self
,
item
):
if
len
(
self
)
==
0
:
return
None
return
next
((
j
for
j
in
range
(
len
(
self
))
if
np
.
allclose
(
self
[
j
],
item
)),
None
)
def
findall
(
self
,
item
):
if
len
(
self
)
==
0
:
return
[]
return
[
j
for
j
in
range
(
len
(
self
))
if
np
.
allclose
(
self
[
j
],
item
)]
def
sort
(
self
,
overwrite
=
False
,
*
args
,
**
kwargs
):
dataT
=
np
.
array
([
tuple
(
x
[
0
])
for
x
in
self
],
dtype
=
[(
str
(
j
),
self
.
dtype
)
for
j
in
range
(
self
.
shape
[
1
])])
sortedP
=
parameterList
([
list
(
x
)
for
x
in
np
.
sort
(
dataT
,
*
args
,
**
kwargs
)])
if
overwrite
:
self
.
data
=
sortedP
.
data
return
sortedP
def
unique
(
self
,
overwrite
=
False
,
*
args
,
**
kwargs
):
dataT
=
np
.
array
([
tuple
(
x
[
0
])
for
x
in
self
],
dtype
=
[(
str
(
j
),
self
.
dtype
)
for
j
in
range
(
self
.
shape
[
1
])])
uniqueT
=
np
.
unique
(
dataT
,
*
args
,
**
kwargs
)
if
isinstance
(
uniqueT
,
(
tuple
,)):
extraT
=
uniqueT
[
1
:]
uniqueT
=
uniqueT
[
0
]
else
:
extraT
=
()
uniqueP
=
parameterList
([
list
(
x
)
for
x
in
uniqueT
])
if
overwrite
:
self
.
data
=
uniqueP
.
data
uniqueP
=
(
uniqueP
,)
+
extraT
if
len
(
uniqueP
)
==
1
:
return
uniqueP
[
0
]
return
uniqueP
Event Timeline
Log In to Comment