1. Filter data with a lambda function

Imagine that you are a doctor who is receiving all kinds of data from your patient, but you’re only interested in her heart rate. In this section, you will filter out the unwanted data.

Develop your application for this section in the code cell under the Lab 2 – Handle a variety of patient data heading.

  1. Invoke the filter operation on the patientData stream and assign the name heartRate to the filtered stream. heartRate = patientData.filter()
  2. As input to the filter operation, create a lambda function that takes a patient data tuple and returns True if its code value is equal to 8867-4. For your reference, the sample patient data is shown below:
    {"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.health/1.0"}}}heartRate = patientData.filter(lambda tuple: (tuple[‘reading’][‘readingType’][‘code’]==‘8867-4’))
  3. Sink the filtered stream to the console.Whenever you’re adding in new stream operations, make sure that the downstream code matches correctly. In this case, change your existing sink invocation to reflect the change that you made in declaring a new heartrate stream:heartRate.sink(print)
  4. Submit your application to the Bluemix Streaming Analytics service. This step is the same as in Lab 1.
  5. Run your application and view the output on the Streams Console.Your output will look exactly the same as in the first exercise. Why? Look closely at your sample patient data: its readingType code is 8867-4.Each of the tuples that run through your application are clones of this patient data. They are all heart rate readings! If you were to manually change this type code to something else and then rerun the program, you’d notice that none of the tuples make it through to output.
  6. Cancel all jobs running on the Streams Console.