Web App to Predict Diabetes using IRIS IntegratedML
Tags: ai, webdev, programming
Published: 2023-06-27 17:37:03

Diabetes can be discovered from some parameters well known to the medical community. In this way, in order to help the medical community and computerized systems, especially AI, the National Institute of Diabetes and Digestive and Kidney Diseases published a very useful dataset for training ML algorithms in the detection/prediction of diabetes. This publication can be found on the largest and best known data repository for ML, Kaggle at https://www.kaggle.com/datasets/mathchi/diabetes-data-set.

The diabetes dataset has the following metadata information (source: https://www.kaggle.com/datasets/mathchi/diabetes-data-set):

  * Pregnancies: Number of times pregnant
  * Glucose: Plasma glucose concentration a 2 hours in an oral glucose tolerance test
  * BloodPressure: Diastolic blood pressure (mm Hg)
  * SkinThickness: Triceps skin fold thickness (mm)
  * Insulin: 2-Hour serum insulin (mu U/ml)
  * BMI: Body mass index (weight in kg/(height in m)^2)
  * DiabetesPedigreeFunction: Diabetes pedigree function (It provided some data on diabetes mellitus history in relatives and the genetic relationship of those relatives to the patient. This measure of genetic influence gave us an idea of the hereditary risk one might have with the onset of diabetes mellitus - source: https://machinelearningmastery.com/case-study-predicting-the-onset-of-diabetes-within-five-years-part-1-of-3/)
  * Age: Age (years)
  * Outcome: Class variable (0 or 1)

#### Number of Instances: 768

#### Number of Attributes: 8 plus class

#### For Each Attribute: (all numeric-valued)

  1. Number of times pregnant
  2. Plasma glucose concentration a 2 hours in an oral glucose tolerance test
  3. Diastolic blood pressure (mm Hg)
  4. Triceps skin fold thickness (mm)
  5. 2-Hour serum insulin (mu U/ml)
  6. Body mass index (weight in kg/(height in m)^2)
  7. Diabetes pedigree function
  8. Age (years)
  9. Class variable (0 or 1)

#### Missing Attribute Values: Yes

#### Class Distribution: (class value 1 is interpreted as "tested positive for diabetes")

## Get the Diabetes data from Kaggle

The Diabetes data from Kaggle can be loaded into an IRIS table using the Health-Dataset application: https://openexchange.intersystems.com/package/Health-Dataset. To do this, from your module.xml project, set the dependency (ModuleReference for Health Dataset):

 Module.xml with Health Dataset application reference
  
```
  <?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
  <Document name="predict-diseases.ZPM">
    <Module>
      <Name>predict-diseases</Name>
      <Version>1.0.0</Version>
      <Packaging>module</Packaging>
      <SourcesRoot>src/iris</SourcesRoot>
      <Resource Name="dc.predict.disease.PKG"/>
      <Dependencies>
        <ModuleReference>
          <Name>swagger-ui</Name>
          <Version>1.*.*</Version>
        </ModuleReference>
        <ModuleReference>
          <Name>dataset-health</Name>
          <Version>*</Version>
        </ModuleReference>
      </Dependencies>
       <CSPApplication
        Url="/predict-diseases"
        DispatchClass="dc.predict.disease.PredictDiseaseRESTApp"
        MatchRoles=":{$dbrole}"
        PasswordAuthEnabled="1"
        UnauthenticatedEnabled="1"
        Recurse="1"
        UseCookies="2"
        CookiePath="/predict-diseases"
       />
       <CSPApplication
        CookiePath="/disease-predictor/"
        DefaultTimeout="900"
        SourcePath="/src/csp"
        DeployPath="${cspdir}/csp/${namespace}/"
        MatchRoles=":{$dbrole}"
        PasswordAuthEnabled="0"
        Recurse="1"
        ServeFiles="1"
        ServeFilesTimeout="3600"
        UnauthenticatedEnabled="1"
        Url="/disease-predictor"
        UseSessionCookie="2"
      />
    </Module>
   
  </Document>
</Export>
```
<h2>
Web Frontend and Backend Application to Predict Diabetes
</h2>

<p>
Go to Open Exchange  app link (https://openexchange.intersystems.com/package/Disease-Predictor) and follow these steps:
</p>
        
<ol>
<li>
Clone/git pull the repo into any local directory
</li>
</ol>
        
<pre><code>$ git clone https://github.com/yurimarx/predict-diseases.git
</code></pre>
        
<ol start="2">
<li>
Open a Docker terminal in this directory and run:
</li>
</ol>
        
<pre><code>$ docker-compose build
</code></pre>
        
<ol start="3">
<li>
Run the IRIS container:
</li>
</ol>
        
<pre><code>$ docker-compose up -d 
</code></pre>
        
<ol start="4">
<li>
Go to Execute Query into Management Portal to train the AI model: <a href="http://localhost:52773/csp/sys/exp/%25CSP.UI.Portal.SQL.Home.zen?%24NAMESPACE=USER" rel="nofollow">http://localhost:52773/csp/sys/exp/%25CSP.UI.Portal.SQL.Home.zen?$NAMESPACE=USER</a>
</li>
<li>
Create the VIEW used to train:
</li>
</ol>
        
<pre><code>CREATE VIEW DiabetesTrain AS SELECT Outcome, age, bloodpressure, bmi, diabetespedigree, glucose, insulin, pregnancies, skinthickness FROM dc_data_health.Diabetes
</code></pre>
        
<ol start="6">
<li>
Create the AI Model using the view:
</li>
</ol>
        
<pre><code>CREATE MODEL DiabetesModel PREDICTING (Outcome) FROM DiabetesTrain
</code></pre>
        
<ol start="7">
<li>
Train the model:
</li>
</ol>

<pre><code>TRAIN MODEL DiabetesModel
</code></pre>
        
<ol start="8">
<li>
Go to <a href="http://localhost:52773/disease-predictor/index.html" rel="nofollow">http://localhost:52773/disease-predictor/index.html</a> to use the Disease Predictor frontend and predict diseases like this: <img alt="Disease-Predictor" src="https://github.com/yurimarx/predict-diseases/raw/master/mainscreen.png" title="Disease Predictor" />
</li>
</ol>
        
<h2>
Behind the scenes
</h2>

<h3>
Backend ClassMethod to predict Diabetes
</h3>

<p>
InterSystems IRIS allows you execute SELECT to predict using the previous model created.
</p>
        


Backend ClassMethod to predict Diabetes

```
/// Predict Diabetes
ClassMethod PredictDiabetes() As %Status
{
    Try {
      Set data = {}.%FromJSON(%request.Content)
     
      Set qry = "SELECT PREDICT(DiabetesModel) As PredictedDiabetes, "
                  _"age, bloodpressure, bmi, diabetespedigree, glucose, insulin, "
                  _"pregnancies, skinthickness "
                  _"FROM (SELECT "_data.age_" AS age, "
                  _data.bloodpressure_" As bloodpressure, "
                  _data.bmi_" AS bmi, "
                  _data.diabetespedigree_" AS diabetespedigree, "
                  _data.glucose_" As glucose, "
                  _data.insulin_" AS insulin, "
                  _data.pregnancies_" As pregnancies, "
                  _data.skinthickness_" AS skinthickness)"
      Set tStatement = ##class(%SQL.Statement).%New()
      Set qStatus = tStatement.%Prepare(qry)
      If qStatus'=1 {WRITE "%Prepare failed:" DO $System.Status.DisplayError(qStatus) QUIT}
      Set rset = tStatement.%Execute()
      Do rset.%Next()
 
      Set Response = {}
      Set Response.PredictedDiabetes = rset.PredictedDiabetes
      Set Response.age = rset.age
      Set Response.bloodpressure = rset.bloodpressure
      Set Response.bmi = rset.bmi
      Set Response.diabetespedigree = rset.diabetespedigree
      Set Response.glucose = rset.glucose
      Set Response.insulin = rset.insulin
      Set Response.pregnancies = rset.pregnancies
      Set Response.skinthickness = rset.skinthickness
 
      Set %response.Status = 200
      Set %response.Headers("Access-Control-Allow-Origin")="*"
     
      Write Response.%ToJSON()
     
      Return 1
     
    } Catch err {
      write !, "Error name: ", ?20, err.Name,
          !, "Error code: ", ?20, err.Code,
          !, "Error location: ", ?20, err.Location,
          !, "Additional data: ", ?20, err.Data, !
      Return 0
    }
}
```
<p>
Now, any web application can consume the prediction and show the results. See the source code into frontend folder to predict-diseases application.
</p>

<p>

</p></body></html>