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

[MNT] Improve performance of fetch_electronegativity #198

Merged
merged 3 commits into from
Nov 4, 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
15 changes: 0 additions & 15 deletions docs/source/api/mendeleev.fetch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,9 @@

.. autosummary::

add_plot_columns
fetch_electronegativities
fetch_ionic_radii
fetch_ionization_energies
fetch_neutral_data
fetch_table
get_app_data
get_zeff













8 changes: 4 additions & 4 deletions mendeleev/electronegativity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def allred_rochow(zeff: float, radius: float) -> float:
radius: value of the radius
"""

return zeff / math.pow(radius, 2)
return zeff / np.power(radius, 2)


def cottrell_sutton(zeff: float, radius: float) -> float:
Expand All @@ -37,7 +37,7 @@ def cottrell_sutton(zeff: float, radius: float) -> float:
radius: value of the radius
"""

return math.sqrt(zeff / radius)
return np.sqrt(zeff / radius)


def gordy(zeff: float, radius: float) -> float:
Expand Down Expand Up @@ -137,7 +137,7 @@ def nagle(nvalence: int, polarizability: float) -> float:
polarizability: dipole polarizability
"""

return math.pow(nvalence / polarizability, 1.0 / 3.0)
return np.power(nvalence / polarizability, 1.0 / 3.0)


def sanderson(radius: float, noble_gas_radius: float) -> float:
Expand Down Expand Up @@ -179,4 +179,4 @@ def generic(zeff: float, radius: float, rpow: float = 1, apow: float = 1) -> flo
- :math:`\alpha,\beta` parameters
"""

return math.pow(zeff / math.pow(radius, rpow), apow)
return np.power(zeff / np.power(radius, rpow), apow)
75 changes: 41 additions & 34 deletions mendeleev/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
from typing import List, Union

import pandas as pd
from deprecated import deprecated
from sqlalchemy.dialects import sqlite
from sqlalchemy import text

from mendeleev import element
from mendeleev import element, get_all_elements
from mendeleev import __version__ as version
from mendeleev.electronegativity import allred_rochow, gordy, cottrell_sutton

from .db import get_engine, get_session
from .models import Element, IonizationEnergy


@deprecated(
reason="This function is deprecated and will be removed in the future version."
)
def get_zeff(an, method: str = "slater") -> float:
"""
A helper function to calculate the effective nuclear charge.
Expand Down Expand Up @@ -84,33 +89,38 @@ def fetch_electronegativities(scales: List[str] = None) -> pd.DataFrame:
Returns:
df (pandas.DataFrame): Pandas DataFrame with the contents of the table
"""

session = get_session()
engine = get_engine()

query = session.query(Element.atomic_number).order_by("atomic_number")
df = pd.read_sql_query(query.statement.compile(dialect=sqlite.dialect()), engine)

scales = [
"allen",
"allred-rochow",
"cottrell-sutton",
"ghosh",
"gordy",
"li-xue",
"martynov-batsanov",
"mulliken",
"nagle",
"pauling",
"sanderson",
]

session = get_session()
engine = get_engine()

query = session.query(
Element.atomic_number,
Element.symbol,
Element.covalent_radius_pyykko.label("radius"),
Element.en_pauling.label("Pauling"),
Element.en_allen.label("Allen"),
Element.en_ghosh.label("Ghosh"),
).order_by("atomic_number")
df = pd.read_sql_query(query.statement.compile(dialect=sqlite.dialect()), engine)

elems = get_all_elements()

df.loc[:, "zeff"] = [e.zeff() for e in elems]
# scales
df.loc[:, "Allred-Rochow"] = allred_rochow(df["zeff"], df["radius"])
df.loc[:, "Cottrell-Sutton"] = cottrell_sutton(df["zeff"], df["radius"])
df.loc[:, "Gordy"] = gordy(df["zeff"], df["radius"])

for scale in scales:
scale_name = "-".join(s.capitalize() for s in scale.split("-"))
df.loc[:, scale_name] = [
element(int(row.atomic_number)).electronegativity(scale=scale)
for _, row in df.iterrows()
]
df.loc[:, scale_name] = [e.electronegativity(scale=scale) for e in elems]
return df.set_index("atomic_number")


Expand Down Expand Up @@ -192,22 +202,13 @@ def fetch_neutral_data() -> pd.DataFrame:
)

elements.rename(columns={"color": "series_colors"}, inplace=True)

for attr in ["hardness", "softness"]:
elements[attr] = [
getattr(element(row.symbol), attr)() for _, row in elements.iterrows()
]

elements["mass"] = [
element(row.symbol).mass_str() for _, row in elements.iterrows()
]

elements.loc[:, "zeff_slater"] = elements.apply(
lambda x: get_zeff(x["atomic_number"], method="slater"), axis=1
)
elements.loc[:, "zeff_clementi"] = elements.apply(
lambda x: get_zeff(x["atomic_number"], method="clementi"), axis=1
)
# get all element objects
ELEMS = get_all_elements()
elements.loc[:, "hardness"] = [e.hardness() for e in ELEMS]
elements.loc[:, "softness"] = [e.softness() for e in ELEMS]
elements.loc[:, "mass"] = [e.mass_str() for e in ELEMS]
elements.loc[:, "zeff_slater"] = [e.zeff(method="slater") for e in ELEMS]
elements.loc[:, "zeff_clementi"] = [e.zeff(method="clementi") for e in ELEMS]

ens = fetch_electronegativities()
elements = pd.merge(elements, ens.reset_index(), on="atomic_number", how="left")
Expand Down Expand Up @@ -240,6 +241,9 @@ def fetch_ionic_radii(radius: str = "ionic_radius") -> pd.DataFrame:
)


@deprecated(
reason="This function is deprecated and will be removed in the future version."
)
def add_plot_columns(elements: pd.DataFrame) -> pd.DataFrame:
"""
Add columns needed for the creating the plots
Expand Down Expand Up @@ -275,6 +279,9 @@ def add_plot_columns(elements: pd.DataFrame) -> pd.DataFrame:
return elements


@deprecated(
reason="This function is deprecated and will be removed in the future version."
)
def get_app_data() -> None:
"write a file with the neutral data"
data = fetch_neutral_data()
Expand Down
Loading
Loading