-
-
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
Changes from 1 commit
86bf335
330a807
2d65280
0c204d2
d06e572
f5da34f
c8388f9
38d92b5
dfc9cca
db3d206
96ff817
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is how it was originally, but I'm not sure if it's right. The intensities go over 1 and a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ping @matthew-brett our resident expert on MRI :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that this normalization seemed weird to me too but I did not feel comfortable changing it to (2**16 - 1) as I do not know anything about MRI. PS: anyway, currently there are no units for the MRI... |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaky |
||
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() |
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.
I am sorry, but I still don't like this. This is also python code, meant for a specific purpose. Can you switch to comments?