IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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
7import IMP
8import IMP.domino
9import IMP.core
10import IMP.container
11import sys
12
13IMP.setup_from_argv(sys.argv, "custom filter")
14
15# set restraints
16
17
18def 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)
27 # d = IMP.core.DistanceToSingletonScore(IMP.core.HarmonicUpperBound(2, 1),
28 # IMP.algebra.Vector3D(2, 0, 0))
29 return [pr]
30
31
32def 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
41def 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
60class 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
100 and self.p not in sum([list(x) for x in excluded], [])):
101 # pass the position of self.p and the value that it must have
102 return self.MyFilter(list(subset).index(self.p), self.s)
103 else:
104 return None
105
106
107def create_sampler(m, ps, rs, pst):
108 s = IMP.domino.DominoSampler(m, pst)
109 s.set_restraints(rs)
110 # s.set_log_level(IMP.VERBOSE)
111 # the following lines recreate the defaults and so are optional
112 filters = []
113 # do not allow particles with the same ParticleStates object
114 # to have the same state index
115 filters.append(IMP.domino.ExclusionSubsetFilterTable(pst))
117 rc.add_restraints(rs)
118 # filter states that score worse than the cutoffs in the Model
120 filters[-1].set_log_level(IMP.SILENT)
121 mf = MyFilterTable(m.get_particle(ps[1]), 0)
122 # try with and without this line
123 filters.append(mf)
124 states = IMP.domino.BranchAndBoundAssignmentsTable(pst, filters)
125 # states.set_log_level(IMP.SILENT);
126 s.set_assignments_table(states)
127 s.set_subset_filter_tables(filters)
128 return s
129
130
131IMP.set_log_level(IMP.TERSE)
132m = IMP.Model()
133m.set_log_level(IMP.SILENT)
134
135print("creating representation")
136ps = create_representation(m)
137print("creating discrete states")
138pst = create_discrete_states(m, ps)
139print("creating score function")
140rs = create_scoring(m, ps)
141
142print("creating sampler")
143s = create_sampler(m, ps, rs, pst)
144
145# s.set_log_level(IMP.SILENT)
146print("sampling")
147cs = s.create_sample()
148
149print("found ", cs.get_number_of_configurations(), "solutions")
151for i in range(cs.get_number_of_configurations()):
152 cs.load_configuration(i)
153 print("solution number:", i, " is:", sf.evaluate(False))
154 for p in ps:
155 print(IMP.core.XYZ(m, p).get_x(), IMP.core.XYZ(m, p).get_y())
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Store a list of ParticleIndexPairs.
Applies a PairScore to each Pair in a list.
Score distance between two particle centers using a harmonic function.
Create a scoring function on a list of restraints.
A decorator for a particle with x,y,z coordinates.
Definition XYZ.h:30
static XYZ setup_particle(Model *m, ParticleIndex pi)
Definition XYZ.h:51
Sample best solutions using Domino.
Do not allow two particles to be in the same state.
Filter a configuration of the subset using the Model thresholds.
VectorD< 3 > Vector3D
Definition VectorD.h:407
Various classes to hold sets of particles.
Basic functionality that is expected to be used by a wide variety of IMP users.
Divide-and-conquer inferential optimization in discrete space.
void set_log_level(LogLevel l)
Set the current global log level.
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.