/
storage.py
269 lines (236 loc) · 10.7 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
# -*- coding: utf-8 -*-
"""
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
"""
import tables
import numpy as np
# Compression filter used by default for arrays
default_compression = tables.Filters(complevel=6, complib='blosc')
class Storage(object):
def __init__(self, fname, nparams=dict(), attr_params=dict(),
overwrite=True):
"""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 `oldfile=False` (default) `fname` will be overwritten (if exists).
"""
if not overwrite:
# Create a new empty file
self.data_file = tables.open_file(fname, mode = "a")
else:
# Create a new empty file
self.data_file = tables.open_file(fname, mode = "w",
title = "Brownian motion simulation")
# Create the groups
self.data_file.create_group('/', 'trajectories',
'Simulated trajectories')
self.data_file.create_group('/', 'parameters',
'Simulation parameters')
self.data_file.create_group('/', 'psf',
'PSFs used in the simulation')
self.data_file.create_group('/', 'timestamps',
'Timestamps of emitted photons')
# Set the simulation parameters
self.set_sim_params(nparams, attr_params)
def close(self):
self.data_file.close()
def open(self):
"""Reopen a file after has been closed (uses the store filename)."""
self.__init__(self.data_file.filename, overwrite=False)
def set_sim_params(self, nparams, attr_params):
"""Store parameters in `params` in `data_file.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.iteritems():
val = value[0] if value[0] is not None else 'none'
self.data_file.create_array('/parameters', name, obj=val,
title=value[1])
for name, value in attr_params.items():
self.data_file.set_node_attr('/parameters', name, value)
def get_sim_nparams(self):
"""Return a dict containing all (key, values) stored in '/parameters'
"""
nparams = dict()
for p in self.data_file.root.parameters:
nparams[p.name] = p.read()
return nparams
def get_sim_nparams_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.data_file.root.parameters:
nparams[p.name] = (p.read(), p.title)
return nparams
def add_timestamps(self, name, clk_p, max_rate, bg_rate,
num_particles, bg_particle,
overwrite=False, chunksize=2**16,
comp_filter=default_compression):
if name in self.data_file.root.timestamps:
if overwrite:
self.data_file.remove_node('/timestamps', name=name)
self.data_file.remove_node('/timestamps', name=name+'_par')
else:
raise ValueError('Timestam array already exist (%s)' % name)
times_array = self.data_file.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)
particles_array = self.data_file.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
def add_trajectory(self, name, overwrite=False, shape=(0,), title='',
chunksize=2**19, comp_filter=default_compression,
atom=tables.Float64Atom(), params=dict()):
"""Add an trajectory array in '/trajectories'.
"""
group = self.data_file.root.trajectories
if name in group:
print "%s already exists ..." % name,
if overwrite:
self.data_file.remove_node(group, name)
print " deleted."
else:
print " old returned."
return group.get_node(name)
nparams = self.get_sim_nparams()
num_t_steps = nparams['t_max']/nparams['t_step']
if chunksize is None:
chunkshape = None
elif len(shape) == 1:
chunkshape = (chunksize,)
elif len(shape) == 2:
chunkshape = (shape[0], chunksize/shape[0],)
elif len(shape) == 3:
chunkshape = (shape[0], shape[1], chunksize/(shape[0]*shape[1]),)
store_array = self.data_file.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()):
"""Add the `emission_tot` array in '/trajectories'.
"""
return self.add_trajectory('emission_tot', overwrite=overwrite,
chunksize=chunksize, comp_filter=comp_filter,
atom=tables.Float32Atom(),
title = 'Summed emission trace of all the particles',
params=params)
def add_emission(self, chunksize=2**19, comp_filter=default_compression,
overwrite=False, params=dict()):
"""Add the `emission` array in '/trajectories'.
"""
nparams = self.get_sim_nparams()
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()):
"""Add the `position` array in '/trajectories'.
"""
nparams = self.get_sim_nparams()
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)
def add_timetrace_tot(self, chunksize=2**19,
comp_filter=default_compression,
overwrite=False):
"""Add the `timetrace_tot` array in '/trajectories'.
"""
return self.add_trajectory('timetrace_tot', overwrite=overwrite,
chunksize=chunksize, comp_filter=comp_filter,
atom=tables.UInt8Atom(),
title = 'Timetrace of emitted photons with bin = t_step')
def add_timetrace(self, chunksize=2**19, comp_filter=default_compression,
overwrite=False):
"""Add the `timetrace` array in '/trajectories'.
"""
group = self.data_file.root.trajectories
nparams = self.get_sim_nparams()
num_particles = nparams['np']
num_t_steps = nparams['t_max']/nparams['t_step']
dt = np.dtype([('counts', 'u1')])
timetrace_p = []
for particle in xrange(num_particles):
name = 'timetrace_p' + str(particle)
if name in group:
print "%s already exists ..." % name,
if overwrite:
self.data_file.remove_node(group, name)
print " deleted."
else:
print " using the old one."
timetrace_p.append(group.get_node(name))
continue
timetrace_p.append(
self.data_file.create_table(
group, name, description=dt, chunkshape=chunksize,
expectedrows=num_t_steps,
title='Binned timetrace of emitted ph (bin = t_step)'
' - particle_%d' % particle)
)
return timetrace_p
if __name__ == '__main__':
store = Storage('h2.h5', {'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)')})
# em_tot_array = add_em_tot_array(hf)
# em_array = add_em_array(hf)
#
# #%%timeit -n1 -r1
# for i in xrange(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()
#