Skip to content

Commit

Permalink
refactor/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nickxbs committed Dec 23, 2024
1 parent 4dd27ee commit 2360e74
Show file tree
Hide file tree
Showing 15 changed files with 708 additions and 728 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
241 changes: 121 additions & 120 deletions src/CGMSIMsimulator.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,121 @@
import logger from './utils';
import bolus from './bolus';
import basal from './basal';
import cortisone from './cortisone';
import alcohol from './alcohol';
import carbs from './carbs';
import liverRun from './liver';
import sgv from './sgv';
import pump from './pump';
import { MainParams, SimulationResult } from './Types';
import moment = require('moment');
import { physicalIsf, physicalLiver } from './physical';
import { transformNoteTreatmentsDrug } from './drug';
/**
* Simulation module for blood glucose data calculation.
* @param params - Main parameters for running the simulation.
* @returns Simulation result containing blood glucose data and other parameters.
*/
const simulator = (params: MainParams): SimulationResult => {
const {
patient,
entries,
treatments,
profiles, //PUMP SIMULATION
pumpEnabled,
activities, //7-DAYS
user,
} = params;
logger.info(user.nsUrl + ' Run Init CGMSim NSUrl:%o', user.nsUrl);

if (!treatments) {
throw new Error('treatments is ' + treatments);
}
if (!profiles) {
throw new Error('profiles is ' + profiles);
}

const isfConstant = patient.ISF;
const age = patient.AGE;
const gender = patient.GENDER;
const tz = patient?.TZ || 'UTC';

let isfActivityDependent = isfConstant;
let liverActivityFactor = 1;
if (isfActivityDependent < 9) {
throw new Error('Isf must be greater than or equal to 9');
}
if (activities && activities.length > 0) {
isfActivityDependent = isfConstant * physicalIsf(activities, age, gender);
liverActivityFactor = physicalLiver(activities, age, gender);
}

const weight = patient.WEIGHT;
const dia = patient.DIA;
const peak = patient.TP;
const carbsAbs = patient.CARBS_ABS_TIME;
const cr = patient.CR;

//Find basal boluses
const drugs = transformNoteTreatmentsDrug(treatments);

const activeDrugTreatments = drugs.filter(function (e) {
return e.minutesAgo <= 45 * 60; // keep only the basals from the last 45 hours
});

const bolusActivity = bolus(treatments, dia, peak);
const basalBolusActivity = basal(activeDrugTreatments, weight);
const cortisoneActivity = cortisone(activeDrugTreatments, weight);
const alcoholActivity = alcohol(activeDrugTreatments, weight, gender);
const basalPumpActivity = pumpEnabled
? pump(treatments, profiles, dia, peak)
: 0;
const carbsActivity = carbs(treatments, carbsAbs, isfActivityDependent, cr);

//activity calc carb
const liverActivity = liverRun(
isfConstant,
cr,
{
physical: liverActivityFactor,
alcohol: alcoholActivity,
},
weight,
tz,
);

const now = moment();
const orderedEntries = entries
.filter((e) => e.mills <= now.toDate().getTime())
.sort((a, b) => b.mills - a.mills);

const newSgvValue = sgv(
orderedEntries,
{
basalActivity: basalBolusActivity + basalPumpActivity,
liverActivity,
carbsActivity,
bolusActivity,
cortisoneActivity,
alcoholActivity,
},
isfActivityDependent,
);

logger.info(user.nsUrl + 'this is the simulator result: %o', {
...newSgvValue,
physicalISF: isfActivityDependent / isfConstant,
physicalLiver: liverActivityFactor,
});

// const arrows = arrowsRun([newSgvValue, ...entries]);

return {
...newSgvValue,
activityFactor: liverActivityFactor,
isf: { dynamic: isfActivityDependent, constant: isfConstant },
};
};

