Skip to content

Commit

Permalink
Group layer elements (#45)
Browse files Browse the repository at this point in the history
Closes #42

- Elements from a single layer are grouped in `<g>`
- Change layer-to-xml functions to return elements rather than lists
  • Loading branch information
jni authored Jun 20, 2024
1 parent f1e125c commit dfd90a5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions napari_svg/hook_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def writer(path, layer_data):
full_extrema = None
for ld in layer_data:
function_string = ld[2] + '_to_xml(ld[0], ld[1])'
xml_list, extrema = eval(function_string)
full_xml_list += xml_list
layer_xml, extrema = eval(function_string)
full_xml_list.append(layer_xml)
if full_extrema is None:
full_extrema = extrema
else:
Expand Down Expand Up @@ -140,10 +140,10 @@ def napari_write_image(path, data, meta):
return None

# Generate xml list and data extrema
xml_list, extrema = image_to_xml(data, meta)
layer_xml, extrema = image_to_xml(data, meta)

# Generate svg string
svg = xml_to_svg(xml_list, extrema=extrema)
svg = xml_to_svg(layer_xml, extrema=extrema)

# Write svg string
with open(path, 'w') as file:
Expand Down Expand Up @@ -183,10 +183,10 @@ def napari_write_labels(path, data, meta):
return None

# Generate xml list and data extrema
xml_list, extrema = labels_to_xml(data, meta)
layer_xml, extrema = labels_to_xml(data, meta)

# Generate svg string
svg = xml_to_svg(xml_list, extrema=extrema)
svg = xml_to_svg([layer_xml], extrema=extrema)

# Write svg string
with open(path, 'w') as file:
Expand Down Expand Up @@ -225,10 +225,10 @@ def napari_write_points(path, data, meta):
return None

# Generate xml list and data extrema
xml_list, extrema = points_to_xml(data, meta)
layer_xml, extrema = points_to_xml(data, meta)

# Generate svg string
svg = xml_to_svg(xml_list, extrema=extrema)
svg = xml_to_svg([layer_xml], extrema=extrema)

# Write svg string
with open(path, 'w') as file:
Expand Down Expand Up @@ -266,10 +266,10 @@ def napari_write_shapes(path, data, meta):
return None

# Generate xml list and data extrema
xml_list, extrema = shapes_to_xml(data, meta)
layer_xml, extrema = shapes_to_xml(data, meta)

# Generate svg string
svg = xml_to_svg(xml_list, extrema=extrema)
svg = xml_to_svg([layer_xml], extrema=extrema)

# Write svg string
with open(path, 'w') as file:
Expand Down Expand Up @@ -307,10 +307,10 @@ def napari_write_vectors(path, data, meta):
return None

# Generate xml list and data extrema
xml_list, extrema = vectors_to_xml(data, meta)
layer_xml, extrema = vectors_to_xml(data, meta)

# Generate svg string
svg = xml_to_svg(xml_list, extrema=extrema)
svg = xml_to_svg([layer_xml], extrema=extrema)

# Write svg string
with open(path, 'w') as file:
Expand Down
48 changes: 23 additions & 25 deletions napari_svg/layer_to_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def image_to_xml(data, meta):
Returns
-------
xml_list : list of xml.etree.ElementTree.Element
List of a single xml element specifying the image as a png according to
layer_xml : xml.etree.ElementTree.Element
Single xml element specifying the image as a png according to
the svg specification.
extrema : array (2, 2)
Extrema of data, specified as a minumum then maximum of the (x, y)
Expand Down Expand Up @@ -208,17 +208,16 @@ def image_to_xml(data, meta):

transform = layer_transforms_to_xml_string(meta)

xml = Element(
layer_xml = Element(
'image',
width=width,
height=height,
opacity=str(opacity),
transform=transform,
**props,
)
xml_list = [xml]

return xml_list, extrema
return layer_xml, extrema


def extrema_points(data, meta):
Expand Down Expand Up @@ -249,9 +248,9 @@ def points_to_xml(data, meta):
Returns
-------
xml_list : list of xml.etree.ElementTree.Element
List of xml elements defining each point according to the
svg specification
layer_xml : xml.etree.ElementTree.Element
XML group element containing each point according to the
svg specification.
extrema : array (2, 2)
Extrema of data, specified as a minumum then maximum of the (x, y)
coordinates.
Expand Down Expand Up @@ -307,8 +306,8 @@ def points_to_xml(data, meta):
stroke_width *= size

transform = layer_transforms_to_xml_string(meta)
layer_xml = Element('g', transform=transform)

xml_list = []
for p, s, fc, sc, sw in zip(points, size, face_color, stroke_color, stroke_width):
cx = str(p[1])
cy = str(p[0])
Expand All @@ -326,12 +325,11 @@ def points_to_xml(data, meta):
cx=cx, cy=cy, r=r,
stroke=stroke,
fill=fill,
transform=transform,
**props,
)
xml_list.append(element)
layer_xml.append(element)

return xml_list, extrema
return layer_xml, extrema


def extrema_shapes(shapes_data, meta):
Expand All @@ -355,8 +353,8 @@ def shapes_to_xml(data, meta):
Returns
-------
xml_list : list of xml.etree.ElementTree.Element
List of xml elements defining each shapes according to the
layer_xml : xml.etree.ElementTree.Element
XML group element containing each shape according to the
svg specification
extrema : array (2, 2)
Extrema of data, specified as a minumum then maximum of the (x, y)
Expand Down Expand Up @@ -404,13 +402,13 @@ def shapes_to_xml(data, meta):
extrema = np.full((2, 2), np.nan)

transform = layer_transforms_to_xml_string(meta)
layer_xml = Element('g', transform=transform)
raw_xml_list = []
zipped = zip(shapes, shape_type, face_color, edge_color, edge_width)
for s, st, fc, ec, ew in zipped:
props = {
'stroke-width': str(ew),
'opacity': str(opacity),
'transform': transform,
}
fc_int = (255 * fc).astype(int)
props['fill'] = f'rgb{tuple(map(int, fc_int[:3]))}'
Expand All @@ -421,9 +419,9 @@ def shapes_to_xml(data, meta):
raw_xml_list.append(element)

# reorder according to z-index
z_order = np.argsort(z_index)
xml_list = [raw_xml_list[i] for i in z_order]
return xml_list, extrema
for i in np.argsort(z_index):
layer_xml.append(raw_xml_list[i])
return layer_xml, extrema


def extrema_vectors(vectors, meta):
Expand Down Expand Up @@ -455,8 +453,8 @@ def vectors_to_xml(data, meta):
Returns
-------
xml_list : list of xml.etree.ElementTree.Element
List of xml elements defining each vector as a line according to the
layer_xml : xml.etree.ElementTree.Element
XML group element containing each vector as a line according to the
svg specification
extrema : array (2, 2)
Extrema of data, specified as a minumum then maximum of the (x, y)
Expand Down Expand Up @@ -494,13 +492,12 @@ def vectors_to_xml(data, meta):
extrema = extrema_vectors(vectors, meta)

transform = layer_transforms_to_xml_string(meta)
layer_xml = Element('g', transform=transform)

props = {
'stroke-width': str(edge_width),
'opacity': str(opacity),
'transform': transform,
}

xml_list = []
for v, ec in zip(vectors, edge_color):
x1 = str(v[0, -2])
y1 = str(v[0, -1])
Expand All @@ -510,6 +507,7 @@ def vectors_to_xml(data, meta):
stroke = f'rgb{tuple(map(int, ec_int[:3]))}'
props['stroke'] = stroke
element = Element('line', x1=y1, y1=x1, x2=y2, y2=x2, **props)
xml_list.append(element)
layer_xml.append(element)


return xml_list, extrema
return layer_xml, extrema

0 comments on commit dfd90a5

Please sign in to comment.