Esempio n. 1
0
def makeOutput(game):

    gameFile = 'games/%s.txt' % game
    seqFile = 'json/%s_sequences.json' % game
    dataFile = 'json/%s_datapoints.json' % game
    
    p1, p2 = parsing.processInfosets(gameFile)
    parsing.processSeqIDs(p1, p2, seqFile)
    parsing.getData(p1, p2, dataFile)
    '''
    hm1 = hv.HoloMap({(i.player,name): genCurves(i)
                      for p in (p1,p2)
                      for name,i in p.items()},
                     kdims=['player','infoset'])
    '''
    p1 = hv.HoloMap({name: genCurves(i) for name,i in p1.items()},
                    kdims=['infoset'])
    p2 = hv.HoloMap({name: genCurves(i) for name,i in p2.items()},
                    kdims=['infoset'])
    grid1 = hv.GridSpace(p1)
    grid2 = hv.GridSpace(p2)

    layout1 = hv.NdLayout(grid1).cols(2)
    layout2 = hv.NdLayout(grid2).cols(2)
    hv.output(layout1 + layout2, size=150, filename=game)
Esempio n. 2
0
def plot(
    metrics,
    x="step",
    y="mean_episode_return",
    palette="Set1",
    kdims=["lr"],
    kdims_facet=["lstm"],
    subsample=1000,
    cols=3,
):
    hmap = {}
    model2color = {}
    colors = hv.Palette(palette).values
    colors = list(set(colors))
    for _facet, models in metrics.items():
        for model, _ in models.items():
            if model not in model2color:
                model2color[model] = colors[len(model2color) - 1]

    for facet, models in metrics.items():
        p = plot_facet(
            models, x, y, palette, model2color, kdims=kdims_facet, subsample=subsample
        )
        if facet not in hmap:
            hmap[facet] = {}
        hmap[facet] = p
    p = hv.HoloMap(hmap, kdims=kdims)
    p = hv.NdLayout(p).cols(cols)
    return p
Esempio n. 3
0
 def ndlayout_(self, dataset, kdims, cols=3):
     """
     Create a Holoview NdLayout from a dictionnary of chart objects
     """
     try:
         return hv.NdLayout(dataset, kdims=kdims).cols(cols)
     except Exception as e:
         self.err(e, self.layout_, "Can not create layout")
Esempio n. 4
0
def plot_stimulus(obj, **args):
    spatial = args.get('spatial', False)
    bin = args.get('bin', float('Inf'))
    if np.isinf(bin):
        bins = np.array([0, obj.duration])
        num = 1
    else:
        bins = np.r_[0:obj.duration + bin / 1000.:bin / 1000.]
        num = bins.size - 1

    if not spatial:
        grid = args.get('grid', False)
        hm = dict()
        tmin = np.min(obj.trace)
        tmax = np.max(obj.trace)
        for t in range(num):
            if num == 1:
                d = {i:hv.Curve((obj.time,obj.trace[i]))\
                    for i in range(obj.trace.shape[0])}
            else:
                d = {i:hv.Curve((obj.time,obj.trace[i]))*\
                    hv.Curve([(bins[t+1],tmin),(bins[t+1],tmax)])
                    for i in range(obj.trace.shape[0])}
            if grid:
                hvobj = hv.NdLayout(d)
            else:
                hvobj = hv.NdOverlay(d)
            hm[t] = hvobj
        hvobj = hv.HoloMap(hm,
                           kdims='Time bin [' + str(bin) + ' ms]').collate()
    else:
        sur = args.get('surface', hand_surface)
        mid = (bins[1:] + bins[:-1]) / 2.
        d = np.array([np.interp(mid,obj.time,obj.trace[i])\
            for i in range(obj.trace.shape[0])])
        d = (d - np.min(d))
        d = 1 - d / np.max(d)

        hm = dict()
        locs = sur.hand2pixel(obj.location)
        rad = obj.pin_radius * sur.pxl_per_mm
        for t in range(num):
            p = hv.Polygons(
                [{
                    ('x', 'y'):
                    hv.Ellipse(locs[l, 0], locs[l, 1], 2 * rad).array(),
                    'z':
                    d[l, t]
                } for l in range(obj.location.shape[0])],
                vdims='z').opts(
                    plot=dict(color_index='z', aspect='equal'),
                    style=dict(linewidth=0.,
                               line_width=0.01)).options(cmap='fire')
            hm[t] = p
        hvobj = hv.HoloMap(hm, kdims='Time bin [' + str(bin) + ' ms]')
    return hvobj
Esempio n. 5
0
def plot_thresholds(summary, df):
    feat_d = get_feature_data(summary, df)
    plot_d = {
        col: hv.Curve(feat_d[col]).opts(shared_axes=False,
                                        normalize=False,
                                        title=col,
                                        tools=["hover"])
        for col in feat_d.keys()
    }
    return hv.NdLayout(plot_d, kdims="Serie")
Esempio n. 6
0
def modify_doc(doc):
    frequencies = [0.5, 0.75, 1.0, 1.25]
    curve_dict = {f: sine_curve(0, f) for f in frequencies}
    NdLayout = hv.NdLayout(curve_dict, kdims='frequency')

    final = NdLayout

    plot = renderer.get_plot(final)
    layout = row(plot.state)
    # renderer.server_doc(layout)

    doc.add_root(layout)
Esempio n. 7
0
def plot(
    metrics,
    x="step",
    y="mean_episode_return",
    palette="Set1",
    subsample=1000,
    cols=3,
    y_min=None,
    y_max=None,
):
    hmap = {}
    model2color = {}
    colors = hv.Palette(palette).values
    colors = list(set(colors))
    for _facet, models in metrics.items():
        for model, _ in models.items():
            if model not in model2color:
                model2color[model] = colors[(len(model2color) - 1) %
                                            len(colors)]
    for facet, models in metrics.items():
        p = plot_facet(
            models,
            x,
            y,
            palette,
            model2color,
            subsample=subsample,
            y_min=y_min,
            y_max=y_max,
        )
        if facet not in hmap:
            hmap[facet] = {}
        hmap[facet] = p
    kdims = get_kdims(next(iter(metrics)))
    p = hv.HoloMap(hmap, kdims=kdims)
    p = hv.NdLayout(p).cols(cols)
    return p
Esempio n. 8
0
def get_portfolio_analysis(_):
    stocks = get_stocks()
    stocks = stocks[selector.value]
    log_ret = np.log(stocks / stocks.shift(1))
    log_ret_array = log_ret.values

    @nb.jit
    def get_ret_vol_sr(weights):
        """
        Takes in weights, returns array of return, volatility, sharpe ratio
        """
        (ncols, ) = weights.shape
        mean_ret = np.array(
            [np.nanmean(log_ret_array[:, i]) for i in range(ncols)])
        ret = np.sum(mean_ret * weights) * 252

        log_ret[log_ret > 0] = 0
        vol = np.sqrt(
            np.dot(
                weights.T,
                np.dot(np.cov(log_ret_array[1:], rowvar=False) * 252,
                       weights)))
        sr = ret / vol

        return np.array([ret, vol, sr])

    def minimize_volatility(weights):
        return get_ret_vol_sr(weights)[1]

    def minimize_difference(weights, des_vol, des_ret):
        ret, vol, _ = get_ret_vol_sr(weights)
        return abs(des_ret - ret) + abs(des_vol - vol)

    def find_best_allocation(vol, ret):
        cols = len(stocks.columns)
        bounds = tuple((0, 1) for i in range(cols))
        init_guess = [1. / cols for i in range(cols)]
        cons = ({
            'type': 'eq',
            'fun': check_sum
        }, {
            'type': 'eq',
            'fun': lambda w: get_ret_vol_sr(w)[0] - ret
        }, {
            'type': 'eq',
            'fun': lambda w: get_ret_vol_sr(w)[1] - vol
        })
        return minimize(minimize_difference,
                        init_guess,
                        args=(vol, ret),
                        method='SLSQP',
                        bounds=bounds,
                        constraints=cons)

    def random_allocation_cb(n_samples):
        all_weights, ret_arr, vol_arr, sharpe_arr = random_allocation(
            stocks.values, stocks.shift(1).values, num_ports=n_samples)
        vdims = [
            *('%s Weight' % c for c in stocks.columns), 'Return', 'Volatility',
            'Sharpe Ratio'
        ]
        return hv.Dataset((*all_weights.T, ret_arr, vol_arr, sharpe_arr),
                          vdims=vdims)

    def compute_frontier(start=0, end=0.3, n=100):
        frontier_ret = np.linspace(start, end, n)
        frontier_volatility = []

        cols = len(stocks.columns)
        bounds = tuple((0, 1) for i in range(cols))
        # Initial Guess (equal distribution)
        init_guess = [1. / cols for i in range(cols)]
        for possible_return in frontier_ret:
            # function for return
            cons = ({
                'type': 'eq',
                'fun': check_sum
            }, {
                'type': 'eq',
                'fun': lambda w: get_ret_vol_sr(w)[0] - possible_return
            })

            result = minimize(minimize_volatility,
                              init_guess,
                              method='SLSQP',
                              bounds=bounds,
                              constraints=cons)
            frontier_volatility.append(result['fun'])
        return frontier_volatility, frontier_ret

    def plot_frontier(ds):
        ret_min, ret_max = ds.range('Return')
        ret_pad = (ret_max - ret_min) * 0.1
        frontier = compute_frontier(ret_min - ret_pad, ret_max + ret_pad, 100)
        return hv.Curve(frontier,
                        label='Efficient Frontier').opts(line_dash='dashed',
                                                         color='green')

    def plot_closest(x, y):
        vdims = list(stocks.columns)
        if (x is None or y is None):
            return hv.Points([], vdims=vdims)

        opt = find_best_allocation(x, y)
        weights = opt.x
        ret, vol, _ = get_ret_vol_sr(weights)
        return hv.Points([(vol, ret, *weights)], ['Volatility', 'Return'],
                         vdims=vdims,
                         label='Current Portfolio').opts(color='green',
                                                         size=10,
                                                         line_color='black',
                                                         tools=['hover'])

    def plot_table(ds):
        arr = ds.array()
        weights = list(zip(stocks.columns, arr[0, 2:])) if len(arr) else []
        return hv.Table(weights, 'Stock', 'Weight').opts(editable=True)

    def portfolio_text(ds):
        arr = ds.array()
        ret, vol, sharpe = get_ret_vol_sr(arr[0, 2:])
        text = """
        The selected portfolio has a volatility of %.2f, a return of %.2f
        and Sharpe ratio of %.2f.
        """ % (vol, ret, sharpe)
        return hv.Div(text).opts(height=40)

    weights = np.array([1. / len(selector.value) for _ in selector.value])
    ret, vol, _ = get_ret_vol_sr(weights)

    posxy = hv.streams.Tap(y=ret, x=vol)

    closest = hv.DynamicMap(plot_closest, streams=[posxy])
    table = closest.apply(plot_table)

    random = random_allocation_cb(n_samples.value)
    vol_ret = (random.apply(plot_portfolios) * random.apply(plot_frontier) *
               random.apply(plot_max_sharpe) *
               closest).opts(legend_position='bottom_right')
    div = closest.apply(portfolio_text)

    start, end = stocks.index.min(), stocks.index.max()
    year = pn.widgets.DateRangeSlider(name='Year',
                                      value=(start, end),
                                      start=start,
                                      end=end)
    investment = pn.widgets.Spinner(name='Investment Value in $',
                                    value=5000,
                                    step=1000,
                                    start=1000,
                                    end=100000)

    investment_widgets = pn.Row(year, investment)

    def plot_return_curve(table, investment, dates):
        weight = table['Weight']
        allocations = weight * investment
        amount = allocations / stocks[dates[0]:].iloc[0]
        return hv.Curve((stocks[dates[0]:dates[1]] * amount).sum(
            axis=1).reset_index().rename(columns={0: 'Total Value ($)'})).opts(
                responsive=True,
                framewise=True,
                min_height=300,
                padding=(0.05, 0.1))

    return_curve = table.apply(plot_return_curve,
                               investment=investment.param.value,
                               dates=year.param.value)

    reindexed = stocks.reset_index()
    timeseries = hv.NdOverlay({
        col: hv.Curve(reindexed, 'Date', col).redim(**{col: 'Stock Price ($)'})
        for col in stocks.columns
    }).opts(title_format='Daily Stock Price',
            min_height=300,
            responsive=True,
            show_grid=True,
            legend_position='top_left')

    log_ret_ds = hv.Dataset(log_ret)
    log_ret_hists = hv.NdLayout(
        {
            col: log_ret_ds.hist(col, num_bins=100, adjoin=False)
            for col in log_ret.columns
        },
        kdims=['Stock']).cols(2).opts(
            hv.opts.NdLayout(sizing_mode='stretch_width'),
            hv.opts.Histogram(height=300, min_width=400, responsive=True))

    return pn.Tabs(
        ('Analysis',
         pn.Column(pn.Row(vol_ret,
                          pn.layout.Spacer(width=20),
                          pn.Column(div, table),
                          sizing_mode='stretch_width'),
                   pn.Column(pn.Row(year, investment),
                             return_curve,
                             sizing_mode='stretch_width'),
                   sizing_mode='stretch_width')), ('Timeseries', timeseries),
        ('Log Return',
         pn.Column(
             '## Daily normalized log returns',
             'Width of distribution indicates volatility and center of distribution the mean daily return.',
             log_ret_hists,
             sizing_mode='stretch_width')))
Esempio n. 9
0
    cent_ma = cents[(cents['session'] == ss) & (cents['unit_id'].isin(uid_ma))]
    cent_nm = cents[(cents['session'] == ss) & (cents['unit_id'].isin(uid_nm))]
    hv_A_ma = hv.Image(
        A_shifted.sel(session=ss, unit_id=uid_ma).sum('unit_id').compute(),
        ['width', 'height']).opts(**opts_im)
    hv_A_nm = hv.Image(
        A_shifted.sel(session=ss, unit_id=uid_nm).sum('unit_id').compute(),
        ['width', 'height']).opts(**opts_im)
    A_dict[(ss, 'matching')] = hv_A_ma
    A_dict[(ss, 'non-matching')] = hv_A_nm


# In[18]:


hv.NdLayout(A_dict, kdims=['session', 'ma']).cols(2)


# In[19]:


mappings_match, mappings_nonmatch = subset_by_session(sess, mappings_meta_fill)
A_dict = dict()
cent_dict = dict()
for cur_ss in sess:
    cur_uid_ma = mappings_match[[('meta', d) for d in group_dim] + [('session', cur_ss)]]
    cur_uid_nm = mappings_nonmatch[[('meta', d) for d in group_dim] + [('session', cur_ss)]].dropna()
    cur_uid_ma.columns = cur_uid_ma.columns.droplevel()
    cur_uid_nm.columns = cur_uid_nm.columns.droplevel()
    cur_uid_ma = cur_uid_ma.rename(columns={cur_ss:'unit_id'})
    cur_uid_nm = cur_uid_nm.rename(columns={cur_ss:'unit_id'})
Esempio n. 10
0
def create_figures(df, categories, accuracies, dimension=1000, mobile=False):
    """
    Create graphics from the dataframe with the data.

    The datasets must have the structure:

    algorithm | f1 | f2 | ... | accuracy | dimension)

    :param df: dataframe with the values to compare.
    :param group: dict with for each category list the functions.
    :param categories: categories to compare (sorted).
    :param algs: algorithm list (sorted).
    """
    num_cols = 3
    dim_df = cec2013_normalize(df, dimension)
    milestones = dim_df['milestone'].unique().tolist()
    milestones.sort()
    print(milestones)

    if mobile:
        num_cols = 1

    hv.extension('bokeh')
    renderer = hv.renderer('bokeh')
    options = "Bars [xrotation=90]"

    total_figs = {}
    fig_names = []

    num_rows, num_columns = dim_df.shape
    num_functions = num_columns - 3
    assert (num_functions > 0)
    # append all data in a pivot table
    total_pivot_df = pd.DataFrame(index=np.arange(0,
                                                  num_rows * len(categories)),
                                  columns=('category', 'milestone', 'alg',
                                           'ranking'))
    pivot_i = 0

    for cat in categories:
        cat_name = cat.name
        # Filter only the function in the category
        functions = ['F{}'.format(x) for x in cat.functions()]
        cat_figs = []
        accuracies.sort()

        for milestone in milestones:
            current_df = dim_df >> mask(X.milestone == milestone)

            if not current_df.empty:
                # Remove field milestone and index by alg
                current_df = current_df.drop('milestone', 1).set_index('alg')
                # Add to total value
                sum_df = _get_values_df(current_df, functions)
                title = "Accuracy: {:2.1E}".format(milestone)
                plot = get_plot_bar(sum_df, title)
                cat_figs.append(plot)
                # Add extra information

                for alg, rank in sum_df.values:
                    new_ranking_row = [cat_name, milestone, alg, rank]
                    total_pivot_df.loc[pivot_i] = new_ranking_row
                    pivot_i += 1

            total_figs[cat_name] = renderer.get_plot(
                hv.Layout(cat_figs).opts(options).cols(num_cols)).state

    cat_global = []

    plot_dyn = {}

    for mil in milestones:
        plot = get_plot_by_milestone(total_pivot_df, mil)
        plot_dyn[mil] = plot
        cat_global.append(plot)

    plots = hv.Layout(cat_global).opts(options)
    # legend_opts = {'NdOverlay': dict(show_legend=True, legend_position='right')}
    # plots.Bars.III = plots.Bars.III(plot=legend_opts)
    total_figs['All'] = renderer.get_plot(plots.cols(num_cols)).state
    fig_names.extend([cat.name for cat in categories])
    fig_names.append('All')

    return figure_json(fig_names, total_figs, type='bokeh')

    final = hv.NdLayout(plot_dyn, kdims='milestone')
    total_figs['Final'] = renderer.get_plot(final).state
    fig_names.append('Final')
    plot_bokeh = renderer.get_plot(hv.HoloMap(final)).state
    setattr(plot_bokeh, 'plot_width', 500)
    setattr(plot_bokeh, 'plot_height', 300)
    total_figs['Final2'] = plot_bokeh
    fig_names.append('Final2')

    return figure_json(fig_names, total_figs, type='bokeh')