Python h5py.is_hdf5() Examples

The following are 15 code examples of h5py.is_hdf5(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module h5py , or try the search function .
Example #1
Source File: hdf5.py    From nata with MIT License 6 votes vote down vote up
def is_valid_backend(path: Union[Path, str]) -> bool:
        if isinstance(path, str):
            path = Path(path)

        if not isinstance(path, Path):
            return False

        if not path.is_file():
            return False

        if not path.suffix == ".h5":
            return False

        if not h5.is_hdf5(path):
            return False

        with h5.File(path, mode="r") as f:
            if ("TYPE" in f.attrs) and ("LABELS" not in f.attrs):
                type_ = f.attrs["TYPE"].astype(str)[0]

                if type_ == "particles":
                    return True

        return False 
Example #2
Source File: hdf5.py    From nata with MIT License 6 votes vote down vote up
def is_valid_backend(file_path: Union[Path, str]) -> bool:
        if isinstance(file_path, str):
            file_path = Path(file_path)

        if not isinstance(file_path, Path):
            return False

        if not file_path.is_file():
            return False

        if not file_path.suffix == ".h5":
            return False

        if not h5.is_hdf5(file_path):
            return False

        with h5.File(file_path, mode="r") as f:
            if ("TYPE" in f.attrs) and ("LABELS" in f.attrs):
                type_ = f.attrs["TYPE"].astype(str)[0]

                if type_ == "particles":
                    return True

        return False 
Example #3
Source File: fileops.py    From cooler with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def is_multires_file(filepath, min_version=1):
    """
    Determine if a file is a multi-res cooler file.
    Returns False if the file doesn't exist.

    """
    if not h5py.is_hdf5(filepath):
        return False

    with h5py.File(filepath) as f:
        fmt = f.attrs.get("format", None)
        if "resolutions" in f.keys() and len(f["resolutions"].keys()) > 0:
            name = next(iter(f["resolutions"].keys()))
            if fmt == "HDF5::MCOOL" and _is_cooler(f["resolutions"][name]):
                return True
        elif "0" in f.keys() and _is_cooler(f["0"]) and min_version < 2:
            return True

    return False 
Example #4
Source File: api.py    From cooler with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, store, root=None, **kwargs):
        if isinstance(store, six.string_types):
            if root is None:
                self.filename, self.root = parse_cooler_uri(store)
            elif h5py.is_hdf5(store):
                with open_hdf5(store, **kwargs) as h5:
                    self.filename = h5.file.filename
                    self.root = root
            else:
                raise ValueError("Not a valid path to a Cooler file")
            self.uri = self.filename + "::" + self.root
            self.store = self.filename
            self.open_kws = kwargs
        else:
            # Assume an open HDF5 handle, ignore open_kws
            self.filename = store.file.filename
            self.root = store.name
            self.uri = self.filename + "::" + self.root
            self.store = store.file
            self.open_kws = {}
        self._refresh() 
Example #5
Source File: chkfile.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def dump_scf(mol, chkfile, e_tot, mo_energy, mo_coeff, mo_occ,
             overwrite_mol=True):
    '''save temporary results'''
    if h5py.is_hdf5(chkfile) and not overwrite_mol:
        with h5py.File(chkfile, 'a') as fh5:
            if 'mol' not in fh5:
                fh5['mol'] = mol.dumps()
    else:
        save_mol(mol, chkfile)

    scf_dic = {'e_tot'    : e_tot,
               'mo_energy': mo_energy,
               'mo_occ'   : mo_occ,
               'mo_coeff' : mo_coeff}
    save(chkfile, 'scf', scf_dic) 
Example #6
Source File: outcore.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def _create_h5file(erifile, dataname):
    if h5py.is_hdf5(erifile):
        feri = h5py.File(erifile, 'a')
        if dataname in feri:
            del(feri[dataname])
    else:
        feri = h5py.File(erifile, 'w')
    return feri 
Example #7
Source File: chkfile.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def dump_mcscf(mc, chkfile=None, key='mcscf',
               e_tot=None, mo_coeff=None, ncore=None, ncas=None,
               mo_occ=None, mo_energy=None, e_cas=None, ci_vector=None,
               casdm1=None, overwrite_mol=True):
    '''Save CASCI/CASSCF calculation results or intermediates in chkfile.
    '''
    if chkfile is None: chkfile = mc.chkfile
    if ncore is None: ncore = mc.ncore
    if ncas is None: ncas = mc.ncas
    if e_tot is None: e_tot = mc.e_tot
    if e_cas is None: e_cas = mc.e_cas
    if mo_coeff is None: mo_coeff = mc.mo_coeff
    #if ci_vector is None: ci_vector = mc.ci

    if h5py.is_hdf5(chkfile):
        fh5 = h5py.File(chkfile, 'a')
        if key in fh5:
            del(fh5[key])
    else:
        fh5 = h5py.File(chkfile, 'w')

    if 'mol' not in fh5:
        fh5['mol'] = mc.mol.dumps()
    elif overwrite_mol:
        del(fh5['mol'])
        fh5['mol'] = mc.mol.dumps()

    fh5[key+'/mo_coeff'] = mo_coeff

    def store(subkey, val):
        if val is not None:
            fh5[key+'/'+subkey] = val
    store('e_tot', e_tot)
    store('e_cas', e_cas)
    store('ci', ci_vector)
    store('ncore', ncore)
    store('ncas', ncas)
    store('mo_occ', mo_occ)
    store('mo_energy', mo_energy)
    store('casdm1', casdm1)
    fh5.close() 
