[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[IMP-dev] Removing names from restraints



API changes: Restraint.set_name/Restraint.get_name went away because no one was using them.

There are a couple of minor changes:
- RestraintSet uses the IMP_CONTAINER macros.
- RestraintSet uses an auto_ptr instead of catching all exceptions
Index: test/xml/test_xml.py
===================================================================
--- test/xml/test_xml.py	(revision 664)
+++ test/xml/test_xml.py	(working copy)
@@ -35,18 +35,6 @@
         #restraint_sets = self.imp_model.get_restraints
         self.assertEqual(self.imp_model.number_of_restraints(), 3,
                          "xml file contains three restraint sets")
-        self.assertEqual(self.imp_model.get_restraint(IMP.RestraintIndex(0)).get_name(),
-                         'exclusion_volumes',
-                         "not expecting restraint set name: "
-                         + self.imp_model.get_restraint(IMP.RestraintIndex(0)).get_name())
-        self.assertEqual(self.imp_model.get_restraint(IMP.RestraintIndex(1)).get_name(),
-                         'torus',
-                         "not expecting restraint set name: "
-                         + self.imp_model.get_restraint(IMP.RestraintIndex(1)).get_name())
-        self.assertEqual(self.imp_model.get_restraint(IMP.RestraintIndex(2)).get_name(),
-                         'connections',
-                         "not expecting restraint set name: "
-                         + self.imp_model.get_restraint(IMP.RestraintIndex(2)).get_name())
 
         # test restraints
         score = self.imp_model.get_restraint(IMP.RestraintIndex(0)).evaluate(None)
Index: include/IMP/utility.h
===================================================================
--- include/IMP/utility.h	(revision 664)
+++ include/IMP/utility.h	(working copy)
@@ -164,7 +164,7 @@
    \param[in] init Code to modify the passed in object. The object is obj
    its index index.
  */
-#define IMP_CONTAINER_IMPL(Class, Ucname, lcname, IndexType, init)           \
+#define IMP_CONTAINER_IMPL(Class, Ucname, lcname, IndexType, init)      \
   IndexType Class::add_##lcname(Ucname *obj) {                          \
     IndexType index(lcname##_vector_.size());                           \
     lcname##_vector_.push_back(obj);                                    \
Index: include/IMP/restraints/RestraintSet.h
===================================================================
--- include/IMP/restraints/RestraintSet.h	(revision 664)
+++ include/IMP/restraints/RestraintSet.h	(working copy)
@@ -25,32 +25,20 @@
 
   IMP_RESTRAINT("0.5", "Daniel Russel")
 
-  //! The type to use to retrieve a restraint
-  typedef int RestraintIndex;
+  IMP_CONTAINER(Restraint, restraint, RestraintIndex);
 
+ public:
+
   //! Set weight for all restraints contained by this set.
-  /** \param[in] weight The new value of the weight.
+  /** Setting the weight to 0 disables the restraints in the set.
+
+      \param[in] weight The new value of the weight.
     */
   void set_weight(Float weight) { weight_ = weight; }
 
-  //! Add restraint to the restraint set.
-  /** \param[in] restraint The restraint to add to the restraint set.
-      \return the index of the newly-added restraint in the restraint set.
-   */
-  RestraintIndex add_restraint(Restraint *restraint);
+  //! Get weight for all restraints contained by this set.
+  Float get_weight() { return weight_; }
 
-  //! Access a restraint in the restraint set.
-  /** \param[in] i The RestraintIndex of the restraint to retrieve.
-      \exception std::out_of_range the index is out of range.
-      \return Pointer to the Restraint.
-   */
-  Restraint *get_restraint(RestraintIndex i) const;
-
-  //! Return the total number of restraints
-  unsigned int number_of_restraints() const {
-    return restraints_.size();
-  }
-
   //! Called when at least one particle has been inactivated.
   /** Check each restraint to see if it changes its active status.
    */
@@ -58,15 +46,10 @@
 
 protected:
 
-  //! Restraints to evaluate.
-  /** These can be accessed with an iterator by a filter.
-      In the case where the restraint is a single simple restraint,
-      this vector contains a pointer to itself.
-   */
-  std::vector<Restraint *> restraints_;
-
   //! Weight for all restraints.
   Float weight_;
+
+  std::string name_;
 };
 
 } // namespace IMP
