Esempio n. 1
0
    def test_to_from_dict(self):
        # no complicated objects in the 'data' or 'nodes' field
        a = StructureNL(self.s, self.hulk, ['test_project'], self.pmg,
                        ['remark1'], {"_my_data": "string"},
                        [self.valid_node, self.valid_node2])
        b = StructureNL.from_dict(a.as_dict())
        self.assertEqual(a, b)
        # complicated objects in the 'data' and 'nodes' field
        complicated_node = {
            "name": "complicated node",
            "url": "www.complicatednodegoeshere.com",
            "description": {
                "structure": self.s2
            }
        }
        a = StructureNL(self.s, self.hulk, ['test_project'], self.pmg,
                        ['remark1'], {"_my_data": {
                            "structure": self.s2
                        }}, [complicated_node, self.valid_node])
        b = StructureNL.from_dict(a.as_dict())
        self.assertEqual(
            a, b, 'to/from dict is broken when object embedding is '
            'used! Apparently MontyEncoding is broken...')

        # Test molecule
        molnl = StructureNL(self.mol, self.hulk, references=self.pmg)
        b = StructureNL.from_dict(molnl.as_dict())
        self.assertEqual(molnl, b)
Esempio n. 2
0
    def match(self, snls, mat):
        """
        Finds a material doc that matches with the given snl
        Args:
            snl ([dict]): the snls list
            mat (dict): a materials doc
        Returns:
            generator of materials doc keys
        """

        m_strucs = [Structure.from_dict(mat["structure"])] + [
            Structure.from_dict(init_struc)
            for init_struc in mat["initial_structures"]
        ]
        snl_strucs = [StructureNL.from_dict(snl) for snl in snls]

        groups = group_structures(
            m_strucs + snl_strucs,
            ltol=self.settings.LTOL,
            stol=self.settings.STOL,
            angle_tol=self.settings.ANGLE_TOL,
        )
        matched_groups = [
            group for group in groups if any(
                isinstance(struc, Structure) for struc in group)
        ]
        snls = [
            struc for struc in group for group in matched_groups
            if isinstance(struc, StructureNL)
        ]

        self.logger.debug(f"Found {len(snls)} SNLs for {mat['material_id']}")
        return snls
Esempio n. 3
0
    def collect_snls_mp(self, snl_dict):
        """
        Converts a dict of materials and snls into docs for the snl by choosing the first by creation date and storing all applicable ICSD ids
        """

        snls = []

        for mat_id, snl_list in snl_dict.items():
            snl = sorted(
                snl_list, key=lambda x: StructureNL.from_dict(x).created_at)[0]
            icsd_ids = list(filter(None, [get(snl, "about._icsd.icsd_id", None) for snl in snl_list]))
            snls.append({self.snls.key: mat_id, "snl": snl, "icsd_ids": icsd_ids})
        return snls
Esempio n. 4
0
    def test_to_from_dict(self):
        # no complicated objects in the 'data' or 'nodes' field
        a = StructureNL(self.s, self.hulk, ['test_project'], self.pmg,
                        ['remark1'], {"_my_data": "string"},
                        [self.valid_node, self.valid_node2])
        b = StructureNL.from_dict(a.as_dict())
        self.assertEqual(a, b)
        # complicated objects in the 'data' and 'nodes' field
        complicated_node = {"name": "complicated node",
                            "url": "www.complicatednodegoeshere.com",
                            "description": {"structure": self.s2}}
        a = StructureNL(self.s, self.hulk, ['test_project'], self.pmg,
                        ['remark1'], {"_my_data": {"structure": self.s2}},
                        [complicated_node, self.valid_node])
        b = StructureNL.from_dict(a.as_dict())
        self.assertEqual(a, b,
                         'to/from dict is broken when object embedding is '
                         'used! Apparently MontyEncoding is broken...')

        #Test molecule
        molnl = StructureNL(self.mol, self.hulk, references=self.pmg)
        b = StructureNL.from_dict(molnl.as_dict())
        self.assertEqual(molnl, b)
