From 33c71a3fec86a73034b8f80eb22fbe37a60e30ba Mon Sep 17 00:00:00 2001 From: Chon Lok Lei Date: Tue, 7 May 2019 17:19:45 +0100 Subject: [PATCH 1/6] Add histogram plots; close pints-team/pints#783 --- pfunk/plot.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/pfunk/plot.py b/pfunk/plot.py index 5333d83..fa24604 100644 --- a/pfunk/plot.py +++ b/pfunk/plot.py @@ -17,6 +17,73 @@ import pfunk +def histogram(results, variable, title, xlabel, threshold=None): + """ + Creates and returns a histogram plot for a variable (over commits). + """ + fig = plt.figure(figsize=(11, 4.5)) + plt.suptitle(title + ' : ' + pfunk.date()) + + # Left plot: Variable per commit for all data as 1 histogram + plt.subplot(1, 2, 1) + plt.ylabel('Frequency') + plt.xlabel(xlabel) + fig.autofmt_xdate() + x, y, u, m, s = pfunk.gather_statistics_per_commit(results, variable) + if len(x) == 0: + plt.text(0.5, 0.5, 'No data') + else: + last_commit = [i for i, k in enumerate(x) if k == u[-1]] + y = np.asarray(y) + mask_lc = np.ones(y.shape, dtype=bool) + mask_lc[last_commit] = False + + n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7) + + if len(last_commit) > 25: + plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5, + label=u[-1]) + else: + stem_height = [np.max(n)] * len(last_commit) + ml, sl, bl = plt.stem(y[last_commit], stem_height, label=u[-1]) + plt.setp(ml, color='#0504aa', alpha=0.5) + plt.setp(sl, color='#0504aa', alpha=0.5) + plt.setp(bl, visible=False) + + + # Right plot: Same, to most recent data. + plt.subplot(1, 2, 2) + plt.ylabel('Frequency') + plt.xlabel(xlabel) + fig.autofmt_xdate() + x, y, u, m, s = pfunk.gather_statistics_per_commit( + results, variable, remove_outliers=False, n=12) + if len(x) == 0: + plt.text(0.5, 0.5, 'No data') + else: + last_commit = [i for i, k in enumerate(x) if k == u[-1]] + y = np.asarray(y) + mask_lc = np.ones(y.shape, dtype=bool) + mask_lc[last_commit] = False + + n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7) + + if len(last_commit) > 10: + plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5, + label=u[-1]) + else: + stem_height = [np.max(n)] * len(last_commit) + ml, sl, bl = plt.stem(y[last_commit], stem_height, label=u[-1]) + plt.setp(ml, color='#0504aa', alpha=0.5) + plt.setp(sl, color='#0504aa', alpha=0.5) + plt.setp(bl, visible=False) + + plt.legend() + + # raise NotImplementedError + return fig + + def variable(results, variable, title, ylabel, threshold=None): """ Creates and returns a default plot for a variable vs commits. From a222725dfab2aa329622ee87b55e1b86ec3205e8 Mon Sep 17 00:00:00 2001 From: Chon Lok Lei Date: Tue, 7 May 2019 17:20:13 +0100 Subject: [PATCH 2/6] Try it on mcmc_banana --- pfunk/tests/mcmc_banana.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pfunk/tests/mcmc_banana.py b/pfunk/tests/mcmc_banana.py index 2fddc11..fc0661b 100644 --- a/pfunk/tests/mcmc_banana.py +++ b/pfunk/tests/mcmc_banana.py @@ -157,6 +157,14 @@ def _plot(self, results): 'Kullback-Leibler divergence', 3 * self._pass_threshold) ) + # Figure: KL histogram + figs.append(pfunk.plot.histogram( + results, + 'kld', + 'Banana w. ' + self._method, + 'Kullback-Leibler divergence') + ) + # Figure: KL over time figs.append(pfunk.plot.convergence( results, @@ -176,4 +184,12 @@ def _plot(self, results): 'Effective sample size') ) + # Figure: ESS histogram + figs.append(pfunk.plot.histogram( + results, + 'ess', + 'Banana w. ' + self._method, + 'Effective sample size') + ) + return figs From 6dadf1282d1ddbeb7e9d3d132a3bc35602604b59 Mon Sep 17 00:00:00 2001 From: Chon Lok Lei Date: Tue, 7 May 2019 17:58:36 +0100 Subject: [PATCH 3/6] Slight style fix --- pfunk/plot.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pfunk/plot.py b/pfunk/plot.py index fa24604..9eae19e 100644 --- a/pfunk/plot.py +++ b/pfunk/plot.py @@ -33,8 +33,8 @@ def histogram(results, variable, title, xlabel, threshold=None): if len(x) == 0: plt.text(0.5, 0.5, 'No data') else: - last_commit = [i for i, k in enumerate(x) if k == u[-1]] y = np.asarray(y) + last_commit = [i for i, k in enumerate(x) if k == u[-1]] mask_lc = np.ones(y.shape, dtype=bool) mask_lc[last_commit] = False @@ -50,19 +50,19 @@ def histogram(results, variable, title, xlabel, threshold=None): plt.setp(sl, color='#0504aa', alpha=0.5) plt.setp(bl, visible=False) - # Right plot: Same, to most recent data. plt.subplot(1, 2, 2) - plt.ylabel('Frequency') + n_commits = 12 + plt.ylabel('Frequency (last %s commits)' % n_commits) plt.xlabel(xlabel) fig.autofmt_xdate() - x, y, u, m, s = pfunk.gather_statistics_per_commit( - results, variable, remove_outliers=False, n=12) + x, y, u, m, s = pfunk.gather_statistics_per_commit(results, variable, + n=n_commits) if len(x) == 0: plt.text(0.5, 0.5, 'No data') else: - last_commit = [i for i, k in enumerate(x) if k == u[-1]] y = np.asarray(y) + last_commit = [i for i, k in enumerate(x) if k == u[-1]] mask_lc = np.ones(y.shape, dtype=bool) mask_lc[last_commit] = False @@ -77,10 +77,8 @@ def histogram(results, variable, title, xlabel, threshold=None): plt.setp(ml, color='#0504aa', alpha=0.5) plt.setp(sl, color='#0504aa', alpha=0.5) plt.setp(bl, visible=False) - plt.legend() - # raise NotImplementedError return fig From 3be839e192f992135867abd588113cf59ddd5208 Mon Sep 17 00:00:00 2001 From: Chon Lok Lei Date: Wed, 8 May 2019 10:52:54 +0100 Subject: [PATCH 4/6] Update legend --- pfunk/plot.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pfunk/plot.py b/pfunk/plot.py index 9eae19e..1ec4973 100644 --- a/pfunk/plot.py +++ b/pfunk/plot.py @@ -38,7 +38,8 @@ def histogram(results, variable, title, xlabel, threshold=None): mask_lc = np.ones(y.shape, dtype=bool) mask_lc[last_commit] = False - n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7) + n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7, + label='All commits') if len(last_commit) > 25: plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5, @@ -49,6 +50,7 @@ def histogram(results, variable, title, xlabel, threshold=None): plt.setp(ml, color='#0504aa', alpha=0.5) plt.setp(sl, color='#0504aa', alpha=0.5) plt.setp(bl, visible=False) + plt.legend() # Right plot: Same, to most recent data. plt.subplot(1, 2, 2) @@ -66,7 +68,11 @@ def histogram(results, variable, title, xlabel, threshold=None): mask_lc = np.ones(y.shape, dtype=bool) mask_lc[last_commit] = False - n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7) + legend_hist = u[0].split('\n')[0] + ' to ' + u[-2].split('\n')[0] \ + + '\n' \ + + u[0].split('\n')[1] + ' ' + u[-2].split('\n')[1] + n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7, + label=legend_hist) if len(last_commit) > 10: plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5, From e55fd29fcbabda61ffd79fa5d9e78b849d3480a5 Mon Sep 17 00:00:00 2001 From: Chon Lok Lei Date: Wed, 8 May 2019 14:33:20 +0100 Subject: [PATCH 5/6] Update legend --- pfunk/plot.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/pfunk/plot.py b/pfunk/plot.py index 1ec4973..928b03b 100644 --- a/pfunk/plot.py +++ b/pfunk/plot.py @@ -26,8 +26,6 @@ def histogram(results, variable, title, xlabel, threshold=None): # Left plot: Variable per commit for all data as 1 histogram plt.subplot(1, 2, 1) - plt.ylabel('Frequency') - plt.xlabel(xlabel) fig.autofmt_xdate() x, y, u, m, s = pfunk.gather_statistics_per_commit(results, variable) if len(x) == 0: @@ -39,24 +37,25 @@ def histogram(results, variable, title, xlabel, threshold=None): mask_lc[last_commit] = False n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7, - label='All commits') + label='All %s commits' % len(u)) if len(last_commit) > 25: plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5, - label=u[-1]) + label='Last commit') else: stem_height = [np.max(n)] * len(last_commit) - ml, sl, bl = plt.stem(y[last_commit], stem_height, label=u[-1]) + ml, sl, bl = plt.stem(y[last_commit], stem_height, + label='Last commit') plt.setp(ml, color='#0504aa', alpha=0.5) plt.setp(sl, color='#0504aa', alpha=0.5) plt.setp(bl, visible=False) + plt.ylabel('Frequency (total %s runs)' % len(y)) + plt.xlabel(xlabel) plt.legend() # Right plot: Same, to most recent data. plt.subplot(1, 2, 2) n_commits = 12 - plt.ylabel('Frequency (last %s commits)' % n_commits) - plt.xlabel(xlabel) fig.autofmt_xdate() x, y, u, m, s = pfunk.gather_statistics_per_commit(results, variable, n=n_commits) @@ -68,21 +67,21 @@ def histogram(results, variable, title, xlabel, threshold=None): mask_lc = np.ones(y.shape, dtype=bool) mask_lc[last_commit] = False - legend_hist = u[0].split('\n')[0] + ' to ' + u[-2].split('\n')[0] \ - + '\n' \ - + u[0].split('\n')[1] + ' ' + u[-2].split('\n')[1] n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7, - label=legend_hist) + label='Last %s commits' % n_commits) if len(last_commit) > 10: plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5, - label=u[-1]) + label='Last commit') else: stem_height = [np.max(n)] * len(last_commit) - ml, sl, bl = plt.stem(y[last_commit], stem_height, label=u[-1]) + ml, sl, bl = plt.stem(y[last_commit], stem_height, + label='Last commit') plt.setp(ml, color='#0504aa', alpha=0.5) plt.setp(sl, color='#0504aa', alpha=0.5) plt.setp(bl, visible=False) + plt.ylabel('Frequency (total %s runs)' % len(y)) + plt.xlabel(xlabel) plt.legend() return fig From c8e05ce88ccbc72b8899bb52b30c0157b730473d Mon Sep 17 00:00:00 2001 From: Chon Lok Lei Date: Wed, 8 May 2019 14:34:18 +0100 Subject: [PATCH 6/6] Add installation detail --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6e8d838..da3fbaf 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,12 @@ Some details: ### Creating plots, running analysis -- Use `./funk plot test_name` to run a plot, or `./funk plot --all` to run all plots -- Use `./funk analyse test_name` to check if a test passed or failed, or `./funk analyse --all` to check all tests +- Use `./funk plot test_name` to run a plot, or `./funk plot --all` to run all plots. +- Use `./funk analyse test_name` to check if a test passed or failed, or `./funk analyse --all` to check all tests. ### Installing -- Functional testing requires Python 3.4 or later -- When cloning, make sure to add the `--recursive` switch +- Functional testing requires Python 3.4 or later. +- When cloning, make sure to add the `--recursive` switch; `git reset --hard` within each submodule might be needed to get it working. - To install, use `python3 -m pip install -r requirements.txt`. This makes sure you have all the dependencies you know.