23 | IBM Streams Tutorials
24 | 25 |Under constructions, check back soon!
26 | 27 | 32 | 33 |├── .gitattributes
├── .gitignore
├── GRADUATION_STATUS.md
├── LICENSE.md
├── OnlineCourse_IntroToPython
├── IntroToPython_exercise.ipynb
├── IntroToPython_solution.ipynb
├── README.md
├── credentials.PNG
└── service name.PNG
├── OnlineCourse_IntroToStreams
└── labfiles.zip
├── README.md
├── _config.yml
├── index.html
├── introlab
├── IntroLab_4.0.1.zip
├── IntroLab_4.1.1.zip
├── IntroLab_4.1.1b.zip
├── IntroLab_4.1.zip
├── README.md
├── Streams 4 Lab Guide 1.1a qse.pdf
├── Streams 4.1 Lab Guide 1.0a qse.pdf
├── Streams 4.1 Lab Guide 1.0c qse.pdf
├── Streams 4.1 Lab Guide 1.1a qse.docx
└── Streams 4.1 Lab Guide 1.1a qse.pdf
├── params.json
├── spsslab
├── README.md
├── SPSS_SPLProjects.zip
└── SPSSmodels.zip
└── stylesheets
├── github-light.css
├── normalize.css
└── stylesheet.css
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | #################
2 | ## Eclipse
3 | #################
4 |
5 | *.pydevproject
6 | .project
7 | .metadata
8 | bin/
9 | tmp/
10 | *.tmp
11 | *.bak
12 | *.swp
13 | *~.nib
14 | local.properties
15 | .classpath
16 | .settings/
17 | .loadpath
18 |
19 | # External tool builders
20 | .externalToolBuilders/
21 |
22 | # Locally stored "Eclipse launch configurations"
23 | *.launch
24 |
25 | # CDT-specific
26 | .cproject
27 |
28 | # PDT-specific
29 | .buildpath
30 |
31 |
32 | #################
33 | ## Visual Studio
34 | #################
35 |
36 | ## Ignore Visual Studio temporary files, build results, and
37 | ## files generated by popular Visual Studio add-ons.
38 |
39 | # User-specific files
40 | *.suo
41 | *.user
42 | *.sln.docstates
43 |
44 | # Build results
45 |
46 | [Dd]ebug/
47 | [Rr]elease/
48 | x64/
49 | build/
50 | [Bb]in/
51 | [Oo]bj/
52 |
53 | # MSTest test Results
54 | [Tt]est[Rr]esult*/
55 | [Bb]uild[Ll]og.*
56 |
57 | *_i.c
58 | *_p.c
59 | *.ilk
60 | *.meta
61 | *.obj
62 | *.pch
63 | *.pdb
64 | *.pgc
65 | *.pgd
66 | *.rsp
67 | *.sbr
68 | *.tlb
69 | *.tli
70 | *.tlh
71 | *.tmp
72 | *.tmp_proj
73 | *.log
74 | *.vspscc
75 | *.vssscc
76 | .builds
77 | *.pidb
78 | *.log
79 | *.scc
80 |
81 | # Visual C++ cache files
82 | ipch/
83 | *.aps
84 | *.ncb
85 | *.opensdf
86 | *.sdf
87 | *.cachefile
88 |
89 | # Visual Studio profiler
90 | *.psess
91 | *.vsp
92 | *.vspx
93 |
94 | # Guidance Automation Toolkit
95 | *.gpState
96 |
97 | # ReSharper is a .NET coding add-in
98 | _ReSharper*/
99 | *.[Rr]e[Ss]harper
100 |
101 | # TeamCity is a build add-in
102 | _TeamCity*
103 |
104 | # DotCover is a Code Coverage Tool
105 | *.dotCover
106 |
107 | # NCrunch
108 | *.ncrunch*
109 | .*crunch*.local.xml
110 |
111 | # Installshield output folder
112 | [Ee]xpress/
113 |
114 | # DocProject is a documentation generator add-in
115 | DocProject/buildhelp/
116 | DocProject/Help/*.HxT
117 | DocProject/Help/*.HxC
118 | DocProject/Help/*.hhc
119 | DocProject/Help/*.hhk
120 | DocProject/Help/*.hhp
121 | DocProject/Help/Html2
122 | DocProject/Help/html
123 |
124 | # Click-Once directory
125 | publish/
126 |
127 | # Publish Web Output
128 | *.Publish.xml
129 | *.pubxml
130 |
131 | # NuGet Packages Directory
132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
133 | #packages/
134 |
135 | # Windows Azure Build Output
136 | csx
137 | *.build.csdef
138 |
139 | # Windows Store app package directory
140 | AppPackages/
141 |
142 | # Others
143 | sql/
144 | *.Cache
145 | ClientBin/
146 | [Ss]tyle[Cc]op.*
147 | ~$*
148 | *~
149 | *.dbmdl
150 | *.[Pp]ublish.xml
151 | *.pfx
152 | *.publishsettings
153 |
154 | # RIA/Silverlight projects
155 | Generated_Code/
156 |
157 | # Backup & report files from converting an old project file to a newer
158 | # Visual Studio version. Backup files are not needed, because we have git ;-)
159 | _UpgradeReport_Files/
160 | Backup*/
161 | UpgradeLog*.XML
162 | UpgradeLog*.htm
163 |
164 | # SQL Server files
165 | App_Data/*.mdf
166 | App_Data/*.ldf
167 |
168 | #############
169 | ## Windows detritus
170 | #############
171 |
172 | # Windows image file caches
173 | Thumbs.db
174 | ehthumbs.db
175 |
176 | # Folder config file
177 | Desktop.ini
178 |
179 | # Recycle Bin used on file shares
180 | $RECYCLE.BIN/
181 |
182 | # Mac crap
183 | .DS_Store
184 |
185 |
186 | #############
187 | ## Python
188 | #############
189 |
190 | *.py[co]
191 |
192 | # Packages
193 | *.egg
194 | *.egg-info
195 | dist/
196 | build/
197 | eggs/
198 | parts/
199 | var/
200 | sdist/
201 | develop-eggs/
202 | .installed.cfg
203 |
204 | # Installer logs
205 | pip-log.txt
206 |
207 | # Unit test / coverage reports
208 | .coverage
209 | .tox
210 |
211 | #Translations
212 | *.mo
213 |
214 | #Mr Developer
215 | .mr.developer.cfg
216 |
--------------------------------------------------------------------------------
/GRADUATION_STATUS.md:
--------------------------------------------------------------------------------
1 | # tutorials Graduation Status
2 |
3 |
4 | ## Overview
5 | Incubation for projects is covered here: https://github.com/IBMStreams/administration/wiki/Project-Incubation-Process
6 |
7 | ## Status
8 |
9 | - [ ] :red_circle: Stable release(s)
10 | * Status: **TBD**
11 | - [ ] :red_circle: Project is properly documented
12 | * Status: **TBD**
13 | - [ ] :red_circle: Project has samples and instructions to help users get started easily
14 | * Status: **TBD**
15 | - [ ] :red_circle: Sufficient testing
16 | * Status: **TBD**
17 | - [ ] :red_circle: Instructions on how to get started with development and contributing to project
18 | * Status: **TBD**
19 | - [ ] :red_circle: Website that contains information and documentation of the project
20 | * Status: **TBD**
21 | - [ ] :red_circle: Clear release plan
22 | * Status: **TBD**
23 | - [ ] :red_circle: At least one active committer
24 | * Status: **TBD**
25 |
26 | ### Key
27 | * :white_check_mark: - Complete
28 | * :large_orange_diamond: - Partially complete
29 | * :red_circle: - Not complete/started or unknown
30 |
31 | ## Instructions
32 | A project should have this file copied to `GRADUATION_STATUS.md` at the top-level.
33 |
34 | Projects self-report their status though IBMStreams admins may override any status.
35 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------
/OnlineCourse_IntroToPython/IntroToPython_exercise.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Prerequisites\n",
8 | "\n",
9 | "Ensure you are using a Python 3.5 kernel to run this notebook.\n",
10 | "\n",
11 | "## Setting up the streamsx.health module\n",
12 | "\n",
13 | "For this course, you'll need the streamsx.health module. Run the cell below to install it:"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": null,
19 | "metadata": {
20 | "collapsed": true
21 | },
22 | "outputs": [],
23 | "source": [
24 | "!pip install --upgrade \"git+https://github.com/IBMStreams/streamsx.health#egg=streamsx_health.ingest&subdirectory=ingest/common/python/package\""
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {},
30 | "source": [
31 | "## Setting up Bluemix\n",
32 | "\n",
33 | "Open the Bluemix web portal and log in or sign up for a free Bluemix account.\n",
34 | "\n",
35 | "Ensure that your Streaming Analytics service is running in Bluemix.\n",
36 | "\n",
37 | "If you don’t have a service, you can create one as follows: \n",
38 | "1. Click **Catalog** or **Create Service**, browse for `Streaming Analytics` and then click on it.
\n",
39 | "1. Follow the instructions on the Streaming Analytics catalog page, type the *Service name* to set up your service.
\n",
40 | "
\n",
41 | "1. Click **Create** to open the Streaming Analytics service dashboard. Your service will start automatically.\n",
42 | "\n",
43 | "\n",
44 | "## Set up access to the service\n",
45 | "\n",
46 | "You must provide the information that your streaming app needs to access the service.\n",
47 | "Run the cells below to provide your service name and credentials. If you are not prompted to enter the service credentials, click **Kernel -> Restart** on the menu bar and rerun the cells.\n"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {
54 | "collapsed": true
55 | },
56 | "outputs": [],
57 | "source": [
58 | "service_name = input(\"Streaming Analytics service name:\")"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {
65 | "collapsed": true
66 | },
67 | "outputs": [],
68 | "source": [
69 | "import getpass\n",
70 | "credentials=getpass.getpass('Streaming Analytics credentials:')"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "**Tip:** \n",
78 | "To copy your service credentials, open the Streaming Analytics service dashboard click **Service Credentials**, then **View Credentials**, and finally click the Copy icon and paste your service credentials when prompted.
\n",
79 | "\n",
80 | "
\n"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {
86 | "nbpresent": {
87 | "id": "908f9bdf-26e3-4fee-8480-c0c75d4d0042"
88 | }
89 | },
90 | "source": [
91 | "# Lab 1 - Create a simple Python application\n",
92 | "\n",
93 | "## Step 1 - Develop a basic Python application\n",
94 | "\n",
95 | "In this exercise, you will develop a simple streaming application to process sample data.\n",
96 | "\n",
97 | "### Instructions\n",
98 | "\n",
99 | "1. Import the necessary modules from the streamsx package. You will need:\n",
100 | " - the `Topology` import, from `streamsx.topology`\n",
101 | " - everything in the `streamsx.topology.context` module
\n",
102 | "2. Create a function, called 'Observations'\n",
103 | " - Create a string `jsonStr` and assign it the sample patient data shown in the section below\n",
104 | " - Import the `json` module, and load the JSON data into a dictionary called `dictObj`\n",
105 | " - Import the `time` module and slow your source by one second\n",
106 | " - Yield `dictObj` with each iteration through the above code\n",
107 | " - Finally, to simulate a continuous source, generate the data infinitely.
\n",
108 | "3. Create a new topology. Then, create a new stream `patientData` from the data source you defined above.\n",
109 | "4. Print the data stream in the console.\n",
110 | "5. Submit the application over Bluemix.\n",
111 | "\n",
112 | "### Sample Data\n",
113 | "\n",
114 | "`{\"patientId\":\"patient-1\", \"device\":{\"id\":\"VitalsGenerator\", \"locationId\":\"bed1\"}, \"readingSource\":{\"id\":123, \"deviceId\":\"VitalsGenerator\", \"sourceType\":\"generated\"}, \"reading\": {\"ts\": 605, \"uom\":\"bpm\", \"value\":82.56785326532197, \"readingType\": {\"code\":\"8867-4\", \"system\":\"streamsx.heath/1.0\"}}}`"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": null,
120 | "metadata": {
121 | "collapsed": true,
122 | "nbpresent": {
123 | "id": "315dedf8-4caa-49bf-9c7a-4a0a86cb0b29"
124 | },
125 | "scrolled": true
126 | },
127 | "outputs": [],
128 | "source": [
129 | "#Imports\n",
130 | "\n",
131 | "#Set up access to Streaming Analytics service\n",
132 | "vs={'streaming-analytics': [{'name': service_name, 'credentials': json.loads (credentials)}]}\n",
133 | "cfg = {}\n",
134 | "cfg[ConfigParams.VCAP_SERVICES] = vs\n",
135 | "cfg[ConfigParams.SERVICE_NAME] = service_name\n",
136 | "\n",
137 | "# Define data source\n",
138 | " \n",
139 | "# Create Topology and read from data source \n",
140 | "\n",
141 | "# Print\n",
142 | "\n",
143 | "# Submit on Bluemix"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {
149 | "collapsed": true,
150 | "nbpresent": {
151 | "id": "4eea0842-9947-46d3-b671-9ae238a11786"
152 | }
153 | },
154 | "source": [
155 | "# Lab 2 - Handle a diversity of patient data\n",
156 | "\n",
157 | "## Step 1 - Filter data using lambda function\n",
158 | "\n",
159 | "In this exercise, you will modify the application to only handle heart rate from the patient readings.\n",
160 | "\n",
161 | "1. Filter out all the readings whose `code` value is not '8867-4'. Assign the name `heartRate` to the filtered stream. \n",
162 | "2. Fix the `sink()` function to sink the `heartRate` stream instead.\n",
163 | "3. Submit the application over Bluemix and view the output on your Streams Console.\n",
164 | "\n",
165 | "## Step 2 - Submit the patient simulator job\n",
166 | "\n",
167 | "1. On the Streams Console, click the 'Submit Job' button\n",
168 | "2. Leave the instance field as its default value\n",
169 | "3. Select 'Specify the URL of the application bundle' and enter the following URL:\n",
170 | " https://github.com/IBMStreams/streamsx.health/releases/download/v0.1/com.ibm.streamsx.health.simulate.beacon.services.HealthDataBeaconService.sab\n",
171 | "4. Click 'Submit.'
\n",
172 | " \n",
173 | "## Step 3 - Subscribe to the simulator\n",
174 | "1. Import the `schema` module from `streamsx.topology`.\n",
175 | "2. Replace your `Observations` source with a Subscribe call to the patient simulator. The topic to subscribe to is `'ingest-beacon'`.\n",
176 | "3. Make sure the simulator is running before you submit any subsequent applications.\n",
177 | "4. Submit your application over Bluemix and view the output."
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": null,
183 | "metadata": {
184 | "collapsed": true,
185 | "nbpresent": {
186 | "id": "cf4e0dd9-3f44-41e6-a3ea-93edb515fc3c"
187 | }
188 | },
189 | "outputs": [],
190 | "source": []
191 | },
192 | {
193 | "cell_type": "markdown",
194 | "metadata": {},
195 | "source": [
196 | "# Lab 3 - Anonymize and average data \n",
197 | "\n",
198 | "## Step 1 - Anonymize Patient Data\n",
199 | "\n",
200 | "In this exercise, you are going to 'anonymize' patient data by hashing identifying information.\n",
201 | "\n",
202 | "1. Import the `hashlib` module\n",
203 | "2. Make a function that...\n",
204 | " * Has a single parameter\n",
205 | " * Hashes `patientId` and `locationId` using the sha256() algorithm
\n",
206 | " You can use: `ITEM_TO_ENCODE = hashlib.sha256(ITEM_TO_ENCODE.encode('utf-8')).digest()`
\n",
207 | " * Returns the modified tuple \n",
208 | "3. Modify the content of each tuple on the `patientData` stream by calling the anonymize method. Name the new stream `patientX.` This step should precede the filter.\n",
209 | "4. Submit over Bluemix and view the output.\n",
210 | "\n",
211 | "## Step 2 - Keeping States\n",
212 | "\n",
213 | "In this exercise, you will keep state of the last 10 tuples from the patient data stream. For each new tuple that comes in, you will calculate the moving average from the last 10 tuples.\n",
214 | "\n",
215 | "1. Import the `getReadingValue` function from the `streamsx_health.ingest.Observation` module.\n",
216 | "2. Create a new callable class. The class should have a field called `last_n`, which is a list that keeps track of the last n tuples.\n",
217 | " * The \\__call\\__ method of the class should take a single parameter, `n`, which is the number of tuples over which to calculate the average\n",
218 | " * When the \\__call\\__ method is called, append the tuple's heart rate reading to the list. If the length of the list > `n`, pop the oldest tuple.\n",
219 | " * Return the average of all values from the list
\n",
220 | "2. Calculate the moving average of heart rate, over the last 10 tuples, calling the new stream `avgHr`\n",
221 | "3. Submit the application over Bluemix, and view the results - a simple list of average heart rate readings.\n"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": null,
227 | "metadata": {
228 | "collapsed": true
229 | },
230 | "outputs": [],
231 | "source": []
232 | },
233 | {
234 | "cell_type": "markdown",
235 | "metadata": {},
236 | "source": [
237 | "# Lab 4 - Visualize data in Python notebook\n",
238 | "\n",
239 | "## Step 1 - Viewing Data\n",
240 | "\n",
241 | "1. Before the print statement, create a view from the Average Heart Rate stream.\n",
242 | "2. Launch the application and proceed to the cells below.\n",
243 | " "
244 | ]
245 | },
246 | {
247 | "cell_type": "code",
248 | "execution_count": null,
249 | "metadata": {
250 | "collapsed": true
251 | },
252 | "outputs": [],
253 | "source": []
254 | },
255 | {
256 | "cell_type": "markdown",
257 | "metadata": {},
258 | "source": [
259 | "## Step 2a - Fetch View Data\n",
260 | "\n",
261 | "The following code shows how to fetch view data from a view that we have set up in a Streams application.\n",
262 | "\n",
263 | "1. Import `deque` from the collections module. \n",
264 | "2. Create a deque called `plotQueue` that holds up to 2000 tuples.\n",
265 | "3. Start fetching data for your view.\n",
266 | "4. Create a `data_collector` function that iterates through your view data and appends each value to `plotQueue`.\n",
267 | "5. Run `data_collector` in a background thread and save data in `plotQueue` - `plotQueue` will be used to visualize data in the next cell."
268 | ]
269 | },
270 | {
271 | "cell_type": "code",
272 | "execution_count": null,
273 | "metadata": {
274 | "collapsed": true
275 | },
276 | "outputs": [],
277 | "source": [
278 | "#Imports\n",
279 | "\n",
280 | "# Create a buffer of 2000 tuples for plotting\n",
281 | "\n",
282 | "#Fetch view data\n",
283 | "\n",
284 | "#Define data collector function\n",
285 | "\n",
286 | "#Start a background thread and save view data in deque"
287 | ]
288 | },
289 | {
290 | "cell_type": "markdown",
291 | "metadata": {
292 | "collapsed": true
293 | },
294 | "source": [
295 | "## Step 2b - Visualize View Data using Matplotlib\n",
296 | "\n",
297 | "The following cell shows how to view realtime data in Jupyter Notebook.\n",
298 | "The code updates the view once every second. \n",
299 | "Data is plotted from the `plotQueue` variable.\n",
300 | "\n",
301 | "As long as the data view and the view is running, you can start and stop the following cell when you work on your analytics and visualizations."
302 | ]
303 | },
304 | {
305 | "cell_type": "code",
306 | "execution_count": null,
307 | "metadata": {
308 | "collapsed": true
309 | },
310 | "outputs": [],
311 | "source": [
312 | "# Visualize view data in a line graph\n",
313 | "%matplotlib inline\n",
314 | "import time\n",
315 | "from IPython import display\n",
316 | "import pylab as pl\n",
317 | "\n",
318 | "pl.rcParams['figure.figsize'] = (14.0, 8.0)\n",
319 | "\n",
320 | "while (True):\n",
321 | " pl.clf()\n",
322 | " ax = pl.gca()\n",
323 | " ax.set_autoscale_on(False)\n",
324 | " ax.plot(plotQueue)\n",
325 | " ax.axis([0, 2000, 50, 120])\n",
326 | " display.display(pl.gcf())\n",
327 | " print(len(plotQueue))\n",
328 | " display.clear_output(wait=True)\n",
329 | " time.sleep(1.0)\n"
330 | ]
331 | },
332 | {
333 | "cell_type": "code",
334 | "execution_count": null,
335 | "metadata": {
336 | "collapsed": true
337 | },
338 | "outputs": [],
339 | "source": []
340 | }
341 | ],
342 | "metadata": {
343 | "anaconda-cloud": {},
344 | "kernelspec": {
345 | "display_name": "Python 3",
346 | "language": "python",
347 | "name": "python3"
348 | },
349 | "language_info": {
350 | "codemirror_mode": {
351 | "name": "ipython",
352 | "version": 3
353 | },
354 | "file_extension": ".py",
355 | "mimetype": "text/x-python",
356 | "name": "python",
357 | "nbconvert_exporter": "python",
358 | "pygments_lexer": "ipython3",
359 | "version": "3.6.0"
360 | },
361 | "nbpresent": {
362 | "slides": {
363 | "429e034a-fc51-4734-8597-8233f00b1ca0": {
364 | "id": "429e034a-fc51-4734-8597-8233f00b1ca0",
365 | "prev": "4576f8c7-5923-40b5-9428-f93816cdb3c3",
366 | "regions": {
367 | "3e564196-15de-4439-b62e-c69a256999c4": {
368 | "attrs": {
369 | "height": 0.8,
370 | "width": 0.8,
371 | "x": 0.1,
372 | "y": 0.1
373 | },
374 | "content": {
375 | "cell": "d14afdc1-1169-42f0-8888-f2a817d13996",
376 | "part": "whole"
377 | },
378 | "id": "3e564196-15de-4439-b62e-c69a256999c4"
379 | }
380 | }
381 | },
382 | "4576f8c7-5923-40b5-9428-f93816cdb3c3": {
383 | "id": "4576f8c7-5923-40b5-9428-f93816cdb3c3",
384 | "prev": "64cf6d43-0bcd-4852-9edf-f34ed27c0268",
385 | "regions": {
386 | "541f47da-9924-42fb-92e4-604912bd8650": {
387 | "attrs": {
388 | "height": 0.8,
389 | "width": 0.8,
390 | "x": 0.1,
391 | "y": 0.1
392 | },
393 | "content": {
394 | "cell": "cf4e0dd9-3f44-41e6-a3ea-93edb515fc3c",
395 | "part": "whole"
396 | },
397 | "id": "541f47da-9924-42fb-92e4-604912bd8650"
398 | }
399 | }
400 | },
401 | "64cf6d43-0bcd-4852-9edf-f34ed27c0268": {
402 | "id": "64cf6d43-0bcd-4852-9edf-f34ed27c0268",
403 | "prev": "ec6e26ad-1a15-477c-9f16-095cccf3b622",
404 | "regions": {
405 | "b4495fcd-3e14-4aa2-a668-211ff89a5d55": {
406 | "attrs": {
407 | "height": 0.8,
408 | "width": 0.8,
409 | "x": 0.1,
410 | "y": 0.1
411 | },
412 | "content": {
413 | "cell": "4eea0842-9947-46d3-b671-9ae238a11786",
414 | "part": "whole"
415 | },
416 | "id": "b4495fcd-3e14-4aa2-a668-211ff89a5d55"
417 | }
418 | }
419 | },
420 | "69f0a312-ecfc-4508-bc4b-0658265c9545": {
421 | "id": "69f0a312-ecfc-4508-bc4b-0658265c9545",
422 | "prev": "429e034a-fc51-4734-8597-8233f00b1ca0",
423 | "regions": {
424 | "c79a8c13-c882-4633-b33a-709a715ac359": {
425 | "attrs": {
426 | "height": 0.8,
427 | "width": 0.8,
428 | "x": 0.1,
429 | "y": 0.1
430 | },
431 | "content": {
432 | "cell": "684e54af-8510-4236-8d44-e385759f11f2",
433 | "part": "whole"
434 | },
435 | "id": "c79a8c13-c882-4633-b33a-709a715ac359"
436 | }
437 | }
438 | },
439 | "9a4a3d93-b83d-4a71-b255-201e7b10570f": {
440 | "id": "9a4a3d93-b83d-4a71-b255-201e7b10570f",
441 | "prev": "69f0a312-ecfc-4508-bc4b-0658265c9545",
442 | "regions": {
443 | "1575c709-30c1-46af-ba43-56d7ca938a8b": {
444 | "attrs": {
445 | "height": 0.8,
446 | "width": 0.8,
447 | "x": 0.1,
448 | "y": 0.1
449 | },
450 | "content": {
451 | "cell": "f104cf13-b382-4f4b-974e-465e0853713c",
452 | "part": "whole"
453 | },
454 | "id": "1575c709-30c1-46af-ba43-56d7ca938a8b"
455 | }
456 | }
457 | },
458 | "a6d39083-6cbe-43a1-b708-37f62644a0bd": {
459 | "id": "a6d39083-6cbe-43a1-b708-37f62644a0bd",
460 | "prev": null,
461 | "regions": {
462 | "d77984e4-6f46-4a7a-b569-534f721164ab": {
463 | "attrs": {
464 | "height": 0.8,
465 | "width": 0.8,
466 | "x": 0.1,
467 | "y": 0.1
468 | },
469 | "content": {
470 | "cell": "908f9bdf-26e3-4fee-8480-c0c75d4d0042",
471 | "part": "whole"
472 | },
473 | "id": "d77984e4-6f46-4a7a-b569-534f721164ab"
474 | }
475 | }
476 | },
477 | "b378bfd6-575a-4aa7-b276-f706500a4ef6": {
478 | "id": "b378bfd6-575a-4aa7-b276-f706500a4ef6",
479 | "prev": "a6d39083-6cbe-43a1-b708-37f62644a0bd",
480 | "regions": {
481 | "c28e1060-3b74-4c75-bac0-8587f87d66f0": {
482 | "attrs": {
483 | "height": 0.8,
484 | "width": 0.8,
485 | "x": 0.1,
486 | "y": 0.1
487 | },
488 | "content": {
489 | "cell": "979be16b-e571-4324-856a-1c7124de7b18",
490 | "part": "whole"
491 | },
492 | "id": "c28e1060-3b74-4c75-bac0-8587f87d66f0"
493 | }
494 | }
495 | },
496 | "ec6e26ad-1a15-477c-9f16-095cccf3b622": {
497 | "id": "ec6e26ad-1a15-477c-9f16-095cccf3b622",
498 | "prev": "b378bfd6-575a-4aa7-b276-f706500a4ef6",
499 | "regions": {
500 | "b441a92e-7724-4866-907c-6fd9a98c1266": {
501 | "attrs": {
502 | "height": 0.8,
503 | "width": 0.8,
504 | "x": 0.1,
505 | "y": 0.1
506 | },
507 | "content": {
508 | "cell": "315dedf8-4caa-49bf-9c7a-4a0a86cb0b29",
509 | "part": "whole"
510 | },
511 | "id": "b441a92e-7724-4866-907c-6fd9a98c1266"
512 | }
513 | }
514 | }
515 | },
516 | "themes": {}
517 | }
518 | },
519 | "nbformat": 4,
520 | "nbformat_minor": 1
521 | }
522 |
--------------------------------------------------------------------------------
/OnlineCourse_IntroToPython/IntroToPython_solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Prerequisites\n",
8 | "\n",
9 | "Ensure you are using a Python 3.5 kernel to run this notebook.\n",
10 | "\n",
11 | "## Setting up the streamsx.health module\n",
12 | "\n",
13 | "For this course, you'll need the streamsx.health module. Run the cell below to install it:"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": null,
19 | "metadata": {
20 | "collapsed": true
21 | },
22 | "outputs": [],
23 | "source": [
24 | "!pip install --upgrade \"git+https://github.com/IBMStreams/streamsx.health#egg=streamsx_health.ingest&subdirectory=ingest/common/python/package\""
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {},
30 | "source": [
31 | "## Setting up Bluemix\n",
32 | "\n",
33 | "Open the Bluemix web portal and log in or sign up for a free Bluemix account.\n",
34 | "\n",
35 | "Ensure that your Streaming Analytics service is running in Bluemix.\n",
36 | "\n",
37 | "If you don’t have a service, you can create one as follows: \n",
38 | "1. Click **Catalog** or **Create Service**, browse for `Streaming Analytics` and then click on it.
\n",
39 | "1. Follow the instructions on the Streaming Analytics catalog page, type the *Service name* to set up your service.
\n",
40 | "
\n",
41 | "1. Click **Create** to open the Streaming Analytics service dashboard. Your service will start automatically.\n",
42 | "\n",
43 | "\n",
44 | "## Set up access to the service\n",
45 | "\n",
46 | "You must provide the information that your streaming app needs to access the service.\n",
47 | "Run the cells below to provide your service name and credentials. If you are not prompted to enter the service credentials, click **Kernel -> Restart** on the menu bar and rerun the cells.\n"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {
54 | "collapsed": true
55 | },
56 | "outputs": [],
57 | "source": [
58 | "service_name = input(\"Streaming Analytics service name:\")"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": null,
64 | "metadata": {
65 | "collapsed": true
66 | },
67 | "outputs": [],
68 | "source": [
69 | "import getpass\n",
70 | "credentials=getpass.getpass('Streaming Analytics credentials:')"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "**Tip:** \n",
78 | "To copy your service credentials, open the Streaming Analytics service dashboard click **Service Credentials**, then **View Credentials**, and finally click the Copy icon and paste your service credentials when prompted.
\n",
79 | "\n",
80 | "
\n"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {
86 | "nbpresent": {
87 | "id": "908f9bdf-26e3-4fee-8480-c0c75d4d0042"
88 | }
89 | },
90 | "source": [
91 | "# Lab 1 - Create a simple Python application\n",
92 | "\n",
93 | "## Step 1 - Develop a basic Python application\n",
94 | "\n",
95 | "In this exercise, you will develop a simple streaming application to process sample data.\n",
96 | "\n",
97 | "### Instructions\n",
98 | "\n",
99 | "1. Import the necessary modules from the streamsx package. You will need:\n",
100 | " - the `Topology` import, from `streamsx.topology`\n",
101 | " - everything in the `streamsx.topology.context` module
\n",
102 | "2. Create a function, called 'Observations'\n",
103 | " - Create a string `jsonStr` and assign it the sample patient data shown in the section below\n",
104 | " - Import the `json` module, and load the JSON data into a dictionary called `dictObj`\n",
105 | " - Import the `time` module and slow your source by one second\n",
106 | " - Yield `dictObj` with each iteration through the above code\n",
107 | " - Finally, to simulate a continuous source, generate the data infinitely.
\n",
108 | "3. Create a new topology. Then, create a new stream `patientData` from the data source you defined above.\n",
109 | "4. Print the data stream in the console.\n",
110 | "5. Submit the application over Bluemix.\n",
111 | "\n",
112 | "### Sample Data\n",
113 | "\n",
114 | "`{\"patientId\":\"patient-1\", \"device\":{\"id\":\"VitalsGenerator\", \"locationId\":\"bed1\"}, \"readingSource\":{\"id\":123, \"deviceId\":\"VitalsGenerator\", \"sourceType\":\"generated\"}, \"reading\": {\"ts\": 605, \"uom\":\"bpm\", \"value\":82.56785326532197, \"readingType\": {\"code\":\"8867-4\", \"system\":\"streamsx.heath/1.0\"}}}`"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": null,
120 | "metadata": {
121 | "collapsed": true,
122 | "nbpresent": {
123 | "id": "315dedf8-4caa-49bf-9c7a-4a0a86cb0b29"
124 | },
125 | "scrolled": true
126 | },
127 | "outputs": [],
128 | "source": [
129 | "#Imports\n",
130 | "from streamsx.topology.topology import Topology\n",
131 | "from streamsx.topology.context import *\n",
132 | "import json\n",
133 | "import time\n",
134 | "\n",
135 | "#Set up access to Streaming Analytics service\n",
136 | "vs={'streaming-analytics': [{'name': service_name, 'credentials': json.loads (credentials)}]}\n",
137 | "cfg = {}\n",
138 | "cfg[ConfigParams.VCAP_SERVICES] = vs\n",
139 | "cfg[ConfigParams.SERVICE_NAME] = service_name\n",
140 | "\n",
141 | "# Define data source\n",
142 | "def Observations():\n",
143 | " while True: \n",
144 | " jsonStr = '''{\"patientId\":\"patient-1\", \"device\":\n",
145 | " {\"id\":\"VitalsGenerator\", \"locationId\":\"bed1\"},\n",
146 | " \"readingSource\":\n",
147 | " {\"id\":123, \"deviceId\":\"VitalsGenerator\", \"sourceType\":\"generated\"},\n",
148 | " \"reading\":\n",
149 | " {\"ts\": 605, \"uom\":\"bpm\", \"value\":82.56785326532197,\n",
150 | " \"readingType\": \n",
151 | " {\"code\":\"8867-4\", \"system\":\"streamsx.heath/1.0\"}}}'''\n",
152 | " \n",
153 | " dictObj = json.loads(jsonStr)\n",
154 | " time.sleep(1)\n",
155 | " yield dictObj\n",
156 | " \n",
157 | "# Create Topology and read from data source\n",
158 | "topo = Topology()\n",
159 | "patientData = topo.source(Observations) \n",
160 | "\n",
161 | "# Print\n",
162 | "patientData.print()\n",
163 | "\n",
164 | "# Submit on Bluemix\n",
165 | "submit('STREAMING_ANALYTICS_SERVICE', topo, cfg)"
166 | ]
167 | },
168 | {
169 | "cell_type": "markdown",
170 | "metadata": {
171 | "collapsed": true,
172 | "nbpresent": {
173 | "id": "4eea0842-9947-46d3-b671-9ae238a11786"
174 | }
175 | },
176 | "source": [
177 | "# Lab 2 - Handle a diversity of patient data\n",
178 | "\n",
179 | "## Step 1 - Filter data using lambda function\n",
180 | "\n",
181 | "In this exercise, you will modify the application to only handle heart rate from the patient readings.\n",
182 | "\n",
183 | "1. Filter out all the readings whose `code` value is not '8867-4'. Assign the name `heartRate` to the filtered stream. \n",
184 | "2. Fix the `sink()` function to sink the `heartRate` stream instead.\n",
185 | "3. Submit the application over Bluemix and view the output on your Streams Console.\n",
186 | "\n",
187 | "## Step 2 - Submit the patient simulator job\n",
188 | "\n",
189 | "1. On the Streams Console, click the 'Submit Job' button\n",
190 | "2. Leave the instance field as its default value\n",
191 | "3. Select 'Specify the URL of the application bundle' and enter the following URL:\n",
192 | " https://github.com/IBMStreams/streamsx.health/releases/download/v0.1/com.ibm.streamsx.health.simulate.beacon.services.HealthDataBeaconService.sab\n",
193 | "4. Click 'Submit.'
\n",
194 | " \n",
195 | "## Step 3 - Subscribe to the simulator\n",
196 | "1. Import the `schema` module from `streamsx.topology`.\n",
197 | "2. Replace your `Observations` source with a Subscribe call to the patient simulator. The topic to subscribe to is `'ingest-beacon'`.\n",
198 | "3. Make sure the simulator is running before you submit any subsequent applications.\n",
199 | "4. Submit your application over Bluemix and view the output."
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": null,
205 | "metadata": {
206 | "collapsed": true,
207 | "nbpresent": {
208 | "id": "cf4e0dd9-3f44-41e6-a3ea-93edb515fc3c"
209 | }
210 | },
211 | "outputs": [],
212 | "source": [
213 | "#Imports\n",
214 | "from streamsx.topology import schema\n",
215 | "from streamsx.topology.topology import Topology\n",
216 | "from streamsx.topology.context import *\n",
217 | "import json\n",
218 | "import time\n",
219 | "\n",
220 | "#Set up access to Streaming Analytics service\n",
221 | "vs={'streaming-analytics': [{'name': service_name, 'credentials': json.loads (credentials)}]}\n",
222 | "cfg = {}\n",
223 | "cfg[ConfigParams.VCAP_SERVICES] = vs\n",
224 | "cfg[ConfigParams.SERVICE_NAME] = service_name\n",
225 | "\n",
226 | "# Define data source\n",
227 | "def Observations():\n",
228 | " while True:\n",
229 | " jsonStr = '{\"patientId\":\"patient-1\", \"device\":{\"id\":\"VitalsGenerator\", \"locationId\":\"bed1\"}, \"readingSource\": {\"id\":123, \"deviceId\":\"VitalsGenerator\", \"sourceType\":\"generated\"}, \"reading\": {\"ts\": 605, \"uom\":\"bpm\", \"value\":82.56785326532197, \"readingType\": {\"code\":\"8867-4\", \"system\":\"streamsx.heath/1.0\"}}}'\n",
230 | " dictObj = json.loads(jsonStr)\n",
231 | " time.sleep(1)\n",
232 | " yield dictObj\n",
233 | " \n",
234 | "# Create Topology and read from data source\n",
235 | "topo = Topology()\n",
236 | "patientData = topo.subscribe('ingest-beacon', schema.CommonSchema.Json)\n",
237 | "\n",
238 | "# Create new data stream called heartRate\n",
239 | "heartRate = patientData.filter(lambda tuple: (tuple['reading']['readingType']['code']==\"8867-4\"))\n",
240 | "\n",
241 | "# Only print heart rate\n",
242 | "heartRate.print()\n",
243 | "\n",
244 | "# Submit on Bluemix\n",
245 | "submit('STREAMING_ANALYTICS_SERVICE', topo, cfg)"
246 | ]
247 | },
248 | {
249 | "cell_type": "markdown",
250 | "metadata": {},
251 | "source": [
252 | "# Lab 3 - Anonymize and average data \n",
253 | "\n",
254 | "## Step 1 - Anonymize Patient Data\n",
255 | "\n",
256 | "In this exercise, you are going to 'anonymize' patient data by hashing identifying information.\n",
257 | "\n",
258 | "1. Import the `hashlib` module\n",
259 | "2. Make a function that...\n",
260 | " * Has a single parameter\n",
261 | " * Hashes `patientId` and `locationId` using the sha256() algorithm
\n",
262 | " You can use: `ITEM_TO_ENCODE = hashlib.sha256(ITEM_TO_ENCODE.encode('utf-8')).digest()`
\n",
263 | " * Returns the modified tuple \n",
264 | "3. Modify the content of each tuple on the `patientData` stream by calling the anonymize method. Name the new stream `patientX.` This step should precede the filter.\n",
265 | "4. Submit over Bluemix and view the output.\n",
266 | "\n",
267 | "## Step 2 - Keeping States\n",
268 | "\n",
269 | "In this exercise, you will keep state of the last 10 tuples from the patient data stream. For each new tuple that comes in, you will calculate the moving average from the last 10 tuples.\n",
270 | "\n",
271 | "1. Import the `getReadingValue` function from the `streamsx_health.ingest.Observation` module.\n",
272 | "2. Create a new callable class. The class should have a field called `last_n`, which is a list that keeps track of the last n tuples.\n",
273 | " * The \\__call\\__ method of the class should take a single parameter, `n`, which is the number of tuples over which to calculate the average\n",
274 | " * When the \\__call\\__ method is called, append the tuple's heart rate reading to the list. If the length of the list > `n`, pop the oldest tuple.\n",
275 | " * Return the average of all values from the list
\n",
276 | "2. Calculate the moving average of heart rate, over the last 10 tuples, calling the new stream `avgHr`\n",
277 | "3. Submit the application over Bluemix, and view the results - a simple list of average heart rate readings.\n"
278 | ]
279 | },
280 | {
281 | "cell_type": "code",
282 | "execution_count": null,
283 | "metadata": {
284 | "collapsed": true
285 | },
286 | "outputs": [],
287 | "source": [
288 | "#Imports\n",
289 | "from streamsx.topology import schema\n",
290 | "from streamsx.topology.topology import Topology\n",
291 | "from streamsx.topology.context import *\n",
292 | "import json\n",
293 | "import time\n",
294 | "import hashlib\n",
295 | "\n",
296 | "# Import Observation\n",
297 | "from streamsx_health.ingest.Observation import getReadingValue\n",
298 | "\n",
299 | "# Set up access to Streaming Analytics service\n",
300 | "vs={'streaming-analytics': [{'name': service_name, 'credentials': json.loads (credentials)}]}\n",
301 | "cfg = {}\n",
302 | "cfg[ConfigParams.VCAP_SERVICES] = vs\n",
303 | "cfg[ConfigParams.SERVICE_NAME] = service_name\n",
304 | "\n",
305 | "# Anonymize patient data \n",
306 | "def anonymize(tuple):\n",
307 | " tuple['patientId'] = hashlib.sha256(tuple['patientId'].encode('utf-8')).digest()\n",
308 | " device = tuple['device']\n",
309 | " device['locationId'] = hashlib.sha256(device['locationId'].encode('utf-8')).digest()\n",
310 | " return tuple\n",
311 | "\n",
312 | "# Define Callable Class to keep state of last 10 tuples\n",
313 | "class Avg:\n",
314 | " def __init__(self, n):\n",
315 | " self.n = n\n",
316 | " self.last_n = []\n",
317 | " \n",
318 | " def __call__(self, tuple):\n",
319 | " self.last_n.append(getReadingValue(tuple))\n",
320 | " if (len(self.last_n) > self.n):\n",
321 | " self.last_n.pop(0) \n",
322 | " return sum(self.last_n)/len(self.last_n)\n",
323 | " \n",
324 | "# Create Topology and read from data source\n",
325 | "topo = Topology()\n",
326 | "patientData = topo.subscribe('ingest-beacon', schema.CommonSchema.Json)\n",
327 | "\n",
328 | "# Anonymize patient data before processing\n",
329 | "patientX = patientData.map(anonymize)\n",
330 | "\n",
331 | "# Create new data stream called heartRate\n",
332 | "heartRate = patientX.filter(lambda tuple: (tuple['reading']['readingType']['code']==\"8867-4\"))\n",
333 | "\n",
334 | "# Avg Heart Rate stream\n",
335 | "avgHr = heartRate.map(Avg(10))\n",
336 | "\n",
337 | "# Print Avg Heart Rate\n",
338 | "avgHr.print()\n",
339 | "\n",
340 | "# Submit on Bluemix\n",
341 | "submit('STREAMING_ANALYTICS_SERVICE', topo, cfg)"
342 | ]
343 | },
344 | {
345 | "cell_type": "markdown",
346 | "metadata": {},
347 | "source": [
348 | "# Lab 4 - Visualize data in Python notebook\n",
349 | "\n",
350 | "## Step 1 - Viewing Data\n",
351 | "\n",
352 | "1. Before the print statement, create a view from the Average Heart Rate stream.\n",
353 | "2. Launch the application and proceed to the cells below.\n",
354 | " "
355 | ]
356 | },
357 | {
358 | "cell_type": "code",
359 | "execution_count": null,
360 | "metadata": {
361 | "collapsed": true
362 | },
363 | "outputs": [],
364 | "source": [
365 | "#Imports\n",
366 | "from streamsx.topology import schema\n",
367 | "from streamsx.topology.topology import Topology\n",
368 | "from streamsx.topology.context import *\n",
369 | "import json\n",
370 | "import time\n",
371 | "import hashlib\n",
372 | "\n",
373 | "# Import Observation\n",
374 | "from streamsx_health.ingest.Observation import getReadingValue\n",
375 | "\n",
376 | "# Set up access to Streaming Analytics service\n",
377 | "vs={'streaming-analytics': [{'name': service_name, 'credentials': json.loads (credentials)}]}\n",
378 | "cfg = {}\n",
379 | "cfg[ConfigParams.VCAP_SERVICES] = vs\n",
380 | "cfg[ConfigParams.SERVICE_NAME] = service_name\n",
381 | "\n",
382 | "\n",
383 | "# Anonymize patient data \n",
384 | "def anonymize(tuple):\n",
385 | " tuple['patientId'] = hashlib.sha256(tuple['patientId'].encode('utf-8')).digest()\n",
386 | " device = tuple['device']\n",
387 | " device['locationId'] = hashlib.sha256(device['locationId'].encode('utf-8')).digest()\n",
388 | " return tuple\n",
389 | "\n",
390 | "# define Callable Class to keep state of last 10 tuples\n",
391 | "class Avg:\n",
392 | " def __init__(self, n):\n",
393 | " self.n = n\n",
394 | " self.last_n = []\n",
395 | " def __call__(self, tuple):\n",
396 | " self.last_n.append(getReadingValue(tuple))\n",
397 | " if (len(self.last_n) > self.n):\n",
398 | " self.last_n.pop(0)\n",
399 | " return sum(self.last_n)/len(self.last_n)\n",
400 | " \n",
401 | "# Create Topology and read from data source\n",
402 | "topo = Topology()\n",
403 | "patientData = topo.subscribe('ingest-beacon', schema.CommonSchema.Json)\n",
404 | "\n",
405 | "# Anonymize patient data before processing\n",
406 | "patientX = patientData.map(anonymize)\n",
407 | "\n",
408 | "# Create new data stream called heartRate\n",
409 | "heartRate = patientX.filter(lambda tuple: (tuple['reading']['readingType']['code']==\"8867-4\"))\n",
410 | "\n",
411 | "# Avg Heart Rate stream\n",
412 | "avgHr = heartRate.map(Avg(10))\n",
413 | "\n",
414 | "# Create a view for Average HR\n",
415 | "avgHrView = avgHr.view()\n",
416 | "\n",
417 | "# Print Avg Heart Rate\n",
418 | "avgHr.print()\n",
419 | "\n",
420 | "# Submit on Bluemix\n",
421 | "submit('STREAMING_ANALYTICS_SERVICE', topo, cfg)"
422 | ]
423 | },
424 | {
425 | "cell_type": "markdown",
426 | "metadata": {},
427 | "source": [
428 | "## Step 2a - Fetch View Data\n",
429 | "\n",
430 | "The following code shows how to fetch view data from a view that we have set up in a Streams application.\n",
431 | "\n",
432 | "1. Import `deque` from the collections module. \n",
433 | "2. Create a deque called `plotQueue` that holds up to 2000 tuples.\n",
434 | "3. Start fetching data for your view.\n",
435 | "4. Create a `data_collector` function that iterates through your view data and appends each value to `plotQueue`.\n",
436 | "5. Run `data_collector` in a background thread and save data in `plotQueue` - `plotQueue` will be used to visualize data in the next cell."
437 | ]
438 | },
439 | {
440 | "cell_type": "code",
441 | "execution_count": null,
442 | "metadata": {
443 | "collapsed": true
444 | },
445 | "outputs": [],
446 | "source": [
447 | "#Imports\n",
448 | "from collections import deque\n",
449 | "from IPython.lib import backgroundjobs as bg\n",
450 | "\n",
451 | "# Create a buffer of 2000 tuples for plotting\n",
452 | "plotQueue = deque([], 2000)\n",
453 | "\n",
454 | "#Fetch view data\n",
455 | "view = avgHrView.start_data_fetch()\n",
456 | "\n",
457 | "#Define data collector function\n",
458 | "def data_collector(view):\n",
459 | " for d in iter(view.get, None):\n",
460 | " plotQueue.append(float(d))\n",
461 | "\n",
462 | "#Start a background thread and save view data in deque\n",
463 | "jobs = bg.BackgroundJobManager()\n",
464 | "jobs.new(data_collector, view)"
465 | ]
466 | },
467 | {
468 | "cell_type": "markdown",
469 | "metadata": {
470 | "collapsed": true
471 | },
472 | "source": [
473 | "## Step 2b - Visualize View Data using Matplotlib\n",
474 | "\n",
475 | "The following cell shows how to view realtime data in Jupyter Notebook.\n",
476 | "The code updates the view once every second. \n",
477 | "Data is plotted from the `plotQueue` variable.\n",
478 | "\n",
479 | "As long as the data view and the view is running, you can start and stop the following cell when you work on your analytics and visualizations."
480 | ]
481 | },
482 | {
483 | "cell_type": "code",
484 | "execution_count": null,
485 | "metadata": {
486 | "collapsed": true
487 | },
488 | "outputs": [],
489 | "source": [
490 | "# Visualize view data in a line graph\n",
491 | "%matplotlib inline\n",
492 | "import time\n",
493 | "from IPython import display\n",
494 | "import pylab as pl\n",
495 | "\n",
496 | "pl.rcParams['figure.figsize'] = (14.0, 8.0)\n",
497 | "\n",
498 | "while (True):\n",
499 | " pl.clf()\n",
500 | " ax = pl.gca()\n",
501 | " ax.set_autoscale_on(False)\n",
502 | " ax.plot(plotQueue)\n",
503 | " ax.axis([0, 2000, 50, 120])\n",
504 | " display.display(pl.gcf())\n",
505 | " print(len(plotQueue))\n",
506 | " display.clear_output(wait=True)\n",
507 | " time.sleep(1.0)\n"
508 | ]
509 | },
510 | {
511 | "cell_type": "code",
512 | "execution_count": null,
513 | "metadata": {
514 | "collapsed": true
515 | },
516 | "outputs": [],
517 | "source": []
518 | }
519 | ],
520 | "metadata": {
521 | "anaconda-cloud": {},
522 | "kernelspec": {
523 | "display_name": "Python 3",
524 | "language": "python",
525 | "name": "python3"
526 | },
527 | "language_info": {
528 | "codemirror_mode": {
529 | "name": "ipython",
530 | "version": 3
531 | },
532 | "file_extension": ".py",
533 | "mimetype": "text/x-python",
534 | "name": "python",
535 | "nbconvert_exporter": "python",
536 | "pygments_lexer": "ipython3",
537 | "version": "3.6.0"
538 | },
539 | "nbpresent": {
540 | "slides": {
541 | "429e034a-fc51-4734-8597-8233f00b1ca0": {
542 | "id": "429e034a-fc51-4734-8597-8233f00b1ca0",
543 | "prev": "4576f8c7-5923-40b5-9428-f93816cdb3c3",
544 | "regions": {
545 | "3e564196-15de-4439-b62e-c69a256999c4": {
546 | "attrs": {
547 | "height": 0.8,
548 | "width": 0.8,
549 | "x": 0.1,
550 | "y": 0.1
551 | },
552 | "content": {
553 | "cell": "d14afdc1-1169-42f0-8888-f2a817d13996",
554 | "part": "whole"
555 | },
556 | "id": "3e564196-15de-4439-b62e-c69a256999c4"
557 | }
558 | }
559 | },
560 | "4576f8c7-5923-40b5-9428-f93816cdb3c3": {
561 | "id": "4576f8c7-5923-40b5-9428-f93816cdb3c3",
562 | "prev": "64cf6d43-0bcd-4852-9edf-f34ed27c0268",
563 | "regions": {
564 | "541f47da-9924-42fb-92e4-604912bd8650": {
565 | "attrs": {
566 | "height": 0.8,
567 | "width": 0.8,
568 | "x": 0.1,
569 | "y": 0.1
570 | },
571 | "content": {
572 | "cell": "cf4e0dd9-3f44-41e6-a3ea-93edb515fc3c",
573 | "part": "whole"
574 | },
575 | "id": "541f47da-9924-42fb-92e4-604912bd8650"
576 | }
577 | }
578 | },
579 | "64cf6d43-0bcd-4852-9edf-f34ed27c0268": {
580 | "id": "64cf6d43-0bcd-4852-9edf-f34ed27c0268",
581 | "prev": "ec6e26ad-1a15-477c-9f16-095cccf3b622",
582 | "regions": {
583 | "b4495fcd-3e14-4aa2-a668-211ff89a5d55": {
584 | "attrs": {
585 | "height": 0.8,
586 | "width": 0.8,
587 | "x": 0.1,
588 | "y": 0.1
589 | },
590 | "content": {
591 | "cell": "4eea0842-9947-46d3-b671-9ae238a11786",
592 | "part": "whole"
593 | },
594 | "id": "b4495fcd-3e14-4aa2-a668-211ff89a5d55"
595 | }
596 | }
597 | },
598 | "69f0a312-ecfc-4508-bc4b-0658265c9545": {
599 | "id": "69f0a312-ecfc-4508-bc4b-0658265c9545",
600 | "prev": "429e034a-fc51-4734-8597-8233f00b1ca0",
601 | "regions": {
602 | "c79a8c13-c882-4633-b33a-709a715ac359": {
603 | "attrs": {
604 | "height": 0.8,
605 | "width": 0.8,
606 | "x": 0.1,
607 | "y": 0.1
608 | },
609 | "content": {
610 | "cell": "684e54af-8510-4236-8d44-e385759f11f2",
611 | "part": "whole"
612 | },
613 | "id": "c79a8c13-c882-4633-b33a-709a715ac359"
614 | }
615 | }
616 | },
617 | "9a4a3d93-b83d-4a71-b255-201e7b10570f": {
618 | "id": "9a4a3d93-b83d-4a71-b255-201e7b10570f",
619 | "prev": "69f0a312-ecfc-4508-bc4b-0658265c9545",
620 | "regions": {
621 | "1575c709-30c1-46af-ba43-56d7ca938a8b": {
622 | "attrs": {
623 | "height": 0.8,
624 | "width": 0.8,
625 | "x": 0.1,
626 | "y": 0.1
627 | },
628 | "content": {
629 | "cell": "f104cf13-b382-4f4b-974e-465e0853713c",
630 | "part": "whole"
631 | },
632 | "id": "1575c709-30c1-46af-ba43-56d7ca938a8b"
633 | }
634 | }
635 | },
636 | "a6d39083-6cbe-43a1-b708-37f62644a0bd": {
637 | "id": "a6d39083-6cbe-43a1-b708-37f62644a0bd",
638 | "prev": null,
639 | "regions": {
640 | "d77984e4-6f46-4a7a-b569-534f721164ab": {
641 | "attrs": {
642 | "height": 0.8,
643 | "width": 0.8,
644 | "x": 0.1,
645 | "y": 0.1
646 | },
647 | "content": {
648 | "cell": "908f9bdf-26e3-4fee-8480-c0c75d4d0042",
649 | "part": "whole"
650 | },
651 | "id": "d77984e4-6f46-4a7a-b569-534f721164ab"
652 | }
653 | }
654 | },
655 | "b378bfd6-575a-4aa7-b276-f706500a4ef6": {
656 | "id": "b378bfd6-575a-4aa7-b276-f706500a4ef6",
657 | "prev": "a6d39083-6cbe-43a1-b708-37f62644a0bd",
658 | "regions": {
659 | "c28e1060-3b74-4c75-bac0-8587f87d66f0": {
660 | "attrs": {
661 | "height": 0.8,
662 | "width": 0.8,
663 | "x": 0.1,
664 | "y": 0.1
665 | },
666 | "content": {
667 | "cell": "979be16b-e571-4324-856a-1c7124de7b18",
668 | "part": "whole"
669 | },
670 | "id": "c28e1060-3b74-4c75-bac0-8587f87d66f0"
671 | }
672 | }
673 | },
674 | "ec6e26ad-1a15-477c-9f16-095cccf3b622": {
675 | "id": "ec6e26ad-1a15-477c-9f16-095cccf3b622",
676 | "prev": "b378bfd6-575a-4aa7-b276-f706500a4ef6",
677 | "regions": {
678 | "b441a92e-7724-4866-907c-6fd9a98c1266": {
679 | "attrs": {
680 | "height": 0.8,
681 | "width": 0.8,
682 | "x": 0.1,
683 | "y": 0.1
684 | },
685 | "content": {
686 | "cell": "315dedf8-4caa-49bf-9c7a-4a0a86cb0b29",
687 | "part": "whole"
688 | },
689 | "id": "b441a92e-7724-4866-907c-6fd9a98c1266"
690 | }
691 | }
692 | }
693 | },
694 | "themes": {}
695 | }
696 | },
697 | "nbformat": 4,
698 | "nbformat_minor": 1
699 | }
700 |
--------------------------------------------------------------------------------
/OnlineCourse_IntroToPython/README.md:
--------------------------------------------------------------------------------
1 | Notebooks for use in Python API Course
2 |
--------------------------------------------------------------------------------
/OnlineCourse_IntroToPython/credentials.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IBMStreams/tutorials/3c0e4f1b6bd3b55d9f60c701307f318647c516c4/OnlineCourse_IntroToPython/credentials.PNG
--------------------------------------------------------------------------------
/OnlineCourse_IntroToPython/service name.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IBMStreams/tutorials/3c0e4f1b6bd3b55d9f60c701307f318647c516c4/OnlineCourse_IntroToPython/service name.PNG
--------------------------------------------------------------------------------
/OnlineCourse_IntroToStreams/labfiles.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/IBMStreams/tutorials/3c0e4f1b6bd3b55d9f60c701307f318647c516c4/OnlineCourse_IntroToStreams/labfiles.zip
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # tutorials
2 | This repository contains tutorials for:
3 |
4 | * [Streams Introduction to SPL](https://github.com/IBMStreams/tutorials/tree/main/introlab)
5 | * [SPSS Tutorial](https://github.com/IBMStreams/tutorials/tree/main/spsslab)
6 |
7 | ## Tutorials catalog
8 |
9 | https://ibmstreams.github.io/tutorials/
10 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | url: http://ibmstreams.github.io/tutorials/
2 | baseurl: /tutorials
3 | github_editme_path: IBMStreams/tutorials/blob/gh-pages
4 |
5 | # Site settings
6 | title: IBM Streams Tutorials
7 | description: "IBM Streams Tutorials"
8 |
9 | # Build settings
10 | markdown: kramdown
11 | permalink: pretty
12 | detach: false
13 | port: 4000
14 | host: 127.0.0.1
15 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Under constructions, check back soon!
26 | 27 | 32 | 33 |