def build_histogram(data_id, col, query, point_filter): data = run_query( handle_predefined(data_id), query, global_state.get_context_variables(data_id), ) query, _ = build_group_inputs_filter(data, [point_filter]) data = run_query(data, query) s = data[~pd.isnull(data[col])][col] hist_data, hist_labels = np.histogram(s, bins=10) hist_labels = list( map(lambda x: json_float(x, precision=3), hist_labels[1:])) axes_builder = build_axes( dict( data=dict(all=dict(Frequency=hist_data, Bins=hist_labels)), min=dict(Frequency=0), max=dict(Frequency=max(hist_data)), ), "Bins", dict(type="single", data={}), ) hist_data = dict(data={"all": dict(x=hist_labels, Frequency=hist_data)}) bars = bar_builder( hist_data, "Bins", ["Frequency"], axes_builder, chart_builder_passthru, modal=True, ) bars.figure["layout"]["xaxis"]["type"] = "category" bars.figure["layout"]["title"]["text"] = "{} {} ({} {})".format( text("Histogram of"), col, len(s), text("data points")) return bars
def test_build_axes(unittest): df = pd.DataFrame(dict(a=[1, 2, 3], b=[1, 2, 3], c=[4, 5, 6], d=[8, 9, 10], e=[11, 12, 13], f=[14, 15, 16])) with mock.patch('dtale.global_state.DATA', {'1': df}): y = ['b', 'c', 'd'] yaxis_data = dict(type='multi', data=dict(b=dict(min=1, max=4), c=dict(min=5, max=7), d=dict(min=8, max=10))) mins = dict(b=2, c=5, d=8) maxs = dict(b=4, c=6, d=10) axes = build_axes('1', 'a', yaxis_data, mins, maxs)(y) unittest.assertEqual(axes, ({ 'yaxis': {'title': 'b', 'range': [1, 4], 'tickformat': '.0f'}, 'yaxis2': {'title': 'c', 'overlaying': 'y', 'side': 'right', 'anchor': 'x', 'range': [5, 7], 'tickformat': '.0f'}, 'yaxis3': {'title': 'd', 'overlaying': 'y', 'side': 'left', 'anchor': 'free', 'position': 0.05, 'tickformat': '.0f'}, 'xaxis': {'domain': [0.1, 1], 'tickformat': '.0f', 'title': 'a'} }, True)) y.append('e') yaxis_data['data']['e'] = dict(min=11, max=13) mins['e'] = 11 maxs['e'] = 13 axes = build_axes('1', 'a', yaxis_data, mins, maxs)(y) unittest.assertEqual(axes, ({ 'yaxis': {'title': 'b', 'range': [1, 4], 'tickformat': '.0f'}, 'yaxis2': {'title': 'c', 'overlaying': 'y', 'side': 'right', 'anchor': 'x', 'range': [5, 7], 'tickformat': '.0f'}, 'yaxis3': {'title': 'd', 'overlaying': 'y', 'side': 'left', 'anchor': 'free', 'position': 0.05, 'tickformat': '.0f'}, 'yaxis4': {'title': 'e', 'overlaying': 'y', 'side': 'right', 'anchor': 'free', 'position': 0.95, 'tickformat': '.0f'}, 'xaxis': {'domain': [0.1, 0.8999999999999999], 'tickformat': '.0f', 'title': 'a'} }, True)) y.append('f') yaxis_data['data']['f'] = dict(min=14, max=17) mins['f'] = 14 maxs['f'] = 17 axes = build_axes('1', 'a', yaxis_data, mins, maxs)(y) unittest.assertEqual(axes, ({ 'yaxis': {'title': 'b', 'range': [1, 4], 'tickformat': '.0f'}, 'yaxis2': {'title': 'c', 'overlaying': 'y', 'side': 'right', 'anchor': 'x', 'range': [5, 7], 'tickformat': '.0f'}, 'yaxis3': {'title': 'd', 'overlaying': 'y', 'side': 'left', 'anchor': 'free', 'position': 0.05, 'tickformat': '.0f'}, 'yaxis4': {'title': 'e', 'overlaying': 'y', 'side': 'right', 'anchor': 'free', 'position': 0.95, 'tickformat': '.0f'}, 'yaxis5': {'title': 'f', 'overlaying': 'y', 'side': 'left', 'anchor': 'free', 'position': 0.1, 'tickformat': '.0f'}, 'xaxis': {'domain': [0.15000000000000002, 0.8999999999999999], 'tickformat': '.0f', 'title': 'a'} }, True)) df = pd.DataFrame(dict(a=[1, 2, 3], b=[1, 2, 3], c=[4, 5, 6])) with mock.patch('dtale.global_state.DATA', {'1': df}): y = ['b'] yaxis_data = dict(type='multi', data=dict(b=dict(min=1, max=4), c=dict(min=5, max=7), d=dict(min=8, max=10))) mins = dict(b=2, c=5, d=8) maxs = dict(b=4, c=6, d=10) axes = build_axes('1', 'a', yaxis_data, mins, maxs, z='c')(y) unittest.assertEqual(axes, ({ 'xaxis': {'title': 'a', 'tickformat': '.0f'}, 'yaxis': {'title': 'b', 'range': [1, 4], 'tickformat': '.0f'}, 'zaxis': {'title': 'c', 'tickformat': '.0f'} }, False)) axes = build_axes('1', 'a', yaxis_data, mins, maxs, z='c', agg='corr')(y) unittest.assertEqual(axes, ({ 'xaxis': {'title': 'a', 'tickformat': '.0f'}, 'yaxis': {'title': 'b', 'range': [1, 4], 'tickformat': '.0f'}, 'zaxis': {'title': 'c (Correlation)', 'tickformat': '.0f'} }, False)) axes = build_axes('1', 'a', yaxis_data, mins, maxs, agg='corr')(y) unittest.assertEqual(axes, ({ 'xaxis': {'title': 'a', 'tickformat': '.0f'}, 'yaxis': {'title': 'b (Correlation)', 'range': [1, 4], 'tickformat': '.0f'}, }, False)) yaxis_data['type'] = 'single' axes = build_axes('1', 'a', yaxis_data, mins, maxs, agg='corr')(y) unittest.assertEqual(axes, ({ 'xaxis': {'title': 'a', 'tickformat': '.0f'}, 'yaxis': {'tickformat': '.0f', 'title': 'b (Correlation)'}, }, False))