forked from tritemio/PyBroMo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.py
290 lines (245 loc) · 10.9 KB
/
storage.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#
# PyBroMo - A single molecule diffusion simulator in confocal geometry.
#
# Copyright (C) 2013-2015 Antonino Ingargiola tritemio@gmail.com
#
"""
This module implements functions to store simulation results to a file.
The module uses the HDF5 file format through the PyTables library.
File part of PyBroMo: a single molecule diffusion simulator.
Copyright (C) 2013-2014 Antonino Ingargiola tritemio@gmail.com
"""
from __future__ import print_function, absolute_import, division
from builtins import range, zip, dict
from pathlib import Path
import tables
# Compression filter used by default for arrays
default_compression = tables.Filters(complevel=5, complib='blosc')
class BaseStore(object):
@staticmethod
def calc_chunkshape(chunksize, shape, kind='bytes'):
assert kind in ['times', 'bytes']
if chunksize is None:
return None
if kind == 'bytes':
divisor = 1
for dimsize in shape[:-1]:
divisor *= dimsize
if len(shape) == 1:
chunkshape = (chunksize,)
elif len(shape) == 2:
chunkshape = (shape[0], chunksize / divisor)
elif len(shape) == 3:
chunkshape = (shape[0], shape[1], chunksize / divisor)
return chunkshape
def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
mode='r'):
"""Return a new HDF5 file to store simulation results.
The HDF5 file has two groups:
'/parameters'
containing all the simulation numeric-parameters
If `mode='w'`, `datafile` will be overwritten (if exists).
"""
if isinstance(datafile, Path):
self.filepath = datafile
else:
assert Path(path).exists()
self.filepath = Path(path, datafile)
self.h5file = tables.open_file(str(self.filepath), mode=mode)
self.filename = str(self.filepath)
if mode != 'r':
self.h5file.title = "PyBroMo simulation file"
# Create the groups
self.h5file.create_group('/', 'parameters', 'Simulation parameters')
# Set the simulation parameters
self.set_sim_params(nparams, attr_params)
def close(self):
self.h5file.close()
def open(self):
"""Reopen a file after has been closed (uses the store filename)."""
self.__init__(self.h5file.filename, mode='r')
def set_sim_params(self, nparams, attr_params):
"""Store parameters in `params` in `h5file.root.parameters`.
`nparams` (dict)
A dict as returned by `get_params()` in `ParticlesSimulation()`
The format is:
keys:
used as parameter name
values: (2-elements tuple)
first element is the parameter value
second element is a string used as "title" (description)
`attr_params` (dict)
A dict whole items are stored as attributes in '/parameters'
"""
for name, value in nparams.items():
val = value[0] if value[0] is not None else 'none'
self.h5file.create_array('/parameters', name, obj=val,
title=value[1])
for name, value in attr_params.items():
self.h5file.set_node_attr('/parameters', name, value)
@property
def numeric_params(self):
"""Return a dict containing all (key, values) stored in '/parameters'
"""
nparams = dict()
for p in self.h5file.root.parameters:
nparams[p.name] = p.read()
return nparams
@property
def numeric_params_meta(self):
"""Return a dict with all parameters and metadata in '/parameters'.
This returns the same dict format as returned by get_params() method
in ParticlesSimulation().
"""
nparams = dict()
for p in self.h5file.root.parameters:
nparams[p.name] = (p.read(), p.title)
return nparams
class TrajectoryStore(BaseStore):
def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
mode='r'):
"""Return a new HDF5 file to store simulation results.
The HDF5 file has two groups:
'/parameters'
containing all the simulation numeric-parameters
'/trajectories'
containing simulation trajectories (positions, emission traces)
If `mode='w'`, `datafile` will be overwritten (if exists).
"""
super().__init__(datafile, path=path, nparams=nparams,
attr_params=attr_params, mode=mode)
if mode != 'r':
# Create the groups
self.h5file.create_group('/', 'trajectories',
'Simulated trajectories')
self.h5file.create_group('/', 'psf', 'PSFs used in the simulation')
def add_trajectory(self, name, overwrite=False, shape=(0,), title='',
chunksize=2**19, comp_filter=default_compression,
atom=tables.Float64Atom(), params=dict(),
chunkslice='bytes'):
"""Add an trajectory array in '/trajectories'.
"""
group = self.h5file.root.trajectories
if name in group:
print("%s already exists ..." % name, end='')
if overwrite:
self.h5file.remove_node(group, name)
print(" deleted.")
else:
print(" old returned.")
return group.get_node(name)
nparams = self.numeric_params
num_t_steps = nparams['t_max'] / nparams['t_step']
chunkshape = self.calc_chunkshape(chunksize, shape, kind=chunkslice)
store_array = self.h5file.create_earray(
group, name, atom=atom,
shape = shape,
chunkshape = chunkshape,
expectedrows = num_t_steps,
filters = comp_filter,
title = title)
# Set the array parameters/attributes
for key, value in params.items():
store_array.set_attr(key, value)
return store_array
def add_emission_tot(self, chunksize=2**19, comp_filter=default_compression,
overwrite=False, params=dict(),
chunkslice='bytes'):
"""Add the `emission_tot` array in '/trajectories'.
"""
kwargs = dict(overwrite=overwrite, chunksize=chunksize, params=params,
comp_filter=comp_filter, atom=tables.Float32Atom(),
title='Summed emission trace of all the particles')
return self.add_trajectory('emission_tot', **kwargs)
def add_emission(self, chunksize=2**19, comp_filter=default_compression,
overwrite=False, params=dict(), chunkslice='bytes'):
"""Add the `emission` array in '/trajectories'.
"""
nparams = self.numeric_params
num_particles = nparams['np']
return self.add_trajectory('emission', shape=(num_particles, 0),
overwrite=overwrite, chunksize=chunksize,
comp_filter=comp_filter,
atom=tables.Float32Atom(),
title='Emission trace of each particle',
params=params)
def add_position(self, chunksize=2**19, comp_filter=default_compression,
overwrite=False, params=dict(), chunkslice='bytes'):
"""Add the `position` array in '/trajectories'.
"""
nparams = self.numeric_params
num_particles = nparams['np']
return self.add_trajectory('position', shape=(num_particles, 3, 0),
overwrite=overwrite, chunksize=chunksize,
comp_filter=comp_filter,
atom=tables.Float32Atom(),
title='3-D position trace of each particle',
params=params)
class TimestampStore(BaseStore):
def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
mode='r'):
"""Return a new HDF5 file to store simulation results.
The HDF5 file has two groups:
'/parameters'
containing all the simulation numeric-parameters
'/timestamps'
containing simulated timestamps
If `overwrite=True` (default) `datafile` is overwritten (if exists).
"""
super().__init__(datafile, path=path, nparams=nparams,
attr_params=attr_params, mode=mode)
if mode != 'r':
# Create the groups
self.h5file.create_group('/', 'timestamps', 'Simulated timestamps')
def add_timestamps(self, name, clk_p, max_rate, bg_rate,
num_particles, bg_particle, populations=None,
overwrite=False, chunksize=2**16,
comp_filter=default_compression):
if name in self.h5file.root.timestamps:
if overwrite:
self.h5file.remove_node('/timestamps', name=name)
self.h5file.remove_node('/timestamps', name=name + '_par')
else:
raise ValueError('Timestamp array already exist (%s)' % name)
times_array = self.h5file.create_earray(
'/timestamps', name, atom=tables.Int64Atom(),
shape = (0,),
chunkshape = (chunksize,),
filters = comp_filter,
title = 'Simulated photon timestamps')
times_array.set_attr('clk_p', clk_p)
times_array.set_attr('max_rate', max_rate)
times_array.set_attr('bg_rate', bg_rate)
times_array.set_attr('populations', populations)
particles_array = self.h5file.create_earray(
'/timestamps', name + '_par', atom=tables.UInt8Atom(),
shape = (0,),
chunkshape = (chunksize,),
filters = comp_filter,
title = 'Particle number for each timestamp')
particles_array.set_attr('num_particles', num_particles)
particles_array.set_attr('bg_particle', bg_particle)
return times_array, particles_array
if __name__ == '__main__':
d = {'D': (1.2e-11, 'Diffusion coefficient (m^2/s)'),
'EID': (0, 'IPython engine ID (int)'),
'ID': (0, 'Simulation ID (int)'),
'np': (40, 'Number of simulated particles'),
'pico_mol': (86.4864063019005, 'Particles concentration (pM)'),
't_max': (0.1, 'Simulation total time (s)'),
't_step': (5e-07, 'Simulation time-step (s)')}
store = TrajectoryStore('h2.h5', d)
# em_tot_array = add_em_tot_array(hf)
# em_array = add_em_array(hf)
#
# #%%timeit -n1 -r1
# for i in range(0, int(n_rows/chunksize)):
# em_tot_array.append(np.random.rand(chunksize))
# em_tot_array.flush()
#
#
# #%%timeit -n1 -r1
# for i in xrange(0, int(n_rows/chunksize)):
# em_array.append(np.random.rand(chunksize, num_particles))
# em_array.flush()
#