Skip to content

Commit f2c1189

Browse files
author
Aaron Meyer
committed
Switch SVG cartoon overlay from svgutils to drawsvg
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. Fixes meyer-lab/lab-internals#68
1 parent 891c82a commit f2c1189

3 files changed

Lines changed: 36 additions & 176 deletions

File tree

‎lineage/figures/common.py‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import math
88
import sys
99
import time
10+
import xml.etree.ElementTree as ET
1011
from string import ascii_lowercase
1112

13+
import drawsvg as draw
1214
import matplotlib
1315
import numpy as np
1416
import pandas as pd
1517
import seaborn as sns
16-
import svgutils.transform as st
1718
from matplotlib import gridspec, rcParams
1819
from matplotlib import pyplot as plt
1920

@@ -24,6 +25,9 @@
2425

2526
matplotlib.use("AGG")
2627

28+
ET.register_namespace("", "http://www.w3.org/2000/svg")
29+
ET.register_namespace("xlink", "http://www.w3.org/1999/xlink")
30+
2731

2832
# T: transition probability matrix
2933
T = np.array([[0.9, 0.1], [0.1, 0.9]], dtype=float)
@@ -130,19 +134,32 @@ def subplotLabel(axs):
130134
i += 1
131135

132136

137+
def _read_svg(path):
138+
"""Parse an SVG file, returning its root attributes and inner markup."""
139+
root = ET.parse(path).getroot()
140+
inner = "".join(ET.tostring(child, encoding="unicode") for child in root)
141+
return root.attrib, inner
142+
143+
133144
def overlayCartoon(figFile, cartoonFile, x, y, scalee=1, scale_x=1, scale_y=1, rotate=None):
134145
"""Add cartoon to a figure file."""
135146

136147
# Overlay Figure cartoons
137-
template = st.fromfile(figFile)
138-
cartoon = st.fromfile(cartoonFile).getroot()
148+
fig_attrib, fig_inner = _read_svg(figFile)
149+
_, cartoon_inner = _read_svg(cartoonFile)
139150

140-
cartoon.moveto(x, y, scale_x=scalee * scale_x, scale_y=scalee * scale_y)
151+
d = draw.Drawing(fig_attrib.get("width"), fig_attrib.get("height"), viewBox=fig_attrib.get("viewBox"))
152+
d.append(draw.Raw(fig_inner))
153+
154+
transform = f"translate({x}, {y}) scale({scalee * scale_x} {scalee * scale_y})"
141155
if rotate:
142-
cartoon.rotate(rotate, x, y)
156+
transform += f" rotate({rotate} {x} {y})"
157+
158+
cartoon = draw.Group(transform=transform)
159+
cartoon.append(draw.Raw(cartoon_inner))
160+
d.append(cartoon)
143161

144-
template.append(cartoon)
145-
template.save(figFile)
162+
d.save_svg(figFile)
146163

147164

148165
def genFigure():

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies = [
1818
"scipy",
1919
"scikit-learn",
2020
"seaborn",
21-
"svgutils",
21+
"drawsvg",
2222
"openpyxl",
2323
"biopython",
2424
"networkx",

0 commit comments

Comments
 (0)