コード例 #1
0
def dump_h5(grp, data):
    '''Dump a (HORTON) object to a HDF5 file.

       grp
            A HDF5 group or a filename of a new HDF5 file.

       data
            The object to be written. This can be a dictionary of objects or
            an instance of a HORTON class that has a ``to_hdf5`` method. The
            dictionary my contain numpy arrays
    '''
    if isinstance(grp, basestring):
        with LockedH5File(grp, 'w') as f:
            dump_h5(f, data)
    elif isinstance(data, dict):
        for key, value in data.iteritems():
            # Simply overwrite old data
            if key in grp:
                del grp[key]
            if isinstance(value, int) or isinstance(value, float) or isinstance(value, np.ndarray) or isinstance(value, basestring):
                grp[key] = value
            else:
                subgrp = grp.require_group(key)
                dump_h5(subgrp, value)
    else:
        # clear the group if anything was present
        for key in grp.keys():
            del grp[key]
        for key in grp.attrs.keys():
            del grp.attrs[key]
        data.to_hdf5(grp)
        # The following is needed to create object of the right type when
        # reading from the checkpoint:
        grp.attrs['class'] = data.__class__.__name__
コード例 #2
0
def load_h5(item):
    '''Load a (HORTON) object from an h5py File/Group

       **Arguments:**

       item
            A HD5 Dataset or group, or a filename of an HDF5 file
    '''
    if isinstance(item, basestring):
        with LockedH5File(item, 'r') as f:
            return load_h5(f)
    elif isinstance(item, h5.Dataset):
        if len(item.shape) > 0:
            # convert to a numpy array
            return np.array(item)
        else:
            # convert to a scalar
            return item[()]
    elif isinstance(item, h5.Group):
        class_name = item.attrs.get('class')
        if class_name is None:
            # assuming that an entire dictionary must be read.
            result = {}
            for key, subitem in item.iteritems():
                result[key] = load_h5(subitem)
            return result
        else:
            # special constructor. the class is found with the imp module
            cls = __import__('horton',
                             fromlist=[class_name]).__dict__[class_name]
            return cls.from_hdf5(item)
コード例 #3
0
ファイル: common.py プロジェクト: susilehtola/horton
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))
コード例 #4
0
ファイル: common.py プロジェクト: susilehtola/horton
def check_output(fn_h5, grp_name, overwrite):
    '''Check if the output is already present in print a warning if --overwrite is not used

       **Arguments:**

       fn_h5
            The output HDF5 file

       grp_name
            The HDF5 group in which the output is stored.

       overwrite
            Whether the user wants to overwrite contents that were already
            present.
    '''
    if os.path.isfile(fn_h5):
        with LockedH5File(fn_h5, 'r') as f:
            if grp_name in f and len(f[grp_name]) > 0:
                if overwrite:
                    if log.do_warning:
                        log.warn(
                            'Overwriting the contents of "%s" in the file "%s".'
                            % (grp_name, fn_h5))
                    return False
                else:
                    if log.do_warning:
                        log.warn(
                            'Skipping because the group "%s" is already present in the file "%s" and it is not empty.'
                            % (grp_name, fn_h5))
                    return True
    return False
コード例 #5
0
def dump_checkpoint(filename, system):
    '''Dump the system in a Horton checkpoint file.

       **Arguments:**

       filename
            This is the file name of an HDF5 Horton checkpoint file. It may also
            be an open h5.Group object.

       system
            A System instance.
    '''
    if isinstance(filename, basestring):
        chk = LockedH5File(filename, 'w')
        do_close = True
    else:
        chk = filename
        do_close = False

    try:
        for field in attribute_register.itervalues():
            field.write(chk, system)
    finally:
        if do_close:
            chk.close()
コード例 #6
0
    def to_file(self, filename):
        '''Write the database to an HDF5 file


           **Arguments:**

           filename
                A string with the filename of the hdf5 file, or a h5.File or
                h5.Group object.
        '''
        # parse the argument
        if isinstance(filename, basestring):
            f = LockedH5File(filename, 'w')
            do_close = True
        elif isinstance(filename, h5.Group):
            f = filename
            do_close = False
        try:
            # Write
            for record in self._records:
                name = 'Z=%i_Q=%+i' % (record.number, record.charge)
                if name in f:
                    del f[name]
                grp = f.create_group(name)
                grp.attrs['number'] = record.number
                grp.attrs['charge'] = record.charge
                grp.attrs['energy'] = record.energy
                if record.homo_energy is not None:
                    grp.attrs['homo_energy'] = record.homo_energy
                grp.attrs['rtransform'] = record.rgrid.rtransform.to_string()
                grp['rho'] = record.rho
                if record.deriv is not None:
                    grp['deriv'] = record.deriv
                grp.attrs['pseudo_number'] = record.pseudo_number
                if record.ipot_energy is not None:
                    grp.attrs['ipot_energy'] = record.ipot_energy
        finally:
            # close
            if do_close:
                f.close()
コード例 #7
0
def load_checkpoint(filename, lf):
    """Load constructor arguments from a Horton checkpoint file

       **Arguments:**

       filename
            This is the file name of an HDF5 Horton checkpoint file. It may also
            be an open h5.Group object.

       lf
            A LinalgFactory instance.
    """
    result = {}
    if isinstance(filename, basestring):
        chk = LockedH5File(filename, 'r')
        do_close = True
    else:
        chk = filename
        do_close = False

    try:
        for field in attribute_register.itervalues():
            att = field.read(chk, lf)
            d = result
            name = field.att_name
            if field.key is not None:
                d = result.setdefault(field.att_name, {})
                name = field.key
            if att is not None:
                if field.tags is None:
                    d[name] = att
                else:
                    d[name] = att, field.tags
    finally:
        if do_close:
            chk.close()
    result['chk'] = filename
    return result
コード例 #8
0
def load_proatom_records_h5_file(filename):
    '''Load proatom records from the given HDF5 file'''
    with LockedH5File(filename) as f:
        return load_proatom_records_h5_group(f)
コード例 #9
0
def load_charges(arg_charges):
    '''Load a charges given at the command line'''
    fn_h5, ds_name = parse_h5(arg_charges, 'charges', path_optional=False)
    with LockedH5File(fn_h5, 'r') as f:
        return f[ds_name][:]
コード例 #10
0
def load_cost(arg_cost):
    '''Load an ESP cost function given at the command line'''
    fn_h5_in, grp_name_in = parse_h5(arg_cost, 'cost')
    with LockedH5File(fn_h5_in, 'r') as f:
        return ESPCost.from_hdf5(f[grp_name_in]['cost']), f[grp_name_in]['used_volume'][()]
コード例 #11
0
ファイル: common.py プロジェクト: susilehtola/horton
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))