In [1]:
from csvreader import read_patient_csv
from PyTCI.weights import leanbodymass
from matplotlib import pyplot
from patient_solver import solve_for_marsh, solve_for_schnider
import math
import statistics
import numpy

In [2]:
patients = read_patient_csv()

obese_patients = []
for patient in patients:
    patient["bmi"] = leanbodymass.bmi(patient["height"], patient["weight"])
    if patient["bmi"] >=35:
        obese_patients.append(patient)

In [3]:
len(obese_patients)


Out[3]:
28

In [4]:
bmis = []
for person in obese_patients:
    bmis.append(person["bmi"])
    
pyplot.hist(bmis)


Out[4]:
(array([6., 5., 4., 8., 1., 0., 1., 1., 1., 1.]),
 array([35.4 , 37.14, 38.88, 40.62, 42.36, 44.1 , 45.84, 47.58, 49.32,
        51.06, 52.8 ]),
 <a list of 10 Patch objects>)

In [5]:
marsh_medians= []
marsh_bias = []
schnider_median = []
schnider_bias = []

mod_marsh_medians= []
mod_marsh_bias = []
mod_schnider_median = []
mod_schnider_bias = []

for patient in obese_patients:
    res = solve_for_marsh(patient, [])
    marsh_medians.append(res["median"])
    marsh_bias.append(res["bias"])
    
    res = solve_for_schnider(patient, [])
    schnider_median.append(res["median"])
    schnider_bias.append(res["bias"])
    
    if patient["sex"] == 'm':
        patient["weight"] = 1 * (patient["height"]-60)
    else:
        patient["weight"] = 1 * (patient["height"]-65)
    
    res = solve_for_marsh(patient, [])
    mod_marsh_medians.append(res["median"])
    mod_marsh_bias.append(res["bias"])
    
    res = solve_for_schnider(patient, [])
    mod_schnider_median.append(res["median"])
    mod_schnider_bias.append(res["bias"])

In [6]:
def returnmedian(a, b):
    marsh_medians= []
        
    for patient in obese_patients:

        if patient["sex"] == 'm':
            patient["weight"] = a * (patient["height"]-b)
        else:
            patient["weight"] = a * (patient["height"]-(b+5))

        res = solve_for_marsh(patient, [])
        marsh_medians.append(res["median"])
        
    return round(statistics.mean(marsh_medians), 2)

In [7]:
base = [marsh_medians, marsh_bias, schnider_median, schnider_bias]

modified = [mod_marsh_medians, mod_marsh_bias, mod_schnider_median, mod_schnider_bias]

print("baseline")
for thing in base:
    c = [i ** 2 for i in thing]
    a = numpy.sqrt(c)
    b = statistics.median(a)
    print(b)
    
print("modified")    
for thing in modified:
    c = [i ** 2 for i in thing]
    a = numpy.sqrt(c)
    b = statistics.median(a)
    print(b)


baseline
0.4130745879822339
0.35323276362642625
0.47536457968460366
0.4509686532186564
modified
0.40025640404345164
0.4421080524909651
0.5499296036115516
0.4349164561521482

In [8]:
pyplot.figure()
pyplot.scatter(marsh_medians, marsh_bias, alpha=0.5)
pyplot.scatter(mod_marsh_medians, mod_marsh_bias, alpha=0.5)
pyplot.title('Marsh')
pyplot.xlabel('Median Error')
pyplot.ylabel('Bias')


# pyplot.axis([0, 2, -2, 2])

pyplot.figure()
pyplot.scatter(schnider_median, schnider_bias, alpha=0.5)
pyplot.scatter(mod_schnider_median, mod_schnider_bias, alpha=0.5)
pyplot.title('Schnider')
pyplot.xlabel('Median Error')
pyplot.ylabel('Bias')
# pyplot.axis([0, 2, -2, 2])

pyplot.show()



In [ ]:
alphas = numpy.linspace(0.5, 2, num=16)
betas = numpy.linspace(0, 150, num=16)

row = (f"{b:4}"for b in betas)
print('{:4}'.format('XXX') + ' '.join(row))
for a in alphas:
#     print(*(f"{returnmedian(a, b):4}" for b in betas))
    row = (f"{returnmedian(a, b):4}" for b in betas)
    print(f"{a:4} " + ' '.join(row))


XXX  0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0 110.0 120.0 130.0 140.0 150.0
/home/jmathilee/.local/share/virtualenvs/Propofol-O2oO_sjt/lib/python3.7/site-packages/PyTCI/models/base.py:49: RuntimeWarning: divide by zero encountered in double_scalars
  self.x1 = self.x1 + drug_milligrams / self.v1
/home/jmathilee/.local/share/virtualenvs/Propofol-O2oO_sjt/lib/python3.7/site-packages/PyTCI/models/base.py:66: RuntimeWarning: invalid value encountered in double_scalars
  self.x1 = self.x1 + (x2k21 - x1k12 + x3k31 - x1k13 - x1k10)
 0.5 0.47 0.55  0.6 0.72 0.87 1.03 1.22 1.48 1.79 2.17 2.71  3.5 4.86 7.08 11.06 23.4

In [ ]: