Commit ea37e807 authored by Daniel Boeckenhoff's avatar Daniel Boeckenhoff
Browse files

fixed test routine

parent 5750f527
......@@ -11,9 +11,10 @@ def run_doctests():
"""
Find all doctests and execute them
"""
this_dir = pathlib.Path(__file__).resolve()
this_dir = pathlib.Path(__file__).parent.resolve()
for f in list(this_dir.glob('**/*.py')):
doctest.testfile(str(f.relative_to(pathlib.Path.cwd().resolve()))) # , verbose=True, optionflags=doctest.ELLIPSIS)
doctest.testfile(str(f.resolve()),
module_relative=False) # , verbose=True, optionflags=doctest.ELLIPSIS)
def load_unittests(loader=None, suite=None):
......@@ -30,11 +31,11 @@ def load_unittests(loader=None, suite=None):
sys.path.remove(str(f.parent))
return suite
def run_unittests():
unittest.main(defaultTest='load_unittests')
if __name__ == '__main__': # pragma: no cover
if len(sys.argv) > 1:
arg = sys.argv.pop(1)
......
......@@ -16,6 +16,10 @@ from itertools import cycle
import logging
def show():
plt.show()
def gca(dim=None, **kwargs):
"""
Forwarding to plt.gca but translating the dimension to projection
......@@ -97,11 +101,13 @@ def save(path, *fmts, **kwargs):
# catch figure from axis or fig
axis = kwargs.get('axis', None)
if axis is None:
figDefault = plt.gcf()
fig_default = plt.gcf()
axis = gca()
if fig_default is None:
raise ValueError("fig_default may not be None")
else:
figDefault = axis.figure
fig = kwargs.get('fig', figDefault)
fig_default = axis.figure
fig = kwargs.get('fig', fig_default)
# set current figure
plt.figure(fig.number)
......@@ -109,24 +115,25 @@ def save(path, *fmts, **kwargs):
# crop the plot down based on the extents of the artists in the plot
kwargs['bbox_inches'] = kwargs.pop('bbox_inches', 'tight')
if kwargs['bbox_inches'] == 'tight':
extraArtists = None
extra_artists = None
for ax in fig.get_axes():
firstLabel = ax.get_legend_handles_labels()[0] or None
if firstLabel:
if not extraArtists:
extraArtists = []
extraArtists.append(firstLabel)
kwargs['bbox_extra_artists'] = kwargs.pop('bbox_extra_artists', extraArtists)
first_label = ax.get_legend_handles_labels()[0] or None
if first_label:
if not extra_artists:
extra_artists = []
extra_artists.append(first_label)
kwargs['bbox_extra_artists'] = kwargs.pop('bbox_extra_artists',
extra_artists)
if len(fmts) != 0:
for fmt in fmts:
if path.endswith('.'):
newFilePath = path + fmt
new_file_path = path + fmt
elif '{fmt}' in path:
newFilePath = path.format(**locals())
new_file_path = path.format(**locals())
else:
newFilePath = path + '.' + fmt
save(newFilePath, **kwargs)
new_file_path = path + '.' + fmt
save(new_file_path, **kwargs)
else:
path = tfields.lib.in_out.resolve(path)
log.info("Saving figure as {0}".format(path))
......@@ -568,6 +575,21 @@ def setLegend(axis, artists):
def set_color_bar(axis, artist, label=None, divide=True, **kwargs):
"""
Note:
Bug found in matplotlib:
when calling axis.clear(), the colorbar has to be removed by hand,
since it will not be removed by clear.
>> import tfields
>> axis = tfields.plotting.gca()
>> artist = ...
>> cbar = tfields.plotting.set_color_bar(axis, artist)
>> tfields.plotting.save(...)
>> cbar.remove() # THIS IS IMPORTANT. Otherwise you will have problems
# at the next call of savefig.
>> axis.clear()
"""
# colorbar
if divide:
divider = make_axes_locatable(axis)
......@@ -576,9 +598,9 @@ def set_color_bar(axis, artist, label=None, divide=True, **kwargs):
# label
if label is None:
artLabel = artist.get_label()
if artLabel:
label = artLabel
art_label = artist.get_label()
if art_label:
label = art_label
if label is not None:
labelpad = 30
cbar.set_label(label, rotation=270, labelpad=labelpad)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment