Skip to content

Commit aef533a

Browse files
Improve: import pymatgen and scipy when needed
1 parent 4a5957c commit aef533a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

octadist/src/io.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from operator import itemgetter
1818

1919
import numpy as np
20-
import pymatgen
21-
from scipy.spatial import distance
2220

2321
from octadist.src import elements, popup
2422

@@ -127,6 +125,14 @@ def get_coord_cif(f):
127125

128126
warnings.filterwarnings("ignore")
129127

128+
# Lazy import to avoid requiring pymatgen unless CIF parsing is used
129+
try:
130+
import pymatgen
131+
except ImportError as e:
132+
raise ImportError(
133+
"pymatgen is required to read CIF files. Install it or use a different input format."
134+
) from e
135+
130136
# works only with pymatgen <= v2021.3.3
131137
structure = pymatgen.Structure.from_file(f)
132138
atom = list(map(lambda x: elements.number_to_symbol(x), structure.atomic_numbers))
@@ -226,9 +232,12 @@ def get_coord_xyz(f):
226232

227233
atom = []
228234
for l in line:
229-
# read atom on 1st column and insert to list
235+
# read atom symbol on 1st column using arbitrary whitespace splitter
236+
# handles spaces and tabs in XYZ files
230237
l_strip = l.strip()
231-
lst = l_strip.split(" ")[0]
238+
if not l_strip:
239+
continue
240+
lst = l_strip.split()[0]
232241
atom.append(lst)
233242

234243
file = open(f, "r")
@@ -967,6 +976,9 @@ def extract_octa(atom, coord, ref_index=0, cutoff_ref_ligand=2.8):
967976
"index of the reference center atom is greater than the total number of atoms in the complex."
968977
)
969978

979+
# Lazy import to avoid requiring scipy unless octa extraction is used
980+
from scipy.spatial import distance
981+
970982
dist_list = []
971983
for i in range(len(list(atom))):
972984
dist = distance.euclidean(coord[ref_index], coord[i])

0 commit comments

Comments
 (0)