Example #8
Source File: hdf5.py    From nata with MIT License 5 votes vote down vote up
def is_valid_backend(path: Union[Path, str]) -> bool:
        if isinstance(path, str):
            path = Path(path)

        if not isinstance(path, Path):
            return False

        if not path.is_file():
            return False

        if not path.suffix == ".h5":
            return False

        if not h5.is_hdf5(path):
            return False

        with h5.File(path, mode="r") as f:
            if (
                ("NAME" in f.attrs)
                and ("TYPE" in f.attrs)
                and ("LABEL" not in f.attrs)
            ):
                type_: str = f.attrs["TYPE"].astype(str)[0]
                # general naming
                name_: str = f.attrs["NAME"].astype(str)[0]
                names: Tuple[str, ...] = (name_,)
                # special case naming
                name_ = name_.split()[-1]
                name_ = name_.replace("_", "")

                names += (name_,)
                if (type_ == "grid") and any([name in f for name in names]):
                    return True

        return False 
Example #9
Source File: hdf5.py    From nata with MIT License 5 votes vote down vote up
def is_valid_backend(file_path: Union[Path, str]) -> bool:
        if isinstance(file_path, str):
            file_path = Path(file_path)

        if not isinstance(file_path, Path):
            return False

        if not file_path.is_file():
            return False

        if not file_path.suffix == ".h5":
            return False

        if not h5.is_hdf5(file_path):
            return False

        with h5.File(file_path, mode="r") as f:
            if (
                ("NAME" in f.attrs)
                and ("TYPE" in f.attrs)
                and ("LABEL" in f.attrs)
            ):
                type_ = f.attrs["TYPE"].astype(str)[0]
                # general naming
                name_ = f.attrs["NAME"].astype(str)[0]
                names = (name_,)
                # special case naming
                name_ = name_.split()[-1]
                name_ = name_.replace("_", "")

                names += (name_,)
                if (type_ == "grid") and any([name in f for name in names]):
                    return True

        return False 
Example #10
Source File: fileops.py    From cooler with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_cooler(uri):
    """
    Determine if a URI string references a cooler data collection.
    Returns False if the file or group path doesn't exist.

    """
    filepath, grouppath = parse_cooler_uri(uri)
    if not h5py.is_hdf5(filepath):
        return False
    with h5py.File(filepath) as f:
        return _is_cooler(f[grouppath]) 
Example #11
Source File: fileops.py    From cooler with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_coolers(filepath):
    """
    List group paths to all cooler data collections in a file.

    Parameters
    ----------
    filepath : str

    Returns
    -------
    list
        Cooler group paths in the file.

    """
    if not h5py.is_hdf5(filepath):
        raise OSError("'{}' is not an HDF5 file.".format(filepath))

    listing = []

    def _check_cooler(pth, grp):
        if _is_cooler(grp):
            listing.append("/" + pth if not pth.startswith("/") else pth)

    with h5py.File(filepath, "r") as f:
        _check_cooler("/", f)
        visititems(f, _check_cooler)

    return natsorted(listing) 
Example #12
Source File: fileops.py    From cooler with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ls(uri):
    """
    Get all groups and datasets in an HDF5 file.

    Parameters
    ----------
    uri : str

    Returns
    -------
    list
        Group and dataset paths.

    """
    filepath, grouppath = parse_cooler_uri(uri)
    if not h5py.is_hdf5(filepath):
        raise OSError("'{}' is not an HDF5 file.".format(filepath))

    listing = []

    def _check_all(pth, grp):
        listing.append("/" + pth if not pth.startswith("/") else pth)

    with h5py.File(filepath, "r") as f:
        _check_all(grouppath, f)
        visititems(f[grouppath], _check_all)

    return listing 
