def Insert(iterable, index, items): """ iterable >> Insert(index, items) Insert item(s) into lists/tuples in iterable. >>> [(1, 2), (3, 4)] >> Insert(1, 'X') >> Collect() [(1, 'X', 2), (3, 'X', 4)] >>> items = ['a', 'b'] >>> [(1, 2), (3, 4)] >> Insert(2, items) >> Collect() [(1, 2, 'a'), (3, 4, 'b')] >>> items = [('a', 'b'), ('c', 'd')] >>> [(1, 2), (3, 4)] >> Insert(1, items) >> Collect() [(1, 'a', 'b', 2), (3, 'c', 'd', 4)] >>> from nutsflow import Enumerate >>> [(1, 2), (3, 4)] >> Insert(0, Enumerate()) >> Collect() [(0, 1, 2), (1, 3, 4)] :param iterable iterable iterable: Any iterable over tuples or lists :param int index: Index at which position items are inserted. :param iterable|object items: A single object or an iterable over objects. :return: iterator where items are inserted into the iterable elements. :rtype: iterator over tuples """ items = items if is_iterable(items) else itt.repeat(items) for elem, item in zip(iterable, items): elem = list(elem) head, tail = elem[:index], elem[index:] yield tuple(head + as_list(item) + tail)
def _add_data(self, data): """Add data point to data buffer""" if hasattr(data, 'ndim'): # is it a numpy array? data = data.tolist() if data.ndim else [data.item()] else: data = as_list(data) if hasattr(self.xcols, '__iter__'): x = next(self.xcols) for i, _ in enumerate(self.ycols): self.xdata[i].append(x) elif hasattr(self.xcols, '__call__'): x = self.xcols() for i, _ in enumerate(self.ycols): self.xdata[i].append(x) else: for i, xcol in enumerate(as_tuple(self.xcols)): self.xdata[i].append(data[xcol]) for i, ycol in enumerate(self.ycols): self.ydata[i].append(data if ycol < 0 else data[ycol])
def __init__(self, ycols, xcols=None, layout=(1, None), every_sec=0, every_n=0, filterfunc=lambda data: True, figsize=None, filepath=None): """ iterable >> PlotLines(ycols) >> Consume() >>> import os >>> import numpy as np >>> from nutsflow import Consume >>> fp = 'tests/data/temp_plotter.png' >>> xs = np.arange(0, 6.3, 1.2) >>> ysin, ycos = np.sin(xs), np.cos(xs) >>> data = zip(xs, ysin, ycos) >>> data >> PlotLines(1, 0, filepath=fp) >> Consume() >>> list(ycos) >> PlotLines(0, filepath=fp) >> Consume() >>> data >> PlotLines(ycols=(1,2), filepath=fp) >> Consume() >>> ysin.tolist() >> PlotLines(ycols=None, filepath=fp) >> Consume() >>> if os.path.exists(fp): os.remove(fp) :param int|tuple|None ycols: Index or tuple of indices of the data columns that contain the y-data for the plot. If None data is used directly. :param int|tuple|function|iterable|None xcols: Index or tuple of indices of the data columns that contain the x-data for the plot. Alternatively an iterator or a function can be provided that generates the x-data for the plot, e.g. xcols = itertools.count() or xcols = lambda: epoch For xcols==None, itertools.count() will be used. :param tuple layout: Rows and columns of the plotter layout., e.g. a layout of (2,3) means that 6 plots in the data are arranged in 2 rows and 3 columns. Number of cols can be None is then derived from ycols :param float every_sec: Plot every given second, e.g. to plot every 2.5 sec every_sec = 2.5 :param int every_n: Plot every n-th call. :param function filterfunc: Boolean function to filter plot data. :param tuple figsize: Figure size in inch. :param filepath: Path to a file to draw plot to. If provided the plot will not appear on the screen. :return: Returns input unaltered :rtype: any """ self.ycols = [-1] if ycols is None else as_list(ycols) self.xcols = itt.count() if xcols is None else xcols self.filepath = filepath self.figsize = figsize self.cnt = 0 self.time = time.clock() self.filterfunc = filterfunc self.every_sec = every_sec self.every_n = every_n r, c, n = layout[0], layout[1], len(self.ycols) if c is None: c = n self.figure = plt.figure(figsize=figsize) self.axes = [self.figure.add_subplot(r, c, i + 1) for i in range(n)] self.reset()
def test_as_list(): assert as_list(1) == [1] assert as_list((1, 2)) == [1, 2] assert as_list([1, 2]) == [1, 2]