Beispiel #1
0
    def setUp(self):
        with open(os.path.join(test_dir, 'si_dos.json'), 'r') as sDOS:
            si_dos = CompleteDos.from_dict(json.load(sDOS))
        self.df = pd.DataFrame({'dos': [si_dos], 'site': [0]})

        with open(os.path.join(test_dir, 'nb3sn_dos.json'), 'r') as sDOS:
            nb3sn_dos = CompleteDos.from_dict(json.load(sDOS))
        self.nb3sn_df = pd.DataFrame({'dos': [nb3sn_dos]})
Beispiel #2
0
    def setUp(self):
        with open(os.path.join(test_dir, 'si_dos.json'), 'r') as sDOS:
            si_dos = CompleteDos.from_dict(json.load(sDOS))
        self.df = pd.DataFrame({'dos': [si_dos], 'site': [0]})

        with open(os.path.join(test_dir, 'nb3sn_dos.json'), 'r') as sDOS:
            nb3sn_dos = CompleteDos.from_dict(json.load(sDOS))
        self.nb3sn_df = pd.DataFrame({'dos': [nb3sn_dos]})
Beispiel #3
0
 def setUp(self):
     with open(
             os.path.join(PymatgenTest.TEST_FILES_DIR,
                          "complete_dos.json")) as f:
         self.dos = CompleteDos.from_dict(json.load(f))
     with zopen(
             os.path.join(PymatgenTest.TEST_FILES_DIR,
                          "pdag3_complete_dos.json.gz")) as f:
         self.dos_pdag3 = CompleteDos.from_dict(json.load(f))
 def setUp(self):
     with open(os.path.join(test_dir, "complete_dos.json"),
               "r",
               encoding='utf-8') as f:
         self.dos = CompleteDos.from_dict(json.load(f))
         self.plotter = DosPlotter(sigma=0.2, stack=True)
     warnings.simplefilter("ignore")
Beispiel #5
0
 def get_dos(self, task_id):
     m_task = self.collection.find_one({"task_id": task_id}, {"calcs_reversed": 1})
     fs_id = m_task['calcs_reversed'][0]['dos_fs_id']
     fs = gridfs.GridFS(self.db, 'dos_fs')
     dos_json = zlib.decompress(fs.get(fs_id).read())
     dos_dict = json.loads(dos_json.decode())
     return CompleteDos.from_dict(dos_dict)
Beispiel #6
0
def extract_dos(mat):

    dos = None
    if "dos" in mat["bandstructure"]:
        dos_dict = mat["bandstructure"]["dos"]
        dos = CompleteDos.from_dict(dos_dict)
    return dos
    def setUpClass(cls):
        cls.vbm_val = 2.6682
        cls.gap = 1.5
        cls.entries = list(loadfn(os.path.join(os.path.dirname(__file__), "GaAs_test_defentries.json")).values())
        for entry in cls.entries:
            entry.parameters.update({"vbm": cls.vbm_val})
        cls.pd = DefectPhaseDiagram(cls.entries, cls.vbm_val, cls.gap)
        cls.mu_elts = {Element("As"): -4.658070555, Element("Ga"): -3.7317319750000006}

        # make Vac_As (q= -2) only defect test single-stable-charge exceptions
        cls.extra_entry = DefectEntry(cls.entries[5].defect.copy(), 100.0)
        sep_entries = [
            ent for ent in cls.entries if not (ent.name == "Vac_As_mult4" and ent.charge in [-2, -1, 0, 1, 2])
        ]
        sep_entries.append(cls.extra_entry.copy())
        cls.sep_pd = DefectPhaseDiagram(sep_entries, cls.vbm_val, cls.gap)

        # make Vac_As (q= -2) is incompatible for larger supercell
        ls_entries = cls.entries[:]
        for entry in ls_entries:
            if entry.name == "Vac_As_mult4" and entry.charge == -2.0:
                entry.parameters["is_compatible"] = False
        cls.pd_ls_fcTrue = DefectPhaseDiagram(ls_entries, cls.vbm_val, cls.gap, filter_compatible=True)
        cls.pd_ls_fcFalse = DefectPhaseDiagram(ls_entries, cls.vbm_val, cls.gap, filter_compatible=False)

        # load complete dos for fermi energy solving
        with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "complete_dos.json")) as f:
            dos_dict = json.load(f)
        cls.dos = CompleteDos.from_dict(dos_dict)
Beispiel #8
0
 def get_dos(self, task_id):
     m_task = self.collection.find_one({"task_id": task_id}, {"calcs_reversed": 1})
     fs_id = m_task['calcs_reversed'][0]['dos_fs_id']
     fs = gridfs.GridFS(self.db, 'dos_fs')
     dos_json = zlib.decompress(fs.get(fs_id).read())
     dos_dict = json.loads(dos_json.decode())
     return CompleteDos.from_dict(dos_dict)
    def setUp(self):
        self.vbm_val = 2.6682
        self.gap = 1.5
        self.entries = list(loadfn(os.path.join(os.path.dirname(__file__), "GaAs_test_defentries.json")).values())
        for entry in self.entries:
            entry.parameters.update( {'vbm': self.vbm_val})
        self.pd = DefectPhaseDiagram(self.entries, self.vbm_val, self.gap)
        self.mu_elts = {Element("As"): -4.658070555, Element("Ga"): -3.7317319750000006}

        # make Vac_As (q= -2) only defect test single-stable-charge exceptions
        self.extra_entry = DefectEntry(self.entries[5].defect.copy(), 100.)
        sep_entries = [ent for ent in self.entries if not (ent.name == 'Vac_As_mult4' and
                                                           ent.charge in [-2,-1,0,1,2])]
        sep_entries.append( self.extra_entry.copy())
        self.sep_pd = DefectPhaseDiagram( sep_entries, self.vbm_val, self.gap)

        # make Vac_As (q= -2) is incompatible for larger supercell
        ls_entries = self.entries[:]
        for entry in ls_entries:
            if entry.name == 'Vac_As_mult4' and entry.charge == -2.:
                entry.parameters['is_compatible'] = False
        self.pd_ls_fcTrue = DefectPhaseDiagram(ls_entries, self.vbm_val, self.gap, filter_compatible=True)
        self.pd_ls_fcFalse = DefectPhaseDiagram(ls_entries, self.vbm_val, self.gap, filter_compatible=False)

        # load complete dos for fermi energy solving
        with open(os.path.join(test_dir, "complete_dos.json"), "r") as f:
            dos_dict = json.load(f)
        self.dos = CompleteDos.from_dict(dos_dict)
Beispiel #10
0
 def setUp(self):
     with open(os.path.join(PymatgenTest.TEST_FILES_DIR,
                            "complete_dos.json"),
               encoding="utf-8") as f:
         self.dos = CompleteDos.from_dict(json.load(f))
         self.plotter = DosPlotter(sigma=0.2, stack=True)
     warnings.simplefilter("ignore")
Beispiel #11
0
 def setUp(self):
     try:
         import scipy
     except ImportError:
         raise SkipTest("scipy not present. Skipping...")
     with open(os.path.join(test_dir, "complete_dos.json"), "r") as f:
         self.dos = CompleteDos.from_dict(json.load(f))
         self.plotter = DosPlotter(sigma=0.2, stack=True)
Beispiel #12
0
def get_bs(dos, vasprun_bands, kpts_bands):  
    ## get BandStructureSymmLine object and "save" in hidden div in json format
    bands = Vasprun(vasprun_bands, parse_projected_eigen = True)
    if dos:
        dos = CompleteDos.from_dict(json.loads(dos))
        bs = bands.get_band_structure(kpts_bands, line_mode=True, efermi=dos.efermi)  
    else:
        bs = bands.get_band_structure(kpts_bands, line_mode=True) 
    return json.dumps(bs.as_dict(), cls=MyEncoder)
Beispiel #13
0
    def from_dict(complete_dos_dict):
        """
        Returns FeffLdos object from dict representation.

        Args:
            complete_dos_dict:
                dict representation os complete_dos
        """
        complete_dos = CompleteDos.from_dict(complete_dos_dict)
        return FeffLdos(complete_dos)
Beispiel #14
0
    def from_dict(d):
        """
        Returns FeffLdos object from dict representation

        Args:
            complete_dos: dict representation of complete_dos
        """

        complete_dos = CompleteDos.from_dict(d['complete_dos'])
        charge_transfer = d['charge_transfer']
        return FeffLdos(complete_dos, charge_transfer)
Beispiel #15
0
    def from_dict(d):
        """
        Returns FeffLdos object from dict representation

        Args:
            complete_dos: dict representation of complete_dos
        """

        complete_dos = CompleteDos.from_dict(d['complete_dos'])
        charge_transfer = d['charge_transfer']
        return FeffLdos(complete_dos, charge_transfer)
def update_dosbandsfig(n_clicks, dos, bs, projlist):
    ## figure updates when the inputs change or the button is clicked
    ## figure does NOT update when elements or orbitals are selected
    ## de-serialize dos and bs from json format to pymatgen objects
    if dos:
        dos = CompleteDos.from_dict(json.loads(dos))
    if bs:
        bs = BandStructureSymmLine.from_dict(json.loads(bs))
    ## update the band structure and dos figure
    dosbandfig = BandsFig().generate_fig(dos, bs, projlist)
    return dosbandfig
Beispiel #17
0
    def get_dos(self, task_id):
        """
        Read the DOS data into a PMG DOS object

        Args:
            task_id(int or str): the task_id containing the data
        Returns:
            CompleteDos object
        """
        obj_dict = self.get_data_from_maggma_or_gridfs(task_id, key="dos")
        return CompleteDos.from_dict(obj_dict)
Beispiel #18
0
    def featurize(self, dos):
        """
        Args:
            dos (pymatgen CompleteDos or their dict):
                The density of states to featurize. Must be a complete DOS,
                (i.e. contains PDOS and structure, in addition to total DOS)
                and must contain the structure.

        Returns:
            xbm_score_i (float): fractions of ith contributor orbital
            xbm_location_i (str): fractional coordinate of ith contributor.
                For example, '0.0;0.0;0.0' if Gamma
            xbm_specie_i: (str) elemental specie of ith contributor (ex: 'Ti')
            xbm_character_i: (str) orbital character of ith contributor (s p d or f)
            xbm_nsignificant: (int) the number of orbitals with contributions
                above the significance_threshold
        """

        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')
        orbscores = get_cbm_vbm_scores(dos, self.energy_cutoff,
                                       self.sampling_resolution,
                                       self.gaussian_smear)
        self.feat = {}
        for ex in ['cbm', 'vbm']:
            orbscores.sort(key=lambda x: x['{}_score'.format(ex)],
                           reverse=True)
            scores = np.array([s['{}_score'.format(ex)] for s in orbscores])
            self.feat['{}_nsignificant'.format(ex)] = len(
                scores[scores > self.significance_threshold])
            i = 0
            while i < self.contributors:
                sd = orbscores[i]
                if i < len(orbscores):
                    for p in ['character', 'specie']:
                        self.feat['{}_{}_{}'.format(ex, p, i + 1)] = sd[p]
                    self.feat['{}_location_{}'.format(
                        ex,
                        i + 1)] = '{};{};{}'.format(sd['location'][0],
                                                    sd['location'][1],
                                                    sd['location'][2])
                    self.feat['{}_score_{}'.format(ex, i + 1)] = float(
                        sd['{}_score'.format(ex)])
                else:
                    for p in [
                            '{}_score'.format(ex), 'character', 'specie',
                            'location'
                    ]:
                        self.feat['{}_{}_{}'.format(ex, p,
                                                    i + 1)] = float('NaN')
                i += 1
        return list(self.feat.values())
Beispiel #19
0
    def featurize(self, dos, decay_length=None):
        """
        takes in the density of state and return the orbitals contributions
        and hybridizations.

        Args:
            dos (pymatgen CompleteDos): note that dos.structure is required
            decay_length (float or None): if set, it overrides the instance
                variable self.decay_length.

        Returns ([float]): features, see class doc for more info
        """
        decay_length = decay_length or self.decay_length
        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')

        orbscores = get_cbm_vbm_scores(dos,
                                       decay_length,
                                       self.sampling_resolution,
                                       self.gaussian_smear)
        feat = OrderedDict()
        for ex in ['cbm', 'vbm']:
            for orbital in ['s', 'p', 'd', 'f']:
                feat['{}_{}'.format(ex, orbital)] = 0.0
                for specie in self.species:
                    feat['{}_{}_{}'.format(ex, specie, orbital)] = 0.0
            for hybrid in ['sp', 'sd', 'sf', 'pd', 'pf', 'df']:
                feat['{}_{}'.format(ex, hybrid)] = 0.0

        for contrib in orbscores:
            character = contrib['character']
            feat['cbm_{}'.format(character)] += contrib['cbm_score']
            feat['vbm_{}'.format(character)] += contrib['vbm_score']
            for specie in self.species:
                if contrib['specie'] == specie:
                    feat['cbm_{}_{}'.format(specie, character)] += contrib[
                        'cbm_score']
                    feat['vbm_{}_{}'.format(specie, character)] += contrib[
                        'vbm_score']

        for ex in ['cbm', 'vbm']:
            for hybrid in ['sp', 'sd', 'sf', 'pd', 'pf', 'df']:
                orb1 = feat['{}_{}'.format(ex, hybrid[0])]
                orb2 = feat['{}_{}'.format(ex, hybrid[1])]
                feat['{}_{}'.format(ex, hybrid)] = (orb1 * orb2) * 4.0  # 4x so max=1.0
        return list(feat.values())
Beispiel #20
0
    def featurize(self, dos, energy_cutoff=None):
        """
        takes in the density of state and return the orbitals contributions
        and hybridizations.

        Args:
            dos (pymatgen CompleteDos): note that dos.structure is required
            energy_cutoff (float or None): if set, it overrides the instance
                variable self.energy_cutoff.

        Returns ([float]): features, see class doc for more info
        """
        energy_cutoff = energy_cutoff or self.energy_cutoff
        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')

        orbscores = get_cbm_vbm_scores(dos, energy_cutoff,
                                       self.sampling_resolution,
                                       self.gaussian_smear)
        feat = OrderedDict()
        for ex in ['cbm', 'vbm']:
            for orbital in ['s', 'p', 'd', 'f']:
                feat['{}_{}'.format(ex, orbital)] = 0.0
                for specie in self.species:
                    feat['{}_{}_{}'.format(ex, specie, orbital)] = 0.0
            for hybrid in ['sp', 'sd', 'sf', 'pd', 'pf', 'df']:
                feat['{}_{}'.format(ex, hybrid)] = 0.0

        for contrib in orbscores:
            character = contrib['character']
            feat['cbm_{}'.format(character)] += contrib['cbm_score']
            feat['vbm_{}'.format(character)] += contrib['vbm_score']
            for specie in self.species:
                if contrib['specie'] == specie:
                    feat['cbm_{}_{}'.format(specie,
                                            character)] += contrib['cbm_score']
                    feat['vbm_{}_{}'.format(specie,
                                            character)] += contrib['vbm_score']

        for ex in ['cbm', 'vbm']:
            for hybrid in ['sp', 'sd', 'sf', 'pd', 'pf', 'df']:
                orb1 = feat['{}_{}'.format(ex, hybrid[0])]
                orb2 = feat['{}_{}'.format(ex, hybrid[1])]
                feat['{}_{}'.format(
                    ex, hybrid)] = (orb1 * orb2) * 4.0  # 4x so max=1.0
        return list(feat.values())
Beispiel #21
0
    def test_to_from_dict(self):
        d = self.dos.as_dict()
        dos = CompleteDos.from_dict(d)
        el_dos = dos.get_element_dos()
        self.assertEqual(len(el_dos), 4)
        spd_dos = dos.get_spd_dos()
        sum_spd = spd_dos[OrbitalType.s] + spd_dos[OrbitalType.p] + spd_dos[OrbitalType.d]
        sum_element = None
        for pdos in el_dos.values():
            if sum_element is None:
                sum_element = pdos
            else:
                sum_element += pdos

        # The sums of the SPD or the element doses should be the same.
        self.assertTrue((abs(sum_spd.energies - sum_element.energies) < 0.0001).all())
Beispiel #22
0
    def test_to_from_dict(self):
        d = self.dos.as_dict()
        dos = CompleteDos.from_dict(d)
        el_dos = dos.get_element_dos()
        self.assertEqual(len(el_dos), 4)
        spd_dos = dos.get_spd_dos()
        sum_spd = spd_dos[OrbitalType.s] + spd_dos[OrbitalType.p] + spd_dos[OrbitalType.d]
        sum_element = None
        for pdos in el_dos.values():
            if sum_element is None:
                sum_element = pdos
            else:
                sum_element += pdos

        # The sums of the SPD or the element doses should be the same.
        self.assertTrue((abs(sum_spd.energies - sum_element.energies) < 0.0001).all())
