<s> from weakref import ref <EOL> import numpy as np <EOL> from openmdao . main . api import VariableTree <EOL> from openmdao . lib . casehandlers . query import DictList , ListResult <EOL> _GLOBAL_DICT = dict ( __builtins__ = None ) <EOL> class CaseDatasetHDF5 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , format ) : <EOL> format = format . lower ( ) <EOL> if format == '<STR_LIT>' : <EOL> self . _reader = _HDF5Reader ( filename ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _query_id = self . _query_itername = self . _parent_id = self . _parent_itername = self . _driver_id = self . _driver_name = None <EOL> self . _case_ids = self . _drivers = self . _case_iternames = None <EOL> self . metadata_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ property <EOL> def data ( self ) : <EOL> """<STR_LIT>""" <EOL> return QueryHDF5 ( self ) <EOL> @ property <EOL> def drivers ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _reader . drivers ( ) <EOL> @ property <EOL> def simulation_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _reader . simulation_info <EOL> def _fetch ( self , query ) : <EOL> """<STR_LIT>""" <EOL> self . _setup ( query ) <EOL> if query . vnames : <EOL> tmp = [ ] <EOL> for name in self . metadata_names : <EOL> if name in query . vnames : <EOL> tmp . append ( name ) <EOL> self . metadata_names = tmp <EOL> names = query . vnames <EOL> else : <EOL> if query . driver_name : <EOL> driver_info = self . _drivers [ self . _driver_name ] <EOL> prefix = driver_info [ '<STR_LIT>' ] <EOL> all_names = [ prefix + name <EOL> for name in driver_info [ '<STR_LIT>' ] ] <EOL> else : <EOL> all_names = [ ] <EOL> for driver_info in self . _drivers . values ( ) : <EOL> prefix = driver_info [ '<STR_LIT>' ] <EOL> all_names . extend ( [ prefix + name <EOL> for name in driver_info [ '<STR_LIT>' ] ] ) <EOL> names = sorted ( all_names + self . metadata_names ) <EOL> if query . names : <EOL> return names <EOL> nan = float ( '<STR_LIT>' ) <EOL> rows = ListResult ( ) <EOL> state = { } <EOL> for case_data in self . _reader . cases ( ) : <EOL> data = case_data [ '<STR_LIT:data>' ] <EOL> metadata = case_data [ '<STR_LIT>' ] <EOL> case_id = metadata [ '<STR_LIT>' ] <EOL> case_driver_id = metadata [ '<STR_LIT>' ] <EOL> case_driver_name = metadata [ '<STR_LIT>' ] <EOL> case_itername = metadata [ '<STR_LIT>' ] <EOL> prefix = self . _drivers [ case_driver_name ] [ '<STR_LIT>' ] <EOL> if prefix : <EOL> pass <EOL> else : <EOL> data = data . copy ( ) <EOL> state . update ( data ) <EOL> if self . _driver_name is not None and case_driver_name != self . _driver_name : <EOL> continue <EOL> if self . _case_iternames is None or case_itername in self . _case_iternames : <EOL> for name in self . metadata_names : <EOL> data [ name ] = case_data [ '<STR_LIT>' ] [ name ] <EOL> row = DictList ( names ) <EOL> for name in names : <EOL> if query . local_only : <EOL> if name in self . metadata_names : <EOL> row . append ( data [ name ] ) <EOL> else : <EOL> driver = self . _drivers [ case_driver_name ] <EOL> lnames = [ prefix + rec for rec in driver [ '<STR_LIT>' ] ] <EOL> if name in lnames : <EOL> row . append ( data [ name ] ) <EOL> else : <EOL> row . append ( nan ) <EOL> elif name in state : <EOL> row . append ( state [ name ] ) <EOL> elif name in data : <EOL> row . append ( data [ name ] ) <EOL> else : <EOL> row . append ( nan ) <EOL> rows . append ( row ) <EOL> if case_itername == self . _query_itername or case_itername == self . _parent_itername : <EOL> break <EOL> if self . _query_id and not rows : <EOL> raise ValueError ( '<STR_LIT>' % self . _query_id ) <EOL> if query . transpose : <EOL> tmp = DictList ( names ) <EOL> for i in range ( len ( rows [ <NUM_LIT:0> ] ) ) : <EOL> tmp . append ( [ row [ i ] for row in rows ] ) <EOL> tmp . cds = self <EOL> return tmp <EOL> rows . cds = self <EOL> return rows <EOL> def _write ( self , query , out , format ) : <EOL> raise NotImplementedError <EOL> def _setup ( self , query ) : <EOL> """<STR_LIT>""" <EOL> if query . vnames is not None : <EOL> bad = [ ] <EOL> metadata = self . simulation_info [ '<STR_LIT>' ] <EOL> expressions = self . simulation_info [ '<STR_LIT>' ] <EOL> for name in query . vnames : <EOL> if name not in metadata and name not in [ e [ '<STR_LIT>' ] for e in expressions . values ( ) ] and name not in self . metadata_names : <EOL> bad . append ( name ) <EOL> if bad : <EOL> raise RuntimeError ( '<STR_LIT>' % bad ) <EOL> self . _drivers = { } <EOL> self . _driver_id = None <EOL> self . _driver_name = None <EOL> for driver_info in self . _reader . drivers ( ) : <EOL> _id = driver_info [ '<STR_LIT>' ] <EOL> name = driver_info [ '<STR_LIT:name>' ] <EOL> prefix , _ , name = name . rpartition ( '<STR_LIT:.>' ) <EOL> if prefix : <EOL> prefix += '<STR_LIT:.>' <EOL> driver_info [ '<STR_LIT>' ] = prefix <EOL> self . _drivers [ driver_info [ '<STR_LIT:name>' ] ] = driver_info <EOL> if ( driver_info [ '<STR_LIT:name>' ] ) == query . driver_name : <EOL> self . _driver_name = query . driver_name <EOL> if query . driver_name : <EOL> if self . _driver_name is None : <EOL> raise ValueError ( '<STR_LIT>' % query . driver_name ) <EOL> self . _case_ids = None <EOL> self . _query_id = None <EOL> self . _parent_id = None <EOL> if query . case_itername is not None : <EOL> self . _query_itername = query . case_itername <EOL> self . _case_iternames = set ( ( self . _query_itername , ) ) <EOL> self . _driver_name = None <EOL> elif query . parent_itername is not None : <EOL> self . _parent_itername = query . parent_itername <EOL> self . _case_iternames = set ( ( self . _parent_itername , ) ) <EOL> parent_itername_parts = self . _parent_itername . split ( '<STR_LIT:->' ) <EOL> for case_data in self . _reader . cases ( ) : <EOL> itername = case_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> itername_parts = itername . split ( '<STR_LIT:->' ) <EOL> if len ( parent_itername_parts ) + <NUM_LIT:1> == len ( itername_parts ) and itername_parts [ : - <NUM_LIT:1> ] == parent_itername_parts : <EOL> self . _case_iternames . add ( itername ) <EOL> def restore ( self , assembly , case_id ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _set ( self , assembly , name , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , dict ) : <EOL> curr = assembly . get ( name ) <EOL> if isinstance ( curr , VariableTree ) : <EOL> for key , val in value . items ( ) : <EOL> self . _set ( assembly , '<STR_LIT:.>' . join ( ( name , key ) ) , val ) <EOL> elif '<STR_LIT:[>' in name : <EOL> if isinstance ( value , unicode ) : <EOL> value = str ( value ) <EOL> exec ( '<STR_LIT>' % name , _GLOBAL_DICT , locals ( ) ) <EOL> else : <EOL> for key , val in value . items ( ) : <EOL> if isinstance ( val , unicode ) : <EOL> value [ key ] = str ( val ) <EOL> assembly . set ( name , value ) <EOL> else : <EOL> if isinstance ( value , unicode ) : <EOL> value = str ( value ) <EOL> if '<STR_LIT:[>' in name : <EOL> exec ( '<STR_LIT>' % name , _GLOBAL_DICT , locals ( ) ) <EOL> else : <EOL> assembly . set ( name , value ) <EOL> class QueryHDF5 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dataset ) : <EOL> self . _dataset = dataset <EOL> self . driver_name = None <EOL> self . case_id = None <EOL> self . case_itername = None <EOL> self . parent_id = None <EOL> self . parent_itername = None <EOL> self . vnames = None <EOL> self . local_only = False <EOL> self . names = False <EOL> self . transpose = False <EOL> def fetch ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dataset . _fetch ( self ) <EOL> def write ( self , out , format = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def driver ( self , driver_name ) : <EOL> """<STR_LIT>""" <EOL> self . driver_name = driver_name <EOL> return self <EOL> def case ( self , case_itername ) : <EOL> """<STR_LIT>""" <EOL> self . case_itername = case_itername <EOL> self . parent_itername = None <EOL> return self <EOL> def parent_case ( self , parent_case_id ) : <EOL> """<STR_LIT>""" <EOL> self . parent_id = parent_case_id <EOL> self . parent_itername = parent_case_id <EOL> self . case_id = None <EOL> return self <EOL> def vars ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . vnames = [ ] <EOL> for arg in args : <EOL> if isinstance ( arg , basestring ) : <EOL> self . vnames . append ( arg ) <EOL> else : <EOL> self . vnames . extend ( arg ) <EOL> return self <EOL> def local ( self ) : <EOL> """<STR_LIT>""" <EOL> self . local_only = True <EOL> return self <EOL> def by_case ( self ) : <EOL> """<STR_LIT>""" <EOL> self . transpose = False <EOL> return self <EOL> def by_variable ( self ) : <EOL> """<STR_LIT>""" <EOL> self . transpose = True <EOL> return self <EOL> def var_names ( self ) : <EOL> """<STR_LIT>""" <EOL> self . names = True <EOL> return self <EOL> class _HDF5Reader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename ) : <EOL> import h5py <EOL> self . _inp = h5py . File ( filename , '<STR_LIT:r>' ) <EOL> self . _simulation_info = self . read_simulation_info ( ) <EOL> self . _state = '<STR_LIT>' <EOL> self . _info = None <EOL> @ property <EOL> def simulation_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _simulation_info <EOL> def read_iteration_case_from_hdf5 ( self , hdf5file , driver_name , iteration_case_name ) : <EOL> info = { } <EOL> driver_grp = self . _inp [ '<STR_LIT>' ] [ driver_name ] <EOL> iteration_grp = self . _inp [ '<STR_LIT>' ] [ driver_name ] [ iteration_case_name ] <EOL> info [ '<STR_LIT>' ] = self . read_from_hdf5 ( iteration_grp [ '<STR_LIT>' ] ) <EOL> data_grp = iteration_grp [ '<STR_LIT:data>' ] <EOL> info [ '<STR_LIT:data>' ] = { } <EOL> float_names = driver_grp [ '<STR_LIT>' ] <EOL> int_names = driver_grp [ '<STR_LIT>' ] <EOL> str_names = driver_grp [ '<STR_LIT>' ] <EOL> for i , name in enumerate ( float_names ) : <EOL> info [ '<STR_LIT:data>' ] [ name ] = data_grp [ '<STR_LIT>' ] [ i ] <EOL> for i , name in enumerate ( str_names ) : <EOL> info [ '<STR_LIT:data>' ] [ name ] = data_grp [ '<STR_LIT>' ] [ i ] <EOL> for i , name in enumerate ( int_names ) : <EOL> info [ '<STR_LIT:data>' ] [ name ] = data_grp [ '<STR_LIT>' ] [ i ] <EOL> for name in data_grp . keys ( ) : <EOL> if name not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in data_grp [ name ] . attrs : <EOL> info [ '<STR_LIT:data>' ] [ name ] = { } <EOL> for n , v in data_grp [ name ] . items ( ) : <EOL> info [ '<STR_LIT:data>' ] [ name ] [ n ] = self . read_from_hdf5 ( data_grp [ name ] [ n ] ) <EOL> info [ '<STR_LIT:data>' ] [ name ] = self . read_from_hdf5 ( data_grp [ name ] ) <EOL> return info <EOL> def read_from_hdf5 ( self , value ) : <EOL> import h5py <EOL> if isinstance ( value , h5py . _hl . group . Group ) : <EOL> d = { } <EOL> group = value <EOL> for name , value in group . attrs . items ( ) : <EOL> d [ name ] = self . read_from_hdf5 ( value ) <EOL> for name , value in group . items ( ) : <EOL> d [ name ] = self . read_from_hdf5 ( value ) <EOL> return d <EOL> elif value . dtype . names : <EOL> d = { } <EOL> for name in value . dtype . names : <EOL> d [ name ] = value [ name ] [ <NUM_LIT:0> ] <EOL> return d <EOL> else : <EOL> return value [ ( ) ] <EOL> def read_simulation_info ( self ) : <EOL> sim_info_grp = self . _inp [ '<STR_LIT>' ] <EOL> sim_info = { } <EOL> for name , value in sim_info_grp . attrs . items ( ) : <EOL> sim_info [ name ] = self . read_from_hdf5 ( value ) <EOL> for name , value in sim_info_grp . items ( ) : <EOL> sim_info [ name ] = self . read_from_hdf5 ( value ) <EOL> return sim_info <EOL> def drivers ( self ) : <EOL> """<STR_LIT>""" <EOL> driver_info = [ ] <EOL> for name in self . _inp . keys ( ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> driver_info . append ( self . read_from_hdf5 ( self . _inp [ name ] ) ) <EOL> return driver_info <EOL> def cases ( self ) : <EOL> """<STR_LIT>""" <EOL> iteration_cases_grp = self . _inp [ '<STR_LIT>' ] <EOL> case_timestamps = { } <EOL> for driver_name in iteration_cases_grp : <EOL> for iteration_case_name in iteration_cases_grp [ driver_name ] : <EOL> if iteration_case_name . startswith ( '<STR_LIT>' ) : <EOL> timestamp = iteration_cases_grp [ driver_name ] [ iteration_case_name ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> case_timestamps [ timestamp ] = ( driver_name , iteration_case_name ) <EOL> sorted_timestamps = sorted ( case_timestamps ) <EOL> for timestamp in sorted_timestamps : <EOL> driver_name , iteration_case_name = case_timestamps [ timestamp ] <EOL> info = self . read_iteration_case_from_hdf5 ( self . _inp , driver_name , iteration_case_name ) <EOL> yield info <EOL> def _next ( self ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
