Skip to content
Snippets Groups Projects
Commit 24cbed00 authored by Amandine  PERRIN's avatar Amandine PERRIN
Browse files

Changed way to compare matplotlib results

Before, matplotlib png outputs were compared bit by bit. But, if the version of matplotlib changes, there can be some very small differences. So, now, we compare using pytest-mpl module, tolerating a given RMS on the difference between the expected and output images
parent f7a0e33f
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -213,7 +213,12 @@ def plot_distributions(genomes, res_path, listfile_base, l90, nbconts):
outl90 = os.path.join(res_path, "QC_L90-" + listfile_base + ".png")
nbcont_vals = [val for _, (_, _, _, val, _) in genomes.items()]
outnbcont = os.path.join(res_path, "QC_nb-contigs-" + listfile_base + ".png")
utils.plot_distr(L90_vals, l90, outl90, "L90 distribution for all genomes",
dist1 = utils.plot_distr(L90_vals, l90, "L90 distribution for all genomes",
"max L90 =")
utils.plot_distr(nbcont_vals, nbconts, outnbcont,
"Distribution of number of contigs among all genomes", "max #contigs =")
dist2 = utils.plot_distr(nbcont_vals, nbconts,
"Distribution of number of contigs among all genomes",
"max #contigs =")
dist1.savefig(outl90)
dist2.savefig(outnbcont)
return L90_vals, nbcont_vals, dist1, dist2
......@@ -208,7 +208,7 @@ def run_cmd(cmd, error, eof=False, **kwargs):
return call
def plot_distr(values, limit, outfile, title, text):
def plot_distr(values, limit, title, text):
""" Plot histogram of given 'values', and add a vertical line corresponding to the chosen
'limit' and saves the image into the 'outfile'
......@@ -224,7 +224,9 @@ def plot_distr(values, limit, outfile, title, text):
import matplotlib
matplotlib.use('AGG')
from matplotlib import pyplot as plt
plt.figure(figsize=(10,7))
plt.close("all")
fig = plt.figure(figsize=(10,7))
ax = fig.add_subplot(1,1,1)
max_x = max(values)
# if too many values, group them to have less bins in the histogram.
# Put 'group_values' values in each bin ->
......@@ -233,14 +235,13 @@ def plot_distr(values, limit, outfile, title, text):
dec_ax = math.exp(0.001 * max_x) - 1
dec_text = 3 * dec_ax
bins = np.arange(0, max_x + 2*group_values, group_values) - 0.5
axes = plt.hist(values, bins = bins, edgecolor="black", color="blue")
plt.xlim(0.5, max_x + 0.5*group_values)
plt.axvline(x=limit + 0.5*group_values + dec_ax, color="r")
plt.text(x=limit + 0.5*group_values + dec_text, y=plt.ylim()[1]/2,
axes = ax.hist(values, bins = bins, edgecolor="black", color="blue")
ax.set_xlim(0.5, max_x + 0.5*group_values)
ax.axvline(x=limit + 0.5*group_values + dec_ax, color="r")
ax.text(x=limit + 0.5*group_values + dec_text, y=plt.ylim()[1]/2,
s=text + " " + str(limit), color="r", rotation=90)
plt.title(title)
plt.savefig(outfile)
plt.close("all")
ax.set_title(title)
return fig
def write_warning_skipped(skipped, format=False):
......
argparse
pytest>=2.5.2
pytest-cov>=1.6
pytest-mpl>=0.8
pytest-catchlog>=1.2.2
coverage>=4
progressbar2 >= 3.18.0
......
[test]
local_freetype = True
tests = True
[pep8]
max-line-length = 100
[tool:pytest]
addopts = --junitxml=junit_tests.xml --cov-report=html --cov-report=xml --cov genomeAPCAT
addopts = --junitxml=junit_tests.xml --cov-report=html --cov-report=xml --cov genomeAPCAT --mpl
# add option if coverage must be added to previous one --cov-append
# add option to get report in terminal --cov-report=term
norecursedirs = *.egg* bin lib
......@@ -16,3 +20,4 @@ exclude_lines =
[build_sphinx]
source-dir = doc_sources/
build-dir = doc/
test/data/annotate/exp_files/baseline/test_dist_l90.png

17.9 KiB

test/data/annotate/exp_files/baseline/test_dist_nbcont.png

22.8 KiB

test/data/annotate/exp_files/baseline/test_plot_dist.png

14.4 KiB

test/data/annotate/exp_files/res_QC_L90-test_plot_dist.png

19.4 KiB

test/data/annotate/exp_files/res_plot_distr.png

12.5 KiB

......@@ -16,6 +16,11 @@ def dbpath():
return os.path.join("test", "data", "annotate", "genomes")
@pytest.fixture(scope="module")
def baseline_dir():
return os.path.join("..", "data", "annotate", "exp_files", "baseline")
def test_sort_genomes():
"""
Test the function sorting genomes by L90 and nb contigs.
......@@ -393,11 +398,13 @@ def test_analyseAllGenomes_cut_empty(dbpath):
os.remove(os.path.join(dbpath, gs[3]))
def test_plot_dist():
def get_plot_distribs():
"""
For all genomes, plot the distribution of their L90 values, and their number of contigs.
Add a vertical line at the given threshold.
genomes: {genome: [name, path, size, nbcont, l90]}
output of plot_distributions is L90_vals, nbcont_vals, l90_dist, nbcont_dist
these outputs will be compared to expected results in tests
"""
genomes_dir = os.path.join("test", "data", "annotate", "genomes")
gs = ["genome1.fasta", "genome2.fasta", "genome3.fasta", "genome4.fasta",
......@@ -410,16 +417,52 @@ def test_plot_dist():
gs[5]: ["ESCO.0216", os.path.join(genomes_dir, gs[5]), 116, 60, 50],
gs[6]: ["SAEN.1115", os.path.join(genomes_dir, gs[6]), 137, 20, 12]}
res_path = os.path.join("test", "data", "annotate")
exp_path = os.path.join(res_path, "exp_files")
listfile_base = "test_plot_dist"
l90 = 13
nbconts = 19
gfunc.plot_distributions(genomes, res_path, listfile_base, l90, nbconts)
outfiles = [os.path.join(res_path, "QC_L90-" + listfile_base + ".png"),
os.path.join(res_path, "QC_nb-contigs-" + listfile_base + ".png")]
expfiles = [os.path.join(exp_path, "res_QC_L90-" + listfile_base + ".png"),
os.path.join(exp_path, "res_QC_nb-contigs-" + listfile_base + ".png")]
for out, exp in zip(outfiles, expfiles):
assert util_tests.compare_files_bin(out, exp)
os.remove(out)
outdist = gfunc.plot_distributions(genomes, res_path, listfile_base, l90, nbconts)
return outdist
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir(), tolerance=6)
def test_dist_l90():
"""
For created L90 graph, check that calculated L90 values are as expected,
and graph is also as expected
"""
res_path = os.path.join("test", "data", "annotate")
listfile_base = "test_plot_dist"
outfile1 = os.path.join(res_path, "QC_L90-" + listfile_base + ".png")
outfile2 = os.path.join(res_path, "QC_nb-contigs-" + listfile_base + ".png")
l90, _, dist, _ = get_plot_distribs()
# Check that png file was created
assert os.path.isfile(outfile1)
assert os.path.isfile(outfile2)
os.remove(outfile1)
os.remove(outfile2)
# Check values calculated for l90
assert l90 == [2, 13, 11, 11, 12, 50 , 12]
# Check that output plot is as expected
return dist
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir(), tolerance=6)
def test_dist_nbcont():
"""
For created L90 graph, check that calculated L90 values are as expected,
and graph is also as expected
"""
res_path = os.path.join("test", "data", "annotate")
listfile_base = "test_plot_dist"
outfile1 = os.path.join(res_path, "QC_L90-" + listfile_base + ".png")
outfile2 = os.path.join(res_path, "QC_nb-contigs-" + listfile_base + ".png")
_, nbcont, _, dist = get_plot_distribs()
# Check that png file was created
assert os.path.isfile(outfile1)
assert os.path.isfile(outfile2)
os.remove(outfile1)
os.remove(outfile2)
# Check values calculated for l90
assert nbcont == [2, 15, 15, 17, 17, 60 ,20]
# Check that output plot is as expected
return dist
......@@ -13,6 +13,10 @@ import shutil
import test.test_unit.utilities_for_tests as util_tests
def baseline_dir():
return os.path.join("..", "data", "annotate", "exp_files", "baseline")
def test_check_install():
"""
Try to run prokka, which is installed, and check that there is no problem
......@@ -28,6 +32,7 @@ def test_check_install_error():
assert not utils.check_installed("plop false command...")
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir(), tolerance=5)
def test_plot_dist():
"""
Plot a given distribution, and check that output is as expected
......@@ -36,13 +41,11 @@ def test_plot_dist():
limit = 3
res_dir = os.path.join("test", "data", "annotate")
os.makedirs(res_dir, exist_ok=True)
outfile = os.path.join(res_dir, "distrib.png")
reffile = os.path.join("test", "data", "annotate", "exp_files", "res_plot_distr.png")
# reffile = os.path.join("test", "data", "annotate", "exp_files", "res_plot_distr.png")
title = "Distribution test"
text = "Max L90 ="
utils.plot_distr(values, limit, outfile, title, text)
assert util_tests.compare_files_bin(outfile, reffile)
os.remove(outfile)
myfig = utils.plot_distr(values, limit, title, text)
return myfig
def test_skipped_prokka(capsys):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment