Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Neel Shah
NIFTy
Commits
bf3848ee
Commit
bf3848ee
authored
Sep 26, 2017
by
Theo Steininger
Browse files
Merge branch 'byebye_bare' into 'nightly'
Get rid of the "bare" keyword See merge request !197
parents
10319edf
6cea60c4
Changes
12
Hide whitespace changes
Inline
Side-by-side
demos/critical_filtering.py
View file @
bf3848ee
...
...
@@ -68,7 +68,7 @@ if __name__ == "__main__":
h_space
=
fft
.
target
[
0
]
# Set up power space
p_space
=
PowerSpace
(
h_space
,
logarithmic
=
True
,
p_space
=
PowerSpace
(
h_space
,
binbounds
=
PowerSpace
.
useful_binbounds
(
h_space
,
logarithmic
=
True
)
,
distribution_strategy
=
distribution_strategy
)
# Choose the prior correlation structure and defining correlation operator
...
...
@@ -91,7 +91,8 @@ if __name__ == "__main__":
R
=
AdjointFFTResponse
(
fft
,
Instrument
)
noise
=
1.
N
=
DiagonalOperator
(
s_space
,
diagonal
=
noise
,
bare
=
True
)
ndiag
=
Field
(
s_space
,
noise
).
weight
(
1
)
N
=
DiagonalOperator
(
s_space
,
ndiag
)
n
=
Field
.
from_random
(
domain
=
s_space
,
random_type
=
'normal'
,
std
=
sqrt
(
noise
),
...
...
demos/log_normal_wiener_filter.py
View file @
bf3848ee
...
...
@@ -39,7 +39,8 @@ if __name__ == "__main__":
R_harmonic
=
ComposedOperator
([
fft
,
R
],
default_spaces
=
[
0
,
0
])
# Setting up the noise covariance and drawing a random noise realization
N
=
DiagonalOperator
(
data_domain
,
diagonal
=
mock_signal
.
var
()
/
signal_to_noise
,
bare
=
True
)
ndiag
=
Field
(
data_domain
,
mock_signal
.
var
()
/
signal_to_noise
).
weight
(
1
)
N
=
DiagonalOperator
(
data_domain
,
ndiag
)
noise
=
Field
.
from_random
(
domain
=
data_domain
,
random_type
=
'normal'
,
std
=
mock_signal
.
std
()
/
np
.
sqrt
(
signal_to_noise
),
mean
=
0
)
data
=
R
(
exp
(
mock_signal
))
+
noise
#|\label{code:wf_mock_data}|
...
...
demos/paper_demos/cartesian_wiener_filter.py
View file @
bf3848ee
...
...
@@ -93,8 +93,8 @@ if __name__ == "__main__":
R_harmonic
=
ift
.
ComposedOperator
([
fft
,
R
],
default_spaces
=
(
0
,
1
,
0
,
1
))
# Setting up the noise covariance and drawing a random noise realization
N
=
ift
.
DiagonalOperator
(
data_domain
,
diagonal
=
mock_signal
.
var
()
/
signal_to_noise
,
bare
=
True
)
ndiag
=
ift
.
Field
(
data_domain
,
mock_signal
.
var
()
/
signal_to_noise
).
weight
(
1
)
N
=
ift
.
DiagonalOperator
(
data_domain
,
ndiag
)
noise
=
ift
.
Field
.
from_random
(
domain
=
data_domain
,
random_type
=
'normal'
,
std
=
mock_signal
.
std
()
/
np
.
sqrt
(
signal_to_noise
),
mean
=
0
)
...
...
demos/paper_demos/wiener_filter.py
View file @
bf3848ee
...
...
@@ -41,7 +41,8 @@ if __name__ == "__main__":
R_harmonic
=
ift
.
ComposedOperator
([
fft
,
R
],
default_spaces
=
[
0
,
0
])
# Setting up the noise covariance and drawing a random noise realization
N
=
ift
.
DiagonalOperator
(
data_domain
,
diagonal
=
mock_signal
.
var
()
/
signal_to_noise
,
bare
=
True
)
ndiag
=
ift
.
Field
(
data_domain
,
mock_signal
.
var
()
/
signal_to_noise
).
weight
(
1
)
N
=
ift
.
DiagonalOperator
(
data_domain
,
ndiag
)
noise
=
ift
.
Field
.
from_random
(
domain
=
data_domain
,
random_type
=
'normal'
,
std
=
mock_signal
.
std
()
/
np
.
sqrt
(
signal_to_noise
),
mean
=
0
)
data
=
R
(
mock_signal
)
+
noise
#|\label{code:wf_mock_data}|
...
...
@@ -57,7 +58,7 @@ if __name__ == "__main__":
proby
=
Proby
(
signal_space
,
probe_count
=
800
)
proby
(
lambda
z
:
fft
(
wiener_curvature
.
inverse_times
(
fft
.
inverse_times
(
z
))))
#|\label{code:wf_variance_fft_wrap}|
sm
=
ift
.
SmoothingOperator
.
make
(
signal_space
,
sigma
=
0.03
)
sm
=
ift
.
FFT
SmoothingOperator
(
signal_space
,
sigma
=
0.03
)
variance
=
ift
.
sqrt
(
sm
(
proby
.
diagonal
.
weight
(
-
1
)))
#|\label{code:wf_variance_weighting}|
repo
=
Repository
(
'repo_800.h5'
)
...
...
demos/wiener_filter_via_curvature.py
View file @
bf3848ee
...
...
@@ -54,9 +54,8 @@ if __name__ == "__main__":
data_domain
=
R
.
target
[
0
]
R_harmonic
=
ComposedOperator
([
fft
,
R
],
default_spaces
=
[
0
,
0
])
N
=
DiagonalOperator
(
data_domain
,
diagonal
=
mock_signal
.
var
()
/
signal_to_noise
,
bare
=
True
)
ndiag
=
Field
(
data_domain
,
mock_signal
.
var
()
/
signal_to_noise
).
weight
(
1
)
N
=
DiagonalOperator
(
data_domain
,
ndiag
)
noise
=
Field
.
from_random
(
domain
=
data_domain
,
random_type
=
'normal'
,
std
=
mock_signal
.
std
()
/
np
.
sqrt
(
signal_to_noise
),
...
...
demos/wiener_filter_via_hamiltonian.py
View file @
bf3848ee
...
...
@@ -74,7 +74,8 @@ if __name__ == "__main__":
# Adding a harmonic transformation to the instrument
R
=
AdjointFFTResponse
(
fft
,
Instrument
)
signal_to_noise
=
1.
N
=
DiagonalOperator
(
s_space
,
diagonal
=
ss
.
var
()
/
signal_to_noise
,
bare
=
True
)
ndiag
=
Field
(
s_space
,
val
=
ss
.
var
()
/
signal_to_noise
).
weight
()
N
=
DiagonalOperator
(
s_space
,
diagonal
=
ndiag
)
n
=
Field
.
from_random
(
domain
=
s_space
,
random_type
=
'normal'
,
std
=
ss
.
std
()
/
np
.
sqrt
(
signal_to_noise
),
...
...
nifty/field.py
View file @
bf3848ee
...
...
@@ -1072,7 +1072,7 @@ class Field(Loggable, Versionable, object):
new_field
.
set_val
(
new_val
=
new_val
,
copy
=
False
)
return
new_field
def
vdot
(
self
,
x
=
None
,
spaces
=
None
,
bare
=
False
):
def
vdot
(
self
,
x
=
None
,
spaces
=
None
):
""" Computes the volume-factor-aware dot product of 'self' with x.
Parameters
...
...
@@ -1084,9 +1084,6 @@ class Field(Loggable, Versionable, object):
If the domain of `self` and `x` are not the same, `spaces` specfies
the mapping.
bare : boolean
If true, no volume factors will be included in the computation.
Returns
-------
out : float, complex
...
...
@@ -1097,10 +1094,7 @@ class Field(Loggable, Versionable, object):
"the NIFTy field class"
)
# Compute the dot respecting the fact of discrete/continuous spaces
if
bare
:
y
=
self
else
:
y
=
self
.
weight
(
power
=
1
)
y
=
self
.
weight
(
power
=
1
)
if
spaces
is
None
:
x_val
=
x
.
get_val
(
copy
=
False
)
...
...
nifty/library/critical_filter/critical_power_energy.py
View file @
bf3848ee
...
...
@@ -83,7 +83,7 @@ class CriticalPowerEnergy(Energy):
@
memo
def
value
(
self
):
energy
=
self
.
_theta
.
sum
()
energy
+=
self
.
position
.
vdot
(
self
.
_rho_prime
,
bare
=
True
)
energy
+=
self
.
position
.
weight
(
-
1
).
vdot
(
self
.
_rho_prime
)
energy
+=
0.5
*
self
.
position
.
vdot
(
self
.
_Tt
)
return
energy
.
real
...
...
nifty/operators/diagonal_operator/diagonal_operator.py
View file @
bf3848ee
...
...
@@ -42,9 +42,6 @@ class DiagonalOperator(EndomorphicOperator):
The domain on which the Operator's input Field lives.
diagonal : {scalar, list, array, Field, d2o-object}
The diagonal entries of the operator.
bare : boolean
Indicates whether the input for the diagonal is bare or not
(default: False).
copy : boolean
Internal copy of the diagonal (default: True)
distribution_strategy : string
...
...
@@ -73,16 +70,6 @@ class DiagonalOperator(EndomorphicOperator):
Raises
------
Notes
-----
The ambiguity of bare or non-bare diagonal entries is based on the choice
of a matrix representation of the operator in question. The naive choice
of absorbing the volume weights into the matrix leads to a matrix-vector
calculus with the non-bare entries which seems intuitive, though.
The choice of keeping matrix entries and volume weights separate
deals with the bare entries that allow for correct interpretation
of the matrix entries; e.g., as variance in case of an covariance operator.
Examples
--------
>>> x_space = RGSpace(5)
...
...
@@ -101,7 +88,7 @@ class DiagonalOperator(EndomorphicOperator):
# ---Overwritten properties and methods---
def
__init__
(
self
,
domain
=
(),
diagonal
=
None
,
bare
=
False
,
copy
=
True
,
def
__init__
(
self
,
domain
=
(),
diagonal
=
None
,
copy
=
True
,
distribution_strategy
=
None
,
default_spaces
=
None
):
super
(
DiagonalOperator
,
self
).
__init__
(
default_spaces
)
...
...
@@ -119,12 +106,12 @@ class DiagonalOperator(EndomorphicOperator):
self
.
_self_adjoint
=
None
self
.
_unitary
=
None
self
.
set_diagonal
(
diagonal
=
diagonal
,
bare
=
bare
,
copy
=
copy
)
self
.
set_diagonal
(
diagonal
=
diagonal
,
copy
=
copy
)
def
_add_attributes_to_copy
(
self
,
copy
,
**
kwargs
):
copy
.
_domain
=
self
.
_domain
copy
.
_distribution_strategy
=
self
.
_distribution_strategy
copy
.
set_diagonal
(
diagonal
=
self
.
diagonal
(
bare
=
True
),
bare
=
True
)
copy
.
set_diagonal
(
diagonal
=
self
.
diagonal
(
)
)
copy
.
_self_adjoint
=
self
.
_self_adjoint
copy
.
_unitary
=
self
.
_unitary
copy
=
super
(
DiagonalOperator
,
self
).
_add_attributes_to_copy
(
copy
,
...
...
@@ -146,13 +133,11 @@ class DiagonalOperator(EndomorphicOperator):
return
self
.
_times_helper
(
x
,
spaces
,
operation
=
lambda
z
:
z
.
conjugate
().
__rtruediv__
)
def
diagonal
(
self
,
bare
=
False
,
copy
=
True
):
def
diagonal
(
self
,
copy
=
True
):
""" Returns the diagonal of the Operator.
Parameters
----------
bare : boolean
Whether the returned Field values should be bare or not.
copy : boolean
Whether the returned Field should be copied or not.
...
...
@@ -162,29 +147,22 @@ class DiagonalOperator(EndomorphicOperator):
The diagonal of the Operator.
"""
if
bare
:
diagonal
=
self
.
_diagonal
.
weight
(
power
=-
1
)
elif
copy
:
if
copy
:
diagonal
=
self
.
_diagonal
.
copy
()
else
:
diagonal
=
self
.
_diagonal
return
diagonal
def
inverse_diagonal
(
self
,
bare
=
False
):
def
inverse_diagonal
(
self
):
""" Returns the inverse-diagonal of the operator.
Parameters
----------
bare : boolean
Whether the returned Field values should be bare or not.
Returns
-------
out : Field
The inverse of the diagonal of the Operator.
"""
return
1.
/
self
.
diagonal
(
bare
=
bare
,
copy
=
False
)
return
1.
/
self
.
diagonal
(
copy
=
False
)
# ---Mandatory properties and methods---
...
...
@@ -236,16 +214,13 @@ class DiagonalOperator(EndomorphicOperator):
"Invalid distribution_strategy!"
)
return
distribution_strategy
def
set_diagonal
(
self
,
diagonal
,
bare
=
False
,
copy
=
True
):
def
set_diagonal
(
self
,
diagonal
,
copy
=
True
):
""" Sets the diagonal of the Operator.
Parameters
----------
diagonal : {scalar, list, array, Field, d2o-object}
The diagonal entries of the operator.
bare : boolean
Indicates whether the input for the diagonal is bare or not
(default: False).
copy : boolean
Specifies if a copy of the input shall be made (default: True).
...
...
@@ -257,13 +232,6 @@ class DiagonalOperator(EndomorphicOperator):
distribution_strategy
=
self
.
distribution_strategy
,
copy
=
copy
)
# weight if the given values were `bare` is True
# do inverse weightening if the other way around
if
bare
:
# If `copy` is True, we won't change external data by weightening
# Otherwise, inplace weightening would change the external field
f
.
weight
(
inplace
=
copy
)
# Reset the self_adjoint property:
self
.
_self_adjoint
=
None
...
...
nifty/probing/mixin_classes/trace_prober_mixin.py
View file @
bf3848ee
...
...
@@ -37,9 +37,9 @@ class TraceProberMixin(object):
def
finish_probe
(
self
,
probe
,
pre_result
):
if
self
.
__evaluate_probe_in_signal_space
:
fft
=
create_composed_fft_operator
(
self
.
_domain
,
all_to
=
'position'
)
result
=
fft
(
probe
[
1
]).
vdot
(
fft
(
pre_result
)
,
bare
=
True
)
result
=
fft
(
probe
[
1
]).
weight
(
-
1
).
vdot
(
fft
(
pre_result
))
else
:
result
=
probe
[
1
].
vdot
(
pre_result
,
bare
=
True
)
result
=
probe
[
1
].
weight
(
-
1
).
vdot
(
pre_result
)
self
.
__sum_of_probings
+=
result
if
self
.
compute_variance
:
...
...
nifty/sugar.py
View file @
bf3848ee
...
...
@@ -78,7 +78,8 @@ def create_power_operator(domain, power_spectrum, dtype=None,
f
=
f
.
real
f
**=
2
return
DiagonalOperator
(
domain
,
diagonal
=
f
,
bare
=
True
)
diag
=
Field
(
domain
,
f
).
weight
()
return
DiagonalOperator
(
domain
,
diag
)
def
generate_posterior_sample
(
mean
,
covariance
):
...
...
@@ -111,7 +112,7 @@ def generate_posterior_sample(mean, covariance):
power
=
sqrt
(
S
.
diagonal
().
power_analyze
())
mock_signal
=
power
.
power_synthesize
(
real_signal
=
True
)
noise
=
N
.
diagonal
(
bare
=
True
)
noise
=
N
.
diagonal
(
).
weight
(
-
1
)
mock_noise
=
Field
.
from_random
(
random_type
=
"normal"
,
domain
=
N
.
domain
,
dtype
=
noise
.
dtype
)
...
...
test/test_operators/test_diagonal_operator.py
View file @
bf3848ee
...
...
@@ -17,8 +17,8 @@ from test.common import expand
class
DiagonalOperator_Tests
(
unittest
.
TestCase
):
spaces
=
generate_spaces
()
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_property
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_property
(
self
,
space
,
copy
):
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
)
if
D
.
domain
[
0
]
!=
space
:
...
...
@@ -28,53 +28,53 @@ class DiagonalOperator_Tests(unittest.TestCase):
if
D
.
self_adjoint
!=
True
:
raise
TypeError
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_times_adjoint
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_times_adjoint
(
self
,
space
,
copy
):
rand1
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
rand2
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
bare
=
bare
,
copy
=
copy
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
copy
=
copy
)
tt1
=
rand1
.
vdot
(
D
.
times
(
rand2
))
tt2
=
rand2
.
vdot
(
D
.
times
(
rand1
))
assert_approx_equal
(
tt1
,
tt2
)
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_times_inverse
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_times_inverse
(
self
,
space
,
copy
):
rand1
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
bare
=
bare
,
copy
=
copy
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
copy
=
copy
)
tt1
=
D
.
times
(
D
.
inverse_times
(
rand1
))
assert_allclose
(
rand1
.
val
.
get_full_data
(),
tt1
.
val
.
get_full_data
())
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_times
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_times
(
self
,
space
,
copy
):
rand1
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
bare
=
bare
,
copy
=
copy
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
copy
=
copy
)
tt
=
D
.
times
(
rand1
)
assert_equal
(
tt
.
domain
[
0
],
space
)
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_adjoint_times
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_adjoint_times
(
self
,
space
,
copy
):
rand1
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
bare
=
bare
,
copy
=
copy
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
copy
=
copy
)
tt
=
D
.
adjoint_times
(
rand1
)
assert_equal
(
tt
.
domain
[
0
],
space
)
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_inverse_times
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_inverse_times
(
self
,
space
,
copy
):
rand1
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
bare
=
bare
,
copy
=
copy
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
copy
=
copy
)
tt
=
D
.
inverse_times
(
rand1
)
assert_equal
(
tt
.
domain
[
0
],
space
)
@
expand
(
product
(
spaces
,
[
True
,
False
],
[
True
,
False
]))
def
test_adjoint_inverse_times
(
self
,
space
,
bare
,
copy
):
@
expand
(
product
(
spaces
,
[
True
,
False
]))
def
test_adjoint_inverse_times
(
self
,
space
,
copy
):
rand1
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
bare
=
bare
,
copy
=
copy
)
D
=
DiagonalOperator
(
space
,
diagonal
=
diag
,
copy
=
copy
)
tt
=
D
.
adjoint_inverse_times
(
rand1
)
assert_equal
(
tt
.
domain
[
0
],
space
)
...
...
Write
Preview
Supports
Markdown
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