|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Tests for meg_utils.plotting — focusing on savefig and vector output. |
| 5 | +""" |
| 6 | +import sys; sys.path.append('../..') |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import tempfile |
| 11 | + |
| 12 | +import matplotlib |
| 13 | +matplotlib.use('Agg') |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import pytest |
| 16 | +from PIL import Image |
| 17 | + |
| 18 | +from meg_utils.plotting import savefig |
| 19 | + |
| 20 | + |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +# Helpers |
| 23 | +# --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +def _simple_fig(): |
| 26 | + fig, ax = plt.subplots() |
| 27 | + ax.plot([1, 2, 3], [1, 4, 9]) |
| 28 | + return fig |
| 29 | + |
| 30 | + |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# savefig — basic file creation |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | + |
| 35 | +class TestSavefig: |
| 36 | + |
| 37 | + def test_saves_png(self, tmp_path): |
| 38 | + fig = _simple_fig() |
| 39 | + out = str(tmp_path / 'plot.png') |
| 40 | + savefig(fig, out, metadata=False) |
| 41 | + assert os.path.exists(out) |
| 42 | + plt.close('all') |
| 43 | + |
| 44 | + def test_default_extension_added(self, tmp_path): |
| 45 | + fig = _simple_fig() |
| 46 | + out = str(tmp_path / 'plot') |
| 47 | + savefig(fig, out, metadata=False) |
| 48 | + assert os.path.exists(out + '.png') |
| 49 | + plt.close('all') |
| 50 | + |
| 51 | + def test_saves_jpg(self, tmp_path): |
| 52 | + fig = _simple_fig() |
| 53 | + out = str(tmp_path / 'plot.jpg') |
| 54 | + savefig(fig, out, metadata=False) |
| 55 | + assert os.path.exists(out) |
| 56 | + plt.close('all') |
| 57 | + |
| 58 | + def test_saves_svg(self, tmp_path): |
| 59 | + fig = _simple_fig() |
| 60 | + out = str(tmp_path / 'plot.svg') |
| 61 | + savefig(fig, out, metadata=False) |
| 62 | + assert os.path.exists(out) |
| 63 | + plt.close('all') |
| 64 | + |
| 65 | + def test_creates_output_directory(self, tmp_path): |
| 66 | + fig = _simple_fig() |
| 67 | + out = str(tmp_path / 'subdir' / 'deep' / 'plot.png') |
| 68 | + savefig(fig, out, metadata=False) |
| 69 | + assert os.path.exists(out) |
| 70 | + plt.close('all') |
| 71 | + |
| 72 | + |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | +# savefig — vector output (save_vector=True) |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | + |
| 77 | +class TestSavefigVector: |
| 78 | + |
| 79 | + def test_vector_creates_vectors_subdir(self, tmp_path): |
| 80 | + fig = _simple_fig() |
| 81 | + out = str(tmp_path / 'plot.png') |
| 82 | + savefig(fig, out, save_vector=True, metadata=False) |
| 83 | + assert os.path.isdir(str(tmp_path / 'vectors')) |
| 84 | + plt.close('all') |
| 85 | + |
| 86 | + def test_vector_creates_svg(self, tmp_path): |
| 87 | + fig = _simple_fig() |
| 88 | + out = str(tmp_path / 'plot.png') |
| 89 | + savefig(fig, out, save_vector=True, metadata=False) |
| 90 | + assert os.path.exists(str(tmp_path / 'vectors' / 'plot.svg')) |
| 91 | + plt.close('all') |
| 92 | + |
| 93 | + def test_vector_creates_eps(self, tmp_path): |
| 94 | + fig = _simple_fig() |
| 95 | + out = str(tmp_path / 'plot.png') |
| 96 | + savefig(fig, out, save_vector=True, metadata=False) |
| 97 | + assert os.path.exists(str(tmp_path / 'vectors' / 'plot.eps')) |
| 98 | + plt.close('all') |
| 99 | + |
| 100 | + def test_vector_disabled(self, tmp_path): |
| 101 | + fig = _simple_fig() |
| 102 | + out = str(tmp_path / 'plot.png') |
| 103 | + savefig(fig, out, save_vector=False, metadata=False) |
| 104 | + assert not os.path.isdir(str(tmp_path / 'vectors')) |
| 105 | + plt.close('all') |
| 106 | + |
| 107 | + def test_vector_basename_matches_source(self, tmp_path): |
| 108 | + fig = _simple_fig() |
| 109 | + out = str(tmp_path / 'my_figure.png') |
| 110 | + savefig(fig, out, save_vector=True, metadata=False) |
| 111 | + assert os.path.exists(str(tmp_path / 'vectors' / 'my_figure.svg')) |
| 112 | + assert os.path.exists(str(tmp_path / 'vectors' / 'my_figure.eps')) |
| 113 | + plt.close('all') |
| 114 | + |
| 115 | + def test_vector_dpi_kwarg_excluded(self, tmp_path): |
| 116 | + """dpi should not be passed to vector formats (no error raised).""" |
| 117 | + fig = _simple_fig() |
| 118 | + out = str(tmp_path / 'plot.png') |
| 119 | + savefig(fig, out, save_vector=True, metadata=False, dpi=300) |
| 120 | + assert os.path.exists(str(tmp_path / 'vectors' / 'plot.svg')) |
| 121 | + plt.close('all') |
| 122 | + |
| 123 | + def test_vector_metadata_json_written(self, tmp_path): |
| 124 | + fig = _simple_fig() |
| 125 | + out = str(tmp_path / 'plot.png') |
| 126 | + savefig(fig, out, save_vector=True, metadata={'key': 'value'}) |
| 127 | + json_file = str(tmp_path / 'vectors' / 'plot.json') |
| 128 | + assert os.path.exists(json_file) |
| 129 | + with open(json_file) as f: |
| 130 | + data = json.load(f) |
| 131 | + assert data['key'] == 'value' |
| 132 | + plt.close('all') |
| 133 | + |
| 134 | + def test_vector_no_json_when_metadata_false(self, tmp_path): |
| 135 | + fig = _simple_fig() |
| 136 | + out = str(tmp_path / 'plot.png') |
| 137 | + savefig(fig, out, save_vector=True, metadata=False) |
| 138 | + json_file = str(tmp_path / 'vectors' / 'plot.json') |
| 139 | + assert not os.path.exists(json_file) |
| 140 | + plt.close('all') |
| 141 | + |
| 142 | + def test_vector_svg_for_svg_source(self, tmp_path): |
| 143 | + """When saving an SVG directly, vectors/ still gets SVG and EPS copies.""" |
| 144 | + fig = _simple_fig() |
| 145 | + out = str(tmp_path / 'plot.svg') |
| 146 | + savefig(fig, out, save_vector=True, metadata=False) |
| 147 | + assert os.path.exists(str(tmp_path / 'vectors' / 'plot.svg')) |
| 148 | + assert os.path.exists(str(tmp_path / 'vectors' / 'plot.eps')) |
| 149 | + plt.close('all') |
| 150 | + |
| 151 | + |
| 152 | +# --------------------------------------------------------------------------- |
| 153 | +# savefig — metadata embedding |
| 154 | +# --------------------------------------------------------------------------- |
| 155 | + |
| 156 | +class TestSavefigMetadata: |
| 157 | + |
| 158 | + def test_png_metadata_dict(self, tmp_path): |
| 159 | + fig = _simple_fig() |
| 160 | + out = str(tmp_path / 'plot.png') |
| 161 | + savefig(fig, out, metadata={'author': 'test', 'experiment': 'MEG'}) |
| 162 | + img = Image.open(out) |
| 163 | + assert img.text.get('author') == 'test' |
| 164 | + assert img.text.get('experiment') == 'MEG' |
| 165 | + img.close() |
| 166 | + plt.close('all') |
| 167 | + |
| 168 | + def test_png_metadata_string(self, tmp_path): |
| 169 | + fig = _simple_fig() |
| 170 | + out = str(tmp_path / 'plot.png') |
| 171 | + savefig(fig, out, metadata='my note') |
| 172 | + img = Image.open(out) |
| 173 | + assert img.text.get('metadata') == 'my note' |
| 174 | + img.close() |
| 175 | + plt.close('all') |
| 176 | + |
| 177 | + def test_png_metadata_false_no_chunks(self, tmp_path): |
| 178 | + fig = _simple_fig() |
| 179 | + out = str(tmp_path / 'plot.png') |
| 180 | + savefig(fig, out, metadata=False) |
| 181 | + img = Image.open(out) |
| 182 | + # No custom text chunks should be present |
| 183 | + assert 'author' not in img.text |
| 184 | + img.close() |
| 185 | + plt.close('all') |
| 186 | + |
| 187 | + def test_png_metadata_none_auto(self, tmp_path): |
| 188 | + """metadata=None should auto-generate at least script_path.""" |
| 189 | + fig = _simple_fig() |
| 190 | + out = str(tmp_path / 'plot.png') |
| 191 | + savefig(fig, out, save_vector=False, metadata=None) |
| 192 | + img = Image.open(out) |
| 193 | + assert 'script_path' in img.text |
| 194 | + img.close() |
| 195 | + plt.close('all') |
0 commit comments