コード例 #1
0
def test_build_agg_data(unittest, rolling_data):
    import dtale.views as views

    df, _ = views.format_data(rolling_data)
    extended_aggregation = [
        dict(col="0", agg="mean"),
        dict(col="0", agg="sum"),
        dict(col="1", agg="mean"),
    ]
    output, code, cols = chart_utils.build_agg_data(
        df,
        "date", ["0", "1"], {},
        None,
        extended_aggregation=extended_aggregation)
    unittest.assertEqual(sorted(output.columns),
                         ["0|mean", "0|sum", "1|mean", "date"])
    unittest.assertEqual(sorted(cols), ["0|mean", "0|sum", "1|mean"])

    output, code, cols = chart_utils.build_agg_data(df, "date", ["0", "1"], {},
                                                    "mean")
    unittest.assertEqual(list(output.columns), ["date", "0|mean", "1|mean"])
    unittest.assertEqual(cols, ["0|mean", "1|mean"])

    output, code, cols = chart_utils.build_agg_data(df, "date", ["0", "1"], {},
                                                    "drop_duplicates")
    unittest.assertEqual(
        list(output.columns),
        ["index", "date", "0|drop_duplicates", "1|drop_duplicates"],
    )
    unittest.assertEqual(cols, ["0|drop_duplicates", "1|drop_duplicates"])

    extended_aggregation = [
        dict(col="0", agg="mean"),
        dict(col="0", agg="pctsum"),
        dict(col="1", agg="mean"),
        dict(col="1", agg="first"),
    ]
    output, code, cols = chart_utils.build_agg_data(
        df,
        "date", ["0", "1"], {},
        None,
        extended_aggregation=extended_aggregation)
    unittest.assertEqual(
        sorted(list(output.columns)),
        ["0|mean", "0|pctsum", "1|first", "1|mean", "date"],
    )
    unittest.assertEqual(sorted(cols),
                         ["0|mean", "0|pctsum", "1|first", "1|mean"])
コード例 #2
0
def heatmap_builder(data_id, **inputs):
    """
    Builder function for :plotly:`plotly.graph_objects.Heatmap <plotly.graph_objects.Heatmap>`

    :param data_id: integer string identifier for a D-Tale process's data
    :type data_id: str
    :param inputs: Optional keyword arguments containing the following information:
        - x: column to be used as x-axis of chart
        - y: column to be used as y-axis of chart
        - z: column to use for the Z-Axis
        - agg: points to a specific function that can be applied to :func: pandas.core.groupby.DataFrameGroupBy
    :type inputs: dict
    :return: heatmap
    :rtype: :plotly:`plotly.graph_objects.Heatmap <plotly.graph_objects.Heatmap>`
    """

    try:
        if not valid_chart(**inputs):
            return None
        raw_data = global_state.get_data(data_id)
        wrapper = chart_wrapper(data_id, raw_data, inputs)
        hm_kwargs = dict(hoverongaps=False,
                         colorscale='Greens',
                         showscale=True,
                         hoverinfo='x+y+z')
        x, y, z, agg = (inputs.get(p) for p in ['x', 'y', 'z', 'agg'])
        y = y[0]
        data = retrieve_chart_data(raw_data, x, y, z)
        x_title = update_label_for_freq(x)
        y_title = update_label_for_freq(y)
        z_title = z
        data = data.sort_values([x, y])
        check_all_nan(data)
        dupe_cols = [x, y]
        if agg is not None:
            z_title = '{} ({})'.format(z_title, AGGS[agg])
            if agg == 'corr':
                data = data.dropna()
                data = data.set_index([x, y]).unstack().corr()
                data = data.stack().reset_index(0, drop=True)
                y_title = x_title
                dupe_cols = [
                    '{}{}'.format(col, i)
                    for i, col in enumerate(data.index.names)
                ]
                [x, y] = dupe_cols
                data.index.names = dupe_cols
                data = data.reset_index()
                data.loc[data[x] == data[y], z] = np.nan
                hm_kwargs = dict_merge(
                    hm_kwargs,
                    dict(colorscale=[[0, 'red'], [0.5, 'yellow'],
                                     [1.0, 'green']],
                         zmin=-1,
                         zmax=1))
            else:
                data = build_agg_data(data, x, y, inputs, agg, z=z)
        if not len(data):
            raise Exception('No data returned for this computation!')
        check_exceptions(
            data[dupe_cols],
            agg != 'corr',
            data_limit=40000,
            limit_msg=
            'Heatmap exceeds {} cells, cannot render. Please apply filter...')
        dtypes = {
            c: classify_type(dtype)
            for c, dtype in get_dtypes(data).items()
        }
        data_f, _ = chart_formatters(data)
        data = data_f.format_df(data)
        data = data.sort_values([x, y])
        data = data.set_index([x, y])
        data = data.unstack(0)[z]

        x_data = weekday_tick_handler(data.columns, x)
        y_data = weekday_tick_handler(data.index.values, y)
        heat_data = data.values

        x_axis = dict_merge({
            'title': x_title,
            'tickangle': -20
        }, build_spaced_ticks(x_data))
        if dtypes.get(x) == 'I':
            x_axis['tickformat'] = '.0f'

        y_axis = dict_merge({
            'title': y_title,
            'tickangle': -20
        }, build_spaced_ticks(y_data))
        if dtypes.get(y) == 'I':
            y_axis['tickformat'] = '.0f'

        hovertemplate = ''.join([
            x_title, ': %{customdata[0]}<br>', y_title,
            ': %{customdata[1]}<br>', z_title, ': %{z}<extra></extra>'
        ])
        hm_kwargs = dict_merge(
            hm_kwargs,
            dict(z=heat_data,
                 colorbar={'title': z_title},
                 hoverinfo='x+y+z',
                 hovertemplate=hovertemplate,
                 customdata=[[[xd, yd] for xd in x_data] for yd in y_data]))
        return wrapper(
            dcc.Graph(id='heatmap-graph-{}'.format(y),
                      style={
                          'margin-right': 'auto',
                          'margin-left': 'auto',
                          'height': 600
                      },
                      figure=dict(data=[go.Heatmap(**hm_kwargs)],
                                  layout=build_layout(
                                      dict_merge(
                                          dict(xaxis=x_axis,
                                               yaxis=y_axis,
                                               xaxis_zeroline=False,
                                               yaxis_zeroline=False),
                                          build_title(x, y, z=z, agg=agg))))))
    except BaseException as e:
        return build_error(str(e), str(traceback.format_exc()))
コード例 #3
0
def test_build_agg_data():
    with pytest.raises(NotImplementedError):
        chart_utils.build_agg_data(None, None, None, None, "rolling", z="z")
    with pytest.raises(NotImplementedError):
        chart_utils.build_agg_data(None, None, None, None, "corr")
コード例 #4
0
ファイル: test_charts.py プロジェクト: ktaranov/dtale
def test_build_agg_data():
    with pytest.raises(NotImplementedError):
        chart_utils.build_agg_data(None, None, None, None, 'rolling', z='z')
    with pytest.raises(NotImplementedError):
        chart_utils.build_agg_data(None, None, None, None, 'corr')