8000 DOC: switch pylab example `mri_with_eeg.py` to OO interface + cosmetick fixes by afvincent · Pull Request #7192 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DOC: structure blocks of code with only a top real comment line
  • Loading branch information
afvincent committed Sep 30, 2016
commit f5da34f0d75ebf01f9440c1b7a65af07a2d00cfd
23 changes: 7 additions & 16 deletions examples/pylab_examples/mri_with_eeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,36 @@

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')
Copy link
Member

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.

Copy link
Member

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.

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
Copy link
Member

Choose a reason for hiding this comment

The 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 uint16 should have a maximum of 2**16, but maybe this just how MRIs are defined? I don't know, but it seems odd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @matthew-brett our resident expert on MRI :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.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)
data = np.fromstring(open(eegfile, 'rb').read(), float)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaky open; can use np.fromfile(eegfile, float) instead

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)
Expand All @@ -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'])

Expand Down
0