Esempio n. 1
0
    def test_serialization(self):
        BaTiO3_se_fpath = os.path.join(
            self.TEST_FILES_DIR,
            "chemenv",
            "structure_environments_files",
            "se_mp-5020.json",
        )
        with open(BaTiO3_se_fpath) as f:
            dd = json.load(f)
        se = StructureEnvironments.from_dict(dd)
        lse = LightStructureEnvironments.from_structure_environments(
            strategy=SimplestChemenvStrategy(), structure_environments=se
        )
        cf = ConnectivityFinder()
        sc = cf.get_structure_connectivity(light_structure_environments=lse)
        sc_from_dict = StructureConnectivity.from_dict(sc.as_dict())
        assert sc.light_structure_environments == sc_from_dict.light_structure_environments
        assert set(sc._graph.nodes()) == set(sc_from_dict._graph.nodes())
        assert set(sc._graph.edges()) == set(sc_from_dict._graph.edges())

        sc_from_json = StructureConnectivity.from_dict(json.loads(json.dumps(sc.as_dict())))
        assert sc.light_structure_environments == sc_from_json.light_structure_environments
        assert set(sc._graph.nodes()) == set(sc_from_json._graph.nodes())
        assert set(sc._graph.edges()) == set(sc_from_json._graph.edges())

        if bson is not None:
            bson_data = bson.BSON.encode(sc.as_dict())
            sc_from_bson = StructureConnectivity.from_dict(bson_data.decode())
            assert sc.light_structure_environments == sc_from_bson.light_structure_environments
            assert set(sc._graph.nodes()) == set(sc_from_bson._graph.nodes())
            assert set(sc._graph.edges()) == set(sc_from_bson._graph.edges())
Esempio n. 2
0
    def get_structure_connectivity(self, light_structure_environments):
        """
        Get the structure connectivity from the coordination environments provided
        as an input.

        :param light_structure_environments: LightStructureEnvironments with the
        relevant coordination environments in the structure
        :return: a StructureConnectivity object describing the connectivity of
        the environments in the structure
        """
        logging.info("Setup of structure connectivity graph")
        structure_connectivity = StructureConnectivity(
            light_structure_environments)
        structure_connectivity.add_sites()
        for isite, site in enumerate(light_structure_environments.structure):
            site_neighbors_sets = light_structure_environments.neighbors_sets[
                isite]
            if site_neighbors_sets is None:
                continue
            if len(site_neighbors_sets) > 1:
                if self.multiple_environments_choice is None:
                    raise ValueError(
                        "Local environment of site {:d} is a mix and "
                        "nothing is asked about it".format(isite))
                elif self.multiple_environments_choice == "TAKE_HIGHEST_FRACTION":
                    imax = np.argmax([
                        ee["ce_fraction"]
                        for ee in light_structure_environments.
                        coordination_environments[isite]
                    ])
                    print("IMAX {:d}".format(imax))
                    site_neighbors_set = site_neighbors_sets[imax]
                else:
                    raise RuntimeError("Should not be here")
            else:
                site_neighbors_set = site_neighbors_sets[0]
            structure_connectivity.add_bonds(isite, site_neighbors_set)
        return structure_connectivity