Skip to content

Measurement

This example example shows how to:

  1. Start a measurement
  2. Read out predictions
  3. Stop the measurement

Note

This and other examples assume all API calls are successful and do not include error handling for brevity. This will be discussed separately in the Errors subsection.

Measurement
import time

import requests

base_url = "http://10.0.0.2:5555"
name = "example-start-measurement"

# NOTE: for more information on the start parameters, check the endpoint documentation at http://10.0.0.2/docs
requests.post(
    url=f"{base_url}/api/measurement/start",
    json={
        # the filename under which the measurement data will be stored
        "name": name,
        # which component to analyze, we can choose multiple components as well
        "components": ["glucose"],
        # set resolution to 4 cm^-1
        "spectrometer_resolution": 400,
        # how long we collect data for each data point in seconds
        "spectrometer_measurement_time": 10,
        # we only use one spectrum for blanking to speed up the example, it is recommended to leave this at 'default'
        "blanking_mode": "first_spectrum",
    },
)
print("Measurement started, running for one minute...")

time_start = time.time()

while time.time() - time_start < 60:
    n_samples = requests.get(
        f"{base_url}/api/storage/measurements/{name}/params/n-samples",
    ).json()

    print(f"Number of samples collected: {n_samples}")

    if n_samples > 0:
        prediction_list = requests.get(
            f"{base_url}/api/storage/measurements/{name}/predictions/glucose",
            params={
                "index_from": n_samples - 1,
                "index_to": n_samples,
            },
        ).json()

        # NOTE: we always get a list of predictions, even when the result only contains one entry
        prediction = prediction_list[0]

        print(f"Current concentration prediction: {prediction:.3f} g/L")

    time.sleep(5)

requests.post(f"{base_url}/api/measurement/stop")
print("Measurement stopped.")