Covid Patient Health Assessing Device Using Sliding Window

This notebook mimics the one from @Manivannan, where he develops "A pocket-sized medical device based on TinyML application using Edge Impulse to predicts the Covid patient's health conditions."

Here we're using the exact same dataset, but with a very different approach: instead of using Neural Networks (a 32 + 16 + 8 + 4 fully connected network), we'll use a simple sliding window with some basic statistics (min / max / mean / std...).

This sliding window approach will come useful in many other situations where you'll be working with time-series data (IMU gesture classification, vibration pattern classification, ...).

The whole system resolves around a Python class that you fit on your data and that transpiles to plain C++, ready to be embedded inside your project with no external dependencies!.

1. Generate data

This is an exact copy of the original post: we're going to generate synthetic data that mimics the expected behavior of Covid patatients.

From the plots below you will notice that the different classes are very different from each other and well separable: using a Neural Network for this task is, in my opinion, overkill at best!

2. Apply sliding window

A sliding window (also called rolling window) is a window that selects only a chunk of data at each step. It moves forward by a given shift, selecting each time different elements.

It is often the case that these windows overlaps, thus covering a given event from different time perspectives. In Machine Learning tasks this greatly helps creating features that are (to some degree) time invariant, or better, that describe the same event with different features, making the classifier more robust.

The emebedded_window package has a single class, called Window, that implements this mechanism.

# install with
pip install embedded_window

Visit the package repo on Github to look at the code and remember to star!

3. Fit classifier

Once we have our features, we can fit any classifier on them. For this task even a Decision Tree will perform good.

4. Port to C++

Now that we know our features are good at classifying our data, it's time to export the sliding window to plain C++. Guess what? It's a one liner!.

5. Use in Arduino C++

Here is a short example on how to use the exported code in an Arduino project. The class is totally self-contained and doesn't need any further configuration.

It expones the following API:

Following a minimal sketch that shows how to actually use the class.

#include "Window.h"


Window window;
float X[30][4] = {...};


void setup() {
  Serial.begin(115200);
  delay(2000);
}

void loop() {
  for (int i = 0; i < 30; i++) {
    if (window.transform(X[i])) {
      print_array(window.features, window.features_count);
    }
  }

  delay(60000);
}

/**
 * Print array of given number of elements
 */
void print_array(float *array, int size) {
  for (int i = 0; i < size - 1; i++) {
    Serial.print(array[i]);
    Serial.print(", ");
  }

  Serial.println(array[size - 1]);
}

6. Go invent

This sliding window may be used as a foundation for many other kind of tasks. The extracted features are really simple at the moment, but will cover simple problems like the one described here (I already implemented many other features, but will integrate them later in a different class).

The hope is: before blindly using Neural Networks, think twice if there's a lighter, easier, faster way to deal with the problem at hand!