Skip to content

Instantly share code, notes, and snippets.

@NeutralKaon
Created December 6, 2020 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeutralKaon/f0f242f96992da1763ad76bf76ba4484 to your computer and use it in GitHub Desktop.
Save NeutralKaon/f0f242f96992da1763ad76bf76ba4484 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# JJM -- parse cvi42 / cmr42 xml reports and extract relevant cardiac parameters
# Plonk them into a csv file for later dealings (in R)
# Supposedly biologist friendly.
import sys
import csv
from bs4 import BeautifulSoup
multiplyFlag = True
if __name__ == "__main__":
if ((len(sys.argv) ==1) or (len(sys.argv) >3)) :
print("\nusage: input_file.xml output_file.csv")
print("Alternately, just supply input_file.xml and the output is stdout\n")
print("Starting gui...\n")
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
window = Tk()
window.title("Cmr42 XML to CSV script")
lbl = Label(window, text="CMR42 Converter script")
lbl.grid(column=0,row=0)
def clicked():
xmlNames = tkFileDialog.askopenfilenames(title="Select XML files",filetypes = (("XML files","*.xml"),))
csv_file_path = tkFileDialog.asksaveasfilename(title="CSV File Path",filetypes = (("CSV files","*.csv"),),defaultextension="csv")
#Open CSV file, write header
csv_file = csv.writer(open(csv_file_path, "wb+"),quoting=csv.QUOTE_MINIMAL)
for ix in range(len(xmlNames)):
#Write contents
xfp = open(xmlNames[ix],'r')
y = BeautifulSoup(xfp,features="xml")
try:
value=y.RV.EDV["val"];
doRV = True;
except:
doRV = False;
if (ix == 0):
if (doRV == True):
csv_file.writerow(["Name",
"Custom.ID",
"History",
"EDV.ml",
"ESV.ml",
"SV.ml",
"EF.percent",
"CO.ml.min",
"HeartRate.min",
"MyoMassDiastole.g",
"MyoMassSystole.g",
"RVEDV.ml",
"RVESV.ml",
"RVSV.ml",
"RVEF.percent",
"RVCO.ml.min"])
elif (doRV == False):
csv_file.writerow(["Name",
"Custom.ID",
"History",
"EDV.ml",
"ESV.ml",
"SV.ml",
"EF.percent",
"CO.ml.min",
"HeartRate.min",
"MyoMassDiastole.g",
"MyoMassSystole.g"])
if (doRV == True):
csv_file.writerow([y.Patient.Name["val"],
y.Patient.CustomPatientID["val"],
y.Study.History["val"],
y.LV.EDV["val"],
y.LV.ESV["val"],
y.LV.SV["val"],
y.LV.EF["val"],
str(5*float(y.LV.CO["val"])),
str(5*float(y.LV.HR["val"])),
y.LV.MyoMass_diast["val"],
y.LV.MyoMass_syst["val"],
y.RV.EDV["val"],
y.RV.ESV["val"],
y.RV.SV["val"],
y.RV.EF["val"],
y.RV.CO["val"]\
])
elif (doRV == False):
csv_file.writerow([y.Patient.Name["val"],
y.Patient.CustomPatientID["val"],
y.Study.History["val"],
y.LV.EDV["val"],
y.LV.ESV["val"],
y.LV.SV["val"],
y.LV.EF["val"],
str(5*float(y.LV.CO["val"])),
str(5*float(y.LV.HR["val"])),
y.LV.MyoMass_diast["val"],
y.LV.MyoMass_syst["val"],
])
else:
print("This should never have occurred Jack!");
quit()
btn=Button(window,text="Run",command=clicked)
btn.grid(column=0,row=1)
window.mainloop()
quit()
elif len(sys.argv) == 2:
#Put output to stdout
doStdOutFlag=1
elif len(sys.argv) == 3:
doStdOutFlag=0
#Read arguments
xml_file_path = sys.argv[1]
xfp = open(xml_file_path,'r')
y = BeautifulSoup(xfp,features="xml")
if doStdOutFlag:
csv_file = csv.writer(sys.stdout,quoting=csv.QUOTE_MINIMAL)
else:
csv_file_path = sys.argv[2]
csv_file = csv.writer(open(csv_file_path, "wb+"),quoting=csv.QUOTE_MINIMAL)
csv_file.writerow(["Name",
"Custom.ID",
"History",
"EDV.ml",
"ESV.ml",
"SV.ml",
"EF.percent",
"CO.ml.min",
"HeartRate.min",
"MyoMassDiastole.g",
"MyoMassSystole.g",
"RVEDV.ml",
"RVESV.ml",
"RVSV.ml",
"RVEF.percent",
"RVCO.ml.min"])
csv_file.writerow([y.Patient.Name["val"],
y.Patient.CustomPatientID["val"],
y.Study.History["val"],
y.LV.EDV["val"],
y.LV.ESV["val"],
y.LV.SV["val"],
y.LV.EF["val"],
y.LV.CO["val"],
y.LV.HR["val"],
y.LV.MyoMass_diast["val"],
y.LV.MyoMass_syst["val"],
y.RV.EDV["val"],
y.RV.ESV["val"],
y.RV.SV["val"],
y.RV.EF["val"],
y.RV.CO["val"]\
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment