From 5e3516ece8312d3443c60bd2bbc9c9a36a18c2c3 Mon Sep 17 00:00:00 2001 From: Kay Robbins <1189050+VisLab@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:16:06 -0600 Subject: [PATCH] Removed set_path parameter from word cloud creation --- .../operations/summarize_hed_tags_op.py | 11 +++++----- hed/tools/visualization/tag_word_cloud.py | 6 ++---- .../visualization/test_tag_word_cloud.py | 21 +++++-------------- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/hed/tools/remodeling/operations/summarize_hed_tags_op.py b/hed/tools/remodeling/operations/summarize_hed_tags_op.py index 36e19665..80d96f1d 100644 --- a/hed/tools/remodeling/operations/summarize_hed_tags_op.py +++ b/hed/tools/remodeling/operations/summarize_hed_tags_op.py @@ -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'), @@ -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. @@ -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')) diff --git a/hed/tools/visualization/tag_word_cloud.py b/hed/tools/visualization/tag_word_cloud.py index 59c586af..c934c389 100644 --- a/hed/tools/visualization/tag_word_cloud.py +++ b/hed/tools/visualization/tag_word_cloud.py @@ -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: @@ -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: @@ -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", "") diff --git a/tests/tools/visualization/test_tag_word_cloud.py b/tests/tools/visualization/test_tag_word_cloud.py index 8bf16e27..a148baf4 100644 --- a/tests/tools/visualization/test_tag_word_cloud.py +++ b/tests/tools/visualization/test_tag_word_cloud.py @@ -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}