Skip to content

Commit

Permalink
Merge pull request #123 from valeriupredoi/dev_dpi
Browse files Browse the repository at this point in the history
Added dpi and savepdf options to comparison report for publications
  • Loading branch information
ledm authored Dec 14, 2023
2 parents b086386 + 558c95e commit e389ba0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ do_mass_download: <bool>
master_suites: <str>
auto_download: <bool>
strictFileCheck: <bool>
dpi: <int>
savepdf: <bool>
jobs:
<jobID1>:
Expand Down Expand Up @@ -268,6 +270,15 @@ 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`:
- 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.
- 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:
Expand Down
23 changes: 23 additions & 0 deletions bgcval2/analysis_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def timeseries_compare(jobs,
labels = {},
ensembles={},
config_user=None,
dpi=None,
savepdf=False,
):
"""
timeseries_compare:
Expand Down Expand Up @@ -423,6 +425,8 @@ def timeseries_compare(jobs,
thicknesses=lineThicknesses,
linestyles=linestyles,
labels=labels,
dpi=dpi,
savepdf=savepdf,
)

# Generate a list of comparison images:
Expand Down Expand Up @@ -504,6 +508,19 @@ 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)

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)
auto_download_dict = {jobID: auto_download for jobID in details['jobs'].keys()}
Expand Down Expand Up @@ -579,6 +596,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())
Expand Down Expand Up @@ -632,6 +652,9 @@ def load_yml_and_run(compare_yml, config_user, skip_timeseries):
linestyles=linestyles,
labels=labels,
config_user=config_user,
dpi=dpi,
savepdf=savepdf,

)


Expand Down
27 changes: 21 additions & 6 deletions bgcval2/bgcval2_make_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,42 @@ 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:
shutil.copy2(fn, newfn)
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:
Expand Down Expand Up @@ -1636,7 +1652,6 @@ def newImageLocation(fn):

if len(otherFilenames):
# I think this is never happens anymore.
assert 0
href = 'OtherPlots-others'

hrefs.append(href)
Expand Down
15 changes: 14 additions & 1 deletion bgcval2/timeseries/timeseriesPlots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()


Expand Down
3 changes: 3 additions & 0 deletions input_yml/TerraFIRMA_overshoot_runs_minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ strict_file_check: False

clean: True

dpi: 300
savepdf: True

jobs:
u-cs495:
description: 'PI-Control'
Expand Down

0 comments on commit e389ba0

Please sign in to comment.