|
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. |
6 | 3 | """
|
7 | 4 | from __future__ import division, print_function
|
8 | 5 |
|
9 | 6 | import numpy as np
|
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import matplotlib.cbook as cbook |
| 9 | +import matplotlib.cm as cm |
10 | 10 |
|
11 |
| -from matplotlib.pyplot import * |
12 | 11 | 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") |
15 | 15 |
|
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() |
21 | 21 |
|
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') |
26 | 26 |
|
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') |
37 | 38 |
|
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 |
40 | 46 |
|
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) |
57 | 58 |
|
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) |
62 | 63 |
|
63 |
| - offsets = np.zeros((numRows, 2), dtype=float) |
64 |
| - offsets[:, 1] = ticklocs |
| 64 | +offsets = np.zeros((numRows, 2), dtype=float) |
| 65 | +offsets[:, 1] = ticklocs |
65 | 66 |
|
66 |
| - lines = LineCollection(segs, offsets=offsets, |
67 |
| - transOffset=None, |
68 |
| - ) |
| 67 | +lines = LineCollection(segs, offsets=offsets, transOffset=None) |
| 68 | +ax2.add_collection(lines) |
69 | 69 |
|
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']) |
71 | 73 |
|
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)') |
75 | 75 |
|
76 |
| - xlabel('time (s)') |
77 | 76 |
|
78 |
| -show() |
| 77 | +plt.tight_layout() |
| 78 | +plt.show() |
0 commit comments