IMP logo
IMP Reference Guide  2.7.0
The Integrative Modeling Platform
custom_filter.py
1 ## \example domino/custom_filter.py
2 # This example looks like the six particle optimization example except a
3 # filter is used instead of a restraint to remove the flip degree of
4 # freedom. The filter is written is Python, which makes for quick
5 # prototyping, but slower run times.
6 
7 import IMP
8 import IMP.domino
9 import IMP.core
10 import IMP.container
11 import sys
12 
13 IMP.setup_from_argv(sys.argv, "custom filter")
14 
15 # set restraints
16 
17 
18 def create_scoring(m, ps):
19  pairs = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [1, 5]]
21  # the restraint will be broken apart during optimization
23  [(ps[p[0]], ps[p[1]]) for p in pairs],
24  "Restrained pairs")
25  pr = IMP.container.PairsRestraint(score, pc)
26  pr.set_maximum_score(.01)
28  IMP.algebra.Vector3D(2, 0, 0))
29  return [pr]
30 
31 
32 def create_representation(m):
33  ps = []
34  for i in range(0, 6):
35  p = m.add_particle("P%d" % i)
37  ps.append(p)
38  return ps
39 
40 
41 def create_discrete_states(m, ps):
43  vs = [IMP.algebra.Vector3D(1, 0, 0),
44  IMP.algebra.Vector3D(0, 1, 0),
45  IMP.algebra.Vector3D(1, 1, 0),
46  IMP.algebra.Vector3D(2, 1, 0),
47  IMP.algebra.Vector3D(2, 0, 0)]
48  vs = vs + [-v for v in vs]
49  print(len(vs), "states for each particle")
50  print(vs[1])
51  states = IMP.domino.XYZStates(vs)
52  # special case ps[0] to remove a sliding degree of freedom
53  for p in ps[1:]:
54  pst.set_particle_states(m.get_particle(p), states)
55  return pst
56 
57 # force particle p to be in state s
58 
59 
60 class MyFilterTable(IMP.domino.SubsetFilterTable):
61 
62  class MyFilter(IMP.domino.SubsetFilter):
63 
64  def __init__(self, pos, value):
65  # print "Filtering with", pos, value
66  IMP.domino.SubsetFilter.__init__(
67  self, "MF" + str(pos) + " " + str(value))
68  self.pos = pos
69  self.value = value
70 
71  def get_next_state(self, pos, s):
72  # suggest that the sampler try the correct state
73  # this method is only called if the filter failed, so pos must be
74  # self.pos
75  if s[self.pos] > self.value:
76  # a very large number
77  return 2 ** 29
78  else:
79  return self.value
80 
81  def get_is_ok(self, state):
82  # it is only OK if it has the required state
83  ret = state[self.pos] == self.value
84  return ret
85 
86  def get_strength(self, s, excluded):
87  # return the maximum value since it dictates the state for the particle
88  return 1
89 
90  def __init__(self, p, s):
91  # set the name of the object to something vaguely useful
92  IMP.domino.SubsetFilterTable.__init__(
93  self, "MFT" + p.get_name() + " at " + str(s))
94  self.p = p
95  self.s = s
96 
97  def get_subset_filter(self, subset, excluded):
98  # create a filter if self.p is in subset but not in excluded
99  if self.p in subset and self.p not in sum([list(x) for x in excluded], []):
100  # pass the position of self.p and the value that it must have
101  return self.MyFilter(list(subset).index(self.p), self.s)
102  else:
103  return None
104 
105 
106 def create_sampler(m, ps, rs, pst):
107  s = IMP.domino.DominoSampler(m, pst)
108  s.set_restraints(rs)
109  # s.set_log_level(IMP.VERBOSE)
110  # the following lines recreate the defaults and so are optional
111  filters = []
112  # do not allow particles with the same ParticleStates object
113  # to have the same state index
114  filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
115  rc = IMP.domino.RestraintCache(pst)
116  rc.add_restraints(rs)
117  # filter states that score worse than the cutoffs in the Model
119  filters[-1].set_log_level(IMP.SILENT)
120  mf = MyFilterTable(m.get_particle(ps[1]), 0)
121  # try with and without this line
122  filters.append(mf)
123  states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
124  # states.set_log_level(IMP.SILENT);
125  s.set_assignments_table(states)
126  s.set_subset_filter_tables(filters)
127  return s
128 
129 IMP.set_log_level(IMP.TERSE)
130 m = IMP.Model()
131 m.set_log_level(IMP.SILENT)
132 
133 print("creating representation")
134 ps = create_representation(m)
135 print("creating discrete states")
136 pst = create_discrete_states(m, ps)
137 print("creating score function")
138 rs = create_scoring(m, ps)
139 
140 print("creating sampler")
141 s = create_sampler(m, ps, rs, pst)
142 
143 # s.set_log_level(IMP.SILENT)
144 print("sampling")
145 cs = s.create_sample()
146 
147 print("found ", cs.get_number_of_configurations(), "solutions")
149 for i in range(cs.get_number_of_configurations()):
150  cs.load_configuration(i)
151  print("solution number:", i, " is:", sf.evaluate(False))
152  for p in ps:
153  print(IMP.core.XYZ(m, p).get_x(), IMP.core.XYZ(m, p).get_y())
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Various classes to hold sets of particles.
Upper bound harmonic function (non-zero when feature > mean)
Sample best solutions using Domino.
Definition: DominoSampler.h:32
Filter a configuration of the subset using the Model thresholds.
static XYZ setup_particle(Model *m, ParticleIndex pi)
Definition: XYZ.h:51
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:72
Do not allow two particles to be in the same state.
Store a list of ParticleIndexPairs.
Apply a function to the distance to a fixed point.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
void set_log_level(LogLevel l)
Set the current global log level.
Basic functionality that is expected to be used by a wide variety of IMP users.
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Applies a PairScore to each Pair in a list.
Divide-and-conquer inferential optimization in discrete space.