def write_script_output(fn_h5, grp_name, results, args): '''Store the output of a script in an open HDF5 file **Arguments:** fn_h5 The HDF5 filename grp_name The name of the group where the results will be stored. results A dictionary with results. args The results of the command line parser. All arguments are stored as attributes in the HDF5 output file. ''' with LockedH5File(fn_h5) as f: # Store results grp = f.require_group(grp_name) for key in grp.keys(): del grp[key] dump_h5(grp, results) # Store command-line arguments arguments store_args(args, grp) if log.do_medium: log('Results written to %s:%s' % (fn_h5, grp_name))
def to_file(self, filename): '''Write data to a file **Arguments:** filename The file to write the data to This routine uses the extension or prefix of the filename to determine the file format. For each file format, a specialized function is called that does the real work. ''' if isinstance(filename, h5.Group) or filename.endswith('.h5'): data = vars(self).copy() # get rid of leading underscores for key in data.keys(): if key[0] == '_': data[key[1:]] = data[key] del data[key] from horton.io.internal import dump_h5 dump_h5(filename, data) elif filename.endswith('.xyz'): from horton.io.xyz import dump_xyz dump_xyz(filename, self) elif filename.endswith('.cube'): from horton.io.cube import dump_cube dump_cube(filename, self) elif filename.endswith('.cif'): from horton.io.cif import dump_cif dump_cif(filename, self) elif filename.endswith('.molden.input') or filename.endswith( '.molden'): from horton.io.molden import dump_molden dump_molden(filename, self) elif os.path.basename(filename).startswith('POSCAR'): from horton.io.vasp import dump_poscar dump_poscar(filename, self) elif 'FCIDUMP' in os.path.basename(filename): from horton.io.molpro import dump_fcidump dump_fcidump(filename, self) else: raise ValueError('Unknown file format for writing: %s' % filename)
def to_file(self, filename): '''Write data to a file **Arguments:** filename The file to write the data to This routine uses the extension or prefix of the filename to determine the file format. For each file format, a specialized function is called that does the real work. ''' if isinstance(filename, h5.Group) or filename.endswith('.h5'): data = vars(self).copy() # get rid of leading underscores for key in data.keys(): if key[0] == '_': data[key[1:]] = data[key] del data[key] from horton.io.internal import dump_h5 dump_h5(filename, data) elif filename.endswith('.xyz'): from horton.io.xyz import dump_xyz dump_xyz(filename, self) elif filename.endswith('.cube'): from horton.io.cube import dump_cube dump_cube(filename, self) elif filename.endswith('.cif'): from horton.io.cif import dump_cif dump_cif(filename, self) elif filename.endswith('.molden.input') or filename.endswith('.molden'): from horton.io.molden import dump_molden dump_molden(filename, self) elif os.path.basename(filename).startswith('POSCAR'): from horton.io.vasp import dump_poscar dump_poscar(filename, self) elif 'FCIDUMP' in os.path.basename(filename): from horton.io.molpro import dump_fcidump dump_fcidump(filename, self) else: raise ValueError('Unknown file format for writing: %s' % filename)
def write_part_output(fn_h5, grp_name, part, keys, args): '''Write the output of horton-wpart.py **Arguments:** fn_h5 The filename for the HDF5 output file. grp_name the destination group part The partitioning object (instance of subclass of horton.part.base.Part) keys The keys of the cached items that must go in the HDF5 outut file. args The results of the command line parser. All arguments are stored as attributes in the HDF5 output file. ''' def get_results(part, keys): results = {} for key in keys: if isinstance(key, basestring): results[key] = part[key] elif isinstance(key, tuple): assert len(key) == 2 index = key[1] assert isinstance(index, int) assert index >= 0 assert index < part.natom atom_results = results.setdefault('atom_%05i' % index, {}) atom_results[key[0]] = part[key] return results # Store the results in an HDF5 file with LockedH5File(fn_h5) as f: # Transform results to a suitable dictionary structure results = get_results(part, keys) # Store results grp = f.require_group(grp_name) for key in grp.keys(): del grp[key] dump_h5(grp, results) # Store command line arguments store_args(args, grp) if args.debug: # Collect debug results debug_keys = [key for key in part.cache.iterkeys() if key not in keys] debug_results = get_results(part, debug_keys) # Store additional data for debugging if 'debug' in grp: del grp['debug'] debuggrp = f.create_group('debug') dump_h5(debuggrp, debug_results) if log.do_medium: log('Results written to %s:%s' % (fn_h5, grp_name))
def write_part_output(fn_h5, grp_name, part, keys, args): '''Write the output of horton-wpart.py **Arguments:** fn_h5 The filename for the HDF5 output file. grp_name the destination group part The partitioning object (instance of subclass of horton.part.base.Part) keys The keys of the cached items that must go in the HDF5 outut file. args The results of the command line parser. All arguments are stored as attributes in the HDF5 output file. ''' def get_results(part, keys): results = {} for key in keys: if isinstance(key, basestring): results[key] = part[key] elif isinstance(key, tuple): assert len(key) == 2 index = key[1] assert isinstance(index, int) assert index >= 0 assert index < part.natom atom_results = results.setdefault('atom_%05i' % index, {}) atom_results[key[0]] = part[key] return results # Store the results in an HDF5 file with LockedH5File(fn_h5) as f: # Transform results to a suitable dictionary structure results = get_results(part, keys) # Store results grp = f.require_group(grp_name) for key in grp.keys(): del grp[key] dump_h5(grp, results) # Store command line arguments store_args(args, grp) if args.debug: # Collect debug results debug_keys = [ key for key in part.cache.iterkeys() if key not in keys ] debug_results = get_results(part, debug_keys) # Store additional data for debugging if 'debug' in grp: del grp['debug'] debuggrp = f.create_group('debug') dump_h5(debuggrp, debug_results) if log.do_medium: log('Results written to %s:%s' % (fn_h5, grp_name))