跳转到主要内容

电生理时间序列的无损压缩

项目描述

Python中的多通道时间序列无损压缩

Build Status Coverage Status

此库实现了一种简单的无损压缩方案,适用于时间依赖性高频、高维信号。它正在国际脑实验室内开发,旨在成为所有基于Neuropixels的大型电生理记录使用的压缩库。信号通常以30 kHz和10位深度记录,包含数百个通道。

压缩方案

对于压缩方案的要求如下

  • 仅无损压缩(应该能够检索到字节到字节的精确解压缩数据)。
  • 使用纯Python(无C扩展)编写,依赖性最小,以简化分发。
  • 可扩展到高采样率、大量通道、长录音时间。
  • 比实时速度快(即压缩所需时间应少于录音所需时间)。
  • 多线程以利用多个CPU核心。
  • 即时解压缩和随机读取访问。
  • 尽可能简单。

压缩方案如下

  • 数据沿着时间轴分割成块。
  • 计算所有通道的时间差。
  • 使用zlib对这些时间差进行压缩。
  • 将压缩后的块(以及每个块的初始值)追加到二进制文件中。
  • 关于压缩的元数据,包括压缩二进制文件中的块偏移量,保存在辅助的JSON文件中。

保存偏移量允许即时解压缩和随机数据访问:只需确定应加载哪些块,然后直接从压缩的二进制文件中加载它们。压缩块使用zlib解压缩,并使用累积和(时间差操作的逆)恢复原始数据。

在大规模神经生理记录中,我们实现了3倍的压缩比。

作为一致性检查,默认情况下,压缩文件会自动透明地解压缩并与原始文件逐字节比较。

依赖项

  • Python 3.7+
  • NumPy
  • tqdm [用于进度条]

仅用于开发

  • flake8
  • pytest
  • pytest-cov
  • coverage

安装

pip install mtscomp

命令行界面

示例

# Compression: specify the number of channels, sample rate, dtype, optionally save the parameters
# as default in ~/.mtscomp with --set-default
mtscomp data.bin -n 385 -s 30000 -d int16 [--set-default]
# Decompression
mtsdecomp data.cbin -o data.decomp.bin

用法

usage: mtscomp [-h] [-d DTYPE] [-s SAMPLE_RATE] [-n N_CHANNELS] [-p CPUS]
               [-c CHUNK] [-nc] [-v] [--set-default]
               path [out] [outmeta]

Compress a raw binary file.

positional arguments:
  path                  input path of a raw binary file
  out                   output path of the compressed binary file (.cbin)
  outmeta               output path of the compression metadata JSON file
                        (.ch)

optional arguments:
  -h, --help            show this help message and exit
  -d DTYPE, --dtype DTYPE
                        data type
  -s SAMPLE_RATE, --sample-rate SAMPLE_RATE
                        sample rate
  -n N_CHANNELS, --n-channels N_CHANNELS
                        number of channels
  -p CPUS, --cpus CPUS  number of CPUs to use
  -c CHUNK, --chunk CHUNK
                        chunk duration
  -nc, --no-check       no check
  -v, --debug           verbose
  --set-default         set the specified parameters as the default



usage: mtsdecomp [-h] [-o [OUT]] [--overwrite] [-nc] [-v] cdata [cmeta]

Decompress a raw binary file.

positional arguments:
  cdata                 path to the input compressed binary file (.cbin)
  cmeta                 path to the input compression metadata JSON file (.ch)

optional arguments:
  -h, --help            show this help message and exit
  -o [OUT], --out [OUT]
                        path to the output decompressed file (.bin)
  --overwrite, -f       overwrite existing output
  -nc, --no-check       no check
  -v, --debug           verbose

高级API

示例

import numpy as np
from mtscomp.mtscomp import compress, decompress

# Compress a .bin file into a pair .cbin (compressed binary file) and .ch (JSON file).
compress('data.bin', 'data.cbin', 'data.ch', sample_rate=20000., n_channels=256, dtype=np.int16)
# Decompress a pair (.cbin, .ch) and return an object that can be sliced like a NumPy array.
arr = decompress('data.cbin', 'data.ch')
X = arr[start:end, :]  # decompress the data on the fly directly from the file on disk
arr.close()  # Close the file when done

低级API

示例

import numpy as np
from mtscomp import Writer, Reader

# Define a writer to compress a flat raw binary file.
w = Writer(chunk_duration=1.)
# Open the file to compress.
w.open('data.bin', sample_rate=20000., n_channels=256, dtype=np.int16)
# Compress it into a compressed binary file, and a JSON header file.
w.write('data.cbin', 'data.ch')
w.close()

# Define a reader to decompress a compressed array.
r = Reader()
# Open the compressed dataset.
r.open('data.cbin', 'data.ch')
# The reader can be sliced as a NumPy array: decompression happens on the fly. Only chunks
# that need to be loaded are loaded and decompressed.
# Here, we load everything in memory.
array = r[:]
# Or we can decompress into a new raw binary file on disk.
r.tofile('data_dec.bin')
r.close()

实现细节

  • 多线程:由于Python的zlib释放了GIL,库在压缩文件时使用多个线程。块被分组到包含与线程数量相同的块的批次中。在每个批次之后,块按正确顺序写入二进制文件(因为批次的线程没有理由按顺序完成)。

性能

在Neuropixels数据集(30 kHz,385通道)和Intel 10核i9-9820X CPU @ 3.3GHz上的性能

  • 压缩比:-63%(压缩文件几乎缩小3倍)
  • 压缩时间(20线程):88 MB/s,比实时快4倍
  • 解压缩时间(目前为单线程):22 MB/s,比实时快3倍

项目详情


下载文件

下载适合您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分发

mtscomp-1.0.2.tar.gz (16.0 kB 查看哈希值)

上传时间:

构建分发

mtscomp-1.0.2-py2.py3-none-any.whl (16.4 kB 查看哈希值)

上传时间: Python 2 Python 3

支持者