Skip to content

Commit fc3c174

Browse files
committed
Cleanup chunksize and chunkslice arguments
1 parent 6b6132d commit fc3c174

2 files changed

Lines changed: 44 additions & 45 deletions

File tree

pybromo/diffusion.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ def concentration(self, pM=False):
486486
All the previously stored data in that file will be lost.
487487
"""[1:]
488488

489-
def _open_store(self, store, prefix='', path='./', chunksize=2**19,
490-
chunkslice='bytes', mode='w'):
489+
def _open_store(self, store, prefix='', path='./', mode='w'):
491490
"""Open and setup the on-disk storage file (pytables HDF5 file).
492491
493492
Low level method used to implement different stores.
@@ -498,13 +497,10 @@ def _open_store(self, store, prefix='', path='./', chunksize=2**19,
498497
Returns:
499498
Store object.
500499
"""
501-
nparams = self.numeric_params
502-
self.chunksize = chunksize
503-
nparams.update(chunksize=(chunksize, 'Chunksize for arrays'))
504500
store_fname = '%s_%s.hdf5' % (prefix, self.compact_name())
505501
attr_params = dict(particles=self.particles.to_json(), box=self.box)
506-
kwargs = dict(path=path, nparams=nparams, attr_params=attr_params,
507-
mode=mode)
502+
kwargs = dict(path=path, nparams=self.numeric_params,
503+
attr_params=attr_params, mode=mode)
508504
store_obj = store(store_fname, **kwargs)
509505
return store_obj
510506

@@ -519,8 +515,6 @@ def open_store_traj(self, path='./', chunksize=2**19, chunkslice='bytes',
519515
self.store = self._open_store(TrajectoryStore,
520516
prefix=ParticlesSimulation._PREFIX_TRAJ,
521517
path=path,
522-
chunksize=chunksize,
523-
chunkslice=chunkslice,
524518
mode=mode)
525519

526520
self.psf_pytables = self.psf.to_hdf5(self.store.h5file, '/psf')
@@ -530,13 +524,12 @@ def open_store_traj(self, path='./', chunksize=2**19, chunkslice='bytes',
530524
self.traj_group = self.store.h5file.root.trajectories
531525
self.traj_group._v_attrs['psf_name'] = self.psf.fname
532526

533-
kwargs = dict(chunksize=self.chunksize, chunkslice=chunkslice)
527+
kwargs = dict(chunksize=chunksize, chunkslice=chunkslice)
534528
self.emission_tot = self.store.add_emission_tot(**kwargs)
535529
self.emission = self.store.add_emission(**kwargs)
536530
self.position = self.store.add_position(radial=radial, **kwargs)
537531

538-
def open_store_timestamp(self, path=None, chunksize=2**19,
539-
chunkslice='bytes', mode='w'):
532+
def open_store_timestamp(self, path=None, mode='w'):
540533
"""Open and setup the on-disk storage file (pytables HDF5 file).
541534
542535
Arguments:
@@ -553,8 +546,6 @@ def open_store_timestamp(self, path=None, chunksize=2**19,
553546
self.ts_store = self._open_store(TimestampStore,
554547
prefix=ParticlesSimulation._PREFIX_TS,
555548
path=path,
556-
chunksize=chunksize,
557-
chunkslice=chunkslice,
558549
mode=mode)
559550
self.ts_group = self.ts_store.h5file.root.timestamps
560551

@@ -964,7 +955,7 @@ def simulate_timestamps_mix(self, max_rates, populations, bg_rate,
964955
timeslice (float or None): timestamps are simulated until
965956
`timeslice` seconds. If None, simulate until `self.t_max`.
966957
"""
967-
self.open_store_timestamp(chunksize=chunksize, path=path)
958+
self.open_store_timestamp(path=path)
968959
rs = self._get_group_randomstate(rs, seed, self.ts_group)
969960
if t_chunksize is None:
970961
t_chunksize = self.emission.chunkshape[1]
@@ -1084,7 +1075,7 @@ def simulate_timestamps_mix_da(self, max_rates_d, max_rates_a,
10841075
timeslice (float or None): timestamps are simulated until
10851076
`timeslice` seconds. If None, simulate until `self.t_max`.
10861077
"""
1087-
self.open_store_timestamp(chunksize=chunksize, path=path)
1078+
self.open_store_timestamp(path=path)
10881079
rs = self._get_group_randomstate(rs, seed, self.ts_group)
10891080
if t_chunksize is None:
10901081
t_chunksize = self.emission.chunkshape[1]
@@ -1206,7 +1197,7 @@ def simulate_timestamps_mix_da_online(self, max_rates_d, max_rates_a,
12061197
timeslice (float or None): timestamps are simulated until
12071198
`timeslice` seconds. If None, simulate until `self.t_max`.
12081199
"""
1209-
self.open_store_timestamp(chunksize=chunksize, path=path)
1200+
self.open_store_timestamp(path=path)
12101201
rs = self._get_group_randomstate(rs, seed, self.ts_group)
12111202
if t_chunksize is None:
12121203
t_chunksize = 2**19

pybromo/storage.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def calc_chunkshape(chunksize, shape, kind='bytes'):
4040
if chunksize is None:
4141
return None
4242

43+
divisor = 1
4344
if kind == 'bytes':
44-
divisor = 1
4545
for dimsize in shape[:-1]:
4646
divisor *= dimsize
4747

4848
if len(shape) == 1:
49-
chunkshape = (chunksize,)
49+
chunkshape = (chunksize / divisor,)
5050
elif len(shape) == 2:
5151
chunkshape = (shape[0], chunksize / divisor)
5252
elif len(shape) == 3:
@@ -89,16 +89,18 @@ def open(self):
8989
def set_sim_params(self, nparams, attr_params):
9090
"""Store parameters in `params` in `h5file.root.parameters`.
9191
92-
`nparams` (dict)
93-
A dict as returned by `get_params()` in `ParticlesSimulation()`
94-
The format is:
95-
keys:
96-
used as parameter name
97-
values: (2-elements tuple)
98-
first element is the parameter value
99-
second element is a string used as "title" (description)
100-
`attr_params` (dict)
101-
A dict whole items are stored as attributes in '/parameters'
92+
Argument:
93+
nparams (dict): a dict of parameters from
94+
`ParticlesSimulation().nparams`. The format is:
95+
96+
keys:
97+
used as parameter name
98+
values: (2-elements tuple)
99+
first element is the parameter value
100+
second element is a string used as "title" (description)
101+
102+
attr_params (dict): dict items will be stored as attributes in
103+
'/parameters'
102104
"""
103105
for name, value in nparams.items():
104106
val = value[0] if value[0] is not None else 'none'
@@ -112,24 +114,25 @@ def numeric_params(self):
112114
"""Return a dict containing all (key, values) stored in '/parameters'
113115
"""
114116
nparams = dict()
115-
for p in self.h5file.root.parameters:
116-
nparams[p.name] = p.read()
117+
for par in self.h5file.root.parameters:
118+
nparams[par.name] = par.read()
117119
return nparams
118120

119121
@property
120122
def numeric_params_meta(self):
121123
"""Return a dict with all parameters and metadata in '/parameters'.
122124
123-
This returns the same dict format as returned by get_params() method
124-
in ParticlesSimulation().
125+
This returns the same dict format as `ParticlesSimulation().nparams`.
125126
"""
126127
nparams = dict()
127-
for p in self.h5file.root.parameters:
128-
nparams[p.name] = (p.read(), p.title)
128+
for par in self.h5file.root.parameters:
129+
nparams[par.name] = (par.read(), par.title)
129130
return nparams
130131

131132

132133
class TrajectoryStore(BaseStore):
134+
"""An on-disk HDF5 store for trajectories.
135+
"""
133136
def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
134137
mode='r'):
135138
"""Return a new HDF5 file to store simulation results.
@@ -152,9 +155,9 @@ def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
152155
self.h5file.create_group('/', 'psf', 'PSFs used in the simulation')
153156

154157
def add_trajectory(self, name, overwrite=False, shape=(0,), title='',
155-
chunksize=2**19, comp_filter=default_compression,
156-
atom=tables.Float64Atom(), params=dict(),
157-
chunkslice='bytes'):
158+
chunksize=2**19, chunkslice='bytes',
159+
comp_filter=default_compression,
160+
atom=tables.Float64Atom(), params=dict()):
158161
"""Add an trajectory array in '/trajectories'.
159162
"""
160163
group = self.h5file.root.trajectories
@@ -186,25 +189,28 @@ def add_trajectory(self, name, overwrite=False, shape=(0,), title='',
186189
store_array.set_attr('creation_time', current_time())
187190
return store_array
188191

189-
def add_emission_tot(self, chunksize=2**19, comp_filter=default_compression,
190-
overwrite=False, params=dict(),
191-
chunkslice='bytes'):
192+
def add_emission_tot(self, chunksize=2**19, chunkslice='bytes',
193+
comp_filter=default_compression,
194+
overwrite=False, params=dict()):
192195
"""Add the `emission_tot` array in '/trajectories'.
193196
"""
194-
kwargs = dict(overwrite=overwrite, chunksize=chunksize, params=params,
197+
kwargs = dict(overwrite=overwrite, params=params,
198+
chunksize=chunksize, chunkslice=chunkslice,
195199
comp_filter=comp_filter, atom=tables.Float32Atom(),
196200
title='Summed emission trace of all the particles')
197201
return self.add_trajectory('emission_tot', **kwargs)
198202

199-
def add_emission(self, chunksize=2**19, comp_filter=default_compression,
200-
overwrite=False, params=dict(), chunkslice='bytes'):
203+
def add_emission(self, chunksize=2**19, chunkslice='bytes',
204+
comp_filter=default_compression,
205+
overwrite=False, params=dict()):
201206
"""Add the `emission` array in '/trajectories'.
202207
"""
203208
nparams = self.numeric_params
204209
num_particles = nparams['np']
205210

206211
return self.add_trajectory('emission', shape=(num_particles, 0),
207212
overwrite=overwrite, chunksize=chunksize,
213+
chunkslice=chunkslice,
208214
comp_filter=comp_filter,
209215
atom=tables.Float32Atom(),
210216
title='Emission trace of each particle',
@@ -224,13 +230,16 @@ def add_position(self, radial=False, chunksize=2**19, chunkslice='bytes',
224230
title = '%s position trace of each particle' % prefix
225231
return self.add_trajectory(name, shape=(num_particles, ncoords, 0),
226232
overwrite=overwrite, chunksize=chunksize,
233+
chunkslice=chunkslice,
227234
comp_filter=comp_filter,
228235
atom=tables.Float32Atom(),
229236
title=title,
230237
params=params)
231238

232239

233240
class TimestampStore(BaseStore):
241+
"""An on-disk HDF5 store for timestamps.
242+
"""
234243
def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
235244
mode='r'):
236245
"""Return a new HDF5 file to store simulation results.
@@ -248,7 +257,6 @@ def __init__(self, datafile, path='./', nparams=dict(), attr_params=dict(),
248257
attr_params=attr_params, mode=mode)
249258
if mode != 'r':
250259
if 'timestamps' not in self.h5file.root:
251-
# Create the groups
252260
self.h5file.create_group('/', 'timestamps',
253261
'Simulated timestamps')
254262

0 commit comments

Comments
 (0)