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

Skip to content

Commit 5b8efe1

Browse files
committed
Avoid leaks with cbook.get_sample_data.
1 parent 4df2bb7 commit 5b8efe1

File tree

7 files changed

+34
-35
lines changed

7 files changed

+34
-35
lines changed

examples/frontpage/membrane.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import numpy as np
1212

1313

14-
datafile = cbook.get_sample_data('membrane.dat', asfileobj=False)
15-
x = np.fromfile(datafile, np.float32)
14+
with cbook.get_sample_data('membrane.dat') as datafile:
15+
x = np.fromfile(datafile, np.float32)
1616
# 0.0005 is the sample interval
1717

1818
fig, ax = plt.subplots()

examples/images_contours_and_fields/image_clip_path.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import matplotlib.cbook as cbook
1111

1212

13-
image_file = cbook.get_sample_data('grace_hopper.png')
14-
image = plt.imread(image_file)
13+
with cbook.get_sample_data('grace_hopper.png') as image_file:
14+
image = plt.imread(image_file)
1515

1616
fig, ax = plt.subplots()
1717
im = ax.imshow(image)

examples/images_contours_and_fields/image_demo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
# It is also possible to show images of pictures.
4242

4343
# A sample image
44-
image_file = cbook.get_sample_data('ada.png')
45-
image = plt.imread(image_file)
44+
with cbook.get_sample_data('ada.png') as image_file:
45+
image = plt.imread(image_file)
4646

4747
fig, ax = plt.subplots()
4848
ax.imshow(image)
@@ -53,8 +53,8 @@
5353

5454
w, h = 512, 512
5555

56-
datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True)
57-
s = datafile.read()
56+
with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile:
57+
s = datafile.read()
5858
A = np.fromstring(s, np.uint16).astype(float).reshape((w, h))
5959
A /= A.max()
6060

examples/pylab_examples/mri_demo.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
import matplotlib.cm as cm
1313
import numpy as np
1414

15-
fig, ax = plt.subplots(num="MRI_demo")
1615

1716
# Data are 256x256 16 bit integers
18-
dfile = cbook.get_sample_data('s1045.ima.gz')
19-
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
20-
dfile.close()
17+
with cbook.get_sample_data('s1045.ima.gz') as dfile:
18+
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
2119

20+
fig, ax = plt.subplots(num="MRI_demo")
2221
ax.imshow(im, cmap=cm.gray)
2322
ax.axis('off')
2423

examples/pylab_examples/mri_with_eeg.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
fig = plt.figure("MRI_with_EEG")
2121

2222
# Load the MRI data (256x256 16 bit integers)
23-
dfile = cbook.get_sample_data('s1045.ima.gz')
24-
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
25-
dfile.close()
23+
with cbook.get_sample_data('s1045.ima.gz') as dfile:
24+
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
2625

2726
# Plot the MRI image
2827
ax0 = fig.add_subplot(2, 2, 1)
@@ -43,9 +42,8 @@
4342

4443
# Load the EEG data
4544
numSamples, numRows = 800, 4
46-
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
47-
print('Loading EEG %s' % eegfile)
48-
data = np.fromfile(eegfile, dtype=float)
45+
with cbook.get_sample_data('eeg.dat') as eegfile:
46+
data = np.fromfile(eegfile, dtype=float)
4947
data.shape = (numSamples, numRows)
5048
t = 10.0 * np.arange(numSamples) / numSamples
5149

examples/showcase/bachelors_degrees_by_gender.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from matplotlib.mlab import csv2rec
1515
from matplotlib.cbook import get_sample_data
1616

17-
fname = get_sample_data('percent_bachelors_degrees_women_usa.csv')
18-
gender_degree_data = csv2rec(fname)
17+
with get_sample_data('percent_bachelors_degrees_women_usa.csv') as fname:
18+
gender_degree_data = csv2rec(fname)
1919

2020
# These are the colors that will be used in the plot
2121
color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',

examples/specialty_plots/topographic_hillshading.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@
2121
from matplotlib.cbook import get_sample_data
2222
from matplotlib.colors import LightSource
2323

24-
dem = np.load(get_sample_data('jacksboro_fault_dem.npz'))
25-
z = dem['elevation']
2624

27-
#-- Optional dx and dy for accurate vertical exaggeration --------------------
28-
# If you need topographically accurate vertical exaggeration, or you don't want
29-
# to guess at what *vert_exag* should be, you'll need to specify the cellsize
30-
# of the grid (i.e. the *dx* and *dy* parameters). Otherwise, any *vert_exag*
31-
# value you specify will be relative to the grid spacing of your input data
32-
# (in other words, *dx* and *dy* default to 1.0, and *vert_exag* is calculated
33-
# relative to those parameters). Similarly, *dx* and *dy* are assumed to be in
34-
# the same units as your input z-values. Therefore, we'll need to convert the
35-
# given dx and dy from decimal degrees to meters.
36-
dx, dy = dem['dx'], dem['dy']
37-
dy = 111200 * dy
38-
dx = 111200 * dx * np.cos(np.radians(dem['ymin']))
39-
#-----------------------------------------------------------------------------
25+
with np.load(get_sample_data('jacksboro_fault_dem.npz')) as dem:
26+
z = dem['elevation']
27+
28+
#-- Optional dx and dy for accurate vertical exaggeration ----------------
29+
# If you need topographically accurate vertical exaggeration, or you don't
30+
# want to guess at what *vert_exag* should be, you'll need to specify the
31+
# cellsize of the grid (i.e. the *dx* and *dy* parameters). Otherwise, any
32+
# *vert_exag* value you specify will be relative to the grid spacing of
33+
# your input data (in other words, *dx* and *dy* default to 1.0, and
34+
# *vert_exag* is calculated relative to those parameters). Similarly, *dx*
35+
# and *dy* are assumed to be in the same units as your input z-values.
36+
# Therefore, we'll need to convert the given dx and dy from decimal degrees
37+
# to meters.
38+
dx, dy = dem['dx'], dem['dy']
39+
dy = 111200 * dy
40+
dx = 111200 * dx * np.cos(np.radians(dem['ymin']))
41+
#-------------------------------------------------------------------------
4042

4143
# Shade from the northwest, with the sun 45 degrees from horizontal
4244
ls = LightSource(azdeg=315, altdeg=45)

0 commit comments

Comments
 (0)