Skip to content

Commit bbdd5ac

Browse files
authored
Code snippets for Jupyter data visualization tutorial (GoogleCloudPlatform#1560)
1 parent d797d75 commit bbdd5ac

File tree

2 files changed

+169
-1
lines changed

2 files changed

+169
-1
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Copyright 2018 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import IPython
15+
from IPython.terminal import interactiveshell
16+
from IPython.testing import tools
17+
import matplotlib
18+
import pytest
19+
20+
21+
# Ignore semicolon lint warning because semicolons are used in notebooks
22+
# flake8: noqa E703
23+
24+
25+
@pytest.fixture(scope='session')
26+
def ipython():
27+
config = tools.default_config()
28+
config.TerminalInteractiveShell.simple_prompt = True
29+
shell = interactiveshell.TerminalInteractiveShell.instance(config=config)
30+
return shell
31+
32+
33+
@pytest.fixture()
34+
def ipython_interactive(request, ipython):
35+
"""Activate IPython's builtin hooks
36+
37+
for the duration of the test scope.
38+
"""
39+
with ipython.builtin_trap:
40+
yield ipython
41+
42+
43+
def _strip_region_tags(sample_text):
44+
"""Remove blank lines and region tags from sample text"""
45+
magic_lines = [line for line in sample_text.split('\n')
46+
if len(line) > 0 and '# [' not in line]
47+
return '\n'.join(magic_lines)
48+
49+
50+
def test_jupyter_tutorial(ipython):
51+
matplotlib.use('agg')
52+
ip = IPython.get_ipython()
53+
ip.extension_manager.load_extension('google.cloud.bigquery')
54+
55+
sample = """
56+
# [START bigquery_jupyter_magic_gender_by_year]
57+
%%bigquery
58+
SELECT
59+
source_year AS year,
60+
COUNT(is_male) AS birth_count
61+
FROM `bigquery-public-data.samples.natality`
62+
GROUP BY year
63+
ORDER BY year DESC
64+
LIMIT 15
65+
# [END bigquery_jupyter_magic_gender_by_year]
66+
"""
67+
result = ip.run_cell(_strip_region_tags(sample))
68+
result.raise_error() # Throws an exception if the cell failed.
69+
70+
sample = """
71+
# [START bigquery_jupyter_magic_gender_by_year_var]
72+
%%bigquery total_births
73+
SELECT
74+
source_year AS year,
75+
COUNT(is_male) AS birth_count
76+
FROM `bigquery-public-data.samples.natality`
77+
GROUP BY year
78+
ORDER BY year DESC
79+
LIMIT 15
80+
# [END bigquery_jupyter_magic_gender_by_year_var]
81+
"""
82+
result = ip.run_cell(_strip_region_tags(sample))
83+
result.raise_error() # Throws an exception if the cell failed.
84+
85+
assert 'total_births' in ip.user_ns # verify that variable exists
86+
total_births = ip.user_ns['total_births']
87+
# [START bigquery_jupyter_plot_births_by_year]
88+
total_births.plot(kind='bar', x='year', y='birth_count');
89+
# [END bigquery_jupyter_plot_births_by_year]
90+
91+
sample = """
92+
# [START bigquery_jupyter_magic_gender_by_weekday]
93+
%%bigquery births_by_weekday
94+
SELECT
95+
wday,
96+
SUM(CASE WHEN is_male THEN 1 ELSE 0 END) AS male_births,
97+
SUM(CASE WHEN is_male THEN 0 ELSE 1 END) AS female_births
98+
FROM `bigquery-public-data.samples.natality`
99+
WHERE wday IS NOT NULL
100+
GROUP BY wday
101+
ORDER BY wday ASC
102+
# [END bigquery_jupyter_magic_gender_by_weekday]
103+
"""
104+
result = ip.run_cell(_strip_region_tags(sample))
105+
result.raise_error() # Throws an exception if the cell failed.
106+
107+
assert 'births_by_weekday' in ip.user_ns # verify that variable exists
108+
births_by_weekday = ip.user_ns['births_by_weekday']
109+
# [START bigquery_jupyter_plot_births_by_weekday]
110+
births_by_weekday.plot(x='wday');
111+
# [END bigquery_jupyter_plot_births_by_weekday]
112+
113+
# [START bigquery_jupyter_import_and_client]
114+
from google.cloud import bigquery
115+
client = bigquery.Client()
116+
# [END bigquery_jupyter_import_and_client]
117+
118+
# [START bigquery_jupyter_query_plurality_by_year]
119+
sql = """
120+
SELECT
121+
plurality,
122+
COUNT(1) AS count,
123+
year
124+
FROM
125+
`bigquery-public-data.samples.natality`
126+
WHERE
127+
NOT IS_NAN(plurality) AND plurality > 1
128+
GROUP BY
129+
plurality, year
130+
ORDER BY
131+
count DESC
132+
"""
133+
df = client.query(sql).to_dataframe()
134+
df.head()
135+
# [END bigquery_jupyter_query_plurality_by_year]
136+
137+
# [START bigquery_jupyter_plot_plurality_by_year]
138+
pivot_table = df.pivot(index='year', columns='plurality', values='count')
139+
pivot_table.plot(kind='bar', stacked=True, figsize=(15, 7));
140+
# [END bigquery_jupyter_plot_plurality_by_year]
141+
142+
# [START bigquery_jupyter_query_weight_by_gestation]
143+
sql = """
144+
SELECT
145+
gestation_weeks,
146+
AVG(weight_pounds) AS ave_weight
147+
FROM
148+
`bigquery-public-data.samples.natality`
149+
WHERE
150+
NOT IS_NAN(gestation_weeks) AND gestation_weeks <> 99
151+
GROUP BY
152+
gestation_weeks
153+
ORDER BY
154+
gestation_weeks
155+
"""
156+
df = client.query(sql).to_dataframe()
157+
# [END bigquery_jupyter_query_weight_by_gestation]
158+
159+
# [START bigquery_jupyter_plot_weight_by_gestation]
160+
ax = df.plot(
161+
kind='bar', x='gestation_weeks', y='ave_weight', figsize=(15, 7))
162+
ax.set_title('Average Weight by Gestation Weeks')
163+
ax.set_xlabel('Gestation Weeks')
164+
ax.set_ylabel('Average Weight');
165+
# [END bigquery_jupyter_plot_weight_by_gestation]
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
google-cloud-bigquery==1.3.0
1+
google-cloud-bigquery[pandas]==1.3.0
22
google-auth-oauthlib==0.2.0
3+
ipython==5.5; python_version < "3"
4+
ipython; python_version > "3"
5+
matplotlib
36
pytz==2018.3

0 commit comments

Comments
 (0)