Beispiel #23
0
    def process_item(self, mat):
        """
        Process the tasks and materials into just a list of materials

        Args:
            mat (dict): material document

        Returns:
            (dict): electronic_structure document
        """
        d = {self.electronic_structure.key: mat[self.materials.key]}
        self.logger.info("Processing: {}".format(mat[self.materials.key]))

        bs = build_bs(mat["bandstructure"]["bs"], mat)
        dos = CompleteDos.from_dict(mat["bandstructure"]["dos"])

        if bs and dos:
            try:
                pdos = get_pdos(dos)
                dos_plotter = SDOSPlotter(dos, pdos)
                bs_plotter = SBSPlotter(bs)
                plt = bs_plotter.get_plot(dos_plotter=dos_plotter,
                                          **self.plot_options)
                d["plot"] = image_from_plot(plt)
                plt.close()
            except Exception:
                traceback.print_exc()
                self.logger.warning(
                    "Caught error in bandstructure plotting for {}: {}".format(
                        mat[self.materials.key], traceback.format_exc()))

        # Reduced Band structure plot
        try:
            gap = bs.get_band_gap()["energy"]
            plot_data = bs_plotter.bs_plot_data()
            d["bs_plot_small"] = get_small_plot(plot_data, gap)
        except Exception:
            self.logger.warning(
                "Caught error in generating reduced bandstructure plot for {}: {}"
                .format(mat[self.materials.key], traceback.format_exc()))

        # Store task_ids
        for k in ["bs_task", "dos_task", "uniform_task"]:
            if k in mat["bandstructure"]:
                d[k] = mat["bandstructure"][k]

        return d
Beispiel #24
0
    def _get_bs_dos(data):

        data = data or {}

        # this component can be loaded either from mpid or
        # directly from BandStructureSymmLine or CompleteDos objects
        # if mpid is supplied, this is preferred

        mpid = data.get("mpid")
        bandstructure_symm_line = data.get("bandstructure_symm_line")
        density_of_states = data.get("density_of_states")

        if not mpid and (bandstructure_symm_line is None
                         or density_of_states is None):
            return None, None

        if mpid:

            with MPRester() as mpr:

                try:
                    bandstructure_symm_line = mpr.get_bandstructure_by_material_id(
                        mpid)
                except Exception as exc:
                    print(exc)
                    bandstructure_symm_line = None

                try:
                    density_of_states = mpr.get_dos_by_material_id(mpid)
                except Exception as exc:
                    print(exc)
                    density_of_states = None

        else:

            if bandstructure_symm_line and isinstance(bandstructure_symm_line,
                                                      dict):
                bandstructure_symm_line = BandStructureSymmLine.from_dict(
                    bandstructure_symm_line)

            if density_of_states and isinstance(density_of_states, dict):
                density_of_states = CompleteDos.from_dict(density_of_states)

        return bandstructure_symm_line, density_of_states
Beispiel #25
0
    def featurize(self, dos):
        """
        Args:
            dos (pymatgen CompleteDos or their dict):
                The density of states to featurize. Must be a complete DOS,
                (i.e. contains PDOS and structure, in addition to total DOS)
                and must contain the structure.
        """
        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')

        orbscores = get_cbm_vbm_scores(dos, self.energy_cutoff,
                                       self.sampling_resolution,
                                       self.gaussian_smear)

        feat = OrderedDict()
        for ex in ['cbm', 'vbm']:
            orbscores.sort(key=lambda x: x['{}_score'.format(ex)],
                           reverse=True)
            scores = np.array([s['{}_score'.format(ex)] for s in orbscores])
            feat['{}_nsignificant'.format(ex)] = len(
                scores[scores > self.significance_threshold])

            i = 0
            while i < self.contributors:
                sd = orbscores[i]
                if i < len(orbscores):
                    for p in ['character', 'specie']:
                        feat['{}_{}_{}'.format(ex, p, i + 1)] = sd[p]
                    feat['{}_location_{}'.format(ex,
                                                 i + 1)] = '{};{};{}'.format(
                                                     sd['location'][0],
                                                     sd['location'][1],
                                                     sd['location'][2])
                    feat['{}_score_{}'.format(ex, i + 1)] = float(
                        sd['{}_score'.format(ex)])
                else:
                    for p in ['character', 'specie', 'location', 'score']:
                        feat['{}_{}_{}'.format(ex, p, i + 1)] = float('NaN')
                i += 1

        return list(feat.values())
    def setUp(self):
        self.vbm_val = 2.6682
        self.gap = 1.5
        self.entries = list(
            loadfn(
                os.path.join(os.path.dirname(__file__),
                             "GaAs_test_defentries.json")).values())
        for entry in self.entries:
            entry.parameters.update({'vbm': self.vbm_val})
        self.pd = DefectPhaseDiagram(self.entries, self.vbm_val, self.gap)
        self.mu_elts = {
            Element("As"): -4.658070555,
            Element("Ga"): -3.7317319750000006
        }

        # make Vac_As (q= -2) only defect test single-stable-charge exceptions
        self.extra_entry = DefectEntry(self.entries[5].defect.copy(), 100.)
        sep_entries = [
            ent for ent in self.entries if not (
                ent.name == 'Vac_As_mult4' and ent.charge in [-2, -1, 0, 1, 2])
        ]
        sep_entries.append(self.extra_entry.copy())
        self.sep_pd = DefectPhaseDiagram(sep_entries, self.vbm_val, self.gap)

        # make Vac_As (q= -2) is incompatible for larger supercell
        ls_entries = self.entries[:]
        for entry in ls_entries:
            if entry.name == 'Vac_As_mult4' and entry.charge == -2.:
                entry.parameters['is_compatible'] = False
        self.pd_ls_fcTrue = DefectPhaseDiagram(ls_entries,
                                               self.vbm_val,
                                               self.gap,
                                               filter_compatible=True)
        self.pd_ls_fcFalse = DefectPhaseDiagram(ls_entries,
                                                self.vbm_val,
                                                self.gap,
                                                filter_compatible=False)

        # load complete dos for fermi energy solving
        with open(os.path.join(test_dir, "complete_dos.json"), "r") as f:
            dos_dict = json.load(f)
        self.dos = CompleteDos.from_dict(dos_dict)
Beispiel #27
0
    def featurize(self, dos):
        """
        Args:
            dos (pymatgen CompleteDos or their dict):
                The density of states to featurize. Must be a complete DOS,
                (i.e. contains PDOS and structure, in addition to total DOS)
                and must contain the structure.
        """
        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')

        orbscores = get_cbm_vbm_scores(dos, self.decay_length,
                                       self.sampling_resolution,
                                       self.gaussian_smear)

        feat = OrderedDict()
        for ex in ['cbm', 'vbm']:
            orbscores.sort(key=lambda x: x['{}_score'.format(ex)],
                           reverse=True)
            scores = np.array([s['{}_score'.format(ex)] for s in orbscores])
            feat['{}_hybridization'.format(ex)] = - np.sum(
                scores * np.log(scores + 1e-10))  # avoid log(0)

            i = 0
            while i < self.contributors:
                sd = orbscores[i]
                if i < len(orbscores):
                    for p in ['character', 'specie']:
                        feat['{}_{}_{}'.format(ex, p, i + 1)] = sd[p]
                    feat['{}_location_{}'.format(ex, i + 1)] =\
                        '{};{};{}'.format(sd['location'][0], sd['location'][1],
                                          sd['location'][2])
                    feat['{}_score_{}'.format(ex, i + 1)] =\
                        float(sd['{}_score'.format(ex)])
                else:
                    for p in ['character', 'specie', 'location', 'score']:
                        feat['{}_{}_{}'.format(ex, p, i + 1)] = float('NaN')
                i += 1

        return list(feat.values())
Beispiel #28
0
    def featurize(self, dos, idx):
        """
        get dos scores for given site index

        Args:
            dos (pymatgen CompleteDos or their dict):
                dos to featurize, must contain pdos and structure
            idx (int): index of target site in structure.
        """
        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')

        orbscores = get_site_dos_scores(dos, idx, self.decay_length,
                                        self.sampling_resolution,
                                        self.gaussian_smear)

        features = []
        for edge in ['cbm', 'vbm']:
            for score in ['s', 'p', 'd', 'f', 'total']:
                features.append(orbscores[edge][score])
        return features
