Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b1bab75

Browse filesBrowse files
authored
Merge pull request #7192 from afvincent/doc_pylab_examples_mri_with_eeg
DOC: switch pylab example `mri_with_eeg.py` to OO interface + cosmetic fixes
2 parents 007031c + 96ff817 commit b1bab75
Copy full SHA for b1bab75

File tree

Expand file treeCollapse file tree

2 files changed

+83
-75
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+83
-75
lines changed

‎examples/pylab_examples/mri_demo.py

Copy file name to clipboard
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
from __future__ import print_function
1+
"""Displays an MRI image."""
2+
23
import matplotlib.pyplot as plt
34
import matplotlib.cbook as cbook
5+
import matplotlib.cm as cm
46
import numpy as np
5-
# data are 256x256 16 bit integers
7+
8+
fig, ax = plt.subplots(num="MRI_demo")
9+
10+
# Data are 256x256 16 bit integers
611
dfile = cbook.get_sample_data('s1045.ima.gz')
712
im = np.fromstring(dfile.read(), np.uint16).astype(float)
8-
im.shape = 256, 256
13+
im.shape = (256, 256)
14+
dfile.close()
915

10-
plt.imshow(im, cmap=plt.cm.gray)
11-
plt.axis('off')
16+
ax.imshow(im, cmap=cm.gray)
17+
ax.axis('off')
1218

1319
plt.show()
+72-70Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,79 @@
1-
"""
2-
This now uses the imshow command instead of pcolor which *is much
3-
faster*
1+
"""Displays a set of subplots with an MRI image, its intensity histogram and
2+
some EEG traces.
43
"""
54

65
from __future__ import division, print_function
76

87
import numpy as np
8+
import matplotlib.pyplot as plt
9+
import matplotlib.cbook as cbook
10+
import matplotlib.cm as cm
911

10-
from matplotlib.pyplot import *
1112
from matplotlib.collections import LineCollection
12-
import matplotlib.cbook as cbook
13-
# I use if 1 to break up the different regions of code visually
14-
15-
if 1: # load the data
16-
# data are 256x256 16 bit integers
17-
dfile = cbook.get_sample_data('s1045.ima.gz')
18-
im = np.fromstring(dfile.read(), np.uint16).astype(float)
19-
im.shape = 256, 256
20-
21-
if 1: # plot the MRI in pcolor
22-
subplot(221)
23-
imshow(im, cmap=cm.gray)
24-
axis('off')
25-
26-
if 1: # plot the histogram of MRI intensity
27-
subplot(222)
28-
im = np.ravel(im)
29-
im = im[np.nonzero(im)] # ignore the background
30-
im = im/(2.0**15) # normalize
31-
hist(im, 100)
32-
xticks([-1, -.5, 0, .5, 1])
33-
yticks([])
34-
xlabel('intensity')
35-
ylabel('MRI density')
36-
37-
if 1: # plot the EEG
38-
# load the data
39-
40-
numSamples, numRows = 800, 4
41-
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
42-
print('loading eeg %s' % eegfile)
43-
data = np.fromstring(open(eegfile, 'rb').read(), float)
44-
data.shape = numSamples, numRows
45-
t = 10.0 * np.arange(numSamples, dtype=float)/numSamples
46-
ticklocs = []
47-
ax = subplot(212)
48-
xlim(0, 10)
49-
xticks(np.arange(10))
50-
dmin = data.min()
51-
dmax = data.max()
52-
dr = (dmax - dmin)*0.7 # Crowd them a bit.
53-
y0 = dmin
54-
y1 = (numRows - 1) * dr + dmax
55-
ylim(y0, y1)
56-
57-
segs = []
58-
for i in range(numRows):
59-
segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis])))
60-
ticklocs.append(i*dr)
61-
62-
offsets = np.zeros((numRows, 2), dtype=float)
63-
offsets[:, 1] = ticklocs
64-
65-
lines = LineCollection(segs, offsets=offsets,
66-
transOffset=None,
67-
)
68-
69-
ax.add_collection(lines)
70-
71-
# set the yticks to use axes coords on the y axis
72-
ax.set_yticks(ticklocs)
73-
ax.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
74-
75-
xlabel('time (s)')
76-
77-
show()
13+
from matplotlib.ticker import MultipleLocator
14+
15+
fig = plt.figure("MRI_with_EEG")
16+
17+
# Load the MRI data (256x256 16 bit integers)
18+
dfile = cbook.get_sample_data('s1045.ima.gz')
19+
im = np.fromstring(dfile.read(), np.uint16).astype(float)
20+
im.shape = (256, 256)
21+
dfile.close()
22+
23+
# Plot the MRI image
24+
ax0 = fig.add_subplot(2, 2, 1)
25+
ax0.imshow(im, cmap=cm.gray)
26+
ax0.axis('off')
27+
28+
# Plot the histogram of MRI intensity
29+
ax1 = fig.add_subplot(2, 2, 2)
30+
im = np.ravel(im)
31+
im = im[np.nonzero(im)] # Ignore the background
32+
im = im / (2**16 - 1) # Normalize
33+
ax1.hist(im, bins=100)
34+
ax1.xaxis.set_major_locator(MultipleLocator(0.4))
35+
ax1.minorticks_on()
36+
ax1.set_yticks([])
37+
ax1.set_xlabel('Intensity (a.u.)')
38+
ax1.set_ylabel('MRI density')
39+
40+
# Load the EEG data
41+
numSamples, numRows = 800, 4
42+
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
43+
print('Loading EEG %s' % eegfile)
44+
data = np.fromfile(eegfile, dtype=float)
45+
data.shape = (numSamples, numRows)
46+
t = 10.0 * np.arange(numSamples) / numSamples
47+
48+
# Plot the EEG
49+
ticklocs = []
50+
ax2 = fig.add_subplot(2, 1, 2)
51+
ax2.set_xlim(0, 10)
52+
ax2.set_xticks(np.arange(10))
53+
dmin = data.min()
54+
dmax = data.max()
55+
dr = (dmax - dmin) * 0.7 # Crowd them a bit.
56+
y0 = dmin
57+
y1 = (numRows - 1) * dr + dmax
58+
ax2.set_ylim(y0, y1)
59+
60+
segs = []
61+
for i in range(numRows):
62+
segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis])))
63+
ticklocs.append(i * dr)
64+
65+
offsets = np.zeros((numRows, 2), dtype=float)
66+
offsets[:, 1] = ticklocs
67+
68+
lines = LineCollection(segs, offsets=offsets, transOffset=None)
69+
ax2.add_collection(lines)
70+
71+
# Set the yticks to use axes coordinates on the y axis
72+
ax2.set_yticks(ticklocs)
73+
ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
74+
75+
ax2.set_xlabel('Time (s)')
76+
77+
78+
plt.tight_layout()
79+
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.