From b4f8aa6efa8d517bec2493e428014db5ce6c76d6 Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 14:09:23 +0000 Subject: [PATCH 1/8] Added dpi and savepdf options to comparison reporst for publications --- README.md | 8 ++++++++ bgcval2/analysis_compare.py | 16 ++++++++++++++++ bgcval2/bgcval2_make_report.py | 2 +- bgcval2/timeseries/timeseriesPlots.py | 15 ++++++++++++++- input_yml/TerraFIRMA_overshoot_runs_minimal.yml | 3 +++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d67affd4..fdfda0d8 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,8 @@ do_mass_download: master_suites: auto_download: strictFileCheck: +dpi: +savepdf: jobs: : @@ -268,6 +270,12 @@ These values are: - `strictFileCheck`: - A boolean which when True will raise an error if input model files are missing. - Default is True, set to False to skip this check. + - `dpi`: + - Output images Dots per inch (dpi) + - dpi=100 is fine for most purposes but 300 is needed for highres posters/publications. + - `savepdf`: + - Outpout the image as a pdf in addition to the standard png. + - This doesn't replace the image in the report, but saves a pdf version of the image in the images directory. - `jobs`: - A list of jobIDs, and some options on how they will appear in the final report. - The options are: diff --git a/bgcval2/analysis_compare.py b/bgcval2/analysis_compare.py index f1d6b833..c19b7ec0 100755 --- a/bgcval2/analysis_compare.py +++ b/bgcval2/analysis_compare.py @@ -192,6 +192,8 @@ def timeseries_compare(jobs, labels = {}, ensembles={}, config_user=None, + dpi=None, + savepdf=False, ): """ timeseries_compare: @@ -423,6 +425,8 @@ def timeseries_compare(jobs, thicknesses=lineThicknesses, linestyles=linestyles, labels=labels, + dpi=dpi, + savepdf=savepdf, ) # Generate a list of comparison images: @@ -504,6 +508,12 @@ def load_comparison_yml(master_compare_yml_fn): details['do_mass_download'] = input_yml_dict.get('do_mass_download', False) details['master_suites'] = input_yml_dict.get('master_suites', []) + # Image output settings: + # dpi: pixels per inch (image resolution) + # savepdf: also save the image as a pdf. + details['dpi'] = input_yml_dict.get('dpi', None) + details['savepdf'] = input_yml_dict.get('savepdf', False) + # auto download, can differ for each job. auto_download = input_yml_dict.get('auto_download', True) auto_download_dict = {jobID: auto_download for jobID in details['jobs'].keys()} @@ -579,6 +589,9 @@ def load_yml_and_run(compare_yml, config_user, skip_timeseries): suites = details['suites'] auto_download = details['auto_download'] strictFileCheck = details.get('strictFileCheck', True) + dpi = details.get('dpi', None) + savepdf = details.get('savepdf', False) + print('---------------------') print('timeseries_compare:', analysis_name) print('job ids:', jobs.keys()) @@ -632,6 +645,9 @@ def load_yml_and_run(compare_yml, config_user, skip_timeseries): linestyles=linestyles, labels=labels, config_user=config_user, + dpi=dpi, + savepdf=savepdf, + ) diff --git a/bgcval2/bgcval2_make_report.py b/bgcval2/bgcval2_make_report.py index 50bd6884..2371a97b 100755 --- a/bgcval2/bgcval2_make_report.py +++ b/bgcval2/bgcval2_make_report.py @@ -1636,7 +1636,7 @@ def newImageLocation(fn): if len(otherFilenames): # I think this is never happens anymore. - assert 0 + #assert 0 href = 'OtherPlots-others' hrefs.append(href) diff --git a/bgcval2/timeseries/timeseriesPlots.py b/bgcval2/timeseries/timeseriesPlots.py index c34115a2..e25f4e45 100644 --- a/bgcval2/timeseries/timeseriesPlots.py +++ b/bgcval2/timeseries/timeseriesPlots.py @@ -35,6 +35,8 @@ import matplotlib.patches as mpatches import cartopy import numpy as np +import pathlib + from numpy import hanning, hamming, bartlett, blackman from scipy import interpolate from collections import defaultdict @@ -706,6 +708,8 @@ def multitimeseries( thicknesses=defaultdict(lambda: 1), linestyles=defaultdict(lambda: '-'), labels={}, + dpi=None, + savepdf=False, ): if 0 in [len(timesD), len(list(timesD.keys()))]: return @@ -1053,7 +1057,16 @@ def multitimeseries( pyplot.suptitle(title) print("multitimeseries:\tsimpletimeseries:\tSaving:", filename) - pyplot.savefig(filename) + if not dpi: + pyplot.savefig(filename) + imext = pathlib.Path(filename).suffix + + # save image as pdf (publication?) + if savepdf: + pyplot.savefig(filename.replace(imext, '.pdf')) + + if pathlib.Path(filename).suffix in ['.png', '.jpg', '.jpeg']: + pyplot.savefig(filename, dpi=dpi) pyplot.close() diff --git a/input_yml/TerraFIRMA_overshoot_runs_minimal.yml b/input_yml/TerraFIRMA_overshoot_runs_minimal.yml index cfa54aac..f0e778ba 100644 --- a/input_yml/TerraFIRMA_overshoot_runs_minimal.yml +++ b/input_yml/TerraFIRMA_overshoot_runs_minimal.yml @@ -16,6 +16,9 @@ strict_file_check: False clean: True +dpi: 300 +savepdf: True + jobs: u-cs495: description: 'PI-Control' From edd97203289b94bc93fcfad3aeb642dddffa929a Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 14:11:31 +0000 Subject: [PATCH 2/8] Update bgcval2/bgcval2_make_report.py --- bgcval2/bgcval2_make_report.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bgcval2/bgcval2_make_report.py b/bgcval2/bgcval2_make_report.py index 2371a97b..6cd9104d 100755 --- a/bgcval2/bgcval2_make_report.py +++ b/bgcval2/bgcval2_make_report.py @@ -1636,7 +1636,6 @@ def newImageLocation(fn): if len(otherFilenames): # I think this is never happens anymore. - #assert 0 href = 'OtherPlots-others' hrefs.append(href) From f6180545381ee2158005c58acab6049dcd2f1404 Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 14:31:52 +0000 Subject: [PATCH 3/8] automatically asdding opdf file to report now too, just in case. --- bgcval2/bgcval2_make_report.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/bgcval2/bgcval2_make_report.py b/bgcval2/bgcval2_make_report.py index 2371a97b..cdacd691 100755 --- a/bgcval2/bgcval2_make_report.py +++ b/bgcval2/bgcval2_make_report.py @@ -81,11 +81,25 @@ def addImageToHtml(fn, imagesfold, reportdir, debug=True): "\n\tnewfn:", newfn, "\n\trelfn:", relfn) - + # sending a pdf as well. + pdf_fn = fn.replace('.png', '.pdf') + if os.path.exists(pdf_fn): + new_pdf_fn = newfn.replace('.png', '.pdf') + # Check if file is newer than the one in images. + if shouldIMakeFile( + pdf_fn, + new_pdf_fn, + ): + if debug: + print("addImageToHtml:\tAdding new pdf to report.", new_pdf_fn) + if os.path.exists(new_pdf_fn): + os.remove(new_pdf_fn) + shutil.copy2(pdf_fn, new_pdf_fn) + if not os.path.exists(newfn): - if debug: print("addImageToHtml:\tcopytree", fn, newfn) + if debug: + print("addImageToHtml:\tcopytree", fn, newfn) basedir = folder(os.path.dirname(newfn)) - #copytree(fn, newfn) if os.path.isdir(fn): shutil.copytree(fn, newfn, symlinks, ignore) else: @@ -93,14 +107,16 @@ def addImageToHtml(fn, imagesfold, reportdir, debug=True): else: #### # Check if the newer file is the same one from images. - if os.path.getmtime(fn) == os.path.getmtime(newfn): return relfn + if os.path.getmtime(fn) == os.path.getmtime(newfn): + return relfn #### # Check if file is newer than the one in images. if shouldIMakeFile( fn, newfn, ): - if debug: print("addImageToHtml:\tremoving old file", fn) + if debug: + print("addImageToHtml:\tremoving old file", fn) os.remove(newfn) shutil.copy2(fn, newfn) if debug: From 9efd7008b5ed274339745b3ee00a19e6321aab48 Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 15:35:50 +0000 Subject: [PATCH 4/8] added error message for incorrect dpi. --- bgcval2/analysis_compare.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bgcval2/analysis_compare.py b/bgcval2/analysis_compare.py index c19b7ec0..9c3d5c6e 100755 --- a/bgcval2/analysis_compare.py +++ b/bgcval2/analysis_compare.py @@ -514,6 +514,12 @@ def load_comparison_yml(master_compare_yml_fn): details['dpi'] = input_yml_dict.get('dpi', None) details['savepdf'] = input_yml_dict.get('savepdf', False) + try: + int(details['dpi']) + except: + raise ValueError(''.join(["Loading yml error: `dpi` needs to be an integer. Current value:", + str(details['dpi'])])) + # auto download, can differ for each job. auto_download = input_yml_dict.get('auto_download', True) auto_download_dict = {jobID: auto_download for jobID in details['jobs'].keys()} From 9d88a5de72623b62cb23c2f36be7e4bd1999cf0b Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 15:40:36 +0000 Subject: [PATCH 5/8] bug fix --- bgcval2/analysis_compare.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bgcval2/analysis_compare.py b/bgcval2/analysis_compare.py index 9c3d5c6e..e0ad87fb 100755 --- a/bgcval2/analysis_compare.py +++ b/bgcval2/analysis_compare.py @@ -514,11 +514,12 @@ def load_comparison_yml(master_compare_yml_fn): details['dpi'] = input_yml_dict.get('dpi', None) details['savepdf'] = input_yml_dict.get('savepdf', False) - try: - int(details['dpi']) - except: - raise ValueError(''.join(["Loading yml error: `dpi` needs to be an integer. Current value:", - str(details['dpi'])])) + if details['dpi']: # None is valid! + try: + int(details['dpi']) + except: + raise ValueError(''.join(["Loading yml error: `dpi` needs to be an integer. Current value:", + str(details['dpi'])])) # auto download, can differ for each job. auto_download = input_yml_dict.get('auto_download', True) From 93f859e6f6e57930652ab05c67ec060df20ab081 Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 15:57:16 +0000 Subject: [PATCH 6/8] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fdfda0d8..9ec8baf6 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,8 @@ These values are: - `savepdf`: - Outpout the image as a pdf in addition to the standard png. - This doesn't replace the image in the report, but saves a pdf version of the image in the images directory. + - The pdfs will be web-visible in the report image directory, but will not linked in the html. + - To view a pdf from a report, simply click the image, and replace the `png` extension with `pdf` in the browser path. - `jobs`: - A list of jobIDs, and some options on how they will appear in the final report. - The options are: From 721adff2e7326ca41385ee24085e3b78f4afb81c Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 15:57:23 +0000 Subject: [PATCH 7/8] Apply suggestions from code review --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ec8baf6..39da243b 100644 --- a/README.md +++ b/README.md @@ -271,10 +271,10 @@ These values are: - A boolean which when True will raise an error if input model files are missing. - Default is True, set to False to skip this check. - `dpi`: - - Output images Dots per inch (dpi) + - The resolution in dots per inch (dpi) of the output image. - dpi=100 is fine for most purposes but 300 is needed for highres posters/publications. - `savepdf`: - - Outpout the image as a pdf in addition to the standard png. + - Output the image as a pdf in addition to the standard png. - This doesn't replace the image in the report, but saves a pdf version of the image in the images directory. - The pdfs will be web-visible in the report image directory, but will not linked in the html. - To view a pdf from a report, simply click the image, and replace the `png` extension with `pdf` in the browser path. From 558c95e5606a04bec0bd8ae66d5df5f6b639c9ef Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Thu, 14 Dec 2023 16:03:22 +0000 Subject: [PATCH 8/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 39da243b..1ffbcbcd 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,7 @@ These values are: - `dpi`: - The resolution in dots per inch (dpi) of the output image. - dpi=100 is fine for most purposes but 300 is needed for highres posters/publications. + - If set to `None`, then the dpi will be set to your default value. - `savepdf`: - Output the image as a pdf in addition to the standard png. - This doesn't replace the image in the report, but saves a pdf version of the image in the images directory.