From 220a58bd22cde2abd035c18e385182ee761a027e Mon Sep 17 00:00:00 2001 From: Jorg Bornschein Date: Sun, 18 Oct 2015 22:55:51 -0400 Subject: [PATCH 1/4] Import correct plot.py from blocks.extras; not stale one from blocks. --- bin/blocks-plot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/blocks-plot b/bin/blocks-plot index 9f4331f..904d1f2 100755 --- a/bin/blocks-plot +++ b/bin/blocks-plot @@ -41,7 +41,7 @@ def plot_dataframe(dataframe): def main(args): import readline - import blocks.scripts.plot as plot + import blocks.extras.scripts.plot as plot from six import iteritems from six.moves import input From d8cb0e6fb57e56e0c5c9cd4d17532d926a618391 Mon Sep 17 00:00:00 2001 From: Jorg Bornschein Date: Sun, 18 Oct 2015 22:57:18 -0400 Subject: [PATCH 2/4] Ensure experiments with unequal number of rows are plotted correctly --- bin/blocks-plot | 2 +- blocks/extras/scripts/plot.py | 8 +++++--- tests/scripts/test_plot.py | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/bin/blocks-plot b/bin/blocks-plot index 904d1f2..583d3db 100755 --- a/bin/blocks-plot +++ b/bin/blocks-plot @@ -34,7 +34,7 @@ def plot_dataframe(dataframe): print("Plotting {} channels:".format(len(dataframe.columns))) for cname, series in iteritems(dataframe): print(" {}".format(cname)) - pylab.plot(t, series.interpolate(), label=cname) + pylab.plot(t, series, label=cname) pylab.legend() pylab.show(block=True) diff --git a/blocks/extras/scripts/plot.py b/blocks/extras/scripts/plot.py index b6f42d9..4d3ff37 100644 --- a/blocks/extras/scripts/plot.py +++ b/blocks/extras/scripts/plot.py @@ -13,7 +13,7 @@ from blocks.serialization import load try: - from pandas import DataFrame + import pandas PANDAS_AVAILABLE = True except ImportError: PANDAS_AVAILABLE = False @@ -95,7 +95,7 @@ def match_column_specs(experiments, column_specs): " install it with pip.") # We iterate over all column and match each spec to the # channels of all experiments. - df = DataFrame() + df = pandas.DataFrame() for spec in column_specs: if ":" in spec: exp_spec, column_spec = spec.split(":") @@ -111,6 +111,8 @@ def match_column_specs(experiments, column_specs): continue column_name = "{}:{}".format(i, column) - df[column_name] = exp[column] + + exp = exp.rename(columns={column: column_name}) + df = pandas.concat((df, exp[column_name]), axis=1) return df diff --git a/tests/scripts/test_plot.py b/tests/scripts/test_plot.py index a0cefc4..253d856 100644 --- a/tests/scripts/test_plot.py +++ b/tests/scripts/test_plot.py @@ -22,8 +22,8 @@ def some_experiments(): experiments['exp0']['col0'] = (0, 1, 2) experiments['exp0']['col1'] = (3, 4, 5) experiments['exp1'] = DataFrame() - experiments['exp1']['col0'] = (6, 7, 8) - experiments['exp1']['col1'] = (9, 9, 9) + experiments['exp1']['col0'] = (6, 7, 8, 9) + experiments['exp1']['col1'] = (9, 9, 9, 9) return experiments @@ -66,3 +66,4 @@ def test_match_column_specs(): assert isinstance(df, DataFrame) assert list(df.columns) == ['0:col0', '0:col1', '1:col1'] + assert list(df.index) == [0, 1, 2, 3] From d8302e851d6407f586310444a1bea89b18ff5bad Mon Sep 17 00:00:00 2001 From: Jorg Bornschein Date: Sun, 18 Oct 2015 22:58:19 -0400 Subject: [PATCH 3/4] Ignore spaces in comma separated channel specs --- bin/blocks-plot | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/blocks-plot b/bin/blocks-plot index 583d3db..109f565 100755 --- a/bin/blocks-plot +++ b/bin/blocks-plot @@ -121,6 +121,7 @@ def main(args): break column_specs = column_spec.split(',') + column_specs = [s.strip() for s in column_specs] matched = plot.match_column_specs(experiments, column_specs) if len(matched.columns) == 0: From 02cd0f5f20e540e53926ffaee782980e7715e06b Mon Sep 17 00:00:00 2001 From: Jorg Bornschein Date: Mon, 19 Oct 2015 12:12:54 -0400 Subject: [PATCH 4/4] blocks-plot: Use 'nearest' interpolation --- bin/blocks-plot | 2 +- tests/scripts/test_plot.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/bin/blocks-plot b/bin/blocks-plot index 109f565..22883ac 100755 --- a/bin/blocks-plot +++ b/bin/blocks-plot @@ -34,7 +34,7 @@ def plot_dataframe(dataframe): print("Plotting {} channels:".format(len(dataframe.columns))) for cname, series in iteritems(dataframe): print(" {}".format(cname)) - pylab.plot(t, series, label=cname) + pylab.plot(t, series.interpolate(method='nearest'), label=cname) pylab.legend() pylab.show(block=True) diff --git a/tests/scripts/test_plot.py b/tests/scripts/test_plot.py index 253d856..581fab5 100644 --- a/tests/scripts/test_plot.py +++ b/tests/scripts/test_plot.py @@ -3,6 +3,7 @@ from collections import OrderedDict from tests import silence_printing, skip_if_not_available +from numpy import nan, isfinite from blocks.log import TrainingLog from blocks.main_loop import MainLoop @@ -67,3 +68,22 @@ def test_match_column_specs(): assert isinstance(df, DataFrame) assert list(df.columns) == ['0:col0', '0:col1', '1:col1'] assert list(df.index) == [0, 1, 2, 3] + + +def test_interpolate(): + skip_if_not_available(modules=['pandas']) + """ Ensure tha DataFrame.interpolate(method='nearest') has the + desired properties. + + It is used by blocks-plot and should: + + * interpolate missing/NaN datapoints between valid ones + * not replace any NaN before/after the first/last finite datapoint + """ + y = [nan, nan, 2., 3., nan, 5, nan, nan] + df = DataFrame(y) + df_ = df.interpolate(method='nearest')[0] + + assert all(isfinite(df_[2:6])) + assert all(~isfinite(df_[0:2])) + assert all(~isfinite(df_[6:8]))