From 87d73e59cb858203fd607cd3d8b8bd08b949fecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20M=C3=A4hler?= Date: Sun, 23 Aug 2020 12:09:53 +0200 Subject: [PATCH] Resolves #32 --- .../1_quantitative_analysis_of_wti.ipynb | 7 +- 1_quantitative_analysis/party_analysis_gui.py | 138 +++++++++--------- 3_text_analysis/textacy_corpus_gui.py | 4 +- 3 files changed, 78 insertions(+), 71 deletions(-) diff --git a/1_quantitative_analysis/1_quantitative_analysis_of_wti.ipynb b/1_quantitative_analysis/1_quantitative_analysis_of_wti.ipynb index 73cac8a..6da199d 100644 --- a/1_quantitative_analysis/1_quantitative_analysis_of_wti.ipynb +++ b/1_quantitative_analysis/1_quantitative_analysis_of_wti.ipynb @@ -55,7 +55,8 @@ "cell_type": "code", "execution_count": null, "metadata": { - "code_folding": [] + "code_folding": [], + "tags": [] }, "outputs": [], "source": [ @@ -177,7 +178,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.7.7-final" }, "toc": { "base_numbering": 1, @@ -233,4 +234,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/1_quantitative_analysis/party_analysis_gui.py b/1_quantitative_analysis/party_analysis_gui.py index 2c63466..8209b0a 100644 --- a/1_quantitative_analysis/party_analysis_gui.py +++ b/1_quantitative_analysis/party_analysis_gui.py @@ -1,5 +1,6 @@ import ipywidgets as widgets import itertools +import types import pandas as pd import common.widgets_config as widgets_config import common.config as config @@ -130,101 +131,106 @@ def period_group_window(period_group_index): return min(periods), max(periods) + treaty_source_options = ['LTS', 'UNTS', 'UNXX'] party_preset_options = wti_index.get_party_preset_options() period_group_index_widget = widgets_config.period_group_widget(index_as_value=True) min_year, max_year = period_group_window(period_group_index_widget.value) - year_limit_widget = widgets_config.rangeslider('Window', min_year, max_year, [min_year, max_year], layout=lw('900px'), continuous_update=False) - - party_name_widget = widgets_config.party_name_widget() - normalize_values_widget = widgets_config.toggle('Display %', False, icon='', layout=lw('100px')) - chart_type_name_widget = widgets_config.dropdown('Output', config.CHART_TYPE_NAME_OPTIONS, "plot_stacked_bar", layout=lw('200px')) - - plot_style_widget = widgets_config.plot_style_widget() - top_n_parties_widget = widgets_config.slider('Top #', 0, 10, 0, continuous_update=False, layout=lw(width='200px')) - party_preset_widget = widgets_config.dropdown('Presets', party_preset_options, None, layout=lw(width='200px')) - parties_widget = widgets_config.parties_widget(options=wti_index.get_countries_list(excludes=['ALL', 'ALL OTHER']), value=['FRANCE'], rows=8,) - treaty_filter_widget = widgets_config.dropdown('Filter', config.TREATY_FILTER_OPTIONS, 'is_cultural', layout=lw(width='150px')) - extra_category_widget = widgets_config.dropdown('Include', OTHER_CATEGORY_OPTIONS, '', layout=lw(width='150px')) - #overlay_option_widget = widgets_config.toggle('Overlay', True, icon='', layout=lw()) - progress_widget = widgets_config.progress(0, 5, 1, 0, layout=lw('95%')) - info_widget = widgets.Label(value="", layout=lw('95%')) + gui = types.SimpleNamespace( + year_limit = widgets_config.rangeslider('Window', min_year, max_year, [min_year, max_year], layout=lw('900px'), continuous_update=False), + sources=widgets_config.select_multiple(description='Sources', options=treaty_source_options, values=treaty_source_options, disabled=True, layout=lw('200px')), + period_group_index=period_group_index_widget, + party_name = widgets_config.party_name_widget(), + normalize_values = widgets_config.toggle('Display %', False, icon='', layout=lw('100px')), + chart_type_name = widgets_config.dropdown('Output', config.CHART_TYPE_NAME_OPTIONS, "plot_stacked_bar", layout=lw('200px')), + + plot_style = widgets_config.plot_style_widget(), + top_n_parties = widgets_config.slider('Top #', 0, 10, 0, continuous_update=False, layout=lw(width='200px')), + party_preset = widgets_config.dropdown('Presets', party_preset_options, None, layout=lw(width='200px')), + parties = widgets_config.parties_widget(options=wti_index.get_countries_list(excludes=['ALL', 'ALL OTHER']), value=['FRANCE'], rows=8,), + treaty_filter = widgets_config.dropdown('Filter', config.TREATY_FILTER_OPTIONS, 'is_cultural', layout=lw(width='200px')), + extra_category = widgets_config.dropdown('Include', OTHER_CATEGORY_OPTIONS, '', layout=lw(width='200px')), + #overlay_option = widgets_config.toggle('Overlay', True, icon='', layout=lw()), + progress = widgets_config.progress(0, 5, 1, 0, layout=lw('95%')), + info = widgets.Label(value="", layout=lw('95%')) + ) def stepper(step=None): - progress_widget.value = progress_widget.value + 1 if step is None else step + gui.progress.value = gui.progress.value + 1 if step is None else step itw = widgets.interactive( display_quantity_by_party, period_group_index=period_group_index_widget, - year_limit=year_limit_widget, - party_name=party_name_widget, - parties=parties_widget, - treaty_filter=treaty_filter_widget, - extra_category=extra_category_widget, - normalize_values=normalize_values_widget, - chart_type_name=chart_type_name_widget, - plot_style=plot_style_widget, - top_n_parties=top_n_parties_widget, - overlay=widgets.fixed(False), # overlay_option_widget, + year_limit=gui.year_limit, + party_name=gui.party_name, + parties=gui.parties, + treaty_filter=gui.treaty_filter, + extra_category=gui.extra_category, + normalize_values=gui.normalize_values, + chart_type_name=gui.chart_type_name, + plot_style=gui.plot_style, + top_n_parties=gui.top_n_parties, + overlay=widgets.fixed(False), # overlay_option, progress=widgets.fixed(stepper), wti_index=widgets.fixed(wti_index), - print_args=widgets.fixed(print_args) + print_args=widgets.fixed(print_args), + treaty_sources=gui.sources ) def on_party_preset_change(change): # pylint: disable=W0613 - if party_preset_widget.value is None: + if gui.party_preset.value is None: return try: - parties_widget.unobserve(on_parties_change, names='value') - top_n_parties_widget.unobserve(on_top_n_parties_change, names='value') - if 'ALL' in party_preset_widget.value: - parties_widget.value = parties_widget.options + gui.parties.unobserve(on_parties_change, names='value') + gui.top_n_parties.unobserve(on_top_n_parties_change, names='value') + if 'ALL' in gui.party_preset.value: + gui.parties.value = gui.parties.options else: - parties_widget.value = party_preset_widget.value + gui.parties.value = gui.party_preset.value - if top_n_parties_widget.value > 0: - top_n_parties_widget.value = 0 + if gui.top_n_parties.value > 0: + gui.top_n_parties.value = 0 except Exception as ex: logger.info(ex) finally: - parties_widget.observe(on_parties_change, names='value') - top_n_parties_widget.observe(on_top_n_parties_change, names='value') + gui.parties.observe(on_parties_change, names='value') + gui.top_n_parties.observe(on_top_n_parties_change, names='value') def on_parties_change(change): # pylint: disable=W0613 try: - if top_n_parties_widget.value != 0: - top_n_parties_widget.unobserve(on_top_n_parties_change, names='value') - top_n_parties_widget.value = 0 - top_n_parties_widget.observe(on_top_n_parties_change, names='value') + if gui.top_n_parties.value != 0: + gui.top_n_parties.unobserve(on_top_n_parties_change, names='value') + gui.top_n_parties.value = 0 + gui.top_n_parties.observe(on_top_n_parties_change, names='value') except Exception as ex: # pylint: disable=W0703 logger.info(ex) def on_top_n_parties_change(change): # pylint: disable=W0613 try: - if top_n_parties_widget.value > 0: - parties_widget.unobserve(on_parties_change, names='value') - parties_widget.disabled = True - party_preset_widget.disabled = True - if len(parties_widget.value) > 0: - parties_widget.value = [] + if gui.top_n_parties.value > 0: + gui.parties.unobserve(on_parties_change, names='value') + gui.parties.disabled = True + gui.party_preset.disabled = True + if len(gui.parties.value) > 0: + gui.parties.value = [] else: - parties_widget.observe(on_parties_change, names='value') - parties_widget.disabled = False - party_preset_widget.disabled = False + gui.parties.observe(on_parties_change, names='value') + gui.parties.disabled = False + gui.party_preset.disabled = False except Exception as ex: # pylint: disable=W0703 logger.info(ex) def set_years_window(period_group_index): try: min_year, max_year = period_group_window(period_group_index) - year_limit_widget.min, year_limit_widget.max = min_year, max_year - year_limit_widget.value = (min_year, max_year) + gui.year_limit.min, gui.year_limit.max = min_year, max_year + gui.year_limit.value = (min_year, max_year) period_group = config.DEFAULT_PERIOD_GROUPS[period_group_index] - year_limit_widget.disabled = (period_group['type'] != 'range') + gui.year_limit.disabled = (period_group['type'] != 'range') except Exception as ex: # pylint: disable=W0703 logger.info(ex) @@ -232,26 +238,26 @@ def on_period_change(change): period_group_index = change['new'] set_years_window(period_group_index) - parties_widget.observe(on_parties_change, names='value') - period_group_index_widget.observe(on_period_change, names='value') - party_preset_widget.observe(on_party_preset_change, names='value') - top_n_parties_widget.observe(on_top_n_parties_change, names='value') + gui.parties.observe(on_parties_change, names='value') + gui.period_group_index.observe(on_period_change, names='value') + gui.party_preset.observe(on_party_preset_change, names='value') + gui.top_n_parties.observe(on_top_n_parties_change, names='value') - set_years_window(period_group_index_widget.value) + set_years_window(gui.period_group_index.value) boxes = widgets.HBox([ - widgets.VBox([ period_group_index_widget, party_name_widget, top_n_parties_widget, party_preset_widget]), - widgets.VBox([ parties_widget ]), + widgets.VBox([ gui.period_group_index, gui.party_name, gui.top_n_parties, gui.party_preset]), + widgets.VBox([ gui.parties ]), widgets.VBox([ widgets.HBox([ - widgets.VBox([ treaty_filter_widget, extra_category_widget]), - widgets.VBox([ chart_type_name_widget, plot_style_widget]), - widgets.VBox([ normalize_values_widget ]) + widgets.VBox([ gui.treaty_filter, gui.extra_category, gui.sources]), + widgets.VBox([ gui.chart_type_name, gui.plot_style]), + widgets.VBox([ gui.normalize_values ]) ]), - progress_widget, - info_widget + gui.progress, + gui.info ]) ]) - display(widgets.VBox([boxes, year_limit_widget, itw.children[-1]])) + display(widgets.VBox([boxes, gui.year_limit, itw.children[-1]])) itw.update() \ No newline at end of file diff --git a/3_text_analysis/textacy_corpus_gui.py b/3_text_analysis/textacy_corpus_gui.py index 5ceae7a..b8af066 100644 --- a/3_text_analysis/textacy_corpus_gui.py +++ b/3_text_analysis/textacy_corpus_gui.py @@ -95,7 +95,7 @@ def display_corpus_load_gui(data_folder, wti_index, container): lw = lambda w: widgets.Layout(width=w) - treaty_sources = ['LTS', 'UNTS', 'UNXX'] + treaty_source_options = ['LTS', 'UNTS', 'UNXX'] language_options = { config.LANGUAGE_MAP[k].title(): k for k in config.LANGUAGE_MAP.keys() if k in [ 'en', 'fr' ] @@ -123,7 +123,7 @@ def display_corpus_load_gui(data_folder, wti_index, container): options=corpus_files, value=corpus_files[-1], layout=lw('400px')), - sources=widgets_config.select_multiple(description='Sources', options=treaty_sources, values=treaty_sources, disabled=True, layout=lw('180px')), + sources=widgets_config.select_multiple(description='Sources', options=treaty_source_options, values=treaty_source_options, disabled=True, layout=lw('180px')), language=widgets_config.dropdown(description='Language', options=language_options, value='en', layout=lw('180px')), period_group=widgets_config.dropdown('Period', period_group_options, 'years_1935-1972', disabled=False, layout=lw('180px')),