Categories
Medical Calculators

How to make a Medical Calculator (SIRS)

Systemic inflammatory response syndrome (SIRS) is a problem in hospitals. It refers to widespread inflammation across the body. Using a simple program, we can create a calculator to rapidly (and automatically) determine if a patient meets the criteria for SIRS.

Pre-Requisites

Ensure that you read this tutorial to learn the basics of Colaboratory and how to navigate around files, and the interface. We’ll be using Colaboratory for our code so we won’t need to download anything.

Introduction

Firstly let’s print out the title of the progam we’re going to make. We can do this using a print function.

print ('SIRS Criteria Calculator')

We know we’re going to need to meet 2 of the SIRS criteria for a diagnosis. Therefore we need to keep track of how many criteria we’ve met. Let’s make a variable called points to store the number of criteria met.

print ('SIRS Criteria Calculator')
points = 0

You can name your variable anything you like, as long as it’s “1 word” (number_of_points is good as a variable name, but number of points will not work).

Now, let’s ask the user for the first bit of information; the patient’s temperature.

temp = input("What is the patient's temperature?")

Data Types

The variable temp here will be a ‘string’. For coding in general, it’s important to have a basic understanding of data types. We’ve listed the main ones below.

DATA TYPEDESCRIPTION
BooleanTrue or False, 1 or 0. It’s a binary system to see if a criteria is met.
FloatMore accurate than integers. All decimal numbers are floats. E.g. 4.2
IntegerWhole number values e.g. 1, -3, 29
StringA group of characters joined together e.g. "var""test variable"

We want to convert the string variable temp to a ‘float’ so we can measure it to 1 decimal place and perform mathematical operations with it later on. To do this, we need to make sure that the temp variable is a number and doesn’t contain any letters.

Let’s do both of these things using a combination of a ‘while’ loop with a ‘try’ statement.

#While True means to keep running the script in the loop until it's finished
while True:
    temp = input("What is the patient's temperature? ")
    try:
        #Tries to convert the string to a float, and sees if any errors occur (see below) 
        temp = float(temp)
    except ValueError:
        #If an error occurs, then it prompts the user to input a number
        print("You did not enter a number. Please enter a number.")
        continue
    else:
        #break - breaks the loop, saying that it's finished
        break

This should convert the temp variable to a float. We can check this by printing the output of the type function.

print(type(temp))

We usually describe temperature to 1 decimal place, so make sure to round it using the ‘round()’ function.

temp = round(temp, 1)
print("Temperature: " "%.1f" %(temp))

Let’s find out if the patient has met the first criteria for SIRS: temperature above 38C or below 36C. Using an ‘IF’ statement, we can tell if the criteria has been met.

if(temp > 38.0 or temp < 36.0):
    points += 1
    print("Points: " "%d" %(points))
else:
    print("Temperature is normal.")
    print("Points: " "%d" %(points))

So far, we’ve introduced the user to the program and set up a variable to keep track of how many criteria are met, recorded the patient’s temperature and then compared it to the criteria.

Let’s move on to the heart rate. We can do this in a similar way to the temperature, but we want it to be an integer rather than a floar (heart rate is not a decimal number!).

while True:
    heartRate = input("What is the patient's heart rate per minute? ")
    try:
        heartRate = int(heartRate)
    except:
        print("You did not enter a number. Please enter an integer.")
        continue
    else:
        break

Let’s compare it to the criteria for heart rate (>90 for SIRS) using an ‘IF’ statement.

print("Heart rate: " "%d" %(heartRate))
if(heartRate > 90):
    #adds 1 to the points variable
    points += 1
    print("Points: " "%d" %(points))
else:
    print("Heart rate is normal.")
    print("Points: " "%d" %(points))

We then need to know the respiratory rate and PaCO2. Let’s do the same as before.

while True:
    respRate = input("What is the patient's respiratory rate per minute? ")
    try:
        respRate = int(respRate)
    except:
        print("You did not enter a number. Please enter an integer.")
        continue
    else:
        break
print("Respiratory rate: " "%d" %(respRate))

while True:
    paCO2 = input("What is the patient's PaCO2 in mmHg? ")
    try:
        paCO2 = float(paCO2)
    except:
        print("You did not enter a number. Please enter a number")
        continue
    else:
        break
paCO2 = round(paCO2,1)
print("PaCO2: " "%.1f" %(paCO2))

The SIRS criteria require either respiratory rate or PaCO2 to be abnormal to get the SIRS point. Therefore we require the use of the ‘OR’ operator in our ‘IF’ statement.

if(respRate > 20 or paCO2 < 32):
    points += 1
    print("Points: " "%d" %(points))
else:
    print("Respiratory rate and PaCO2 are both normal.")
    print("Points: " "%d" %(points))

The final criteria is the white blood cell count. See if you can work it out before checking how we did it below.

Answer  

while True:
  wbc = input("What is the patient's White Blood Cell count /mm^3? ")
  try:
      wbc = int(wbc)
  except:
      print("You did not enter a number. Please enter an integer.")
      continue
  else:
      break
print("White Blood Cell count /mm^3: " "%.d" %(wbc))

We’ve taken the white blood cell count as an input, now let’s compare to to the SIRS criteria for white blood cells.

if(wbc > 12000 or wbc < 4000):
    points += 1
    print("Points: " "%d" %(points))
else:
    print("WBC is normal.")
    print("Points: " "%d" %(points))

We’ve completed the criteria assessment. We need to tell the user how many criteria were met and whether this indicates a diagnosis (meeting 2 of the criteria indicates a diagnosis of SIRS).

print("Total score: " "%d" %(points))
if(points >=2):
    print("The patient has met the criteria for SIRS.")
else:
    print("Your patient has NOT met the criteria for SIRS.")

Summary

All done! We’ve created a fully functioning SIRS Criteria Calculator. Along the way, we’ve learnt about the following:

  • Data types in Python and how to manipulate data types
  • If and While loops
  • Taking user inputs into a program and verifying inputs

Hopefully you’ve managed to follow along and found this useful! If you enjoyed this, or have any ideas, concerns, or expectations, please drop us a message here (we’d absolutely love to hear from you)!

Leave a Reply

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