Insulin Pump Simulator (Production Version)

Continuing the theme of developing an insulin pump simulator in the format of a telegram bot, I have achieved some success. The previous version of the simulator had very limited functionality, which did not allow it to be used for teaching the principles of pump insulin therapy. What has been done in the new version:

1. All key settings (UK, FCI and BP) are presented as lists of 12 values ​​(2-hour interval), which allowed the virtual patient to introduce variability of values ​​during the day. Accordingly, the adjustment is made separately for each interval or one value for all, which is very convenient for the initial setup of the simulator.

Result of clicking on the button "UK"

Result of clicking on the “UK” button

2. The glucose value is displayed in the form of graphs constructed using the Pil library. At the same time, the display of glucose change statistics for 3 days is implemented, which allows you to identify patterns and adjust the settings. The source code for collecting glucose change statistics is presented below

  # добавление данных
        if users[call.from_user.id]['day'] % 3 == 1:
            users[call.from_user.id]['sug_1'][int(users[call.from_user.id]['chas']/2)] = users[call.from_user.id]['gluk_tek']
        elif users[call.from_user.id]['day'] % 3 == 2:
            users[call.from_user.id]['sug_2'][int(users[call.from_user.id]['chas']/2)] = users[call.from_user.id]['gluk_tek']
        elif users[call.from_user.id]['day'] % 3 == 0:
            users[call.from_user.id]['sug_3'][int(users[call.from_user.id]['chas']/2)] = users[call.from_user.id]['gluk_tek']

The graphs are constructed by the grafik.py module.

from PIL import Image, ImageDraw

def graf(data):
    image = Image.new("RGB", (215, 190), (0, 0, 0))
    draw = ImageDraw.Draw(image)

    for i in range(0,12):
        draw.text((10+i*15,10),str(i*2))
        draw.line(((15+i*15,30),(15+i*15,180)),fill="grey",width=1)

    for i in range(0,4):
        draw.line(((15, 149-i*35), (185, 149-i*35)), fill="grey", width=1)  # 5
        draw.text((190, 143-i*35), str(5+i * 5))
    draw.line(((15, 170), (185, 170)), fill="grey", width=1) #2 шаг по 7
    draw.text((190, 164), str(2))
    for i in range(0,12):
        if data[i]!=100:
            draw.ellipse((13+15*i,182-data[i]*7,17+15*i,186-data[i]*7),"red")
            if i>0:
                draw.line(((15+15*(i-1),184-data[i-1]*7),(15+15*i,184-data[i]*7)), fill="red", width=1)
            #draw.line(((15+i*15,30),(15+i*15,180)),fill="grey",width=1)

    del draw
    image.save("test.png", "PNG")

After this, all that remains is to display the graphs depending on the current day number (for day 1, 1 graph is displayed, for the second – 2, for the third and subsequent days, the last 3 days are displayed).

 if call.data == "btn_graf":  # Обработка нажатия inline кнопки
        bot.send_message(call.message.chat.id,"График глюкозы "+str(users[call.from_user.id]['day'])+" день")
        if users[call.from_user.id]['day'] % 3 == 1:
            graf(users[call.from_user.id]['sug_1'])
        elif users[call.from_user.id]['day'] % 3 == 2:
            graf(users[call.from_user.id]['sug_2'])
        elif users[call.from_user.id]['day'] % 3 == 0:
            graf(users[call.from_user.id]['sug_3'])
        f = open("test.png", 'rb')
        bot.send_photo(call.message.chat.id, f)

        for j in range(1,3):
            if users[call.from_user.id]['day']>j:
                bot.send_message(call.message.chat.id, "График глюкозы " + str(users[call.from_user.id]['day']-j) + " день")
                if (users[call.from_user.id]['day']-j) % 3 == 1:
                    graf(users[call.from_user.id]['sug_1'])
                elif (users[call.from_user.id]['day']-j) % 3 == 2:
                    graf(users[call.from_user.id]['sug_2'])
                elif (users[call.from_user.id]['day']-j) % 3 == 0:
                    graf(users[call.from_user.id]['sug_3'])
                f = open("test.png", 'rb')
                bot.send_photo(call.message.chat.id, f)

3. The bot provides an alarm for high and low glucose levels, as well as for reaching compensation (deviation of key settings values ​​by no more than 0.1 from the reference values ​​(checking is element-by-element, not total)).

High or low sugar alarm

High or low sugar alarm

