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

Allow adjust size/color of svg contour in wordcloud #863

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion hed/models/string_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def gather_descriptions(hed_string):
description(str): The concatenated values of all description tags.

Side effect:
The input HedString has its Definition tags removed.
The input HedString has its description tags removed.

"""
desc_tags = hed_string.find_tags("description", recursive=True, include_groups=0)
Expand Down
9 changes: 5 additions & 4 deletions hed/tools/visualization/word_cloud_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def generate_contour_svg(wc, width, height):
contour = _get_contour_mask(wc, width, height)
if contour is None:
return ""
return _numpy_to_svg(contour)
return _numpy_to_svg(contour, radius=wc.contour_width, color=wc.contour_color)


def _get_contour_mask(wc, width, height):
Expand Down Expand Up @@ -91,19 +91,20 @@ def _draw_contour(wc, img: Image):
WordCloud._draw_contour = _draw_contour


def _numpy_to_svg(contour):
def _numpy_to_svg(contour, radius=1, color="black"):
""" Convert a numpy array to SVG.

Parameters:
contour (np.Array): Image to be converted.

radius (float): The radius of the contour to draw.
color(string): the color to draw it as, e.g. "red"
Returns:
str: The SVG representation.
"""
svg_elements = []
points = np.array(contour.nonzero()).T
for y, x in points:
svg_elements.append(f'<circle cx="{x}" cy="{y}" r="1" stroke="black" fill="black" />')
svg_elements.append(f'<circle cx="{x}" cy="{y}" r="{radius}" stroke="{color}" fill="{color}" />')

return '\n'.join(svg_elements)

Expand Down