-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
DOC: switch pylab example mri_with_eeg.py
to OO interface + cosmetick fixes
#7192
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
Merged
tacaswell
merged 11 commits into
matplotlib:master
from
afvincent:doc_pylab_examples_mri_with_eeg
Oct 3, 2016
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
86bf335
DOC: switch to pyplot OO interface
afvincent 330a807
DOC: cosmetick adjustements (tight_layout, labels and comments)
afvincent 2d65280
Replace (unfortunate) manual xticks with example of MultipleLocator
afvincent 0c204d2
DOC: correct statement in docstring
afvincent d06e572
Switch to triple quotes comment blocks as visual separators
afvincent f5da34f
DOC: structure blocks of code with only a top real comment line
afvincent c8388f9
Close files or use safer access functions
afvincent 38d92b5
Remove top comment 'pcolor vs imshow'
afvincent dfc9cca
Normalize intensity values in [0, 1] and adjust x-ticks
afvincent db3d206
Add a top dosctring and perform small cleaning/fixing ops to mri_with…
afvincent 96ff817
Switch mri_demo.py to OO interface
afvincent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
from __future__ import print_function | ||
"""Displays an MRI image.""" | ||
|
||
import matplotlib.pyplot as plt | ||
import matplotlib.cbook as cbook | ||
import matplotlib.cm as cm | ||
import numpy as np | ||
# data are 256x256 16 bit integers | ||
|
||
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 | ||
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,79 @@ | ||
""" | ||
This now uses the imshow command instead of pcolor which *is much | ||
faster* | ||
"""Displays a set of subplots with an MRI image, its intensity histogram and | ||
some EEG traces. | ||
""" | ||
|
||
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 | ||
|
||
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 | ||
subplot(221) | ||
imshow(im, cmap=cm.gray) | ||
axis('off') | ||
|
||
if 1: # plot the histogram of MRI intensity | ||
subplot(222) | ||
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') | ||
|
||
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, dtype=float)/numSamples | ||
ticklocs = [] | ||
ax = subplot(212) | ||
xlim(0, 10) | ||
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 | ||
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, | ||
) | ||
|
||
ax.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']) | ||
|
||
xlabel('time (s)') | ||
|
||
show() | ||
from matplotlib.ticker import MultipleLocator | ||
|
||
fig = plt.figure("MRI_with_EEG") | ||
|
||
# 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) | ||
dfile.close() | ||
|
||
# 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**16 - 1) # Normalize | ||
ax1.hist(im, bins=100) | ||
ax1.xaxis.set_major_locator(MultipleLocator(0.4)) | ||
ax1.minorticks_on() | ||
ax1.set_yticks([]) | ||
ax1.set_xlabel('Intensity (a.u.)') | ||
ax1.set_ylabel('MRI density') | ||
|
||
# Load the EEG data | ||
numSamples, numRows = 800, 4 | ||
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False) | ||
print('Loading EEG %s' % eegfile) | ||
data = np.fromfile(eegfile, dtype=float) | ||
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) | ||
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 coordinates 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should be closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_sample_data
should probably be deprecated and replaced with something with a sane interface.In any case, python will close that file automatically at the end of the script without problems considering it is a read-only file.