[15a331]: / static / js / script.js

Download this file

189 lines (175 with data), 7.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
document.addEventListener('DOMContentLoaded', function () {
const form = document.getElementById('risk-form');
const resultDiv = document.getElementById('prediction-result');
const resultText = document.getElementById('result-text');
const calculateBMIButton = document.getElementById('calculate-bmi');
const bmiCalculator = document.getElementById('bmi-calculator');
const submitBMIButton = document.getElementById('submit-bmi');
const bmiInput = document.getElementById('bmi');
let riskFactorsChart;
// Create the initial empty chart on page load
createRiskFactorsChart({});
calculateBMIButton.addEventListener('click', function () {
bmiCalculator.classList.toggle('hidden');
});
submitBMIButton.addEventListener('click', function () {
const height = parseFloat(document.getElementById('height').value) / 100; // convert cm to m
const weight = parseFloat(document.getElementById('weight').value);
if (height && weight) {
const bmi = weight / (height * height);
bmiInput.value = bmi.toFixed(1);
bmiCalculator.classList.add('hidden');
}
});
form.addEventListener('submit', function (e) {
e.preventDefault();
const formData = new FormData(form);
const data = Object.fromEntries(formData.entries());
// Convert numeric strings to numbers
['age', 'bmi', 'glucose_level'].forEach(key => {
if (data[key] !== '') {
data[key] = parseFloat(data[key]);
} else if (key === 'glucose_level') {
// If glucose_level is empty, remove it from the data object
delete data[key];
}
});
// Convert boolean strings to integers
['hypertension', 'heart_disease', 'ever_married', 'residence_type'].forEach(key => {
data[key] = parseInt(data[key]);
});
fetch('/api/predict', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(result => {
console.log('Received result:', result);
if (result.success) {
const predictionData = result.prediction; // Access the nested prediction object
if (typeof predictionData.prediction === 'number' && !isNaN(predictionData.prediction)) {
const riskPercentage = (predictionData.prediction * 100).toFixed(2);
resultText.textContent = `The estimated stroke risk is ${riskPercentage}%`;
resultDiv.classList.remove('hidden');
if (predictionData.feature_importances) {
createRiskFactorsChart(predictionData.feature_importances);
} else {
console.warn('Feature importances not provided in the response');
}
} else {
console.error('Invalid prediction value received:', predictionData.prediction);
throw new Error('Invalid prediction value received');
}
} else {
console.error('Error in response:', result.error);
throw new Error(result.error || 'Unknown error occurred');
}
})
.catch(error => {
console.error('Error:', error);
resultText.textContent = `An error occurred while processing your request: ${error.message}`;
resultDiv.classList.remove('hidden');
});
});
function createRiskFactorsChart(featureImportances) {
const ctx = document.getElementById('risk-factors-chart').getContext('2d');
if (riskFactorsChart) {
riskFactorsChart.destroy();
}
const sortedImportances = Object.entries(featureImportances)
.sort((a, b) => b[1] - a[1])
.slice(0, 10);
const labels = sortedImportances.map(item => {
switch (item[0]) {
case 'age':
return 'Age';
case 'hypertension':
return 'Hypertension';
case 'heart_disease':
return 'Heart Disease';
case 'ever_married':
return 'Ever Married';
case 'residence_type':
return 'Residence Type';
case 'bmi':
return 'BMI';
case 'gender':
return 'Gender';
case 'smoking_status':
return 'Smoking Status';
case 'avg_glucose_level':
return 'Avg Glucose Level';
case 'age_glucose':
return 'Age * Avg Glucose Level';
case 'age_hypertension':
return 'Age * Hypertension';
case 'age_heart_disease':
return 'Age * Heart Disease';
case 'age_squared':
return 'Age Squared';
case 'glucose_squared':
return 'Avg Glucose Level Squared';
case 'bmi_age':
return 'BMI * Age';
case 'bmi_glucose':
return 'BMI * Avg Glucose Level';
default:
return item[0]; // For any other feature not explicitly mentioned
}
});
const data = sortedImportances.map(item => item[1]);
const COLORS = ["#CC7B5C", "#D4A27F", "#EBDBBC", "#9C8AA5", "#91A694", "#8B9BAE", "#666663", "#BFBFBA", "#E5E4DF", "#F0F0EB"];
riskFactorsChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Feature Importance',
data: data,
backgroundColor: COLORS,
borderColor: COLORS,
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Importance'
}
},
x: {
title: {
display: true,
text: ''
},
ticks: {
maxRotation: 45,
minRotation: -45
}
}
},
plugins: {
title: {
display: true,
text: 'Top 10 Risk Factors Importance'
},
legend: {
display: false
}
}
}
});
}
});