IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
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
7import IMP.domino
8import sys
9
10IMP.setup_from_argv(sys.argv, "save assignments")
11
12# create a model and some particles; they are just used as markers here
13m = IMP.Model()
14ps = [IMP.Particle(m) for i in range(0, 10)]
15
16# create a subset with a few of the particles
17ss = IMP.domino.Subset([ps[3], ps[5], ps[7]])
18
19file_name = IMP.create_temporary_file_name("assignments", ".asn")
20
21print("File name is", file_name)
22
23# create a list of assignments
24
25IMP.set_log_level(IMP.MEMORY)
26asl = IMP.domino.WriteAssignmentContainer(file_name, ss, ps, "writer")
27written = []
28for 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
35del asl
36
37# to check, we can read it back immediately
38back_asl = IMP.domino.ReadAssignmentContainer(file_name, ss, ps, "reader")
39
40
41if back_asl.get_assignments() == written:
42 print("They match!")
43else:
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
51mp = IMP.Model()
52psp = [IMP.Particle(mp) for i in range(0, 10)]
53
54# create a subset with a few of the particles
55ssp = IMP.domino.Subset([psp[3], psp[5], psp[7]])
56
57print("Note the subsets are differently ordered (most of the time): ", ss, ssp)
58
59back_aslp = IMP.domino.ReadAssignmentContainer(file_name, ssp, psp, "reader2")
60
61# however, the states are demuxed so they match the particles
62print([str(a) for a in back_aslp.get_assignments()])
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Class to handle individual particles of a Model object.
Definition Particle.h:45
Store a configuration of a subset.
Definition Assignment.h:35
Read the assignments from binary data on disk.
Represent a subset of the particles being optimized.
Definition Subset.h:34
Store the assignments on disk as binary data.
Divide-and-conquer inferential optimization in discrete space.
void set_log_level(LogLevel l)
Set the current global log level.
std::string create_temporary_file_name(std::string prefix="imp_temp", std::string suffix="")
Create a temporary file.
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.