def populate_registry(fpath): import entities import registry h5in = tables.openFile(fpath, mode="r") h5root = h5in.root for table in h5root.entities: registry.entity_registry.add(entities.Entity.from_table(table)) globals_def = {} if hasattr(h5root, 'globals'): for table in h5root.globals: if isinstance(table, tables.Array): global_def = normalize_type(table.dtype.type) else: global_def = get_fields(table) globals_def[table.name] = global_def h5in.close() return globals_def
def entities_from_h5(fpath): from entities import Entity h5in = tables.open_file(fpath) h5root = h5in.root entities = {} for table in h5root.entities: entity = Entity.from_table(table) entities[entity.name] = entity globals_def = {} if hasattr(h5root, 'globals'): for table in h5root.globals: if isinstance(table, tables.Array): global_def = normalize_type(table.dtype.type) else: global_def = get_fields(table) globals_def[table.name] = global_def h5in.close() return globals_def, entities
def entities_from_h5(fpath): from entities import Entity h5in = tables.open_file(fpath) h5root = h5in.root entities = {} for table in h5root.entities: entity = Entity.from_table(table) entities[entity.name] = entity globals_def = {} if hasattr(h5root, 'globals'): for table in h5root.globals: if isinstance(table, tables.Array): global_def = {'type': normalize_type(table.dtype.type)} else: global_def = {'fields': get_fields(table)} globals_def[table.name] = global_def h5in.close() return globals_def, entities
def assert_valid_type(array, wanted_type, allowed_missing=None, context=None): if isinstance(wanted_type, list): wanted_fields = wanted_type # extract types from field description and normalise to python types actual_fields = get_fields(array) # check that all required fields are present wanted_names = set(name for name, _ in wanted_fields) actual_names = set(name for name, _ in actual_fields) allowed_missing = set(allowed_missing) if allowed_missing is not None \ else set() missing = wanted_names - actual_names - allowed_missing if missing: raise Exception("Missing field(s) in hdf5 input file: %s" % ', '.join(missing)) # check that types match common_fields1 = sorted((name, type_) for name, type_ in actual_fields if name in wanted_names) common_fields2 = sorted((name, type_) for name, type_ in wanted_fields if name in actual_names) bad_fields = [] for (name1, t1), (name2, t2) in zip(common_fields1, common_fields2): # this can happen if we have duplicates in wanted_fields assert name1 == name2, "%s != %s" % (name1, name2) if t1 != t2: bad_fields.append((name1, t2.__name__, t1.__name__)) if bad_fields: bad_fields_str = "\n".join(" - %s: %s instead of %s" % f for f in bad_fields) raise Exception("Field types in hdf5 input file differ from those " "defined in the simulation:\n%s" % bad_fields_str) else: assert isinstance(wanted_type, type) actual_type = normalize_type(array.dtype.type) if actual_type != wanted_type: raise Exception("Field type for '%s' in hdf5 input file is '%s' " "instead of '%s'" % (context, actual_type.__name__, wanted_type.__name__))
def fdef2field(name, fielddef): initialdata = True output = True default_value = None if isinstance(fielddef, Field): return fielddef elif isinstance(fielddef, (dict, str)): if isinstance(fielddef, dict): strtype = fielddef['type'] initialdata = fielddef.get('initialdata', True) output = fielddef.get('output', True) default_value = fielddef.get('default', default_value_by_strtype[strtype]) elif isinstance(fielddef, str): strtype = fielddef default_value = default_value_by_strtype[strtype] else: raise Exception('invalid field definition') dtype = field_str_to_type(strtype, "field '%s'" % name) else: assert isinstance(fielddef, type) dtype = normalize_type(fielddef) return Field(name, dtype, initialdata, output, default_value)
def assert_valid_type(array, wanted_type, context=None): if isinstance(wanted_type, list): wanted_fields = wanted_type # extract types from field description and normalise to python types actual_fields = get_fields(array) # check that all required fields are present wanted_names = set(name for name, _ in wanted_fields) actual_names = set(name for name, _ in actual_fields) missing = wanted_names - actual_names if missing: raise Exception("Missing field(s) in hdf5 input file: %s" % ', '.join(missing)) # check that types match common_fields1 = sorted((name, type_) for name, type_ in actual_fields if name in wanted_names) common_fields2 = sorted((name, type_) for name, type_ in wanted_fields if name in actual_names) bad_fields = [] for (name1, t1), (name2, t2) in zip(common_fields1, common_fields2): # this can happen if we have duplicates in wanted_fields assert name1 == name2, "%s != %s" % (name1, name2) if t1 != t2: bad_fields.append((name1, t2.__name__, t1.__name__)) if bad_fields: bad_fields_str = "\n".join(" - %s: %s instead of %s" % f for f in bad_fields) raise Exception("Field types in hdf5 input file differ from those " "defined in the simulation:\n%s" % bad_fields_str) else: assert isinstance(wanted_type, type) actual_type = normalize_type(array.dtype.type) if actual_type != wanted_type: raise Exception("Field type for '%s' in hdf5 input file is '%s' " "instead of '%s'" % (context, actual_type.__name__, wanted_type.__name__))
def fdef2field(name, fielddef): initialdata = True output = True default_value = None if isinstance(fielddef, Field): return fielddef elif isinstance(fielddef, (dict, str)): if isinstance(fielddef, dict): strtype = fielddef['type'] initialdata = fielddef.get('initialdata', True) output = fielddef.get('output', True) default_value = fielddef.get( 'default', default_value_by_strtype[strtype]) elif isinstance(fielddef, str): strtype = fielddef default_value = default_value_by_strtype[strtype] else: raise Exception('invalid field definition') dtype = field_str_to_type(strtype, "field '%s'" % name) else: assert isinstance(fielddef, type) dtype = normalize_type(fielddef) return Field(name, dtype, initialdata, output, default_value)
def get_fields(array): dtype = array.dtype return [(name, normalize_type(dtype[name].type)) for name in dtype.names]
def get_fields(array): dtype = array.dtype field_types = dtype.fields return [(name, normalize_type(field_types[name][0].type)) for name in dtype.names]