Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy import pandas in Structure.as_dataframe() to improve startup speed #3568

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from typing import TYPE_CHECKING, Any, Callable, Literal, SupportsIndex, cast, get_args

import numpy as np
import pandas as pd
from monty.dev import deprecated
from monty.io import zopen
from monty.json import MSONable
Expand Down Expand Up @@ -2625,16 +2624,19 @@ def as_dataframe(self):
0 (Si) 0.0 0.0 0.000000e+00 0.0 0.000000e+00 0.000000e+00 5
1 (Si) 0.0 0.0 1.000000e-7 0.0 -2.217138e-7 3.135509e-7 -5
"""
data = []
# pandas lazy imported for speed (https://github.com/materialsproject/pymatgen/issues/3563)
import pandas as pd

data: list[list[str | float]] = []
site_properties = self.site_properties
prop_keys = list(site_properties)
for site in self:
row = [site.species, *site.frac_coords, *site.coords]
for k in prop_keys:
row.append(site.properties.get(k))
for key in prop_keys:
row.append(site.properties.get(key))
data.append(row)

df = pd.DataFrame(data, columns=["Species", "a", "b", "c", "x", "y", "z", *prop_keys])
df = pd.DataFrame(data, columns=["Species", *"abcxyz", *prop_keys])
df.attrs["Reduced Formula"] = self.composition.reduced_formula
df.attrs["Lattice"] = self.lattice
return df
Expand Down
9 changes: 6 additions & 3 deletions tests/core/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ def test_get_orderings(self):
assert sqs[0].formula == "Mn8 Fe8"

def test_as_dataframe(self):
df = self.propertied_structure.as_dataframe()
assert df.attrs["Reduced Formula"] == self.propertied_structure.composition.reduced_formula
assert df.shape == (2, 8)
df_struct = self.propertied_structure.as_dataframe()
assert df_struct.attrs["Reduced Formula"] == self.propertied_structure.composition.reduced_formula
assert df_struct.shape == (2, 8)
assert list(df_struct) == ["Species", *"abcxyz", "magmom"]
assert list(df_struct["magmom"]) == [5, -5]
assert list(map(str, df_struct["Species"])) == ["Si1", "Si1"]

def test_equal(self):
struct = self.struct
Expand Down
7 changes: 4 additions & 3 deletions tests/electronic_structure/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import matplotlib.pyplot as plt
import numpy as np
import scipy
from matplotlib import rc
from numpy.testing import assert_allclose
from pytest import approx
Expand Down Expand Up @@ -269,13 +268,15 @@ def test_bz_plot(self):
)

def test_fold_point(self):
assert scipy.allclose(
assert_allclose(
fold_point([0.0, -0.5, 0.5], lattice=self.rec_latt),
self.rec_latt.get_cartesian_coords([0.0, 0.5, 0.5]),
atol=1e-8,
)
assert scipy.allclose(
assert_allclose(
fold_point([0.1, -0.6, 0.2], lattice=self.rec_latt),
self.rec_latt.get_cartesian_coords([0.1, 0.4, 0.2]),
atol=1e-8,
)


Expand Down