Skip to content

Commit 2cec793

Browse files
author
Release Manager
committed
Trac #33562: Bad error message for weighted adjacency matrix
{{{ H=Graph({0:[1,2,3], 4:[0,2], 6:[1,2,3,4,5]}) H.weighted(True) H.weighted_adjacency_matrix() }}} should give something useful, but instead is an inexplicable error message {{{ TypeError: NoneType takes no arguments }}} This happens whether or not this graph is said to be weighted. There should be an error catch for a useful message. URL: https://trac.sagemath.org/33562 Reported by: kcrisman Ticket author(s): David Coudert Reviewer(s): Dima Pasechnik
2 parents 3670306 + fe8b4d5 commit 2cec793

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

src/sage/graphs/generic_graph.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,8 @@ def distance_matrix(self, vertices=None, *, base_ring=None, **kwds):
23242324
ret.set_immutable()
23252325
return ret
23262326

2327-
def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=None, **kwds):
2327+
def weighted_adjacency_matrix(self, sparse=True, vertices=None,
2328+
default_weight=None, *, base_ring=None, **kwds):
23282329
"""
23292330
Return the weighted adjacency matrix of the graph.
23302331

@@ -2341,6 +2342,10 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
23412342
each vertex is represented by its position in the list returned by
23422343
method :meth:`vertices`
23432344

2345+
- ``default_weight`` -- (default: ``None``); specifies the weight to
2346+
replace any ``None`` edge label. When not specified an error is raised
2347+
if the label of an edge is ``None``.
2348+
23442349
- ``base_ring`` -- a ring (default: determined from the weights); the base
23452350
ring of the matrix space to use.
23462351

@@ -2394,6 +2399,22 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
23942399
[0 0 0]
23952400
[1 0 0]
23962401
[1 0 0]
2402+
2403+
Check error message for non numerical edge weights (:trac:`33562`)::
2404+
2405+
sage: G = Graph([(0, 1)])
2406+
sage: G.weighted_adjacency_matrix()
2407+
Traceback (most recent call last):
2408+
...
2409+
ValueError: cannot find the weight of (0, 1, None). Consider setting parameter 'default_weight'
2410+
sage: G.weighted_adjacency_matrix(default_weight=3)
2411+
[0 3]
2412+
[3 0]
2413+
sage: G = Graph([(0, 1, 'a')])
2414+
sage: G.weighted_adjacency_matrix()
2415+
Traceback (most recent call last):
2416+
...
2417+
TypeError: Cannot convert NoneType to sage.structure.parent.Parent
23972418
"""
23982419
if self.has_multiple_edges():
23992420
raise NotImplementedError("don't know how to represent weights for a multigraph")
@@ -2404,20 +2425,35 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
24042425
set(vertices) != set(self.vertex_iterator())):
24052426
raise ValueError("``vertices`` must be a permutation of the vertices")
24062427

2407-
new_indices = {v: i for i, v in enumerate(vertices)}
2428+
# Method for checking edge weights and setting default weight
2429+
if default_weight is None:
2430+
def func(u, v, label):
2431+
if label is None:
2432+
raise ValueError(f"cannot find the weight of ({u}, {v}, None). "
2433+
"Consider setting parameter 'default_weight'")
2434+
return label
2435+
else:
2436+
def func(u, v, label):
2437+
if label is None:
2438+
return default_weight
2439+
return label
2440+
2441+
new_indices = {v: i for i,v in enumerate(vertices)}
24082442

24092443
D = {}
24102444
if self._directed:
2411-
for u, v, l in self.edge_iterator():
2445+
for u, v, label in self.edge_iterator():
24122446
i = new_indices[u]
24132447
j = new_indices[v]
2414-
D[i, j] = l
2448+
D[i, j] = func(u, v, label)
24152449
else:
2416-
for u, v, l in self.edge_iterator():
2450+
for u, v, label in self.edge_iterator():
24172451
i = new_indices[u]
24182452
j = new_indices[v]
2419-
D[i, j] = l
2420-
D[j, i] = l
2453+
label = func(u, v, label)
2454+
D[i, j] = label
2455+
D[j, i] = label
2456+
24212457
from sage.matrix.constructor import matrix
24222458
if base_ring is None:
24232459
M = matrix(self.num_verts(), D, sparse=sparse, **kwds)

0 commit comments

Comments
 (0)