Skip to content

Commit 13b5516

Browse files
authored
Move needlessly function-scoped imports to module scope (#3462)
* move function-scoped imports to module scope * more * more * more
1 parent d4e253d commit 13b5516

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+113
-244
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ci:
88

99
repos:
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.1.4
11+
rev: v0.1.5
1212
hooks:
1313
- id: ruff
1414
args: [--fix]

dev_scripts/regen_libxcfunc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import json
1313
import os
1414
import sys
15+
from copy import deepcopy
1516

1617

1718
def parse_libxc_docs(path):
@@ -46,8 +47,6 @@ def parse_section(section):
4647

4748
def write_libxc_docs_json(xcfuncs, jpath):
4849
"""Write json file with libxc metadata to path jpath."""
49-
from copy import deepcopy
50-
5150
xcfuncs = deepcopy(xcfuncs)
5251

5352
# Remove XC_FAMILY from Family and XC_ from Kind to make strings more human-readable.

dev_scripts/update_pt_data.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
from __future__ import annotations
99

10+
import collections
1011
import json
1112
import re
1213
from itertools import product
1314

15+
import requests
16+
from bs4 import BeautifulSoup
1417
from monty.serialization import dumpfn, loadfn
1518
from ruamel import yaml
1619

@@ -160,7 +163,6 @@ def update_ionic_radii():
160163
def parse_shannon_radii():
161164
with open("periodic_table.yaml") as f:
162165
data = yaml.load(f)
163-
import collections
164166

165167
from openpyxl import load_workbook
166168

@@ -250,8 +252,6 @@ def gen_iupac_ordering():
250252

251253
def add_electron_affinities():
252254
"""Update the periodic table data file with electron affinities."""
253-
import requests
254-
from bs4 import BeautifulSoup
255255

256256
req = requests.get("https://wikipedia.org/wiki/Electron_affinity_(data_page)")
257257
soup = BeautifulSoup(req.text, "html.parser")
@@ -276,9 +276,6 @@ def add_electron_affinities():
276276

277277
def add_ionization_energies():
278278
"""Update the periodic table data file with ground level and ionization energies from NIST."""
279-
import collections
280-
281-
from bs4 import BeautifulSoup
282279

283280
with open("NIST Atomic Ionization Energies Output.html") as f:
284281
soup = BeautifulSoup(f.read(), "html.parser")

docs/apidoc/conf.py

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pymatgen/analysis/bond_valence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ class BVAnalyzer:
117117
is selected.
118118
"""
119119

120-
CHARGE_NEUTRALITY_TOLERANCE = 0.00001
120+
CHARGE_NEUTRALITY_TOLERANCE = 0.000_01
121121

122122
def __init__(
123123
self,
124124
symm_tol=0.1,
125125
max_radius=4,
126-
max_permutations=100000,
126+
max_permutations=100_000,
127127
distance_scale_factor=1.015,
128128
charge_neutrality_tolerance=CHARGE_NEUTRALITY_TOLERANCE,
129129
forbidden_species=None,

pymatgen/analysis/chemenv/connectivity/connected_components.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import itertools
66
import logging
77

8+
import matplotlib.pyplot as plt
89
import networkx as nx
910
import numpy as np
1011
from matplotlib.patches import Circle, FancyArrowPatch
@@ -533,7 +534,6 @@ def show_graph(
533534
If not provided, the graph is not saved.
534535
drawing_type (str): The type of drawing to use. Can be "internal" or "external".
535536
"""
536-
import matplotlib.pyplot as plt
537537

538538
shown_graph = self._connected_subgraph if graph is None else graph
539539

@@ -550,12 +550,8 @@ def show_graph(
550550
plt.savefig(save_file)
551551
# nx.draw(self._connected_subgraph)
552552
elif drawing_type == "draw_graphviz":
553-
import networkx as nx
554-
555553
nx.nx_pydot.graphviz_layout(shown_graph)
556554
elif drawing_type == "draw_random":
557-
import networkx as nx
558-
559555
nx.draw_random(shown_graph)
560556

561557
@property

pymatgen/analysis/chemenv/coordination_environments/coordination_geometries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,15 @@ def pauling_stability_ratio(self):
697697
if self.ce_symbol in ["S:1", "L:2"]:
698698
self._pauling_stability_ratio = 0.0
699699
else:
700-
min_dist_anions = 1000000.0
701-
min_dist_cation_anion = 1000000.0
700+
min_dist_anions = 1_000_000
701+
min_dist_cation_anion = 1_000_000
702702
for ipt1 in range(len(self.points)):
703703
pt1 = np.array(self.points[ipt1])
704704
min_dist_cation_anion = min(min_dist_cation_anion, np.linalg.norm(pt1 - self.central_site))
705705
for ipt2 in range(ipt1 + 1, len(self.points)):
706706
pt2 = np.array(self.points[ipt2])
707707
min_dist_anions = min(min_dist_anions, np.linalg.norm(pt1 - pt2))
708-
anion_radius = min_dist_anions / 2.0
708+
anion_radius = min_dist_anions / 2
709709
cation_radius = min_dist_cation_anion - anion_radius
710710
self._pauling_stability_ratio = cation_radius / anion_radius
711711
return self._pauling_stability_ratio

pymatgen/analysis/chemenv/coordination_environments/structure_environments.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
from __future__ import annotations
1010

11-
from typing import TYPE_CHECKING
12-
11+
import matplotlib.pyplot as plt
1312
import numpy as np
13+
from matplotlib import cm
14+
from matplotlib.colors import Normalize
15+
from matplotlib.gridspec import GridSpec
16+
from matplotlib.patches import Polygon
1417
from monty.json import MontyDecoder, MSONable, jsanitize
1518

1619
from pymatgen.analysis.chemenv.coordination_environments.coordination_geometries import AllCoordinationGeometries
@@ -19,9 +22,6 @@
1922
from pymatgen.analysis.chemenv.utils.defs_utils import AdditionalConditions
2023
from pymatgen.core import Element, PeriodicNeighbor, PeriodicSite, Species, Structure
2124

22-
if TYPE_CHECKING:
23-
import matplotlib.pyplot as plt
24-
2525
__author__ = "David Waroquiers"
2626
__copyright__ = "Copyright 2012, The Materials Project"
2727
__credits__ = "Geoffroy Hautier"
@@ -645,11 +645,6 @@ def plot_csm_and_maps(self, isite, max_csm=8.0):
645645
isite: Index of the site for which the plot has to be done
646646
max_csm: Maximum continuous symmetry measure to be shown.
647647
"""
648-
try:
649-
import matplotlib.pyplot as plt
650-
except ImportError:
651-
print('Plotting Chemical Environments requires matplotlib ... exiting "plot" function')
652-
return
653648
fig = self.get_csm_and_maps(isite=isite, max_csm=max_csm)
654649
if fig is None:
655650
return
@@ -673,13 +668,6 @@ def get_csm_and_maps(
673668
Returns:
674669
Matplotlib figure and axes representing the CSM and maps.
675670
"""
676-
try:
677-
import matplotlib.pyplot as plt
678-
from matplotlib.gridspec import GridSpec
679-
except ImportError:
680-
print('Plotting Chemical Environments requires matplotlib ... exiting "plot" function')
681-
return None
682-
683671
if symmetry_measure_type is None:
684672
symmetry_measure_type = "csm_wcs_ctwcc"
685673
# Initializes the figure
@@ -826,15 +814,6 @@ def get_environments_figure(
826814
Returns:
827815
tuple[plt.Figure, plt.Axes]: matplotlib figure and axes representing the environments.
828816
"""
829-
try:
830-
import matplotlib.pyplot as plt
831-
from matplotlib import cm
832-
from matplotlib.colors import Normalize
833-
from matplotlib.patches import Polygon
834-
except ImportError:
835-
print('Plotting Chemical Environments requires matplotlib ... exiting "plot" function')
836-
return None
837-
838817
# Initializes the figure
839818
fig = plt.figure(figsize=figsize)
840819
ax = fig.add_subplot(111)

pymatgen/analysis/chemenv/coordination_environments/voronoi.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import time
77

8+
import matplotlib.pyplot as plt
89
import numpy as np
910
from monty.json import MSONable
1011
from scipy.spatial import Voronoi
@@ -793,8 +794,6 @@ def get_rdf_figure(self, isite, normalized=True, figsize=None, step_function=Non
793794
def dp_func(dp):
794795
return 1.0 - 1.0 / np.power(dp, 3.0)
795796

796-
import matplotlib.pyplot as plt
797-
798797
if step_function is None:
799798
step_function = {"type": "normal_cdf", "scale": 0.0001}
800799

@@ -845,8 +844,6 @@ def get_sadf_figure(self, isite, normalized=True, figsize=None, step_function=No
845844
def ap_func(ap):
846845
return np.power(ap, -0.1)
847846

848-
import matplotlib.pyplot as plt
849-
850847
if step_function is None:
851848
step_function = {"type": "step_function", "scale": 0.0001}
852849

pymatgen/analysis/diffraction/core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import collections
77
from typing import TYPE_CHECKING
88

9+
import matplotlib.pyplot as plt
910
import numpy as np
10-
from matplotlib import pyplot as plt
1111

1212
from pymatgen.core.spectrum import Spectrum
1313
from pymatgen.util.plotting import add_fig_kwargs, pretty_plot
@@ -186,7 +186,6 @@ def plot_structures(self, structures, fontsize=6, **kwargs):
186186
long version, e.g. (1, 0, 0). If None, do not show anything.
187187
fontsize: (int) fontsize for peak labels.
188188
"""
189-
import matplotlib.pyplot as plt
190189

191190
nrows = len(structures)
192191
fig, axes = plt.subplots(nrows=nrows, ncols=1, sharex=True, squeeze=False)

0 commit comments

Comments
 (0)