[docs]defproject_to_hdf5(project,filename=''):r""" Creates an HDF5 file containing data from the specified objects Parameters ---------- network : Network The network containing the desired data phases : list[Phase]s (optional, default is none) A list of phase objects whose data are to be included Returns ------- f : hdf5 file handle A handle to an hdf5 file. This must be closed when done (i.e. ``f.close()``. """fromh5pyimportFileashdfFileiffilename=='':filename=project.namefilename=_parse_filename(filename,ext='hdf')dct=project_to_dict(project=project)d=pd.json_normalize(dct,sep='.').to_dict(orient='records')[0]forkinlist(d.keys()):d[k.replace('.','/')]=d.pop(k)f=hdfFile(filename,"w")foriteminlist(d.keys()):tempname='_'.join(item.split('.'))arr=d[item]ifd[item].dtype=='O':logger.warning(item+' has dtype object, will not write to file')deld[item]elif'U'instr(arr[0].dtype):passelse:f.create_dataset(name='/'+tempname,shape=arr.shape,dtype=arr.dtype,data=arr)returnf
[docs]defprint_hdf5(f,flat=False):r""" Given an hdf5 file handle, prints to console in a human readable manner Parameters ---------- f : file handle The hdf5 file to print flat : bool Flag to indicate if print should be nested or flat. The default is ``flat==False`` resulting in a nested view. """ifflatisFalse:defprint_level(f,p='',indent='-'):foriteminf.keys():ifhasattr(f[item],'keys'):p=print_level(f[item],p=p,indent=indent+indent[0])elifindent[-1]!=' ':indent=indent+''p=indent+item+'\n'+preturnpp=print_level(f)print(p)else:f.visit(print)