Skip to content

Commit

Permalink
Add toggle_labels() action in CorrelogramView
Browse files Browse the repository at this point in the history
  • Loading branch information
rossant committed Jul 13, 2019
1 parent 5e008d3 commit 2443778
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ docs/_build
*.code-workspace
msdfgen
*.sh
tools/*.png
tools/*.ttf
15 changes: 13 additions & 2 deletions phy/cluster/views/correlogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from phy.plot.transform import Scale
from phy.plot.visuals import HistogramVisual, LineVisual, TextVisual
from phylib.utils import Bunch
from phylib.utils.color import selected_cluster_color
from phylib.utils.color import selected_cluster_color, _override_hsv, add_alpha
from .base import ManualClusteringView, ScalingMixin

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -120,7 +120,9 @@ def get_clusters_data(self, load_all=None):
b.firing_rate = fr[i, j] if fr is not None else None
b.data_bounds = (0, 0, n_bins, m)
b.pair_index = i, j
b.color = selected_cluster_color(i, 1) if i == j else (1, 1, 1, 1)
b.color = selected_cluster_color(i, 1)
if i != j:
b.color = add_alpha(_override_hsv(b.color[:3], s=.1, v=1))
bunchs.append(b)
return bunchs

Expand Down Expand Up @@ -211,11 +213,20 @@ def toggle_normalization(self, checked):
self.uniform_normalization = checked
self.plot()

def toggle_labels(self, checked):
"""Show or hide all labels."""
if checked:
self.text_visual.show()
else:
self.text_visual.hide()
self.canvas.update()

def attach(self, gui):
"""Attach the view to the GUI."""
super(CorrelogramView, self).attach(gui)

self.actions.add(self.toggle_normalization, shortcut='n', checkable=True)
self.actions.add(self.toggle_labels, checkable=True, checked=True)
self.actions.separator()

self.actions.add(
Expand Down
2 changes: 2 additions & 0 deletions phy/cluster/views/tests/test_correlogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def get_firing_rate(cluster_ids, bin_size):
v.on_select(cluster_ids=[0, 2])

v.toggle_normalization(True)
v.toggle_labels(False)
v.toggle_labels(True)

v.set_bin(1)
v.set_window(100)
Expand Down
2 changes: 2 additions & 0 deletions phy/plot/visuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ class TextVisual(BaseVisual):
-----------
color : 4-tuple
font_size : float
The font size, in points (8 by default).
Parameters
----------
Expand Down
21 changes: 17 additions & 4 deletions tools/makefont.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Create a multi-channel signed distance field map.
Use https://github.com/Chlumsky/msdfgen/
You need in this directory:
* msdfgen executable
* SourceCodePro-Regular.ttf
Just run this script to (re)create the font map directly in phy/plot/static/ in npy.gz format.
"""


import gzip
import os
from pathlib import Path
Expand All @@ -14,10 +27,6 @@
class FontMapGenerator(object):
"""Generate a SDF font map for a monospace font, with a given uniform glyph size.
Need in the directory:
* msdfgen executable
* SourceCodePro-Regular.ttf
"""
def __init__(self):
self.rows, self.cols = FONT_MAP_SIZE
Expand All @@ -43,18 +52,21 @@ def _get_glyph_range(self, i, j):
return x, x + self.width, y, y + self.height

def _get_cmd(self, char_number):
"""Command that generates a glyph signed distance field PNG to be used in the font map."""
return (
f'{self.msdfgen_path} msdf -font {self.font} {char_number} -o {self.glyph_output} '
f'-size {self.width} {self.height} '
'-pxrange 4 -scale 3.9 -translate 0.5 4')

def _get_glyph_array(self, char_number):
"""Return the NumPy array with a glyph, by calling the msdfgen tool."""
cmd = self._get_cmd(char_number)
os.system(cmd)
assert self.glyph_output.exists()
return imageio.imread(self.glyph_output)

def _make_font_map(self):
"""Create the font map by putting together all used glyphs."""
Z = np.zeros((self.height * self.rows, self.width * self.cols, 3), dtype=np.uint8)
for i, j in self._iter_table():
char_number = self._get_char_number(i, j)
Expand All @@ -64,6 +76,7 @@ def _make_font_map(self):
return Z

def make(self):
"""Create the font map and save it in phy/plot/static."""
Z = self._make_font_map()
with gzip.open(str(self.font_map_output), 'wb') as f:
np.save(f, Z)
Expand Down

0 comments on commit 2443778

Please sign in to comment.