Skip to content

Commit

Permalink
Merge branch 'materialsproject:master' into ele_band_color
Browse files Browse the repository at this point in the history
  • Loading branch information
cnncnnzh committed Jan 15, 2023
2 parents 00dc5d3 + 00609e5 commit a0811e7
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Pymatgen is free to use. However, we also welcome your help to improve this libr
- Bug reports or feature requests: Please submit a [GitHub Issue](https://github.com/materialsproject/pymatgen/issues).
- Code contributions via [pull requests](https://github.com/materialsproject/pymatgen/pulls) are welcome.
- For help with usage that is unrelated to bugs or feature requests, please use the `pymatgen` [MatSci page](https://discuss.matsci.org/c/pymatgen).
- [`matgenb`](https://matgenb.materialsvirtuallab.org) provides some Jupyter notebooks demonstrating functionality.
- [`matgenb`](https://github.com/materialsvirtuallab/matgenb#introduction) provides some Jupyter notebooks demonstrating functionality.
- Follow us on [Twitter](https://twitter.com/pymatgen) to get news and tips.

## Why use pymatgen?
Expand Down
2 changes: 1 addition & 1 deletion docs_rst/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ reports. The following are resources for pymatgen:
* Code contributions via `pull requests <https://github.com/materialsproject/pymatgen/pulls>`_ welcome.
* For help with usage that are unrelated to bugs or feature requests, please use the pymatgen `Discourse page
<https://discuss.matsci.org/c/pymatgen>`_.
* `matgenb <http://matgenb.materialsvirtuallab.org>`_ provides some Jupyter notebooks demonstrating functionality.
* `matgenb <https://github.com/materialsvirtuallab/matgenb#introduction>`_ provides some Jupyter notebooks demonstrating functionality.
* Follow us on `Twitter <http://twitter.com/pymatgen>`_ to get news and tips.

*The code is mightier than the pen.*
Expand Down
26 changes: 26 additions & 0 deletions pymatgen/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,23 @@ def from_dict(cls, d) -> Composition:
"""
return cls(d)

@classmethod
def from_weight_dict(cls, weight_dict) -> Composition:
"""
Creates a Composition based on a dict of atomic fractions calculated
from a dict of weight fractions. Allows for quick creation of the class
from weight-based notations commonly used in the industry, such as
Ti6V4Al and Ni60Ti40.
Args:
weight_dict (dict): {symbol: weight_fraction} dict.
"""

weight_sum = sum([val / Element(el).atomic_mass for el, val in weight_dict.items()])
comp_dict = {el: val / Element(el).atomic_mass / weight_sum for el, val in weight_dict.items()}

return cls(comp_dict)

def get_el_amt_dict(self) -> dict[str, float]:
"""
Returns:
Expand Down Expand Up @@ -674,6 +691,15 @@ def to_reduced_dict(self) -> dict:
"""
return self.get_reduced_composition_and_factor()[0].as_dict()

@property
def to_weight_dict(self) -> dict:
"""
Returns:
Dict with weight fraction of each component
{"Ti": 0.90, "V": 0.06, "Al": 0.04}
"""
return {str(el): self.get_wt_fraction(el) for el in self.elements}

@property
def to_data_dict(self) -> dict:
"""
Expand Down
16 changes: 16 additions & 0 deletions pymatgen/core/tests/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ def test_from_dict(self):
comp2 = Composition.from_dict(comp.as_dict())
assert comp == comp2

def test_from_weight_dict(self):
weight_dict_list = [{"Ti": 90, "V": 6, "Al": 4}, {"Ni": 60, "Ti": 40}, {"H": 0.1119, "O": 0.8881}]
formula_list = ["Ti87.6 V5.5 Al6.9", "Ti44.98 Ni55.02", "H2O"]

for weight_dict, formula in zip(weight_dict_list, formula_list):
c1 = Composition(formula).fractional_composition
c2 = Composition.from_weight_dict(weight_dict).fractional_composition
assert set(c1.elements) == set(c2.elements)
for el in c1.elements:
assert c1[el] == pytest.approx(c2[el], abs=1e-3)

def test_tofrom_weight_dict(self):
for c in self.comp:
c2 = Composition().from_weight_dict(c.to_weight_dict)
c.almost_equals(c2)

def test_as_dict(self):
c = Composition.from_dict({"Fe": 4, "O": 6})
d = c.as_dict()
Expand Down
33 changes: 25 additions & 8 deletions pymatgen/electronic_structure/tests/test_bandstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_from_dict(self):
self.assertEqual(kpoint.label, "X")


class BandStructureSymmLine_test(PymatgenTest):
class BandStructureSymmLineTest(PymatgenTest):
def setUp(self):
self.bs = loadfn(os.path.join(PymatgenTest.TEST_FILES_DIR, "Cu2O_361_bandstructure.json"))
self.bs2 = loadfn(os.path.join(PymatgenTest.TEST_FILES_DIR, "CaO_2605_bandstructure.json"))
Expand Down Expand Up @@ -213,12 +213,29 @@ def test_get_sym_eq_kpoints_and_degeneracy(self):
self.assertTrue([0.0, 0.0, 0.0] in vbm_eqs)

def test_as_dict(self):
s = json.dumps(self.bs.as_dict())
self.assertIsNotNone(s)
s = json.dumps(self.bs2.as_dict())
self.assertIsNotNone(s)
s = json.dumps(self.bs_spin.as_dict())
self.assertIsNotNone(s)
expected_keys = {
"@module",
"@class",
"lattice_rec",
"efermi",
"kpoints",
"bands",
"is_metal",
"vbm",
"cbm",
"band_gap",
"labels_dict",
"is_spin_polarized",
"projections",
# "structure", # not always present
"branches",
}
d1 = self.bs.as_dict()
assert set(d1) >= expected_keys, f"{expected_keys - set(d1)=}"
d2 = self.bs2.as_dict()
assert set(d2) >= expected_keys, f"{expected_keys - set(d2)=}"
d3 = self.bs_spin.as_dict()
assert set(d3) >= expected_keys, f"{expected_keys - set(d3)=}"

def test_old_format_load(self):
with open(os.path.join(PymatgenTest.TEST_FILES_DIR, "bs_ZnS_old.json")) as f:
Expand Down Expand Up @@ -252,7 +269,7 @@ def test_vasprun_bs(self):
bs.get_projection_on_elements()


class LobsterBandStructureSymmLine_test(PymatgenTest):
class LobsterBandStructureSymmLineTest(PymatgenTest):
def setUp(self):
warnings.simplefilter("ignore")
with open(
Expand Down
50 changes: 25 additions & 25 deletions test_files/.pytest-split-durations
Original file line number Diff line number Diff line change
Expand Up @@ -1127,33 +1127,33 @@
"pymatgen/core/tests/test_units.py::FloatWithUnitTest::test_unitized": 0.0010557089990470558,
"pymatgen/core/tests/test_units.py::UnitTest::test_init": 0.00048183402395807207,
"pymatgen/core/tests/test_xcfunc.py::LibxcFuncTest::test_xcfunc_api": 0.000498748995596543,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_as_dict": 0.10654545793659054,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_basic": 0.41333766595926136,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_band_gap": 0.3354746660334058,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_branch": 0.047333666996564716,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_cbm": 0.048969583003781736,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_direct_band_gap": 0.04812708401004784,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_direct_band_gap_dict": 0.048522499011596665,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_sym_eq_kpoints_and_degeneracy": 0.16099829101585783,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_get_vbm": 0.048561333009274676,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_is_metal": 0.048073498968733475,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_old_format_load": 0.40718304202891886,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLine_test::test_properties": 0.04850958302267827,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_as_dict": 0.10654545793659054,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_basic": 0.41333766595926136,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_band_gap": 0.3354746660334058,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_branch": 0.047333666996564716,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_cbm": 0.048969583003781736,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_direct_band_gap": 0.04812708401004784,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_direct_band_gap_dict": 0.048522499011596665,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_sym_eq_kpoints_and_degeneracy": 0.16099829101585783,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_get_vbm": 0.048561333009274676,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_is_metal": 0.048073498968733475,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_old_format_load": 0.40718304202891886,
"pymatgen/electronic_structure/tests/test_bandstructure.py::BandStructureSymmLineTest::test_properties": 0.04850958302267827,
"pymatgen/electronic_structure/tests/test_bandstructure.py::KpointTest::test_as_dict": 0.0004644579894375056,
"pymatgen/electronic_structure/tests/test_bandstructure.py::KpointTest::test_from_dict": 0.0003553740098141134,
"pymatgen/electronic_structure/tests/test_bandstructure.py::KpointTest::test_properties": 0.00022700100089423358,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_as_dict": 0.07780262498999946,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_basic": 2.6535320830298588,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_band_gap": 0.03351566698984243,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_branch": 0.029398249986115843,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_cbm": 0.029687250062124804,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_direct_band_gap": 0.02957983303349465,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_direct_band_gap_dict": 0.028597293014172465,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_sym_eq_kpoints_and_degeneracy": 0.030959791998611763,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_get_vbm": 0.02978300000540912,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_is_metal": 0.028640333970542997,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_old_format_load": 0.038196709007024765,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLine_test::test_proj_bandstructure_plot": 4.267984291975154,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_as_dict": 0.07780262498999946,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_basic": 2.6535320830298588,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_band_gap": 0.03351566698984243,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_branch": 0.029398249986115843,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_cbm": 0.029687250062124804,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_direct_band_gap": 0.02957983303349465,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_direct_band_gap_dict": 0.028597293014172465,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_sym_eq_kpoints_and_degeneracy": 0.030959791998611763,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_get_vbm": 0.02978300000540912,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_is_metal": 0.028640333970542997,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_old_format_load": 0.038196709007024765,
"pymatgen/electronic_structure/tests/test_bandstructure.py::LobsterBandStructureSymmLineTest::test_proj_bandstructure_plot": 4.267984291975154,
"pymatgen/electronic_structure/tests/test_bandstructure.py::ReconstructBandStructureTest::test_reconstruct_band_structure": 0.025210416002664715,
"pymatgen/electronic_structure/tests/test_bandstructure.py::ReconstructBandStructureTest::test_vasprun_bs": 1.0287437920051161,
"pymatgen/electronic_structure/tests/test_boltztrap.py::BoltztrapAnalyzerTest::test_extreme": 0.00022050002007745206,
Expand Down Expand Up @@ -2375,4 +2375,4 @@
"pymatgen/util/tests/test_typing.py::test_vector_like": 0.00013479203335009515,
"pymatgen/vis/tests/test_plotters.py::SpectrumPlotterTest::test_get_plot": 0.07970808498794213,
"pymatgen/vis/tests/test_plotters.py::SpectrumPlotterTest::test_get_stacked_plot": 0.02657462502247654
}
}

0 comments on commit a0811e7

Please sign in to comment.