Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ift
NIFTy
Commits
030321eb
Commit
030321eb
authored
Nov 17, 2019
by
Philipp Haim
Browse files
First working implementation
parent
db4db3c2
Changes
1
Hide whitespace changes
Inline
Side-by-side
nifty5/library/correlated_fields.py
View file @
030321eb
...
@@ -39,15 +39,18 @@ from ..sugar import from_global_data, from_random, full, makeDomain, get_default
...
@@ -39,15 +39,18 @@ from ..sugar import from_global_data, from_random, full, makeDomain, get_default
def
_reshaper
(
x
,
N
):
def
_reshaper
(
x
,
N
):
x
=
np
.
asfarray
(
x
)
x
=
np
.
asfarray
(
x
)
if
x
.
shape
in
[(),
(
1
,)]:
if
x
.
shape
in
[(),
(
1
,)]:
return
np
.
full
(
N
,
x
)
if
N
!=
1
else
x
.
reshape
(())
return
np
.
full
(
N
,
x
)
if
N
!=
0
else
x
.
reshape
(())
elif
x
.
shape
==
(
N
,):
elif
x
.
shape
==
(
N
,):
return
x
return
x
else
:
else
:
raise
TypeError
(
"Shape of parameters cannot be interpreted"
)
raise
TypeError
(
"Shape of parameters cannot be interpreted"
)
def
_lognormal_moments
(
mean
,
sig
,
N
=
1
):
def
_lognormal_moments
(
mean
,
sig
,
N
=
0
):
mean
,
sig
=
(
_reshaper
(
param
,
N
)
for
param
in
(
mean
,
sig
))
if
N
==
0
:
mean
,
sig
=
np
.
asfarray
(
mean
),
np
.
asfarray
(
sig
)
else
:
mean
,
sig
=
(
_reshaper
(
param
,
N
)
for
param
in
(
mean
,
sig
))
assert
np
.
all
(
mean
>
0
)
assert
np
.
all
(
mean
>
0
)
assert
np
.
all
(
sig
>
0
)
assert
np
.
all
(
sig
>
0
)
logsig
=
np
.
sqrt
(
np
.
log
((
sig
/
mean
)
**
2
+
1
))
logsig
=
np
.
sqrt
(
np
.
log
((
sig
/
mean
)
**
2
+
1
))
...
@@ -55,12 +58,13 @@ def _lognormal_moments(mean, sig, N = 1):
...
@@ -55,12 +58,13 @@ def _lognormal_moments(mean, sig, N = 1):
return
logmean
,
logsig
return
logmean
,
logsig
def
_normal
(
mean
,
sig
,
key
,
N
=
1
):
def
_normal
(
mean
,
sig
,
key
,
N
=
0
):
if
N
==
1
:
if
N
==
0
:
domain
=
DomainTuple
.
scalar_domain
()
domain
=
DomainTuple
.
scalar_domain
()
mean
,
sig
=
np
.
asfarray
(
mean
),
np
.
asfarray
(
sig
)
else
:
else
:
domain
=
UnstructuredDomain
(
N
)
domain
=
UnstructuredDomain
(
N
)
mean
,
sig
=
(
_reshaper
(
param
,
N
)
for
param
in
(
mean
,
sig
))
mean
,
sig
=
(
_reshaper
(
param
,
N
)
for
param
in
(
mean
,
sig
))
return
Adder
(
from_global_data
(
domain
,
mean
))
@
(
return
Adder
(
from_global_data
(
domain
,
mean
))
@
(
DiagonalOperator
(
from_global_data
(
domain
,
sig
))
DiagonalOperator
(
from_global_data
(
domain
,
sig
))
@
ducktape
(
domain
,
None
,
key
))
@
ducktape
(
domain
,
None
,
key
))
...
@@ -280,16 +284,17 @@ class _Amplitude(Operator):
...
@@ -280,16 +284,17 @@ class _Amplitude(Operator):
assert
isinstance
(
asperity
,
Operator
)
assert
isinstance
(
asperity
,
Operator
)
assert
isinstance
(
loglogavgslope
,
Operator
)
assert
isinstance
(
loglogavgslope
,
Operator
)
N_copies
=
max
(
dofdex
)
+
1
if
len
(
dofdex
)
>
0
:
assert
N_copies
>
0
N_copies
=
max
(
dofdex
)
+
1
if
N_copies
>
1
:
space
=
1
space
=
1
distributed_tgt
=
makeDomain
((
UnstructuredDomain
(
len
(
dofdex
)),
target
))
distributed_tgt
=
makeDomain
((
UnstructuredDomain
(
len
(
dofdex
)),
target
))
target
=
makeDomain
((
UnstructuredDomain
(
N_copies
),
target
))
target
=
makeDomain
((
UnstructuredDomain
(
N_copies
),
target
))
Distributor
=
_Distributor
(
dofdex
,
target
,
distributed_tgt
,
0
)
Distributor
=
_Distributor
(
dofdex
,
target
,
distributed_tgt
,
0
)
else
:
else
:
N_copies
=
0
space
=
0
space
=
0
target
=
makeDomain
(
target
)
distributed_tgt
=
target
=
makeDomain
(
target
)
azm_expander
=
ContractionOperator
(
distributed_tgt
,
spaces
=
space
).
adjoint
assert
isinstance
(
target
[
space
],
PowerSpace
)
assert
isinstance
(
target
[
space
],
PowerSpace
)
twolog
=
_TwoLogIntegrations
(
target
,
space
)
twolog
=
_TwoLogIntegrations
(
target
,
space
)
...
@@ -337,12 +342,12 @@ class _Amplitude(Operator):
...
@@ -337,12 +342,12 @@ class _Amplitude(Operator):
sigma
=
sig_flex
*
(
Adder
(
shift
)
@
sig_asp
).
sqrt
()
sigma
=
sig_flex
*
(
Adder
(
shift
)
@
sig_asp
).
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
space
==
1
:
if
N_copies
>
0
:
op
=
Distributor
@
op
op
=
Distributor
@
op
sig_fluc
=
Distributor
@
sig_fluc
sig_fluc
=
Distributor
@
sig_fluc
op
=
(
Distributor
@
Adder
(
vol0
))
@
(
sig_fluc
*
(
ps
_expander
@
azm
.
one_over
())
*
op
)
op
=
Adder
(
Distributor
(
vol0
))
@
(
sig_fluc
*
(
azm
_expander
@
azm
.
one_over
())
*
op
)
else
:
else
:
op
=
(
Adder
(
vol0
))
@
(
sig_fluc
*
(
ps
_expander
@
azm
.
one_over
())
*
op
)
op
=
(
Adder
(
vol0
))
@
(
sig_fluc
*
(
azm
_expander
@
azm
.
one_over
())
*
op
)
self
.
apply
=
op
.
apply
self
.
apply
=
op
.
apply
self
.
_fluc
=
fluctuations
self
.
_fluc
=
fluctuations
...
@@ -365,7 +370,7 @@ class CorrelatedFieldMaker:
...
@@ -365,7 +370,7 @@ class CorrelatedFieldMaker:
self
.
_total_N
=
total_N
self
.
_total_N
=
total_N
@
staticmethod
@
staticmethod
def
make
(
offset_amplitude_mean
,
offset_amplitude_stddev
,
prefix
,
total_N
=
1
):
def
make
(
offset_amplitude_mean
,
offset_amplitude_stddev
,
prefix
,
total_N
=
0
):
offset_amplitude_stddev
=
float
(
offset_amplitude_stddev
)
offset_amplitude_stddev
=
float
(
offset_amplitude_stddev
)
offset_amplitude_mean
=
float
(
offset_amplitude_mean
)
offset_amplitude_mean
=
float
(
offset_amplitude_mean
)
assert
offset_amplitude_stddev
>
0
assert
offset_amplitude_stddev
>
0
...
@@ -393,15 +398,15 @@ class CorrelatedFieldMaker:
...
@@ -393,15 +398,15 @@ class CorrelatedFieldMaker:
dofdex
=
np
.
full
(
self
.
_total_N
,
0
)
dofdex
=
np
.
full
(
self
.
_total_N
,
0
)
else
:
else
:
assert
len
(
dofdex
)
==
self
.
_total_N
assert
len
(
dofdex
)
==
self
.
_total_N
N
=
max
(
dofdex
)
if
self
.
_total_N
>
1
:
if
self
.
_total_N
>
0
:
space
=
1
space
=
1
position_space
=
makeDomain
((
UnstructuredDomain
(
self
.
_total_N
),
position_space
))
N
=
max
(
dofdex
)
+
1
position_space
=
makeDomain
((
UnstructuredDomain
(
N
),
position_space
))
else
:
else
:
space
=
0
space
=
0
N
=
0
position_space
=
makeDomain
(
position_space
)
position_space
=
makeDomain
(
position_space
)
N
=
1
power_space
=
PowerSpace
(
position_space
[
space
].
get_default_codomain
())
power_space
=
PowerSpace
(
position_space
[
space
].
get_default_codomain
())
prefix
=
str
(
prefix
)
prefix
=
str
(
prefix
)
#assert isinstance(position_space[space], (RGSpace, HPSpace, GLSpace)
#assert isinstance(position_space[space], (RGSpace, HPSpace, GLSpace)
...
@@ -410,14 +415,6 @@ class CorrelatedFieldMaker:
...
@@ -410,14 +415,6 @@ class CorrelatedFieldMaker:
fluctuations_stddev
,
fluctuations_stddev
,
prefix
+
'fluctuations'
,
prefix
+
'fluctuations'
,
N
)
N
)
#if copies:
# fluct = fluct*self._azm.one_over()
#else:
# #print(fluct.
# co = ContractionOperator(self._azm.target, None).adjoint
# fluct = (co @ fluct)*self._azm.one_over()
flex
=
_LognormalMomentMatching
(
flexibility_mean
,
flexibility_stddev
,
flex
=
_LognormalMomentMatching
(
flexibility_mean
,
flexibility_stddev
,
prefix
+
'flexibility'
,
prefix
+
'flexibility'
,
N
)
N
)
...
@@ -442,16 +439,19 @@ class CorrelatedFieldMaker:
...
@@ -442,16 +439,19 @@ class CorrelatedFieldMaker:
assert
isinstance
(
zeromode
,
Operator
)
assert
isinstance
(
zeromode
,
Operator
)
self
.
_azm
=
zeromode
self
.
_azm
=
zeromode
n_amplitudes
=
len
(
self
.
_a
)
n_amplitudes
=
len
(
self
.
_a
)
if
self
.
_total_N
>
1
:
if
self
.
_total_N
>
0
:
hspace
=
makeDomain
([
UnstructuredDomain
(
self
.
_total_N
)]
+
hspace
=
makeDomain
([
UnstructuredDomain
(
self
.
_total_N
)]
+
[
dd
[
-
1
].
get_default_codomain
()
for
dd
in
self
.
_position_spaces
])
[
dd
[
-
1
].
get_default_codomain
()
for
dd
in
self
.
_position_spaces
])
spaces
=
tuple
(
len
(
dd
)
for
dd
in
self
.
_position_spaces
)
spaces
=
list
(
1
+
np
.
arange
(
n_amplitudes
))
spaces
=
1
+
np
.
cumsum
(
spaces
)
#spaces = tuple(len(dd) for dd in self._position_spaces)
#spaces = 1 + np.cumsum(spaces)
zeroind
=
(
slice
(
None
),)
+
(
0
,)
*
(
len
(
hspace
.
shape
)
-
1
)
else
:
else
:
hspace
=
makeDomain
(
hspace
=
makeDomain
(
[
dd
[
-
1
].
get_default_codomain
()
for
dd
in
self
.
_position_spaces
])
[
dd
[
-
1
].
get_default_codomain
()
for
dd
in
self
.
_position_spaces
])
spaces
=
tuple
(
range
(
n_amplitudes
))
spaces
=
tuple
(
range
(
n_amplitudes
))
zeroind
=
(
slice
(
None
),)
*
(
1
-
1
//
self
.
_total_N
)
+
(
0
,)
*
(
len
(
hspace
.
shape
)
-
1
+
1
//
self
.
_total_N
)
spaces
=
list
(
np
.
arange
(
n_amplitudes
))
zeroind
=
(
0
,)
*
len
(
hspace
.
shape
)
foo
=
np
.
ones
(
hspace
.
shape
)
foo
=
np
.
ones
(
hspace
.
shape
)
foo
[
zeroind
]
=
0
foo
[
zeroind
]
=
0
...
@@ -460,7 +460,7 @@ class CorrelatedFieldMaker:
...
@@ -460,7 +460,7 @@ class CorrelatedFieldMaker:
self
.
_azm
.
target
,
zeroind
).
adjoint
self
.
_azm
.
target
,
zeroind
).
adjoint
azm
=
Adder
(
from_global_data
(
hspace
,
foo
))
@
ZeroModeInserter
@
zeromode
azm
=
Adder
(
from_global_data
(
hspace
,
foo
))
@
ZeroModeInserter
@
zeromode
spaces
=
np
.
array
(
range
(
n_amplitudes
))
+
1
-
1
//
self
.
_total_N
#
spaces = np.array(range(n_amplitudes)) + 1 - 1//self._total_N
ht
=
HarmonicTransformOperator
(
hspace
,
ht
=
HarmonicTransformOperator
(
hspace
,
self
.
_position_spaces
[
0
][
self
.
_spaces
[
0
]],
self
.
_position_spaces
[
0
][
self
.
_spaces
[
0
]],
space
=
spaces
[
0
])
space
=
spaces
[
0
])
...
@@ -475,12 +475,10 @@ class CorrelatedFieldMaker:
...
@@ -475,12 +475,10 @@ class CorrelatedFieldMaker:
self
.
_a
[
i
].
target
[
self
.
_spaces
[
i
]],
self
.
_a
[
i
].
target
[
self
.
_spaces
[
i
]],
space
=
spaces
[
i
]))
space
=
spaces
[
i
]))
#breakpoint()
all_spaces
=
list
(
range
(
len
(
hspace
)))
a
=
ContractionOperator
(
pd
.
domain
,
spaces
[
1
:]).
adjoint
@
self
.
_a
[
0
]
a
=
ContractionOperator
(
pd
.
domain
,
spaces
[
1
:]).
adjoint
@
self
.
_a
[
0
]
for
i
in
range
(
1
,
n_amplitudes
):
for
i
in
range
(
1
,
n_amplitudes
):
co
=
ContractionOperator
(
pd
.
domain
,
co
=
ContractionOperator
(
pd
.
domain
,
all_
spaces
[:
spaces
[
i
]]
+
all_spaces
[
spaces
[
i
]
+
1
:])
spaces
[:
i
]
+
spaces
[
i
+
1
:])
a
=
a
*
(
co
.
adjoint
@
self
.
_a
[
i
])
a
=
a
*
(
co
.
adjoint
@
self
.
_a
[
i
])
return
ht
(
azm
*
(
pd
@
a
)
*
ducktape
(
hspace
,
None
,
prefix
+
'xi'
))
return
ht
(
azm
*
(
pd
@
a
)
*
ducktape
(
hspace
,
None
,
prefix
+
'xi'
))
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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