Пример #1
0
def test_imagesetfactory(centroid_files, dials_data):
    sequence = ImageSetFactory.new(centroid_files)

    assert isinstance(sequence[0], ImageSequence)

    template = dials_data("centroid_test_data").join(
        "centroid_####.cbf").strpath
    image_range = (3, 6)

    sequence = ImageSetFactory.from_template(template, image_range)

    assert isinstance(sequence[0], ImageSequence)
    assert len(sequence[0]) == 4
    assert sequence[0].paths()[0].endswith("3.cbf")
    assert sequence[0].paths()[-1].endswith("6.cbf")

    imageset = ImageSetFactory.make_imageset(centroid_files)
    assert len(imageset) == 9

    imageset = ImageSetFactory.make_imageset(centroid_files,
                                             check_format=False)
    assert len(imageset) == 9

    sequence = ImageSetFactory.make_sequence(template, list(range(1, 9 + 1)))
    assert len(sequence) == 9

    sequence = ImageSetFactory.make_sequence(template, list(range(3, 6 + 1)))
    assert len(sequence) == 4
Пример #2
0
def test_make_sequence_with_percent_character(dials_data, tmp_path):
    images = [
        dials_data("centroid_test_data").join(f"centroid_{i:04}.cbf")
        for i in range(1, 10)
    ]
    directory = tmp_path / "test%"
    directory.mkdir()
    try:
        for image in images:
            try:
                (directory / image.basename).symlink_to(image)
            except OSError:
                shutil.copy(image, directory)

        template = str(directory / "centroid_####.cbf")
        sequence = ImageSetFactory.make_sequence(template, range(1, 10))
        assert len(sequence) == 9

        sequences = ImageSetFactory.new(
            [str(directory / image.basename) for image in images])
        assert len(sequences) == 1
        assert len(sequences[0]) == 9

        sequences = ImageSetFactory.from_template(template)
        assert len(sequences) == 1
        assert len(sequences[0]) == 9

    finally:  # clean up potentially copied files after running test
        for image in images:
            try:
                (directory / image.basename).unlink()
            except FileNotFoundError:
                pass
Пример #3
0
    def convert_stills_to_sequence(self, imageset):
        from dxtbx.model import Scan

        assert self.params.geometry.scan.oscillation is not None
        beam = imageset.get_beam(index=0)
        detector = imageset.get_detector(index=0)
        goniometer = imageset.get_goniometer(index=0)
        for i in range(1, len(imageset)):
            b_i = imageset.get_beam(i)
            d_i = imageset.get_detector(i)
            g_i = imageset.get_goniometer(i)
            assert (beam is None and b_i is None) or beam.is_similar_to(
                imageset.get_beam(index=i),
                wavelength_tolerance=self.params.input.tolerance.beam.wavelength,
                direction_tolerance=self.params.input.tolerance.beam.direction,
                polarization_normal_tolerance=self.params.input.tolerance.beam.polarization_normal,
                polarization_fraction_tolerance=self.params.input.tolerance.beam.polarization_fraction,
            )
            assert (detector is None and d_i is None) or detector.is_similar_to(
                imageset.get_detector(index=i),
                fast_axis_tolerance=self.params.input.tolerance.detector.fast_axis,
                slow_axis_tolerance=self.params.input.tolerance.detector.slow_axis,
                origin_tolerance=self.params.input.tolerance.detector.origin,
            )
            assert (goniometer is None and g_i is None) or goniometer.is_similar_to(
                imageset.get_goniometer(index=i),
                rotation_axis_tolerance=self.params.input.tolerance.goniometer.rotation_axis,
                fixed_rotation_tolerance=self.params.input.tolerance.goniometer.fixed_rotation,
                setting_rotation_tolerance=self.params.input.tolerance.goniometer.setting_rotation,
            )
        oscillation = self.params.geometry.scan.oscillation
        from dxtbx.imageset import ImageSetFactory
        from dxtbx.sequence_filenames import template_regex_from_list

        template, indices = template_regex_from_list(imageset.paths())
        image_range = (min(indices), max(indices))
        assert (image_range[1] + 1 - image_range[0]) == len(indices)
        scan = Scan(image_range=image_range, oscillation=oscillation)
        if template is None:
            paths = [imageset.get_path(i) for i in range(len(imageset))]
            assert len(set(paths)) == 1
            template = paths[0]
        new_sequence = ImageSetFactory.make_sequence(
            template=template,
            indices=indices,
            format_class=imageset.reader().get_format_class(),
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
        )
        return new_sequence
