forked from manoharan-lab/camera-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_h5.py
executable file
·79 lines (68 loc) · 2.6 KB
/
compress_h5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2014, Thomas G. Dimiduk, Rebecca W. Perry, Aaron Goldfain
#
# This file is part of Camera Controller
#
# Camera Controller is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HoloPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HoloPy. If not, see <http://www.gnu.org/licenses/>.
'''Compress hdf5 files from the camera controllers raw dump format
into a single dataset
For speed, the camera writes out timeseries without compression and
with each image as an individual dataset in the file. This module is
then called in a seperate process to compress the data.
.. moduleauthor:: Thomas G. Dimiduk <tom@dimiduk.net>
'''
from __future__ import division
import h5py
import sys
import numpy as np
import os
def compress_h5(name, delete=False, progress=False):
parts = name.split('.')
if parts[-2] != 'uncompressed':
print("file name should be of the form something.uncompressed.h5")
return
outname = '.'.join(parts[:-2] + parts[-1:])
inf = h5py.File(name)
shape2d = inf['1'].shape
dtype = inf['1'].dtype
n_frames = len(inf.keys())
shape = shape2d + (n_frames,)
chunk = min(n_frames, 100)
outf = h5py.File(outname, 'w')
outf.create_dataset('images', shape, chunks=(64, 64, chunk),
compression='gzip', dtype=dtype)
buffer = np.zeros(shape2d+(chunk,), dtype=dtype)
block = 0
while (block+1)*chunk <= n_frames:
for i in range(chunk):
buffer[...,i] = inf[str(block*chunk+i)]
outf['images'][...,block*chunk:(block+1)*chunk] = buffer
block += 1
if progress:
print("{}%".format(block*chunk/n_frames * 100))
# finish a partial chunk if the chunk size does not evenly divide n_frames
if block*chunk < n_frames:
partial = n_frames - block*chunk
buffer = np.zeros(shape2d+(partial,), dtype=dtype)
for i in range(partial):
buffer[...,i] = inf[str(block*chunk+i)]
outf['images'][...,block*chunk:] = buffer
inf.close()
if delete:
os.remove(name)
outf.close()
if __name__ == '__main__':
name = sys.argv[1]
compress_h5(name, progress=True)