function validateBaseParam(baseParamInputValue) {
// 将输入值解析为浮点数
let baseParam = parseFloat(baseParamInputValue);
// 验证 baseParam 是否在合法范围内,否则设置为默认值 0.8
if (isNaN(baseParam) || baseParam <= 0 || baseParam >= 1) {
baseParam = 0.8;
}
return baseParam;
}
function parsePlateletCounts(inputString) {
// 将输入字符串分隔并转换为浮点数数组,过滤掉无效值
return inputString.split(',').map(num => parseFloat(num.trim())).filter(num => !isNaN(num));
}
function calculatePVI(baseParam, plateletCounts) {
// 验证是否有有效的血小板计数
if (plateletCounts.length === 0) {
alert("Invalid input. Please enter a valid sequence of numbers.");
return;
}
const epsilon = 1e-10;
const n = plateletCounts.length;
const lowestCount = Math.min(...plateletCounts);
const severity = Math.pow(baseParam, lowestCount);
let pviIndex, pviScore;
if (n <= 2) {
// 少于或等于两个血小板计数时的计算
pviIndex = Math.log(severity + epsilon);
} else {
// 多于两个血小板计数时的计算
const avgPlatelet = plateletCounts.reduce((a, b) => a + b, 0) / n; // 平均值
const ssd = plateletCounts.reduce((sum, x) => sum + Math.pow(x - avgPlatelet, 2), 0); // 标准化平方差
const firstDiffs = plateletCounts.slice(1).map((x, i) => x - plateletCounts[i]); // 相邻差值
const rssDiff = Math.sqrt(firstDiffs.reduce((sum, x) => sum + x * x, 0)); // 均方根
const fluctuation = Math.sqrt(ssd) * rssDiff; // 波动性
pviIndex = Math.log(severity * fluctuation + epsilon);
}
// 根据 pviIndex 的值计算 pviScore
if (pviIndex <= -10) {
pviScore = 0;
} else if (pviIndex <= -5) {
pviScore = 1;
} else if (pviIndex <= 0) {
pviScore = 2;
} else if (pviIndex <= 5) {
pviScore = 3;
} else if (pviIndex <= 8) {
pviScore = 4;
} else if (pviIndex <= 12) {
pviScore = 5;
} else {
pviScore = 6;
}
// 输出结果到控制台
console.log(`Base Parameter: ${baseParam}`);
console.log(`Platelet Counts: ${plateletCounts}`);
console.log(`Lowest Count: ${lowestCount}`);
console.log(`Severity: ${severity}`);
console.log(`PVI Index: ${pviIndex}`);
console.log(`PVI Score: ${pviScore}`);
}
// 示例使用
const baseParamInputValue = "0.8"; // 模拟用户输入的基础参数值
const plateletInputString = "10.5, 16, 48.1, 72"; // 模拟用户输入的血小板计数
const baseParam = validateBaseParam(baseParamInputValue);
const plateletCounts = parsePlateletCounts(plateletInputString);
calculatePVI(baseParam, plateletCounts);