4. The administrator has access to statistics for connections to the bot by keywords, as well as the ability to clear statistics.

User connection statistics

User connection statistics

At the moment, the bot has added only my connections to the statistics. The user name is determined by the identifier

    UsrInfo = bot.get_chat_member(message.chat.id, message.chat.id).user
    zapis(str(UsrInfo.first_name))

The zapis function simply writes to the statistics file.

5. At the moment, the option of viewing the difference between the set parameters and the reference ones is available (in the case where there is no other way at all) using a password.

6. Also added help. No matter how intuitive the telegram bot interface is, you can't do without help.

Field testing…

We will test all the innovations in the general concept

Start screensaver

Start screensaver

After the splash screen appears, the bot prompts us to press the “Start” button

After the start, a warning message is displayed – this is just a bot, not your attending physician and a short sequence of actions. After clicking on the “Menu” button, the main menu of the simulator is displayed.

Simulator menu

Simulator menu

In accordance with the first point of the reference, we will form a patient. In the response message, the bot sent us a description of the patient.

Patient description

Patient description

Let's enter the initial settings of the MC. The MC button, the bot sends the current values. We send the bot 13 and enter 2.

Initial values ​​of the UK

Initial values ​​of the UK

We check if the settings of the management system have changed. Yes, everything is fine. We exit without correction.

Values ​​of the Criminal Code

Values ​​of the Criminal Code

We do the same with the FCI (enter the value 5) and BP (enter the value 0.5). Let's make the first step of the simulation “Rewind 2 hours”.

Glucose chart

Glucose chart

Yes, the base is small. Let's add the base to 1.

Glucose chart

Glucose chart

It's better, but still not enough. Let's add more and see what happens. After several iterations, we managed to achieve a more or less adequate base.

Glucose chart for less than 2 days

Glucose chart for less than 2 days

Yes, we are such “radishes” that our virtual patient has not eaten for the second day. Okay, for field tests it is excusable.

Let's try to estimate the FCI. The exact glucose value can be viewed using the “Info” button.

Bot response to click "Info"

Bot response to click “Info”

We set the unit of insulin. Click “Insulin” and, ignoring the bolus assistant, set 1.

Introduction of 1 insulin

Introduction of 1 insulin

Let's rewind 2 hours and look at the detailed information.

Detailed information

Detailed information

Sugar was 11.4 mmol/l, we put 1 unit of insulin and sugar became 10.2 mmol/l. If the base is approximately correct, then the FCI is approximately 1.2 mmol/l. We enter this value in the simulator settings.

Control of the correctness of the introduction of the FCI

Control of the correctness of the introduction of the FCI

Let's test the correctness of the FCI. Now glucose is 10.2 mmol/l. We have adjusted the FCI, so we will trust the bolus assistant to decrease.

Bolus Assistant

Bolus Assistant

The assistant suggests 2.7 units for correction. Let's agree. Rewind 2 hours. The FCI turned out to be a little overstated. It seems to me that we can set 1.1 mmol/l for now.

Glucose 7.9 with a target value of 7

Glucose 7.9 with a target value of 7

Let's leave the FCI for now. Let's try the UK. Let's eat 2 XE. Click “Food” and enter 2 XE. Next is the bolus assistant. It offers us to put 4 units of insulin per meal. We put it and rewind to 2 hours.

We get a glucose value of 8.5 mmol/l, and before eating it was 7.9 mmol/l. Let's add UK. It was 2, let's make it 2.5. We also eat 2 XE and the bolus assistant suggests putting 5 units of insulin. We agree and roll. After 2 hours, glucose is 8.5 mmol/l.

Thus, we very roughly configured the simulator according to the main parameters without taking into account fluctuations during the day. At the same time, we did not take into account the patient's meal schedule, which is very bad. The simulator did not give us a message about achieving correction. But to shorten the narrative, I suggest that we display a comparison of our configuration option with the correct one using a password.

Result of comparison of values ​​for BP and FCI

Result of comparison of values ​​for BP and FCI

Comparison result by UK

Comparison result by UK

We've almost done it with the BP. The deviation of the FCI is within the tolerance, but the MC still needs to be selected and selected.

The advantages of this simulator:

1) does not depend on the operating system, works wherever there is Telegram;

2) generates unique options, which prevents you from “ripping off” your neighbor;

3) introduces a “game” element into the learning of insulin pump parameter selection, which increases the interest of trainees;

Can be used in diabetes schools, medical institutions when studying the method of setting up insulin pumps.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *