r"""Save graphs and optionally their labels to file. Besides saving to local files, DGL supports writing the graphs directly to S3 (by providing a ``"s3://..."`` path) or to HDFS (by providing ``"hdfs://..."`` a path). The function saves both the graph structure and node/edge featu
(filename, g_list, labels=None, formats=None)
| 81 | |
| 82 | |
| 83 | def save_graphs(filename, g_list, labels=None, formats=None): |
| 84 | r"""Save graphs and optionally their labels to file. |
| 85 | |
| 86 | Besides saving to local files, DGL supports writing the graphs directly |
| 87 | to S3 (by providing a ``"s3://..."`` path) or to HDFS (by providing |
| 88 | ``"hdfs://..."`` a path). |
| 89 | |
| 90 | The function saves both the graph structure and node/edge features to file |
| 91 | in DGL's own binary format. For graph-level features, pass them via |
| 92 | the :attr:`labels` argument. |
| 93 | |
| 94 | Parameters |
| 95 | ---------- |
| 96 | filename : str |
| 97 | The file name to store the graphs and labels. |
| 98 | g_list: list |
| 99 | The graphs to be saved. |
| 100 | labels: dict[str, Tensor] |
| 101 | labels should be dict of tensors, with str as keys |
| 102 | formats: str or list[str] |
| 103 | Save graph in specified formats. It could be any combination of |
| 104 | ``coo``, ``csc`` and ``csr``. If not specified, save one format |
| 105 | only according to what format is available. If multiple formats |
| 106 | are available, selection priority from high to low is ``coo``, |
| 107 | ``csc``, ``csr``. |
| 108 | |
| 109 | Examples |
| 110 | ---------- |
| 111 | >>> import dgl |
| 112 | >>> import torch as th |
| 113 | |
| 114 | Create :class:`DGLGraph` objects and initialize node |
| 115 | and edge features. |
| 116 | |
| 117 | >>> g1 = dgl.graph(([0, 1, 2], [1, 2, 3])) |
| 118 | >>> g2 = dgl.graph(([0, 2], [2, 3])) |
| 119 | >>> g2.edata["e"] = th.ones(2, 4) |
| 120 | |
| 121 | Save Graphs into file |
| 122 | |
| 123 | >>> from dgl.data.utils import save_graphs |
| 124 | >>> graph_labels = {"glabel": th.tensor([0, 1])} |
| 125 | >>> save_graphs("./data.bin", [g1, g2], graph_labels) |
| 126 | |
| 127 | See Also |
| 128 | -------- |
| 129 | load_graphs |
| 130 | """ |
| 131 | # if it is local file, do some sanity check |
| 132 | if is_local_path(filename): |
| 133 | if os.path.isdir(filename): |
| 134 | raise DGLError( |
| 135 | "Filename {} is an existing directory.".format(filename) |
| 136 | ) |
| 137 | f_path = os.path.dirname(filename) |
| 138 | if f_path and not os.path.exists(f_path): |
| 139 | os.makedirs(f_path) |
| 140 | g_sample = g_list[0] if isinstance(g_list, list) else g_list |
no test coverage detected