Skip to content

Commit 17cd7eb

Browse files
committed
Provide option to use Zopfli
Includes an option to use Zopfli to perform gzip compression. This can make a small though notable improvement in compression size of the data. Though can take a bit longer to run. As the data is still gzip compressed, it can still be decompressed with any gzip implementation like Pako. So no other changes are needed to handle the data.
1 parent 81e0223 commit 17cd7eb

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

nanshe_ipython.ipynb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,9 @@
16711671
"import mmap\n",
16721672
"import textwrap\n",
16731673
"\n",
1674+
"import zopfli\n",
1675+
"import zopfli.gzip\n",
1676+
"\n",
16741677
"import numpy\n",
16751678
"import numpy as np\n",
16761679
"\n",
@@ -1728,7 +1731,8 @@
17281731
"* `roi_alpha` (`float`): transparency of the ROIs in a range of [0.0, 1.0].\n",
17291732
"* `roi_border_width` (`int`): width of the line border on each ROI.\n",
17301733
"* `trace_plot_width` (`int`): width of the trace plot.\n",
1731-
"* `compresslevel` (`int`): compression level to use with zlib for trace data [0, 9]."
1734+
"* `compresslevel` (`int`): compression level to use with zlib for trace data [0, 9].\n",
1735+
"* `use_zopfli` (`bool`): whether to use Zopfli or zlib."
17321736
]
17331737
},
17341738
{
@@ -1743,6 +1747,7 @@
17431747
"roi_border_width = 3\n",
17441748
"trace_plot_width = 500\n",
17451749
"compresslevel = 6\n",
1750+
"use_zopfli = False\n",
17461751
"\n",
17471752
"\n",
17481753
"bio.curdoc().clear()\n",
@@ -1821,18 +1826,21 @@
18211826
" plot_projs.append(plot_std)\n",
18221827
"\n",
18231828
"\n",
1824-
"def gzip_compress(data, compresslevel=6):\n",
1825-
" compressed = io.BytesIO()\n",
1826-
" with gzip.GzipFile(fileobj=compressed, mode=\"wb\", compresslevel=compresslevel) as compressor:\n",
1827-
" compressor.write(data)\n",
1828-
" return compressed.getvalue()\n",
1829+
"def gzip_compress(data, compresslevel=6, use_zopfli=False):\n",
1830+
" if use_zopfli:\n",
1831+
" return zopfli.gzip.compress(data, gzip_mode=compresslevel)\n",
1832+
" else:\n",
1833+
" compressed = io.BytesIO()\n",
1834+
" with gzip.GzipFile(fileobj=compressed, mode=\"wb\", compresslevel=compresslevel) as compressor:\n",
1835+
" compressor.write(data)\n",
1836+
" return compressed.getvalue()\n",
18291837
"\n",
18301838
"\n",
18311839
"all_tr_dtype_srcs = ColumnDataSource(data=dict(traces_dtype=traces.dtype.type(0)[None]))\n",
18321840
"all_tr_shape_srcs = ColumnDataSource(data=dict(traces_shape=traces.shape))\n",
18331841
"all_tr_srcs = ColumnDataSource(data=dict(\n",
18341842
" traces=numpy.frombuffer(\n",
1835-
" gzip_compress(traces.tobytes(), compresslevel=compresslevel),\n",
1843+
" gzip_compress(traces.tobytes(), compresslevel=compresslevel, use_zopfli=use_zopfli),\n",
18361844
" dtype=np.uint8\n",
18371845
" )\n",
18381846
"))\n",

0 commit comments

Comments
 (0)