Skip to content

Commit 5e3516e

Browse files
committed
Removed set_path parameter from word cloud creation
1 parent 9910f1b commit 5e3516e

3 files changed

Lines changed: 13 additions & 25 deletions

File tree

hed/tools/remodeling/operations/summarize_hed_tags_op.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ def __init__(self, parameters):
154154
"prefer_horizontal": wc_params.get("prefer_horizontal", 0.75),
155155
"min_font_size": wc_params.get("min_font_size", 8),
156156
"max_font_size": wc_params.get("max_font_size", 15),
157-
"set_font": wc_params.get("set_font", False),
158-
"font_path": wc_params.get("font_path", ""),
157+
"font_path": wc_params.get("font_path", None),
159158
"scale_adjustment": wc_params.get("scale_adjustment", 7),
160159
"contour_width": wc_params.get("contour_width", 3),
161160
"contour_color": wc_params.get("contour_color", 'black'),
@@ -164,8 +163,10 @@ def __init__(self, parameters):
164163
"mask_path": wc_params.get("mask_path", None)
165164
}
166165
if self.word_cloud["use_mask"] and not self.word_cloud["mask_path"]:
167-
self.word_cloud["mask_path"] = os.path.realpath(os.path.join(os.path.dirname(__file__),
168-
'../../../resources/word_cloud_brain_mask.png'))
166+
self.word_cloud["mask_path"] = os.path.realpath(
167+
os.path.join(os.path.dirname(__file__), '../../../resources/word_cloud_brain_mask.png'))
168+
if self.word_cloud["font_path"]:
169+
self.word_cloud["font_path"] = os.path.realpath(self.word_cloud["font_path"])
169170

170171
def do_op(self, dispatcher, df, name, sidecar=None):
171172
""" Summarize the HED tags present in the dataset.
@@ -314,7 +315,7 @@ def save_visualizations(self, save_dir, file_formats=['.svg'], individual_summar
314315
prefer_horizontal=wc["prefer_horizontal"], background_color=wc["background_color"],
315316
min_font_size=wc["min_font_size"], max_font_size=wc["max_font_size"],
316317
contour_width=wc["contour_width"], contour_color=wc["contour_color"],
317-
set_font=wc["set_font"], font_path=wc["font_path"])
318+
font_path=wc["font_path"])
318319
svg_data = word_cloud_to_svg(tag_wc)
319320
cloud_filename = os.path.realpath(os.path.join(save_dir, self.sum_op.summary_name,
320321
self.sum_op.summary_name + '_word_cloud.svg'))

hed/tools/visualization/tag_word_cloud.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import matplotlib.font_manager as fm
88

99

10-
def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400, height=300, set_font=False, **kwargs):
10+
def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400, height=300, **kwargs):
1111
""" Takes a word dict and returns a generated word cloud object.
1212
1313
Parameters:
@@ -16,8 +16,6 @@ def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400
1616
background_color (str or None): If None, transparent background.
1717
width (int): width in pixels.
1818
height (int): height in pixels.
19-
font_path (str): a filename or font name to use. Assumed to be a full file path if it ends with .ttf or .otf.
20-
Font names will use a default if a close enough match isn't found.
2119
kwargs (kwargs): Any other parameters WordCloud accepts, overrides default values where relevant.
2220
2321
Returns:
@@ -46,7 +44,7 @@ def create_wordcloud(word_dict, mask_path=None, background_color=None, width=400
4644
kwargs.setdefault('relative_scaling', 1)
4745
kwargs.setdefault('max_font_size', height / 20)
4846
kwargs.setdefault('min_font_size', 8)
49-
if not set_font or 'font_path' not in kwargs:
47+
if 'font_path' not in kwargs:
5048
kwargs['font_path'] = None
5149
elif kwargs['font_path'] and not kwargs['font_path'].endswith((".ttf", ".otf", ".TTF", ".OTF")):
5250
raise HedFileError("InvalidFontPath", f"Font {kwargs['font_path']} not valid on this system", "")

tests/tools/visualization/test_tag_word_cloud.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,21 @@ def test_create_wordcloud(self):
2525
self.assertEqual(wc.width, width)
2626
self.assertEqual(wc.height, height)
2727

28-
def test_create_wordcloud_font(self):
29-
word_dict = {'tag1': 5, 'tag2': 3, 'tag3': 7}
30-
width = 400
31-
height = 200
32-
wc = tag_word_cloud.create_wordcloud(word_dict, width=width, height=height, font_path="Serif")
33-
34-
self.assertIsInstance(wc, wordcloud.WordCloud)
35-
self.assertEqual(wc.width, width)
36-
self.assertEqual(wc.height, height)
37-
self.assertIn("Serif", wc.font_path)
38-
3928
def test_create_wordcloud_font_direct(self):
4029
word_dict = {'tag1': 5, 'tag2': 3, 'tag3': 7}
4130
width = 400
4231
height = 200
4332

4433
fonts = fm.findSystemFonts()
45-
first_font = fonts[0]
46-
x = '/C/Windows/Fonts/timesi.ttf'
47-
#y = 'C:\\Windows\\Fonts\\arialbd.ttf'
48-
wc = tag_word_cloud.create_wordcloud(word_dict, width=width, height=height, font_path=first_font)
34+
if not fonts:
35+
return
36+
font_path = os.path.realpath(fonts[0])
37+
wc = tag_word_cloud.create_wordcloud(word_dict, width=width, height=height, font_path=font_path)
4938

5039
self.assertIsInstance(wc, wordcloud.WordCloud)
5140
self.assertEqual(wc.width, width)
5241
self.assertEqual(wc.height, height)
53-
self.assertIn(first_font, wc.font_path)
42+
self.assertIn(font_path, wc.font_path)
5443

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

0 commit comments

Comments
 (0)