Skip to content

Commit

Permalink
distplot; fix charting whole corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
interrogator committed Dec 9, 2019
1 parent 4a45684 commit 119859c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
32 changes: 22 additions & 10 deletions explorer/parts/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import dash
import pandas as pd
from buzz.dashview import _df_to_figure
from buzz.exceptions import DataTypeError
from dash import no_update
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

from .chart import _df_to_figure
from .helpers import (_cast_query, _get_specs_and_corpus, _special_search,
_translate_relative, _tuple_or_list, _update_concordance,
_update_conll, _update_frequencies)
Expand Down Expand Up @@ -75,10 +75,20 @@ def render_content(tab, search_from, **kwargs):
State(f"chart-top-n-{i}", "value"),
State(f"chart-transpose-{i}", "on"),
State("session-tables", "data"),
State("session-configs", "data"),
State("slug", "title"),
],
)
def _new_chart(
n_clicks, table_from, chart_type, top_n, transpose, session_tables, **kwargs
n_clicks,
table_from,
chart_type,
top_n,
transpose,
session_tables,
conf,
slug,
**kwargs,
):
"""
Make new chart by kind. Do it 5 times, once for each chart space
Expand All @@ -88,8 +98,13 @@ def _new_chart(
return no_update
# get correct dataset to chart

this_table = session_tables[str(table_from)]
df = FREQUENCY_TABLES[_tuple_or_list(this_table, tuple)]
conf = conf[slug]

if str(table_from) in session_tables:
this_table = session_tables[str(table_from)]
df = FREQUENCY_TABLES[_tuple_or_list(this_table, tuple)]
else:
df = INITIAL_TABLES[slug]

# transpose and cut down items to plot
if transpose:
Expand Down Expand Up @@ -158,7 +173,7 @@ def _new_search(
session_search,
session_clicks_clear,
session_clicks_show,
url,
slug,
**kwargs,
):
"""
Expand All @@ -170,7 +185,6 @@ def _new_search(
if n_clicks is None:
return [no_update] * 11

slug = url.rstrip("/").split("/")[-1]
conf = conf[slug]
add_governor = conf["add_governor"]
max_row, max_col = conf["table_size"]
Expand Down Expand Up @@ -368,7 +382,7 @@ def _new_table(
session_search,
session_tables,
session_click_table,
url,
slug,
**kwargs,
):
"""
Expand All @@ -378,7 +392,6 @@ def _new_table(
if n_clicks is None:
raise PreventUpdate

slug = url.rstrip("/").split("/")[-1]
conf = conf[slug]

# because no option below can return initial table, rows can now be deleted
Expand Down Expand Up @@ -511,14 +524,13 @@ def _new_table(
State("slug", "title"),
],
)
def _new_conc(n_clicks, show, search_from, conf, session_search, url, **kwargs):
def _new_conc(n_clicks, show, search_from, conf, session_search, slug, **kwargs):
"""
Callback for concordance. We just pick what to show and where from...
"""
if n_clicks is None:
return [no_update] * 4

slug = url.rstrip("/").split("/")[-1]
conf = conf[slug]

# easy validation!
Expand Down
36 changes: 32 additions & 4 deletions explorer/parts/chart.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import numpy as np
import plotly.express as px
import plotly.figure_factory as ff
import plotly.graph_objects as go

CHART_TYPES = {"line", "bar", "heatmap", "area", "stacked_bar"} # "pie"
CHART_TYPES = {
"line",
"bar",
"heatmap",
"area",
"stacked_bar",
"distplot",
"stacked_distplot",
} # "pie"


def _bar_chart(row):
Expand All @@ -24,6 +35,13 @@ def _area_chart(row):
)


def _distplot(df):
data = df.T.values
labels = df.columns
result = ff.create_distplot(data, labels)
return result["data"], result["layout"]


def _heatmap(df):
return [go.Heatmap(z=df.T.values, x=list(df.index), y=list(df.columns))]

Expand All @@ -38,12 +56,22 @@ def _df_to_figure(df, kind="bar"):
heatmap=_heatmap,
area=_area_chart,
stacked_bar=_bar_chart,
distplot=_distplot,
stacked_distplot=_distplot,
)
plotter = plotters[kind]

datapoints = plotter(df) if kind == "heatmap" else df.T.apply(plotter)
layout = {}
if kind == "heatmap":
datapoints = plotter(df)
elif kind.endswith("distplot"):
datapoints, layout = plotter(df)
else:
datapoints = df.apply(plotter)

layout = dict(width=1300)
if kind == "stacked_bar":
layout["width"] = 1300

if kind.startswith("stacked"):
layout["barmode"] = "stack"

return dict(data=datapoints, layout=layout)

0 comments on commit 119859c

Please sign in to comment.