Beispiel #29
0
    def featurize(self, dos, idx):
        """
        get dos scores for given site index

        Args:
            dos (pymatgen CompleteDos or their dict):
                dos to featurize, must contain pdos and structure
            idx (int): index of target site in structure.
        """
        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)
        if dos.structure is None:
            raise ValueError('The input dos must contain the structure.')

        orbscores = get_site_dos_scores(dos, idx, self.fermi_temperature,
                                        self.sampling_resolution,
                                        self.gaussian_smear)

        features = []
        for edge in ['cbm', 'vbm']:
            for score in ['s', 'p', 'd', 'f', 'total']:
                features.append(orbscores[edge][score])
        return features
Beispiel #30
0
 def setUp(self):
     with open(os.path.join(test_dir, "complete_dos.json"), "r") as f:
         self.dos = CompleteDos.from_dict(json.load(f))
    def process_item(self, mat):
        """
        Process the tasks and materials into just a list of materials

        Args:
            mat (dict): material document

        Returns:
            (dict): electronic_structure document
        """

        self.logger.info("Processing: {}".format(mat[self.materials.key]))

        d = {self.electronic_structure.key: mat[
            self.materials.key], "bandstructure": {}}
        bs = None
        dos = None
        interpolated_dos = None

        # Process the bandstructure for information
        if "bs" in mat["bandstructure"]:
            if "structure" not in mat["bandstructure"]["bs"]:
                mat["bandstructure"]["bs"]["structure"] = mat["structure"]
            if len(mat["bandstructure"]["bs"].get("labels_dict", {})) == 0:
                struc = Structure.from_dict(mat["structure"])
                kpath = HighSymmKpath(struc)._kpath["kpoints"]
                mat["bandstructure"]["bs"]["labels_dict"] = kpath
            # Somethign is wrong with the as_dict / from_dict encoding in the two band structure objects so have to use this hodge podge serialization
            # TODO: Fix bandstructure objects in pymatgen
            bs = BandStructureSymmLine.from_dict(
                BandStructure.from_dict(mat["bandstructure"]["bs"]).as_dict())
            d["bandstructure"]["band_gap"] = {"band_gap": bs.get_band_gap()["energy"],
                                              "direct_gap": bs.get_direct_band_gap(),
                                              "is_direct": bs.get_band_gap()["direct"],
                                              "transition": bs.get_band_gap()["transition"]}

            if self.small_plot:
                d["bandstructure"]["plot_small"] = get_small_plot(bs)

        if "dos" in mat["bandstructure"]:
            dos = CompleteDos.from_dict(mat["bandstructure"]["dos"])

        if self.interpolate_dos and "uniform_bs" in mat["bandstructure"]:
            try:
                interpolated_dos = self.get_interpolated_dos(mat)
            except Exception:
                self.logger.warning("Boltztrap interpolation failed for {}. Continuing with regular DOS".format(mat[self.materials.key]))

        # Generate static images
        if self.static_images:
            try:
                ylim = None
                if bs:
                    plotter = WebBSPlotter(bs)
                    fig = plotter.get_plot()
                    ylim = fig.ylim()  # Used by DOS plot
                    fig.close()

                    d["bandstructure"]["bs_plot"] = image_from_plotter(plotter)

                if dos:
                    plotter = WebDosVertPlotter()
                    plotter.add_dos_dict(dos.get_element_dos())

                    if interpolated_dos:
                        plotter.add_dos("Total DOS", interpolated_dos)
                        d["bandstructure"]["dos_plot"] = image_from_plotter(plotter, ylim=ylim)

                    d["bandstructure"]["dos_plot"] = image_from_plotter(plotter, ylim=ylim)

            except Exception:
                self.logger.warning(
                    "Caught error in electronic structure plotting for {}: {}".format(mat[self.materials.key], traceback.format_exc()))
                return None

        return d
Beispiel #32
0
    def process_item(self, mat):
        """
        Process the tasks and materials into just a list of materials

        Args:
            mat (dict): material document

        Returns:
            (dict): electronic_structure document
        """
        d = {self.electronic_structure.key: mat[self.materials.key]}
        self.logger.info("Processing: {}".format(mat[self.materials.key]))

        bs = BandStructureSymmLine.from_dict(mat["bandstructure"]["bs"])
        dos = CompleteDos.from_dict(mat["bandstructure"]["dos"])

        # Plot Band structure
        if bs:
            try:
                plotter = WebBSPlotter(bs)
                plot = plotter.get_plot()
                ylim = plot.ylim()
                d["bs_plot"] = image_from_plot(plot)
                plot.close()
            except Exception:
                self.logger.warning(
                    "Caught error in bandstructure plotting for {}: {}".format(
                        mat[self.materials.key], traceback.format_exc()))

        # Reduced Band structure plot
        try:
            gap = bs.get_band_gap()["energy"]
            plot_data = plotter.bs_plot_data()
            d["bs_plot_small"] = get_small_plot(plot_data, gap)
        except Exception:
            self.logger.warning(
                "Caught error in generating reduced bandstructure plot for {}: {}"
                .format(mat[self.materials.key], traceback.format_exc()))

        # Plot DOS
        if dos:
            try:
                plotter = WebDosVertPlotter()
                plotter.add_dos_dict(dos.get_element_dos())
                plot = plotter.get_plot(ylim=ylim)
                d["dos_plot"] = image_from_plot(plot)
                plot.close()
            except Exception:
                self.logger.warning(
                    "Caught error in dos plotting for {}: {}".format(
                        mat[self.materials.key], traceback.format_exc()))

        # Get basic bandgap properties
        if bs:
            try:
                d["band_gap"] = {
                    "band_gap": bs.get_band_gap()["energy"],
                    "direct_gap": bs.get_direct_band_gap(),
                    "is_direct": bs.get_band_gap()["direct"],
                    "transition": bs.get_band_gap()["transition"]
                }
            except Exception:
                self.logger.warning(
                    "Caught error in calculating bandgap {}: {}".format(
                        mat[self.materials.key], traceback.format_exc()))

        return d
Beispiel #33
0
 def setUp(self):
     with open(
             os.path.join(PymatgenTest.TEST_FILES_DIR,
                          "complete_dos.json")) as f:
         self.dos = CompleteDos.from_dict(json.load(f))
     self.dos = FermiDos(self.dos)
