Medical Apps & Things

October 2019

Polynomial Calculator Online

Polynomial Calculator Link
Polynomial Calculator PHP Link

Screen Shot 2019-10-16 at 1.19.58 pm

Attempt P(x) for small x,y datasets

import numpy
import matplotlib.pylab as plt

x = numpy.array([0,1,2,3,4,5,6,7,8,9])

# y = risk of stroke/tia/systemic embolism
y = numpy.array([0.3,0.9,2.9,4.6,6.7,10.0,13.6,15.7,15.2,17.4])
# y = risk of ischaemic stroke
#y = numpy.array([0.2,0.6,2.2,3.2,4.8,7.2,9.7,11.2,10.8,12.2])

degree = 1
modelvalues = numpy.array([0])
test = False

# Polynomial Regression
results = {}

while not test:

coeffs = numpy.polyfit(x, y, degree)

# Polynomial Coefficients
results['polynomial'] = coeffs.tolist()

# r-squared
p = numpy.poly1d(coeffs)
# fit values, and mean
yhat = p(x) # or [p(z) for z in x]
ybar = numpy.sum(y)/len(y) # or sum(y)/len(y)
ssreg = numpy.sum((yhat-ybar)**2) # or sum([ (yihat - ybar)**2 for yihat in yhat])
sstot = numpy.sum((y - ybar)**2) # or sum([ (yi - ybar)**2 for yi in y])
results['r^2'] = ssreg / sstot
derived = numpy.polyval(coeffs, x).tolist()
modelvalues = numpy.around(derived, decimals=1).tolist()
test = numpy.array_equal(y, modelvalues)

degree += 1

print ("COEFFICIENTS (Nth to 0 order):",results)
print("PROVIDED Y-VALUES:", y)
print("DERIVED Y-VALUES:",modelvalues)
plt.plot(x, y, "bo")
plt.plot(x, numpy.polyval(coeffs, x), "r--", lw = 2)

print(test, degree, "COEFFICIENTS")


How to get table data (sometimes) from PDF

#How to (try to) extract PDF tables
import camelot
import ghostscript
tables = camelot.read_pdf('PathToPDFFile', 'rb')
tables #Gives number of tables
tables.export('foo.csv', f='csv', compress=True) #to json, excel, html
tables[0] #Gives shape of table
tables[0].parsing_report #Gives data regarding table
tables[0].to_csv('foo.csv') #to_json, to_excel, to_html
tables[0].df #To a pandas DataFrame

Extract PDF text

#How to extract PDF text if no tables
import ghostscript
import PyPDF2
pdfFileObj = open(‘PathToPDFFile’, ‘rb’)
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0)
print(pageObj.extractText(), file=open(‘PathToOutputFile.txt’,’w’)) #'a' for append