@@ -171,20 +171,71 @@ class Dispatcher:
171
171
# End auto-generated code: dispatch
172
172
173
173
@staticmethod
174
- def convert_from_nx (graph , weight = None , * , name = None ):
174
+ def convert_from_nx (
175
+ graph ,
176
+ edge_attrs = None ,
177
+ node_attrs = None ,
178
+ preserve_edge_attrs = False ,
179
+ preserve_node_attrs = False ,
180
+ preserve_graph_attrs = False ,
181
+ name = None ,
182
+ graph_name = None ,
183
+ * ,
184
+ weight = None , # For nx.__version__ <= 3.1
185
+ ):
175
186
import networkx as nx
176
187
177
188
from .classes import DiGraph , Graph , MultiDiGraph , MultiGraph
178
189
190
+ if preserve_edge_attrs :
191
+ if graph .is_multigraph ():
192
+ attrs = set ().union (
193
+ * (
194
+ datadict
195
+ for nbrs in graph ._adj .values ()
196
+ for keydict in nbrs .values ()
197
+ for datadict in keydict .values ()
198
+ )
199
+ )
200
+ else :
201
+ attrs = set ().union (
202
+ * (datadict for nbrs in graph ._adj .values () for datadict in nbrs .values ())
203
+ )
204
+ if len (attrs ) == 1 :
205
+ [attr ] = attrs
206
+ edge_attrs = {attr : None }
207
+ elif attrs :
208
+ raise NotImplementedError ("`preserve_edge_attrs=True` is not fully implemented" )
209
+ if node_attrs :
210
+ raise NotImplementedError ("non-None `node_attrs` is not yet implemented" )
211
+ if preserve_node_attrs :
212
+ attrs = set ().union (* (datadict for node , datadict in graph .nodes (data = True )))
213
+ if attrs :
214
+ raise NotImplementedError ("`preserve_node_attrs=True` is not implemented" )
215
+ if edge_attrs :
216
+ if len (edge_attrs ) > 1 :
217
+ raise NotImplementedError (
218
+ "Multiple edge attributes is not implemented (bad value for edge_attrs)"
219
+ )
220
+ if weight is not None :
221
+ raise TypeError ("edge_attrs and weight both given" )
222
+ [[weight , default ]] = edge_attrs .items ()
223
+ if default is not None and default != 1 :
224
+ raise NotImplementedError (f"edge default != 1 is not implemented; got { default } " )
225
+
179
226
if isinstance (graph , nx .MultiDiGraph ):
180
- return MultiDiGraph .from_networkx (graph , weight = weight )
181
- if isinstance (graph , nx .MultiGraph ):
182
- return MultiGraph .from_networkx (graph , weight = weight )
183
- if isinstance (graph , nx .DiGraph ):
184
- return DiGraph .from_networkx (graph , weight = weight )
185
- if isinstance (graph , nx .Graph ):
186
- return Graph .from_networkx (graph , weight = weight )
187
- raise TypeError (f"Unsupported type of graph: { type (graph )} " )
227
+ G = MultiDiGraph .from_networkx (graph , weight = weight )
228
+ elif isinstance (graph , nx .MultiGraph ):
229
+ G = MultiGraph .from_networkx (graph , weight = weight )
230
+ elif isinstance (graph , nx .DiGraph ):
231
+ G = DiGraph .from_networkx (graph , weight = weight )
232
+ elif isinstance (graph , nx .Graph ):
233
+ G = Graph .from_networkx (graph , weight = weight )
234
+ else :
235
+ raise TypeError (f"Unsupported type of graph: { type (graph )} " )
236
+ if preserve_graph_attrs :
237
+ G .graph .update (graph .graph )
238
+ return G
188
239
189
240
@staticmethod
190
241
def convert_to_nx (obj , * , name = None ):
0 commit comments