Skip to content

Commit 9be8ea9

Browse files
committed
remove old periodic dependency
1 parent 762f2b5 commit 9be8ea9

File tree

5 files changed

+125
-119
lines changed

5 files changed

+125
-119
lines changed

atomdb/datasets/slater/run.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
import re
2020
import atomdb
2121

22-
from atomdb.periodic import Element
2322
from grid.onedgrid import UniformInteger
2423
from grid.rtransform import ExpRTransform
2524

2625
# from importlib_resources import files
2726
from atomdb.utils import DEFAULT_DATAPATH
2827
from scipy.special import factorial
29-
from dataclasses import dataclass, field
28+
from dataclasses import dataclass
3029
from typing import Optional, Dict
30+
from atomdb.periodic_test import element_symbol_map, get_scalar_data
3131

3232
__all__ = ["AtomicDensity", "load_slater_wfn", "run"]
3333

@@ -1123,7 +1123,7 @@ def run(elem, charge, mult, nexc, dataset, datapath):
11231123

11241124
# Set up internal variables
11251125
elem = atomdb.element_symbol(elem)
1126-
atnum = atomdb.element_number(elem)
1126+
atnum = element_symbol_map[elem][0]
11271127
nelec = atnum - charge
11281128
nspin = mult - 1
11291129

@@ -1177,23 +1177,13 @@ def run(elem, charge, mult, nexc, dataset, datapath):
11771177
mo_ked_a = species.eval_orbs_ked_positive_definite(rs)[:norba, :]
11781178
mo_ked_b = species.eval_orbs_ked_positive_definite(rs)[:norba, :]
11791179

1180-
1181-
1182-
1183-
# Get information about the element --> (dont forget) needs to be refactored
1184-
atom = Element(elem)
1185-
atmass = atom.mass
1186-
cov_radius, vdw_radius, at_radius, polarizability, dispersion = [
1187-
None,
1188-
] * 5
1189-
# overwrite values for neutral atomic species
1190-
if charge == 0:
1191-
cov_radius, vdw_radius, at_radius = (atom.cov_radius, atom.vdw_radius, atom.at_radius)
1192-
polarizability = atom.pold
1193-
dispersion = {"C6": atom.c6}
1194-
1195-
1196-
1180+
# Get periodic data
1181+
cov_radius = get_scalar_data('cov_radius', atnum, nelec)
1182+
vdw_radius = get_scalar_data('vdw_radius', atnum, nelec)
1183+
at_radius = get_scalar_data('at_radius', atnum, nelec)
1184+
polarizability = get_scalar_data('polarizability', atnum, nelec)
1185+
dispersion = get_scalar_data('dispersion', atnum, nelec)
1186+
atmass = get_scalar_data('atmass', atnum, nelec)
11971187

11981188

11991189

@@ -1211,12 +1201,12 @@ def run(elem, charge, mult, nexc, dataset, datapath):
12111201
nelec=nelec,
12121202
nspin=nspin,
12131203
nexc=nexc,
1214-
atmass=atmass, #
1215-
cov_radius=cov_radius, #
1216-
vdw_radius=vdw_radius, #
1217-
at_radius=at_radius, #
1218-
polarizability=polarizability, #
1219-
dispersion=dispersion, #
1204+
atmass=atmass,
1205+
cov_radius=cov_radius,
1206+
vdw_radius=vdw_radius,
1207+
at_radius=at_radius,
1208+
polarizability=polarizability,
1209+
dispersion=dispersion,
12201210
energy=energy,
12211211
mo_energy_a=mo_e_up,
12221212
mo_energy_b=mo_e_dn,

atomdb/migration/periodic/elements_data.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@
1414
hdf5_file = files("atomdb.data").joinpath("elements_data.h5")
1515

1616

