imp icon indicating copy to clipboard operation
imp copied to clipboard

Incorrect polymorphic behaviour in the LogWrapper(IMP::isd::ISDRestraintsTemp)

Open Pellarin opened this issue 9 years ago • 2 comments

referring to #927, we have an ISDRestraint class working and passing tests. Now we want the LogWrapper get the probabilities from a IMP::isd::ISDRestraintsTemp. (we couldn't commit the LogWrapper code and the failing tests because it would break IMP).

We changed all RestraintSet macros in LogWrapper as following:

in LogWrapper.h

    IMP_LIST_ACTION(public, ISDRestraint, ISDRestraints, isdrestraint, isdrestraints,
                  IMP::isd::ISDRestraint *, IMP::isd::ISDRestraints, on_add(obj), on_change(),
                  if (container) on_remove(container, obj));

in LogWrapper.cpp

IMP_LIST_IMPL(LogWrapper, ISDRestraint, isdrestraint, IMP::isd::ISDRestraint *, IMP::isd::ISDRestraints);

we changed all Restraint -> ISDRestraint in the code... etc, accordingly (except where IMP::Restraint was needed).

and in unprotected_evaluate:

    for (unsigned int i = 0; i <get_number_of_isdrestraints(); ++i) {
      double p(get_isdrestraint(i)->get_probability());
      std::cout << p << std::endl ;
      prob *= p;
      if (prob<=std::numeric_limits<double>::min()*1000000.0){
        score=score-std::log(prob);
        prob=1.0;
      }
    }

It compiles fine.

We tested the following:

#!/usr/bin/env python

# imp general
import IMP
import IMP.core
import IMP.isd
import math

# unit testing framework
import IMP.test

class ISDRestraint(IMP.isd.ISDRestraint):
    def __init__(self,m):
        IMP.isd.ISDRestraint.__init__(self, m, "ISDRestraint %1%")

    def unprotected_evaluate(self,da):
        return -math.log(self.get_probability())

    def get_probability(self):
        return math.pi

    def do_get_inputs(self):
        return []


class TestLogWrapperRestraint(IMP.test.TestCase):

    def test_score_restraint(self):
        m=IMP.Model()
        r=ISDRestraint(m)
        target_score=r.unprotected_evaluate(None)
        lw=IMP.isd.LogWrapper(m)
        lw.add_isdrestraint(r)
        test_score=lw.unprotected_evaluate(None)
        self.assertAlmostEqual(target_score,test_score)


having

======================================================================
FAIL: test_score_restraint (__main__.TestLogWrapperRestraint)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_LogWrapper.py", line 35, in test_score_restraint
    self.assertAlmostEqual(target_score,test_score)
AssertionError: -1.1447298858494002 != 0.0 within 7 places

meaning that LogWrapper is calling get_probability() from the base class and not from the derived class.

Pellarin avatar Jan 29 '16 15:01 Pellarin

@cgreenberg I guess you've implemented these macros in LogWrapper once. Would you please check that we're doing it OK?

Pellarin avatar Jan 29 '16 15:01 Pellarin

Ideas anyone? :-)

Pellarin avatar Feb 03 '16 20:02 Pellarin

This code isn't in current IMP.

benmwebb avatar Nov 29 '22 06:11 benmwebb