Example #1
0
    def test_write_read_nd_crystalmap_properties(self, temp_file_path, crystal_map):
        """Crystal map properties with more than one value in each point
        (e.g. top matching scores from dictionary indexing) can be written
        and read from file correctly.
        """
        xmap = crystal_map
        map_size = xmap.size

        prop2d_name = "prop2d"
        prop2d_shape = (map_size, 2)
        prop2d = np.arange(map_size * 2).reshape(prop2d_shape)
        xmap.prop[prop2d_name] = prop2d

        prop3d_name = "prop3d"
        prop3d_shape = (map_size, 2, 2)
        prop3d = np.arange(map_size * 4).reshape(prop3d_shape)
        xmap.prop[prop3d_name] = prop3d

        save(filename=temp_file_path, object2write=xmap)
        xmap2 = load(temp_file_path)

        assert np.allclose(xmap2.prop[prop2d_name], xmap.prop[prop2d_name])
        assert np.allclose(xmap2.prop[prop3d_name], xmap.prop[prop3d_name])
        assert np.allclose(xmap2.prop[prop2d_name].reshape(prop2d_shape), prop2d)
        assert np.allclose(xmap2.prop[prop3d_name].reshape(prop3d_shape), prop3d)
Example #2
0
    def test_point_group_aliases(self, crystal_map, tmp_path, point_group):
        crystal_map.phases[0].point_group = point_group
        fname = tmp_path / "test_point_group_aliases.ang"
        save(fname, crystal_map)
        xmap_reload = load(fname)

        phase_ids = xmap_reload.phases.ids
        assert xmap_reload.phases[phase_ids[0]].point_group.name == point_group
Example #3
0
    def test_hdf5group2dict_update_dict(self, temp_file_path, crystal_map):
        save(temp_file_path, crystal_map)
        with File(temp_file_path, mode="r") as f:
            this_dict = {"hello": "there"}
            this_dict = hdf5group2dict(f["crystal_map"], dictionary=this_dict)

            assert this_dict["hello"] == "there"
            assert this_dict["data"] == f["crystal_map/data"]
            assert this_dict["header"] == f["crystal_map/header"]
Example #4
0
    def test_write_read_loop(self, crystal_map, tmp_path):
        fname = tmp_path / "test_write_read_loop.ang"
        save(filename=fname, object2write=crystal_map)
        xmap_reload = load(filename=fname)

        assert np.allclose(
            xmap_reload.rotations.to_euler(),
            crystal_map.rotations.to_euler(),
        )
        assert np.allclose(xmap_reload.phase_id - 1, crystal_map.phase_id)
Example #5
0
    def test_points_not_in_data(self, crystal_map, tmp_path):
        crystal_map.prop["iq"] = np.ones(crystal_map.size)
        crystal_map[2].iq = 0
        xmap2 = crystal_map[crystal_map.iq == 1]
        fname = tmp_path / "test_points_not_in_data.ang"
        save(fname, xmap2)

        xmap_reload = load(fname)
        assert not xmap2.is_in_data.all()
        assert np.allclose(xmap2.is_in_data, xmap_reload.is_indexed)
Example #6
0
 def test_overwrite_or_not_input(self, crystal_map, temp_file_path, answer,
                                 expected):
     save(temp_file_path, crystal_map)
     if answer == "m":
         with replace_stdin(StringIO(answer)):
             with pytest.raises(EOFError):
                 _overwrite_or_not(temp_file_path)
     else:
         with replace_stdin(StringIO(answer)):
             assert _overwrite_or_not(temp_file_path) is expected
Example #7
0
    def test_1d_map(self, crystal_map_input, tmp_path):
        xmap = CrystalMap(**crystal_map_input)
        assert xmap.ndim == 1
        fname = tmp_path / "test_1d_map.ang"
        save(fname, xmap)

        xmap_reload = load(fname)

        assert xmap_reload.ndim == 1
        assert np.allclose(xmap.rotations.to_euler(),
                           xmap_reload.rotations.to_euler())
