IMP  2.4.0
The Integrative Modeling Platform
save_assignments.py
1 ## \example domino/save_assignments.py
2 # It can often be useful to save the assignments to a file. \imp provides support to do this to an a data set in an hdf5 file.
3 #
4 # You can use \c h5dump to view the contents of the created file.
5 
6 import IMP.domino
7 
8 # create a model and some particles, they are just used as markers here
10 ps = [IMP.kernel.Particle(m) for i in range(0, 10)]
11 
12 # create a subset with a few of the particles
13 ss = IMP.domino.Subset([ps[3], ps[5], ps[7]])
14 
15 file_name = IMP.create_temporary_file_name("assignments", ".asn")
16 
17 print("File name is", file_name)
18 
19 # create a list of assignments
20 
21 IMP.base.set_log_level(IMP.base.MEMORY)
22 asl = IMP.domino.WriteAssignmentContainer(file_name, ss, ps, "writer")
23 written = []
24 for i in range(0, 5):
25  for j in range(0, 5):
26  for k in range(0, 5):
27  a = IMP.domino.Assignment([i, j, k])
28  written.append(a)
29  asl.add_assignment(a)
30 
31 del asl
32 
33 # to check, we can read it back immediately
34 back_asl = IMP.domino.ReadAssignmentContainer(file_name, ss, ps, "reader")
35 
36 
37 if back_asl.get_assignments() == written:
38  print("They match!")
39 else:
40  print(back_asl.get_assignments())
41  print(written)
42  raise RuntimeError("They don't match :-(")
43 
44 
45 # More interestingly, we can create a new model and read back the
46 # assignments for that
47 mp = IMP.kernel.Model()
48 psp = [IMP.kernel.Particle(mp) for i in range(0, 10)]
49 
50 # create a subset with a few of the particles
51 ssp = IMP.domino.Subset([psp[3], psp[5], psp[7]])
52 
53 print("Note the subsets are differently ordered (most of the time): ", ss, ssp)
54 
55 back_aslp = IMP.domino.ReadAssignmentContainer(file_name, ssp, psp, "reader2")
56 
57 # however, the states are demuxed so they match the particles
58 print([str(a) for a in back_aslp.get_assignments()])
void set_log_level(LogLevel l)
Set the current global log level.
Represent a subset of the particles being optimized.
Definition: Subset.h:33
Class to handle individual model particles.
Store a configuration of a subset.
Definition: Assignment.h:32
Divide-and-conquer inferential optimization in discrete space.
Class for storing model, its restraints, constraints, and particles.
Definition: kernel/Model.h:73