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

pints issue 783 plot histogram #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
70 changes: 70 additions & 0 deletions pfunk/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,76 @@
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)
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:
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

n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7,
label='All %s commits' % len(u))

if len(last_commit) > 25:
plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5,
label='Last commit')
else:
stem_height = [np.max(n)] * len(last_commit)
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
fig.autofmt_xdate()
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:
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

n, _, _ = plt.hist(y[mask_lc], bins='auto', color='#607c8e', alpha=0.7,
label='Last %s commits' % n_commits)

if len(last_commit) > 10:
plt.hist(y[last_commit], bins='auto', color='#0504aa', alpha=0.5,
label='Last commit')
else:
stem_height = [np.max(n)] * len(last_commit)
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


def variable(results, variable, title, ylabel, threshold=None):
"""
Creates and returns a default plot for a variable vs commits.
Expand Down
16 changes: 16 additions & 0 deletions pfunk/tests/mcmc_banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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