Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mpl 1.2 rc2 #109

Merged
merged 22 commits into from
Sep 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions lib/iris/palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,30 @@ def __init__(self, pivot, *args, **kwargs):
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.pivot)

def _update(self, val, update_min=True, update_max=True):
# Update both _vmin and _vmax from given value.
val_diff = numpy.abs(val - self.pivot)
vmin_diff = numpy.abs(self._vmin - self.pivot) if self._vmin else 0.0
vmax_diff = numpy.abs(self._vmax - self.pivot) if self._vmax else 0.0
diff = max(val_diff, vmin_diff, vmax_diff)

if update_min:
self._vmin = self.pivot - diff
if update_max:
self._vmax = self.pivot + diff

def _get_vmin(self):
return getattr(self, '_vmin')

def _set_vmin(self, val):
if val is None:
self._vmin = None
elif self._vmax is None:
# Don't set _vmax, it'll stop matplotlib from giving us one.
self._update(val, update_max=False)
else:
vmax = self._vmax or self.pivot

if numpy.abs(vmax - self.pivot) > numpy.abs(val - self.pivot):
if vmax == self.pivot:
self._vmin = self.pivot
else:
self._vmin = self.pivot - numpy.abs(vmax - self.pivot)
self._vmax = vmax
else:
self._vmin = val
self._vmax = self.pivot + numpy.abs(val - self.pivot)
# Set both _vmin and _vmax from value
self._update(val)

vmin = property(_get_vmin, _set_vmin)

Expand All @@ -202,18 +208,12 @@ def _get_vmax(self):
def _set_vmax(self, val):
if val is None:
self._vmax = None
elif self._vmin is None:
# Don't set _vmin, it'll stop matplotlib from giving us one.
self._update(val, update_min=False)
else:
vmin = self._vmin or self.pivot

if numpy.abs(vmin - self.pivot) > numpy.abs(val - self.pivot):
if vmin == self.pivot:
self._vmax = self.pivot
else:
self._vmax = self.pivot + numpy.abs(vmin - self.pivot)
self._vmin = vmin
else:
self._vmin = self.pivot - numpy.abs(val - self.pivot)
self._vmax = val
# Set both _vmin and _vmax from value
self._update(val)

vmax = property(_get_vmax, _set_vmax)

Expand Down
5 changes: 4 additions & 1 deletion lib/iris/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,10 @@ def outline(cube, coords=None):
the plot and the second element is the vertical axis of the plot.

