Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
lscsoft
bilby
Commits
680b7d53
Commit
680b7d53
authored
Oct 17, 2018
by
Moritz Huebner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resolve "Replace inspect.getargspec references"
parent
5daa5b4c
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
133 additions
and
21 deletions
+133
-21
.gitlab-ci.yml
.gitlab-ci.yml
+2
-2
bilby/core/prior.py
bilby/core/prior.py
+2
-4
bilby/core/sampler/pymc3.py
bilby/core/sampler/pymc3.py
+4
-5
bilby/core/utils.py
bilby/core/utils.py
+32
-3
bilby/hyper/model.py
bilby/hyper/model.py
+7
-5
test/utils_py3_test.py
test/utils_py3_test.py
+54
-0
test/utils_test.py
test/utils_test.py
+32
-2
No files found.
.gitlab-ci.yml
View file @
680b7d53
# This script is an edited version of the example found at
# https://git.ligo.org/lscsoft/example-ci-project/blob/python/.gitlab-ci.yml
# Each 0th-inden
d
ation level is a job that will be run within GitLab CI
# Each 0th-inden
t
ation level is a job that will be run within GitLab CI
# The only exception are a short list of reserved keywords
#
# https://docs.gitlab.com/ee/ci/yaml/#gitlab-ci-yml
...
...
@@ -25,7 +25,7 @@ python-2:
script
:
-
python setup.py install
# Run tests without finding coverage
-
pytest
-
pytest
--ignore=test/utils_py3_test.py
# test example on python 3
python-3
:
...
...
bilby/core/prior.py
View file @
680b7d53
...
...
@@ -9,10 +9,9 @@ import os
from
collections
import
OrderedDict
from
future.utils
import
iteritems
from
.utils
import
logger
from
.utils
import
logger
,
infer_args_from_method
from
.
import
utils
import
bilby
# noqa
import
inspect
class
PriorSet
(
OrderedDict
):
...
...
@@ -426,8 +425,7 @@ class Prior(object):
str: A string representation of this instance
"""
subclass_args
=
inspect
.
getargspec
(
self
.
__init__
).
args
subclass_args
.
pop
(
0
)
subclass_args
=
infer_args_from_method
(
self
.
__init__
)
prior_name
=
self
.
__class__
.
__name__
property_names
=
[
p
for
p
in
dir
(
self
.
__class__
)
if
isinstance
(
getattr
(
self
.
__class__
,
p
),
property
)]
...
...
bilby/core/sampler/pymc3.py
View file @
680b7d53
from
__future__
import
absolute_import
,
print_function
from
collections
import
OrderedDict
import
inspect
import
numpy
as
np
from
..utils
import
derivatives
,
logger
from
..utils
import
derivatives
,
logger
,
infer_args_from_method
from
..prior
import
Prior
from
..result
import
Result
from
.base_sampler
import
Sampler
,
MCMCSampler
...
...
@@ -437,7 +436,7 @@ class Pymc3(MCMCSampler):
# then use that log_likelihood function, with the assumption that it
# takes in a Pymc3 Sampler, with a pymc3_model attribute, and defines
# the likelihood within that context manager
likeargs
=
in
spect
.
getargspec
(
self
.
likelihood
.
log_likelihood
)
.
args
likeargs
=
in
fer_args_from_method
(
self
.
likelihood
.
log_likelihood
)
if
'sampler'
in
likeargs
:
self
.
likelihood
.
log_likelihood
(
sampler
=
self
)
else
:
...
...
@@ -480,7 +479,7 @@ class Pymc3(MCMCSampler):
for
key
in
self
.
priors
:
# if the prior contains ln_prob method that takes a 'sampler' argument
# then try using that
lnprobargs
=
in
spect
.
getargspec
(
self
.
priors
[
key
].
ln_prob
)
.
args
lnprobargs
=
in
fer_args_from_method
(
self
.
priors
[
key
].
ln_prob
)
if
'sampler'
in
lnprobargs
:
try
:
self
.
pymc3_priors
[
key
]
=
self
.
priors
[
key
].
ln_prob
(
sampler
=
self
)
...
...
@@ -500,7 +499,7 @@ class Pymc3(MCMCSampler):
if
pymc3distname
not
in
pymc3
.
__dict__
:
raise
ValueError
(
"Prior '{}' is not a known PyMC3 distribution."
.
format
(
pymc3distname
))
reqargs
=
in
spect
.
getargspec
(
pymc3
.
__dict__
[
pymc3distname
].
__init__
)
.
args
[
1
:]
reqargs
=
in
fer_args_from_method
(
pymc3
.
__dict__
[
pymc3distname
].
__init__
)
# set keyword arguments
priorkwargs
=
{}
...
...
bilby/core/utils.py
View file @
680b7d53
...
...
@@ -18,10 +18,39 @@ radius_of_earth = 6371 * 1e3 # metres
def
infer_parameters_from_function
(
func
):
""" Infers the arguments of function (except the first arg which is
assumed to be the dep. variable)
""" Infers the arguments of a function
(except the first arg which is assumed to be the dep. variable).
Throws out *args and **kwargs type arguments
Can deal with type hinting!
Returns
---------
list: A list of strings with the parameters
"""
parameters
=
inspect
.
getargspec
(
func
).
args
return
_infer_args_from_function_except_for_first_arg
(
func
=
func
)
def
infer_args_from_method
(
method
):
""" Infers all arguments of a method except for 'self'
Throws out *args and **kwargs type arguments.
Can deal with type hinting!
Returns
---------
list: A list of strings with the parameters
"""
return
_infer_args_from_function_except_for_first_arg
(
func
=
method
)
def
_infer_args_from_function_except_for_first_arg
(
func
):
try
:
parameters
=
inspect
.
getfullargspec
(
func
).
args
except
AttributeError
:
parameters
=
inspect
.
getargspec
(
func
).
args
parameters
.
pop
(
0
)
return
parameters
...
...
bilby/hyper/model.py
View file @
680b7d53
import
inspect
from
bilby.core.utils
import
infer_parameters_from_function
class
Model
(
object
):
...
...
@@ -18,8 +18,9 @@ class Model(object):
self
.
models
=
model_functions
self
.
parameters
=
dict
()
for
function
in
self
.
models
:
for
key
in
inspect
.
getargspec
(
function
).
args
[
1
:]:
for
func
in
self
.
models
:
param_keys
=
infer_parameters_from_function
(
func
)
for
key
in
param_keys
:
self
.
parameters
[
key
]
=
None
def
prob
(
self
,
data
):
...
...
@@ -30,6 +31,7 @@ class Model(object):
probability
*=
function
(
data
,
**
self
.
_get_function_parameters
(
function
))
return
probability
def
_get_function_parameters
(
self
,
function
):
parameters
=
{
key
:
self
.
parameters
[
key
]
for
key
in
inspect
.
getargspec
(
function
).
args
[
1
:]}
def
_get_function_parameters
(
self
,
func
):
param_keys
=
infer_parameters_from_function
(
func
)
parameters
=
{
key
:
self
.
parameters
[
key
]
for
key
in
param_keys
}
return
parameters
test/utils_py3_test.py
0 → 100644
View file @
680b7d53
from
__future__
import
absolute_import
,
division
import
unittest
from
bilby.core
import
utils
class
TestInferParameters
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
source_function1
(
freqs
,
a
,
b
:
int
):
return
None
def
source_function2
(
freqs
,
a
,
b
,
*
args
,
**
kwargs
):
return
None
def
source_function3
(
freqs
,
a
,
b
:
int
,
*
args
,
**
kwargs
):
return
None
class
TestClass
:
def
test_method
(
self
,
a
,
b
:
int
,
*
args
,
**
kwargs
):
pass
self
.
source1
=
source_function1
self
.
source2
=
source_function2
self
.
source3
=
source_function3
test_obj
=
TestClass
()
self
.
source4
=
test_obj
.
test_method
def
tearDown
(
self
):
del
self
.
source1
del
self
.
source2
del
self
.
source3
del
self
.
source4
def
test_type_hinting
(
self
):
expected
=
[
'a'
,
'b'
]
actual
=
utils
.
infer_parameters_from_function
(
self
.
source1
)
self
.
assertListEqual
(
expected
,
actual
)
def
test_args_kwargs_handling
(
self
):
expected
=
[
'a'
,
'b'
]
actual
=
utils
.
infer_parameters_from_function
(
self
.
source2
)
self
.
assertListEqual
(
expected
,
actual
)
def
test_both
(
self
):
expected
=
[
'a'
,
'b'
]
actual
=
utils
.
infer_parameters_from_function
(
self
.
source3
)
self
.
assertListEqual
(
expected
,
actual
)
def
test_self_handling
(
self
):
expected
=
[
'a'
,
'b'
]
actual
=
utils
.
infer_args_from_method
(
self
.
source4
)
self
.
assertListEqual
(
expected
,
actual
)
test/utils_test.py
View file @
680b7d53
from
__future__
import
absolute_import
,
division
import
bilby
import
unittest
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
bilby
from
bilby.core
import
utils
class
TestFFT
(
unittest
.
TestCase
):
...
...
@@ -30,5 +31,34 @@ class TestFFT(unittest.TestCase):
self
.
assertTrue
(
np
.
all
(
np
.
abs
((
tds
-
tds2
)
/
tds
)
<
1e-12
))
class
TestInferParameters
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
source_function
(
freqs
,
a
,
b
,
*
args
,
**
kwargs
):
return
None
class
TestClass
:
def
test_method
(
self
,
a
,
b
,
*
args
,
**
kwargs
):
pass
self
.
source1
=
source_function
test_obj
=
TestClass
()
self
.
source2
=
test_obj
.
test_method
def
tearDown
(
self
):
del
self
.
source1
del
self
.
source2
def
test_args_kwargs_handling
(
self
):
expected
=
[
'a'
,
'b'
]
actual
=
utils
.
infer_parameters_from_function
(
self
.
source1
)
self
.
assertListEqual
(
expected
,
actual
)
def
test_self_handling
(
self
):
expected
=
[
'a'
,
'b'
]
actual
=
utils
.
infer_args_from_method
(
self
.
source2
)
self
.
assertListEqual
(
expected
,
actual
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment