Skip to content

Commit

Permalink
Merge pull request #808 from AFM-SPM/ns-rse/807-dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-rse authored Mar 5, 2024
2 parents 8798977 + ccfbe23 commit b9212b8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
84 changes: 78 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Test utils."""
"""Test the utils module."""

from pathlib import Path

Expand Down Expand Up @@ -46,11 +46,14 @@ def test_update_config(caplog) -> None:
@pytest.mark.parametrize(
("image_name", "core_set", "title", "zrange"),
[
("extracted_channel", False, "Raw Height", [0, 3]),
("z_threshed", True, "Height Thresholded", [0, 3]),
("grain_image", False, "", [0, 3]), # non-binary image
("grain_mask", False, "", [None, None]), # binary image
("grain_mask_image", False, "", [0, 3]), # non-binary image
pytest.param("extracted_channel", False, "Raw Height", [0, 3], id="Non-binary image, not in core, with title"),
pytest.param("z_threshed", True, "Height Thresholded", [0, 3], id="Non-binary image, in core, with title"),
pytest.param(
"grain_mask", False, "", [None, None], id="Binary image, not in core, set no title"
), # binary image
pytest.param(
"grain_mask_image", False, "", [0, 3], id="Non-binary image, not in core, set no title"
), # non-binary image
],
)
def test_update_plotting_config(
Expand All @@ -68,6 +71,75 @@ def test_update_plotting_config(
assert process_scan_config["plotting"]["plot_dict"][image_name]["zrange"] == zrange


@pytest.mark.parametrize(
("plotting_config", "target_config"),
[
pytest.param(
{
"run": True,
"savefig_dpi": None,
"pixel_interpolation": None,
"plot_dict": {
"extracted_channel": {
"filename": "00-raw_heightmap",
"image_type": "non-binary",
"savefig_dpi": 100,
}
},
},
{
"run": True,
"savefig_dpi": None,
"pixel_interpolation": None,
"plot_dict": {
"extracted_channel": {
"filename": "00-raw_heightmap",
"image_type": "non-binary",
"savefig_dpi": 100,
"pixel_interpolation": None,
}
},
},
id="DPI None in main, extracted channel DPI should stay at 100",
),
pytest.param(
{
"run": True,
"savefig_dpi": 600,
"pixel_interpolation": None,
"plot_dict": {
"extracted_channel": {
"filename": "00-raw_heightmap",
"image_type": "non-binary",
"savefig_dpi": 100,
}
},
},
{
"run": True,
"savefig_dpi": 600,
"pixel_interpolation": None,
"plot_dict": {
"extracted_channel": {
"filename": "00-raw_heightmap",
"image_type": "non-binary",
"savefig_dpi": 600,
"pixel_interpolation": None,
}
},
},
id="DPI 600 in main, extracted channel DPI should update to 600",
),
],
)
def test_udpate_plotting_config_adding_required_options(plotting_config: dict, target_config: dict, caplog) -> None:
"""Only updates plotting_dict parameters from parent plotting config if value is not None."""
update_plotting_config(plotting_config)
assert plotting_config == target_config
if plotting_config["savefig_dpi"] == 600:
assert "100 > 600" in caplog.text


def test_get_thresholds_otsu(image_random: np.ndarray) -> None:
"""Test of get_thresholds() method otsu threshold."""
thresholds = get_thresholds(image=image_random, threshold_method="otsu", **THRESHOLD_OPTIONS)
Expand Down
2 changes: 1 addition & 1 deletion topostats/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ plotting:
run: true # Options : true, false
style: topostats.mplstyle # Options : topostats.mplstyle or path to a matplotlibrc params file
savefig_format: null # Options : null, png, svg or pdf. tif is also available although no metadata will be saved. (defaults to png) See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
savefig_dpi: null # Options : null (defaults to figure) see https://afm-spm.github.io/TopoStats/main/configuration.html#further-customisation and https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
savefig_dpi: null # Options : null (defaults to the value in topostats/plotting_dictionary.yaml), see https://afm-spm.github.io/TopoStats/main/configuration.html#further-customisation and https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
pixel_interpolation: null # Options : https://matplotlib.org/stable/gallery/images_contours_and_fields/interpolation_methods.html
image_set: core # Options : all, core
zrange: [null, null] # low and high height range for core images (can take [null, null]). low <= high
Expand Down
2 changes: 1 addition & 1 deletion topostats/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ def dict_to_hdf5(open_hdf5_file: h5py.File, group_path: str, dictionary: dict) -
None
"""
for key, item in dictionary.items():
LOGGER.info(f"Saving key: {key}")
LOGGER.debug(f"Saving key: {key}")

if item is None:
LOGGER.warning(f"Item '{key}' is None. Skipping.")
Expand Down
15 changes: 14 additions & 1 deletion topostats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from argparse import Namespace
from collections import defaultdict
from pathlib import Path
from pprint import pformat

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -105,8 +106,20 @@ def update_plotting_config(plotting_config: dict) -> dict:
main_config = plotting_config.copy()
for opt in ["plot_dict", "run"]:
main_config.pop(opt)
LOGGER.debug(
f"Main plotting options that need updating/adding to plotting dict :\n{pformat(main_config, indent=4)}"
)
for image, options in plotting_config["plot_dict"].items():
plotting_config["plot_dict"][image] = {**options, **main_config}
LOGGER.debug(f"Dictionary for image : {image}")
LOGGER.debug(f"{pformat(options, indent=4)}")
# First update options with values that exist in main_config
plotting_config["plot_dict"][image] = update_config(options, main_config)
LOGGER.debug(f"Updated values :\n{pformat(plotting_config['plot_dict'][image])}")
# Then combine the remaining key/values we need from main_config that don't already exist
for key_main, value_main in main_config.items():
if key_main not in plotting_config["plot_dict"][image]:
plotting_config["plot_dict"][image][key_main] = value_main
LOGGER.debug(f"After adding missing configuration options :\n{pformat(plotting_config['plot_dict'][image])}")
# Make it so that binary images do not have the user-defined z-scale
# applied, but non-binary images do.
if plotting_config["plot_dict"][image]["image_type"] == "binary":
Expand Down

0 comments on commit b9212b8

Please sign in to comment.