"""
return _draw_2d_from_bounds('pcolormesh', cube, facecolors='none', edgecolors='k', antialiased=True, coords=coords)
result = _draw_2d_from_bounds('pcolormesh', cube, facecolors='none', edgecolors='k', antialiased=True, coords=coords)
# set the _is_stroked property to get a single color grid. See https://github.com/matplotlib/matplotlib/issues/1302
result._is_stroked = False
return result


@iris.palette.auto_palette
Expand Down
26 changes: 20 additions & 6 deletions lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
import zlib

import matplotlib
matplotlib.use('agg')

import matplotlib.testing.compare as mcompare
import matplotlib.pyplot as plt

import numpy

import iris.cube
Expand All @@ -57,7 +54,7 @@
import iris.util


_RESULT_PATH = os.path.join(iris.config.ROOT_PATH, 'tests', 'results')
_RESULT_PATH = os.path.join(os.path.dirname(__file__), 'results')
"""Basepath for test results."""


Expand All @@ -73,6 +70,15 @@
logger = logging.getLogger('tests')


# Whether to display matplotlib output to the screen.
_DISPLAY_FIGURES = False

if '-d' in sys.argv:
sys.argv.remove('-d')
plt.switch_backend('tkagg')
_DISPLAY_FIGURES = True


def main():
"""A wrapper for unittest.main() which adds iris.test specific options to the help (-h) output."""
if '-h' in sys.argv or '--help' in sys.argv:
Expand All @@ -86,6 +92,9 @@ def main():
sys.stdout = stdout
lines = buff.getvalue().split('\n')
lines.insert(9, 'Iris-specific options:')
lines.insert(10, ' -d Display matplotlib figures (uses tkagg).')
lines.insert(11, ' NOTE: To compare results of failing tests, ')
lines.insert(12, ' use idiff.py instead')
lines.insert(13, ' --data-files-used Save a list of files used to a temporary file')
print '\n'.join(lines)
else:
Expand Down Expand Up @@ -155,7 +164,7 @@ def assertCDL(self, netcdf_filename, reference_filename):

# Ingest the CDL for comparison, excluding first line.
with open(cdl_filename, 'r') as cdl_file:
cdl = ''.join(cdl_file.readlines()[1:])
cdl = ''.join(cdl_file.readlines()[1:])

os.remove(cdl_filename)
reference_path = get_result_path(reference_filename)
Expand Down Expand Up @@ -355,7 +364,12 @@ def check_graphic(self, tol=0):

err = mcompare.compare_images(expected_fname, result_fname, tol=tol)

assert not err, 'Image comparison failed. Message: %s' % err
if _DISPLAY_FIGURES:
if err:
print 'Image comparison would have failed. Message: %s' % err
plt.show()
else:
assert not err, 'Image comparison failed. Message: %s' % err

finally:
plt.close()
Expand Down
139 changes: 47 additions & 92 deletions lib/iris/tests/idiff.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,110 +22,65 @@

"""

import multiprocessing
import optparse
import os
import os.path
import shutil

import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.image as mimg
import matplotlib.widgets as mwidget


DIFF_DIR = 'diff_images'


class Difference(object):
def __init__(self, name, message):
self.name = name
self.message = message

def __str__(self):
return "%s\n %s" % (self.name, self.message)


def compare_files(dir_path1, dir_path2, name):
result = None
def diff_viewer(expected_fname, result_fname, diff_fname):

path1 = os.path.join(dir_path1, name)
path2 = os.path.join(dir_path2, name)
plt.figure(figsize=(16, 16))
plt.suptitle(os.path.basename(expected_fname))
ax = plt.subplot(221)
ax.imshow(mimg.imread(expected_fname))
ax = plt.subplot(222, sharex=ax, sharey=ax)
ax.imshow(mimg.imread(result_fname))
ax = plt.subplot(223, sharex=ax, sharey=ax)
ax.imshow(mimg.imread(diff_fname))

if not os.path.isfile(path1) or not os.path.isfile(path2):
result = Difference(name, 'missing file')
else:
image1 = plt.imread(path1)
image2 = plt.imread(path2)
if image1.shape != image2.shape:
result = Difference(name, "shape: %s -> %s" % (image1.shape, image2.shape))
else:
diff = (image1 != image2).any(axis=2)
if diff.any():
diff_path = os.path.join(DIFF_DIR, name)
diff_path = os.path.realpath(diff_path)
plt.figure(figsize=reversed(diff.shape), dpi=1)
plt.figimage(diff, cmap=cm.gray)
plt.savefig(diff_path, dpi=1)
result = Difference(name, "diff: %s" % diff_path)
return result


def map_compare_files(args):
return compare_files(*args)

def accept(event):
# removes the expected result, and move the most recent result in
print 'ACCEPTED NEW FILE: %s' % (os.path.basename(expected_fname), )
os.remove(expected_fname)
shutil.copy2(result_fname, expected_fname)
os.remove(diff_fname)
plt.close()

def reject(event):
print 'REJECTED: %s' % (os.path.basename(expected_fname), )
plt.close()

ax_accept = plt.axes([0.7, 0.05, 0.1, 0.075])
ax_reject = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = mwidget.Button(ax_accept, 'Accept change')
bnext.on_clicked(accept)
bprev = mwidget.Button(ax_reject, 'Reject')
bprev.on_clicked(reject)

