From f0f5dbe92ebb07e6ca7c55d5ac5e00d4b50e2752 Mon Sep 17 00:00:00 2001 From: Ali-Tehrani Date: Thu, 3 Aug 2023 11:49:43 -0400 Subject: [PATCH] Update import-resources API, fixing #173 (#176) * Update clenshaw in website table * Fix #173 by updating import-resources API This arises from pull-request #174. Rather than using an older version of import-resources. This fixed the issue to the current API * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- doc/table_onedgrids.csv | 2 +- src/grid/angular.py | 5 ++--- src/grid/atomgrid.py | 11 +++++------ src/grid/hirshfeld.py | 5 ++--- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/doc/table_onedgrids.csv b/doc/table_onedgrids.csv index 491a242a..91f885f8 100644 --- a/doc/table_onedgrids.csv +++ b/doc/table_onedgrids.csv @@ -8,6 +8,6 @@ Grid;Domain;:math:`x_i`;:math:`w_i` :func:`Tanh Sinh`;:math:`[-1,1]`;:math:`\tanh\left( \frac{\pi}{2} \sinh(i\delta) \right)`;:math:`\frac{\frac{\pi}{2}\delta \cosh(i\delta)}{\cosh^2(\frac{\pi}{2}\sinh(i\delta))}` :func:`Simpson`;:math:`[-1,1]`;:math:`-1 + 2 \left(\frac{i-1}{N_{pts}-1}\right)`;:math:`w_i = 2 / (3(N - 1)) \text{ if } i = 0, \quad 8 / (3(N - 1)) \text{ if } i \geq 1 \text{ and is odd}, \quad 4 / (3(N - 1)) \text{ if } i \geq 2 \text{ and is even}.` :func:`MidPoint`;:math:`[-1,1]`;:math:`-1 + \frac{2i + 1}{N_{pts}}`;:math:`\frac{2}{n}` -:func:`Clenshow-Curtis`;:math:`[-1,1]`;:math:`\cos (\pi (i - 1) / (N_{pts} - 1))`;:math:`w_i = \frac{c_k}{n} \bigg(1 - \sum_{j=1}^{\lfloor n/2 \rfloor} \frac{b_j}{4j^2 - 1} \cos(2j\theta_i) \bigg), \quad b_j = 1 \text{ if } j = n/2, \quad 2 \text{ if } j < n/2, \quad c_j = 1 \text{ if } k \in \{0, n\}, \quad 2 \text{ else}` +:func:`Clenshaw-Curtis`;:math:`[-1,1]`;:math:`\cos (\pi (i - 1) / (N_{pts} - 1))`;:math:`w_i = \frac{c_k}{n} \bigg(1 - \sum_{j=1}^{\lfloor n/2 \rfloor} \frac{b_j}{4j^2 - 1} \cos(2j\theta_i) \bigg), \quad b_j = 1 \text{ if } j = n/2, \quad 2 \text{ if } j < n/2, \quad c_j = 1 \text{ if } k \in \{0, n\}, \quad 2 \text{ else}` :func:`Fejer First`;:math:`(-1,1)`;:math:`\cos\bigg(\frac{(2i - 1)\pi}{2N_{pts}}\bigg)`;:math:`\frac{2}{n}\bigg(1 - 2 \sum_{j=1}^{\lfloor n/2 \rfloor} \frac{\cos(2j \theta_j)}{4 j^2 - 1} \bigg)` :func:`Fejer Second`;:math:`(-1,1)`;:math:`\cos(i \pi / N_{pts})`;:math:`\frac{4 \sin(\theta_i)}{n} \sum_{j=1}^{\lfloor n/2 \rfloor} \frac{\sin(2j - 1)\theta_i}{2j - 1}` diff --git a/src/grid/angular.py b/src/grid/angular.py index 211e4cef..845a3d05 100644 --- a/src/grid/angular.py +++ b/src/grid/angular.py @@ -68,7 +68,7 @@ from bisect import bisect_left import numpy as np -from importlib_resources import path +from importlib_resources import files from grid.basegrid import Grid @@ -554,8 +554,7 @@ def _load_precomputed_angular_grid(degree: int, size: int, use_spherical: bool): raise ValueError(f"Given size={size} is not supported, choose from {npoints}") # load npz file corresponding to the given degree & size filename = f"{type}_{degree}_{size}.npz" - with path(file_path, filename) as npz_file: - data = np.load(npz_file) + data = np.load(files(file_path).joinpath(filename)) if len(data["weights"]) == 1: return data["points"], np.ones(len(data["points"])) * data["weights"] return data["points"], data["weights"] diff --git a/src/grid/atomgrid.py b/src/grid/atomgrid.py index 152517f5..4c5dea5d 100644 --- a/src/grid/atomgrid.py +++ b/src/grid/atomgrid.py @@ -22,7 +22,7 @@ from typing import Union import numpy as np -from importlib_resources import path +from importlib_resources import files from scipy.interpolate import CubicSpline from scipy.spatial.transform import Rotation as R @@ -173,11 +173,10 @@ def from_preset( center = np.zeros(3, dtype=float) if center is None else np.asarray(center, dtype=float) cls._input_type_check(rgrid, center) # load radial points and - with path("grid.data.prune_grid", f"prune_grid_{preset}.npz") as npz_file: - data = np.load(npz_file) - # load predefined_radial sectors and num_of_points in each sectors - rad = data[f"{atnum}_rad"] - npt = data[f"{atnum}_npt"] + data = np.load(files("grid.data.prune_grid").joinpath(f"prune_grid_{preset}.npz")) + # load predefined_radial sectors and num_of_points in each sectors + rad = data[f"{atnum}_rad"] + npt = data[f"{atnum}_npt"] degs = AngularGrid.convert_angular_sizes_to_degrees(npt, use_spherical) rad_degs = AtomGrid._find_l_for_rad_list(rgrid.points, rad, degs) diff --git a/src/grid/hirshfeld.py b/src/grid/hirshfeld.py index 60c15397..d1ad0096 100644 --- a/src/grid/hirshfeld.py +++ b/src/grid/hirshfeld.py @@ -21,7 +21,7 @@ import numpy as np -from importlib_resources import path +from importlib_resources import files from scipy.interpolate import CubicSpline @@ -34,8 +34,7 @@ def __init__(self): @staticmethod def _load_npz_proatom(num): """Return radial grid points and neutral density for a given atomic number.""" - with path("grid.data.proatoms", f"a{num:03d}.npz") as fname: - data = np.load(fname) + data = np.load(files("grid.data.proatoms").joinpath(f"a{num:03d}.npz")) return data["r"], data["dn"] @staticmethod