Reputation: 314
I'm making a simple Django site that graphs a specified data set with matplotlib and displays the resulting graph. I've saved a static copy of the correct graph, and I want to compare this against the image displayed on the page.
home.html
<img id="id_graph_image" src="/graphs/selected_graph.png">
graphs/urls.py
urlpatterns = patterns('',
# other patterns...
url(r'^graphs/selected_graph.png$', 'graphs.views.show_selected_graph'),
)
graphs/views.py
def show_selected_graph(request):
# matplotlib code...
canvas = FigureCanvasAgg(figure)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
graphs/tests.py
def test_selected_graph_shows_the_right_graphs(self):
# request and response...
graph_on_page = Image.open(StringIO(response.content)).
expected_graph = Image.open('file://../graphs/static/scenario_1_statistic_1.png')
# TODO: compare graphs
I'd like to add a Selenium test or a unit test to confirm that the graph view returns the correct image for a given data set. I've tried comparing the two images with PIL and the RMS Difference of the histograms, but any two matplotlib graph images have similar histograms.
Is a "percentage difference" simple? Should I test this in a very different way?
I appreciate any and all help, thanks!
Upvotes: 4
Views: 477
Reputation: 6468
If you're dead set on doing image comparisons, you could try using opencv (see e.g. http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html). This, however, feels hokey to me (and may slow your testing down a bit although if you're using selenium, you're probably already going slower than what I shoot for in unit tests). An alternate approach might be to convert the data you're passing to the drawGraph portion into an array and compare that with the expected values (as it looks like your graph contains about 100 x,y pairs which is a lot smaller data than 100 * 100 pixels), but I'm not quite clear if your intention is to test that the matploblib code is generating the right data or that it's then transforming the data correctly into a graph.
Upvotes: 1