plt.show()

def compare_dirs(dir_path1, dir_path2):
if not os.path.isdir(dir_path1) or not os.path.isdir(dir_path2):
raise ValueError('Can only compare directories')

# Prepare the results directory
if os.path.isdir(DIFF_DIR):
shutil.rmtree(DIFF_DIR)
os.makedirs(DIFF_DIR)

pool = multiprocessing.Pool()
names = set(name for name in os.listdir(dir_path1))
names = names.union(name for name in os.listdir(dir_path2))
args = [(dir_path1, dir_path2, name) for name in names if name.endswith('.png')]
diffs = pool.map(map_compare_files, args)
for diff in diffs:
if diff is not None:
print diff
pass
pool.close()
pool.join()
def step_over_diffs():
import iris.tests
image_dir = os.path.join(os.path.dirname(iris.tests.__file__),
'results', 'visual_tests',
)
diff_dir = os.path.join(os.path.dirname(iris.tests.__file__),
'result_image_comparison',
)

def step_over_diffs(d_path1, d_path2):
import matplotlib.pyplot as plt
import matplotlib.image as mimg
for expected_fname in os.listdir(image_dir):
expected_fname = os.path.join(image_dir, expected_fname)
result_fname = os.path.join(diff_dir, 'result-' + os.path.basename(expected_fname))
diff_fname = result_fname[:-4] + '-failed-diff.png'

for fname in os.listdir(DIFF_DIR):
print fname
plt.figure(figsize=(16, 16))
plt.suptitle(fname)
ax = plt.subplot(221)
plt.imshow(mimg.imread(os.path.join(d_path2, fname)))
ax = plt.subplot(222, sharex=ax, sharey=ax)
plt.imshow(mimg.imread(os.path.join(d_path1, fname)))
ax = plt.subplot(223, sharex=ax, sharey=ax)
plt.imshow(mimg.imread(os.path.join(DIFF_DIR, fname)))
plt.show()
# if the test failed, there will be a diff file
if os.path.exists(diff_fname):
diff_viewer(expected_fname, result_fname, diff_fname)


if __name__ == '__main__':
usage = "usage: %prog [options] <dir1> <dir2>"
description = "Compare directories of PNG images, producing black and white mask images of the differences." \
" Designed to compare image output from different branches, created with 'python test_thing.py -sf'." \
" Example: python idiff.py <trunk>/lib/iris_tests/image_results <branch>/lib/iris_tests/image_results"
parser = optparse.OptionParser(usage=usage, description=description)
parser.add_option('-o', '--output', dest='output', help='output directory', metavar='DIR')
parser.add_option('-n', dest='compute_diffs', action="store_false", help='Enable flag to disable diff creation, simply do other things instead (such as view pre computed diffs.)')
parser.add_option('-v', dest='view_diffs', action="store_true", help='view diffs')
(options, args) = parser.parse_args()
if len(args) != 2:
parser.error('Incorrect number of arguments')
if options.output:
DIFF_DIR = options.output
if options.compute_diffs != False:
compare_dirs(*args)
if options.view_diffs:
step_over_diffs(*args)
step_over_diffs()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions lib/iris/tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_pcolormesh(self):
self.check_graphic()

def test_grid(self):
iplt.pcolormesh(self.cube, facecolor='none', edgecolors='#888888')
iplt.outline(self.cube)
self.check_graphic()


Expand All @@ -202,7 +202,9 @@ def test_pcolormesh(self):
self.check_graphic()

def test_grid(self):
iplt.pcolormesh(self.cube, facecolor='none', edgecolors='#888888')
iplt.pcolormesh(self.cube, facecolors='none', edgecolors='blue')
# the result is a graphic which has coloured edges. This is a mpl bug, see
# https://github.com/matplotlib/matplotlib/issues/1302
self.check_graphic()

def test_outline(self):
Expand Down