kopia lustrzana https://github.com/corrscope/corrscope
Remove Renderer.set_colors(), move into constructor
Makes it simpler to create an impl which constructs all canvases within __init__, not on first frame.pull/357/head
rodzic
5c7db27862
commit
88a79a97e8
|
@ -140,8 +140,8 @@ class Ovgen:
|
||||||
yield
|
yield
|
||||||
|
|
||||||
def _load_renderer(self):
|
def _load_renderer(self):
|
||||||
renderer = MatplotlibRenderer(self.cfg.render, self.cfg.layout, self.nchan)
|
renderer = MatplotlibRenderer(self.cfg.render, self.cfg.layout, self.nchan,
|
||||||
renderer.set_colors(self.cfg.channels)
|
self.cfg.channels)
|
||||||
return renderer
|
return renderer
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
|
|
|
@ -41,13 +41,21 @@ class RendererConfig:
|
||||||
|
|
||||||
|
|
||||||
class Renderer(ABC):
|
class Renderer(ABC):
|
||||||
def __init__(self, cfg: RendererConfig, lcfg: 'LayoutConfig', nplots: int):
|
def __init__(self, cfg: RendererConfig, lcfg: 'LayoutConfig', nplots: int,
|
||||||
|
channel_cfgs: Optional[List['ChannelConfig']]):
|
||||||
self.cfg = cfg
|
self.cfg = cfg
|
||||||
self.nplots = nplots
|
self.nplots = nplots
|
||||||
self.layout = RendererLayout(lcfg, nplots)
|
self.layout = RendererLayout(lcfg, nplots)
|
||||||
|
|
||||||
@abstractmethod
|
# Load line colors.
|
||||||
def set_colors(self, channel_cfgs: List['ChannelConfig']) -> None: ...
|
if channel_cfgs is not None:
|
||||||
|
if len(channel_cfgs) != self.nplots:
|
||||||
|
raise ValueError(
|
||||||
|
f"cannot assign {len(channel_cfgs)} colors to {self.nplots} plots"
|
||||||
|
)
|
||||||
|
self._line_colors = [cfg.line_color for cfg in channel_cfgs]
|
||||||
|
else:
|
||||||
|
self._line_colors = [None] * self.nplots
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def render_frame(self, datas: List[np.ndarray]) -> None: ...
|
def render_frame(self, datas: List[np.ndarray]) -> None: ...
|
||||||
|
@ -87,8 +95,6 @@ class MatplotlibRenderer(Renderer):
|
||||||
self._axes: List['Axes'] = None # set by set_layout()
|
self._axes: List['Axes'] = None # set by set_layout()
|
||||||
self._lines: List['Line2D'] = None # set by render_frame() first call
|
self._lines: List['Line2D'] = None # set by render_frame() first call
|
||||||
|
|
||||||
self._line_colors: List = [None] * self.nplots
|
|
||||||
|
|
||||||
self._set_layout() # mutates self
|
self._set_layout() # mutates self
|
||||||
|
|
||||||
def _set_layout(self) -> None:
|
def _set_layout(self) -> None:
|
||||||
|
@ -130,18 +136,6 @@ class MatplotlibRenderer(Renderer):
|
||||||
if self.cfg.create_window:
|
if self.cfg.create_window:
|
||||||
plt.show(block=False)
|
plt.show(block=False)
|
||||||
|
|
||||||
def set_colors(self, channel_cfgs: List['ChannelConfig']):
|
|
||||||
if len(channel_cfgs) != self.nplots:
|
|
||||||
raise ValueError(
|
|
||||||
f"cannot assign {len(channel_cfgs)} colors to {self.nplots} plots"
|
|
||||||
)
|
|
||||||
|
|
||||||
if self._lines is not None:
|
|
||||||
raise ValueError(
|
|
||||||
f'cannot set line colors after calling render_frame()'
|
|
||||||
)
|
|
||||||
self._line_colors = [cfg.line_color for cfg in channel_cfgs]
|
|
||||||
|
|
||||||
def render_frame(self, datas: List[np.ndarray]) -> None:
|
def render_frame(self, datas: List[np.ndarray]) -> None:
|
||||||
ndata = len(datas)
|
ndata = len(datas)
|
||||||
if self.nplots != ndata:
|
if self.nplots != ndata:
|
||||||
|
|
|
@ -70,7 +70,7 @@ def test_renderer_layout():
|
||||||
lcfg = LayoutConfig(ncols=2)
|
lcfg = LayoutConfig(ncols=2)
|
||||||
nplots = 15
|
nplots = 15
|
||||||
|
|
||||||
r = MatplotlibRenderer(cfg, lcfg, nplots)
|
r = MatplotlibRenderer(cfg, lcfg, nplots, None)
|
||||||
|
|
||||||
# 2 columns, 8 rows
|
# 2 columns, 8 rows
|
||||||
assert r.layout.ncols == 2
|
assert r.layout.ncols == 2
|
||||||
|
|
|
@ -19,7 +19,7 @@ NULL_CFG = FFmpegOutputConfig(None, '-f null')
|
||||||
|
|
||||||
def test_render_output():
|
def test_render_output():
|
||||||
""" Ensure rendering to output does not raise exceptions. """
|
""" Ensure rendering to output does not raise exceptions. """
|
||||||
renderer = MatplotlibRenderer(CFG.render, CFG.layout, nplots=1)
|
renderer = MatplotlibRenderer(CFG.render, CFG.layout, nplots=1, channel_cfgs=None)
|
||||||
out: FFmpegOutput = NULL_CFG(CFG)
|
out: FFmpegOutput = NULL_CFG(CFG)
|
||||||
|
|
||||||
renderer.render_frame([ALL_ZEROS])
|
renderer.render_frame([ALL_ZEROS])
|
||||||
|
|
|
@ -32,13 +32,13 @@ def test_default_colors(bg_str, fg_str):
|
||||||
lcfg = LayoutConfig()
|
lcfg = LayoutConfig()
|
||||||
nplots = 1
|
nplots = 1
|
||||||
|
|
||||||
r = MatplotlibRenderer(cfg, lcfg, nplots)
|
r = MatplotlibRenderer(cfg, lcfg, nplots, None)
|
||||||
verify(r, bg_str, fg_str)
|
verify(r, bg_str, fg_str)
|
||||||
|
|
||||||
# Ensure default ChannelConfig(line_color=None) does not override line color
|
# Ensure default ChannelConfig(line_color=None) does not override line color
|
||||||
r = MatplotlibRenderer(cfg, lcfg, nplots)
|
|
||||||
chan = ChannelConfig(wav_path='')
|
chan = ChannelConfig(wav_path='')
|
||||||
r.set_colors([chan] * nplots)
|
channels = [chan] * nplots
|
||||||
|
r = MatplotlibRenderer(cfg, lcfg, nplots, channels)
|
||||||
verify(r, bg_str, fg_str)
|
verify(r, bg_str, fg_str)
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,9 +54,9 @@ def test_line_colors(bg_str, fg_str):
|
||||||
lcfg = LayoutConfig()
|
lcfg = LayoutConfig()
|
||||||
nplots = 1
|
nplots = 1
|
||||||
|
|
||||||
r = MatplotlibRenderer(cfg, lcfg, nplots)
|
|
||||||
chan = ChannelConfig(wav_path='', line_color=fg_str)
|
chan = ChannelConfig(wav_path='', line_color=fg_str)
|
||||||
r.set_colors([chan] * nplots)
|
channels = [chan] * nplots
|
||||||
|
r = MatplotlibRenderer(cfg, lcfg, nplots, channels)
|
||||||
verify(r, bg_str, fg_str)
|
verify(r, bg_str, fg_str)
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue