11 log = logging.getLogger(
"score_model")
13 def score_model(complete_fn_model,
18 images_per_batch=250):
21 Scores a model against the images in the selection file.
22 Reads the images in batchs to avoid memory problems
23 resolution and pixel_size are used for generating projections of the model
24 The finder is an em2d.ProjectionFinder used for projection matching and optimizations
26 print "SCORING MODEL:",complete_fn_model
29 images_dir, nil = os.path.split(images_sel_file)
30 images_names = em2d.read_selection_file(images_sel_file)
31 n_images = len(images_names)
33 raise ValueError(
" Scoring with a empty set of images")
36 params = em2d.Em2DRestraintParameters(pixel_size, resolution)
37 params.coarse_registration_method = em2d.ALIGN2D_PREPROCESSING
38 params.optimization_steps = 4
39 params.simplex_initial_length = 0.1
40 params.simplex_minimum_size=0.02
41 params.save_match_images =
True
42 score_function = em2d.EM2DScore()
43 finder = em2d.ProjectionFinder()
44 finder.setup(score_function, params)
47 srw = em2d.SpiderImageReaderWriter()
48 test_imgs = em2d.read_images([os.path.join(images_dir, images_names[0])], srw)
49 rows = test_imgs[0].get_header().get_number_of_columns()
50 cols = test_imgs[0].get_header().get_number_of_rows()
53 ssel = atom.ATOMPDBSelector()
54 prot = atom.read_pdb(complete_fn_model, model, ssel)
57 proj_params = em2d.get_evenly_distributed_registration_results(n_projections)
58 opts = em2d.ProjectingOptions(pixel_size, resolution)
59 projections = em2d.get_projections(particles, proj_params, rows, cols, opts)
61 finder.set_model_particles(particles)
62 finder.set_projections(projections)
63 optimized_solutions= 2
64 finder.set_fast_mode(optimized_solutions)
66 all_registration_results = []
68 init_time = time.time()
69 while(init_set < n_images):
70 end_set = min( init_set + images_per_batch, n_images )
73 subjects = em2d.read_images(images_names[init_set:end_set], srw)
75 finder.set_subjects(subjects)
77 finder.get_complete_registration()
79 registration_results = finder.get_registration_results()
80 for reg
in registration_results:
81 all_registration_results.append(reg)
82 init_set += images_per_batch
84 em2d.write_registration_results(
"registration.params", all_registration_results)
85 print "score_model: time complete registration",time.time()-init_time
86 print "coarse registration time",finder.get_coarse_registration_time()
87 print "fine registration time",finder.get_fine_registration_time()
88 return all_registration_results
93 model: PDB file of the model to score
94 selfile: Selection file containing the images used for scoring
95 pxsize: Pixel size of the images in Angstrom/pixel
96 nproj: Number of projections of the model used for start registering
97 res: Resolution to generate the projections
98 nimg: Images per batch used when scoring a lot of images (to avoid memory
101 description=
"EMageFit scoring",
103 opts, args = parser.parse_args()
105 parser.error(
"wrong number of arguments")
109 if __name__ ==
"__main__":
111 complete_fn_model = args[0]
112 images_sel_file = args[1]
113 pixel_size = float(args[2])
114 n_projections= int(args[3])
115 resolution= float(args[4])
116 images_per_batch= int(args[5])
118 all_regs = score_model(complete_fn_model,
124 for i, reg
in enumerate(all_regs):
125 print "Score for image %s: %f" % (i, reg.get_score())
126 print "Global score ", em2d.get_global_score(all_regs)