From 86bf3359d5af20fe912f5e75915e820d6900cfd1 Mon Sep 17 00:00:00 2001 From: Adrien VINCENT Date: Wed, 28 Sep 2016 10:24:21 +0200 Subject: [PATCH 01/11] DOC: switch to pyplot OO interface --- examples/pylab_examples/mri_with_eeg.py | 60 ++++++++++++------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index e7f8c7f16937..61fa4ea98a44 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -6,33 +6,36 @@ from __future__ import division, print_function import numpy as np +import matplotlib.pyplot as plt +import matplotlib.cbook as cbook +import matplotlib.cm as cm -from matplotlib.pyplot import * from matplotlib.collections import LineCollection -import matplotlib.cbook as cbook # I use if 1 to break up the different regions of code visually +fig = plt.figure("MRI_with_EEG") + if 1: # load the data # data are 256x256 16 bit integers dfile = cbook.get_sample_data('s1045.ima.gz') im = np.fromstring(dfile.read(), np.uint16).astype(float) - im.shape = 256, 256 + im.shape = (256, 256) if 1: # plot the MRI in pcolor - subplot(221) - imshow(im, cmap=cm.gray) - axis('off') + ax0 = fig.add_subplot(2, 2, 1) + ax0.imshow(im, cmap=cm.gray) + ax0.axis('off') if 1: # plot the histogram of MRI intensity - subplot(222) + ax1 = fig.add_subplot(2, 2, 2) im = np.ravel(im) im = im[np.nonzero(im)] # ignore the background - im = im/(2.0**15) # normalize - hist(im, 100) - xticks([-1, -.5, 0, .5, 1]) - yticks([]) - xlabel('intensity') - ylabel('MRI density') + im = im / (2**15) # normalize + ax1.hist(im, 100) + ax1.set_xticks([-1, -0.5, 0, 0.5, 1]) + ax1.set_yticks([]) + ax1.set_xlabel('intensity') + ax1.set_ylabel('MRI density') if 1: # plot the EEG # load the data @@ -41,37 +44,34 @@ eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) print('loading eeg %s' % eegfile) data = np.fromstring(open(eegfile, 'rb').read(), float) - data.shape = numSamples, numRows - t = 10.0 * np.arange(numSamples, dtype=float)/numSamples + data.shape = (numSamples, numRows) + t = 10.0 * np.arange(numSamples) / numSamples ticklocs = [] - ax = subplot(212) - xlim(0, 10) - xticks(np.arange(10)) + ax2 = fig.add_subplot(2, 1, 2) + ax2.set_xlim(0, 10) + ax2.set_xticks(np.arange(10)) dmin = data.min() dmax = data.max() - dr = (dmax - dmin)*0.7 # Crowd them a bit. + dr = (dmax - dmin) * 0.7 # Crowd them a bit. y0 = dmin y1 = (numRows - 1) * dr + dmax - ylim(y0, y1) + ax2.set_ylim(y0, y1) segs = [] for i in range(numRows): segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis]))) - ticklocs.append(i*dr) + ticklocs.append(i * dr) offsets = np.zeros((numRows, 2), dtype=float) offsets[:, 1] = ticklocs - lines = LineCollection(segs, offsets=offsets, - transOffset=None, - ) - - ax.add_collection(lines) + lines = LineCollection(segs, offsets=offsets, transOffset=None) + ax2.add_collection(lines) # set the yticks to use axes coords on the y axis - ax.set_yticks(ticklocs) - ax.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) + ax2.set_yticks(ticklocs) + ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) - xlabel('time (s)') + ax2.set_xlabel('time (s)') -show() +plt.show() From 330a80792ad28001bfcf8aea569b51325661a00e Mon Sep 17 00:00:00 2001 From: Adrien VINCENT Date: Wed, 28 Sep 2016 10:36:09 +0200 Subject: [PATCH 02/11] DOC: cosmetick adjustements (tight_layout, labels and comments) --- examples/pylab_examples/mri_with_eeg.py | 35 +++++++++++++------------ 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 61fa4ea98a44..63a47c9a43e7 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -1,6 +1,7 @@ -""" -This now uses the imshow command instead of pcolor which *is much -faster* +"""Displays a set of subplots with an MRI image, its density histogram and +some EEG traces. + +NB: ones uses the imshow command instead of pcolor which *is much faster*. """ from __future__ import division, print_function @@ -11,38 +12,37 @@ import matplotlib.cm as cm from matplotlib.collections import LineCollection -# I use if 1 to break up the different regions of code visually +# NB: one uses "if 1:" to break up the different regions of code visually fig = plt.figure("MRI_with_EEG") -if 1: # load the data - # data are 256x256 16 bit integers +if 1: # Load the data + # Data are 256x256 16 bit integers dfile = cbook.get_sample_data('s1045.ima.gz') im = np.fromstring(dfile.read(), np.uint16).astype(float) im.shape = (256, 256) -if 1: # plot the MRI in pcolor +if 1: # Plot the MRI image ax0 = fig.add_subplot(2, 2, 1) ax0.imshow(im, cmap=cm.gray) ax0.axis('off') -if 1: # plot the histogram of MRI intensity +if 1: # Plot the histogram of MRI intensity ax1 = fig.add_subplot(2, 2, 2) im = np.ravel(im) - im = im[np.nonzero(im)] # ignore the background - im = im / (2**15) # normalize + im = im[np.nonzero(im)] # Ignore the background + im = im / (2**15) # Normalize ax1.hist(im, 100) ax1.set_xticks([-1, -0.5, 0, 0.5, 1]) ax1.set_yticks([]) - ax1.set_xlabel('intensity') + ax1.set_xlabel('Intensity') ax1.set_ylabel('MRI density') -if 1: # plot the EEG - # load the data - +if 1: # Plot the EEG + # Load the data numSamples, numRows = 800, 4 eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) - print('loading eeg %s' % eegfile) + print('Loading EEG %s' % eegfile) data = np.fromstring(open(eegfile, 'rb').read(), float) data.shape = (numSamples, numRows) t = 10.0 * np.arange(numSamples) / numSamples @@ -68,10 +68,11 @@ lines = LineCollection(segs, offsets=offsets, transOffset=None) ax2.add_collection(lines) - # set the yticks to use axes coords on the y axis + # Set the yticks to use axes coords on the y axis ax2.set_yticks(ticklocs) ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) - ax2.set_xlabel('time (s)') + ax2.set_xlabel('Time (s)') +plt.tight_layout() plt.show() From 2d65280dfb0b011faca3c2ceedacf8abefd31396 Mon Sep 17 00:00:00 2001 From: Adrien VINCENT Date: Wed, 28 Sep 2016 10:43:35 +0200 Subject: [PATCH 03/11] Replace (unfortunate) manual xticks with example of MultipleLocator --- examples/pylab_examples/mri_with_eeg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 63a47c9a43e7..44b9b8a0a416 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -12,6 +12,7 @@ import matplotlib.cm as cm from matplotlib.collections import LineCollection +from matplotlib.ticker import MultipleLocator # NB: one uses "if 1:" to break up the different regions of code visually fig = plt.figure("MRI_with_EEG") @@ -33,7 +34,7 @@ im = im[np.nonzero(im)] # Ignore the background im = im / (2**15) # Normalize ax1.hist(im, 100) - ax1.set_xticks([-1, -0.5, 0, 0.5, 1]) + ax1.xaxis.set_major_locator(MultipleLocator(0.5)) ax1.set_yticks([]) ax1.set_xlabel('Intensity') ax1.set_ylabel('MRI density') From 0c204d2aaac40cc95a307ae590102166ff170091 Mon Sep 17 00:00:00 2001 From: Adrien VINCENT Date: Wed, 28 Sep 2016 10:46:03 +0200 Subject: [PATCH 04/11] DOC: correct statement in docstring --- examples/pylab_examples/mri_with_eeg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 44b9b8a0a416..54f30272315a 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -1,4 +1,4 @@ -"""Displays a set of subplots with an MRI image, its density histogram and +"""Displays a set of subplots with an MRI image, its intensity histogram and some EEG traces. NB: ones uses the imshow command instead of pcolor which *is much faster*. From d06e57283f97d6d014085dd8f24e73c7f9c39f22 Mon Sep 17 00:00:00 2001 From: Adrien VINCENT Date: Wed, 28 Sep 2016 18:01:17 +0200 Subject: [PATCH 05/11] Switch to triple quotes comment blocks as visual separators --- examples/pylab_examples/mri_with_eeg.py | 125 +++++++++++++----------- 1 file changed, 67 insertions(+), 58 deletions(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 54f30272315a..7aaa823cc909 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -14,66 +14,75 @@ from matplotlib.collections import LineCollection from matplotlib.ticker import MultipleLocator -# NB: one uses "if 1:" to break up the different regions of code visually fig = plt.figure("MRI_with_EEG") -if 1: # Load the data - # Data are 256x256 16 bit integers - dfile = cbook.get_sample_data('s1045.ima.gz') - im = np.fromstring(dfile.read(), np.uint16).astype(float) - im.shape = (256, 256) - -if 1: # Plot the MRI image - ax0 = fig.add_subplot(2, 2, 1) - ax0.imshow(im, cmap=cm.gray) - ax0.axis('off') - -if 1: # Plot the histogram of MRI intensity - ax1 = fig.add_subplot(2, 2, 2) - im = np.ravel(im) - im = im[np.nonzero(im)] # Ignore the background - im = im / (2**15) # Normalize - ax1.hist(im, 100) - ax1.xaxis.set_major_locator(MultipleLocator(0.5)) - ax1.set_yticks([]) - ax1.set_xlabel('Intensity') - ax1.set_ylabel('MRI density') - -if 1: # Plot the EEG - # Load the data - numSamples, numRows = 800, 4 - eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) - print('Loading EEG %s' % eegfile) - data = np.fromstring(open(eegfile, 'rb').read(), float) - data.shape = (numSamples, numRows) - t = 10.0 * np.arange(numSamples) / numSamples - ticklocs = [] - ax2 = fig.add_subplot(2, 1, 2) - ax2.set_xlim(0, 10) - ax2.set_xticks(np.arange(10)) - dmin = data.min() - dmax = data.max() - dr = (dmax - dmin) * 0.7 # Crowd them a bit. - y0 = dmin - y1 = (numRows - 1) * dr + dmax - ax2.set_ylim(y0, y1) - - segs = [] - for i in range(numRows): - segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis]))) - ticklocs.append(i * dr) - - offsets = np.zeros((numRows, 2), dtype=float) - offsets[:, 1] = ticklocs - - lines = LineCollection(segs, offsets=offsets, transOffset=None) - ax2.add_collection(lines) - - # Set the yticks to use axes coords on the y axis - ax2.set_yticks(ticklocs) - ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) - - ax2.set_xlabel('Time (s)') +""" +Load the data +""" +# Data are 256x256 16 bit integers +dfile = cbook.get_sample_data('s1045.ima.gz') +im = np.fromstring(dfile.read(), np.uint16).astype(float) +im.shape = (256, 256) + +""" +Plot the MRI image +""" +ax0 = fig.add_subplot(2, 2, 1) +ax0.imshow(im, cmap=cm.gray) +ax0.axis('off') + +""" +Plot the histogram of MRI intensity +""" +ax1 = fig.add_subplot(2, 2, 2) +im = np.ravel(im) +im = im[np.nonzero(im)] # Ignore the background +im = im / (2**15) # Normalize +ax1.hist(im, 100) +ax1.xaxis.set_major_locator(MultipleLocator(0.5)) +ax1.set_yticks([]) +ax1.set_xlabel('Intensity') +ax1.set_ylabel('MRI density') + +""" +Plot the EEG +""" +# Load the data +numSamples, numRows = 800, 4 +eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) +print('Loading EEG %s' % eegfile) +data = np.fromstring(open(eegfile, 'rb').read(), float) +data.shape = (numSamples, numRows) +t = 10.0 * np.arange(numSamples) / numSamples + +ticklocs = [] +ax2 = fig.add_subplot(2, 1, 2) +ax2.set_xlim(0, 10) +ax2.set_xticks(np.arange(10)) +dmin = data.min() +dmax = data.max() +dr = (dmax - dmin) * 0.7 # Crowd them a bit. +y0 = dmin +y1 = (numRows - 1) * dr + dmax +ax2.set_ylim(y0, y1) + +segs = [] +for i in range(numRows): + segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis]))) + ticklocs.append(i * dr) + +offsets = np.zeros((numRows, 2), dtype=float) +offsets[:, 1] = ticklocs + +lines = LineCollection(segs, offsets=offsets, transOffset=None) +ax2.add_collection(lines) + +# Set the yticks to use axes coords on the y axis +ax2.set_yticks(ticklocs) +ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) + +ax2.set_xlabel('Time (s)') + plt.tight_layout() plt.show() From f5da34f0d75ebf01f9440c1b7a65af07a2d00cfd Mon Sep 17 00:00:00 2001 From: Adrien VINCENT Date: Fri, 30 Sep 2016 10:43:34 +0200 Subject: [PATCH 06/11] DOC: structure blocks of code with only a top real comment line --- examples/pylab_examples/mri_with_eeg.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 7aaa823cc909..e077e3d830ca 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -16,38 +16,28 @@ fig = plt.figure("MRI_with_EEG") -""" -Load the data -""" -# Data are 256x256 16 bit integers +# Load the MRI data (256x256 16 bit integers) dfile = cbook.get_sample_data('s1045.ima.gz') im = np.fromstring(dfile.read(), np.uint16).astype(float) im.shape = (256, 256) -""" -Plot the MRI image -""" +# Plot the MRI image ax0 = fig.add_subplot(2, 2, 1) ax0.imshow(im, cmap=cm.gray) ax0.axis('off') -""" -Plot the histogram of MRI intensity -""" +# Plot the histogram of MRI intensity ax1 = fig.add_subplot(2, 2, 2) im = np.ravel(im) im = im[np.nonzero(im)] # Ignore the background im = im / (2**15) # Normalize -ax1.hist(im, 100) +ax1.hist(im, bins=100) ax1.xaxis.set_major_locator(MultipleLocator(0.5)) ax1.set_yticks([]) ax1.set_xlabel('Intensity') ax1.set_ylabel('MRI density') -""" -Plot the EEG -""" -# Load the data +# Load the EEG data numSamples, numRows = 800, 4 eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) print('Loading EEG %s' % eegfile) @@ -55,6 +45,7 @@ data.shape = (numSamples, numRows) t = 10.0 * np.arange(numSamples) / numSamples +# Plot the EEG ticklocs = [] ax2 = fig.add_subplot(2, 1, 2) ax2.set_xlim(0, 10) @@ -77,7 +68,7 @@ lines = LineCollection(segs, offsets=offsets, transOffset=None) ax2.add_collection(lines) -# Set the yticks to use axes coords on the y axis +# Set the yticks to use axes coordinates on the y axis ax2.set_yticks(ticklocs) ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9']) From c8388f9d8d6fae56d21513862b12fd4df76c22fe Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Sat, 1 Oct 2016 12:44:22 +0200 Subject: [PATCH 07/11] Close files or use safer access functions --- examples/pylab_examples/mri_with_eeg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index e077e3d830ca..8e0a28569e6c 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -20,6 +20,7 @@ dfile = cbook.get_sample_data('s1045.ima.gz') im = np.fromstring(dfile.read(), np.uint16).astype(float) im.shape = (256, 256) +dfile.close() # Plot the MRI image ax0 = fig.add_subplot(2, 2, 1) @@ -41,7 +42,7 @@ numSamples, numRows = 800, 4 eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) print('Loading EEG %s' % eegfile) -data = np.fromstring(open(eegfile, 'rb').read(), float) +data = np.fromfile(eegfile, dtype=float) data.shape = (numSamples, numRows) t = 10.0 * np.arange(numSamples) / numSamples From 38d92b58311e2e830d2748978db325f19ca01085 Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Sun, 2 Oct 2016 10:59:41 +0200 Subject: [PATCH 08/11] Remove top comment 'pcolor vs imshow' --- examples/pylab_examples/mri_with_eeg.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 8e0a28569e6c..736b1e33573e 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -1,7 +1,5 @@ """Displays a set of subplots with an MRI image, its intensity histogram and some EEG traces. - -NB: ones uses the imshow command instead of pcolor which *is much faster*. """ from __future__ import division, print_function From dfc9cca87b4609de7ea015a30a65a9a037bd6a1b Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Sun, 2 Oct 2016 11:21:03 +0200 Subject: [PATCH 09/11] Normalize intensity values in [0, 1] and adjust x-ticks --- examples/pylab_examples/mri_with_eeg.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/pylab_examples/mri_with_eeg.py b/examples/pylab_examples/mri_with_eeg.py index 736b1e33573e..2a601ea707a2 100755 --- a/examples/pylab_examples/mri_with_eeg.py +++ b/examples/pylab_examples/mri_with_eeg.py @@ -29,11 +29,12 @@ ax1 = fig.add_subplot(2, 2, 2) im = np.ravel(im) im = im[np.nonzero(im)] # Ignore the background -im = im / (2**15) # Normalize +im = im / (2**16 - 1) # Normalize ax1.hist(im, bins=100) -ax1.xaxis.set_major_locator(MultipleLocator(0.5)) +ax1.xaxis.set_major_locator(MultipleLocator(0.4)) +ax1.minorticks_on() ax1.set_yticks([]) -ax1.set_xlabel('Intensity') +ax1.set_xlabel('Intensity (a.u.)') ax1.set_ylabel('MRI density') # Load the EEG data From db3d206a4e328cfd6d910217cf46681efa33af96 Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Sun, 2 Oct 2016 11:27:51 +0200 Subject: [PATCH 10/11] Add a top dosctring and perform small cleaning/fixing ops to mri_with_eeg.py --- examples/pylab_examples/mri_demo.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/pylab_examples/mri_demo.py b/examples/pylab_examples/mri_demo.py index 75acc1e4e034..5a084db356ec 100755 --- a/examples/pylab_examples/mri_demo.py +++ b/examples/pylab_examples/mri_demo.py @@ -1,11 +1,14 @@ -from __future__ import print_function +"""Displays an MRI image.""" + import matplotlib.pyplot as plt import matplotlib.cbook as cbook import numpy as np -# data are 256x256 16 bit integers + +# Data are 256x256 16 bit integers dfile = cbook.get_sample_data('s1045.ima.gz') im = np.fromstring(dfile.read(), np.uint16).astype(float) -im.shape = 256, 256 +im.shape = (256, 256) +dfile.close() plt.imshow(im, cmap=plt.cm.gray) plt.axis('off') From 96ff81747aadd609a103886580f3e4e04b60b868 Mon Sep 17 00:00:00 2001 From: Adrien F Vincent Date: Sun, 2 Oct 2016 11:52:02 +0200 Subject: [PATCH 11/11] Switch mri_demo.py to OO interface --- examples/pylab_examples/mri_demo.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/pylab_examples/mri_demo.py b/examples/pylab_examples/mri_demo.py index 5a084db356ec..6574b0bf7e17 100755 --- a/examples/pylab_examples/mri_demo.py +++ b/examples/pylab_examples/mri_demo.py @@ -2,15 +2,18 @@ import matplotlib.pyplot as plt import matplotlib.cbook as cbook +import matplotlib.cm as cm import numpy as np +fig, ax = plt.subplots(num="MRI_demo") + # Data are 256x256 16 bit integers dfile = cbook.get_sample_data('s1045.ima.gz') im = np.fromstring(dfile.read(), np.uint16).astype(float) im.shape = (256, 256) dfile.close() -plt.imshow(im, cmap=plt.cm.gray) -plt.axis('off') +ax.imshow(im, cmap=cm.gray) +ax.axis('off') plt.show()