Beispiel #34
0
        def bs_dos_data(
            mpid,
            path_convention,
            dos_select,
            label_select,
            bandstructure_symm_line,
            density_of_states,
        ):
            if not mpid and (bandstructure_symm_line is None
                             or density_of_states is None):
                raise PreventUpdate

            elif bandstructure_symm_line is None or density_of_states is None:
                if label_select == "":
                    raise PreventUpdate

                # --
                # -- BS and DOS from API or DB
                # --

                bs_data = {"ticks": {}}

                bs_store = GridFSStore(
                    database="fw_bs_prod",
                    collection_name="bandstructure_fs",
                    host="mongodb03.nersc.gov",
                    port=27017,
                    username="******",
                    password="",
                )

                dos_store = GridFSStore(
                    database="fw_bs_prod",
                    collection_name="dos_fs",
                    host="mongodb03.nersc.gov",
                    port=27017,
                    username="******",
                    password="",
                )

                es_store = MongoStore(
                    database="fw_bs_prod",
                    collection_name="electronic_structure",
                    host="mongodb03.nersc.gov",
                    port=27017,
                    username="******",
                    password="",
                    key="task_id",
                )

                # - BS traces from DB using task_id
                es_store.connect()
                bs_query = es_store.query_one(
                    criteria={"task_id": int(mpid)},
                    properties=[
                        "bandstructure.{}.task_id".format(path_convention),
                        "bandstructure.{}.total.equiv_labels".format(
                            path_convention),
                    ],
                )

                es_store.close()

                bs_store.connect()
                bandstructure_symm_line = bs_store.query_one(criteria={
                    "metadata.task_id":
                    int(bs_query["bandstructure"][path_convention]["task_id"])
                }, )

                # If LM convention, get equivalent labels
                if path_convention != label_select:
                    bs_equiv_labels = bs_query["bandstructure"][
                        path_convention]["total"]["equiv_labels"]

                    new_labels_dict = {}
                    for label in bandstructure_symm_line["labels_dict"].keys():

                        label_formatted = label.replace("$", "")

                        if "|" in label_formatted:
                            f_label = label_formatted.split("|")
                            new_labels.append(
                                "$" +
                                bs_equiv_labels[label_select][f_label[0]] +
                                "|" +
                                bs_equiv_labels[label_select][f_label[1]] +
                                "$")
                        else:
                            new_labels_dict["$" + bs_equiv_labels[label_select]
                                            [label_formatted] +
                                            "$"] = bandstructure_symm_line[
                                                "labels_dict"][label]

                    bandstructure_symm_line["labels_dict"] = new_labels_dict

                # - DOS traces from DB using task_id
                es_store.connect()
                dos_query = es_store.query_one(
                    criteria={"task_id": int(mpid)},
                    properties=["dos.task_id"],
                )
                es_store.close()

                dos_store.connect()
                density_of_states = dos_store.query_one(
                    criteria={"task_id": int(dos_query["dos"]["task_id"])}, )

            # - BS Data
            if (type(bandstructure_symm_line) != dict
                    and bandstructure_symm_line is not None):
                bandstructure_symm_line = bandstructure_symm_line.to_dict()

            if type(density_of_states
                    ) != dict and density_of_states is not None:
                density_of_states = density_of_states.to_dict()

            bsml = BSML.from_dict(bandstructure_symm_line)

            bs_reg_plot = BSPlotter(bsml)

            bs_data = bs_reg_plot.bs_plot_data()

            # Make plot continous for lm
            if path_convention == "lm":
                distance_map, kpath_euler = HSKP(
                    bsml.structure).get_continuous_path(bsml)

                kpath_labels = [pair[0] for pair in kpath_euler]
                kpath_labels.append(kpath_euler[-1][1])

            else:
                distance_map = [(i, False)
                                for i in range(len(bs_data["distances"]))]
                kpath_labels = []
                for label_ind in range(len(bs_data["ticks"]["label"]) - 1):
                    if (bs_data["ticks"]["label"][label_ind] !=
                            bs_data["ticks"]["label"][label_ind + 1]):
                        kpath_labels.append(
                            bs_data["ticks"]["label"][label_ind])
                kpath_labels.append(bs_data["ticks"]["label"][-1])

            bs_data["ticks"]["label"] = kpath_labels

            # Obtain bands to plot over and generate traces for bs data:
            energy_window = (-6.0, 10.0)
            bands = []
            for band_num in range(bs_reg_plot._nb_bands):
                if (bs_data["energy"][0][str(Spin.up)][band_num][0] <=
                        energy_window[1]) and (bs_data["energy"][0][str(
                            Spin.up)][band_num][0] >= energy_window[0]):
                    bands.append(band_num)

            bstraces = []

            pmin = 0.0
            tick_vals = [0.0]

            cbm = bsml.get_cbm()
            vbm = bsml.get_vbm()

            cbm_new = bs_data["cbm"]
            vbm_new = bs_data["vbm"]

            for dnum, (d, rev) in enumerate(distance_map):

                x_dat = [
                    dval - bs_data["distances"][d][0] + pmin
                    for dval in bs_data["distances"][d]
                ]

                pmin = x_dat[-1]

                tick_vals.append(pmin)

                if not rev:
                    traces_for_segment = [{
                        "x":
                        x_dat,
                        "y": [
                            bs_data["energy"][d][str(Spin.up)][i][j]
                            for j in range(len(bs_data["distances"][d]))
                        ],
                        "mode":
                        "lines",
                        "line": {
                            "color": "#1f77b4"
                        },
                        "hoverinfo":
                        "skip",
                        "name":
                        "spin ↑"
                        if bs_reg_plot._bs.is_spin_polarized else "Total",
                        "hovertemplate":
                        "%{y:.2f} eV",
                        "showlegend":
                        False,
                        "xaxis":
                        "x",
                        "yaxis":
                        "y",
                    } for i in bands]
                elif rev:
                    traces_for_segment = [{
                        "x":
                        x_dat,
                        "y": [
                            bs_data["energy"][d][str(Spin.up)][i][j]
                            for j in reversed(
                                range(len(bs_data["distances"][d])))
                        ],
                        "mode":
                        "lines",
                        "line": {
                            "color": "#1f77b4"
                        },
                        "hoverinfo":
                        "skip",
                        "name":
                        "spin ↑"
                        if bs_reg_plot._bs.is_spin_polarized else "Total",
                        "hovertemplate":
                        "%{y:.2f} eV",
                        "showlegend":
                        False,
                        "xaxis":
                        "x",
                        "yaxis":
                        "y",
                    } for i in bands]

                if bs_reg_plot._bs.is_spin_polarized:

                    if not rev:
                        traces_for_segment += [{
                            "x":
                            x_dat,
                            "y": [
                                bs_data["energy"][d][str(Spin.down)][i][j]
                                for j in range(len(bs_data["distances"][d]))
                            ],
                            "mode":
                            "lines",
                            "line": {
                                "color": "#ff7f0e",
                                "dash": "dot"
                            },
                            "hoverinfo":
                            "skip",
                            "showlegend":
                            False,
                            "name":
                            "spin ↓",
                            "hovertemplate":
                            "%{y:.2f} eV",
                            "xaxis":
                            "x",
                            "yaxis":
                            "y",
                        } for i in bands]
                    elif rev:
                        traces_for_segment += [{
                            "x":
                            x_dat,
                            "y": [
                                bs_data["energy"][d][str(Spin.down)][i][j]
                                for j in reversed(
                                    range(len(bs_data["distances"][d])))
                            ],
                            "mode":
                            "lines",
                            "line": {
                                "color": "#ff7f0e",
                                "dash": "dot"
                            },
                            "hoverinfo":
                            "skip",
                            "showlegend":
                            False,
                            "name":
                            "spin ↓",
                            "hovertemplate":
                            "%{y:.2f} eV",
                            "xaxis":
                            "x",
                            "yaxis":
                            "y",
                        } for i in bands]

                bstraces += traces_for_segment

                # - Get proper cbm and vbm coords for lm
                if path_convention == "lm":
                    for (x_point, y_point) in bs_data["cbm"]:
                        if x_point in bs_data["distances"][d]:
                            xind = bs_data["distances"][d].index(x_point)
                            if not rev:
                                x_point_new = x_dat[xind]
                            else:
                                x_point_new = x_dat[len(x_dat) - xind - 1]

                            new_label = bs_data["ticks"]["label"][
                                tick_vals.index(x_point_new)]

                            if (cbm["kpoint"].label is None
                                    or cbm["kpoint"].label in new_label):
                                cbm_new.append((x_point_new, y_point))

                    for (x_point, y_point) in bs_data["vbm"]:
                        if x_point in bs_data["distances"][d]:
                            xind = bs_data["distances"][d].index(x_point)
                            if not rev:
                                x_point_new = x_dat[xind]
                            else:
                                x_point_new = x_dat[len(x_dat) - xind - 1]

                            new_label = bs_data["ticks"]["label"][
                                tick_vals.index(x_point_new)]

                            if (vbm["kpoint"].label is None
                                    or vbm["kpoint"].label in new_label):
                                vbm_new.append((x_point_new, y_point))

            bs_data["ticks"]["distance"] = tick_vals

            # - Strip latex math wrapping for labels
            str_replace = {
                "$": "",
                "\\mid": "|",
                "\\Gamma": "Γ",
                "\\Sigma": "Σ",
                "GAMMA": "Γ",
                "_1": "₁",
                "_2": "₂",
                "_3": "₃",
                "_4": "₄",
                "_{1}": "₁",
                "_{2}": "₂",
                "_{3}": "₃",
                "_{4}": "₄",
                "^{*}": "*",
            }

            bar_loc = []
            for entry_num in range(len(bs_data["ticks"]["label"])):
                for key in str_replace.keys():
                    if key in bs_data["ticks"]["label"][entry_num]:
                        bs_data["ticks"]["label"][entry_num] = bs_data[
                            "ticks"]["label"][entry_num].replace(
                                key, str_replace[key])
                        if key == "\\mid":
                            bar_loc.append(
                                bs_data["ticks"]["distance"][entry_num])

            # Vertical lines for disjointed segments
            vert_traces = [{
                "x": [x_point, x_point],
                "y": energy_window,
                "mode": "lines",
                "marker": {
                    "color": "white"
                },
                "hoverinfo": "skip",
                "showlegend": False,
                "xaxis": "x",
                "yaxis": "y",
            } for x_point in bar_loc]

            bstraces += vert_traces

            # Dots for cbm and vbm

            dot_traces = [{
                "x": [x_point],
                "y": [y_point],
                "mode":
                "markers",
                "marker": {
                    "color": "#7E259B",
                    "size": 16,
                    "line": {
                        "color": "white",
                        "width": 2
                    },
                },
                "showlegend":
                False,
                "hoverinfo":
                "text",
                "name":
                "",
                "hovertemplate":
                "CBM: k = {}, {} eV".format(list(cbm["kpoint"].frac_coords),
                                            cbm["energy"]),
                "xaxis":
                "x",
                "yaxis":
                "y",
            } for (x_point, y_point) in set(cbm_new)] + [{
                "x": [x_point],
                "y": [y_point],
                "mode":
                "marker",
                "marker": {
                    "color": "#7E259B",
                    "size": 16,
                    "line": {
                        "color": "white",
                        "width": 2
                    },
                },
                "showlegend":
                False,
                "hoverinfo":
                "text",
                "name":
                "",
                "hovertemplate":
                "VBM: k = {}, {} eV".format(list(vbm["kpoint"].frac_coords),
                                            vbm["energy"]),
                "xaxis":
                "x",
                "yaxis":
                "y",
            } for (x_point, y_point) in set(vbm_new)]

            bstraces += dot_traces

            # - DOS Data
            dostraces = []

            dos = CompleteDos.from_dict(density_of_states)

            dos_max = np.abs(
                (dos.energies - dos.efermi - energy_window[1])).argmin()
            dos_min = np.abs(
                (dos.energies - dos.efermi - energy_window[0])).argmin()

            if bs_reg_plot._bs.is_spin_polarized:
                # Add second spin data if available
                trace_tdos = {
                    "x": -1.0 * dos.densities[Spin.down][dos_min:dos_max],
                    "y": dos.energies[dos_min:dos_max] - dos.efermi,
                    "mode": "lines",
                    "name": "Total DOS (spin ↓)",
                    "line": go.scatter.Line(color="#444444", dash="dot"),
                    "fill": "tozerox",
                    "fillcolor": "#C4C4C4",
                    "xaxis": "x2",
                    "yaxis": "y2",
                }

                dostraces.append(trace_tdos)

                tdos_label = "Total DOS (spin ↑)"
            else:
                tdos_label = "Total DOS"

            # Total DOS
            trace_tdos = {
                "x": dos.densities[Spin.up][dos_min:dos_max],
                "y": dos.energies[dos_min:dos_max] - dos.efermi,
                "mode": "lines",
                "name": tdos_label,
                "line": go.scatter.Line(color="#444444"),
                "fill": "tozerox",
                "fillcolor": "#C4C4C4",
                "legendgroup": "spinup",
                "xaxis": "x2",
                "yaxis": "y2",
            }

            dostraces.append(trace_tdos)

            ele_dos = dos.get_element_dos()
            elements = [str(entry) for entry in ele_dos.keys()]

            if dos_select == "ap":
                proj_data = ele_dos
            elif dos_select == "op":
                proj_data = dos.get_spd_dos()
            elif "orb" in dos_select:
                proj_data = dos.get_element_spd_dos(
                    Element(dos_select.replace("orb", "")))
            else:
                raise PreventUpdate

            # Projected DOS
            count = 0
            colors = [
                "#d62728",  # brick red
                "#2ca02c",  # cooked asparagus green
                "#17becf",  # blue-teal
                "#bcbd22",  # curry yellow-green
                "#9467bd",  # muted purple
                "#8c564b",  # chestnut brown
                "#e377c2",  # raspberry yogurt pink
            ]

            for label in proj_data.keys():

                if bs_reg_plot._bs.is_spin_polarized:
                    trace = {
                        "x":
                        -1.0 *
                        proj_data[label].densities[Spin.down][dos_min:dos_max],
                        "y":
                        dos.energies[dos_min:dos_max] - dos.efermi,
                        "mode":
                        "lines",
                        "name":
                        str(label) + " (spin ↓)",
                        "line":
                        dict(width=3, color=colors[count], dash="dot"),
                        "xaxis":
                        "x2",
                        "yaxis":
                        "y2",
                    }

                    dostraces.append(trace)
                    spin_up_label = str(label) + " (spin ↑)"

                else:
                    spin_up_label = str(label)

                trace = {
                    "x": proj_data[label].densities[Spin.up][dos_min:dos_max],
                    "y": dos.energies[dos_min:dos_max] - dos.efermi,
                    "mode": "lines",
                    "name": spin_up_label,
                    "line": dict(width=2, color=colors[count]),
                    "xaxis": "x2",
                    "yaxis": "y2",
                }

                dostraces.append(trace)

                count += 1
            traces = [bstraces, dostraces, bs_data]

            return (traces, elements)
