Skip to content

Commit

Permalink
Merge pull request #5 from darwinai/CNXC-167
Browse files Browse the repository at this point in the history
[CNXC 167] Dynamically read/write analysis results into PDF
  • Loading branch information
IvanToronto authored Mar 10, 2021
2 parents f5dd628 + 4ad03cc commit 1e3e3c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
41 changes: 23 additions & 18 deletions pdfgeneration/pdfgeneration.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,52 +154,57 @@ def run(self, options):
print(Gstr_title)
print('Version: %s' % self.get_version())
# fetch input data
with open('{}/prediction-default.json'.format(options.inputdir)) as f:
classification_data = json.load(f)
with open(f"{options.inputdir}/prediction-default.json") as f:
classification_data = json.load(f)
try:
with open('{}/severity.json'.format(options.inputdir)) as f:
with open(f"{options.inputdir}/severity.json") as f:
severityScores = json.load(f)
except:
severityScores = None

# output pdf here
print("Creating pdf file in {}...".format(options.outputdir))
print(f"Creating pdf file in {options.outputdir}...")
template_file = "pdf-covid-positive-template.html"
if classification_data['prediction'] != "COVID-19" or severityScores is None:
template_file = "pdf-covid-negative-template.html"
template_file = "pdf-covid-negative-template.html"
# put image file in pdftemple folder to use it in pdf
shutil.copy(options.inputdir + '/' + options.imagefile, "pdftemplate/")
with open("pdftemplate/{}".format(template_file)) as f:
with open(f"pdftemplate/{template_file}") as f:
txt = f.read()
# replace the values
txt = txt.replace("${PATIENT_TOKEN}", options.patientId)
txt = txt.replace("${PREDICTION_CLASSIFICATION}", classification_data['prediction'])
txt = txt.replace("${COVID-19}", classification_data['COVID-19'])
txt = txt.replace("${NORMAL}", classification_data['Normal'])
txt = txt.replace("${PNEUMONIA}", classification_data['Pneumonia'])

prediction_analysis = ""
for column_name in classification_data:
prediction = classification_data.get(column_name, 'N/A')
if (column_name != 'prediction') and (column_name != 'Prediction') and (column_name != '**DISCLAIMER**'):
prediction_analysis += f"<h3>{column_name}: <span>{prediction}</span></h3>"

txt = txt.replace("${PRED_ANALYSIS}", prediction_analysis)
txt = txt.replace("${X-RAY-IMAGE}", options.imagefile)

time = datetime.datetime.now()
txt = txt.replace("${month-date}", time.strftime("%c"))
txt = txt.replace("${year}", time.strftime("%Y"))
# add the severity value if prediction is covid
if template_file == "pdf-covid-positive-template.html":
txt = txt.replace("${GEO_SEVERITY}", severityScores["Geographic severity"])
txt = txt.replace("${GEO_EXTENT_SCORE}", severityScores["Geographic extent score"])
txt = txt.replace("${OPC_SEVERITY}", severityScores["Opacity severity"])
txt = txt.replace("${OPC_EXTENT_SCORE}", severityScores['Opacity extent score'])
txt = txt.replace("${GEO_SEVERITY}", severityScores["Geographic severity"])
txt = txt.replace("${GEO_EXTENT_SCORE}", severityScores["Geographic extent score"])
txt = txt.replace("${OPC_SEVERITY}", severityScores["Opacity severity"])
txt = txt.replace("${OPC_EXTENT_SCORE}", severityScores['Opacity extent score'])
with open("pdftemplate/specificPatient.html", 'w') as writeF:
writeF.write(txt)
writeF.write(txt)

try:
disp = Display().start()
pdfkit.from_file(['pdftemplate/specificPatient.html'], '{}/patient_analysis.pdf'.format(options.outputdir))
disp = Display().start()
pdfkit.from_file(['pdftemplate/specificPatient.html'], f"{options.outputdir}/patient_analysis.pdf")
finally:
disp.stop()
disp.stop()

# cleanup
os.remove("pdftemplate/specificPatient.html")
os.remove("pdftemplate/{}".format(options.imagefile))
os.remove(f"pdftemplate/{options.imagefile}")



Expand Down
4 changes: 1 addition & 3 deletions pdfgeneration/pdftemplate/pdf-covid-negative-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ <h4>Prediction:</h4>
<section class="probability">
<div class="probability--details">
<h2>Probability</h2>
<h3>COVID-19: <span>${COVID-19}</span></h3>
<h3>Normal: <span>${NORMAL}</span></h3>
<h3>Pneumonia: <span>${PNEUMONIA}</span></h3>
${PRED_ANALYSIS}
<p><small>Note: probability percentages are rounded down to nearest percent</small></p>
</div>
</section>
Expand Down
4 changes: 1 addition & 3 deletions pdfgeneration/pdftemplate/pdf-covid-positive-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ <h4>Prediction:</h4>
<section class="probability">
<div class="probability--details">
<h2>Probability</h2>
<h3>COVID-19: <span>${COVID-19}</span></h3>
<h3>Normal: <span>${NORMAL}</span></h3>
<h3>Pneumonia: <span>${PNEUMONIA}</span></h3>
${PRED_ANALYSIS}
<p><small>Note: probability percentages are rounded down to nearest percent</small></p>
</div>
</section>
Expand Down

0 comments on commit 1e3e3c5

Please sign in to comment.