Example #8
0
    def test_save_overwrite(self, temp_file_path, crystal_map, overwrite,
                            expected_phase_name):
        assert crystal_map.phases[0].name == ""
        save(temp_file_path, crystal_map)
        assert os.path.isfile(temp_file_path) is True

        crystal_map.phases[0].name = "hepp"
        save(temp_file_path, crystal_map, overwrite=overwrite)

        crystal_map2 = load(temp_file_path)
        assert crystal_map2.phases[0].name == expected_phase_name
Example #9
0
    def test_write_read_masked(self, crystal_map_input, temp_file_path):
        cm = CrystalMap(**crystal_map_input)
        save(filename=temp_file_path, object2write=cm[cm.x > 2])
        cm2 = load(temp_file_path)

        assert cm2.size != cm.size
        with pytest.raises(ValueError, match="operands could not be broadcast"):
            _ = np.allclose(cm2.x, cm.x)

        cm2.is_in_data = cm.is_in_data
        assert cm2.size == cm.size
        assert np.allclose(cm2.x, cm.x)
Example #10
0
    def test_non_indexed_points(self, crystal_map, tmp_path):
        crystal_map[2].phase_id = -1
        fname = tmp_path / "test_non_indexed_points.ang"
        save(fname, crystal_map)

        xmap_reload = load(fname)

        crystal_map.phases[0].name = "phase1"
        assert xmap_reload.phases.names == crystal_map.phases.names

        new_phase_ids = xmap_reload.phase_id
        new_phase_ids[xmap_reload.is_indexed] -= 1
        assert np.allclose(new_phase_ids, crystal_map.phase_id)
Example #11
0
    def test_extra_prop(self, crystal_map, tmp_path, extra_prop):
        fname = tmp_path / "test_extra_prop.ang"
        desired_arrays = []
        n_points = crystal_map.size
        for i, name in enumerate(extra_prop):
            new_array = np.arange(n_points) * i
            crystal_map.prop[name] = new_array
            desired_arrays.append(new_array)

        save(fname, crystal_map, extra_prop=extra_prop)
        xmap_reload = load(fname)

        for name in extra_prop:
            assert np.allclose(xmap_reload.prop[name], crystal_map.prop[name])
Example #12
0
    def test_extra_phases(self, crystal_map, tmp_path, extra_phase_names):
        crystal_map.phases.add_not_indexed()
        for i, name in enumerate(extra_phase_names):
            crystal_map.phases.add(Phase(name=name))
            crystal_map[i].phase_id = crystal_map.phases.id_from_name(name)
        fname = tmp_path / "test_extra_phases.ang"
        save(fname, crystal_map)
        xmap_reload = load(fname)

        crystal_map.phases[0].name = "phase1"
        assert np.allclose(xmap_reload.phase_id - 1, crystal_map.phase_id)

        pl = crystal_map.phases
        del pl[-1]
        assert xmap_reload.phases.names == pl.names
Example #13
0
    def test_read_point_group_from_v0_3_x(self, temp_file_path, crystal_map):
        crystal_map.phases[0].point_group = "1"
        save(filename=temp_file_path, object2write=crystal_map)

        # First, ensure point group data set name is named "symmetry", as in v0.3.0
        with File(temp_file_path, mode="r+") as f:
            for phase in f["crystal_map/header/phases"].values():
                phase["symmetry"] = phase["point_group"]
                del phase["point_group"]

        # Then, make sure it can still be read
        cm2 = load(filename=temp_file_path)
        # And that the symmetry operations are the same, for good measure
        print(crystal_map)
        print(cm2)
        assert np.allclose(crystal_map.phases[0].point_group.data,
                           cm2.phases[0].point_group.data)
Example #14
0
    def test_write_data_layer_i(self, crystal_map_input, tmp_path, index):
        xmap = CrystalMap(**crystal_map_input)
        xmap.prop["ci"] = np.arange(xmap.size *
                                    xmap.rotations_per_point).reshape(
                                        (xmap.size, xmap.rotations_per_point))
        xmap.prop["iq"] = np.arange(xmap.size)
        extra_prop = "iq_times_ci"
        xmap.prop[extra_prop] = xmap.ci * xmap.iq[:, np.newaxis]
        fname = tmp_path / "test_write_data_layer_i.ang"
        save(fname, xmap, index=index, extra_prop=[extra_prop, "iq"])

        xmap_reload = load(fname)

        assert np.allclose(xmap.rotations[:, index].to_euler(),
                           xmap_reload.rotations.to_euler())
        assert np.allclose(xmap_reload.iq, np.zeros(xmap.size))
        assert np.allclose(xmap_reload.ci, xmap.ci[:, index])
        assert np.allclose(xmap_reload.iq_times_ci, xmap.iq_times_ci[:, index])
Example #15
0
def dictionary_indexing(req: DictionaryIndexingIn):
    f = io.StringIO()
    logs["pattern_matching"] = f

    if req.ebsd_info.uid not in kp_signals:
        raise HTTPException(status_code=404, detail="Patterns not found")

    s = kp_signals[req.ebsd_info.uid]
    detector = kp.detectors.EBSDDetector(
        shape=s.axes_manager.signal_shape[::-1],
        pc=req.detector.pc,
        sample_tilt=req.detector.sample_tilt,
        convention=req.detector.pc_convention,
        tilt=req.detector.camera_tilt,
    )

    mps = []
    rotations_per_mp = []
    sims_per_mp = []
    for mp_req in req.masterpatterns:
        mp = kp.load(
            mp_req.filepath,
            projection=mp_req.projection,
            hemisphere="both",
            energy=mp_req.energy,
        )
        mps.append(mp)
        rotations = sampling.get_sample_fundamental(
            resolution=req.angle_resolution,
            point_group=mp.phase.point_group.proper_subgroup,
        )
        rotations_per_mp.append(rotations)

        sims = mp.get_patterns(
            rotations=rotations,
            detector=detector,
            energy=mp_req.energy,
        )
        sims_per_mp.append(sims)

    with redirect_stderr(f):
        xmaps = s.match_patterns(
            sims_per_mp,
            metric=req.metric,
            keep_n=req.keep_n,
            n_slices=req.n_slices,
            get_orientation_similarity_map=req.osm,
            return_merged_crystal_map=True,
        )

    res = DictionaryIndexingOut()

    if len(mps) == 1:
        # xmaps is a single xmap if 1 set of simulated patterns is matched against
        phase_name = xmaps.phases_in_data.names[0]
        orix_io.save(os.path.join(req.result_dir, f"xmap_{phase_name}.h5"),
                     xmaps)

        xmaps.top_score = xmaps.scores[:, 0]
        orix_io.save(
            os.path.join(req.result_dir, f"xmap_{phase_name}.ang"),
            xmaps,
            confidence_index_prop="top_score",
        )
        main_xmap = xmaps
    else:
        for xmap in xmaps[:-1]:
            phase_name = xmap.phases_in_data.names[0]
            orix_io.save(os.path.join(req.result_dir, f"xmap_{phase_name}.h5"),
                         xmap)
            xmap.top_score = xmap.scores[:, 0]
            orix_io.save(
                os.path.join(req.result_dir, f"xmap_{phase_name}.ang"),
                xmap,
                confidence_index_prop="top_score",
            )
        merged_xmap = xmaps[-1]
        orix_io.save(os.path.join(req.result_dir, f"xmap_merged.h5"),
                     merged_xmap)
        merged_xmap.top_score = merged_xmap.scores[:, 0]
        orix_io.save(
            os.path.join(req.result_dir, f"xmap_merged.ang"),
            merged_xmap,
            confidence_index_prop="top_score",
        )

        svg_string = io.StringIO()
        fig = plt.figure()
        ax = fig.add_subplot(projection="plot_map")
        ax.plot_map(merged_xmap)
        # ax.add_overlay(merged_xmap, merged_xmap.top_score)
        ax.set_yticks([])
        ax.set_xticks([])
        plt.savefig(
            svg_string,
            format="svg",
            bbox_inches="tight",
            transparent=True,
            pad_inches=0,
        )
        svg_string.seek(0)
        res.phase_map = svg_string.getvalue()

        main_xmap = merged_xmap

    svg_string = io.StringIO()
    fig = plt.figure()
    ax = fig.add_subplot(projection="plot_map")
    im = ax.plot_map(main_xmap, main_xmap.top_score, cmap="gray")
    cbar = ax.add_colorbar(req.metric.upper())
    ax.set_axis_off()
    plt.savefig(svg_string,
                format="svg",
                bbox_inches="tight",
                transparent=True,
                pad_inches=0)
    svg_string.seek(0)
    res.score_map = svg_string.getvalue()

    svg_string = io.StringIO()
    fig = plt.figure()
    ax = fig.add_subplot(projection="plot_map")
    im = ax.plot_map(main_xmap, main_xmap.osm, cmap="gray")
    cbar = ax.add_colorbar("Orientation Similarity")
    ax.set_axis_off()
    plt.savefig(svg_string,
                format="svg",
                bbox_inches="tight",
                transparent=True,
                pad_inches=0)
    svg_string.seek(0)
    res.os_map = svg_string.getvalue()

    top_euler_rotations = main_xmap.rotations.to_euler()[:, 0, :]
    res.rgb_orientation_map = euler_rgb_orientation_map(
        req.ebsd_info, top_euler_rotations)

    return res
Example #16
0
 def test_save_overwrite_raises(self, temp_file_path, crystal_map):
     with pytest.raises(ValueError,
                        match="`overwrite` parameter can only be "):
         save(temp_file_path, crystal_map, overwrite=1)
Example #17
0
 def test_save_unsupported_raises(self, temp_file_path, crystal_map):
     _, ext = os.path.splitext(temp_file_path)
     with pytest.raises(IOError,
                        match=f"'{ext}' does not correspond to any "):
         save(temp_file_path, crystal_map)
Example #18
0
 def test_overwrite_or_not(self, crystal_map, temp_file_path):
     save(temp_file_path, crystal_map)
     with pytest.warns(UserWarning,
                       match="Not overwriting, since your terminal "):
         _overwrite_or_not(temp_file_path)
Example #19
0
from orix import sampling, io
import kikuchipy as kp


s_large = kp.data.nickel_ebsd_large()
s_large.remove_static_background()
s_large.remove_dynamic_background()
s_large.average_neighbour_patterns(window="gaussian", std=2)

mp = kp.data.nickel_ebsd_master_pattern_small(projection="lambert")
r = sampling.get_sample_fundamental(
    resolution=4, space_group=mp.phase.space_group.number
)
detector = kp.detectors.EBSDDetector(
    shape=s_large.axes_manager.signal_shape[::-1],
    pc=[0.421, 0.7794, 0.5049],
    convention="tsl",
    sample_tilt=70,
)
sim = mp.get_patterns(rotations=r, detector=detector, energy=20, compute=False)

xmap = s_large.dictionary_indexing(sim, keep_n=1, metric="ncc")
io.save("/home/hakon/ni_large.h5", xmap)
Example #20
0
 def test_file_reader(self, crystal_map, temp_file_path):
     save(filename=temp_file_path, object2write=crystal_map)
     cm2 = load(filename=temp_file_path)
     assert_dictionaries_are_equal(crystal_map.__dict__, cm2.__dict__)
Example #21
0
 def test_file_writer_raises(self, temp_file_path, crystal_map):
     with pytest.raises(OSError,
                        match="Cannot write to the already open file "):
         with File(temp_file_path, mode="w") as _:
             save(temp_file_path, crystal_map, overwrite=True)
Example #22
0
    def test_file_writer(self, crystal_map, temp_file_path):
        save(filename=temp_file_path, object2write=crystal_map)

        with File(temp_file_path, mode="r") as f:
            assert f["manufacturer"][()][0].decode() == "orix"
            assert f["version"][()][0].decode() == orix_version
Example #23
0
 def test_3d_map_raises(self, crystal_map_input, tmp_path):
     xmap = CrystalMap(**crystal_map_input)
     fname = tmp_path / "test_3d_raises.ang"
     with pytest.raises(ValueError,
                        match="Writing a 3D dataset to an .ang file"):
         save(fname, xmap)