Index: include/IMP/Restraint.h
===================================================================
--- include/IMP/Restraint.h	(revision 664)
+++ include/IMP/Restraint.h	(working copy)
@@ -29,8 +29,8 @@
 class IMPDLLEXPORT Restraint : public Object
 {
 public:
-  //! Initialize the Restraint and its model pointer
-  Restraint(std::string name=std::string());
+  //! Initialize the Restraint
+  Restraint();
   virtual ~Restraint();
 
   //! Return the score for this restraint for the current state of the model.
@@ -65,16 +65,6 @@
 
   virtual std::string last_modified_by() const = 0;
 
-  //! Get the name of the restraint
-  const std::string& get_name() const {
-    return name_;
-  }
-
-  //! Set the name of the restraint
-  void set_name(const std::string &name) {
-    name_=name;
-  }
-
   //! The model the restraint is part of.
   /** \param[in] model The model.
    */
@@ -134,8 +124,6 @@
   bool are_particles_active_;
 
   std::vector<Particle*> particles_;
-
-  std::string name_;
 };
 
 IMP_OUTPUT_OPERATOR(Restraint);
Index: impEM/pyext/IMPEM.i
===================================================================
--- impEM/pyext/IMPEM.i	(revision 664)
+++ impEM/pyext/IMPEM.i	(working copy)
@@ -28,6 +28,7 @@
 }
 
 /* Get definitions of IMP base classes (but do not wrap; that is done by IMP) */
+%import "IMP/Object.h"
 %import "IMP/Restraint.h"
 
 /* Get definitions of EMLIB base classes (but do not wrap) */
