Skip to content

finished before demo -real one #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 78 additions & 70 deletions Notebook_Time_Series_Silviu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[1.06 seconds] Normalized Region East Asia and Pacific\n",
"[2.28 seconds] Normalized Region Europe and Central Asia\n",
"[1.32 seconds] Normalized Region Latin America and Caribbean\n",
"[0.85 seconds] Normalized Region Middle East and North Africa\n",
"[0.11 seconds] Normalized Region North America\n",
"[0.43 seconds] Normalized Region South Asia\n",
"[2.16 seconds] Normalized Region Sub-Saharan Africa\n"
"[2.16 seconds] Normalized Region East Asia and Pacific\n",
"[6.21 seconds] Normalized Region Europe and Central Asia\n",
"[2.77 seconds] Normalized Region Latin America and Caribbean\n",
"[1.76 seconds] Normalized Region Middle East and North Africa\n",
"[0.22 seconds] Normalized Region North America\n",
"[0.85 seconds] Normalized Region South Asia\n",
"[4.90 seconds] Normalized Region Sub-Saharan Africa\n"
]
}
],
Expand Down Expand Up @@ -171,7 +171,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1174b81b1ae14433a7cbcb21a6f4ee66",
"model_id": "d3efc234551346128632c5c0f5d0513d",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -185,7 +185,7 @@
{
"data": {
"text/plain": [
"<function __main__.timeWindowCountry(By, Zone, Threshold, Years)>"
"<function __main__.time_window_zone(by, zone, threshold, years)>"
]
},
"execution_count": 6,
Expand All @@ -194,41 +194,41 @@
}
],
"source": [
"def timeWindowCountry(By, Zone, Threshold , Years):\n",
"def time_window_zone(by, zone, threshold , years):\n",
" # Update Widget Status\n",
" if By == 'Country':\n",
" if by == 'Country':\n",
" if status_widget.filter_by != \"Country\":\n",
" status_widget.filter_by = \"Country\"\n",
" zone_drop.options = country_list\n",
" return\n",
" if status_widget.zone != Zone:\n",
" if status_widget.zone != zone:\n",
" #Search for entries of the country.\n",
" status_widget.data = df_gold_index.loc[df_gold_index.index.get_level_values('Country') == Zone]\n",
" status_widget.zone = Zone\n",
" status_widget.data = df_gold_index.loc[df_gold_index.index.get_level_values('Country') == zone]\n",
" status_widget.zone = zone\n",
"\n",
" elif By == 'Region':\n",
" elif by == 'Region':\n",
" if status_widget.filter_by != \"Region\":\n",
" status_widget.filter_by = \"Region\"\n",
" zone_drop.options = region_list\n",
" return\n",
" if status_widget.zone != Zone:\n",
" if status_widget.zone != zone:\n",
" #Search for entries of the country.\n",
" status_widget.data = countries_by_region[Zone] # load_by_region(Zone)\n",
" status_widget.zone = Zone\n",
" status_widget.data = countries_by_region[zone] # load_by_region(Zone)\n",
" status_widget.zone = zone\n",
" \n",
" # Update Widget Data\n",
" df_zone = status_widget.data\n",
" \n",
" #Load the selected year range and the global range.\n",
" df_time = searchTimeSeries(Threshold, Years[0], Years[1], True, df_zone)\n",
" df_global = searchTimeSeries(Threshold, Years[0], Years[1], False, df_zone)\n",
" df_time = searchTimeSeries(threshold, years[0], years[1], True, df_zone)\n",
" df_global = searchTimeSeries(threshold, years[0], years[1], False, df_zone)\n",
"\n",
" # Display Data\n",
" if Years[0] > Years[1]: return print(\"Please, select a valid range of years.\")\n",
" if years[0] > years[1]: return print(\"Please, select a valid range of years.\")\n",
" \n",
" space = \"\\xa0\" * 10\n",
" try:\n",
" df_time = styler_method(df_time, str(Years[0]) + '-' + str(Years[1]))._repr_html_()\n",
" df_time = styler_method(df_time, str(years[0]) + '-' + str(years[1]))._repr_html_()\n",
" except: \n",
" df_time = 'No indicators available for the selected parameters'\n",
" \n",
Expand Down Expand Up @@ -266,7 +266,7 @@
" description='Threshold:',\n",
")\n",
"\n",
"widgets.interact(timeWindowCountry, By = by_drop, Zone = zone_drop, Threshold = floatslider, Years = intslider)"
"widgets.interact(time_window_zone, by = by_drop, zone = zone_drop, threshold = floatslider, years = intslider)"
]
},
{
Expand All @@ -284,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -311,12 +311,12 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"indicators = list(df_gold.columns[3:])\n",
"indicators.remove('GDP')"
"indicators = list(df_gold_index.columns)\n",
"indicators.remove('GDP')\n"
]
},
{
Expand All @@ -338,13 +338,13 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f257d561549a4a4cacc5ff621a39e332",
"model_id": "9c7237ec068d4abd9ec6863b228fb0f2",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -358,31 +358,31 @@
{
"data": {
"text/plain": [
"<function __main__.tableHighCountry(By, Zone)>"
"<function __main__.table_high_country(by, zone)>"
]
},
"execution_count": 18,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tableHighCountry(By, Zone):\n",
" if By == 'Country':\n",
" if len(zone_drop.options) == len(region_list): \n",
" zone_drop.options = country_list\n",
"def table_high_country(by, zone):\n",
" if by == 'Country':\n",
" if len(zone_drop_high.options) == len(region_list): \n",
" zone_drop_high.options = country_list\n",
" return\n",
" #Search for the entries of the country.\n",
" df_zone = df_gold_index.loc[df_gold_index.index.get_level_values('Country') == Zone]\n",
" df_zone = df_gold_index.loc[df_gold_index.index.get_level_values('Country') == zone]\n",
" min_diff = 5\n",
" \n",
" \n",
" elif By == 'Region':\n",
" if len(zone_drop.options) == len(country_list): \n",
" zone_drop.options = region_list\n",
" elif by == 'Region':\n",
" if len(zone_drop_high.options) == len(country_list): \n",
" zone_drop_high.options = region_list\n",
" return\n",
" #Search for the entries of the region and normalize.\n",
" df_zone = countries_by_region[Zone]\n",
" df_zone = countries_by_region[zone]\n",
" min_diff = 2\n",
" \n",
" df_highest = init_highest_table()\n",
Expand Down Expand Up @@ -426,18 +426,18 @@
"\n",
" display(df_highest)\n",
"\n",
"by_drop = widgets.Dropdown(\n",
"by_drop_high = widgets.Dropdown(\n",
" options= ['Country', 'Region'],\n",
" description='By: ',\n",
")\n",
"\n",
"zone_drop = widgets.Dropdown(\n",
"zone_drop_high = widgets.Dropdown(\n",
" options= country_list,\n",
" value ='Afghanistan',\n",
" description='Zone:',\n",
")\n",
"\n",
"widgets.interact(tableHighCountry, By = by_drop, Zone = zone_drop)\n"
"widgets.interact(table_high_country, by = by_drop_high, zone = zone_drop_high)\n"
]
},
{
Expand All @@ -451,18 +451,18 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8ded79576a674a89989070716a2e6336",
"model_id": "a2e489fb3c2e434f83f9dc653282f411",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='Country: ', options=('Afghanistan', 'Albania', 'Algeria', 'Angola'…"
"interactive(children=(Dropdown(description='By: ', options=('Country', 'Region'), value='Country'), Dropdown(d…"
]
},
"metadata": {},
Expand All @@ -471,46 +471,61 @@
{
"data": {
"text/plain": [
"<function __main__.plotYearRange(Zone, Indicator, Years)>"
"<function __main__.plot_year_range(by, zone, indicator, years)>"
]
},
"execution_count": 31,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"indicators = list(df_gold_index.columns)\n",
"indicators.remove('GDP')\n",
"\n",
"def plotYearRange(Zone, Indicator, Years):\n",
" df_aux = df_gold_index.loc[df_gold_index.index.get_level_values('Country') == Zone]\n",
" df_aux = df_aux.loc[(df_aux.index.get_level_values(\"Year\") >= Years[0]) & (df_aux.index.get_level_values(\"Year\") <= Years[1])]\n",
" spear = stats.spearmanr(df_aux[Indicator], df_aux['GDP'])\n",
" df_aux = normalize_by_country(df_aux).reset_index(drop=False)\n",
"def plot_year_range(by, zone, indicator, years):\n",
" if by == 'Country':\n",
" if list(zone_drop_plot.options) != country_list:\n",
" zone_drop_plot.options = country_list\n",
" return\n",
" df_zone = df_gold_index.loc[df_gold_index.index.get_level_values('Country') == zone]\n",
" \n",
" elif by == 'Region':\n",
" if list(zone_drop_plot.options) != region_list:\n",
" zone_drop_plot.options = region_list\n",
" return\n",
" df_zone = countries_by_region[zone]\n",
"\n",
" df_zone = df_zone.loc[(df_zone.index.get_level_values(\"Year\") >= years[0]) & (df_zone.index.get_level_values(\"Year\") <= years[1])]\n",
" spear = stats.spearmanr(df_zone[indicator], df_zone['GDP'])\n",
" df_zone = normalize_by_country(df_zone)\n",
"\n",
"\n",
" #In case if by is region it groups by year.\n",
" df_zone = df_zone.loc[df_zone.index.get_level_values(by_drop_plot.value) == zone, ['GDP', indicator]].groupby(level = 'Year').median()\n",
"\n",
" print(spear)\n",
" plt.figure(figsize=(6,6))\n",
" plt.plot(df_aux[\"Year\"], df_aux[\"GDP\"], color=\"red\", label = 'GDP')\n",
" plt.plot(df_aux[\"Year\"], df_aux[Indicator], color=\"green\", label = Indicator)\n",
" plt.plot(df_zone.index.get_level_values(\"Year\"), df_zone[\"GDP\"], color=\"red\", label = 'GDP')\n",
" plt.plot(df_zone.index.get_level_values(\"Year\"), df_zone[indicator], color=\"green\", label = indicator)\n",
" plt.legend(loc=\"lower right\")\n",
" \n",
"by_drop_plot = widgets.Dropdown(\n",
" options= ['Country', 'Region'],\n",
" description='By: ',\n",
")\n",
"\n",
"\n",
"\n",
"country_drop = widgets.Dropdown(\n",
"zone_drop_plot = widgets.Dropdown(\n",
" options= country_list,\n",
" value='Afghanistan',\n",
" description='Country: ',\n",
")\n",
"\n",
"indicator_drop = widgets.Dropdown(\n",
"indicator_drop_plot = widgets.Dropdown(\n",
" options= sorted(indicators),\n",
" value='AgriShareGDP',\n",
" description='Indicator: :',\n",
")\n",
"\n",
"intslider = widgets.IntRangeSlider(\n",
"intslider_plot = widgets.IntRangeSlider(\n",
" value=[min(set(df_gold['Year'])), max(set(df_gold['Year']))],\n",
" min= min(set(df_gold['Year'])),\n",
" max= max(set(df_gold['Year'])),\n",
Expand All @@ -520,15 +535,8 @@
"\n",
"# TODO By Region: Say Y axis is Qualitative (Not real values but Normalized to observe evolution vs GDP - Tendendency)\n",
"\n",
"widgets.interact(plotYearRange, Zone = country_drop, Indicator = indicator_drop, Years = intslider)"
"widgets.interact(plot_year_range, by = by_drop_plot, zone = zone_drop_plot, indicator = indicator_drop_plot, years = intslider_plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down