Esempio n. 5
0
    def match(self, snls, mat):
        """
        Finds a material doc that matches with the given snl

        Args:
            snl ([dict]): the snls list
            mat (dict): a materials doc

        Returns:
            generator of materials doc keys
        """
        sm = StructureMatcher(ltol=LTOL,
                              stol=STOL,
                              angle_tol=ANGLE_TOL,
                              primitive_cell=True,
                              scale=True,
                              attempt_supercell=False,
                              allow_subset=False,
                              comparator=ElementComparator())

        m_strucs = [Structure.from_dict(mat["structure"])] + [
            Structure.from_dict(init_struc)
            for init_struc in mat["initial_structures"]
        ]
        for snl in snls:
            try:
                snl_struc = StructureNL.from_dict(snl).structure
                # Get SNL Spacegroup
                # This try-except fixes issues for some structures where space group data is not returned by spglib
                try:
                    snl_spacegroup = snl_struc.get_space_group_info(
                        symprec=0.1)[0]
                except:
                    snl_spacegroup = -1
                for struc in m_strucs:

                    # Get Materials Structure Spacegroup
                    try:
                        struc_sg = struc.get_space_group_info(symprec=0.1)[0]
                    except:
                        struc_sg = -1

                    # Match spacegroups
                    if struc_sg == snl_spacegroup and sm.fit(struc, snl_struc):
                        yield snl
                        break
            except:
                self.logger.warning("Bad SNL found : {}".format(
                    snl.get("task_id")))
Esempio n. 6
0
    def update_targets(self, items):
        """
        Inserts the new task_types into the task_types collection

        """
        snls = []

        for snl_dict in filter(None, items):
            for mat_id, snl_list in snl_dict.items():
                snl = sorted(
                    snl_list, key=lambda x: StructureNL.from_dict(x).created_at)[0]
                icsd_ids = [get(snl, "about._icsd.icsd_id")
                            for snl in snl_list if has(snl, "about._icsd")]
                snls.append(
                    {self.snls.key: mat_id, "snl": snl, "icsd_ids": icsd_ids})

        if len(snls) > 0:
            self.snls.update(snls)
        else:
            self.logger.info("No items to update")
Esempio n. 7
0
    def match(self, snls, mat):
        """
        Finds a material doc that matches with the given snl

        Args:
            snl ([dict]): the snls list
            mat (dict): a materials doc

        Returns:
            generator of materials doc keys
        """
        sm = StructureMatcher(ltol=self.ltol,
                              stol=self.stol,
                              angle_tol=self.angle_tol,
                              primitive_cell=True,
                              scale=True,
                              attempt_supercell=False,
                              allow_subset=False,
                              comparator=ElementComparator())

        m_strucs = [Structure.from_dict(mat["structure"])] + [
            Structure.from_dict(init_struc)
            for init_struc in mat["initial_structures"]
        ]
        for snl in snls:
            snl_struc = StructureNL.from_dict(snl).structure
            try:
                snl_spacegroup = snl_struc.get_space_group_info()[0]
            except:
                snl_spacegroup = -1
            for struc in m_strucs:
                try:
                    struc_sg = struc.get_space_group_info()[0]
                except:
                    struc_sg = -1
                # The try-excepts are a temp fix to a spglib bug
                if struc_sg == snl_spacegroup and sm.fit(struc, snl_struc):
                    yield snl
                    break
Esempio n. 8
0
    def match(self, snl, mats):
        """
        Finds a material doc that matches with the given snl

        Args:
            snl (dict): the snl doc
            mats ([dict]): the materials docs to match against

        Returns:
            dict: a materials doc if one is found otherwise returns None
        """
        sm = StructureMatcher(ltol=self.ltol, stol=self.stol, angle_tol=self.angle_tol,
                              primitive_cell=True, scale=True,
                              attempt_supercell=False, allow_subset=False,
                              comparator=ElementComparator())
        snl_struc = StructureNL.from_dict(snl).structure

        for m in mats:
            m_struct = Structure.from_dict(m["structure"])
            init_m_struct = Structure.from_dict(m["initial_structure"])
            if sm.fit(m_struct, snl_struc) or sm.fit(init_m_struct, snl_struc):
                return m[self.materials.key]

        return None