Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions abismal/command_line/abismal.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def main(args=None):
reindexing_ops = reindexing_ops + [op.triplet() for op in ops]
logger.info(f"Adding disambiguation operators: {reindexing_ops}")

posterior_kwargs = {}

if parser.prior_distribution == "wilson":
if parser.parents is not None:
from abismal.prior.structure_factor.wilson import MultiWilsonPrior
Expand All @@ -100,6 +102,7 @@ def main(args=None):
rac,
parser.prior_correlation,
)
posterior_kwargs['independent'] = False
elif parser.posterior_type == "intensity":
from abismal.prior.intensity.wilson import WilsonPrior

Expand Down Expand Up @@ -127,12 +130,13 @@ def main(args=None):
loc_init = tf.ones_like(prior.flat_distribution().mean())
scale_init = parser.init_scale * loc_init

posterior_kwargs = {
posterior_kwargs.update({
"rac": rac,
"loc_init": loc_init,
"scale_init": scale_init,
"epsilon": parser.epsilon,
}
})

if parser.posterior_type == "intensity":
if parser.posterior_distribution == "foldednormal":
from abismal.surrogate_posterior.intensity import (
Expand Down
5 changes: 4 additions & 1 deletion abismal/command_line/cchalf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def main(args=None):
parser.add_argument(
"--reference-mtz", type= str, default=None, help='A reference mtz file which will be used to determine the reindexing operator.',
)
parser.add_argument(
"--run-eagerly", help="Run eagerly which is slower but more straightforward to debug.", action='store_true',
)
parser = parser.parse_args(args)

import tf_keras as tfk
Expand Down Expand Up @@ -50,7 +53,7 @@ def main(args=None):
opt = model.optimizer.from_config(model.optimizer.get_config())

#Now re-compile to re-initialize the optimizer momenta
model.compile(opt)
model.compile(opt, run_eagerly=parser.run_eagerly)

callbacks = [
MtzSaver(f"half_{half_id+1}", reference_mtz=parser.reference_mtz)
Expand Down
6 changes: 3 additions & 3 deletions abismal/prior/structure_factor/wilson.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ def mean(self):
)
return loc

def log_prob(self, z):
if self.parent_id is not None:
def log_prob(self, z, z_pa=None):
if z_pa is None:
z_h = z
z_pa = tf.gather(z, self.parent_id, axis=-1)
else:
z_h, z_pa = tf.unstack(z, axis=-1)
z_h, z_pa = z[...,0],z[...,1]

#Single wilson case for root nodes
ll_sw = WilsonDistribution(self.centric, self.multiplicity, self.sigma).log_prob(z_h)
Expand Down
5 changes: 2 additions & 3 deletions abismal/surrogate_posterior/normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ class MultivariateNormalPosteriorBase(object):
"""
A base class for creating low-rank multivariate normal posteriors.
"""
independent = False

def __init__(self, rac, rank, loc_init=None, scale_init=None, epsilon=1e-12, **kwargs):
super().__init__(rac, epsilon=epsilon, **kwargs)
def __init__(self, rac, rank, loc_init=None, independent=False, scale_init=None, epsilon=1e-12, **kwargs):
super().__init__(rac, epsilon=epsilon, independent=independent, **kwargs)

if loc_init is None:
loc_init = tf.ones(rac.asu_size)
Expand Down
11 changes: 7 additions & 4 deletions abismal/surrogate_posterior/surrogate_posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import tf_keras as tfk

class PosteriorBase(tfk.models.Model):
independent = True #Whether to treat samples as statistically independent

def __init__(self, rac, epsilon=1e-12, **kwargs):
def __init__(self, rac, epsilon=1e-12, independent=True, **kwargs):
"""
rac : ReciprocalASUCollection
"""
super().__init__(**kwargs)
self.independent = independent
self.epsilon = epsilon
self.rac = rac
self.seen = self.add_weight(
Expand All @@ -30,6 +29,7 @@ def get_config(self):
config = {
'rac' : tfk.saving.serialize_keras_object(self.rac),
'epsilon' : self.epsilon,
'independent' : self.independent,
}
return config

Expand Down Expand Up @@ -79,7 +79,10 @@ def compute_kl_terms(self, q, p, samples=None):
q_z = q.log_prob(samples)
p_z = p.log_prob(samples)
if not self.independent:
q_z = tf.expand_dims(q_z, axis=-1)
#it is okay to expand the dimensions to enable combining
#a multivariate posterior and univariate prior
if len(tf.shape(q_z)) < len(tf.shape(p_z)):
q_z = tf.expand_dims(q_z, axis=-1)

kl_div = q_z - p_z
kl_div = tf.reduce_mean(kl_div, axis=0)
Expand Down
17 changes: 16 additions & 1 deletion tests/command_line/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run_abismal(flags, files, additional_asserts=()):
for add in additional_asserts:
assert add()

args = " datamanager.yml epoch_2.keras --sf-init epoch_0.keras "
args = " datamanager.yml epoch_2.keras --run-eagerly --sf-init epoch_0.keras "
cchalf_main(args.split())
assert exists('abismal_xval.mtz')

Expand Down Expand Up @@ -154,3 +154,18 @@ def test_glu(conventional_mtz):
)


def test_multivariate_wilson_prior(conventional_mtz):
flags = base_flags + (
f"--parents=0,0",
f"-r 0.0,0.99",
f"--prior-distribution=wilson",
)
files = [
conventional_mtz,
conventional_mtz,
]
run_abismal(
flags,
files,
)

Loading