<commit_before>#!/usr/bin/env python
#
# aaply_mask.py
#
#  Copyright (C) 2013 Diamond Light Source
#
#  Author: <NAME>
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package.

from __future__ import absolute_import, division, print_function

from iotbx.phil import parse

help_message = """

This program augments a experiements JSON file with one or more masks specified by the
user.  Its only function is to input the mask file paths to the experiments JSON file,
but means that the user does not have to edit the experiments file by hand.

Crucially, the mask files must be provided in the same order as their corresponding
imagesets (sweeps) appear in the experiments JSON file.

Examples::

    dials.apply_mask experiments.json input.mask=mask.pickle

    dials.apply_mask experiments.json input.mask=mask1.pickle input.mask=mask2.pickle

"""

phil_scope = parse(
    """
        input {
            mask = None
                .multiple = True
                .type = str
                .help = "The mask filenames, one mask per imageset"
        }

        output {
            experiments = masked_experiments.json
                .type = str
                .help = "Name of output experiments file"
        }
    """,
    process_includes=True,
)


class Script(object):
    """ A class to encapsulate the script. """

    def __init__(self):
        """ Initialise the script. """
        from dials.util.options import OptionParser
        import libtbx.load_env

        # Create the parser
        usage = (
            "usage: %s experiments.json input.mask=mask.pickle"
            % libtbx.env.dispatcher_name
        )
        self.parser = OptionParser(
            usage=usage, epilog=help_message, phil=phil_scope, read_experiments=True
        )

    def run(self):
        """ Run the script. """
        from dials.util.options import flatten_experiments
        from dxtbx.model.experiment_list import ExperimentListDumper
        from dials.util import Sorry

        # Parse the command line arguments
        params, options = self.parser.parse_args(show_diff_phil=True)
        experiments = flatten_experiments(params.input.experiments)

        # Check that an experiment list and at least one mask file have been provided
        if not (experiments and params.input.mask):
            self.parser.print_help()
            return

        # Check number of experiments
        n_expts = len(experiments)
        n_masks = len(params.input.mask)
        if n_expts != n_masks:
            raise Sorry(
                "The number of masks provided must match the number of imagesets "
                "(sweeps).\n"
                "You have provided an experiment list containing {} imageset(s).\n"
                "You have provided {} mask file(s).".format(n_expts, n_masks)
            )

        # Get the imageset
        imagesets = experiments.imagesets()

        for i, imageset in enumerate(imagesets):
            # Set the lookup
            imageset.external_lookup.mask.filename = params.input.mask[i]

        # Dump the experiments
        print("Writing experiments to %s" % params.output.experiments)
        dump = ExperimentListDumper(experiments)
        dump.as_json(filename=params.output.experiments)


if __name__ == "__main__":
    from dials.util import halraiser

    try:
        script = Script()
        script.run()
    except Exception as e:
        halraiser(e)<commit_msg>Make dials.apply_mask apply the mask ImageBool<commit_after>#!/usr/bin/env python
#
# aaply_mask.py
#
#  Copyright (C) 2013 Diamond Light Source
#
#  Author: <NAME>
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package.

from __future__ import absolute_import, division, print_function

import cPickle as pickle

from dxtbx.format.image import ImageBool
from iotbx.phil import parse

help_message = """

This program augments a experiements JSON file with one or more masks specified by the
user.  Its only function is to input the mask file paths to the experiments JSON file,
but means that the user does not have to edit the experiments file by hand.

Crucially, the mask files must be provided in the same order as their corresponding
imagesets (sweeps) appear in the experiments JSON file.

Examples::

    dials.apply_mask experiments.json input.mask=mask.pickle

    dials.apply_mask experiments.json input.mask=mask1.pickle input.mask=mask2.pickle

"""

phil_scope = parse(
    """
        input {
            mask = None
                .multiple = True
                .type = str
                .help = "The mask filenames, one mask per imageset"
        }

        output {
            experiments = masked_experiments.json
                .type = str
                .help = "Name of output experiments file"
        }
    """,
    process_includes=True,
)


class Script(object):
    """ A class to encapsulate the script. """

    def __init__(self):
        """ Initialise the script. """
        from dials.util.options import OptionParser
        import libtbx.load_env

        # Create the parser
        usage = (
            "usage: %s experiments.json input.mask=mask.pickle"
            % libtbx.env.dispatcher_name
        )
        self.parser = OptionParser(
            usage=usage, epilog=help_message, phil=phil_scope, read_experiments=True
        )

    def run(self):
        """ Run the script. """
        from dials.util.options import flatten_experiments
        from dxtbx.model.experiment_list import ExperimentListDumper
        from dials.util import Sorry

        # Parse the command line arguments
        params, options = self.parser.parse_args(show_diff_phil=True)
        experiments = flatten_experiments(params.input.experiments)

        # Check that an experiment list and at least one mask file have been provided
        if not (experiments and params.input.mask):
            self.parser.print_help()
            return

        # Check number of experiments
        n_expts = len(experiments)
        n_masks = len(params.input.mask)
        if n_expts != n_masks:
            raise Sorry(
                "The number of masks provided must match the number of imagesets "
                "(sweeps).\n"
                "You have provided an experiment list containing {} imageset(s).\n"
                "You have provided {} mask file(s).".format(n_expts, n_masks)
            )

        # Get the imageset
        imagesets = experiments.imagesets()

        for i, imageset in enumerate(imagesets):
            # Set the lookup
            with open(params.input.mask[i]) as f:
                mask = pickle.load(f)
            imageset.external_lookup.mask.filename = params.input.mask[i]
            imageset.external_lookup.mask.data = ImageBool(mask)

        # Dump the experiments
        print("Writing experiments to %s" % params.output.experiments)
        dump = ExperimentListDumper(experiments)
        dump.as_json(filename=params.output.experiments)


if __name__ == "__main__":
    from dials.util import halraiser

    try:
        script = Script()
        script.run()
    except Exception as e:
        halraiser(e)
{'fraction_non_alphanumeric': 0.07731462374466914, 'fraction_numerical': 0.0016508460586050352, 'mean_word_length': 2.277727682596934, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1430947', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1962, 'n_words': 642}