Skip to content

Instantly share code, notes, and snippets.

@ndevenish
Last active September 12, 2019 11:12
Show Gist options
  • Save ndevenish/a0815887805ccad0475055e5cd91f875 to your computer and use it in GitHub Desktop.
Save ndevenish/a0815887805ccad0475055e5cd91f875 to your computer and use it in GitHub Desktop.
Standard DIALS command_line program boilerplate
"""
dials.new_dials_command
Put a description of your program in the top docstring. This will be
used in the OptionParser to display program help.
"""
from __future__ import absolute_import, division, print_function
import pickle
import logging
import sys
import libtbx.phil
import dials.util
import dials.util.log
from dials.util.options import OptionParser, flatten_experiments, flatten_reflections
# Define a logger. __name__ cannot be used as this script is called directly.
logger = logging.getLogger("dials.command_line.new_dials_command")
# Define the master PHIL scope for this program
phil_scope = libtbx.phil.parse(
"""
output {
reflections = stronger.refl
.type = path
log = 'dials.new_dials_command.log'
.type = str
.help = "The log filename"
}
bool_parameter = False
.type = bool
integer_parameter = 0
.type = int
"""
)
def new_dials_command(experiments, reflections, params):
"""Write the behaviour of the program outside run()"""
logger.info("Hello world!")
# Include file output here - any API interface should go elsewhere
logger.info("Writing reflection table to {}".format(params.output.reflections))
with open(params.output.reflections, "w") as f:
pickle.dump(reflections, f)
def run(args=None, phil=phil_scope):
"""
Validate and load the arguments, then run new_dials_command
Try to keep this function minimal, defining only what is necessary to run
the program from the command line.
Arguments:
args (List[str]): If specified, use these instead of sys.argv[1:]
phil (libtbx.phil.scope): Overrides the master phil_scope for this
"""
usage = "dials.new_dials_command [options] model.expt strong.refl"
parser = OptionParser(
usage=usage,
phil=phil,
read_reflections=True,
read_experiments=True,
check_format=False,
epilog=__doc__,
)
params, options = parser.parse_args(args=args, show_diff_phil=False)
# Configure the logging
dials.util.log.config(verbosity=options.verbose, logfile=params.output.log)
# Log the PHIL diff. Done _after_ log config so written to log
diff_phil = parser.diff_phil.as_str()
if diff_phil:
logger.info("The following parameters have been modified:\n%s", diff_phil)
experiments = flatten_experiments(params.input.experiments)
reflections = flatten_reflections(params.input.reflections)
# Do any simple loading validation here
if len(reflections) != 1:
sys.exit("Error: Exactly one reflection file needed")
if len(experiments) != 1:
sys.exit("Error: Exactly one experiment list required")
new_dials_command(experiments, reflections, params)
if __name__ == "__main__":
with dials.util.show_mail_on_error():
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment