Skip to content

Commit

Permalink
simplify table keys, fewer unexpected problem
Browse files Browse the repository at this point in the history
  • Loading branch information
interrogator committed Aug 23, 2019
1 parent 5f1745c commit 0044f81
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 45 deletions.
2 changes: 1 addition & 1 deletion buzzword/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _choose_correct_page(pathname, configs):
layout = _get_explore_layout(slug, configs)
if layout:
return layout
print("LAYOUT ERROR: ", slug, list(CORPORA.keys()))
# layout does not exist. unloaded corpus?
if not pathname:
return start.layout
else:
Expand Down
46 changes: 15 additions & 31 deletions buzzword/parts/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _new_chart(n_clicks, table_from, chart_type, top_n, transpose, session_table
# get correct dataset to chart

this_table = session_tables[str(table_from)]
df = FREQUENCY_TABLES[_tuple_or_list(this_table[:6], tuple)]
df = FREQUENCY_TABLES[_tuple_or_list(this_table, tuple)]

# transpose and cut down items to plot
if transpose:
Expand Down Expand Up @@ -218,7 +218,7 @@ def _new_search(
corpus = CORPORA[slug]
corpus = corpus.iloc[:max_row, :max_col]
cols, data = _update_datatable(corpus, corpus, drop_govs=add_governor)
name = _make_search_name(conf["corpus_name"], len(corpus))
name = _make_search_name(conf["corpus_name"], len(corpus), session_search)
search_from = [dict(value=0, label=name)]
# set number of clicks at last moment
session_clicks_clear = cleared
Expand Down Expand Up @@ -267,7 +267,7 @@ def _new_search(
corpus, df, drop_govs=add_governor, deletable=True
)
if not msg:
name = _make_search_name(this_search, len(corpus))
name = _make_search_name(this_search, len(corpus), session_search)
option = dict(value=new_value, label=name)
search_from_options.append(option)
elif exists:
Expand Down Expand Up @@ -361,10 +361,6 @@ def _new_table(

specs, corpus = _get_specs_and_corpus(search_from, session_search, CORPORA, slug)

# do not store the df._n in store EVER
if isinstance(specs, (list, tuple)):
specs = specs[:-1]

sort = sort or "total"

relative, keyness = _translate_relative(relkey, CORPORA[slug])
Expand All @@ -382,41 +378,31 @@ def _new_table(
updating = True

msg = _table_error(show, subcorpora, updating)
idx = len(session_tables) + 1
this_table = [specs, show, subcorpora, relative, keyness, sort, idx, 0]
this_table_list = [specs, list(show), subcorpora, relative, keyness, sort]
this_table_tuple = _tuple_or_list(this_table_list, tuple)

# if table already made, use that one
key, exists = next(
((k, v) for k, v in session_tables.items() if this_table[:6] == v[:6]),
(False, False),
)
if exists is not False:
exists_as_tuple = _tuple_or_list(exists, tuple)
key = next((k for k, v in session_tables.items() if this_table_list == v), False)
idx = key if key is not False else len(session_tables) + 1

# if we are updating the table:
if updating:
# get the whole table from master dict of them
table = FREQUENCY_TABLES[exists_as_tuple[:6]]
# increment the last number, shows number of edits
exists_as_list = _tuple_or_list(exists, list)
exists_as_list[-1] += 1
# re-store it with the correct number of edits
session_tables[key] = exists_as_list
table = FREQUENCY_TABLES[this_table_tuple]
# fix rows and columns
old_dim = table.shape
table = table[[i["id"] for i in current_cols[1:]]]
table = table.loc[[i["_" + table.index.name] for i in current_data]]
# store table again with same key
FREQUENCY_TABLES[_tuple_or_list(exists_as_list[:6], tuple)] = table
elif exists:
FREQUENCY_TABLES[this_table_tuple] = table
elif key is not False:
msg = "Table already exists. Switching to that one to save memory."
table = FREQUENCY_TABLES[exists_as_tuple[:6]]
table = FREQUENCY_TABLES[this_table_tuple]
# if there was a validation problem, juse use last table (?)
elif msg:
if session_tables:
# todo: figure this out...use current table instead?
key, value = list(session_tables.items())[-1]
table = FREQUENCY_TABLES[_tuple_or_list(value[:6], tuple)]
table = FREQUENCY_TABLES[_tuple_or_list(value, tuple)]
# todo: more here?
else:
table = INITIAL_TABLES[slug]
Expand All @@ -438,8 +424,8 @@ def _new_table(
relative = None

# then store the search information in store/freq table spaces
session_tables[idx] = _tuple_or_list(this_table, list)
FREQUENCY_TABLES[_tuple_or_list(this_table[:6], tuple)] = table
session_tables[idx] = this_table_list
FREQUENCY_TABLES[this_table_tuple] = table

if updating:
cols, data = current_cols, current_data
Expand All @@ -451,11 +437,9 @@ def _new_table(
csv_path = "todo"

if not msg and not updating:
table_name = _make_table_name(this_table)
table_name = _make_table_name(this_table_list)
option = dict(value=idx, label=table_name)
table_from_options.append(option)
elif exists or updating:
idx = key
return (
cols,
data,
Expand Down
4 changes: 2 additions & 2 deletions buzzword/parts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def _get_specs_and_corpus(search_from, searches, corpora, slug):
corpora[slug] = loaded
# if the user wants the corpus, return that
if not int(search_from):
return slug, _get_corpus(slug)
return 0, _get_corpus(slug)
# otherwise, get the search result (i.e. _n col) and make corpus
exists = searches[str(search_from)]
return exists, _get_corpus(slug).iloc[exists[-1]]
return int(search_from), _get_corpus(slug).iloc[exists[-1]]


def _tuple_or_list(this_identifier, typ):
Expand Down
15 changes: 6 additions & 9 deletions buzzword/parts/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _make_table_name(history):
"""
if history == "initial":
return "Part of speech tags by filename"
specs, show, subcorpora, relative, keyness, sort, n, updates = history
specs, show, subcorpora, relative, keyness, sort = history
subcorpora = (
SHORT_TO_LONG_NAME.get(subcorpora, subcorpora).lower().replace("_", " ")
)
Expand All @@ -36,12 +36,9 @@ def _make_table_name(history):
if relative is False and keyness is False:
relkey = " showing absolute frequencies"
basic = f"{show} by {subcorpora}{relkey}, sorting by {sort}"
if updates:
basic += f", {updates} edits"
parent = specs[4] if isinstance(specs, (tuple, list)) else 0
if not parent:
if not int(specs):
return basic
return f"{basic} -- from search #{parent}"
return f"{basic} -- from search #{specs}"


def _format_size(size):
Expand All @@ -56,7 +53,7 @@ def _format_size(size):
return f"{size/1000:.2f} kB"


def _make_search_name(history, size):
def _make_search_name(history, size, searches):
"""
Generate a search name from its history
"""
Expand All @@ -77,9 +74,9 @@ def _make_search_name(history, size):
freq = f"(n={n_results:n}{rel_last}/{relative_corpus:.2f}%)"
basic = f"{col} {no}matching '{search_string}' {freq}"
hyphen = ""
while isinstance(previous, (tuple, list)):
while previous:
hyphen += "──"
previous = previous[0]
previous = int(searches[str(previous)][0])
if hyphen:
basic = f"└{hyphen} " + basic
return f"({n}) {basic}"
Expand Down
6 changes: 4 additions & 2 deletions buzzword/parts/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ def _build_chart_space(table, config):
figure=figure,
style={"height": "60vh", "width": "95vw"},
)
chart = dcc.Graph(**chart_data)
chart = dcc.Loading(
type="default",
children=[dcc.Graph(**chart_data)])
chart_space = html.Div([toolbar, chart])
name = f"Chart #{chart_num}"
summary = html.Summary(name, style=style.CHART_SUMMARY)
Expand All @@ -367,7 +369,7 @@ def _make_tabs(corpus, table, config):
frequencies = _build_frequencies_space(corpus, table, config)
chart = _build_chart_space(table, config)
concordance = _build_concordance_space(corpus, config)
label = _make_search_name(config["corpus_name"], config["len"])
label = _make_search_name(config["corpus_name"], config["len"], dict())
search_from = [dict(value=0, label=label)]
clear = html.Button("Clear history", id="clear-history", style=style.MARGIN_5_MONO)
dropdown = dcc.Dropdown(
Expand Down

0 comments on commit 0044f81

Please sign in to comment.