From 707ae9a90a2bebc4b76a45f0a5e35cbac95887c5 Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Fri, 12 Feb 2021 13:30:58 -0500 Subject: [PATCH 1/9] Work in progres of modifying HTML and python files --- pdfgeneration/pdfgeneration.py | 9 ++++++--- .../pdftemplate/pdf-covid-negative-template.html | 4 +--- .../pdftemplate/pdf-covid-positive-template.html | 6 ++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index d73b544..8c27d41 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -174,9 +174,12 @@ def run(self, options): # 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']) + + predictionAnalysis = "" + for columnName in classification_data: + predictionAnalysis += "

{title}: {prediction}

".format(title = columnName, prediction = classification_data[columnName]) + + txt = txt.replace("${PRED_ANALYSIS}", "

HELLO

") txt = txt.replace("${X-RAY-IMAGE}", options.imagefile) time = datetime.datetime.now() diff --git a/pdfgeneration/pdftemplate/pdf-covid-negative-template.html b/pdfgeneration/pdftemplate/pdf-covid-negative-template.html index 92591ed..dd4610d 100644 --- a/pdfgeneration/pdftemplate/pdf-covid-negative-template.html +++ b/pdfgeneration/pdftemplate/pdf-covid-negative-template.html @@ -33,9 +33,7 @@

Prediction:

Probability

-

COVID-19: ${COVID-19}

-

Normal: ${NORMAL}

-

Pneumonia: ${PNEUMONIA}

+ ${PRED_ANALYSIS}

Note: probability percentages are rounded down to nearest percent

diff --git a/pdfgeneration/pdftemplate/pdf-covid-positive-template.html b/pdfgeneration/pdftemplate/pdf-covid-positive-template.html index 7d01060..1a25402 100644 --- a/pdfgeneration/pdftemplate/pdf-covid-positive-template.html +++ b/pdfgeneration/pdftemplate/pdf-covid-positive-template.html @@ -33,16 +33,14 @@

Prediction:

Probability

-

COVID-19: ${COVID-19}

-

Normal: ${NORMAL}

-

Pneumonia: ${PNEUMONIA}

+ ${PRED_ANALYSIS}

Note: probability percentages are rounded down to nearest percent

-

Severity Scores

+

Severity Scores

  • Geographic severity: ${GEO_SEVERITY}
  • Geographic extent score: ${GEO_EXTENT_SCORE}
  • From be28aff703bd696d7877297c1bcddf1101cc59ba Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Fri, 12 Feb 2021 16:24:59 -0500 Subject: [PATCH 2/9] Added filtering of the json file to only include class names --- pdfgeneration/pdfgeneration.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index 8c27d41..685027f 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -177,9 +177,10 @@ def run(self, options): predictionAnalysis = "" for columnName in classification_data: - predictionAnalysis += "

    {title}: {prediction}

    ".format(title = columnName, prediction = classification_data[columnName]) + if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): + predictionAnalysis += "

    {title}: {prediction}

    ".format(title = columnName, prediction = classification_data[columnName]) - txt = txt.replace("${PRED_ANALYSIS}", "

    HELLO

    ") + txt = txt.replace("${PRED_ANALYSIS}", predictionAnalysis) txt = txt.replace("${X-RAY-IMAGE}", options.imagefile) time = datetime.datetime.now() From effff5cf7fc62fdeb3b5c5317bfc00432065c0df Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Tue, 16 Feb 2021 11:01:04 -0500 Subject: [PATCH 3/9] Spacing fix --- pdfgeneration/pdftemplate/pdf-covid-positive-template.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfgeneration/pdftemplate/pdf-covid-positive-template.html b/pdfgeneration/pdftemplate/pdf-covid-positive-template.html index 1a25402..9bc1ca5 100644 --- a/pdfgeneration/pdftemplate/pdf-covid-positive-template.html +++ b/pdfgeneration/pdftemplate/pdf-covid-positive-template.html @@ -40,7 +40,7 @@

    Probability

    -

    Severity Scores

    +

    Severity Scores

    • Geographic severity: ${GEO_SEVERITY}
    • Geographic extent score: ${GEO_EXTENT_SCORE}
    • From 961774dbc891374aabeb5b33230be1e74a244d70 Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Wed, 24 Feb 2021 12:58:09 -0500 Subject: [PATCH 4/9] Safer dctionary/map accessing with better error handling using "get" --- pdfgeneration/pdfgeneration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index 685027f..c27b2ec 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -178,7 +178,7 @@ def run(self, options): predictionAnalysis = "" for columnName in classification_data: if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): - predictionAnalysis += "

      {title}: {prediction}

      ".format(title = columnName, prediction = classification_data[columnName]) + predictionAnalysis += "

      {title}: {prediction}

      ".format(title = columnName, prediction = classification_data.get(columnName, None)) txt = txt.replace("${PRED_ANALYSIS}", predictionAnalysis) txt = txt.replace("${X-RAY-IMAGE}", options.imagefile) From dee61e868200b11a9154fed3e54b4ef908d40502 Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Mon, 1 Mar 2021 17:48:21 -0500 Subject: [PATCH 5/9] Addressed PR comments about styling + formatting of python code --- pdfgeneration/pdfgeneration.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index c27b2ec..838526d 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -175,12 +175,12 @@ def run(self, options): txt = txt.replace("${PATIENT_TOKEN}", options.patientId) txt = txt.replace("${PREDICTION_CLASSIFICATION}", classification_data['prediction']) - predictionAnalysis = "" + prediction_analysis = "" for columnName in classification_data: if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): - predictionAnalysis += "

      {title}: {prediction}

      ".format(title = columnName, prediction = classification_data.get(columnName, None)) + prediction_analysis += "

      {title}: {prediction}

      ".format(title=columnName, prediction=classification_data.get(columnName, 'N/A')) - txt = txt.replace("${PRED_ANALYSIS}", predictionAnalysis) + txt = txt.replace("${PRED_ANALYSIS}", prediction_analysis) txt = txt.replace("${X-RAY-IMAGE}", options.imagefile) time = datetime.datetime.now() From 8dbf89fc97235865cc5fa5c4fe64cb5e28ed1862 Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Mon, 1 Mar 2021 17:53:49 -0500 Subject: [PATCH 6/9] Replaced previous usage of format() with f-strings, implemented into python script --- pdfgeneration/pdfgeneration.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index 838526d..9ee89a8 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -154,22 +154,22 @@ 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: + 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" # 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) @@ -177,8 +177,9 @@ def run(self, options): prediction_analysis = "" for columnName in classification_data: + prediction=classification_data.get(columnName, 'N/A') if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): - prediction_analysis += "

      {title}: {prediction}

      ".format(title=columnName, prediction=classification_data.get(columnName, 'N/A')) + prediction_analysis += f"

      {columnName}: {prediction}

      " txt = txt.replace("${PRED_ANALYSIS}", prediction_analysis) txt = txt.replace("${X-RAY-IMAGE}", options.imagefile) @@ -197,13 +198,13 @@ def run(self, options): try: disp = Display().start() - pdfkit.from_file(['pdftemplate/specificPatient.html'], '{}/patient_analysis.pdf'.format(options.outputdir)) + pdfkit.from_file(['pdftemplate/specificPatient.html'], f"{options.outputdir}/patient_analysis.pdf") finally: disp.stop() # cleanup os.remove("pdftemplate/specificPatient.html") - os.remove("pdftemplate/{}".format(options.imagefile)) + os.remove(f"pdftemplate/{options.imagefile}" From da7c22789057db9f8a981c4e0a2ff29174bf7039 Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Tue, 2 Mar 2021 11:45:47 -0500 Subject: [PATCH 7/9] Syntax fixes for f-strings --- pdfgeneration/pdfgeneration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index 9ee89a8..56f68f7 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -157,7 +157,7 @@ def run(self, options): with open(f"{options.inputdir}/prediction-default.json") as f: classification_data = json.load(f) try: - with open(f"{options.inputdir}/severity.json" as f: + with open(f"{options.inputdir}/severity.json") as f: severityScores = json.load(f) except: severityScores = None @@ -204,7 +204,7 @@ def run(self, options): # cleanup os.remove("pdftemplate/specificPatient.html") - os.remove(f"pdftemplate/{options.imagefile}" + os.remove(f"pdftemplate/{options.imagefile}") From 37c4da2fe83d97142492ea93c39edec4a5f11ad6 Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Tue, 2 Mar 2021 12:26:01 -0500 Subject: [PATCH 8/9] Refactored to follow PEP 8 indentation practices --- pdfgeneration/pdfgeneration.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index 56f68f7..f9fdae9 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -155,7 +155,7 @@ def run(self, options): print('Version: %s' % self.get_version()) # fetch input data with open(f"{options.inputdir}/prediction-default.json") as f: - classification_data = json.load(f) + classification_data = json.load(f) try: with open(f"{options.inputdir}/severity.json") as f: severityScores = json.load(f) @@ -166,7 +166,7 @@ def run(self, options): 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(f"pdftemplate/{template_file}") as f: @@ -177,9 +177,9 @@ def run(self, options): prediction_analysis = "" for columnName in classification_data: - prediction=classification_data.get(columnName, 'N/A') - if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): - prediction_analysis += f"

      {columnName}: {prediction}

      " + prediction=classification_data.get(columnName, 'N/A') + if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): + prediction_analysis += f"

      {columnName}: {prediction}

      " txt = txt.replace("${PRED_ANALYSIS}", prediction_analysis) txt = txt.replace("${X-RAY-IMAGE}", options.imagefile) @@ -189,18 +189,18 @@ def run(self, options): 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'], f"{options.outputdir}/patient_analysis.pdf") + 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") From 4ad03cc4cf514d131ffa7dd0de1e8b6754421bde Mon Sep 17 00:00:00 2001 From: Colin Wu Date: Tue, 2 Mar 2021 17:08:52 -0500 Subject: [PATCH 9/9] Fixed variable naming and spacing --- pdfgeneration/pdfgeneration.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pdfgeneration/pdfgeneration.py b/pdfgeneration/pdfgeneration.py index f9fdae9..e589ae9 100755 --- a/pdfgeneration/pdfgeneration.py +++ b/pdfgeneration/pdfgeneration.py @@ -176,10 +176,10 @@ def run(self, options): txt = txt.replace("${PREDICTION_CLASSIFICATION}", classification_data['prediction']) prediction_analysis = "" - for columnName in classification_data: - prediction=classification_data.get(columnName, 'N/A') - if (columnName != 'prediction') and (columnName != 'Prediction') and (columnName != '**DISCLAIMER**'): - prediction_analysis += f"

      {columnName}: {prediction}

      " + 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"

      {column_name}: {prediction}

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