Skip to content
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
11 changes: 6 additions & 5 deletions hed/tools/remodeling/operations/summarize_hed_tags_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ def __init__(self, parameters):
"prefer_horizontal": wc_params.get("prefer_horizontal", 0.75),
"min_font_size": wc_params.get("min_font_size", 8),
"max_font_size": wc_params.get("max_font_size", 15),
"set_font": wc_params.get("set_font", False),
"font_path": wc_params.get("font_path", ""),
"font_path": wc_params.get("font_path", None),
"scale_adjustment": wc_params.get("scale_adjustment", 7),
"contour_width": wc_params.get("contour_width", 3),
"contour_color": wc_params.get("contour_color", 'black'),
Expand All @@ -164,8 +163,10 @@ def __init__(self, parameters):
"mask_path": wc_params.get("mask_path", None)
}
if self.word_cloud["use_mask"] and not self.word_cloud["mask_path"]:
self.word_cloud["mask_path"] = os.path.realpath(os.path.join(os.path.dirname(__file__),
'../../../resources/word_cloud_brain_mask.png'))
self.word_cloud["mask_path"] = os.path.realpath(
os.path.join(os.path.dirname(__file__), '../../../resources/word_cloud_brain_mask.png'))
if self.word_cloud["font_path"]:
self.word_cloud["font_path"] = os.path.realpath(self.word_cloud["font_path"])

def do_op(self, dispatcher, df, name, sidecar=None):
""" Summarize the HED tags present in the dataset.
Expand Down Expand Up @@ -314,7 +315,7 @@ def save_visualizations(self, save_dir, file_formats=['.svg'], individual_summar
prefer_horizontal=wc["prefer_horizontal"], background_color=wc["background_color"],
min_font_size=wc["min_font_size"], max_font_size=wc["max_font_size"],
contour_width=wc["contour_width"], contour_color=wc["contour_color"],
set_font=wc["set_font"], font_path=wc["font_path"])
font_path=wc["font_path"])
svg_data = word_cloud_to_svg(tag_wc)
cloud_filename = os.path.realpath(os.path.join(save_dir, self.sum_op.summary_name,
self.sum_op.summary_name + '_word_cloud.svg'))
Expand Down
6 changes: 2 additions & 4 deletions hed/tools/visualization/tag_word_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import matplotlib.font_manager as fm


def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400, height=300, set_font=False, **kwargs):
def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400, height=300, **kwargs):
""" Takes a word dict and returns a generated word cloud object.

Parameters:
Expand All @@ -16,8 +16,6 @@ def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400
background_color (str or None): If None, transparent background.
width (int): width in pixels.
height (int): height in pixels.
font_path (str): a filename or font name to use. Assumed to be a full file path if it ends with .ttf or .otf.
Font names will use a default if a close enough match isn't found.
kwargs (kwargs): Any other parameters WordCloud accepts, overrides default values where relevant.

Returns:
Expand Down Expand Up @@ -46,7 +44,7 @@ def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400
kwargs.setdefault('relative_scaling', 1)
kwargs.setdefault('max_font_size', height / 20)
kwargs.setdefault('min_font_size', 8)
if not set_font or 'font_path' not in kwargs:
if 'font_path' not in kwargs:
kwargs['font_path'] = None
elif kwargs['font_path'] and not kwargs['font_path'].endswith((".ttf", ".otf", ".TTF", ".OTF")):
raise HedFileError("InvalidFontPath", f"Font {kwargs['font_path']} not valid on this system", "")
Expand Down
21 changes: 5 additions & 16 deletions tests/tools/visualization/test_tag_word_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,21 @@ def test_create_wordcloud(self):
self.assertEqual(wc.width, width)
self.assertEqual(wc.height, height)

def test_create_wordcloud_font(self):
word_dict = {'tag1': 5, 'tag2': 3, 'tag3': 7}
width = 400
height = 200
wc = tag_word_cloud.create_wordcloud(word_dict, width=width, height=height, font_path="Serif")

self.assertIsInstance(wc, wordcloud.WordCloud)
self.assertEqual(wc.width, width)
self.assertEqual(wc.height, height)
self.assertIn("Serif", wc.font_path)

def test_create_wordcloud_font_direct(self):
word_dict = {'tag1': 5, 'tag2': 3, 'tag3': 7}
width = 400
height = 200

fonts = fm.findSystemFonts()
first_font = fonts[0]
x = '/C/Windows/Fonts/timesi.ttf'
#y = 'C:\\Windows\\Fonts\\arialbd.ttf'
wc = tag_word_cloud.create_wordcloud(word_dict, width=width, height=height, font_path=first_font)
if not fonts:
return
font_path = os.path.realpath(fonts[0])
wc = tag_word_cloud.create_wordcloud(word_dict, width=width, height=height, font_path=font_path)

self.assertIsInstance(wc, wordcloud.WordCloud)
self.assertEqual(wc.width, width)
self.assertEqual(wc.height, height)
self.assertIn(first_font, wc.font_path)
self.assertIn(font_path, wc.font_path)

def test_create_wordcloud_default_params(self):
word_dict = {'tag1': 5, 'tag2': 3, 'tag3': 7}
Expand Down