Skip to content

Commit a5427f7

Browse files
authored
Update linting tools and and workflows (#217)
* bump pre-commit * autofix * manual fix * original * ignore env * update gh workflows * reparse notebooks
1 parent a3bb39b commit a5427f7

20 files changed

+94
-90
lines changed

.github/workflows/python-publish.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66

77
permissions:
88
contents: read
9+
id-token: write
910

1011
jobs:
1112
deploy:
@@ -26,5 +27,3 @@ jobs:
2627
run: python -m build
2728
- name: Publish package
2829
uses: pypa/gh-action-pypi-publish@release/v1
29-
with:
30-
password: ${{ secrets.PYPI_API_TOKEN }}

.github/workflows/testing.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
runs-on: ubuntu-latest
3535
strategy:
3636
matrix:
37-
python-version: ["3.10", "3.11"]
37+
python-version: ["3.10", "3.11", "3.12"]
3838

3939
env:
4040
PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
@@ -58,8 +58,8 @@ jobs:
5858
- name: Test Notebooks
5959
run: pytest --nbmake ./docs/source/content
6060

61-
- uses: codecov/codecov-action@v1
62-
if: matrix.python-version == 3.10
61+
- uses: codecov/codecov-action@v4
62+
if: matrix.python-version == '3.10'
6363
with:
6464
token: ${{ secrets.CODECOV_TOKEN }}
6565
file: ./coverage.xml
@@ -92,7 +92,7 @@ jobs:
9292
# Develop branch only
9393
- name: Deploy
9494
if: github.ref == 'refs/heads/main'
95-
uses: peaceiris/actions-gh-pages@v3
95+
uses: peaceiris/actions-gh-pages@v4
9696
with:
9797
deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
9898
publish_dir: ./docs/_build/html

.github/workflows/update-precommit.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ jobs:
99
auto-update:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v3
12+
- uses: actions/checkout@v4
1313

1414
- name: Set up Python
15-
- uses: actions/setup-python@v4
15+
uses: actions/setup-python@v5
1616
with:
17-
python-version: ${{ 3.10 }}
17+
python-version: "3.11"
1818
cache: pip
1919
cache-dependency-path: pyproject.toml
2020

@@ -25,7 +25,7 @@ jobs:
2525
run: pre-commit autoupdate
2626

2727
- name: Create Pull Request
28-
uses: peter-evans/create-pull-request@v3
28+
uses: peter-evans/create-pull-request@v6
2929
with:
3030
branch: update/pre-commit-autoupdate
3131
title: Auto-update pre-commit hooks

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ docs.old/_build/
145145
docs.old/
146146

147147
docs/source/api_ref/pymatgen.*.rst
148+
.envrc

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
33
# Ruff version.
4-
rev: v0.4.4
4+
rev: v0.12.4
55
hooks:
66
# Run the linter.
77
- id: ruff
88
args: [ --fix, --unsafe-fixes]
99
# Run the formatter.
1010
- id: ruff-format
1111
- repo: https://github.com/pre-commit/mirrors-mypy
12-
rev: v1.10.0
12+
rev: v1.17.0
1313
hooks:
1414
- id: mypy
1515
files: ^pymatgen/
@@ -20,20 +20,20 @@ repos:
2020
- tokenize-rt==4.1.0
2121
- types-pkg_resources==0.1.2
2222
- repo: https://github.com/codespell-project/codespell
23-
rev: v2.2.6
23+
rev: v2.4.1
2424
hooks:
2525
- id: codespell
2626
name: codespell
2727
description: Checks for common misspellings in text files.
2828
entry: codespell
2929
language: python
30-
types: [text]
30+
files: \.(md|py)$
3131
args: [
3232
--ignore-words-list, 'titel,statics,ba,nd,te,mater,commun,vise,dscribe',
3333
--skip, "*.ipynb,./tests,*paper*",
3434
]
3535
- repo: https://github.com/kynan/nbstripout
36-
rev: 0.7.1
36+
rev: 0.8.1
3737
hooks:
3838
- id: nbstripout
3939
args: [

docs/source/content/defect-finder.ipynb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
"from pathlib import Path\n",
2626
"\n",
2727
"import numpy as np\n",
28-
"from pymatgen.io.vasp import Locpot\n",
2928
"\n",
3029
"TEST_FILES = Path(\"../../../tests/test_files\")\n",
31-
"import glob\n",
3230
"\n",
3331
"from pymatgen.analysis.defects.finder import DefectSiteFinder\n",
3432
"from pymatgen.core.structure import Structure\n",
@@ -37,7 +35,7 @@
3735
"finder = DefectSiteFinder()\n",
3836
"\n",
3937
"for q in range(-1, 3):\n",
40-
" fname = f\"{str(TEST_FILES)}/v_N_GaN/q={q}/CONTCAR.gz\"\n",
38+
" fname = f\"{TEST_FILES!s}/v_N_GaN/q={q}/CONTCAR.gz\"\n",
4139
" struct = Structure.from_file(fname)\n",
4240
" fpos = finder.get_defect_fpos(defect_structure=struct, base_structure=base_struct)\n",
4341
" fpos -= np.round(fpos)\n",

docs/source/content/formation-energy.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
"outputs": [],
6060
"source": [
6161
"import warnings\n",
62+
"\n",
6263
"from monty.serialization import loadfn\n",
6364
"from pymatgen.analysis.defects.thermo import FormationEnergyDiagram\n",
64-
"from pymatgen.io.vasp import Vasprun\n",
6565
"\n",
6666
"warnings.filterwarnings(\"ignore\")\n",
6767
"\n",
@@ -131,7 +131,7 @@
131131
"source": [
132132
"for i, p in enumerate(fed.chempot_limits):\n",
133133
" print(f\"Limits for the chemical potential changes for point {i}\")\n",
134-
" for k,v in p.items():\n",
134+
" for k, v in p.items():\n",
135135
" print(f\"Δμ_{k} = {v:.2f} eV\")"
136136
]
137137
},
@@ -164,10 +164,11 @@
164164
"outputs": [],
165165
"source": [
166166
"from pymatgen.analysis.defects.corrections.freysoldt import plot_plnr_avg\n",
167+
"\n",
167168
"plot_data = fed.defect_entries[1].corrections_metadata[\"freysoldt\"][\"plot_data\"]\n",
168169
"plot_plnr_avg(plot_data[0], title=\"Lattice Direction 1\")\n",
169170
"plot_plnr_avg(plot_data[1], title=\"Lattice Direction 2\")\n",
170-
"plot_plnr_avg(plot_data[2], title=\"Lattice Direction 3\")\n"
171+
"plot_plnr_avg(plot_data[2], title=\"Lattice Direction 3\")"
171172
]
172173
}
173174
],

docs/source/content/freysoldt-correction.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
" defect_frac_coords=center,\n",
8282
" )\n",
8383
" align = np.mean(list(freysoldt_results[q].metadata[\"alignments\"].values()))\n",
84-
" print(f\"alignment correction: {align}\")\n"
84+
" print(f\"alignment correction: {align}\")"
8585
]
8686
},
8787
{
@@ -114,7 +114,7 @@
114114
" \"\"\"\n",
115115
" v_line_file = DEFECT_DIR / f\"q={q}/vline-eV-a{direction}.dat\"\n",
116116
" # split the file into blocks based on the location of the string \"&\"\n",
117-
" with open(v_line_file, \"r\") as f:\n",
117+
" with open(v_line_file) as f:\n",
118118
" blocks = f.read().split(\"&\")\n",
119119
"\n",
120120
" # convert each block into a list of lines and convert to a numpy array\n",
@@ -210,7 +210,7 @@
210210
"outputs": [],
211211
"source": [
212212
"def get_sxd_result(q):\n",
213-
" \"\"\"Read the output of sxdefectalign\"\"\"\n",
213+
" \"\"\"Read the output of sxdefectalign.\"\"\"\n",
214214
" fname = str(DEFECT_DIR / f\"q={q}/corr_align.out\")\n",
215215
" gen = reverse_readfile(fname)\n",
216216
" line = next(gen)\n",

docs/source/content/nonradiative.ipynb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
"source": [
2222
"# disable warnings\n",
2323
"import warnings\n",
24+
"\n",
2425
"warnings.filterwarnings(\"ignore\", category=UserWarning)\n",
2526
"from pathlib import Path\n",
27+
"\n",
2628
"import numpy as np\n",
2729
"from matplotlib import pyplot as plt\n",
2830
"from pymatgen.analysis.defects.ccd import HarmonicDefect, get_SRH_coefficient"
@@ -186,7 +188,7 @@
186188
"wswq_dir = TEST_FILES / \"ccd_0_-1\" / \"wswqs\"\n",
187189
"print(f\"The parsed WSWQ files are: {[f.name for f in wswq_dir.glob('WSWQ*')]}\")\n",
188190
"hd0.read_wswqs(TEST_FILES / \"ccd_0_-1\" / \"wswqs\")\n",
189-
"print(f\"Parsed {len(hd0.wswqs)} WSWQ files.\")\n"
191+
"print(f\"Parsed {len(hd0.wswqs)} WSWQ files.\")"
190192
]
191193
},
192194
{
@@ -205,11 +207,15 @@
205207
"metadata": {},
206208
"outputs": [],
207209
"source": [
208-
"print(f\"The automatically determined defect electronic states are: {hd0.defect_band} \"\n",
209-
" \"(with [band, k-point, spin] indexing).\")\n",
210+
"print(\n",
211+
" f\"The automatically determined defect electronic states are: {hd0.defect_band} \"\n",
212+
" \"(with [band, k-point, spin] indexing).\"\n",
213+
")\n",
210214
"epME = hd0.get_elph_me(defect_state=(138, 1, 1))\n",
211-
"print(f\"The resulting array of shape {epME.shape} contains the matrix elements from the \"\n",
212-
" \"defect state to all other states at the same k-point.\")\n"
215+
"print(\n",
216+
" f\"The resulting array of shape {epME.shape} contains the matrix elements from the \"\n",
217+
" \"defect state to all other states at the same k-point.\"\n",
218+
")"
213219
]
214220
},
215221
{
@@ -233,7 +239,7 @@
233239
"hd1 = HarmonicDefect.from_directories(\n",
234240
" directories=dirs10,\n",
235241
" store_bandstructure=True,\n",
236-
")\n"
242+
")"
237243
]
238244
},
239245
{
@@ -252,7 +258,9 @@
252258
"outputs": [],
253259
"source": [
254260
"T = np.linspace(100, 1000, 20)\n",
255-
"srh_c = get_SRH_coefficient(initial_state=hd0, final_state=hd1, defect_state=(138, 1, 1), T=T, dE=0.3)\n",
261+
"srh_c = get_SRH_coefficient(\n",
262+
" initial_state=hd0, final_state=hd1, defect_state=(138, 1, 1), T=T, dE=0.3\n",
263+
")\n",
256264
"plt.plot(T, srh_c)\n",
257265
"plt.xlabel(\"Temperature [K]\")\n",
258266
"plt.ylabel(\"SRH coefficient [cm$^{-3}$s$^{-1}$]\");"

docs/source/content/photo-conduct.ipynb

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
"outputs": [],
2626
"source": [
2727
"# disable tqdm progress bar\n",
28-
"from tqdm import tqdm\n",
2928
"from functools import partialmethod\n",
29+
"\n",
30+
"from tqdm import tqdm\n",
31+
"\n",
3032
"tqdm.__init__ = partialmethod(tqdm.__init__, disable=True)\n",
3133
"# disable warnings\n",
3234
"import warnings\n",
35+
"\n",
3336
"warnings.filterwarnings(\"ignore\", category=UserWarning)"
3437
]
3538
},
@@ -40,13 +43,15 @@
4043
"metadata": {},
4144
"outputs": [],
4245
"source": [
46+
"import bisect\n",
47+
"import collections\n",
4348
"from pathlib import Path\n",
44-
"from pymatgen.analysis.defects.ccd import HarmonicDefect, get_SRH_coefficient\n",
45-
"from pymatgen.io.vasp.optics import DielectricFunctionCalculator, Spin, Vasprun, Waveder\n",
49+
"\n",
4650
"import numpy as np\n",
4751
"from matplotlib import pyplot as plt\n",
48-
"import bisect\n",
49-
"import collections\n",
52+
"from pymatgen.analysis.defects.ccd import HarmonicDefect\n",
53+
"from pymatgen.io.vasp.optics import Spin, Waveder\n",
54+
"\n",
5055
"TEST_FILES = Path(\"../../../tests/test_files/v_Ga/\")"
5156
]
5257
},
@@ -96,37 +101,36 @@
96101
"metadata": {},
97102
"outputs": [],
98103
"source": [
99-
"\n",
100-
"def print_eigs(vr, bwind=5, defect_bands = ()):\n",
104+
"def print_eigs(vr, bwind=5, defect_bands=()):\n",
101105
" \"\"\"Print the eigenvalues in a small band window around the Fermi level.\n",
102106
"\n",
103107
" Args:\n",
104108
" vr (Vasprun): The Vasprun object.\n",
105109
" bwind (int): The number of bands above and below the Fermi level to print.\n",
106110
" defect_bands (list): A list of tuples of the form (band index, kpt index, spin index)\n",
107111
" \"\"\"\n",
108-
" def _get_spin_idx(spin):\n",
112+
"\n",
113+
" def _get_spin_idx(spin) -> int:\n",
109114
" if spin == Spin.up:\n",
110115
" return 0\n",
111116
" return 1\n",
117+
"\n",
112118
" occ = vr.eigenvalues[Spin.up][0, :, 1] * -1\n",
113-
" fermi_idx = bisect.bisect_left(occ, -0.5) \n",
119+
" fermi_idx = bisect.bisect_left(occ, -0.5)\n",
114120
" output = collections.defaultdict(list)\n",
115121
" for k, spin_eigs in vr.eigenvalues.items():\n",
116122
" spin_idx = _get_spin_idx(k)\n",
117123
" for kpt in range(spin_eigs.shape[0]):\n",
118124
" for ib in range(fermi_idx - bwind, fermi_idx + bwind):\n",
119125
" e, o = spin_eigs[kpt, ib, :]\n",
120126
" idx = (ib, kpt, spin_idx)\n",
121-
" if idx in defect_bands:\n",
122-
" e_out = f\"{e:7.4f}*\"\n",
123-
" else:\n",
124-
" e_out = f\"{e:8.5f}\"\n",
127+
" e_out = f\"{e:7.4f}*\" if idx in defect_bands else f\"{e:8.5f}\"\n",
125128
" output[(ib)].append(e_out)\n",
126129
" print(\"band s=0,k=0 s=0,k=1 s=1,k=0 s=1,k=1\")\n",
127130
" for ib, eigs in output.items():\n",
128131
" print(f\"{ib:3d} {' '.join(eigs)}\")\n",
129132
"\n",
133+
"\n",
130134
"print_eigs(vr=hd0.vrun, defect_bands=hd0.defect_band)"
131135
]
132136
},
@@ -177,7 +181,7 @@
177181
"metadata": {},
178182
"outputs": [],
179183
"source": [
180-
"energy, eps_vbm, eps_cbm = hd0.get_dielectric_function(idir=0,jdir=0)\n",
184+
"energy, eps_vbm, eps_cbm = hd0.get_dielectric_function(idir=0, jdir=0)\n",
181185
"# plotting\n",
182186
"plt.plot(energy, np.imag(eps_vbm), label=\"VBM\")\n",
183187
"plt.plot(energy, np.imag(eps_cbm), label=\"CBM\")\n",
@@ -216,15 +220,20 @@
216220
"metadata": {},
217221
"outputs": [],
218222
"source": [
219-
"from pymatgen.analysis.defects.plotting.optics import plot_optical_transitions\n",
220223
"import matplotlib as mpl\n",
224+
"from pymatgen.analysis.defects.plotting.optics import plot_optical_transitions\n",
225+
"\n",
221226
"fig, ax = plt.subplots()\n",
222-
"cm_ax = fig.add_axes([0.8,0.1,0.02,0.8])\n",
223-
"df_k0, cmap, norm = plot_optical_transitions(hd0, kpt_index=1, band_window=5, x0=3, ax=ax)\n",
224-
"df_k1, _, _ = plot_optical_transitions(hd0, kpt_index=0, band_window=5, x0=0, ax=ax, cmap=cmap, norm=norm)\n",
225-
"mpl.colorbar.ColorbarBase(cm_ax,cmap=cmap,norm=norm,orientation='vertical')\n",
226-
"ax.set_ylabel(\"Energy (eV)\");\n",
227-
"ax.set_xticks([0,3])\n",
227+
"cm_ax = fig.add_axes([0.8, 0.1, 0.02, 0.8])\n",
228+
"df_k0, cmap, norm = plot_optical_transitions(\n",
229+
" hd0, kpt_index=1, band_window=5, x0=3, ax=ax\n",
230+
")\n",
231+
"df_k1, _, _ = plot_optical_transitions(\n",
232+
" hd0, kpt_index=0, band_window=5, x0=0, ax=ax, cmap=cmap, norm=norm\n",
233+
")\n",
234+
"mpl.colorbar.ColorbarBase(cm_ax, cmap=cmap, norm=norm, orientation=\"vertical\")\n",
235+
"ax.set_ylabel(\"Energy (eV)\")\n",
236+
"ax.set_xticks([0, 3])\n",
228237
"ax.set_xticklabels([\"Kpoint-0\", \"Kpoint-1\"])"
229238
]
230239
},

0 commit comments

Comments
 (0)