IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/22
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
3 # support to do this to a data set in an HDF5 file.
4 #
5 # You can use \c h5dump to view the contents of the created file.
6 
7 import IMP.domino
8 import sys
9 
10 IMP.setup_from_argv(sys.argv, "save assignments")
11 
12 # create a model and some particles; they are just used as markers here
13 m = IMP.Model()
14 ps = [IMP.Particle(m) for i in range(0, 10)]
15 
16 # create a subset with a few of the particles
17 ss = IMP.domino.Subset([ps[3], ps[5], ps[7]])
18 
19 file_name = IMP.create_temporary_file_name("assignments", ".asn")
20 
21 print("File name is", file_name)
22 
23 # create a list of assignments
24 
25 IMP.set_log_level(IMP.MEMORY)
26 asl = IMP.domino.WriteAssignmentContainer(file_name, ss, ps, "writer")
27 written = []
28 for i in range(0, 5):
29  for j in range(0, 5):
30  for k in range(0, 5):
31  a = IMP.domino.Assignment([i, j, k])
32  written.append(a)
33  asl.add_assignment(a)
34 
35 del asl
36 
37 # to check, we can read it back immediately
38 back_asl = IMP.domino.ReadAssignmentContainer(file_name, ss, ps, "reader")
39 
40 
41 if back_asl.get_assignments() == written:
42  print("They match!")
43 else:
44  print(back_asl.get_assignments())
45  print(written)
46  raise RuntimeError("They don't match :-(")
47 
48 
49 # More interestingly, we can create a new model and read back the
50 # assignments for that
51 mp = IMP.Model()
52 psp = [IMP.Particle(mp) for i in range(0, 10)]
53 
54 # create a subset with a few of the particles
55 ssp = IMP.domino.Subset([psp[3], psp[5], psp[7]])
56 
57 print("Note the subsets are differently ordered (most of the time): ", ss, ssp)
58 
59 back_aslp = IMP.domino.ReadAssignmentContainer(file_name, ssp, psp, "reader2")
60 
61 # however, the states are demuxed so they match the particles
62 print([str(a) for a in back_aslp.get_assignments()])
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
Represent a subset of the particles being optimized.
Definition: Subset.h:33
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
void set_log_level(LogLevel l)
Set the current global log level.
Read the assignments from binary data on disk.
Store a configuration of a subset.
Definition: Assignment.h:35
Class to handle individual particles of a Model object.
Definition: Particle.h:43
Store the assignments on disk as binary data.
Divide-and-conquer inferential optimization in discrete space.
std::string create_temporary_file_name(std::string prefix="imp_temp", std::string suffix="")
Create a temporary file.