# SGP43 Classifier Python Script

This script allows you to convert the serial output from the Arduino into an EDF file which can be read using Sensirion's free DataViewer software.
DataViewer is included when installing [ControlCenter](https://sensirion.com/products/sensor-evaluation/control-center).

This script also allows you to apply the provided model to the live output. The provided model can detect the presence of banana or coffee beans scent.

## Get started
To get started please refer to the [README.md in the parent folder](../README.md). You will find a complete guide that takes you through the complete process from the wiring to the prediction model

## Advanced usage
### `measure`
This command can be used to log measured data to a file for later processing.


> [!IMPORTANT]
> Plug in the microcontroller before running the measurement script.
You need to provide the port where the microcontroller is attached.


Start the script with this command:

`python main.py measure --port <port>`

Arguments:

- `--port`:     Port where the microcontroller is attached. [**required**, e.g. USB1 or COM1]
- `--log-path`: Path to the folder where the output file will be generated. [**optional**, default: current folder]
- `--baud`:     VCP transmission speed (baud-rate). [**optional**, default: 115200]

A new EDF file will be created when it first detects a new loop. Each time a new loop is started, the previous loop is
written directly to the EDF file.

### `predict`

The predict command can be used to predict the measured substance based on the EDF file created by the measurement script.

The script needs a classifier model which is located in the `sgp43_classifier` folder (e.g. the example model `air_banana_coffee.clf`).

Start the script with this command:

`python main.py predict --p <path>`

Arguments:
-  `-p, --measurement-path PATH`:  Path to the measurement EDF file.  [**required**]
-  `-o, --output-path PATH`:       Path to write the predicted output EDF file. [**optional**, default: current path]
-  `-c, --classifier-path PATH`:   Path to the pickled classifier. [**optional**, default: air_banana_coffee.clf]

A new EDF file with the predicted category will be created. 


### `measure-and-predict`
The measurement and prediction command can be used to measure a substance and output the predicted category live.

Plug in the microcontroller before running the script.
You need to provide the port where the microcontroller is attached.

Start the script with this command:

`python main.py measure-and-predict --port <port>`

Arguments:

- `--port`:                 Port where the microcontroller is attached. [**required**, e.g. USB1 or COM1]
- `--log-path`:             Path to the folder where the output file will be generated. [**optional**, default: current folder]
- `--baud`:                 VCP transmission speed (baud-rate). [**optional**, default: 115200]
- `--classifier-path PATH`: Path to the pickled classifier. [**optional**, default: air_banana_coffee.clf]

A new EDF file will be created when it first detects a new loop. Each time a new loop is started, the previous loop is
added directly to the EDF file.

The predicted category is printed to the console together with the output values of all three predictors.
If the script cannot confidently determine a category, it outputs ???. This typically occurs immediately after powering
up the sensor (during the initial blackout period) or when no category can be selected with a sufficient margin.
The latter case also occurs during transitions between categories (for example, when moving the sensor from ambient air
into a jar filled with coffee beans).

A predictor value close to +1 indicates a strong match with the trained category, whereas a value close to -1 indicates
no match. Therefore, if one of the three predictors shows a clearly positive value while the other two show negative
values, it can be reliably assumed that the category associated with the positive predictor has been detected.