Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEP014] Simulation HDF and deprecated to_hdf cleanup. #768

Merged
merged 2 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions tardis/io/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,63 +311,6 @@ def to_hdf(self, file_path, path='', name=None, collection=None):
super(PlasmaWriterMixin, self).to_hdf(file_path, path, name)


#Deprecated
def to_hdf(path_or_buf, path, elements, complevel=9, complib='blosc'):
"""
A function to uniformly store TARDIS data
to an HDF file.

Scalars will be stored in a Series under path/scalars
1D arrays will be stored under path/property_name as distinct Series
2D arrays will be stored under path/property_name as distinct DataFrames

Units will be stored as their CGS value

Parameters
----------
path_or_buf:
Path or buffer to the HDF store
path: str
Path inside the HDF store to store the `elements`
elements: dict
A dict of property names and their values to be
stored.

Returns
-------

"""
scalars = {}
for key, value in elements.iteritems():
if hasattr(value, 'cgs'):
value = value.cgs.value
if np.isscalar(value):
scalars[key] = value
elif hasattr(value, 'shape'):
if value.ndim == 1:
# This try,except block is only for model.plasma.levels
try:
pd.Series(value).to_hdf(path_or_buf,
os.path.join(path, key))
except NotImplementedError:
pd.DataFrame(value).to_hdf(path_or_buf,
os.path.join(path, key))
else:
pd.DataFrame(value).to_hdf(path_or_buf, os.path.join(path, key))
else:
data = pd.DataFrame([value])
data.to_hdf(path_or_buf, os.path.join(path, key))

if scalars:
scalars_series = pd.Series(scalars)

# Unfortunately, with to_hdf we cannot append, so merge beforehand
scalars_path = os.path.join(path, 'scalars')
with pd.HDFStore(path_or_buf, complevel=complevel, complib=complib) as store:
if scalars_path in store:
scalars_series = store[scalars_path].append(scalars_series)
scalars_series.to_hdf(path_or_buf, os.path.join(path, 'scalars'))

'''
Code for Custom Logger Classes (ColoredFormatter and ColorLogger) and its helper function
(formatter_message) is used from this thread
Expand Down
34 changes: 4 additions & 30 deletions tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from tardis.montecarlo import MontecarloRunner
from tardis.model import Radial1DModel
from tardis.plasma.standard_plasmas import assemble_plasma

from tardis.io.util import HDFWriterMixin
# Adding logging support
logger = logging.getLogger(__name__)


class Simulation(object):
class Simulation(HDFWriterMixin):
"""A composite object containing all the required information for a
simulation.

Expand All @@ -36,6 +36,8 @@ class Simulation(object):
.. note:: TARDIS must be built with OpenMP support in order for
`nthreads` to have effect.
"""
hdf_properties = ['model', 'plasma', 'runner']
hdf_name = 'simulation'
def __init__(self, iterations, model, plasma, runner,
no_of_packets, no_of_virtual_packets, luminosity_nu_start,
luminosity_nu_end, last_no_of_packets,
Expand Down Expand Up @@ -320,34 +322,6 @@ def remove_callback(self, id):
except KeyError:
return False

def to_hdf(self, path_or_buf, path='simulation', plasma_properties=None,
suffix_count=True):
"""
Store the simulation to an HDF structure.

Parameters
----------
path_or_buf
Path or buffer to the HDF store
path : str
Path inside the HDF store to store the simulation
plasma_properties
`None` or a `PlasmaPropertyCollection` which will
be passed as the collection argument to the
plasma.to_hdf method.
suffix_count : bool
If True, the path inside the HDF will be suffixed with the
number of the iteration being stored.
Returns
-------
None
"""
if suffix_count:
path += str(self.iterations_executed)
self.runner.to_hdf(path_or_buf, path)
self.model.to_hdf(path_or_buf, path)
self.plasma.to_hdf(path_or_buf, path, plasma_properties)

@classmethod
def from_config(cls, config, **kwargs):
"""
Expand Down