Skip to content

Commit 5e00fd2

Browse files
committed
Merge pull request #210 from GaelVaroquaux/fast_gzip_read
MRG: fast reads on large gzip files Fixes #209
2 parents d14f287 + 4c3572b commit 5e00fd2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

nibabel/openers.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
import gzip
1414
import bz2
1515

16+
# The largest memory chunk that gzip can use for reads
17+
GZIP_MAX_READ_CHUNK = 100 * 1024 * 1024 # 100Mb
18+
19+
20+
def _gzip_open(fileish, *args, **kwargs):
21+
# open gzip files with faster reads on large files using larger chunks
22+
# See https://github.com/nipy/nibabel/pull/210 for discussion
23+
gzip_file = gzip.open(fileish, *args, **kwargs)
24+
gzip_file.max_read_chunk = GZIP_MAX_READ_CHUNK
25+
return gzip_file
26+
1627

1728
class Opener(object):
1829
""" Class to accept, maybe open, and context-manage file-likes / filenames
@@ -32,7 +43,7 @@ class Opener(object):
3243
passed to opening method when `fileish` is str. Change of defaults as
3344
for \*args
3445
"""
35-
gz_def = (gzip.open, ('mode', 'compresslevel'))
46+
gz_def = (_gzip_open, ('mode', 'compresslevel'))
3647
bz2_def = (bz2.BZ2File, ('mode', 'buffering', 'compresslevel'))
3748
compress_ext_map = {
3849
'.gz': gz_def,

0 commit comments

Comments
 (0)