跳转到主要内容

轻松解析/访问TIFFs文件夹中的一部分数据(<=6D)

项目描述

tifffolder

License: MIT

使用numpy切片语法从图像文件夹中懒加载读取数据的一部分。包括简化但健壮的文件模式匹配语法和多线程文件读取。注意:这并不是为了推广TIFFs文件夹作为存储大量信息的有用方式(如hdf5/n5/klb更可取)。但对于最初以TIFFs文件夹形式开始的数据,tifffolder简化了将该文件夹解析为沿不同轴的数据的过程(如果需要,可以作为转换为更好格式的中间步骤使用)。

使用pip安装

$ pip install tifffolder

使用conda安装

$ conda install -c talley tifffolder

用法

>>> from tifffolder import TiffFolder
>>> tf = TiffFolder('/folder/of/tiffs', patterns={'t': '_stack{d4}', 'c': '_ch{d1}'})

# get dataset shape and order of axes
>>> tf.shape
(10, 2, 65, 184, 157)  # (nt, nc, nz, ny, nx)
>>> tf.axes
'tczyx'

# reorder data  (still experimental)
>>> tf.axes = 'tzcxy'
>>> tf.shape
(10, 65, 2, 157, 184)

# data is only read from disk when explicitly indexed
# get the last 10 Z planes from every other timepoint, 
# in the first channel cropping to the middle half in Y
>>> data = tf[::2, 0, -10:, tf.shape[-2] * 1 // 4 : tf.shape[-2] * 3 // 4 ]
>>> data.shape
(5, 10, 92, 157)   # (nt, nz, ny, nx)


# Can also be used as an iterator/generator for lazily reading data
>>> for timepoint in tf:
>>>     do_something(timepoint)
     
# or just load the whole thing
>>> alldata = tf.asarray()
>>> alldata.shape == tf.shape
True

# asarray() also accepts any axis kwargs
>>> somedata = tf.asarray(t=range(1,10), c=0)

# Or just to select filenames along certain axes:
>>> tf.select_filenames(t=range(1,10,2), c=0)
['./test_ch0_stack0001_488nm.tif',
 './test_ch0_stack0003_488nm.tif',
 './test_ch0_stack0005_488nm.tif',
 './test_ch0_stack0007_488nm.tif',
 './test_ch0_stack0009_488nm.tif']

指定文件名模式

tifffolder将简化的regex语法转换为相对健壮的lookahead regex,它将按任何顺序匹配文件名中的模式或优雅地失败。

The TiffFolder类接受一个patterns参数(字典或二元组列表)。对于patterns字典中的每个(键,值)

  • 键 = 轴名称(例如 'x', 'y', 'z', 'c', 't', 's'
  • 值 = 简化的regex,其中
    • 括号内的内容将被捕获
    • 括号外的字符将需要匹配,但不会被捕获
    • {d} 表示匹配任意数量的数字
    • {D} 表示匹配任意数量的非数字
    • {} 表示匹配任意字母数字字符(不包括下划线)
    • {d2} 表示匹配确切的两个数字(例如)

例如

>>> patterns = {
    'rel': '_{d7}msec',
    'w': '_{d3}nm',
    't': '_stack{d4}',
    'c': '_ch{d1}',
    'cam': 'Cam{D1}'
}
>>> tf = TiffFolder('/folder/of/tiffs', patterns)
>>> tf._parse_filename('cell1_ch0_stack0009_488nm_0034829msec.tif')
{'rel': 34829, 'w': 488, 't': 9, 'c': 0, 'cam': None}

>>> tf._parse_filename('cell1_CamA_ch2_stack0001_560nm_0034829msec.tif')
{'rel': 34829, 'w': 560, 't': 1, 'c': 2, 'cam': 'A'}

>>> tifffolder.build_regex('cam', 'Cam{}')
'(?=.*Cam(?P<cam>[a-zA-Z0-9]+))?'

>>> tifffolder.build_regex('c', '_ch{d1}')
'(?=.*_ch(?P<c>\\d{1}))?'
待办事项
  • 检查轴重排序
  • 检查文件间的z平面

支持者