Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
NIFTy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
13
Issues
13
List
Boards
Labels
Service Desk
Milestones
Merge Requests
13
Merge Requests
13
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ift
NIFTy
Commits
2e57bec1
Commit
2e57bec1
authored
Apr 04, 2020
by
Martin Reinecke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rework pointwise operations
parent
8a3b5d58
Pipeline
#72233
canceled with stages
in 1 minute and 49 seconds
Changes
21
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
74 additions
and
203 deletions
+74
-203
nifty6/domains/rg_space.py
nifty6/domains/rg_space.py
+1
-2
nifty6/field.py
nifty6/field.py
+8
-16
nifty6/library/correlated_fields.py
nifty6/library/correlated_fields.py
+12
-12
nifty6/library/dynamic_operator.py
nifty6/library/dynamic_operator.py
+5
-5
nifty6/library/special_distributions.py
nifty6/library/special_distributions.py
+1
-1
nifty6/linearization.py
nifty6/linearization.py
+5
-88
nifty6/multi_field.py
nifty6/multi_field.py
+6
-13
nifty6/operators/energy_operators.py
nifty6/operators/energy_operators.py
+5
-5
nifty6/operators/operator.py
nifty6/operators/operator.py
+3
-10
nifty6/sugar.py
nifty6/sugar.py
+1
-24
test/test_energy_gradients.py
test/test_energy_gradients.py
+5
-5
test/test_field.py
test/test_field.py
+2
-2
test/test_gaussian_energy.py
test/test_gaussian_energy.py
+2
-2
test/test_linearization.py
test/test_linearization.py
+8
-8
test/test_multi_field.py
test/test_multi_field.py
+1
-1
test/test_operators/test_convolution_operators.py
test/test_operators/test_convolution_operators.py
+1
-1
test/test_operators/test_correlated_fields.py
test/test_operators/test_correlated_fields.py
+1
-1
test/test_operators/test_interpolated.py
test/test_operators/test_interpolated.py
+1
-1
test/test_operators/test_jacobian.py
test/test_operators/test_jacobian.py
+1
-1
test/test_operators/test_partial_multifield_insert.py
test/test_operators/test_partial_multifield_insert.py
+3
-3
test/test_sugar.py
test/test_sugar.py
+2
-2
No files found.
nifty6/domains/rg_space.py
View file @
2e57bec1
...
@@ -142,8 +142,7 @@ class RGSpace(StructuredDomain):
...
@@ -142,8 +142,7 @@ class RGSpace(StructuredDomain):
@
staticmethod
@
staticmethod
def
_kernel
(
x
,
sigma
):
def
_kernel
(
x
,
sigma
):
from
..sugar
import
exp
return
(
x
*
x
*
(
-
2.
*
np
.
pi
*
np
.
pi
*
sigma
*
sigma
)).
ptw
(
"exp"
)
return
exp
(
x
*
x
*
(
-
2.
*
np
.
pi
*
np
.
pi
*
sigma
*
sigma
))
def
get_fft_smoothing_kernel_function
(
self
,
sigma
):
def
get_fft_smoothing_kernel_function
(
self
,
sigma
):
if
(
not
self
.
harmonic
):
if
(
not
self
.
harmonic
):
...
...
nifty6/field.py
View file @
2e57bec1
...
@@ -634,10 +634,9 @@ class Field(object):
...
@@ -634,10 +634,9 @@ class Field(object):
Field
Field
The result of the operation.
The result of the operation.
"""
"""
from
.sugar
import
sqrt
if
self
.
scalar_weight
(
spaces
)
is
not
None
:
if
self
.
scalar_weight
(
spaces
)
is
not
None
:
return
self
.
_contraction_helper
(
'std'
,
spaces
)
return
self
.
_contraction_helper
(
'std'
,
spaces
)
return
s
qrt
(
self
.
var
(
spaces
)
)
return
s
elf
.
var
(
spaces
).
ptw
(
"sqrt"
)
def
s_std
(
self
):
def
s_std
(
self
):
"""Determines the standard deviation of the Field.
"""Determines the standard deviation of the Field.
...
@@ -677,17 +676,11 @@ class Field(object):
...
@@ -677,17 +676,11 @@ class Field(object):
def
flexible_addsub
(
self
,
other
,
neg
):
def
flexible_addsub
(
self
,
other
,
neg
):
return
self
-
other
if
neg
else
self
+
other
return
self
-
other
if
neg
else
self
+
other
def
sigmoid
(
self
):
return
0.5
*
(
1.
+
self
.
tanh
())
def
clip
(
self
,
min
=
None
,
max
=
None
):
def
clip
(
self
,
min
=
None
,
max
=
None
):
min
=
min
.
val
if
isinstance
(
min
,
Field
)
else
min
min
=
min
.
val
if
isinstance
(
min
,
Field
)
else
min
max
=
max
.
val
if
isinstance
(
max
,
Field
)
else
max
max
=
max
.
val
if
isinstance
(
max
,
Field
)
else
max
return
Field
(
self
.
_domain
,
np
.
clip
(
self
.
_val
,
min
,
max
))
return
Field
(
self
.
_domain
,
np
.
clip
(
self
.
_val
,
min
,
max
))
def
one_over
(
self
):
return
1
/
self
def
_binary_op
(
self
,
other
,
op
):
def
_binary_op
(
self
,
other
,
op
):
# if other is a field, make sure that the domains match
# if other is a field, make sure that the domains match
f
=
getattr
(
self
.
_val
,
op
)
f
=
getattr
(
self
.
_val
,
op
)
...
@@ -699,6 +692,13 @@ class Field(object):
...
@@ -699,6 +692,13 @@ class Field(object):
return
Field
(
self
.
_domain
,
f
(
other
))
return
Field
(
self
.
_domain
,
f
(
other
))
return
NotImplemented
return
NotImplemented
def
ptw
(
self
,
op
,
with_deriv
=
False
):
from
.pointwise
import
ptw_dict
if
with_deriv
:
tmp
=
ptw_dict
[
op
][
1
](
self
.
_val
)
return
(
Field
(
self
.
_domain
,
tmp
[
0
]),
Field
(
self
.
_domain
,
tmp
[
1
]))
return
Field
(
self
.
_domain
,
ptw_dict
[
op
][
0
](
self
.
_val
))
for
op
in
[
"__add__"
,
"__radd__"
,
for
op
in
[
"__add__"
,
"__radd__"
,
"__sub__"
,
"__rsub__"
,
"__sub__"
,
"__rsub__"
,
...
@@ -721,11 +721,3 @@ for op in ["__iadd__", "__isub__", "__imul__", "__idiv__",
...
@@ -721,11 +721,3 @@ for op in ["__iadd__", "__isub__", "__imul__", "__idiv__",
"In-place operations are deliberately not supported"
)
"In-place operations are deliberately not supported"
)
return
func2
return
func2
setattr
(
Field
,
op
,
func
(
op
))
setattr
(
Field
,
op
,
func
(
op
))
for
f
in
[
"sqrt"
,
"exp"
,
"log"
,
"sin"
,
"cos"
,
"tan"
,
"sinh"
,
"cosh"
,
"tanh"
,
"absolute"
,
"sinc"
,
"sign"
,
"log10"
,
"log1p"
,
"expm1"
]:
def
func
(
f
):
def
func2
(
self
):
return
Field
(
self
.
_domain
,
getattr
(
np
,
f
)(
self
.
val
))
return
func2
setattr
(
Field
,
f
,
func
(
f
))
nifty6/library/correlated_fields.py
View file @
2e57bec1
...
@@ -126,7 +126,7 @@ class _LognormalMomentMatching(Operator):
...
@@ -126,7 +126,7 @@ class _LognormalMomentMatching(Operator):
logmean
,
logsig
=
_lognormal_moments
(
mean
,
sig
,
N_copies
)
logmean
,
logsig
=
_lognormal_moments
(
mean
,
sig
,
N_copies
)
self
.
_mean
=
mean
self
.
_mean
=
mean
self
.
_sig
=
sig
self
.
_sig
=
sig
op
=
_normal
(
logmean
,
logsig
,
key
,
N_copies
).
exp
(
)
op
=
_normal
(
logmean
,
logsig
,
key
,
N_copies
).
ptw
(
"exp"
)
self
.
_domain
,
self
.
_target
=
op
.
domain
,
op
.
target
self
.
_domain
,
self
.
_target
=
op
.
domain
,
op
.
target
self
.
apply
=
op
.
apply
self
.
apply
=
op
.
apply
...
@@ -224,8 +224,8 @@ class _Normalization(Operator):
...
@@ -224,8 +224,8 @@ class _Normalization(Operator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
amp
=
x
.
exp
(
)
amp
=
x
.
ptw
(
"exp"
)
spec
=
(
2
*
x
).
exp
()
spec
=
amp
*
amp
# FIXME This normalizes also the zeromode which is supposed to be left
# FIXME This normalizes also the zeromode which is supposed to be left
# untouched by this operator
# untouched by this operator
return
self
.
_specsum
(
self
.
_mode_multiplicity
(
spec
))
**
(
-
0.5
)
*
amp
return
self
.
_specsum
(
self
.
_mode_multiplicity
(
spec
))
**
(
-
0.5
)
*
amp
...
@@ -332,17 +332,17 @@ class _Amplitude(Operator):
...
@@ -332,17 +332,17 @@ class _Amplitude(Operator):
sig_fluc
=
vol1
@
ps_expander
@
fluctuations
sig_fluc
=
vol1
@
ps_expander
@
fluctuations
xi
=
ducktape
(
dom
,
None
,
key
)
xi
=
ducktape
(
dom
,
None
,
key
)
sigma
=
sig_flex
*
(
Adder
(
shift
)
@
sig_asp
).
sqrt
(
)
sigma
=
sig_flex
*
(
Adder
(
shift
)
@
sig_asp
).
ptw
(
"sqrt"
)
smooth
=
_SlopeRemover
(
target
,
space
)
@
twolog
@
(
sigma
*
xi
)
smooth
=
_SlopeRemover
(
target
,
space
)
@
twolog
@
(
sigma
*
xi
)
op
=
_Normalization
(
target
,
space
)
@
(
slope
+
smooth
)
op
=
_Normalization
(
target
,
space
)
@
(
slope
+
smooth
)
if
N_copies
>
0
:
if
N_copies
>
0
:
op
=
Distributor
@
op
op
=
Distributor
@
op
sig_fluc
=
Distributor
@
sig_fluc
sig_fluc
=
Distributor
@
sig_fluc
op
=
Adder
(
Distributor
(
vol0
))
@
(
sig_fluc
*
(
azm_expander
@
azm
.
one_over
(
))
*
op
)
op
=
Adder
(
Distributor
(
vol0
))
@
(
sig_fluc
*
(
azm_expander
@
azm
.
ptw
(
"reciprocal"
))
*
op
)
self
.
_fluc
=
(
_Distributor
(
dofdex
,
fluctuations
.
target
,
self
.
_fluc
=
(
_Distributor
(
dofdex
,
fluctuations
.
target
,
distributed_tgt
[
0
])
@
fluctuations
)
distributed_tgt
[
0
])
@
fluctuations
)
else
:
else
:
op
=
Adder
(
vol0
)
@
(
sig_fluc
*
(
azm_expander
@
azm
.
one_over
(
))
*
op
)
op
=
Adder
(
vol0
)
@
(
sig_fluc
*
(
azm_expander
@
azm
.
ptw
(
"reciprocal"
))
*
op
)
self
.
_fluc
=
fluctuations
self
.
_fluc
=
fluctuations
self
.
apply
=
op
.
apply
self
.
apply
=
op
.
apply
...
@@ -527,7 +527,7 @@ class CorrelatedFieldMaker:
...
@@ -527,7 +527,7 @@ class CorrelatedFieldMaker:
for
_
in
range
(
prior_info
):
for
_
in
range
(
prior_info
):
sc
.
add
(
op
(
from_random
(
'normal'
,
op
.
domain
)))
sc
.
add
(
op
(
from_random
(
'normal'
,
op
.
domain
)))
mean
=
sc
.
mean
.
val
mean
=
sc
.
mean
.
val
stddev
=
sc
.
var
.
sqrt
(
).
val
stddev
=
sc
.
var
.
ptw
(
"sqrt"
).
val
for
m
,
s
in
zip
(
mean
.
flatten
(),
stddev
.
flatten
()):
for
m
,
s
in
zip
(
mean
.
flatten
(),
stddev
.
flatten
()):
logger
.
info
(
'{}: {:.02E} ± {:.02E}'
.
format
(
kk
,
m
,
s
))
logger
.
info
(
'{}: {:.02E} ± {:.02E}'
.
format
(
kk
,
m
,
s
))
...
@@ -539,7 +539,7 @@ class CorrelatedFieldMaker:
...
@@ -539,7 +539,7 @@ class CorrelatedFieldMaker:
from
..sugar
import
from_random
from
..sugar
import
from_random
scm
=
1.
scm
=
1.
for
a
in
self
.
_a
:
for
a
in
self
.
_a
:
op
=
a
.
fluctuation_amplitude
*
self
.
_azm
.
one_over
(
)
op
=
a
.
fluctuation_amplitude
*
self
.
_azm
.
ptw
(
"reciprocal"
)
res
=
np
.
array
([
op
(
from_random
(
'normal'
,
op
.
domain
)).
val
res
=
np
.
array
([
op
(
from_random
(
'normal'
,
op
.
domain
)).
val
for
_
in
range
(
nsamples
)])
for
_
in
range
(
nsamples
)])
scm
*=
res
**
2
+
1.
scm
*=
res
**
2
+
1.
...
@@ -573,9 +573,9 @@ class CorrelatedFieldMaker:
...
@@ -573,9 +573,9 @@ class CorrelatedFieldMaker:
return
self
.
average_fluctuation
(
0
)
return
self
.
average_fluctuation
(
0
)
q
=
1.
q
=
1.
for
a
in
self
.
_a
:
for
a
in
self
.
_a
:
fl
=
a
.
fluctuation_amplitude
*
self
.
_azm
.
one_over
(
)
fl
=
a
.
fluctuation_amplitude
*
self
.
_azm
.
ptw
(
"reciprocal"
)
q
=
q
*
(
Adder
(
full
(
fl
.
target
,
1.
))
@
fl
**
2
)
q
=
q
*
(
Adder
(
full
(
fl
.
target
,
1.
))
@
fl
**
2
)
return
(
Adder
(
full
(
q
.
target
,
-
1.
))
@
q
).
sqrt
(
)
*
self
.
_azm
return
(
Adder
(
full
(
q
.
target
,
-
1.
))
@
q
).
ptw
(
"sqrt"
)
*
self
.
_azm
def
slice_fluctuation
(
self
,
space
):
def
slice_fluctuation
(
self
,
space
):
"""Returns operator which acts on prior or posterior samples"""
"""Returns operator which acts on prior or posterior samples"""
...
@@ -587,12 +587,12 @@ class CorrelatedFieldMaker:
...
@@ -587,12 +587,12 @@ class CorrelatedFieldMaker:
return
self
.
average_fluctuation
(
0
)
return
self
.
average_fluctuation
(
0
)
q
=
1.
q
=
1.
for
j
in
range
(
len
(
self
.
_a
)):
for
j
in
range
(
len
(
self
.
_a
)):
fl
=
self
.
_a
[
j
].
fluctuation_amplitude
*
self
.
_azm
.
one_over
(
)
fl
=
self
.
_a
[
j
].
fluctuation_amplitude
*
self
.
_azm
.
ptw
(
"reciprocal"
)
if
j
==
space
:
if
j
==
space
:
q
=
q
*
fl
**
2
q
=
q
*
fl
**
2
else
:
else
:
q
=
q
*
(
Adder
(
full
(
fl
.
target
,
1.
))
@
fl
**
2
)
q
=
q
*
(
Adder
(
full
(
fl
.
target
,
1.
))
@
fl
**
2
)
return
q
.
sqrt
(
)
*
self
.
_azm
return
q
.
ptw
(
"sqrt"
)
*
self
.
_azm
def
average_fluctuation
(
self
,
space
):
def
average_fluctuation
(
self
,
space
):
"""Returns operator which acts on prior or posterior samples"""
"""Returns operator which acts on prior or posterior samples"""
...
...
nifty6/library/dynamic_operator.py
View file @
2e57bec1
...
@@ -97,9 +97,9 @@ def _make_dynamic_operator(target, harmonic_padding, sm_s0, sm_x0, cone, keys, c
...
@@ -97,9 +97,9 @@ def _make_dynamic_operator(target, harmonic_padding, sm_s0, sm_x0, cone, keys, c
m
=
CentralPadd
.
adjoint
(
FFTB
(
Sm
(
m
)))
m
=
CentralPadd
.
adjoint
(
FFTB
(
Sm
(
m
)))
ops
[
'smoothed_dynamics'
]
=
m
ops
[
'smoothed_dynamics'
]
=
m
m
=
-
m
.
log
(
)
m
=
-
m
.
ptw
(
"log"
)
if
not
minimum_phase
:
if
not
minimum_phase
:
m
=
m
.
exp
(
)
m
=
m
.
ptw
(
"exp"
)
if
causal
or
minimum_phase
:
if
causal
or
minimum_phase
:
m
=
Real
.
adjoint
(
FFT
.
inverse
(
Realizer
(
FFT
.
target
).
adjoint
(
m
)))
m
=
Real
.
adjoint
(
FFT
.
inverse
(
Realizer
(
FFT
.
target
).
adjoint
(
m
)))
kernel
=
makeOp
(
kernel
=
makeOp
(
...
@@ -114,19 +114,19 @@ def _make_dynamic_operator(target, harmonic_padding, sm_s0, sm_x0, cone, keys, c
...
@@ -114,19 +114,19 @@ def _make_dynamic_operator(target, harmonic_padding, sm_s0, sm_x0, cone, keys, c
c
=
FieldAdapter
(
UnstructuredDomain
(
len
(
sigc
)),
keys
[
1
])
c
=
FieldAdapter
(
UnstructuredDomain
(
len
(
sigc
)),
keys
[
1
])
c
=
makeOp
(
Field
(
c
.
target
,
np
.
array
(
sigc
)))(
c
)
c
=
makeOp
(
Field
(
c
.
target
,
np
.
array
(
sigc
)))(
c
)
lightspeed
=
ScalingOperator
(
c
.
target
,
-
0.5
)(
c
).
exp
(
)
lightspeed
=
ScalingOperator
(
c
.
target
,
-
0.5
)(
c
).
ptw
(
"exp"
)
scaling
=
np
.
array
(
m
.
target
[
0
].
distances
[
1
:])
/
m
.
target
[
0
].
distances
[
0
]
scaling
=
np
.
array
(
m
.
target
[
0
].
distances
[
1
:])
/
m
.
target
[
0
].
distances
[
0
]
scaling
=
DiagonalOperator
(
Field
(
c
.
target
,
scaling
))
scaling
=
DiagonalOperator
(
Field
(
c
.
target
,
scaling
))
ops
[
'lightspeed'
]
=
scaling
(
lightspeed
)
ops
[
'lightspeed'
]
=
scaling
(
lightspeed
)
c
=
LightConeOperator
(
c
.
target
,
m
.
target
,
quant
)
@
c
.
exp
(
)
c
=
LightConeOperator
(
c
.
target
,
m
.
target
,
quant
)
@
c
.
ptw
(
"exp"
)
ops
[
'light_cone'
]
=
c
ops
[
'light_cone'
]
=
c
m
=
c
*
m
m
=
c
*
m
if
causal
or
minimum_phase
:
if
causal
or
minimum_phase
:
m
=
FFT
(
Real
(
m
))
m
=
FFT
(
Real
(
m
))
if
minimum_phase
:
if
minimum_phase
:
m
=
m
.
exp
(
)
m
=
m
.
ptw
(
"exp"
)
return
m
,
ops
return
m
,
ops
...
...
nifty6/library/special_distributions.py
View file @
2e57bec1
...
@@ -120,7 +120,7 @@ def InverseGammaOperator(domain, alpha, q, delta=1e-2):
...
@@ -120,7 +120,7 @@ def InverseGammaOperator(domain, alpha, q, delta=1e-2):
Distance between sampling points for linear interpolation.
Distance between sampling points for linear interpolation.
"""
"""
op
=
_InterpolationOperator
(
domain
,
lambda
x
:
invgamma
.
ppf
(
norm
.
_cdf
(
x
),
float
(
alpha
)),
op
=
_InterpolationOperator
(
domain
,
lambda
x
:
invgamma
.
ppf
(
norm
.
_cdf
(
x
),
float
(
alpha
)),
-
8.2
,
8.2
,
delta
,
lambda
x
:
x
.
log
(),
lambda
x
:
x
.
exp
(
))
-
8.2
,
8.2
,
delta
,
lambda
x
:
x
.
ptw
(
"log"
),
lambda
x
:
x
.
ptw
(
"exp"
))
if
np
.
isscalar
(
q
):
if
np
.
isscalar
(
q
):
return
op
.
scale
(
q
)
return
op
.
scale
(
q
)
return
makeOp
(
q
)
@
op
return
makeOp
(
q
)
@
op
...
...
nifty6/linearization.py
View file @
2e57bec1
...
@@ -166,7 +166,7 @@ class Linearization(object):
...
@@ -166,7 +166,7 @@ class Linearization(object):
return
self
.
__mul__
(
1.
/
other
)
return
self
.
__mul__
(
1.
/
other
)
def
__rtruediv__
(
self
,
other
):
def
__rtruediv__
(
self
,
other
):
return
self
.
one_over
().
__mul__
(
other
)
return
self
.
reciprocal
().
__mul__
(
other
)
def
__pow__
(
self
,
power
):
def
__pow__
(
self
,
power
):
if
not
np
.
isscalar
(
power
):
if
not
np
.
isscalar
(
power
):
...
@@ -282,9 +282,10 @@ class Linearization(object):
...
@@ -282,9 +282,10 @@ class Linearization(object):
self
.
_val
.
integrate
(
spaces
),
self
.
_val
.
integrate
(
spaces
),
ContractionOperator
(
self
.
_jac
.
target
,
spaces
,
1
)(
self
.
_jac
))
ContractionOperator
(
self
.
_jac
.
target
,
spaces
,
1
)(
self
.
_jac
))
def
exp
(
self
):
def
ptw
(
self
,
op
):
tmp
=
self
.
_val
.
exp
()
from
.pointwise
import
ptw_dict
return
self
.
new
(
tmp
,
makeOp
(
tmp
)(
self
.
_jac
))
t1
,
t2
=
self
.
_val
.
ptw
(
op
,
True
)
return
self
.
new
(
t1
,
makeOp
(
t2
)(
self
.
_jac
))
def
clip
(
self
,
min
=
None
,
max
=
None
):
def
clip
(
self
,
min
=
None
,
max
=
None
):
tmp
=
self
.
_val
.
clip
(
min
,
max
)
tmp
=
self
.
_val
.
clip
(
min
,
max
)
...
@@ -298,90 +299,6 @@ class Linearization(object):
...
@@ -298,90 +299,6 @@ class Linearization(object):
tmp2
=
makeOp
(
1.
-
(
tmp
==
min
)
-
(
tmp
==
max
))
tmp2
=
makeOp
(
1.
-
(
tmp
==
min
)
-
(
tmp
==
max
))
return
self
.
new
(
tmp
,
tmp2
(
self
.
_jac
))
return
self
.
new
(
tmp
,
tmp2
(
self
.
_jac
))
def
sqrt
(
self
):
tmp
=
self
.
_val
.
sqrt
()
return
self
.
new
(
tmp
,
makeOp
(
0.5
/
tmp
)(
self
.
_jac
))
def
sin
(
self
):
tmp
=
self
.
_val
.
sin
()
tmp2
=
self
.
_val
.
cos
()
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
cos
(
self
):
tmp
=
self
.
_val
.
cos
()
tmp2
=
-
self
.
_val
.
sin
()
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
tan
(
self
):
tmp
=
self
.
_val
.
tan
()
tmp2
=
1.
/
(
self
.
_val
.
cos
()
**
2
)
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
sinc
(
self
):
tmp
=
self
.
_val
.
sinc
()
tmp2
=
((
np
.
pi
*
self
.
_val
).
cos
()
-
tmp
)
/
self
.
_val
ind
=
self
.
_val
.
val
==
0
loc
=
tmp2
.
val_rw
()
loc
[
ind
]
=
0
tmp2
=
Field
(
tmp
.
domain
,
loc
)
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
log
(
self
):
tmp
=
self
.
_val
.
log
()
return
self
.
new
(
tmp
,
makeOp
(
1.
/
self
.
_val
)(
self
.
_jac
))
def
log10
(
self
):
tmp
=
self
.
_val
.
log10
()
tmp2
=
1.
/
(
self
.
_val
*
np
.
log
(
10
))
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
log1p
(
self
):
tmp
=
self
.
_val
.
log1p
()
tmp2
=
1.
/
(
1.
+
self
.
_val
)
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
jac
))
def
expm1
(
self
):
tmp
=
self
.
_val
.
expm1
()
tmp2
=
self
.
_val
.
exp
()
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
jac
))
def
sinh
(
self
):
tmp
=
self
.
_val
.
sinh
()
tmp2
=
self
.
_val
.
cosh
()
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
cosh
(
self
):
tmp
=
self
.
_val
.
cosh
()
tmp2
=
self
.
_val
.
sinh
()
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
tanh
(
self
):
tmp
=
self
.
_val
.
tanh
()
return
self
.
new
(
tmp
,
makeOp
(
1.
-
tmp
**
2
)(
self
.
_jac
))
def
sigmoid
(
self
):
tmp
=
self
.
_val
.
tanh
()
tmp2
=
0.5
*
(
1.
+
tmp
)
return
self
.
new
(
tmp2
,
makeOp
(
0.5
*
(
1.
-
tmp
**
2
))(
self
.
_jac
))
def
absolute
(
self
):
if
utilities
.
iscomplextype
(
self
.
_val
.
dtype
):
raise
TypeError
(
"Argument must not be complex"
)
tmp
=
self
.
_val
.
absolute
()
tmp2
=
self
.
_val
.
sign
()
ind
=
self
.
_val
.
val
==
0
loc
=
tmp2
.
val_rw
().
astype
(
float
)
loc
[
ind
]
=
np
.
nan
tmp2
=
Field
(
tmp
.
domain
,
loc
)
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
one_over
(
self
):
tmp
=
1.
/
self
.
_val
tmp2
=
-
tmp
/
self
.
_val
return
self
.
new
(
tmp
,
makeOp
(
tmp2
)(
self
.
_jac
))
def
add_metric
(
self
,
metric
):
def
add_metric
(
self
,
metric
):
return
self
.
new
(
self
.
_val
,
self
.
_jac
,
metric
)
return
self
.
new
(
self
.
_val
,
self
.
_jac
,
metric
)
...
...
nifty6/multi_field.py
View file @
2e57bec1
...
@@ -310,8 +310,12 @@ class MultiField(object):
...
@@ -310,8 +310,12 @@ class MultiField(object):
res
[
key
]
=
-
val
if
neg
else
val
res
[
key
]
=
-
val
if
neg
else
val
return
MultiField
.
from_dict
(
res
)
return
MultiField
.
from_dict
(
res
)
def
one_over
(
self
):
def
ptw
(
self
,
op
,
with_deriv
=
False
):
return
1
/
self
tmp
=
tuple
(
val
.
ptw
(
op
,
with_deriv
)
for
val
in
self
.
values
())
if
with_deriv
:
return
(
MultiField
(
self
.
domain
,
tuple
(
v
[
0
]
for
v
in
tmp
)),
MultiField
(
self
.
domain
,
tuple
(
v
[
1
]
for
v
in
tmp
)))
return
MultiField
(
self
.
domain
,
tmp
)
def
_binary_op
(
self
,
other
,
op
):
def
_binary_op
(
self
,
other
,
op
):
f
=
getattr
(
Field
,
op
)
f
=
getattr
(
Field
,
op
)
...
@@ -347,14 +351,3 @@ for op in ["__iadd__", "__isub__", "__imul__", "__idiv__",
...
@@ -347,14 +351,3 @@ for op in ["__iadd__", "__isub__", "__imul__", "__idiv__",
"In-place operations are deliberately not supported"
)
"In-place operations are deliberately not supported"
)
return
func2
return
func2
setattr
(
MultiField
,
op
,
func
(
op
))
setattr
(
MultiField
,
op
,
func
(
op
))
for
f
in
[
"sqrt"
,
"exp"
,
"log"
,
"sin"
,
"cos"
,
"tan"
,
"sinh"
,
"cosh"
,
"tanh"
,
"absolute"
,
"sinc"
,
"sign"
,
"log10"
,
"log1p"
,
"expm1"
]:
def
func
(
f
):
def
func2
(
self
):
fu
=
getattr
(
Field
,
f
)
return
MultiField
(
self
.
domain
,
tuple
(
fu
(
val
)
for
val
in
self
.
values
()))
return
func2
setattr
(
MultiField
,
f
,
func
(
f
))
nifty6/operators/energy_operators.py
View file @
2e57bec1
...
@@ -127,7 +127,7 @@ class VariableCovarianceGaussianEnergy(EnergyOperator):
...
@@ -127,7 +127,7 @@ class VariableCovarianceGaussianEnergy(EnergyOperator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
res
=
0.5
*
(
x
[
self
.
_r
].
vdot
(
x
[
self
.
_r
]
*
x
[
self
.
_icov
]).
real
-
x
[
self
.
_icov
].
log
(
).
sum
())
res
=
0.5
*
(
x
[
self
.
_r
].
vdot
(
x
[
self
.
_r
]
*
x
[
self
.
_icov
]).
real
-
x
[
self
.
_icov
].
ptw
(
"log"
).
sum
())
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
return
res
return
res
mf
=
{
self
.
_r
:
x
.
val
[
self
.
_icov
],
self
.
_icov
:
.
5
*
x
.
val
[
self
.
_icov
]
**
(
-
2
)}
mf
=
{
self
.
_r
:
x
.
val
[
self
.
_icov
],
self
.
_icov
:
.
5
*
x
.
val
[
self
.
_icov
]
**
(
-
2
)}
...
@@ -229,7 +229,7 @@ class PoissonianEnergy(EnergyOperator):
...
@@ -229,7 +229,7 @@ class PoissonianEnergy(EnergyOperator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
res
=
x
.
sum
()
-
x
.
log
(
).
vdot
(
self
.
_d
)
res
=
x
.
sum
()
-
x
.
ptw
(
"log"
).
vdot
(
self
.
_d
)
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
return
res
return
res
return
res
.
add_metric
(
makeOp
(
1.
/
x
.
val
))
return
res
.
add_metric
(
makeOp
(
1.
/
x
.
val
))
...
@@ -269,7 +269,7 @@ class InverseGammaLikelihood(EnergyOperator):
...
@@ -269,7 +269,7 @@ class InverseGammaLikelihood(EnergyOperator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
res
=
x
.
log
().
vdot
(
self
.
_alphap1
)
+
x
.
one_over
(
).
vdot
(
self
.
_beta
)
res
=
x
.
ptw
(
"log"
).
vdot
(
self
.
_alphap1
)
+
x
.
ptw
(
"reciprocal"
).
vdot
(
self
.
_beta
)
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
return
res
return
res
return
res
.
add_metric
(
makeOp
(
self
.
_alphap1
/
(
x
.
val
**
2
)))
return
res
.
add_metric
(
makeOp
(
self
.
_alphap1
/
(
x
.
val
**
2
)))
...
@@ -298,7 +298,7 @@ class StudentTEnergy(EnergyOperator):
...
@@ -298,7 +298,7 @@ class StudentTEnergy(EnergyOperator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
res
=
((
self
.
_theta
+
1
)
/
2
)
*
(
x
**
2
/
self
.
_theta
).
log1p
(
).
sum
()
res
=
((
self
.
_theta
+
1
)
/
2
)
*
(
x
**
2
/
self
.
_theta
).
ptw
(
"log1p"
).
sum
()
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
return
res
return
res
met
=
ScalingOperator
(
self
.
domain
,
(
self
.
_theta
+
1
)
/
(
self
.
_theta
+
3
))
met
=
ScalingOperator
(
self
.
domain
,
(
self
.
_theta
+
1
)
/
(
self
.
_theta
+
3
))
...
@@ -332,7 +332,7 @@ class BernoulliEnergy(EnergyOperator):
...
@@ -332,7 +332,7 @@ class BernoulliEnergy(EnergyOperator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
res
=
-
x
.
log
().
vdot
(
self
.
_d
)
+
(
1.
-
x
).
log
(
).
vdot
(
self
.
_d
-
1.
)
res
=
-
x
.
ptw
(
"log"
).
vdot
(
self
.
_d
)
+
(
1.
-
x
).
ptw
(
"log"
).
vdot
(
self
.
_d
-
1.
)
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
if
not
isinstance
(
x
,
Linearization
)
or
not
x
.
want_metric
:
return
res
return
res
return
res
.
add_metric
(
makeOp
(
1.
/
(
x
.
val
*
(
1.
-
x
.
val
))))
return
res
.
add_metric
(
makeOp
(
1.
/
(
x
.
val
*
(
1.
-
x
.
val
))))
...
...
nifty6/operators/operator.py
View file @
2e57bec1
...
@@ -222,15 +222,8 @@ class Operator(metaclass=NiftyMeta):
...
@@ -222,15 +222,8 @@ class Operator(metaclass=NiftyMeta):
def
_simplify_for_constant_input_nontrivial
(
self
,
c_inp
):
def
_simplify_for_constant_input_nontrivial
(
self
,
c_inp
):
return
None
,
self
return
None
,
self
def
ptw
(
self
,
op
):
for
f
in
[
"sqrt"
,
"exp"
,
"log"
,
"sin"
,
"cos"
,
"tan"
,
"sinh"
,
"cosh"
,
"tanh"
,
return
_OpChain
.
make
((
_FunctionApplier
(
self
.
target
,
op
),
self
))
"sinc"
,
"sigmoid"
,
"absolute"
,
"one_over"
,
"log10"
,
"log1p"
,
"expm1"
]:
def
func
(
f
):
def
func2
(
self
):
fa
=
_FunctionApplier
(
self
.
target
,
f
)
return
_OpChain
.
make
((
fa
,
self
))
return
func2
setattr
(
Operator
,
f
,
func
(
f
))
class
_ConstCollector
(
object
):
class
_ConstCollector
(
object
):
...
@@ -301,7 +294,7 @@ class _FunctionApplier(Operator):
...
@@ -301,7 +294,7 @@ class _FunctionApplier(Operator):
def
apply
(
self
,
x
):
def
apply
(
self
,
x
):
self
.
_check_input
(
x
)
self
.
_check_input
(
x
)
return
getattr
(
x
,
self
.
_funcname
)(
)
return
x
.
ptw
(
self
.
_funcname
)
class
_Clipper
(
Operator
):
class
_Clipper
(
Operator
):
...
...
nifty6/sugar.py
View file @
2e57bec1
...
@@ -37,10 +37,7 @@ from .plot import Plot
...
@@ -37,10 +37,7 @@ from .plot import Plot
__all__
=
[
'PS_field'
,
'power_analyze'
,
'create_power_operator'
,
__all__
=
[
'PS_field'
,
'power_analyze'
,
'create_power_operator'
,
'create_harmonic_smoothing_operator'
,
'from_random'
,
'create_harmonic_smoothing_operator'
,
'from_random'
,
'full'
,
'makeField'
,
'full'
,
'makeField'
,
'makeDomain'
,
'sqrt'
,
'exp'
,
'log'
,
'tanh'
,
'sigmoid'
,
'makeDomain'
,
'get_signal_variance'
,
'makeOp'
,
'domain_union'
,
'sin'
,
'cos'
,
'tan'
,
'sinh'
,
'cosh'
,
'log10'
,
'absolute'
,
'one_over'
,
'clip'
,
'sinc'
,
"log1p"
,
"expm1"
,
'conjugate'
,
'get_signal_variance'
,
'makeOp'
,
'domain_union'
,
'get_default_codomain'
,
'single_plot'
,
'exec_time'
,
'get_default_codomain'
,
'single_plot'
,
'exec_time'
,
'calculate_position'
]
'calculate_position'
]
...
@@ -366,26 +363,6 @@ def domain_union(domains):
...
@@ -366,26 +363,6 @@ def domain_union(domains):
return
MultiDomain
.
union
(
domains
)
return
MultiDomain
.
union
(
domains
)
# Arithmetic functions working on Fields
_current_module
=
sys
.
modules
[
__name__
]
for
f
in
[
"sqrt"
,
"exp"
,
"log"
,
"log10"
,
"tanh"
,
"sigmoid"
,
"conjugate"
,
'sin'
,
'cos'
,
'tan'
,
'sinh'
,
'cosh'
,
'absolute'
,
'one_over'
,
'sinc'
,
'log1p'
,
'expm1'
]:
def
func
(
f
):
def
func2
(
x
):
from
.linearization
import
Linearization
from
.operators.operator
import
Operator
if
isinstance
(
x
,
(
Field
,
MultiField
,
Linearization
,
Operator
)):
return
getattr
(
x
,
f
)()
else
:
return
getattr
(
np
,
f
)(
x
)
return
func2