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