Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy 2.0 support #205

Merged
merged 8 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -U \
matplotlib \
scipy \
rectpack \
klayout \
freetype-py \
pytest
pip install -e .
pip install -e '.[test]'
- name: Run test
uses: GabrielBB/xvfb-action@v1
with:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ If you found PHIDL useful, please consider citing it in (just one!) of your publ

# Installation / requirements
- Install or upgrade with `pip install -U phidl`
- Install with `pip install -U phidl[all]` to include optional dependencies (e.g. freetype-py, klayout, rectpack)
- Python version >=3.6

## Testing
- Install with test dependencies with `pip install -U phidl[test]` (includes `all` extras as well)
- Run tests with `pytest` (or `python -m pytest`)

# About PHIDL

Expand Down
5 changes: 3 additions & 2 deletions phidl/device_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ def _line_distances(points, start, end):
return np.linalg.norm(points - start, axis=1)

vec = end - start
cross = np.cross(vec, start - points)
vec2 = start - points
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
cross = vec[..., 0] * vec2[..., 1] - vec[..., 1] * vec2[..., 0]
return np.divide(abs(cross), np.linalg.norm(vec))


Expand Down Expand Up @@ -2728,7 +2729,7 @@ def append(self, path):
and np.issubdtype(np.array(path).dtype, np.number)
and (np.shape(path)[1] == 2)
):
points = np.asfarray(path)
points = np.asarray(path, dtype=np.float64)
nx1, ny1 = points[1] - points[0]
start_angle = np.arctan2(ny1, nx1) / np.pi * 180
nx2, ny2 = points[-1] - points[-2]
Expand Down
6 changes: 3 additions & 3 deletions phidl/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ def _merge_floating_point_errors(polygons, tol=1e-10):
Set of corrected polygons.
"""
if len(polygons) == 0:
return np.asfarray([])
return np.asarray([], dtype=np.float64)
stacked_polygons = np.vstack(polygons)
x = stacked_polygons[:, 0]
y = stacked_polygons[:, 1]
Expand Down Expand Up @@ -1488,7 +1488,7 @@ def _kl_region_to_device(kl_region, layer, name, precision):
for polygon in kl_region.each():
polygon = polygon.to_simple_polygon()
points = _kl_polygon_to_array(polygon)
points = np.asfarray(points) * precision
points = np.asarray(points, dtype=np.float64) * precision
D.add_polygon(points, layer=layer)
return D

Expand Down Expand Up @@ -2225,7 +2225,7 @@ def import_gds(filename, cellname=None, flatten=False):
rotation = 0
l = D.add_label(
text=label.text,
position=np.asfarray(label.position),
position=np.asarray(label.position, dtype=np.float64),
magnification=label.magnification,
rotation=rotation * 180 / np.pi,
layer=(label.layer, label.texttype),
Expand Down
10 changes: 6 additions & 4 deletions phidl/path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

import numpy as np

from phidl.device_layout import CrossSection, Path, _rotate_points
Expand Down Expand Up @@ -74,8 +76,8 @@ def _fresnel(R0, s, num_pts, n_iter=8):
y = np.zeros(num_pts)

for n in range(0, n_iter):
x += (-1) ** n * t ** (4 * n + 1) / (np.math.factorial(2 * n) * (4 * n + 1))
y += (-1) ** n * t ** (4 * n + 3) / (np.math.factorial(2 * n + 1) * (4 * n + 3))
x += (-1) ** n * t ** (4 * n + 1) / (math.factorial(2 * n) * (4 * n + 1))
y += (-1) ** n * t ** (4 * n + 3) / (math.factorial(2 * n + 1) * (4 * n + 3))

return np.array([np.sqrt(2) * R0 * x, np.sqrt(2) * R0 * y])

Expand Down Expand Up @@ -142,7 +144,7 @@ def euler(radius=3, angle=90, p=1.0, use_eff=False, num_pts=720):
dx = xp - Rp * np.sin(p * alpha / 2)
dy = yp - Rp * (1 - np.cos(p * alpha / 2))
else:
xbend1 = ybend1 = np.asfarray([])
xbend1 = ybend1 = np.array([], dtype=np.float64)
dx = 0
dy = 0

Expand Down Expand Up @@ -293,7 +295,7 @@ def spiral(num_turns=5, gap=1, inner_gap=2, num_pts=10000):


def _compute_segments(points):
points = np.asfarray(points)
points = np.asarray(points, dtype=np.float64)
normals = np.diff(points, axis=0)
normals = (normals.T / np.linalg.norm(normals, axis=1)).T
dx = np.diff(points[:, 0])
Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
six
gdspy
klayout
numpy
matplotlib
gdspy
rectpack
six
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
line-length = 88

[lint]
select = ["C", "E", "F", "B", "B9", "UP", "I"]
select = ["C", "E", "F", "B", "B9", "UP", "I", "NPY201"]
ignore = [
"E203",
"E266",
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"matplotlib",
]

extras_require = {}
extras_require["all"] = ["freetype-py", "klayout", "rectpack", "scipy"]
extras_require["test"] = extras_require["all"] + ["pytest"]

# read the contents of your README file

this_directory = path.abspath(path.dirname(__file__))
Expand All @@ -24,6 +28,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=install_requires,
extras_require=extras_require,
author="Adam McCaughan",
author_email="[email protected]",
packages=["phidl"],
Expand Down
10 changes: 6 additions & 4 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ def test_text():


def test_truetype():
_ = pytest.importorskip(
"phidl.font", reason="Testing of ttf/otf fonts requires the freetype package."
pytest.importorskip(
"phidl.font", reason="Testing of ttf/otf fonts requires the freetype package.",
exc_type=ImportError,
)
from os import path

Expand All @@ -229,8 +230,9 @@ def test_truetype():

@pytest.mark.skipif(sys.version_info < (3, 0), reason="unicode test requires python3")
def test_unicode():
_ = pytest.importorskip(
"phidl.font", reason="Testing of ttf/otf fonts requires the freetype package."
pytest.importorskip(
"phidl.font", reason="Testing of ttf/otf fonts requires the freetype package.",
exc_type=ImportError,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could go in a separate PR, and this might not be the correct solution.
https://docs.pytest.org/en/8.2.x/deprecations.html#import-or-skip-import-error

)
from os import path

Expand Down
Loading