export default simulator;
import logger from './utils';
import bolus from './bolus';
import basal from './basal';
import cortisone from './cortisone';
import alcohol from './alcohol';
import carbs from './carbs';
import liverRun from './liver';
import sgv from './sgv';
import pump from './pump';
import { MainParams, SimulationResult } from './Types';
import moment = require('moment');
import { physicalIsf, physicalLiver } from './physical';
import { transformNoteTreatmentsDrug } from './drug';
/**
* Simulation module for blood glucose data calculation.
* @param params - Main parameters for running the simulation.
* @returns Simulation result containing blood glucose data and other parameters.
*/
const simulator = (params: MainParams): SimulationResult => {
const {
patient,
entries,
treatments,
profiles, //PUMP SIMULATION
pumpEnabled,
activities, //7-DAYS
user,
} = params;
logger.info(user.nsUrl + ' Run Init CGMSim NSUrl:%o', user.nsUrl);

if (!treatments) {
throw new Error('treatments is ' + treatments);
}
if (!profiles) {
throw new Error('profiles is ' + profiles);
}
if (patient.ISF < 9) {
throw new Error('Isf must be greater than or equal to 9');
}
const isfMMolConstant = patient.ISF / 18;
const age = patient.AGE;
const gender = patient.GENDER;
const tz = patient?.TZ || 'UTC';

Check warning on line 43 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 43 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

let isfActivityDependent = isfMMolConstant;
let liverActivityFactor = 1;

if (activities && activities.length > 0) {

Check warning on line 48 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
isfActivityDependent =
isfMMolConstant * physicalIsf(activities, age, gender);

Check warning on line 50 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
liverActivityFactor = physicalLiver(activities, age, gender);

Check warning on line 51 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 52 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const weight = patient.WEIGHT;
const dia = patient.DIA;
const peak = patient.TP;
const carbsAbs = patient.CARBS_ABS_TIME;
const cr = patient.CR;

//Find basal boluses
const drugs = transformNoteTreatmentsDrug(treatments);

const activeDrugTreatments = drugs.filter(function (e) {
return e.minutesAgo <= 45 * 60; // keep only the basals from the last 45 hours
});

const bolusActivity = bolus(treatments, dia, peak);
const basalBolusActivity = basal(activeDrugTreatments, weight);
const cortisoneActivity = cortisone(activeDrugTreatments, weight);
const alcoholActivity = alcohol(activeDrugTreatments, weight, gender);
const basalPumpActivity = pumpEnabled
? pump(treatments, profiles, dia, peak)

Check warning on line 72 in src/CGMSIMsimulator.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
: 0;
const carbsActivity = carbs(treatments, carbsAbs, isfActivityDependent, cr);

//activity calc carb
const liverActivity = liverRun(
isfMMolConstant,
cr,
{
physical: liverActivityFactor,
alcohol: alcoholActivity,
},
weight,
tz,
);

const now = moment();
const orderedEntries = entries
.filter((e) => e.mills <= now.toDate().getTime())
.sort((a, b) => b.mills - a.mills);

const newSgvValue = sgv(
orderedEntries,
{
basalActivity: basalBolusActivity + basalPumpActivity,
liverActivity,
carbsActivity,
bolusActivity,
cortisoneActivity,
alcoholActivity,
},
isfActivityDependent,
);

logger.info(user.nsUrl + 'this is the simulator result: %o', {
...newSgvValue,
physicalISF: isfActivityDependent / isfMMolConstant,
physicalLiver: liverActivityFactor,
});

// const arrows = arrowsRun([newSgvValue, ...entries]);

return {
...newSgvValue,
activityFactor: liverActivityFactor,
isf: { dynamic: isfActivityDependent, constant: isfMMolConstant },
};
};

export default simulator;
Loading

1 comment on commit 2360e74

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

Action wasn't able to generate report within GitHub comment limit. If you're facing this issue, please let me know by commenting under this issue.

Report generated by 🧪jest coverage report action from 2360e74

Please sign in to comment.