<jupyter_start><jupyter_text>This notebook reproduces a [visualization by the Wall Street Journal](http://graphics.wsj.com/infectious-diseases-and-vaccines/b02g20t20w15) about the incidence of measles over time, which the brilliant [Brian Granger](https://github.com/ellisonbg) adapted into a [computational example for the Altair library](http://nbviewer.jupyter.org/github/ellisonbg/altair/blob/master/altair/notebooks/12-Measles.ipynb).[Original source](http://holoviews.org/gallery/demos/bokeh/measles_example.html) (from [`holoviews.org`](http://holoviews.org))  1. Import required packages/librariesA few Python *packages* need to be imported to execute this notebook:+ [Numpy](https://numpy.org/) (*Numerical Python* for processing numerical arrays of data)+ [Pandas](https://pandas.pydata.org/) (for data analysis of tables of data)+ [HoloViews](http://holoviews.org/) (for creating data visualizations)<jupyter_code># Required import statements
# Print version information

# Display Holoviews & Bokeh logos below<jupyter_output><empty_output><jupyter_text>+ Using aliases (e.g., `np` for `numpy`, `pd` for `pandas`, `hv` for `holoviews` reduces typing.  2. Declaring data+ Use `read_csv` function to read data from the *CSV* (*Comma-Separated Values*) file into a *DataFrame*.+ Bind the result to the identifier `data`.<jupyter_code># Load data from CSV file
data = ...<jupyter_output><empty_output><jupyter_text>+ Create a new table `yearly_data` from the original DataFrame `data`.   + *Drop* the `WEEK` column.   + *Group* the tabular data by `YEAR` & add up the totals.   + *Reset* the index (so `YEAR` is a regular column).+ "*Method chaining*" is common in Python data analysis.<jupyter_code># Add up data by year over all states
yearly_data = ...<jupyter_output><empty_output><jupyter_text>+ *Melt* the wide `yearly_data` table to yield a tall "tidy" table.   + Use the `YEAR` column as `id_vars`.   + Use the string `STATE` as `var_name`.   + Use the `Incidence` column values as `value_name`.+ Bind the result to the identifier `measles`.<jupyter_code># Transform wide table into a tall table
measles = ...<jupyter_output><empty_output><jupyter_text>3. Constructing individual plots components+ The HoloViews library provides convenience functions to produce visualizations from data.+ The function `HeatMap` is one such function; apply it to the table `measles`.   + Use `'Measles Incidence'` as a `label` in constructing this table.+ Bind the result to the identifier `heatmap`.<jupyter_code># Construct heatmap from measles table
heatmap = ...<jupyter_output><empty_output><jupyter_text>+ The function `Dataset` from HoloViews extracts original data from HoloViews plot objects.   + Chain the `aggregate` method to the `YEAR` column with the `mean` & `std` functions from the NumPy package. + Bind the result to the identifier `aggregate`.<jupyter_code># Use heatmap data to compute annual average & standard deviation across all states
aggregated = ...<jupyter_output><empty_output><jupyter_text>+ Construct a line-plot with `hv.Curve` and the data `aggregated`.   + Bind the result to the identifier `line`.<jupyter_code># Construct a line plot 
line = ...<jupyter_output><empty_output><jupyter_text>+ Construct a plot of the error bars using `hv.ErrorBars` and the data `aggregated`.   + Bind the result to the identifier `bars`.<jupyter_code># Construct error bars
bars = ...<jupyter_output><empty_output><jupyter_text>+ Make a vertical line at the year 1963 with `hv.VLine`.   + Bind the result to the identifier `vline`.<jupyter_code># Construct vertical line annotation
vline = ...<jupyter_output><empty_output><jupyter_text>+ Make a text annotation with `hv.Text`.   + Use the text "Vaccine introduction" at coordinates `(1964,  800)`.   + Make the text align to the left using `halign='left'`.   + Bind the result to the identifier `text`.<jupyter_code># Construct text annotation
text = ...<jupyter_output><empty_output><jupyter_text>4. Putting plot pieces together+ Use the `*` and `+` operators to put all the plot elements together.  + Put `line`, `bars`, `vline`, and `text` on a common set of axes.  + Position the resulting line-plot beneath `heatmap` using the `cols` method.+ Bind the result to the identifier `overlay`.<jupyter_code># Construct an overlay showing all plot elements together
overlay = ...<jupyter_output><empty_output><jupyter_text>5. Cleaning up the final plot+ Finally, the object `overlay` just created can be modified using the `opts` method.   + The `hv.opts` module has numerous relevant ways of fine-tuning plot results.   + Use `hv.opts.VLine`, `hv.opts.Overlay`, & `hv.opts.HeatMap` with various arguments to improve the plot.<jupyter_code># Use .opts method & hv.opts to tune plot elements
overlay.opts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11185169756300771, 'fraction_numerical': 0.0054155384294938555, 'mean_word_length': 5.318421052631579, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '237541', 'license': 'MIT', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1483, 'n_words': 536, 'path': 'WORKSHEET_Measles_Example.ipynb', 'repo_name': 'dhavide/test_binder', 'size': '4801'}