Beispiel #35
0
        def bs_dos_traces(bandStructureSymmLine, densityOfStates):

            if bandStructureSymmLine == "error" or densityOfStates == "error":
                return "error"

            if bandStructureSymmLine == None or densityOfStates == None:
                raise PreventUpdate

            # - BS Data
            bstraces = []

            bs_reg_plot = BSPlotter(BSML.from_dict(bandStructureSymmLine))

            bs_data = bs_reg_plot.bs_plot_data()

            # -- Strip latex math wrapping
            str_replace = {
                "$": "",
                "\\mid": "|",
                "\\Gamma": "Γ",
                "\\Sigma": "Σ",
                "_1": "₁",
                "_2": "₂",
                "_3": "₃",
                "_4": "₄",
            }

            for entry_num in range(len(bs_data["ticks"]["label"])):
                for key in str_replace.keys():
                    if key in bs_data["ticks"]["label"][entry_num]:
                        bs_data["ticks"]["label"][entry_num] = bs_data[
                            "ticks"]["label"][entry_num].replace(
                                key, str_replace[key])

            for d in range(len(bs_data["distances"])):
                for i in range(bs_reg_plot._nb_bands):
                    bstraces.append(
                        go.Scatter(
                            x=bs_data["distances"][d],
                            y=[
                                bs_data["energy"][d][str(Spin.up)][i][j]
                                for j in range(len(bs_data["distances"][d]))
                            ],
                            mode="lines",
                            line=dict(color=("#666666"), width=2),
                            hoverinfo="skip",
                            showlegend=False,
                        ))

                    if bs_reg_plot._bs.is_spin_polarized:
                        bstraces.append(
                            go.Scatter(
                                x=bs_data["distances"][d],
                                y=[
                                    bs_data["energy"][d][str(Spin.down)][i][j]
                                    for j in range(len(bs_data["distances"]
                                                       [d]))
                                ],
                                mode="lines",
                                line=dict(color=("#666666"),
                                          width=2,
                                          dash="dash"),
                                hoverinfo="skip",
                                showlegend=False,
                            ))

            # -- DOS Data
            dostraces = []

            dos = CompleteDos.from_dict(densityOfStates)

            if Spin.down in dos.densities:
                # Add second spin data if available
                trace_tdos = go.Scatter(
                    x=dos.densities[Spin.down],
                    y=dos.energies - dos.efermi,
                    mode="lines",
                    name="Total DOS (spin ↓)",
                    line=go.scatter.Line(color="#444444", dash="dash"),
                    fill="tozeroy",
                )

                dostraces.append(trace_tdos)

                tdos_label = "Total DOS (spin ↑)"
            else:
                tdos_label = "Total DOS"

            # Total DOS
            trace_tdos = go.Scatter(
                x=dos.densities[Spin.up],
                y=dos.energies - dos.efermi,
                mode="lines",
                name=tdos_label,
                line=go.scatter.Line(color="#444444"),
                fill="tozeroy",
                legendgroup="spinup",
            )

            dostraces.append(trace_tdos)

            p_ele_dos = dos.get_element_dos()

            # Projected DOS
            count = 0
            colors = [
                "#1f77b4",  # muted blue
                "#ff7f0e",  # safety orange
                "#2ca02c",  # cooked asparagus green
                "#d62728",  # brick red
                "#9467bd",  # muted purple
                "#8c564b",  # chestnut brown
                "#e377c2",  # raspberry yogurt pink
                "#bcbd22",  # curry yellow-green
                "#17becf",  # blue-teal
            ]

            for ele in p_ele_dos.keys():

                if bs_reg_plot._bs.is_spin_polarized:
                    trace = go.Scatter(
                        x=p_ele_dos[ele].densities[Spin.down],
                        y=dos.energies - dos.efermi,
                        mode="lines",
                        name=ele.symbol + " (spin ↓)",
                        line=dict(width=3, color=colors[count], dash="dash"),
                    )

                    dostraces.append(trace)
                    spin_up_label = ele.symbol + " (spin ↑)"

                else:
                    spin_up_label = ele.symbol

                trace = go.Scatter(
                    x=p_ele_dos[ele].densities[Spin.up],
                    y=dos.energies - dos.efermi,
                    mode="lines",
                    name=spin_up_label,
                    line=dict(width=3, color=colors[count]),
                )

                dostraces.append(trace)

                count += 1

            traces = [bstraces, dostraces, bs_data]

            return traces
        el: chempots_delta[el] + c.chempots_reference[el]
        for el in chempots_delta
    }

    pressures.append(np.around(partial_pressure, decimals=4))
    reservoirs.append(chempots_abs)

