From 7d4a28f3687bde7a4504a8ea2225aeedfed24ff9 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Mon, 21 Jul 2014 02:27:31 -0400 Subject: [PATCH 1/4] Allow running plot monitor from within IPython Notebook --- pylearn2/scripts/plot_monitor.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pylearn2/scripts/plot_monitor.py b/pylearn2/scripts/plot_monitor.py index f64308cd50..90ccdbca09 100755 --- a/pylearn2/scripts/plot_monitor.py +++ b/pylearn2/scripts/plot_monitor.py @@ -68,8 +68,12 @@ def main(): parser.add_argument("model_paths", nargs='+') options = parser.parse_args() model_paths = options.model_paths + options_out = options.out - if options.out is not None: + run(model_paths, options_out) + +def run(model_paths=[], options_out=None, show_codes=None): + if options_out is not None: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt @@ -93,7 +97,7 @@ def main(): raise this_model_channels = model.monitor.channels - if len(sys.argv) > 2: + if len(model_paths) > 1: postfix = ":" + model_names[i] else: postfix = "" @@ -127,8 +131,9 @@ def main(): # If there is more than one channel in the monitor ask which ones to # plot prompt = len(channels.values()) > 1 - - if prompt: + if show_codes: + final_codes = show_codes + elif prompt: # Display the codebook for code in sorted_codes: @@ -232,7 +237,10 @@ def main(): # plot the requested channels for idx, code in enumerate(sorted(final_codes)): - channel_name= codebook[code] + if code in codebook.values(): + channel_name = code + else: + channel_name = codebook[code] channel = channels[channel_name] y = np.asarray(channel.val_record) @@ -276,12 +284,12 @@ def main(): # 0.046 is the size of 1 legend box fig.subplots_adjust(bottom=0.11 + 0.046 * len(final_codes)) - if options.out is None: + if options_out is None: plt.show() else: - plt.savefig(options.out) + plt.savefig(options_out) - if not prompt: + if not prompt or show_codes: break if __name__ == "__main__": From 16e33dad88ae72c9a4d39fe12142e945529be211 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Tue, 22 Jul 2014 18:40:23 -0400 Subject: [PATCH 2/4] Ability to render images inline --- pylearn2/utils/image.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pylearn2/utils/image.py b/pylearn2/utils/image.py index c4f40074d2..2f234a224d 100644 --- a/pylearn2/utils/image.py +++ b/pylearn2/utils/image.py @@ -112,6 +112,11 @@ def show(image): If ndarray, integer formats are assumed to use 0-255 and float formats are assumed to use 0-1 """ + viewer_command = string.preprocess('${PYLEARN2_VIEWER_COMMAND}') + + if viewer_command == 'inline': + return imview(image) + if hasattr(image, '__array__'): #do some shape checking because PIL just raises a tuple indexing error #that doesn't make it very clear what the problem is @@ -160,7 +165,6 @@ def show(image): # image.save(name) - viewer_command = string.preprocess('${PYLEARN2_VIEWER_COMMAND}') if os.name == 'nt': subprocess.Popen(viewer_command + ' ' + name +' && del ' + name, shell=True) From 67b7780dfd776f98394e64b4618a448206ea654e Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Wed, 23 Jul 2014 14:13:08 -0400 Subject: [PATCH 3/4] Allow using predict_csv from within Python. Support for more CSV data formats. --- pylearn2/scripts/mlp/predict_csv.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pylearn2/scripts/mlp/predict_csv.py b/pylearn2/scripts/mlp/predict_csv.py index 48004b5363..856fb279af 100644 --- a/pylearn2/scripts/mlp/predict_csv.py +++ b/pylearn2/scripts/mlp/predict_csv.py @@ -19,7 +19,8 @@ from theano import tensor as T from theano import function -if __name__ == "__main__": + +def main(): try: model_path = sys.argv[1] test_path = sys.argv[2] @@ -28,7 +29,10 @@ print "Usage: predict.py " print " predict.py saved_model.pkl test_x.csv predictions.csv\n" quit(-1) + run(model_path, test_path, out_path) + +def run(model_path, test_path, out_path, headers=False, cast_to_float32=False, first_col_label=False): print "loading model..." try: @@ -52,7 +56,14 @@ # x is a numpy array # x = pickle.load(open(test_path, 'rb')) - x = np.loadtxt(test_path, delimiter=',') # no labels in the file + # only skip first row if labels in the file + skiprows = 1 if headers else 0 + x = np.loadtxt(test_path, delimiter=',', skiprows=skiprows) + if cast_to_float32: + x = x.astype(np.float32) + + if first_col_label: + x = x[:,1:] y = f(x) @@ -61,3 +72,6 @@ np.savetxt(out_path, y, fmt='%d') +if __name__ == "__main__": + main() + From 3c50801a495ceb7eca553167f136103dd1c65961 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Wed, 23 Jul 2014 14:14:11 -0400 Subject: [PATCH 4/4] plot monitor takes x_axis parameter --- pylearn2/scripts/plot_monitor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pylearn2/scripts/plot_monitor.py b/pylearn2/scripts/plot_monitor.py index 90ccdbca09..c7a737d47b 100755 --- a/pylearn2/scripts/plot_monitor.py +++ b/pylearn2/scripts/plot_monitor.py @@ -70,9 +70,9 @@ def main(): model_paths = options.model_paths options_out = options.out - run(model_paths, options_out) + plot_monitor(model_paths, options_out) -def run(model_paths=[], options_out=None, show_codes=None): +def plot_monitor(model_paths=[], options_out=None, show_codes=None, x_axis='example'): if options_out is not None: import matplotlib matplotlib.use('Agg') @@ -121,9 +121,6 @@ def run(model_paths=[], options_out=None, show_codes=None): codebook['<'+channel_name+'>'] = channel_name sorted_codes.append(code) - x_axis = 'example' - print 'set x_axis to example' - if len(channels.values()) == 0: print "there are no channels to plot" break @@ -226,6 +223,8 @@ def run(model_paths=[], options_out=None, show_codes=None): else: final_codes ,= set(codebook.keys()) + print 'set x_axis to %s' % x_axis + colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] styles = list(colors) styles += [color+'--' for color in colors]