17-
PROPERTY_NAME_MAP = {
18-
"atmass": "atmass",
19-
"cov_radius": "cov_radius",
20-
"vdw_radius": "vdw_radius",
21-
"at_radius": "at_radius",
22-
"polarizability": "polarizability",
23-
"dispersion_c6": "dispersion_c6",
24-
"dispersion": "dispersion_c6",
25-
"elem": "symbol",
26-
"atnum": "atnum",
27-
"name": "name",
28-
}
29-
30-
3117
# Properties of each element in the HDF5 file.
3218
PROPERTY_CONFIGS = [
3319
{
@@ -362,20 +348,6 @@ def write_data_info_to_hdf5(data_info_list):
362348

363349

364350

365-
def map_element_symbol(ELEMENTS_H5FILE):
366-
element_symbol_map = {}
367-
for element_group in ELEMENTS_H5FILE.root.Elements:
368-
symbol = element_group.symbol[0]['value'].decode('utf-8').strip()
369-
atnum = element_group.atnum[0]['value']
370-
name = element_group.name[0]['value'].decode('utf-8').strip()
371-
element_symbol_map[symbol] = (atnum, name)
372-
373-
return element_symbol_map
374-
375-
376-
377-
378-
379351
if __name__ == "__main__":
380352
# Read the elements data from the CSV file
381353
data, unique_headers, sources_data, units_data = read_elements_data_csv(elements_data_csv)

atomdb/periodic_test.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from enum import IntEnum
2+
from numbers import Integral
3+
import tables as pt
4+
import numpy as np
5+
from importlib_resources import files
6+
7+
8+
__all__ = [
9+
"PROPERTY_NAME_MAP",
10+
"get_scalar_data",
11+
"element_symbol_map",
12+
"ElementAttr",
13+
]
14+
15+
class ElementAttr(IntEnum):
16+
atnum = 0
17+
name = 1
18+
19+
20+
elements_hdf5_file = files("atomdb.data").joinpath("elements_data.h5")
21+
ELEMENTS_H5FILE = pt.open_file(elements_hdf5_file, mode="r")
22+
23+
PROPERTY_NAME_MAP = {
24+
"atmass": "atmass",
25+
"cov_radius": "cov_radius",
26+
"vdw_radius": "vdw_radius",
27+
"at_radius": "at_radius",
28+
"polarizability": "polarizability",
29+
"dispersion_c6": "dispersion_c6",
30+
"dispersion": "dispersion_c6", #fields in run
31+
"elem": "symbol",
32+
"atnum": "atnum",
33+
"name": "name",
34+
}
35+
36+
def get_scalar_data(prop_name, atnum, nelec):
37+
charge = atnum - nelec
38+
39+
if charge != 0 and prop_name not in ["atmass", "elem", "atnum", "name"]:
40+
return None
41+
42+
# get the element group
43+
element_group = f"/Elements/{atnum:03d}"
44+
45+
table_name = PROPERTY_NAME_MAP[prop_name]
46+
table_path = f"{element_group}/{table_name}"
47+
48+
# get the table node from the HDF5 file
49+
table = ELEMENTS_H5FILE.get_node(table_path)
50+
51+
# Handle basic properties (single column --> no sources)
52+
if len(table.colnames) == 1 and table.colnames[0] == "value":
53+
value = table[0]["value"]
54+
# if the value is an int, return it as an int
55+
if isinstance(value, Integral):
56+
return int(value)
57+
# if the value is a string, decode from bytes
58+
elif isinstance(value, bytes):
59+
return value.decode("utf-8")
60+
else:
61+
# handle properties with multiple sources
62+
result = {}
63+
for row in table:
64+
source = row["source"].decode("utf-8")
65+
value = row["value"]
66+
# exclude none values
67+
if not np.isnan(value):
68+
result[source] = float(value)
69+
return result if result else None
70+
71+
72+
73+
def map_element_symbol():
74+
element_symbol_map = {}
75+
for element_group in ELEMENTS_H5FILE.root.Elements:
76+
symbol = element_group.symbol[0]['value'].decode('utf-8').strip()
77+
atnum = element_group.atnum[0]['value']
78+
name = element_group.name[0]['value'].decode('utf-8').strip()
79+
element_symbol_map[symbol] = (atnum, name)
80+
81+
return element_symbol_map
82+
83+
84+
element_symbol_map = map_element_symbol()

atomdb/species.py

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,20 @@
2424
import numpy as np
2525
import pooch
2626
import requests
27-
from msgpack import packb, unpackb
28-
from msgpack_numpy import decode, encode
2927
from numpy import ndarray
3028
from scipy.interpolate import CubicSpline
3129

32-
from atomdb.periodic import Element, element_symbol
30+
from atomdb.periodic_test import element_symbol_map, PROPERTY_NAME_MAP, get_scalar_data, ElementAttr
3331
from atomdb.utils import DEFAULT_DATAPATH, DEFAULT_DATASET, DEFAULT_REMOTE
3432
from importlib_resources import \
3533
files
3634
import tables as pt
3735
from numbers import Integral
38-
from migration.periodic.elements_data import PROPERTY_NAME_MAP, map_element_symbol
3936

40-
elements_hdf5_file = files("atomdb.data").joinpath("elements_data.h5")
4137
datasets_hdf5_file = files("atomdb.data").joinpath("datasets_data.h5")
42-
43-
44-
ELEMENTS_H5FILE = pt.open_file(elements_hdf5_file, mode="r")
4538
DATASETS_H5FILE = pt.open_file(datasets_hdf5_file, mode="r")
4639

47-
element_symbol_map = map_element_symbol(ELEMENTS_H5FILE)
40+
4841

4942
__all__ = [
5043
"Species",
@@ -90,41 +83,6 @@ def wrapper(self):
9083
return wrapper
9184

9285

93-
def get_scalar_data(prop_name, atnum, nelec):
94-
charge = atnum - nelec
95-
96-
if charge != 0 and prop_name != "atmass":
97-
return None
98-
99-
# get the element group
100-
element_group = f"/Elements/{atnum:03d}"
101-
102-
table_name = PROPERTY_NAME_MAP[prop_name]
103-
table_path = f"{element_group}/{table_name}"
104-
105-
# get the table node from the HDF5 file
106-
table = ELEMENTS_H5FILE.get_node(table_path)
107-
108-
# Handle basic properties (single column --> no sources)
109-
if len(table.colnames) == 1 and table.colnames[0] == "value":
110-
value = table[0]["value"]
111-
# if the value is an int, return it as an int
112-
if isinstance(value, Integral):
113-
return int(value)
114-
# if the value is a string, decode from bytes
115-
elif isinstance(value, bytes):
116-
return value.decode("utf-8")
117-
else:
118-
# handle properties with multiple sources
119-
result = {}
120-
for row in table:
121-
source = row["source"].decode("utf-8")
122-
value = row["value"]
123-
# exclude none values
124-
if not np.isnan(value):
125-
result[source] = float(value)
126-
return result if result else None
127-
12886

12987
def _remove_suffix(input_string, suffix):
13088
if suffix and input_string.endswith(suffix):
@@ -781,19 +739,19 @@ def compile_species(
781739
fields = submodule.run(elem, charge, mult, nexc, dataset, datapath)
782740

783741
# dump the data to the HDF5 file
784-
dump(fields, dataset, elem, charge, mult, nexc)
742+
# dump(fields, dataset, elem, charge, mult, nexc)
785743

786744

787-
# fields = asdict(fields)
788-
# # print all fields
789-
# for key, value in fields.items():
790-
# if isinstance(value, np.ndarray):
791-
# print(f"{key}: shape={value.shape}, first 5 elements={value.flat[:5]}")
792-
# else:
793-
# print(f"{key}: {value}")
794-
#
795-
# species = Species(dataset, fields)
796-
# return species
745+
fields = asdict(fields)
746+
# print all fields
747+
for key, value in fields.items():
748+
if isinstance(value, np.ndarray):
749+
print(f"{key}: shape={value.shape}, first 5 elements={value.flat[:5]}")
750+
else:
751+
print(f"{key}: {value}")
752+
753+
species = Species(dataset, fields)
754+
return species
797755

798756

799757

@@ -1014,7 +972,8 @@ def get_species_data(folder_path, elem, DATASET_PROPERTY_CONFIGS):
1014972
fields[config['Carray_property']] = table[:]
1015973

1016974

1017-
fields['atnum'] = element_symbol_map[elem][0]
975+
fields['atnum'] = element_symbol_map[elem][ElementAttr.atnum]
976+
1018977

1019978
# Add scalar properties
1020979
for prop in ('atmass', 'cov_radius', 'vdw_radius', 'at_radius', 'polarizability', 'dispersion'):
@@ -1063,7 +1022,8 @@ def raw_datafile(
10631022
str
10641023
Path to the raw data file.
10651024
"""
1066-
elem = "*" if elem is Ellipsis else element_symbol(elem)
1025+
# elem = "*" if elem is Ellipsis else element_symbol(elem) --> why using element_symbol here
1026+
elem = "*" if elem is Ellipsis else elem
10671027
charge = "*" if charge is Ellipsis else f"{charge:03d}"
10681028
mult = "*" if mult is Ellipsis else f"{mult:03d}"
10691029
nexc = "*" if nexc is Ellipsis else f"{nexc:03d}"

atomdb/test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
# print(f"Element: {hydrogen.elem}, Charge: {hydrogen.charge}, Multiplicity: {hydrogen.mult}, vdw_radius: {hydrogen.vdw_radius}")
1313
# except Exception as e:
1414
# print(f"Error during compilation: {e}")
15-
15+
#
1616

1717

1818

1919
from atomdb import load
2020
# hydrogen = load("H", ..., ..., dataset="slater")
2121

22-
hydrogen = load(..., ..., 2, ..., dataset="slater")
22+
# hydrogen = load(..., ..., 2, ..., dataset="slater")
2323

24-
# hydrogen = load('C', 0, 3, 0, dataset="slater")
24+
hydrogen = load('C', 0, 3, 0, dataset="slater")
2525

2626

27-
for species in hydrogen:
28-
print(f"\nSpecies: {species.elem} (Charge: {species.charge}, Mult: {species.mult})")
29-
print("Attributes available:", vars(species).keys())
30-
print("Energy:", getattr(species, "energy", "Not available"))
31-
27+
# for species in hydrogen:
28+
# print(f"\nSpecies: {species.elem} (Charge: {species.charge}, Mult: {species.mult})")
29+
# print("Attributes available:", vars(species).keys())
30+
# print("Energy:", getattr(species, "energy", "Not available"))
31+
#

0 commit comments

Comments
 (0)