Example #13
Source File: dataset_generator.py    From asr-study with MIT License 5 votes vote down vote up
def flow_from_fname(self, fname, datasets=None):
        """ Returns an specific iterator given the filename

        # Arguments
            datasets: str or list. If str will return one iterator; otherwise
            will return len(dataset) iterators for each dataset

        # Inputs
            fname: path to a file.
                *.h5 (HDF5 format)
                *json (JSON format)

        # Outputs
            If fname is:
                HDF5 format: H5Iterator
                JSON format: JSONIterator
        """
        out = None
        datasets = datasets or ['/']
        if type(datasets) not in (set, list):
            datasets = [datasets]

        if h5py.is_hdf5(fname):
            h5_f = h5py.File(fname, 'r')
            out = [self.flow_from_h5_group(h5_f[dataset])
                   for dataset in datasets]

        ext = os.path.splitext(fname)[1]
        if ext == '.json':
            out = [self.flow_from_json(fname, dataset) for dataset in datasets]

        if out is None:
            raise ValueError("Extension not recognized")

        if len(out) == 1:
            return out[0]
        return out 
Example #14
Source File: khf.py    From pyscf with Apache License 2.0 4 votes vote down vote up
def as_scanner(mf):
    import copy
    if isinstance(mf, lib.SinglePointScanner):
        return mf

    logger.info(mf, 'Create scanner for %s', mf.__class__)

    class SCF_Scanner(mf.__class__, lib.SinglePointScanner):
        def __init__(self, mf_obj):
            self.__dict__.update(mf_obj.__dict__)

        def __call__(self, cell_or_geom, **kwargs):
            from pyscf.pbc import gto
            if isinstance(cell_or_geom, gto.Cell):
                cell = cell_or_geom
            else:
                cell = self.cell.set_geom_(cell_or_geom, inplace=False)

            # Cleanup intermediates associated to the pervious mol object
            self.reset(cell)

            if 'dm0' in kwargs:
                dm0 = kwargs.pop('dm0')
            elif self.mo_coeff is None:
                dm0 = None
            elif self.chkfile and h5py.is_hdf5(self.chkfile):
                dm0 = self.from_chk(self.chkfile)
            else:
                dm0 = self.make_rdm1()
                # dm0 form last calculation cannot be used in the current
                # calculation if a completely different system is given.
                # Obviously, the systems are very different if the number of
                # basis functions are different.
                # TODO: A robust check should include more comparison on
                # various attributes between current `mol` and the `mol` in
                # last calculation.
                if dm0.shape[-1] != cell.nao_nr():
                    #TODO:
                    #from pyscf.scf import addons
                    #if numpy.any(last_mol.atom_charges() != mol.atom_charges()):
                    #    dm0 = None
                    #elif non-relativistic:
                    #    addons.project_dm_nr2nr(last_mol, dm0, last_mol)
                    #else:
                    #    addons.project_dm_r2r(last_mol, dm0, last_mol)
                    dm0 = None
            self.mo_coeff = None  # To avoid last mo_coeff being used by SOSCF
            e_tot = self.kernel(dm0=dm0, **kwargs)
            return e_tot

    return SCF_Scanner(mf) 
Example #15
Source File: data_handler.py    From turbo_seti with MIT License 4 votes vote down vote up
def __init__(self, filename=None, size_limit=SIZE_LIM, out_dir='./', n_coarse_chan=None, coarse_chans=None):
        """
        :param filename:        string,      name of file (.h5 or .fil)
        :param size_limit:      float,       maximum size in MB that the file is allowed to be
        :param out_dir:         string,      directory where output files should be saved
        """

        if filename and os.path.isfile(filename):
            self.filename = filename
            self.out_dir = out_dir
            self.n_coarse_chan = n_coarse_chan
            self.coarse_chans = coarse_chans

            if not h5py.is_hdf5(filename):
                if not sigproc.is_filterbank(filename):
                    raise IOError('No correct format, need .h5. Try again...')
                else:
                    logger.info("File .fil detected. Attempting to create .h5 file in current directory...")
                    try:
                        self.__make_h5_file()
                    except:
                        raise IOError('Unable to create .h5 file. Please, try again with correct format.')

            self.filestat = os.stat(filename)
            self.filesize = self.filestat.st_size/(1024.0**2)

            # Make sure file is not larger than limit. If it is, we must split the file
            if self.filesize > size_limit:
                logger.info("The file is of size %f MB, exceeding our size limit %f MB. Split needed..."%(self.filesize, size_limit))
                self.data_list = self.__split_h5()
            else:
                #EE This is here mainly for testing. Since need to keep in mind the band pass shape.
                logger.debug("File size %f MB within range %f MB, okay..."%(self.filesize, size_limit))
                data_obj = DATAH5(filename)
                self.data_list = [data_obj]

            self.status = True

        else:
            self.status = False
            errmsg = "File {} doesn\'t exist, please check!".format(filename)
            logger.error(errmsg)
            raise IOError(errmsg)