Skip to content

Commit

Permalink
style: fix code folding and headers (#2, closes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlFontal committed Dec 14, 2022
1 parent c24ce93 commit e7ba39d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,5 @@ data/doc/images/[0-9]*
data/shapefiles/administrative_boundaries/municipality_shapes*
data/shapefiles/administrative_boundaries/prefecture_shapes*

*DS_Store
*DS_Store
*_site
1 change: 1 addition & 0 deletions reports/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
4 changes: 1 addition & 3 deletions reports/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ website:
favicon: assets/favicon.ico

navbar:
logo: assets/logo.png

right:

- text: Home
Expand Down Expand Up @@ -49,7 +47,7 @@ website:

format:
html:
number-sections: true
number-sections: false
highlight-style: github
theme:
light: litera
Expand Down
85 changes: 48 additions & 37 deletions reports/prefecture_weighted_averages.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
" smooth-scroll: true \n",
" toc: true \n",
" toc-location: right\n",
" code-tools:\n",
" toggle: true\n",
"\n",
"---"
]
Expand Down Expand Up @@ -244,7 +246,7 @@
}
],
"source": [
"#| code-folding: true\n",
"#| code-fold: true\n",
"\n",
"(p9.ggplot(municipality_shapes) \n",
" + p9.geom_map(alpha=.2, size=.05, color='gray')\n",
Expand Down Expand Up @@ -747,6 +749,7 @@
"metadata": {},
"outputs": [],
"source": [
"area_dir = '../data/air_pollution/prefectures/area_weighted/'\n",
"for prefecture, df in long_pollution_df.groupby('pref'):\n",
" pref_df = (df.groupby(['date', 'variable'])\n",
" .progress_apply(lambda df: df\n",
Expand All @@ -758,7 +761,8 @@
" .reset_index()\n",
" .pivot(index='date', columns='variable', values='value')\n",
")\n",
" pref_df.round(3).to_csv(f'../data/air_pollution/prefectures/area_weighted/{prefecture}.csv')"
" pref_df.round(3).to_csv(f'{area_dir}{prefecture}.csv')\n",
" "
]
},
{
Expand Down Expand Up @@ -786,18 +790,20 @@
},
"outputs": [],
"source": [
"child_pop_dir = '../data/air_pollution/prefectures/under_5_weighted/'\n",
"for prefecture, df in long_pollution_df.groupby('pref'):\n",
" pref_df = (df.groupby(['date', 'variable'])\n",
" .progress_apply(lambda df: df\n",
" .merge(jp_pops_kids)\n",
" .eval('weight = population / population.sum()')\n",
" .eval('value = value * weight')\n",
" .value.sum())\n",
" .rename('value')\n",
" .reset_index()\n",
" .pivot(index='date', columns='variable', values='value')\n",
" .progress_apply(lambda df: df\n",
" .merge(jp_pops_kids)\n",
" .eval('weight = population / population.sum()')\n",
" .eval('value = value * weight')\n",
" .value.sum())\n",
" .rename('value')\n",
" .reset_index()\n",
" .pivot(index='date', columns='variable', values='value')\n",
")\n",
" pref_df.round(3).to_csv(f'../data/air_pollution/prefectures/under_5_weighted/{prefecture}.csv')"
" (pref_df.round(3).to_csv(f'{child_pop_dir}{prefecture}.csv')\n",
" )"
]
},
{
Expand All @@ -820,6 +826,7 @@
"metadata": {},
"outputs": [],
"source": [
"pop_dir = '../data/air_pollution/prefectures/population_weighted/'\n",
"for prefecture, df in long_pollution_df.groupby('pref'):\n",
" pref_df = (df.groupby(['date', 'variable'])\n",
" .progress_apply(lambda df: df\n",
Expand All @@ -831,7 +838,7 @@
" .reset_index()\n",
" .pivot(index='date', columns='variable', values='value')\n",
")\n",
" pref_df.round(3).to_csv(f'../data/air_pollution/prefectures/population_weighted/{prefecture}.csv')"
" pref_df.round(3).to_csv(f'{pop_dir}{prefecture}.csv')"
]
},
{
Expand Down Expand Up @@ -906,15 +913,17 @@
}
],
"source": [
"#| code-folding: true\n",
"#| code-fold: true\n",
"\n",
"(monitoring_stations\n",
" .drop(columns=['station_code', 'latitude', 'longitude', 'altitude'])\n",
" .groupby(['prefecture', 'prefecture_code'])\n",
" .sum(numeric_only=True)\n",
" .astype(int)\n",
" .sort_values('prefecture_code')\n",
" .apply(lambda x: x / monitoring_stations.groupby('prefecture_code').station_code.nunique())\n",
" .apply(lambda x: x / monitoring_stations\n",
" .groupby('prefecture_code')\n",
" .station_code.nunique())\n",
" .applymap(lambda x: round(x * 100, 2))\n",
" .reset_index()\n",
" .melt(['prefecture', 'prefecture_code'])\n",
Expand All @@ -928,24 +937,25 @@
" .reset_index())\n",
" .assign(pref_name=lambda dd: dd.prefecture.str.split('-').str[0] + \n",
" ' (' + dd.n_stations.astype(str) + ')')\n",
" .pipe(lambda dd: p9.ggplot(dd) \n",
" + p9.aes(x='reorder(variable, value, ascending=True)',\n",
" y='reorder(pref_name, value, ascending=False)',\n",
" fill='value') \n",
" + p9.geom_tile(color='black')\n",
" + p9.geom_text(p9.aes(label='value.round(0).astype(int)',\n",
" color='value > 70'), size=2.5) \n",
" + p9.theme_minimal()\n",
" + p9.scale_color_manual(values=['black', 'white'])\n",
" + p9.scale_fill_continuous('Oranges')\n",
" + p9.guides(fill=False, color=False)\n",
" + p9.labs(x='', y='', title='Percentage of stations collecting data')\n",
" + p9.coord_flip()\n",
" + p9.theme(figure_size=(4, 1.25),\n",
" title=p9.element_text(size=8),\n",
" axis_text_x=p9.element_text(angle=90, size=3.5, margin={'t': -10}),\n",
" axis_text_y=p9.element_text(size=4, ha='right', margin={'r': -10}),\n",
" panel_grid=p9.element_blank())\n",
" .pipe(lambda dd: \n",
" p9.ggplot(dd) \n",
" + p9.aes(x='reorder(variable, value, ascending=True)',\n",
" y='reorder(pref_name, value, ascending=False)',\n",
" fill='value') \n",
" + p9.geom_tile(color='black')\n",
" + p9.geom_text(p9.aes(label='value.round(0).astype(int)',\n",
" color='value > 70'), size=2.5) \n",
" + p9.theme_minimal()\n",
" + p9.scale_color_manual(values=['black', 'white'])\n",
" + p9.scale_fill_continuous('Oranges')\n",
" + p9.guides(fill=False, color=False)\n",
" + p9.labs(x='', y='', title='Percentage of stations collecting data')\n",
" + p9.coord_flip()\n",
" + p9.theme(figure_size=(4, 1.25),\n",
" title=p9.element_text(size=8),\n",
" axis_text_x=p9.element_text(angle=90, size=3.5, margin={'t': -10}),\n",
" axis_text_y=p9.element_text(size=4, ha='right', margin={'r': -10}),\n",
" panel_grid=p9.element_blank())\n",
")\n",
")"
]
Expand Down Expand Up @@ -989,7 +999,7 @@
}
],
"source": [
"#| code-folding: true\n",
"#| code-fold: true\n",
"\n",
"(monitoring_stations\n",
" .drop(columns=['station_code', 'latitude', 'longitude', 'altitude'])\n",
Expand Down Expand Up @@ -1060,7 +1070,8 @@
"pop_coverage = (jp_munis\n",
" .merge(jp_pops_full)\n",
" .groupby('PREF')\n",
" .apply(lambda dd: dd.query('has_station').population.sum() / dd.population.sum())\n",
" .apply(lambda dd: dd.query('has_station').population.sum() /\n",
" dd.population.sum())\n",
" .rename('pop_coverage')\n",
" .reset_index()\n",
" .set_index('PREF')\n",
Expand Down Expand Up @@ -1129,7 +1140,7 @@
}
],
"source": [
"#| code-folding: true\n",
"#| code-fold: true\n",
"\n",
"(p9.ggplot(jp_munis.merge(jp_pops_full))\n",
"+ p9.geom_map(p9.aes(fill='population'), size=.01) \n",
Expand Down Expand Up @@ -1178,7 +1189,7 @@
}
],
"source": [
"#| code-folding: true\n",
"#| code-fold: true\n",
"\n",
"f = (p9.ggplot(jp_munis) \n",
" + p9.geom_map(size=.01, fill='white') \n",
Expand Down Expand Up @@ -1254,7 +1265,7 @@
}
],
"source": [
"#| code-folding: true\n",
"#| code-fold: true\n",
"\n",
"for i in tqdm(stats_df.sort_values('ar').prefecture_code):\n",
" shape = jp_munis.query(f'PREF==\"{str(i).zfill(2)}\"')\n",
Expand Down

0 comments on commit e7ba39d

Please sign in to comment.