Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-koschmieder committed Feb 27, 2019
0 parents commit d02ba01
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aixplot License Agreement (MIT License)
---------------------------------------

Copyright (c) 2019 Lukas Koschmieder

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from aixplot.plotter import Plotter
from aixplot.widget import Widget
42 changes: 42 additions & 0 deletions cacher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) 2019 Lukas Koschmieder

from abc import ABC, abstractmethod
import asyncio
from collections import defaultdict
import logging

module_logger = logging.getLogger(__name__)
module_logger.setLevel(logging.INFO)
module_logger.addHandler(logging.StreamHandler())

class Cacher(ABC):
def __init__(self, file, logger=None):
self._file = file
self.logger = logger if logger else module_logger
self.cache = defaultdict(list)

@abstractmethod
def _filter(self, data, cache):
pass

@abstractmethod
async def _async_read(self):
pass

@abstractmethod
def labels(self):
pass

async def async_cache(self, stop_cond=None,
on_stop=None, on_update=None, on_eof=None):
while True:
await asyncio.sleep(0)
new = await self._async_read(self._file)
if new:
self._filter(new, self.cache)
if on_update: on_update(self.cache, new)
else:
if on_eof: on_eof(self.cache)
if stop_cond and stop_cond(self.cache):
if on_stop: on_stop(self.cache)
return self.cache
34 changes: 34 additions & 0 deletions logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2015 Project Jupyter Contributors
# Copyright (c) 2019 Lukas Koschmieder

import logging
from ipywidgets import Output

class OutputWidgetHandler(logging.Handler):
""" Custom logging handler sending logs to an output widget """

def __init__(self, *args, **kwargs):
super(OutputWidgetHandler, self).__init__(*args, **kwargs)
layout = {
'width': '100%',
'height': '160px',
}
self.out = Output(layout=layout)

def emit(self, record):
""" Overload of logging.Handler method """
formatted_record = self.format(record)
new_output = {
'name': 'stdout',
'output_type': 'stream',
'text': formatted_record+'\n'
}
self.out.outputs = (new_output, ) + self.out.outputs

def show_logs(self):
""" Show the logs """
display(self.out)

def clear_logs(self):
""" Clear the current logs """
self.out.clear_output()
30 changes: 30 additions & 0 deletions plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2019 Lukas Koschmieder

import asyncio
from bqplot import Axis, Figure, LinearScale, Lines
import logging

module_logger = logging.getLogger(__name__)
module_logger.setLevel(logging.INFO)
module_logger.addHandler(logging.StreamHandler())

class Plotter(object):
def __init__(self, cacher, logger=None):
self.cacher = cacher
self.logger = logger if logger else module_logger
self._x, self._y, self._marks = None, None, None

def refresh(self, cache, new=None):
if self._marks:
self._marks.x = cache[self._x]
self._marks.y = cache[self._y]

def plot(self, x, y, title="", xscale=None, yscale=None):
xscale = xscale if xscale else LinearScale()
yscale = yscale if yscale else LinearScale()
a = [Axis(label=x, scale=xscale),
Axis(label=y, scale=yscale, side='left')]
m = Lines(x=[], y=[], scales={'x': xscale, 'y': yscale})
fig = Figure(marks=[m], axes=a, title=title)
self._x, self._y, self._marks = x, y, m
return fig
30 changes: 30 additions & 0 deletions reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2019 Lukas Koschmieder

from abc import ABC, abstractmethod
import asyncio

class Reader(ABC):
def __init__(self):
self._fpos = 0

@abstractmethod
def _process_line(self, line):
pass

async def async_read(self, file, tries=3, sleep=1.0, on_done=None):
t = tries
while t > 0:
file.seek(self._fpos)
self._fpos = file.tell()
line = file.readline()
if line:
t = tries
self._fpos = file.tell()
data = self._process_line(line)
if data:
if on_done: on_done(data)
return data
else:
t = t - 1
await asyncio.sleep(sleep)
return None
108 changes: 108 additions & 0 deletions widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright (c) 2019 Lukas Koschmieder

import asyncio
from ipywidgets import Box, Dropdown, HBox, Label, Layout, \
link, Output, ToggleButtons, VBox
import logging
from traitlets import Bool, HasTraits, observe, Unicode

from .logging import OutputWidgetHandler
from .plotter import Plotter

module_logger = logging.getLogger(__name__)
module_logger.setLevel(logging.INFO)
module_logger.addHandler(logging.StreamHandler())

class Widget(Box):
x, y = Unicode(), Unicode()
plot = Bool(True)

def __init__(self, cacher, logger=None, **traits):
self._cacher = cacher
self.logger = logger if logger else module_logger
HasTraits.__init__(self, **traits)

log_handler = OutputWidgetHandler()
fmt = '%(asctime)s [%(levelname)s] %(message)s'
log_handler.setFormatter(logging.Formatter(fmt))
self.logger.addHandler(log_handler)

self._plotter = Plotter(cacher, logger=self.logger)

labels = cacher.labels()
self.x = self.x if self.x else labels[0]
self.y = self.y if self.y else labels[0]
ui_x = Dropdown(options=labels, description='x:', value=self.x)
ui_y = Dropdown(options=labels, description='y:', value=self.y)
ui_onoff = ToggleButtons(description='Plot:', options=[True, False])
self._ui = (ui_x, ui_y, ui_onoff)
self._status = Label()
self._fig_out = Output()

self.observe(self._on_change_xy, names=['x','y'])
self.observe(self._on_change_onoff, names=['plot'])
link((self, 'x'), (ui_x, 'value'))
link((self, 'y'), (ui_y, 'value'))
link((self, 'plot'), (ui_onoff, 'value'))

left = Layout(display='flex', flex_flow='row wrap')
b = (HBox((ui_x, ui_y,), layout=left),)\
+ (HBox((ui_onoff, self._status,), layout=left),)\
+ (self._fig_out,)\
+ (HBox((log_handler.out,),),)

self._fig, self._task = None, None
plot = self.plot; self.plot = False; self.plot = plot
super(self.__class__, self).__init__((VBox(b),))

def _on_change_onoff(self, b):
if not self._fig:
self._display_fig()
self._plot(b.new)
def _on_change_xy(self, b):
self._display_fig()
self._refresh_plot(self._cacher.cache)

def _set_disable_ui(self, b):
for i in self._ui: i.disabled = b

def disable_ui(func):
def deco(self, *args, **kwargs):
self._set_disable_ui(True)
func(self, *args, **kwargs)
self._set_disable_ui(False)
return deco

def _refresh_plot(self, cache, new=None):
self._plotter.refresh(cache, new)

def _on_update(self, cache, new):
self._refresh_plot(cache, new)
self._status.value = "Progress: {}".format(len(cache[self.x]))

def _on_eof(self, cache):
self._refresh_plot(cache)
self._status.value = "Progress: {} | EOF".format(len(cache[self.x]))

@disable_ui
def _plot(self, b):
if b and not self._task:
self.logger.debug("Start")
p, c = self._plotter, self._cacher
coro = c.async_cache(stop_cond=False, on_update=self._on_update,
on_eof=self._on_eof)
self.logger.debug("%s", coro)
self._task = asyncio.ensure_future(coro)
else:
self.logger.debug("Pause")
if self._task: self._task.cancel()
self._task = None

@disable_ui
def _display_fig(self):
p, x, y = self._plotter, self.x, self.y
self.logger.debug("Plot x=%s y=%s", x, y)
fig = p.plot(x, y)
with self._fig_out: display(fig)
if self._fig: self._fig.close()
self._fig = fig

0 comments on commit d02ba01

Please sign in to comment.