我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用tables.NoSuchNodeError()。
def read_pytable(h5f, group=None): if group is None: group = h5f.root data = AttrDict() for child in h5f.list_nodes(group): item = None try: if isinstance(child, tb.group.Group): item = read_pytable(h5f, child) else: item = child.read() if isinstance(item, str) and item.startswith('OBJ_'): item = cPickle.loads(item[4:]) data[child._v_name] = item except tb.NoSuchNodeError: warnings.warn('No such node: "%s", skipping...' % repr(child)) pass return data
def read(self, group=None): if group is None: group = self.h5f.root data = AttrDict() for child in self.h5f.list_nodes(group): item = None try: if isinstance(child, tb.group.Group): item = self.read(child) else: item = child.read() if isinstance(item, str) and item.startswith('OBJ_'): item = cPickle.loads(item[4:]) data[child._v_name] = item except tb.NoSuchNodeError: warnings.warn('No such node: "%s", skipping...' %repr(child)) pass return data
def load_h5_namedtuple(group, namedtuple): """ Load a single namedtuple from an h5 group """ args = [] for field in namedtuple._fields: try: field_value = getattr(group, field).read() if field_value.shape == (): field_value = np.asscalar(field_value) except tables.NoSuchNodeError: try: field_value = getattr(group._v_attrs, field) except AttributeError: field_value = None args.append(field_value) return namedtuple(*args)
def get_matrix_from_h5(filename, genome): """Load the matrix from the HDF5 file. Code is from http://cf.10xgenomics.com/supp/cell-exp/megacell_tutorial.html Args: filename: HDF5 filename genome: Genome of data in the file. Returns: the sparse matrix of data """ with tables.open_file(filename, 'r') as f: try: dsets = {} for node in f.walk_nodes('/' + genome, 'Array'): dsets[node.name] = node.read() matrix = sp_sparse.csc_matrix( (dsets['data'], dsets['indices'], dsets['indptr']), shape=dsets['shape']) return GeneBCMatrix( dsets['genes'], dsets['gene_names'], dsets['barcodes'], matrix) except tables.NoSuchNodeError: raise Exception('Genome %s does not exist in %s.' % (genome, filename)) except KeyError: raise Exception('File %s missing one or more required datasets.' % filename)
def flush_pytable(h5f, data=None, group=None, table=None, force=True): # if data is None: data = self.data # if table is None: table = self.tables # if group is None: group = self.groups # print 'Keys: ', data.keys() for k,v in data.iteritems(): # print 'key,val', k,v, type(v) try: k = str(k) except: print 'Cannot save to DB, key is not string %s ' % k continue # if not isinstance(k, str): # continue # Clean up before writing if force: try: h5f.remove_node(get_node(group._gp,k), recursive=True) except tb.NoSuchNodeError: pass # print 'In Group: ', group, k, v if isinstance(v, dict): # self.log.debug('Attempting to save dict type') # assert(k not in table); table[k] = AttrDict() group[k] = AttrDict(); group[k]._gp = h5f.create_group(group._gp, k) h5f.flush() # self.log.debug('Out Group: %s' % group[k]) flush_pytable(h5f, data=v, group=group[k], table=table[k]) elif isinstance(v, np.ndarray): # self.log.debug('Attempting to save ndarray %s' % type(v)) table[k] = h5f.create_array(group._gp, k, v) # self.log.debug('Out Table: %s' % table[k]) else: # print 'Attempting to save arbitrary type %s' % type(v), k, group._gp try: assert v is not None table[k] = h5f.create_carray(group._gp, k, obj=v) except (TypeError, ValueError, AssertionError): v = 'OBJ_' + cPickle.dumps(v, -1) table[k] = h5f.create_array(group._gp, k, v) # print 'TypeError', v finally: h5f.flush() return
def flush(self, data=None, group=None, table=None, force=True): if data is None: data = self.data if table is None: table = self.tables if group is None: group = self.groups # print 'Keys: ', data.keys() for k,v in data.iteritems(): # print 'key,val', k,v, type(v) if not isinstance(k, str): self.log.debug('Cannot save to DB, key is not string %s ' % k) continue # Clean up before writing if force: try: self.h5f.remove_node(self.get_node(group._gp,k), recursive=True) except tb.NoSuchNodeError: pass # print 'In Group: ', group, k, v if isinstance(v, dict): self.log.debug('Attempting to save dict type') # assert(k not in table); table[k] = AttrDict() group[k] = AttrDict(); group[k]._gp = self.h5f.create_group(group._gp, k) self.h5f.flush() self.log.debug('Out Group: %s' % group[k]) self.flush(data=v, group=group[k], table=table[k]) elif isinstance(v, np.ndarray): self.log.debug('Attempting to save ndarray %s' % type(v)) table[k] = self.h5f.create_array(group._gp, k, v) self.log.debug('Out Table: %s' % table[k]) # elif isinstance(v,io_utils.TableWriter): # self.log.debug('Attempting to save with custom writer') # table[k] = self.h5f.createTable(group._gp, name=k, # description=v.description, # title='%s-data' % (k) ) # v.write(table[k]) # # print 'Creating table with group:%s name:%s desc:%s' % (group._gp, k, writer.description) # # print 'Out Table: ', table[k] else: self.log.debug('Attempting to save arbitrary type %s' % type(v)) try: assert v is not None table[k] = self.h5f.create_carray(group._gp, k, obj=v) except (TypeError, ValueError, AssertionError): v = 'OBJ_' + cPickle.dumps(v, -1) table[k] = self.h5f.create_array(group._gp, k, v) # print 'TypeError', v finally: self.h5f.flush() return
def read_10x_h5(filename, genome='mm10'): """Read hdf5 file with naming conventions of 10X Genomics. Parameters ---------- filename : str Filename. genome : str, optional (default: 'mm10') Genome group in hdf5 file. Returns ------- adata : :class:`~sc.api.AnnData` Annotated data matrix, where samples/cells are named by their barcode and variables/genes by gene name. The data is stored in adata.X, cell names in adata.smp_names and gene names in adata.var_names. """ logg.info('reading', filename, r=True, end=' ') import tables with tables.open_file(filename, 'r') as f: try: dsets = {} for node in f.walk_nodes('/' + genome, 'Array'): dsets[node.name] = node.read() # AnnData works with csr matrices # 10x stores the transposed data, so we do the transposition right away from scipy.sparse import csr_matrix M, N = dsets['shape'] data = dsets['data'] if dsets['data'].dtype == np.dtype('int32'): data = dsets['data'].view('float32') data[:] = dsets['data'] matrix = csr_matrix((data, dsets['indices'], dsets['indptr']), shape=(N, M)) # the csc matrix is automatically the transposed csr matrix # as scanpy expects it, so, no need for a further transpostion adata = AnnData(matrix, {'smp_names': dsets['barcodes'].astype(str)}, {'var_names': dsets['gene_names'].astype(str), 'gene_ids': dsets['genes'].astype(str)}) logg.info(t=True) return adata except tables.NoSuchNodeError: raise Exception('Genome %s does not exist in this file.' % genome) except KeyError: raise Exception('File is missing one or more required datasets.')