def test_reflections_and_experiments_from_files(): """Test correct extracting of reflections and experiments.""" # Test when input reflections order matches the experiments order refl_file_list = [ mock_two_reflection_file_object(ids=[0, 1]), mock_reflection_file_object(id_=2), ] def mock_exp_obj(id_=0): """Make a mock experiments file object.""" exp = Mock() exp.data = ExperimentList() exp.data.append(Experiment(identifier=str(id_))) return exp exp_file_list = [mock_exp_obj(id_=i) for i in [0, 1, 2]] refls, expts = reflections_and_experiments_from_files(refl_file_list, exp_file_list) assert refls[0] is refl_file_list[0].data assert refls[1] is refl_file_list[1].data assert expts[0].identifier == "0" assert expts[1].identifier == "1" assert expts[2].identifier == "2" # Test when input reflections order does not match experiments order. refl_file_list = [ mock_reflection_file_object(id_=2), mock_two_reflection_file_object(ids=[0, 1]), ] refls, expts = reflections_and_experiments_from_files(refl_file_list, exp_file_list) assert refls[0] is refl_file_list[1].data assert refls[1] is refl_file_list[0].data assert expts[0].identifier == "0" assert expts[1].identifier == "1" assert expts[2].identifier == "2"
def run(args): usage = "dials.estimate_resolution [options] (scaled.expt scaled.refl | scaled_unmerged.mtz)" import libtbx.load_env if libtbx.env.dispatcher_name == "dials.resolutionizer": warnings.warn( "dials.resolutionizer is now deprecated, please use dials.estimate_resolution instead" ) parser = OptionParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options, unhandled = parser.parse_args( args=args, return_unhandled=True, show_diff_phil=True ) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) if (not reflections or not experiments) and not unhandled: parser.print_help() return if reflections and experiments and unhandled: sys.exit( "Must provide either scaled unmerged mtz OR dials-format scaled reflections and experiments files" ) # Configure the logging log.config(logfile=params.output.log, verbosity=options.verbose) logger.info(dials_version()) if len(unhandled) == 1: scaled_unmerged = unhandled[0] m = resolution_analysis.Resolutionizer.from_unmerged_mtz( scaled_unmerged, params.resolution ) else: reflections = parse_multiple_datasets(reflections) m = resolution_analysis.Resolutionizer.from_reflections_and_experiments( reflections, experiments, params.resolution ) plots = m.resolution_auto() if params.output.html: output_html_report(plots, params.output.html) if params.output.json: with open(params.output.json, "w") as fh: json.dump(plots, fh) return plots
def run(args=None): usage = "dials.indexed_as_integrated [options] indexed.refl indexed.expt" parser = ArgumentParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, _ = parser.parse_args(args, show_diff_phil=True) log.config(logfile=params.output.log) logger.info( dials_version() + "\n\n----------------------------------------------------------\n" "Do not use the output for structure solution or refinement" "\n----------------------------------------------------------") reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(reflections) != 1 or len(experiments) != 1: parser.print_help() return reflections = reflections[0] pseudo_integrated = indexed_as_integrated(reflections, params, experiments) pseudo_integrated.as_file(params.output.reflections) logger.info( f"\nSaved pseudo-integrated data to: {params.output.reflections}\n")
def run(args=None, phil=phil_scope): """ Validate the arguments and load experiments/reflections for sequence_to_stills Arguments: args: The command line arguments to use. Defaults to sys.argv[1:] phil: The phil_scope. Defaults to the master phil_scope for this program """ # The script usage usage = "usage: dials.sequence_to_stills [options] [param.phil] models.expt reflections.refl" # Create the parser parser = OptionParser( usage=usage, phil=phil, read_experiments=True, read_reflections=True, check_format=False, epilog=__doc__, ) params, options = parser.parse_args(args=args, show_diff_phil=True) # Try to load the models and data if not params.input.experiments or not params.input.reflections: parser.print_help() return reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) (new_experiments, new_reflections) = sequence_to_stills(experiments, reflections, params) # Write out the output experiments, reflections new_experiments.as_file(params.output.experiments) new_reflections.as_file(params.output.reflections)
def run(args: List[str] = None, phil: phil.scope = phil_scope) -> None: parser = OptionParser( usage="", read_experiments=True, read_reflections=True, phil=phil_scope, check_format=False, epilog=__doc__, ) params, options = parser.parse_args(args=args, show_diff_phil=False) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) log.config(verbosity=1, logfile="dials.ssx_inspect.log") logger.info(dials_version()) diff_phil = parser.diff_phil.as_str() if diff_phil: logger.info("The following parameters have been modified:\n%s", diff_phil) inspect(experiments, reflections[0], params)
def run(args: List[str] = None, phil: libtbx.phil.scope = phil_scope) -> None: """Run the scaling from the command-line.""" usage = """Usage: dials.refine_error_model scaled.refl scaled.expt [options]""" parser = OptionParser( usage=usage, read_experiments=True, read_reflections=True, phil=phil, check_format=False, epilog=__doc__, ) params, options = parser.parse_args(args=args, show_diff_phil=False) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) log.config(verbosity=options.verbose, logfile=params.output.log) logger.info(dials_version()) diff_phil = parser.diff_phil.as_str() if diff_phil: logger.info("The following parameters have been modified:\n%s", diff_phil) model = refine_error_model(params, experiments, reflections) if model: make_output(model, params) logger.info("Finished")
def run(args=None): from dials.util import Sorry usage = "dials.augment_spots [options] [models.expt] strong.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(reflections) != 1: parser.print_help() return if len(experiments) > 1: raise Sorry("0, 1 experiments required") stronger = augment_reflections(reflections[0], params, experiments=experiments) stronger.as_file(params.output.reflections)
def run(args=None): from dials.util.options import OptionParser, reflections_and_experiments_from_files usage = "dials.spot_resolution_shells [options] models.expt observations.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, read_reflections=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(experiments) == 0 or len(reflections) == 0: parser.print_help() exit(0) reflections = reflections[0] spot_resolution_shells(experiments, reflections, params)
def run(args=None): """Run the merging from the command-line.""" usage = """Usage: dials.merge scaled.refl scaled.expt [options]""" parser = OptionParser( usage=usage, read_experiments=True, read_reflections=True, phil=phil_scope, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args=args, show_diff_phil=False) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) log.config(verbosity=options.verbose, logfile=params.output.log) logger.info(dials_version()) diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) ### Assert that all data have been scaled with dials - should only be # able to input one reflection table and experimentlist that are # matching and scaled together. if len(reflections) != 1: raise Sorry("""Only data scaled together as a single reflection dataset can be processed with dials.merge""") for k in [ "intensity.scale.value", "intensity.scale.variance", "inverse_scale_factor", ]: if k not in reflections[0]: raise Sorry("""%s not found in the reflection table. Only scaled data can be processed with dials.merge""" % k) try: mtz_file = merge_data_to_mtz(params, experiments, reflections) except ValueError as e: raise Sorry(e) logger.info("\nWriting reflections to %s", (params.output.mtz)) out = StringIO() mtz_file.show_summary(out=out) logger.info(out.getvalue()) mtz_file.write(params.output.mtz) if params.output.html: generate_html_report(mtz_file, params.output.html)
def run(args): import dials.util.log dials.util.log.print_banner() from dials.util.options import OptionParser, reflections_and_experiments_from_files usage = "dials.show [options] models.expt | image_*.cbf" parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, read_experiments_from_images=True, read_reflections=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args=args, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(experiments) == 0 and len(reflections) == 0: parser.print_help() exit() if len(experiments): if not all(e.detector for e in experiments): sys.exit("Error: experiment has no detector") if not all(e.beam for e in experiments): sys.exit("Error: experiment has no beam") print( show_experiments(experiments, show_scan_varying=params.show_scan_varying)) if params.image_statistics.show_raw: show_image_statistics(experiments, "raw") if params.image_statistics.show_corrected: show_image_statistics(experiments, "corrected") if params.show_shared_models: print() print(model_connectivity(experiments)) if len(reflections): print( show_reflections( reflections, show_intensities=params.show_intensities, show_profile_fit=params.show_profile_fit, show_centroids=params.show_centroids, show_all_reflection_data=params.show_all_reflection_data, show_flags=params.show_flags, max_reflections=params.max_reflections, show_identifiers=params.show_identifiers, ))
def run(args=None): usage = "dials.cosym [options] models.expt observations.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options, args = parser.parse_args(args=args, show_diff_phil=False, return_unhandled=True) # Configure the logging log.config(verbosity=options.verbose, logfile=params.output.log) logger.info(dials_version()) # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) if params.seed is not None: flex.set_random_seed(params.seed) np.random.seed(params.seed) random.seed(params.seed) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) reflections = parse_multiple_datasets(reflections) if len(experiments) != len(reflections): raise Sorry( "Mismatched number of experiments and reflection tables found: %s & %s." % (len(experiments), len(reflections))) try: experiments, reflections = assign_unique_identifiers( experiments, reflections) cosym_instance = cosym(experiments=experiments, reflections=reflections, params=params) except ValueError as e: raise Sorry(e) if params.output.html or params.output.json: register_default_cosym_observers(cosym_instance) cosym_instance.run() cosym_instance.export()
def run(args=None, phil=phil_scope): """Run the command-line script.""" usage = "dials.compute_delta_cchalf [options] scaled.expt scaled.refl" parser = OptionParser( usage=usage, phil=phil, epilog=help_message, read_experiments=True, read_reflections=True, check_format=False, ) params, _ = parser.parse_args(args=args, show_diff_phil=False) log.config(logfile=params.output.log) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) if not experiments and not reflections: if not params.input.mtzfile: parser.print_help() return else: try: script = CCHalfFromMTZ(params, params.input.mtzfile) except ValueError as e: sys.exit(f"Error: {e}") else: if not experiments or not reflections: parser.print_help() return else: if not len(reflections) == 1: exit("Only one reflection table can be provided") n_datasets = len(set(reflections[0]["id"]).difference({-1})) if n_datasets != len(experiments): exit( """ The number of experiments (%s) does not match the number of datasets in the reflection table (%s) """, len(experiments), n_datasets, ) try: script = CCHalfFromDials(params, experiments, reflections[0]) except ValueError as e: sys.exit(f"Error: {e}") script.run() script.output()
def run(args: List[str] = None, phil: phil.scope = phil_scope) -> None: """Run the scaling from the command-line.""" usage = """Usage: dials.scale integrated.refl integrated.expt [integrated.refl(2) integrated.expt(2) ....] [options]""" parser = OptionParser( usage=usage, read_experiments=True, read_reflections=True, phil=phil, check_format=False, epilog=__doc__, ) params, options = parser.parse_args(args=args, show_diff_phil=False) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) log.config(verbosity=options.verbose, logfile=params.output.log) logger.info(dials_version()) diff_phil = parser.diff_phil.as_str() if diff_phil: logger.info("The following parameters have been modified:\n%s", diff_phil) try: scaled_experiments, joint_table = run_scaling(params, experiments, reflections) except ValueError as e: raise Sorry(e) else: # Note, cross validation mode does not produce scaled datafiles if scaled_experiments and joint_table: logger.info( "Saving the scaled experiments to %s", params.output.experiments ) scaled_experiments.as_file(params.output.experiments) logger.info( "Saving the scaled reflections to %s", params.output.reflections ) joint_table.as_file(params.output.reflections) if params.output.unmerged_mtz: _export_unmerged_mtz(params, scaled_experiments, joint_table) if params.output.merged_mtz: _export_merged_mtz(params, scaled_experiments, joint_table) logger.info( "See dials.github.io/dials_scale_user_guide.html for more info on scaling options" )
def run(args: List[str] = None, phil: phil.scope = phil_scope) -> None: """Run the command-line script.""" usage = "dials.damage_analysis [options] scaled.expt scaled.refl | scaled.mtz" parser = OptionParser( usage=usage, phil=phil, epilog=__doc__, read_experiments=True, read_reflections=True, check_format=False, ) params, _, unhandled = parser.parse_args(args=args, show_diff_phil=False, return_unhandled=True) log.config(logfile=params.output.log) logger.info(dials_version()) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) try: if experiments and reflections: if len(reflections) != 1: raise ValueError( "A single input reflections datafile is required") if "inverse_scale_factor" not in reflections[0]: raise KeyError("Input data must be scaled.") script = PychefRunner.from_dials_data_files( params, experiments, reflections[0], ) elif unhandled and os.path.isfile(unhandled[0]): try: mtz_object = mtz.object(file_name=unhandled[0]) except RuntimeError: # If an error is encountered trying to read the file as an mtzfile raise ValueError( "Input file cannot be read as a valid experiment/reflection file or MTZ file" ) else: script = PychefRunner.from_mtz(params, mtz_object) else: parser.print_help() raise ValueError("Suitable input datafiles not provided") except (ValueError, KeyError) as e: sys.exit(f"Error: {e}") else: script.run() script.make_html_report(params.output.html, params.output.json)
def run(args): import six.moves.cPickle as pickle from dials.util import Sorry, log usage = "dials.find_hot_pixels [options] models.expt strong.refl" # Create the option parser parser = OptionParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) # Get the parameters params, options = parser.parse_args(show_diff_phil=False) # Configure the log log.config(verbosity=options.verbose, logfile="dials.find_hot_pixels.log") # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) if len(experiments) == 0 and len(reflections) == 0: parser.print_help() exit(0) if len(experiments) > 1: raise Sorry("Only one experiment can be processed at a time") else: imagesets = experiments.imagesets() if len(reflections) == 0: raise Sorry("No reflection lists found in input") if len(reflections) > 1: raise Sorry("Multiple reflections lists provided in input") assert len(reflections) == 1 reflections = reflections[0] mask = hot_pixel_mask(imagesets[0], reflections) with open(params.output.mask, "wb") as fh: pickle.dump(mask, fh, pickle.HIGHEST_PROTOCOL) print("Wrote hot pixel mask to %s" % params.output.mask)
def run(args=None): usage = "dials.estimate_resolution [options] (scaled.expt scaled.refl | scaled_unmerged.mtz)" parser = ArgumentParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=__doc__, ) params, options, unhandled = parser.parse_args(args=args, return_unhandled=True, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if (not reflections or not experiments) and not unhandled: parser.print_help() return if reflections and experiments and unhandled: sys.exit( "Must provide either scaled unmerged mtz OR dials-format scaled reflections and experiments files" ) # Configure the logging log.config(logfile=params.output.log, verbosity=options.verbose) logger.info(dials_version()) if len(unhandled) == 1: scaled_unmerged = unhandled[0] m = resolution_analysis.Resolutionizer.from_unmerged_mtz( scaled_unmerged, params.resolution) else: reflections = parse_multiple_datasets(reflections) if len(experiments) != len(reflections): sys.exit( f"Mismatched number of experiments and reflection tables found: {len(experiments)} & {len(reflections)}." ) m = resolution_analysis.Resolutionizer.from_reflections_and_experiments( reflections, experiments, params.resolution) plots = m.resolution_auto() if params.output.html: output_html_report(plots, params.output.html) if params.output.json: with open(params.output.json, "w") as fh: json.dump(plots, fh) return plots
def run(args=None): from dials.util.options import OptionParser, flatten_experiments usage = "dials.python scan_orientations.py integrated.expt integrated.refl cif_file=mol.cif image=0" parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, read_reflections=True, epilog=__doc__, ) params, _ = parser.parse_args(args, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) # Try to load the integrated geometry models and reflection data nexp = len(experiments) if nexp == 0 or len(reflections) == 0: parser.print_help() return if len(reflections) > 1: sys.exit("Only one reflections list can be imported at present") reflections = reflections[0] if len(experiments) > 1: sys.exit("Only one experiment can be imported at present") experiment = experiments[0] # Try to load the structural model # try: # structure = gemmi.read_small_structure(params.cif_file) # except Exception as e: # sys.exit("No structural model provided to rotate") structure = import_cif.import_xyz(params.cif_file) # Write out structure at the requested orientation orientation = extract_orientation(experiment, params.image) basename, ext = os.path.splitext(os.path.basename(params.cif_file)) filename = params.cif_file.replace( '.xyz', '') + '_rotated%d.xyz' % params.image #basename + ".csv" # if structure: if params.test: test_rotation(structure, orientation) new_pos = write_rotated(structure, orientation, filename, pad=0.5) #if params.test: # plot_atom_positions(new_pos) # Find reflections close to the Ewald sphere nearby = close_to_Ewald_sphere(reflections, experiments, params.image)
def run(args=None): """Run symmetry analysis from the command-line.""" usage = "dials.symmetry [options] models.expt observations.refl" parser = ArgumentParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options, args = parser.parse_args(args=args, show_diff_phil=False, return_unhandled=True) # Configure the logging log.config(verbosity=options.verbose, logfile=params.output.log) logger.info(dials_version()) # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) if params.seed is not None: flex.set_random_seed(params.seed) random.seed(params.seed) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) reflections = parse_multiple_datasets(reflections) if len(experiments) != len(reflections): sys.exit( "Mismatched number of experiments and reflection tables found: %s & %s." % (len(experiments), len(reflections))) try: experiments, reflections = assign_unique_identifiers( experiments, reflections) symmetry(experiments, reflections, params=params) except ValueError as e: sys.exit(e)
def run(args=None, phil=working_phil): usage = "dials.index [options] models.expt strong.refl" parser = OptionParser( usage=usage, phil=phil, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args=args, show_diff_phil=False) # Configure the logging log.config(verbosity=options.verbose, logfile=params.output.log) logger.info(dials_version()) # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) if len(experiments) == 0: parser.print_help() return try: indexed_experiments, indexed_reflections = index( experiments, reflections, params ) except (DialsIndexError, ValueError) as e: sys.exit(str(e)) # Save experiments logger.info("Saving refined experiments to %s", params.output.experiments) assert indexed_experiments.is_consistent() indexed_experiments.as_file(params.output.experiments) # Save reflections logger.info("Saving refined reflections to %s", params.output.reflections) indexed_reflections.as_msgpack_file(filename=params.output.reflections)
def run(args): usage = "dials.reciprocal_lattice_viewer [options] models.expt observations.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, read_reflections=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) if len(experiments) == 0 or len(reflections) == 0: parser.print_help() exit(0) if len(reflections) > 1: assert len(reflections) == len(experiments) for i in range(len(reflections)): reflections[i]["imageset_id"] = flex.int(len(reflections[i]), i) if i > 0: reflections[0].extend(reflections[i]) elif "imageset_id" not in reflections[0]: reflections[0]["imageset_id"] = reflections[0]["id"] reflections[0]["id"] = flex.int(reflections[0].size(), -1) reflections = reflections[0] a = wxtbx.app.CCTBXApp(0) a.settings = params f = ReciprocalLatticeViewer( None, -1, "Reflection data viewer", size=(1024, 768), settings=copy.deepcopy(params), ) f.load_models(experiments, reflections) f.Show() a.SetTopWindow(f) a.MainLoop()
def run(args): usage = "dials.check_indexing_symmetry [options] indexed.expt indexed.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(show_diff_phil=True) # Configure the logging log.config(logfile=params.output.log) logger.info(dials_version()) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(reflections) == 0 or len(experiments) == 0: parser.print_help() return assert len(reflections) == 1 assert len(experiments) == 1 experiment = experiments[0] reflections = reflections[0] # remove reflections with 0, 0, 0 index zero = reflections["miller_index"] == (0, 0, 0) logger.info("Removing %d unindexed reflections" % zero.count(True)) reflections = reflections.select(~zero) h, k, l = reflections["miller_index"].as_vec3_double().parts() h = h.iround() k = k.iround() l = l.iround() logger.info("Range on h: %d to %d" % (flex.min(h), flex.max(h))) logger.info("Range on k: %d to %d" % (flex.min(k), flex.max(k))) logger.info("Range on l: %d to %d" % (flex.min(l), flex.max(l))) test_P1_crystal_indexing(reflections, experiment, params) test_crystal_pointgroup_symmetry(reflections, experiment, params)
def run(): """Run the command line filtering script.""" flags = list(flex.reflection_table.flags.names.items()) flags.sort(key=itemgetter(0)) phil_scope = parse(phil_str, process_includes=True) # The script usage usage = "usage: dials.filter_reflections [options] experiment.expt" # Create the parser parser = OptionParser( usage=usage, phil=phil_scope, epilog=help_message, read_reflections=True, read_experiments=True, check_format=False, ) params, options = parser.parse_args(show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) log.config(verbosity=options.verbose) if not reflections: parser.print_help() return if len(reflections) != 1: raise Sorry("Exactly 1 reflection file must be specified") reflections = reflections[0] # Check if any filter has been set using diff_phil filter_def = [ o for o in parser.diff_phil.objects if o.name not in ["input", "output"] ] if not filter_def: print("No filter specified. Performing analysis instead.") run_analysis(flags, reflections) else: run_filtering(params, experiments, reflections)
def get_exp_refl(path, exp_file='imported.expt', refl_file='strong.refl'): '''Get experiments and reflections[0] form an .expt and .refl files Returns - experiments,reflections[0] ''' if not refl_file: refl_file = exp_file.replace('.expt', '.refl') phil_scope = phil.parse(""" """) parser = OptionParser(usage='', phil=None, read_experiments=True, read_reflections=True) args = [path + exp_file, path + refl_file] params, _ = parser.parse_args(args, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) reflections = flatten_reflections(params.input.reflections) exp = experiments[0] refl = reflections[0] return experiments, refl
def run(args): usage = ( "dials.resolutionizer [options] (scaled.expt scaled.refl | scaled_unmerged.mtz)" ) parser = OptionParser( usage=usage, phil=phil_scope, read_reflections=True, read_experiments=True, check_format=False, epilog=help_message, ) params, options, unhandled = parser.parse_args(return_unhandled=True, show_diff_phil=True) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if (not reflections or not experiments) and not unhandled: parser.print_help() return if reflections and experiments and unhandled: sys.exit( "Must provide either scaled unmerged mtz OR dials-format scaled reflections and experiments files" ) # Configure the logging log.config(logfile=params.output.log, verbosity=options.verbose) logger.info(dials_version()) if len(unhandled) == 1: scaled_unmerged = unhandled[0] m = resolutionizer.Resolutionizer.from_unmerged_mtz( scaled_unmerged, params.resolutionizer) else: reflections = parse_multiple_datasets(reflections) m = resolutionizer.Resolutionizer.from_reflections_and_experiments( reflections, experiments, params.resolutionizer) m.resolution_auto()
def run(args=None): from dials.util.options import ( ArgumentParser, reflections_and_experiments_from_files, ) from dials.util.version import dials_version usage = "dials.export models.expt reflections.refl [options]" parser = ArgumentParser( usage=usage, read_experiments=True, read_reflections=True, phil=phil_scope, epilog=help_message, ) # Get the parameters params, options = parser.parse_args(args, show_diff_phil=False) # Configure the logging log.config(logfile=params.output.log) # Print the version number logger.info(dials_version()) # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) if not params.input.experiments and not params.input.reflections: parser.print_help() sys.exit() # Get the experiments and reflections reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) exporter = BestExporter(params, experiments, reflections) exporter.export()
def run(args=None): """Run assign experiment identifiers from the command line.""" usage = ( """Usage: dials.assign_experiment_identifiers observations.refl models.expt""" ) parser = ArgumentParser( usage=usage, read_experiments=True, read_reflections=True, phil=phil_scope, check_format=False, epilog=help_message, ) params, _ = parser.parse_args(args=args, show_diff_phil=False) if not params.input.experiments or not params.input.reflections: parser.print_help() sys.exit() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) reflections = parse_multiple_datasets(reflections) if len(experiments) != len(reflections): raise Sorry( "Mismatched number of experiments and reflection tables found: %s & %s." % (len(experiments), len(reflections))) try: experiments, reflections = assign_unique_identifiers( experiments, reflections, params.identifiers) except ValueError as e: raise Sorry(e) print(f"assigned identifiers: {list(experiments.identifiers())}") experiments.as_file(params.output.experiments) joint_table = flex.reflection_table() for reflection_table in reflections: joint_table.extend(reflection_table) joint_table.as_file(params.output.reflections)
def run(args=None, phil=working_phil): """ Set up refinement from command line options, files and PHIL parameters. Run refinement and save output files as specified. Called when running dials.refine as a command-line program Args: args (list): Additional command-line arguments phil: The working PHIL parameters Returns: None """ # The script usage usage = ( "usage: dials.refine [options] [param.phil] " "models.expt observations.refl" ) # Create the parser parser = OptionParser( usage=usage, phil=phil, read_reflections=True, read_experiments=True, check_format=False, epilog=__doc__, ) # Parse the command line params, options = parser.parse_args(args=args, show_diff_phil=False) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments ) # Configure the logging dials.util.log.config(verbosity=options.verbose, logfile=params.output.log) # Try to load the models and data nexp = len(experiments) if nexp == 0 or len(reflections) == 0: parser.print_help() return if len(reflections) > 1: sys.exit("Only one reflections list can be imported at present") reflections = reflections[0] # check input is suitable msg = ( "The supplied reflection table does not have the required data " + "column: {0}" ) for key in ["xyzobs.mm.value", "xyzobs.mm.variance"]: if key not in reflections: sys.exit(msg.format(key)) logger.info(dials_version()) # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil: logger.info("The following parameters have been modified:\n") logger.info(diff_phil) # Warn about potentially unhelpful options if params.refinement.mp.nproc > 1: logger.warning( "Setting nproc > 1 is only helpful in rare " "circumstances. It is not recommended for typical data processing " "tasks." ) if params.refinement.parameterisation.scan_varying is not False: # duplicate crystal if necessary for scan varying - will need # to compare the scans with crystals - if not 1:1 will need to # split the crystals crystal_has_scan = {} for j, e in enumerate(experiments): if e.crystal in crystal_has_scan: if e.scan is not crystal_has_scan[e.crystal]: logger.info( "Duplicating crystal model for scan-varying refinement of experiment %d" % j ) e.crystal = copy.deepcopy(e.crystal) else: crystal_has_scan[e.crystal] = e.scan # Run refinement experiments, reflections, refiner, history = run_dials_refine( experiments, reflections, params ) # For the usual case of refinement of one crystal, print that model for information crystals = experiments.crystals() if len(crystals) == 1: logger.info("") logger.info("Final refined crystal model:") logger.info(crystals[0]) # Write table of centroids to file, if requested if params.output.centroids: logger.info( "Writing table of centroids to '{}'".format(params.output.centroids) ) write_centroids_table(refiner, params.output.centroids) # Write scan-varying parameters to file, if there were any if params.output.parameter_table: scans = experiments.scans() if len(scans) > 1: logger.info( "Writing a scan-varying parameter table is only supported " "for refinement of a single scan" ) else: scan = scans[0] text = refiner.get_param_reporter().varying_params_vs_image_number( scan.get_array_range() ) if text: logger.info( "Writing scan-varying parameter table to {}".format( params.output.parameter_table ) ) f = open(params.output.parameter_table, "w") f.write(text) f.close() else: logger.info("No scan-varying parameter table to write") # Save the refined experiments to file output_experiments_filename = params.output.experiments logger.info("Saving refined experiments to {}".format(output_experiments_filename)) experiments.as_file(output_experiments_filename) # Save reflections with updated predictions if requested (allow to switch # this off if it is a time-consuming step) if params.output.reflections: logger.info( "Saving reflections with updated predictions to {}".format( params.output.reflections ) ) if params.output.include_unused_reflections: reflections.as_file(params.output.reflections) else: sel = reflections.get_flags(reflections.flags.used_in_refinement) reflections.select(sel).as_file(params.output.reflections) # Save matches to file for debugging if params.output.matches: matches = refiner.get_matches() logger.info( "Saving matches (use for debugging purposes) to {}".format( params.output.matches ) ) matches.as_file(params.output.matches) # Create correlation plots if params.output.correlation_plot.filename is not None: create_correlation_plots(refiner, params.output) # Save refinement history if params.output.history: logger.info( "Saving refinement step history to {}".format(params.output.history) ) history.to_json_file(params.output.history)
def run(args=None): usage = "dials.spot_counts_per_image [options] imported.expt strong.refl" parser = OptionParser( usage=usage, read_reflections=True, read_experiments=True, phil=phil_scope, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args, show_diff_phil=False) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if not reflections and not experiments: parser.print_help() return # FIXME may want to change this to allow many to be passed i.e. # from parallel runs if len(reflections) != 1: sys.exit("Only one reflection list may be passed") reflections = reflections[0] if "miller_index" in reflections: sys.exit("Only unindexed reflections are currently supported") if any(experiments.crystals()): sys.exit("Only unindexed experiments are currently supported") reflections.centroid_px_to_mm(experiments) reflections.map_centroids_to_reciprocal_space(experiments) if params.id is not None: reflections = reflections.select(reflections["id"] == params.id) all_stats = [] for i, expt in enumerate(experiments): refl = reflections.select(reflections["id"] == i) stats = per_image_analysis.stats_per_image( expt, refl, resolution_analysis=params.resolution_analysis) all_stats.append(stats) # transpose stats summary_table = {} for s in all_stats: for k, value in s._asdict().items(): summary_table.setdefault(k, []) summary_table[k].extend(value) stats = per_image_analysis.StatsMultiImage(**summary_table) print(stats) overall_stats = per_image_analysis.stats_for_reflection_table( reflections, resolution_analysis=params.resolution_analysis) rows = [ ("Overall statistics", ""), ("#spots", "%i" % overall_stats.n_spots_total), ("#spots_no_ice", "%i" % overall_stats.n_spots_no_ice), ("d_min", f"{overall_stats.estimated_d_min:.2f}"), ( "d_min (distl method 1)", "%.2f (%.2f)" % (overall_stats.d_min_distl_method_1, overall_stats.noisiness_method_1), ), ( "d_min (distl method 2)", "%.2f (%.2f)" % (overall_stats.d_min_distl_method_2, overall_stats.noisiness_method_2), ), ] print(tabulate(rows, headers="firstrow")) if params.json: if params.split_json: for k, v in stats._asdict().items(): start, end = params.json.split(".") with open(f"{start}_{k}.{end}", "w") as fp: json.dump(v, fp) if params.joint_json: with open(params.json, "w") as fp: json.dump(stats._asdict(), fp) if params.plot: import matplotlib matplotlib.use("Agg") per_image_analysis.plot_stats(stats, filename=params.plot)
def run(args): usage = "dials.search_beam_position [options] imported.expt strong.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, read_reflections=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args(args, show_diff_phil=False) reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(experiments) == 0 or len(reflections) == 0: parser.print_help() exit(0) # Configure the logging log.config(logfile=params.output.log) # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) if params.seed is not None: flex.set_random_seed(params.seed) random.seed(params.seed) if params.nproc is libtbx.Auto: params.nproc = libtbx.introspection.number_of_processors() imagesets = experiments.imagesets() # Split all the refln tables by ID, corresponding to the respective imagesets reflections = [ refl_unique_id for refl in reflections for refl_unique_id in refl.split_by_experiment_id() ] assert len(imagesets) > 0 assert len(reflections) == len(imagesets) if params.image_range is not None and len(params.image_range) > 0: reflections = [ slice_reflections(refl, params.image_range) for refl in reflections ] for i in range(params.n_macro_cycles): if params.n_macro_cycles > 1: logger.info("Starting macro cycle %i" % (i + 1)) experiments = discover_better_experimental_model( experiments, reflections, params, nproc=params.nproc, d_min=params.d_min, mm_search_scope=params.mm_search_scope, wide_search_binning=params.wide_search_binning, plot_search_scope=params.plot_search_scope, ) logger.info("") logger.info("Saving optimised experiments to %s" % params.output.experiments) experiments.as_file(params.output.experiments)
def run(): from dials.util.options import OptionParser, reflections_and_experiments_from_files from dials.util import log usage = "dials.rl_png [options] experiments.json observations.refl" parser = OptionParser( usage=usage, phil=phil_scope, read_experiments=True, read_reflections=True, check_format=False, epilog=help_message, ) params, options = parser.parse_args() reflections, experiments = reflections_and_experiments_from_files( params.input.reflections, params.input.experiments) if len(experiments) == 0 or len(reflections) == 0: parser.print_help() exit(0) # Configure the logging log.config(logfile="dials.rl_png.log") # Log the diff phil diff_phil = parser.diff_phil.as_str() if diff_phil != "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) reflections = reflections[0] f = ReciprocalLatticePng(settings=params) f.load_models(experiments, reflections) rotation_axis = matrix.col(experiments[0].goniometer.get_rotation_axis()) s0 = matrix.col(experiments[0].beam.get_s0()) e1 = rotation_axis.normalize() e2 = s0.normalize() e3 = e1.cross(e2).normalize() f.viewer.plot("rl_rotation_axis.png", n=e1.elems) f.viewer.plot("rl_beam_vector", n=e2.elems) f.viewer.plot("rl_e3.png", n=e3.elems) n_solutions = params.basis_vector_search.n_solutions if experiments.crystals().count(None) < len(experiments): for i, c in enumerate(experiments.crystals()): A = matrix.sqr(c.get_A()) direct_matrix = A.inverse() a = direct_matrix[:3] b = direct_matrix[3:6] c = direct_matrix[6:9] prefix = "" if len(experiments.crystals()) > 1: prefix = "%i_" % (i + 1) f.viewer.plot("rl_%sa.png" % prefix, n=a) f.viewer.plot("rl_%sb.png" % prefix, n=b) f.viewer.plot("rl_%sc.png" % prefix, n=c) elif n_solutions: if "imageset_id" not in reflections: reflections["imageset_id"] = reflections["id"] reflections.centroid_px_to_mm(experiments) reflections.map_centroids_to_reciprocal_space(experiments) if params.d_min is not None: d_spacings = 1 / reflections["rlp"].norms() sel = d_spacings > params.d_min reflections = reflections.select(sel) # derive a max_cell from mm spots max_cell = find_max_cell(reflections, max_cell_multiplier=1.3, step_size=45).max_cell result = run_dps(experiments[0], reflections, max_cell) if result: solutions = [matrix.col(v) for v in result["solutions"]] for i in range(min(n_solutions, len(solutions))): v = solutions[i] f.viewer.plot("rl_solution_%s.png" % (i + 1), n=v.elems)