def asarray(x: Any, dtype: Optional[str] = None) -> np.ndarray: """Convert `x` to interger Numpy array. Parameters: ---------- x: Tensor, Scipy sparse matrix, Numpy array-like, etc. Returns: ---------- Integer Numpy array with dtype or `graphgallery.intx()` """ if dtype is None: dtype = gg.intx() if gf.is_tensor(x, backend="tensorflow"): if x.dtype != dtype: return tf.cast(x, dtype=dtype) else: return x if gf.is_tensor(x, backend="torch"): if x.dtype != dtype: return x.to(getattr(torch, dtype)) else: return x if gg.is_intscalar(x): x = np.asarray([x], dtype=dtype) elif gg.is_listlike(x) or (isinstance(x, np.ndarray) and x.dtype != "O"): x = np.asarray(x, dtype=dtype) else: raise ValueError( f"Invalid input which should be either array-like or integer scalar, but got {type(x)}." ) return x
def astensor(x, *, dtype=None, device=None, escape=None): try: if x is None or (escape is not None and isinstance(x, escape)): return x except TypeError: raise TypeError(f"argument 'escape' must be a type or tuple of types.") if dtype is None: dtype = gf.infer_type(x) if isinstance(dtype, (np.dtype, str)): dtype = data_type_dict().get(str(dtype), dtype) elif not isinstance(dtype, torch.dtype): raise TypeError( f"argument 'dtype' must be torch.dtype, np.dtype or str, but got {type(dtype)}." ) if is_tensor(x): tensor = x.to(dtype) elif gf.is_tensor(x, backend='tensorflow'): return astensor(gf.tensoras(x), dtype=dtype, device=device, escape=escape) elif sp.isspmatrix(x): if gg.backend() == "dgl_torch": import dgl tensor = dgl.from_scipy(x, idtype=getattr(torch, gg.intx())) elif gg.backend() == "pyg": edge_index, edge_weight = gf.sparse_adj_to_edge(x) return (astensor(edge_index, dtype=gg.intx(), device=device, escape=escape), astensor(edge_weight, dtype=gg.floatx(), device=device, escape=escape)) else: tensor = sparse_adj_to_sparse_tensor(x, dtype=dtype) elif any((isinstance(x, (np.ndarray, np.matrix)), gg.is_listlike(x), gg.is_scalar(x))): tensor = torch.tensor(x, dtype=dtype, device=device) else: raise TypeError( f"Invalid type of inputs. Allowed data type (Tensor, SparseTensor, Numpy array, Scipy sparse tensor, None), but got {type(x)}." ) return tensor.to(device)
def astensor(x, *, dtype=None, device=None, escape=None): try: if x is None or (escape is not None and isinstance(x, escape)): return x except TypeError: raise TypeError(f"argument 'escape' must be a type or tuple of types.") if dtype is None: dtype = gf.infer_type(x) elif isinstance(dtype, tf.dtypes.DType): dtype = dtype.name elif isinstance(dtype, (np.dtype, str)): dtype = str(dtype) else: raise TypeError( f"argument 'dtype' must be tf.dtypes.DType, np.dtype or str, but got {type(dtype)}." ) with tf.device(device): if is_tensor(x): if x.dtype != dtype: return tf.cast(x, dtype=dtype) return tf.identity(x) elif gf.is_tensor(x, backend='torch'): return astensor(gf.tensoras(x), dtype=dtype, device=device, escape=escape) elif sp.isspmatrix(x): if gg.backend() == "dgl_tf": import dgl return dgl.from_scipy(x, idtype=getattr(tf, gg.intx())).to(device) else: return sparse_adj_to_sparse_tensor(x, dtype=dtype) elif any((isinstance(x, (np.ndarray, np.matrix)), gg.is_listlike(x), gg.is_scalar(x))): return tf.convert_to_tensor(x, dtype=dtype) else: raise TypeError( f"Invalid type of inputs. Allowed data type(Tensor, SparseTensor, Numpy array, Scipy sparse matrix, None), but got {type(x)}." )