Skip to content
Open
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
4 changes: 3 additions & 1 deletion include/GaussianEMRestraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class IMPBAYESIANEMEXPORT GaussianEMRestraint : public Restraint
\param[in] density_ps particles for the density GMM
\param[in] global_sigma Particle to modulate the uncertainty
\param[in] model_cutoff_dist Cutoff for the model-model interactions
\param[in] density_cutoff_dist Cutoff for model-density interactions
\param[in] density_cutoff_dist Cutoff for model-density interactions.
Only pairs of Gaussians whose spheres (XYZR) are closer
than the cutoff plus 2A are scored.
\param[in] slope Gentle term to move all particles to the density
\param[in] update_model (DEPRECATED) update model each cycle
\param[in] backbone_slope Limit the slope only to backbone particles
Expand Down
57 changes: 40 additions & 17 deletions src/GaussianEMRestraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@
#include <IMP/isd/isd_config.h>
#include <IMP/isd/Scale.h>
#include <IMP/isd/em_utilities.h>
#include <IMP/core/XYZR.h>
#include <boost/math/special_functions/gamma.hpp>

IMPBAYESIANEM_BEGIN_NAMESPACE

namespace {
// The close pair container lists every pair closer than its cutoff, plus
// other pairs that depend on where the particles were when the list was
// last updated. Score only pairs whose spheres are closer than the cutoff
// plus this margin, so that the score depends only on the coordinates. The
// margin (twice the container's default slack of 1A) is the set of pairs a
// freshly updated list holds, so scores are unchanged right after an update.
const double pair_margin = 2.0;

inline bool get_is_close(Model *m, const ParticleIndexPair &pp,
double cutoff) {
return core::get_distance(core::XYZR(m, std::get<0>(pp)),
core::XYZR(m, std::get<1>(pp))) < cutoff;
}
} // anonymous namespace

GaussianEMRestraint::GaussianEMRestraint(Model *mdl, ParticleIndexes model_ps,
ParticleIndexes density_ps,
ParticleIndex global_sigma,
Expand Down Expand Up @@ -52,7 +69,7 @@ GaussianEMRestraint::GaussianEMRestraint(Model *mdl, ParticleIndexes model_ps,
md_container_ = new container::CloseBipartitePairContainer(
new container::ListSingletonContainer(mdl, model_ps),
new container::ListSingletonContainer(mdl, density_ps),
density_cutoff_dist);
density_cutoff_dist + pair_margin);

compute_initial_scores();

Expand Down Expand Up @@ -146,16 +163,19 @@ GaussianEMRestraint::unprotected_evaluate(DerivativeAccumulator *accum) const {

std::map<ParticleIndex,Float> map_score_dm;
// std::cout<<"calculating MD score"<<std::endl;
double md_cutoff = density_cutoff_dist_ + pair_margin;
IMP_CONTAINER_FOREACH(container::CloseBipartitePairContainer, md_container_, {
Float score = IMP::isd::score_gaussian_overlap(get_model(), _1, &deriv);

if (map_score_dm.find(_1[1]) != map_score_dm.end()) {
map_score_dm[_1[1]] += score;
} else {
map_score_dm[_1[1]] = score;
if (get_is_close(get_model(), _1, md_cutoff)) {
Float score = IMP::isd::score_gaussian_overlap(get_model(), _1, &deriv);

if (map_score_dm.find(_1[1]) != map_score_dm.end()) {
map_score_dm[_1[1]] += score;
} else {
map_score_dm[_1[1]] = score;
}

md_score = KahanSum(md_score, score);
}

md_score = KahanSum(md_score, score);
});

double logterm = 0.0;
Expand Down Expand Up @@ -218,16 +238,19 @@ void GaussianEMRestraint::debug() {

std::map<ParticleIndex,Float> map_score_dm;
// std::cout<<"calculating MD score"<<std::endl;
double md_cutoff = density_cutoff_dist_ + pair_margin;
IMP_CONTAINER_FOREACH(container::CloseBipartitePairContainer, md_container_, {
Float score = IMP::isd::score_gaussian_overlap(get_model(), _1, &deriv);

if (map_score_dm.find(_1[1]) != map_score_dm.end()) {
map_score_dm[_1[1]] += score;
} else {
map_score_dm[_1[1]] = score;
if (get_is_close(get_model(), _1, md_cutoff)) {
Float score = IMP::isd::score_gaussian_overlap(get_model(), _1, &deriv);

if (map_score_dm.find(_1[1]) != map_score_dm.end()) {
map_score_dm[_1[1]] += score;
} else {
map_score_dm[_1[1]] = score;
}

md_score = KahanSum(md_score, score);
}

md_score = KahanSum(md_score, score);
});

double logterm = 0.0;
Expand Down
44 changes: 44 additions & 0 deletions test/test_pair_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import IMP
import IMP.core
import IMP.algebra
import IMP.atom
import IMP.isd
import IMP.test
import IMP.bayesianem


class Tests(IMP.test.TestCase):

def test_score_independent_of_history(self):
"""Test that the score does not depend on close pair list history"""
m = IMP.Model()

def gaussian(x, radius):
p = IMP.Particle(m)
IMP.core.Gaussian.setup_particle(p, IMP.algebra.Gaussian3D(
IMP.algebra.ReferenceFrame3D(IMP.algebra.Transformation3D(
IMP.algebra.Vector3D(x, 0, 0))),
IMP.algebra.Vector3D(9, 9, 9)))
IMP.atom.Mass.setup_particle(p, 1000.)
IMP.core.XYZR.setup_particle(p, radius)
return p.get_index()

def make_restraint():
return IMP.bayesianem.GaussianEMRestraint(
m, model, density, sigma, 10., 10., 0., False, False)
model = [gaussian(0., 3.), gaussian(-36.5, 3.)]
density = [gaussian(0., 9.), gaussian(-60., 9.)]
sigma = IMP.isd.Scale.setup_particle(
IMP.Particle(m), 1.).get_particle_index()
r = make_restraint()
r.evaluate(False)
# Move the second model Gaussian by less than the close pair
# container's slack (1A), so that its pair with the second density
# Gaussian crosses the list edge without a list update
IMP.core.XYZ(m, model[1]).set_x(-35.6)
self.assertAlmostEqual(r.evaluate(False),
make_restraint().evaluate(False), delta=1e-8)


if __name__ == '__main__':
IMP.test.main()
Loading