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
nyanpasu64 2018-11-05 15:45:38 -08:00
rodzic 5c7db27862
commit 88a79a97e8
5 zmienionych plików z 20 dodań i 26 usunięć

Wyświetl plik

@ -140,8 +140,8 @@ class Ovgen:
yield
def _load_renderer(self):
renderer = MatplotlibRenderer(self.cfg.render, self.cfg.layout, self.nchan)
renderer.set_colors(self.cfg.channels)
renderer = MatplotlibRenderer(self.cfg.render, self.cfg.layout, self.nchan,
self.cfg.channels)
return renderer
def play(self):

Wyświetl plik

@ -41,13 +41,21 @@ class RendererConfig:
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.nplots = nplots
self.layout = RendererLayout(lcfg, nplots)
@abstractmethod
def set_colors(self, channel_cfgs: List['ChannelConfig']) -> None: ...
# Load line colors.
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
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._lines: List['Line2D'] = None # set by render_frame() first call
self._line_colors: List = [None] * self.nplots
self._set_layout() # mutates self
def _set_layout(self) -> None:
@ -130,18 +136,6 @@ class MatplotlibRenderer(Renderer):
if self.cfg.create_window:
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:
ndata = len(datas)
if self.nplots != ndata:

Wyświetl plik

@ -70,7 +70,7 @@ def test_renderer_layout():
lcfg = LayoutConfig(ncols=2)
nplots = 15
r = MatplotlibRenderer(cfg, lcfg, nplots)
r = MatplotlibRenderer(cfg, lcfg, nplots, None)
# 2 columns, 8 rows
assert r.layout.ncols == 2

Wyświetl plik

@ -19,7 +19,7 @@ NULL_CFG = FFmpegOutputConfig(None, '-f null')
def test_render_output():
""" 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)
renderer.render_frame([ALL_ZEROS])

Wyświetl plik

@ -32,13 +32,13 @@ def test_default_colors(bg_str, fg_str):
lcfg = LayoutConfig()
nplots = 1
r = MatplotlibRenderer(cfg, lcfg, nplots)
r = MatplotlibRenderer(cfg, lcfg, nplots, None)
verify(r, bg_str, fg_str)
# Ensure default ChannelConfig(line_color=None) does not override line color
r = MatplotlibRenderer(cfg, lcfg, nplots)
chan = ChannelConfig(wav_path='')
r.set_colors([chan] * nplots)
channels = [chan] * nplots
r = MatplotlibRenderer(cfg, lcfg, nplots, channels)
verify(r, bg_str, fg_str)
@ -54,9 +54,9 @@ def test_line_colors(bg_str, fg_str):
lcfg = LayoutConfig()
nplots = 1
r = MatplotlibRenderer(cfg, lcfg, nplots)
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)