def parse_graph_inputs(*graph): # TODO: Maybe I could write it a little more elegantly here? if len(graph) == 0: graph = None elif len(graph) == 1: graph, = graph if isinstance(graph, BaseGraph): ... elif sp.isspmatrix(graph): graph = Graph(graph) elif isinstance(graph, dict): return Graph(**graph) elif is_list_like(graph): # TODO: multi graph ... else: raise ValueError(f"Unrecognized inputs {graph}.") else: if sp.isspmatrix(graph[0]): graph = Graph(*graph) elif is_list_like(graph[0]): # TODO: multi graph ... else: raise ValueError(f"Unrecognized inputs {graph}.") return graph
def asintarr(x, dtype: str = None): """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 = intx() if is_tensor(x): if x.dtype != dtype: kind = backend().kind if kind == "T": x = tf.cast(x, dtype=dtype) else: x = x.to(getattr(torch, dtype)) return x if is_interger_scalar(x): x = np.asarray([x], dtype=dtype) elif is_list_like(x) or isinstance(x, (np.ndarray, np.matrix)): 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 files_exist(files): if is_list_like(files): return len(files) != 0 and all([osp.exists(f) for f in files]) else: return osp.exists(files)
def files_exist(files: Union[List[str], Tuple[str]]): if is_list_like(files): return len(files) != 0 and all([osp.exists(f) for f in files]) else: return osp.exists(files)