Skip to content

Commit

Permalink
Trac #33562: Bad error message for weighted adjacency matrix
Browse files Browse the repository at this point in the history
{{{
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
  • Loading branch information
Release Manager committed Nov 22, 2022
2 parents 3670306 + fe8b4d5 commit 2cec793
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,8 @@ def distance_matrix(self, vertices=None, *, base_ring=None, **kwds):
ret.set_immutable()
return ret

def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=None, **kwds):
def weighted_adjacency_matrix(self, sparse=True, vertices=None,
default_weight=None, *, base_ring=None, **kwds):
"""
Return the weighted adjacency matrix of the graph.

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

- ``default_weight`` -- (default: ``None``); specifies the weight to
replace any ``None`` edge label. When not specified an error is raised
if the label of an edge is ``None``.

- ``base_ring`` -- a ring (default: determined from the weights); the base
ring of the matrix space to use.

Expand Down Expand Up @@ -2394,6 +2399,22 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
[0 0 0]
[1 0 0]
[1 0 0]

Check error message for non numerical edge weights (:trac:`33562`)::

sage: G = Graph([(0, 1)])
sage: G.weighted_adjacency_matrix()
Traceback (most recent call last):
...
ValueError: cannot find the weight of (0, 1, None). Consider setting parameter 'default_weight'
sage: G.weighted_adjacency_matrix(default_weight=3)
[0 3]
[3 0]
sage: G = Graph([(0, 1, 'a')])
sage: G.weighted_adjacency_matrix()
Traceback (most recent call last):
...
TypeError: Cannot convert NoneType to sage.structure.parent.Parent
"""
if self.has_multiple_edges():
raise NotImplementedError("don't know how to represent weights for a multigraph")
Expand All @@ -2404,20 +2425,35 @@ def weighted_adjacency_matrix(self, sparse=True, vertices=None, *, base_ring=Non
set(vertices) != set(self.vertex_iterator())):
raise ValueError("``vertices`` must be a permutation of the vertices")

new_indices = {v: i for i, v in enumerate(vertices)}
# Method for checking edge weights and setting default weight
if default_weight is None:
def func(u, v, label):
if label is None:
raise ValueError(f"cannot find the weight of ({u}, {v}, None). "
"Consider setting parameter 'default_weight'")
return label
else:
def func(u, v, label):
if label is None:
return default_weight
return label

new_indices = {v: i for i,v in enumerate(vertices)}

D = {}
if self._directed:
for u, v, l in self.edge_iterator():
for u, v, label in self.edge_iterator():
i = new_indices[u]
j = new_indices[v]
D[i, j] = l
D[i, j] = func(u, v, label)
else:
for u, v, l in self.edge_iterator():
for u, v, label in self.edge_iterator():
i = new_indices[u]
j = new_indices[v]
D[i, j] = l
D[j, i] = l
label = func(u, v, label)
D[i, j] = label
D[j, i] = label

from sage.matrix.constructor import matrix
if base_ring is None:
M = matrix(self.num_verts(), D, sparse=sparse, **kwds)
Expand Down

0 comments on commit 2cec793

Please sign in to comment.