print('Reading defect calculation data...\n')
with open('vacancies_NN_HSE.json') as json_file:
    data = json.load(json_file)
defects_analysis = DefectsAnalysis.from_dict(data)

print('Reading DOS...\n')
with open('dos_NN_HSE.json') as json_file:
    data = json.load(json_file)
bulk_dos = CompleteDos.from_dict(data)

concentrations = []
for r in reservoirs:

    fermi_energy = defects_analysis.equilibrium_fermi_level(r, bulk_dos)
    cn = defects_analysis.defect_concentrations_stable_charges(
        r, temperature=temperature, fermi_level=fermi_energy)
    concentrations.append(cn)

data = {
    'partial_pressures': pressures,
    'defect_concentrations': concentrations
}

with open('defect_concentrations_NN_HSE.json', 'w') as json_file:
Beispiel #37
0
        def bs_dos_data(
            mpid,
            path_convention,
            dos_select,
            label_select,
            bandstructure_symm_line,
            density_of_states,
        ):
            if (not mpid
                    or "mpid" not in mpid) and (bandstructure_symm_line is None
                                                or density_of_states is None):
                raise PreventUpdate
            elif mpid:
                raise PreventUpdate
            elif bandstructure_symm_line is None or density_of_states is None:

                # --
                # -- BS and DOS from API
                # --

                mpid = mpid["mpid"]
                bs_data = {"ticks": {}}

                # client = MongoClient(
                #     "mongodb03.nersc.gov", username="******", password="", authSource="fw_bs_prod",
                # )

                db = client.fw_bs_prod

                # - BS traces from DB using task_id
                bs_query = list(
                    db.electronic_structure.find(
                        {"task_id": int(mpid)},
                        [
                            "bandstructure.{}.total.traces".format(
                                path_convention)
                        ],
                    ))[0]

                is_sp = (len(bs_query["bandstructure"][path_convention]
                             ["total"]["traces"]) == 2)

                if is_sp:
                    bstraces = (bs_query["bandstructure"][path_convention]
                                ["total"]["traces"]["1"] +
                                bs_query["bandstructure"][path_convention]
                                ["total"]["traces"]["-1"])
                else:
                    bstraces = bs_query["bandstructure"][path_convention][
                        "total"]["traces"]["1"]

                bs_data["ticks"]["distance"] = bs_query["bandstructure"][
                    path_convention]["total"]["traces"]["ticks"]
                bs_data["ticks"]["label"] = bs_query["bandstructure"][
                    path_convention]["total"]["traces"]["labels"]

                # If LM convention, get equivalent labels
                if path_convention == "lm" and label_select != "lm":
                    bs_equiv_labels = bs_query["bandstructure"][
                        path_convention]["total"]["traces"]["equiv_labels"]

                    alt_choice = label_select

                    if label_select == "hin":
                        alt_choice = "h"

                    new_labels = []
                    for label in bs_data["ticks"]["label"]:
                        label_formatted = label.replace("$", "")

                        if "|" in label_formatted:
                            f_label = label_formatted.split("|")
                            new_labels.append(
                                "$" + bs_equiv_labels[alt_choice][f_label[0]] +
                                "|" + bs_equiv_labels[alt_choice][f_label[1]] +
                                "$")
                        else:
                            new_labels.append(
                                "$" +
                                bs_equiv_labels[alt_choice][label_formatted] +
                                "$")

                    bs_data["ticks"]["label"] = new_labels

                # Strip latex math wrapping
                str_replace = {
                    "$": "",
                    "\\mid": "|",
                    "\\Gamma": "Γ",
                    "\\Sigma": "Σ",
                    "GAMMA": "Γ",
                    "_1": "₁",
                    "_2": "₂",
                    "_3": "₃",
                    "_4": "₄",
                }

                for entry_num in range(len(bs_data["ticks"]["label"])):
                    for key in str_replace.keys():
                        if key in bs_data["ticks"]["label"][entry_num]:
                            bs_data["ticks"]["label"][entry_num] = bs_data[
                                "ticks"]["label"][entry_num].replace(
                                    key, str_replace[key])

                # - DOS traces from DB using task_id
                dostraces = []

                dos_tot_ele_traces = list(
                    db.electronic_structure.find(
                        {"task_id": int(mpid)},
                        ["dos.total.traces", "dos.elements"]))[0]

                dostraces = [
                    dos_tot_ele_traces["dos"]["total"]["traces"][spin] for spin
                    in dos_tot_ele_traces["dos"]["total"]["traces"].keys()
                ]

                elements = [
                    ele
                    for ele in dos_tot_ele_traces["dos"]["elements"].keys()
                ]

                if dos_select == "ap":
                    for ele_label in elements:
                        dostraces += [
                            dos_tot_ele_traces["dos"]["elements"][ele_label]
                            ["total"]["traces"][spin]
                            for spin in dos_tot_ele_traces["dos"]["elements"]
                            [ele_label]["total"]["traces"].keys()
                        ]

                elif dos_select == "op":
                    orb_tot_traces = list(
                        db.electronic_structure.find({"task_id": int(mpid)},
                                                     ["dos.orbitals"]))[0]
                    for orbital in ["s", "p", "d"]:
                        dostraces += [
                            orb_tot_traces["dos"]["orbitals"][orbital]
                            ["traces"][spin] for spin in orb_tot_traces["dos"]
                            ["orbitals"]["s"]["traces"].keys()
                        ]

                elif "orb" in dos_select:
                    ele_label = dos_select.replace("orb", "")

                    for orbital in ["s", "p", "d"]:
                        dostraces += [
                            dos_tot_ele_traces["dos"]["elements"][ele_label]
                            [orbital]["traces"][spin]
                            for spin in dos_tot_ele_traces["dos"]["elements"]
                            [ele_label][orbital]["traces"].keys()
                        ]

                traces = [bstraces, dostraces, bs_data]

                return (traces, elements)

            else:

                # --
                # -- BS and DOS passed manually
                # --

                # - BS Data

                if type(bandstructure_symm_line) != dict:
                    bandstructure_symm_line = bandstructure_symm_line.to_dict()

                if type(density_of_states) != dict:
                    density_of_states = density_of_states.to_dict()

                bs_reg_plot = BSPlotter(
                    BSML.from_dict(bandstructure_symm_line))
                bs_data = bs_reg_plot.bs_plot_data()

                # - Strip latex math wrapping
                str_replace = {
                    "$": "",
                    "\\mid": "|",
                    "\\Gamma": "Γ",
                    "\\Sigma": "Σ",
                    "GAMMA": "Γ",
                    "_1": "₁",
                    "_2": "₂",
                    "_3": "₃",
                    "_4": "₄",
                }

                for entry_num in range(len(bs_data["ticks"]["label"])):
                    for key in str_replace.keys():
                        if key in bs_data["ticks"]["label"][entry_num]:
                            bs_data["ticks"]["label"][entry_num] = bs_data[
                                "ticks"]["label"][entry_num].replace(
                                    key, str_replace[key])

                # Obtain bands to plot over:
                energy_window = (-6.0, 10.0)
                bands = []
                for band_num in range(bs_reg_plot._nb_bands):
                    if (bs_data["energy"][0][str(Spin.up)][band_num][0] <=
                            energy_window[1]) and (bs_data["energy"][0][str(
                                Spin.up)][band_num][0] >= energy_window[0]):
                        bands.append(band_num)

                bstraces = []

                # Generate traces for total BS data
                for d in range(len(bs_data["distances"])):
                    dist_dat = bs_data["distances"][d]
                    energy_ind = [
                        i for i in range(len(bs_data["distances"][d]))
                    ]

                    traces_for_segment = [{
                        "x":
                        dist_dat,
                        "y":
                        [bs_data["energy"][d]["1"][i][j] for j in energy_ind],
                        "mode":
                        "lines",
                        "line": {
                            "color": "#666666"
                        },
                        "hoverinfo":
                        "skip",
                        "showlegend":
                        False,
                    } for i in bands]

                    if bs_reg_plot._bs.is_spin_polarized:
                        traces_for_segment += [{
                            "x":
                            dist_dat,
                            "y": [
                                bs_data["energy"][d]["-1"][i][j]
                                for j in energy_ind
                            ],
                            "mode":
                            "lines",
                            "line": {
                                "color": "#666666"
                            },
                            "hoverinfo":
                            "skip",
                            "showlegend":
                            False,
                        } for i in bands]

                    bstraces += traces_for_segment

                # - DOS Data
                dostraces = []

                dos = CompleteDos.from_dict(density_of_states)

                dos_max = np.abs(
                    (dos.energies - dos.efermi - energy_window[1])).argmin()
                dos_min = np.abs(
                    (dos.energies - dos.efermi - energy_window[0])).argmin()

                if bs_reg_plot._bs.is_spin_polarized:
                    # Add second spin data if available
                    trace_tdos = go.Scatter(
                        x=dos.densities[Spin.down][dos_min:dos_max],
                        y=dos.energies[dos_min:dos_max] - dos.efermi,
                        mode="lines",
                        name="Total DOS (spin ↓)",
                        line=go.scatter.Line(color="#444444", dash="dash"),
                        fill="tozerox",
                    )

                    dostraces.append(trace_tdos)

                    tdos_label = "Total DOS (spin ↑)"
                else:
                    tdos_label = "Total DOS"

                # Total DOS
                trace_tdos = go.Scatter(
                    x=dos.densities[Spin.up][dos_min:dos_max],
                    y=dos.energies[dos_min:dos_max] - dos.efermi,
                    mode="lines",
                    name=tdos_label,
                    line=go.scatter.Line(color="#444444"),
                    fill="tozerox",
                    legendgroup="spinup",
                )

                dostraces.append(trace_tdos)

                ele_dos = dos.get_element_dos()
                elements = [str(entry) for entry in ele_dos.keys()]

                if dos_select == "ap":
                    proj_data = ele_dos
                elif dos_select == "op":
                    proj_data = dos.get_spd_dos()
                elif "orb" in dos_select:
                    proj_data = dos.get_element_spd_dos(
                        Element(dos_select.replace("orb", "")))
                else:
                    raise PreventUpdate

                # Projected DOS
                count = 0
                colors = [
                    "#1f77b4",  # muted blue
                    "#ff7f0e",  # safety orange
                    "#2ca02c",  # cooked asparagus green
                    "#9467bd",  # muted purple
                    "#e377c2",  # raspberry yogurt pink
                    "#d62728",  # brick red
                    "#8c564b",  # chestnut brown
                    "#bcbd22",  # curry yellow-green
                    "#17becf",  # blue-teal
                ]

                for label in proj_data.keys():

                    if bs_reg_plot._bs.is_spin_polarized:
                        trace = go.Scatter(
                            x=proj_data[label].densities[Spin.down]
                            [dos_min:dos_max],
                            y=dos.energies[dos_min:dos_max] - dos.efermi,
                            mode="lines",
                            name=str(label) + " (spin ↓)",
                            line=dict(width=3,
                                      color=colors[count],
                                      dash="dash"),
                        )

                        dostraces.append(trace)
                        spin_up_label = str(label) + " (spin ↑)"

                    else:
                        spin_up_label = str(label)

                    trace = go.Scatter(
                        x=proj_data[label].densities[Spin.up][dos_min:dos_max],
                        y=dos.energies[dos_min:dos_max] - dos.efermi,
                        mode="lines",
                        name=spin_up_label,
                        line=dict(width=3, color=colors[count]),
                    )

                    dostraces.append(trace)

                    count += 1

                traces = [bstraces, dostraces, bs_data]

                return (traces, elements)