Пример #4
0
    def extrapolate_imageset(
        self, imageset=None, beam=None, detector=None, goniometer=None, scan=None
    ):
        from dxtbx.imageset import ImageSetFactory

        first, last = scan.get_image_range()
        sequence = ImageSetFactory.make_sequence(
            template=imageset.get_template(),
            indices=list(range(first, last + 1)),
            format_class=imageset.get_format_class(),
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            format_kwargs=imageset.params(),
        )
        return sequence
Пример #5
0
def test_make_sequence_with_percent_character(dials_data, tmp_path):
    images = [
        dials_data("centroid_test_data").join(f"centroid_{i:04}.cbf")
        for i in range(1, 10)
    ]
    directory = tmp_path / "test%"
    directory.mkdir()
    for image in images:
        (directory / image.basename).symlink_to(image)
    template = str(directory / "centroid_####.cbf")
    sequence = ImageSetFactory.make_sequence(template, range(1, 10))
    assert len(sequence) == 9

    sequences = ImageSetFactory.new(
        [str(directory / image.basename) for image in images]
    )
    assert len(sequences) == 1
    assert len(sequences[0]) == 9

    sequences = ImageSetFactory.from_template(template)
    assert len(sequences) == 1
    assert len(sequences[0]) == 9
Пример #6
0
    def _make_sequence(
        self,
        imageset,
        beam=None,
        detector=None,
        goniometer=None,
        scan=None,
        format_kwargs=None,
    ):
        """Make an image sequence."""
        # Get the template format
        template = resolve_path(imageset["template"],
                                directory=self._directory)

        # Get the number of images (if no scan is given we'll try
        # to find all the images matching the template
        if scan is None:
            i0, i1 = template_image_range(template)
        else:
            i0, i1 = scan.get_image_range()

        format_class = None
        if self._check_format is False:
            if "single_file_indices" in imageset:
                format_class = FormatMultiImage

        # Make a sequence from the input data
        return ImageSetFactory.make_sequence(
            template,
            list(range(i0, i1 + 1)),
            format_class=format_class,
            check_format=self._check_format,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            format_kwargs=format_kwargs,
        )
Пример #7
0
def imagesequence_from_dict(d, check_format=True, directory=None):
    """Construct and image sequence from the dictionary."""
    # Get the template (required)
    template = resolve_path(str(d["template"]), directory=directory)

    # If the scan isn't set, find all available files
    scan_dict = d.get("scan")
    if scan_dict is None:
        image_range = None
    else:
        image_range = scan_dict.get("image_range")

    # Set the models with the exisiting models as templates
    beam = BeamFactory.from_dict(d.get("beam"))
    goniometer = GoniometerFactory.from_dict(d.get("goniometer"))
    detector = DetectorFactory.from_dict(d.get("detector"))
    scan = ScanFactory.from_dict(d.get("scan"))

    # Construct the sequence
    try:
        sequence = ImageSetFactory.from_template(
            template,
            image_range,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            check_format=check_format,
        )[0]
    except Exception:
        indices = list(range(image_range[0], image_range[1] + 1))
        sequence = ImageSetFactory.make_sequence(
            template,
            indices,
            beam=beam,
            detector=detector,
            goniometer=goniometer,
            scan=scan,
            check_format=check_format,
        )

    # Set some external lookups
    if "mask" in d and d["mask"] is not None and d["mask"] != "":
        path = resolve_path(d["mask"], directory=directory)
        with open(path) as infile:
            sequence.external_lookup.mask.filename = path
            sequence.external_lookup.mask.data = ImageBool(pickle.load(infile))
    if "gain" in d and d["gain"] is not None and d["gain"] != "":
        path = resolve_path(d["gain"], directory=directory)
        with open(path) as infile:
            sequence.external_lookup.gain.filename = path
            sequence.external_lookup.gain.data = ImageDouble(
                pickle.load(infile))
    if "pedestal" in d and d["pedestal"] is not None and d["pedestal"] != "":
        path = resolve_path(d["pedestal"], directory=directory)
        with open(path) as infile:
            sequence.external_lookup.pedestal.filename = path
            sequence.external_lookup.pedestal.data = ImageDouble(
                pickle.load(infile))

    return sequence