Exemple #1
0
import numpy as np
import pylab as pl
import pandas
from collections import defaultdict
from arsenal.viz.util import update_ax

from arsenal.misc import ddict
lc = ddict(lambda name: LearningCurve(name))


class LearningCurve(object):
    """
    Plot learning curve as data arrives.
    """
    def __init__(self, name, sty=None, legend=True, averaging=True):
        self.name = name
        self.baselines = {}
        self.baselines_reward = {}
        self.data = defaultdict(list)
        self.sty = defaultdict(dict)
        if sty is not None:
            self.sty.update(sty)
        self.ax = None
        self.legend = legend
        self.averaging = averaging

        self.yscale = None
        self.xscale = None

    def plot(self):
        if self.ax is None:
Exemple #2
0
                expect='expect',
                got='got',
                show_regression=1,
                scatter=1,
                **kw):
        from arsenal.maths import compare
        if self.ax is None:
            self.ax = pl.figure().add_subplot(111)
        if self.df.empty:
            return
        with update_ax(self.ax):
            compare(expect, got, data=self.df).plot(ax=self.ax, **kw)


# Global references to numerical debugger class.
DEBUG = ddict(NumericalDebug)


@contextmanager
def lineplot(name,
             with_ax=False,
             halflife=20,
             xlabel=None,
             ylabel=None,
             title=None,
             **style):
    with axman(name, xlabel=xlabel, ylabel=ylabel, title=title) as ax:
        data = DATA[name]
        if with_ax:
            yield (data, ax)
        else:
            self.draw()
            self.last_update = time()
        return self

    def should_update(self):
        "Returns true if its been long enough (>= `min_time`) since the `last_update`."
        return time() - self.last_update >= self.min_time

    def __reduce__(self):
        # Default pickle fails because of the reference to the plotting axis
        x = self.__dict__.copy()
        x['ax'] = None
        return (LearningCurve, (self.name, self.sty), x)


lc = ddict(LearningCurve)

#def run():
#
#    lc = LearningCurve('test')
#    lc.smooth('ewm', 'mean', half_life = 0.01).bands('std')
#    for t in range(1, 1000):
#        lc.update(t, signal = np.exp(np.log(t) * -0.5 + np.random.randn()))
#        if t % 10 == 0:
#            lc.update(t, signal2 = np.exp(np.log(t) * -0.25 + np.random.randn()))
#        lc.loglog().draw()
#
#    pl.ioff(); pl.show()
#
#
#if __name__ == '__main__':
Exemple #4
0
import numpy as np
import pylab as pl
import pandas
from collections import defaultdict
from arsenal.viz.util import update_ax

from arsenal.misc import ddict
lc = ddict(lambda name: LearningCurve(name))

from time import time

class LearningCurve(object):
    """
    Plot learning curve as data arrives.
    """

    def __init__(self, name, sty=None, legend=True, smoothing=10):
        self.name = name
        self.baselines = {}
        self.data = defaultdict(list)
        self.sty = defaultdict(dict)
        if sty is not None:
            self.sty.update(sty)
        self.ax = None
        self.legend = legend

        self.yscale = None
        self.xscale = None

        self.last_update = time()
        self.min_time = 0.5
Exemple #5
0
 def __init__(self, title):
     self.title = title
     self.timers = ddict(Timer)
Exemple #6
0
 def __init__(self, title):
     self.title = title
     self.timers = ddict(Timer)
Exemple #7
0
        "Pass in column values for the row by name as keyword arguments."
        self._data.append(kw)
        self.uptodate = False
        return self

    def compare(self, expect='expect', got='got', show_regression=1, scatter=1, **kw):
        from arsenal.math import compare
        if self.ax is None:
            self.ax = pl.figure().add_subplot(111)
        if self.df.empty:
            return
        with update_ax(self.ax):
            compare(expect, got, data=self.df).plot(ax=self.ax, **kw)

# Global references to numerical debugger class.
DEBUG = ddict(NumericalDebug)

@contextmanager
def lineplot(name, with_ax=False, halflife=20, xlabel=None, ylabel=None, title=None, **style):
    with axman(name, xlabel=xlabel, ylabel=ylabel, title=title) as ax:
        data = DATA[name]
        if with_ax:
            yield (data, ax)
        else:
            yield data
        ax.plot(range(len(data)), data, alpha=0.5, **style)
        if halflife:
            ax.plot(pd.Series(data).ewm(halflife=halflife).mean(), alpha=0.5, c='k', lw=2)


@contextmanager
Exemple #8
0
def timers():
    return ddict(Timer)