Beispiel #38
0
 def setUp(self):
     with open(os.path.join(test_dir, "complete_dos.json"), "r") as f:
         self.dos = CompleteDos.from_dict(json.load(f))
         self.plotter = DosPlotter(sigma=0.2, stack=True)
Beispiel #39
0
 def setUp(self):
     with open(os.path.join(test_dir, "complete_dos.json"), "r",
               encoding='utf-8') as f:
         self.dos = CompleteDos.from_dict(json.load(f))
         self.plotter = DosPlotter(sigma=0.2, stack=True)
Beispiel #40
0
 def setUp(self):
     with open(os.path.join(test_dir, "complete_dos.json"), "r") as f:
         self.dos = CompleteDos.from_dict(json.load(f))
    def featurize(self, dos):
        """
        Args:
            dos (pymatgen CompleteDos or their dict):
                    The density of states to featurize. Must be a complete DOS,
                    (i.e. contains PDOS and structure, in addition to total DOS)

        Returns:
             ([float | string]): a list of band structure features.
                List of currently supported features:
                .. xbm_percents: [(float)] fractions that orbitals contribute
                .. xbm_locations: [[(float)]] cartesian locations of orbitals
                .. xbm_species: [(str)] elemental specie of orbitals (ex: 'Ti')
                .. xbm_characters: [(str)] orbital characters (s p d or f)
                .. xbm_coordinations: [(str)] the coordination geometry that
                        the orbitals reside in. (the coordination environment
                        of the site the orbital is associated with)
                .. xbm_significant_contributors: (int) the number of orbitals
                        with contributions above the significance_threshold
        """

        if isinstance(dos, dict):
            dos = CompleteDos.from_dict(dos)

        # preparation
        orbital_scores = DOSFeaturizer.get_cbm_vbm_scores(
            dos, self.coordination_features, self.energy_cutoff,
            self.sampling_resolution, self.gaussian_smear)

        orbital_scores.sort(key=lambda x: x['cbm_score'], reverse=True)
        cbm_contributors = orbital_scores[0:self.contributors]
        cbm_sig_cont = [
            orb for orb in orbital_scores
            if orb['cbm_score'] > self.significance_threshold
        ]
        orbital_scores.sort(key=lambda x: x['vbm_score'], reverse=True)
        vbm_contributors = orbital_scores[0:self.contributors]
        vbm_sig_cont = [
            orb for orb in orbital_scores
            if orb['vbm_score'] > self.significance_threshold
        ]

        features = []

        for extremum in ["cbm", "vbm"]:
            for feat in [
                    "{}_score".format(extremum), "location", "specie",
                    "character", "coordination", "significant_contributors"
            ]:

                contributors = locals()["{}_contributors".format(extremum)]

                if feat == "coordination":
                    if self.coordination_features:
                        features.append([
                            contributors[i]['coordination']
                            for i in range(0, self.contributors)
                        ])
                elif feat == "significant_contributors":
                    features.append(
                        len(locals()["{}_sig_cont".format(extremum)]))

                else:
                    features.append([
                        contributors[i][feat]
                        for i in range(0, self.contributors)
                    ])

        return features
Beispiel #42
0
from my_functions.phase_diagram.analysis import ChempotAnalysis, Chempots

with open(
        '/home/lorenzo/data/NN_cubic/E-form-vacancies-PBE/vacancies_NN_cubic_PBE.json'
) as f:
    defects_analysis = DefectsAnalysis.from_dict(json.load(f))

with open(
        '/home/lorenzo/data/NN_cubic/phase-diagram-PBE/computed_phases_NN_cubic.json'
) as f:
    computed_phases = json.load(f)

with open(
        '/home/lorenzo/data/NN_cubic/E-form-vacancies-PBE/dos_NN_cubic_PBE.json'
) as f:
    bulk_dos = CompleteDos.from_dict(json.load(f))

with open(
        '/home/lorenzo/data/NN_cubic/phase-diagram-PBE/chempots/chempots_boundary_NN_cubic_PBE.json'
) as f:
    chempots_boundary_delta = json.load(f)

chempots_boundary_delta = {
    res: Chempots.from_dict(chempots_boundary_delta[res]).chempots
    for res in chempots_boundary_delta
}
chempots_analysis = ChempotAnalysis(computed_phases)
chempots_boundary = {
    res: chempots_analysis.get_chempots_abs(chempots_boundary_delta[res])
    for res in chempots_boundary_delta
}