Skip to content

Commit 534b7cc

Browse files
author
Aaron Meyer
committed
Switch SVG cartoon overlay from svgutils to drawsvg, migrate to uv
svgutils is unmaintained; drawsvg is actively maintained. overlayCartoon now composes the base figure and cartoon SVGs directly with drawsvg's Drawing/Group/Raw primitives instead of svgutils' transform API. Also migrates package management from poetry to uv: converts pyproject.toml to PEP 621 + a hatchling build backend, replaces poetry.lock with uv.lock, and updates the makefile and CI workflow to use `uv run`/`uv sync`. The tensordata dev dependency is pinned to the last commit before it required Python 3.14 (newer than this project's supported range). Fixes meyer-lab/lab-internals#68
1 parent ec999e9 commit 534b7cc

6 files changed

Lines changed: 910 additions & 1432 deletions

File tree

‎.github/workflows/pytest.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ jobs:
77
runs-on: self-hosted
88
steps:
99
- uses: actions/checkout@v2
10+
- name: Install uv
11+
uses: astral-sh/setup-uv@v10.0.1
1012
- name: Install dependencies
11-
run: poetry install --no-root
13+
run: uv sync
1214
- name: Test with pytest
1315
run: make test

‎makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
all: test
44

55
test:
6-
poetry run pytest -s -v -x
6+
uv run pytest -s -v -x
77

88
coverage.xml:
9-
poetry run pytest --junitxml=junit.xml --cov=tensorpack --cov-report xml:coverage.xml
9+
uv run pytest --junitxml=junit.xml --cov=tensorpack --cov-report xml:coverage.xml
1010

1111
clean:
1212
rm -rf coverage.xml

‎poetry.lock‎

Lines changed: 0 additions & 1393 deletions
This file was deleted.

‎pyproject.toml‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
1-
[tool.poetry]
1+
[project]
22
name = "tensorpack"
33
version = "0.1.2"
44
description = "A collection of tensor methods from the Meyer lab."
5-
authors = ["Cyrillus Tan <cyztan@gmail.com>"]
5+
authors = [{ name = "Cyrillus Tan", email = "cyztan@gmail.com" }]
66
license = "MIT"
7+
requires-python = ">=3.11,<3.13"
8+
dependencies = [
9+
"numpy>=1.21",
10+
"tensorly>=0.8",
11+
"matplotlib>=3.5.0",
12+
"tqdm>=4.62.3",
13+
"seaborn>=0.11.2",
14+
"drawsvg>=2.4",
15+
"xarray>=2024",
16+
"scikit-learn>=1.1.2",
17+
]
718

8-
[tool.poetry.dependencies]
9-
python = ">=3.11,<3.13"
10-
numpy = ">=1.21"
11-
tensorly = ">=0.8"
12-
matplotlib = ">=3.5.0"
13-
tqdm = ">=4.62.3"
14-
seaborn = ">=0.11.2"
15-
svgutils = "^0.3.4"
16-
xarray = "^2024"
17-
scikit-learn = "^1.1.2"
19+
[dependency-groups]
20+
dev = [
21+
"pytest>=7.0",
22+
"pytest-cov>=3.0.0",
23+
"tensordata",
24+
"pyright>=1.1.379",
25+
"ruff>=0.6.4",
26+
]
1827

19-
[tool.poetry.group.dev.dependencies]
20-
pytest = "^7.0"
21-
pytest-cov = "^3.0.0"
22-
tensordata = { git = "https://github.com/meyer-lab/tensordata.git", branch = "main" }
23-
pyright = "^1.1.379"
24-
ruff = "^0.6.4"
28+
[tool.uv.sources]
29+
# Pinned to the last commit before tensordata required Python 3.14, which is
30+
# newer than tensorpack's supported range (requires-python above).
31+
tensordata = { git = "https://github.com/meyer-lab/tensordata.git", rev = "7c52287" }
2532

2633
[build-system]
27-
requires = ["poetry-core>=1.0.0"]
28-
build-backend = "poetry.core.masonry.api"
34+
requires = ["hatchling"]
35+
build-backend = "hatchling.build"
36+
37+
[tool.hatch.build.targets.wheel]
38+
packages = ["tensorpack"]

‎tensorpack/test/common.py‎

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
"""
22
This file contains functions that are used in multiple figures.
33
"""
4-
import seaborn as sns
4+
5+
import xml.etree.ElementTree as ET
56
from string import ascii_lowercase
7+
8+
import drawsvg as draw
69
import matplotlib
7-
from matplotlib import gridspec, pyplot as plt
8-
import svgutils.transform as st
10+
import seaborn as sns
11+
from matplotlib import gridspec
12+
from matplotlib import pyplot as plt
13+
14+
ET.register_namespace("", "http://www.w3.org/2000/svg")
15+
ET.register_namespace("xlink", "http://www.w3.org/1999/xlink")
916

1017

1118
matplotlib.rcParams["legend.labelspacing"] = 0.2
@@ -22,8 +29,14 @@
2229

2330

2431
def getSetup(figsize, gridd, multz=None, empts=None):
25-
""" Establish figure set-up with subplots. """
26-
sns.set(style="whitegrid", font_scale=0.7, color_codes=True, palette="colorblind", rc={"grid.linestyle": "dotted", "axes.linewidth": 0.6})
32+
"""Establish figure set-up with subplots."""
33+
sns.set(
34+
style="whitegrid",
35+
font_scale=0.7,
36+
color_codes=True,
37+
palette="colorblind",
38+
rc={"grid.linestyle": "dotted", "axes.linewidth": 0.6},
39+
)
2740

2841
# create empty list if empts isn't specified
2942
if empts is None:
@@ -43,27 +56,52 @@ def getSetup(figsize, gridd, multz=None, empts=None):
4356
if x not in empts and x not in multz.keys(): # If this is just a normal subplot
4457
ax.append(f.add_subplot(gs1[x]))
4558
elif x in multz.keys(): # If this is a subplot that spans grid elements
46-
ax.append(f.add_subplot(gs1[x: x + multz[x] + 1]))
59+
ax.append(f.add_subplot(gs1[x : x + multz[x] + 1]))
4760
x += multz[x]
4861
x += 1
4962

5063
return (ax, f)
5164

5265

5366
def subplotLabel(axs):
54-
""" Place subplot labels on figure. """
67+
"""Place subplot labels on figure."""
5568
for ii, ax in enumerate(axs):
56-
ax.text(-0.2, 1.2, ascii_lowercase[ii], transform=ax.transAxes, fontsize=16, fontweight="bold", va="top")
69+
ax.text(
70+
-0.2,
71+
1.2,
72+
ascii_lowercase[ii],
73+
transform=ax.transAxes,
74+
fontsize=16,
75+
fontweight="bold",
76+
va="top",
77+
)
5778

5879

59-
def overlayCartoon(figFile, cartoonFile, x, y, scalee=1, scale_x=1, scale_y=1):
60-
""" Add cartoon to a figure file. """
80+
def _read_svg(path):
81+
"""Parse an SVG file, returning its root attributes and inner markup."""
82+
root = ET.parse(path).getroot()
83+
inner = "".join(ET.tostring(child, encoding="unicode") for child in root)
84+
return root.attrib, inner
6185

62-
# Overlay Figure cartoons
63-
template = st.fromfile(figFile)
64-
cartoon = st.fromfile(cartoonFile).getroot()
6586

66-
cartoon.moveto(x, y, scale_x=scalee * scale_x, scale_y=scalee * scale_y)
87+
def overlayCartoon(figFile, cartoonFile, x, y, scalee=1, scale_x=1, scale_y=1):
88+
"""Add cartoon to a figure file."""
6789

68-
template.append(cartoon)
69-
template.save(figFile)
90+
# Overlay Figure cartoons
91+
fig_attrib, fig_inner = _read_svg(figFile)
92+
_, cartoon_inner = _read_svg(cartoonFile)
93+
94+
d = draw.Drawing(
95+
fig_attrib.get("width"),
96+
fig_attrib.get("height"),
97+
viewBox=fig_attrib.get("viewBox"),
98+
)
99+
d.append(draw.Raw(fig_inner))
100+
101+
cartoon = draw.Group(
102+
transform=f"translate({x}, {y}) scale({scalee * scale_x} {scalee * scale_y})"
103+
)
104+
cartoon.append(draw.Raw(cartoon_inner))
105+
d.append(cartoon)
106+
107+
d.save_svg(figFile)

0 commit comments

Comments
 (0)