Index: src/Model.cpp
===================================================================
--- src/Model.cpp	(revision 664)
+++ src/Model.cpp	(working copy)
@@ -105,7 +105,7 @@
   for (RestraintIterator it = restraints_begin();
        it != restraints_end(); ++it) {
     IMP_CHECK_OBJECT(*it);
-    IMP_LOG(VERBOSE, (*it)->get_name() << ": " << std::flush);
+    IMP_LOG(VERBOSE, **it);
     Float tscore=0;
     if ((*it)->get_is_active()) {
       tscore = (*it)->evaluate(accpt);
@@ -203,12 +203,6 @@
   out << "version: " << version() << "  ";
   out << "last_modified_by: " << last_modified_by() << std::endl;
   out << number_of_particles() << " particles" << std::endl;
-  out << "Restraints:" << std::endl;
-  for (RestraintConstIterator it = restraints_begin(); 
-       it != restraints_end(); ++it) {
-    IMP_CHECK_OBJECT(*it);
-    out << (*it)->get_name() << std::endl;
-  }
 
   internal::show_attributes(out);
 }
Index: src/Restraint.cpp
===================================================================
--- src/Restraint.cpp	(revision 664)
+++ src/Restraint.cpp	(working copy)
@@ -15,7 +15,7 @@
 namespace IMP
 {
 
-Restraint::Restraint(std::string name): name_(name)
+Restraint::Restraint()
 {
   model_ = NULL;
   is_active_ = true; // active by default
@@ -57,8 +57,8 @@
     out << "unknown restraint (inactive):" << std::endl;
   }
 
-  out << "version: " << version() << std::endl;
-  out << "last_modified_by: " << last_modified_by() << std::endl;
+  out << "  version: " << version() << std::endl;
+  out << "  last_modified_by: " << last_modified_by() << std::endl;
 }
 
 
Index: src/restraints/RestraintSet.cpp
===================================================================
--- src/restraints/RestraintSet.cpp	(revision 664)
+++ src/restraints/RestraintSet.cpp	(working copy)
@@ -10,6 +10,7 @@
 #include "IMP/log.h"
 #include "IMP/restraints/RestraintSet.h"
 #include "../mystdexcept.h"
+#include <utility>
 
 namespace IMP
 {
@@ -17,12 +18,14 @@
 
 //! Constructor
 RestraintSet::RestraintSet(const std::string& name)
-    : Restraint(name), weight_(1.0)
+    : weight_(1.0)
 {
   IMP_LOG(VERBOSE, "Restraint set constructed");
+  name_=name;
 }
 
 
+
 //! Destructor
 /** \todo Should reference count restraints correctly, to avoid deleting
           restraints here which live in two or more RestraintSets.
@@ -30,39 +33,15 @@
 RestraintSet::~RestraintSet()
 {
   IMP_LOG(VERBOSE, "Delete RestraintSet");
-  for (unsigned int i = 0; i < restraints_.size(); ++i) {
-    restraints_[i]->set_model(NULL);
-    delete restraints_[i];
-  }
+  IMP_CONTAINER_DELETE(Restraint, restraint);
 
 }
 
+IMP_CONTAINER_IMPL(RestraintSet, Restraint, restraint, RestraintIndex, 
+                   obj->set_model(get_model()););
 
-//! Add restraint to the restraint set.
-/** \param[in] restraint The restraint to add to the restraint set.
-    \return the index of the newly-added restraint in the restraint set.
- */
-RestraintSet::RestraintIndex RestraintSet::add_restraint(Restraint* restraint)
-{
-  restraints_.push_back(restraint);
-  restraint->set_model(get_model());
-  return restraints_.size() - 1;
-}
 
 
-//! Access a restraint in the restraint set.
-/** \param[in] i The RestraintIndex of the restraint to retrieve.
-    \exception std::out_of_range the index is out of range.
-    \return Pointer to the Restraint.
- */
-Restraint* RestraintSet::get_restraint(RestraintIndex i) const
-{
-  IMP_check(static_cast<unsigned int>(i) < restraints_.size(),
-            "Invalid restraint requested",
-            std::out_of_range("Invalid restraint"));
-  return restraints_[i];
-}
-
 //! Calculate the score for this restraint for the current model state.
 /** \param[in] accum If not NULL, use this object to accumulate partial first
                      derivatives.
@@ -70,25 +49,21 @@
  */
 Float RestraintSet::evaluate(DerivativeAccumulator *accum)
 {
+  if (get_weight() == 0) return 0;
   Float score;
-
+  typedef std::auto_ptr<DerivativeAccumulator> DAP;
   // Use a local copy of the accumulator for our sub-restraints
-  DerivativeAccumulator *ouracc = NULL;
+  DAP ouracc;
   if (accum) {
-    ouracc = new DerivativeAccumulator(*accum, weight_);
+    ouracc = DAP(new DerivativeAccumulator(*accum, weight_));
   }
 
   score = (Float) 0.0;
-  try {
-    for (size_t i = 0; i < restraints_.size(); i++) {
-      if (restraints_[i]->get_is_active())
-        score += restraints_[i]->evaluate(ouracc);
+  for (RestraintIterator it= restraints_begin(); it != restraints_end(); ++it) {
+    if ((*it)->get_is_active()) {
+      score += (*it)->evaluate(ouracc.get());
     }
-  } catch (...) {
-    delete ouracc;
-    throw;
   }
-  delete ouracc;
 
   return score * weight_;
 }
@@ -99,8 +74,8 @@
  */
 void RestraintSet::check_particles_active()
 {
-  for (size_t i = 0; i < restraints_.size(); i++) {
-    restraints_[i]->check_particles_active();
+  for (RestraintIterator it= restraints_begin(); it != restraints_end(); ++it) {
+    (*it)->check_particles_active();
   }
 }
 
@@ -111,11 +86,12 @@
  */
 void RestraintSet::show(std::ostream& out) const
 {
-  out << "restraint set " << get_name() << ":" << std::endl;
-  for (size_t i = 0; i < restraints_.size(); i++) {
-    restraints_[i]->show(out);
+  out << "restraint set " << name_ << ":..." << std::endl;
+  for (RestraintConstIterator it= restraints_begin();
+       it != restraints_end(); ++it) {
+    (*it)->show(out);
   }
-  out << "... end restraint set " << get_name() << std::endl;
+  out << "... end restraint set " << name_ << std::endl;
 }