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

from_hdf5 function (TEP014) #711

Closed
wants to merge 8 commits into from
Closed
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
60 changes: 60 additions & 0 deletions tardis/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,63 @@ def from_config(cls, config):
dilution_factor=None,
v_boundary_inner=structure.get('v_inner_boundary', None),
v_boundary_outer=structure.get('v_outer_boundary', None))

@classmethod
def from_hdf(cls,path,h5_file,file_path):
"""
This function returns a Radial1DModel object
from given HDF5 File.

Parameters
----------
path : 'str'
Path to transverse in hdf file
h5_file : 'h5py.File'
Given HDF5 file
file_path : 'str'
Path of Simulation generated HDF file

Returns
-------
model : `~Radial1DModel`
"""

if not h5_file:
raise ValueError("h5_file Parameter can`t be None")

model_path = path + '/model'
plasma_path = path + '/plasma'
model = {}
plasma = {}
plasma_keys = ['abundance', 't_rad', 'scalars']

with pd.HDFStore(file_path, 'r') as data:
for key in h5_file[model_path].keys():
model[key] = {}
buff_path = model_path + '/' + key + '/'
model[key] = data[buff_path]

for key in h5_file[plasma_path].keys():
if key in plasma_keys:
plasma[key] = {}
buff_path = plasma_path + '/' + key + '/'
plasma[key] = data[buff_path]

#Creates corresponding astropy.units.Quantity objects
abundance = plasma['abundance']
time_explosion = plasma['scalars']['time_explosion'] * u.s
t_inner = model['scalars']['t_inner'] * u.K
t_radiative = np.array(plasma['t_rad']) * u.K
v_boundary_inner = model['v_inner'][0] * u.cm / u.s
v_boundary_outer = model['v_outer'][
len(model['v_outer']) - 1] * u.cm / u.s
dilution_factor = np.array(model['w'])
velocity = np.append(model['v_inner'],
v_boundary_outer.value) * u.cm / u.s

# Presently homologous_density and luminosity_requested parameters are
# set to None
return Radial1DModel(velocity, None, abundance, time_explosion,
t_inner, None, t_radiative,
dilution_factor, v_boundary_inner,
v_boundary_outer)
34 changes: 33 additions & 1 deletion tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import pandas as pd
from astropy import units as u
from collections import OrderedDict

import h5py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pandas should be used instead of h5py.

Copy link
Contributor Author

@vg3095 vg3095 Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now, pandas.HDFStore() does not support iterating over hierarchically , while h5py does.
The issue is still opened there . Link.

So, I am using h5py only for transversing HDF file hierarchically, and for reading particular attribute (like time_explosion) , I am using pd.HDFStore()

from tardis.montecarlo import MontecarloRunner
from tardis.model import Radial1DModel
from tardis.plasma.standard_plasmas import assemble_plasma
import os

# Adding logging support
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -410,3 +411,34 @@ def from_config(cls, config, **kwargs):
convergence_strategy=config.montecarlo.convergence_strategy,
nthreads=config.montecarlo.nthreads)

@classmethod
def from_hdf(cls, file_path):
"""
This function converts a hdf5 file to a Radial1DModel Object.

Parameters
----------

file_path : `str`
Path to Simulation generated hdf file

Returns
-------

model : `~Radial1DModel`
"""

if file_path is None:
raise ValueError("File Path can`t be None")

with h5py.File(file_path, 'r') as h5_file:
for simulation in h5_file.keys():
for key in h5_file[simulation]:
if 'model' in key:
model = Radial1DModel.from_hdf(
simulation, h5_file, file_path)

# TODO : Extend it to plasma and montecarlo objects and return Simulation object
# As of now , it cannot return Simulation object , as convergence_strategy cannot
# be set to None , at the time of initialization
return model