Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17fab80

Browse files
committed
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
1 parent b5cac46 commit 17fab80

File tree

2 files changed

+71
-65
lines changed

2 files changed

+71
-65
lines changed

examples/pylab_examples/mri_demo.py

Lines 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()
Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
1-
#!/usr/bin/env python
2-
3-
"""
4-
This now uses the imshow command instead of pcolor which *is much
5-
faster*
1+
"""Displays a set of subplots with an MRI image, its intensity histogram and
2+
some EEG traces.
63
"""
74
from __future__ import division, print_function
85

96
import numpy as np
7+
import matplotlib.pyplot as plt
8+
import matplotlib.cbook as cbook
9+
import matplotlib.cm as cm
1010

11-
from matplotlib.pyplot import *
1211
from matplotlib.collections import LineCollection
13-
import matplotlib.cbook as cbook
14-
# I use if 1 to break up the different regions of code visually
12+
from matplotlib.ticker import MultipleLocator
13+
14+
fig = plt.figure("MRI_with_EEG")
1515

16-
if 1: # load the data
17-
# data are 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
16+
# Load the MRI data (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+
dfile.close()
2121

22-
if 1: # plot the MRI in pcolor
23-
subplot(221)
24-
imshow(im, cmap=cm.gray)
25-
axis('off')
22+
# Plot the MRI image
23+
ax0 = fig.add_subplot(2, 2, 1)
24+
ax0.imshow(im, cmap=cm.gray)
25+
ax0.axis('off')
2626

27-
if 1: # plot the histogram of MRI intensity
28-
subplot(222)
29-
im = np.ravel(im)
30-
im = im[np.nonzero(im)] # ignore the background
31-
im = im/(2.0**15) # normalize
32-
hist(im, 100)
33-
xticks([-1, -.5, 0, .5, 1])
34-
yticks([])
35-
xlabel('intensity')
36-
ylabel('MRI density')
27+
# Plot the histogram of MRI intensity
28+
ax1 = fig.add_subplot(2, 2, 2)
29+
im = np.ravel(im)
30+
im = im[np.nonzero(im)] # Ignore the background
31+
im = im / (2**16 - 1) # Normalize
32+
ax1.hist(im, bins=100)
33+
ax1.xaxis.set_major_locator(MultipleLocator(0.4))
34+
ax1.minorticks_on()
35+
ax1.set_yticks([])
36+
ax1.set_xlabel('Intensity (a.u.)')
37+
ax1.set_ylabel('MRI density')
3738

38-
if 1: # plot the EEG
39-
# load the data
39+
# Load the EEG data
40+
numSamples, numRows = 800, 4
41+
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
42+
print('Loading EEG %s' % eegfile)
43+
data = np.fromfile(eegfile, dtype=float)
44+
data.shape = (numSamples, numRows)
45+
t = 10.0 * np.arange(numSamples) / numSamples
4046

41-
numSamples, numRows = 800, 4
42-
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
43-
print('loading eeg %s' % eegfile)
44-
data = np.fromstring(open(eegfile, 'rb').read(), float)
45-
data.shape = numSamples, numRows
46-
t = 10.0 * np.arange(numSamples, dtype=float)/numSamples
47-
ticklocs = []
48-
ax = subplot(212)
49-
xlim(0, 10)
50-
xticks(np.arange(10))
51-
dmin = data.min()
52-
dmax = data.max()
53-
dr = (dmax - dmin)*0.7 # Crowd them a bit.
54-
y0 = dmin
55-
y1 = (numRows - 1) * dr + dmax
56-
ylim(y0, y1)
47+
# Plot the EEG
48+
ticklocs = []
49+
ax2 = fig.add_subplot(2, 1, 2)
50+
ax2.set_xlim(0, 10)
51+
ax2.set_xticks(np.arange(10))
52+
dmin = data.min()
53+
dmax = data.max()
54+
dr = (dmax - dmin) * 0.7 # Crowd them a bit.
55+
y0 = dmin
56+
y1 = (numRows - 1) * dr + dmax
57+
ax2.set_ylim(y0, y1)
5758

58-
segs = []
59-
for i in range(numRows):
60-
segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis])))
61-
ticklocs.append(i*dr)
59+
segs = []
60+
for i in range(numRows):
61+
segs.append(np.hstack((t[:, np.newaxis], data[:, i, np.newaxis])))
62+
ticklocs.append(i * dr)
6263

63-
offsets = np.zeros((numRows, 2), dtype=float)
64-
offsets[:, 1] = ticklocs
64+
offsets = np.zeros((numRows, 2), dtype=float)
65+
offsets[:, 1] = ticklocs
6566

66-
lines = LineCollection(segs, offsets=offsets,
67-
transOffset=None,
68-
)
67+
lines = LineCollection(segs, offsets=offsets, transOffset=None)
68+
ax2.add_collection(lines)
6969

70-
ax.add_collection(lines)
70+
# Set the yticks to use axes coordinates on the y axis
71+
ax2.set_yticks(ticklocs)
72+
ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
7173

72-
# set the yticks to use axes coords on the y axis
73-
ax.set_yticks(ticklocs)
74-
ax.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
74+
ax2.set_xlabel('Time (s)')
7575

76-
xlabel('time (s)')
7776

78-
show()
77+
plt.tight_layout()
78+
plt.show()

0 commit comments

Comments
 (0)