You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
has_edge behavior change (migrated from Trac #203) #204
The behavior of has_edge has changed in .99 in an additional way which wasn't mentioned in the API changes. Previously, has_edge(u,v,x) was an option. Now, only has_edge(u,v) is an option.
Could this go back in? I found it useful as a quick check to see if the machine had an exact edge. It also hides checks against multigraphs when working with generic graph structures.
From:
'''
def has_edge(self, u, v):
try:
return v in self.adj[u]
except KeyError:
return False
'''
To:
'''
def has_edge(self, u, v, data=None):
try:
v in self.adj[u]
except KeyError:
return False
if data is None:
return rue
else:
if self.multigraph:
return data in self.adj[u][v]
else:
return data == self.adj[u][v]
'''
One problem with this is that it is difficult to ask if the edge data is actually None. In such cases, it might be easiest to demand that the user test for such an edge manually. Or (this is ugly)...
'''
def has_edge(self, u, v, data=None, testNone=False):
try:
v in self.adj[u]
except KeyError:
return False
if data is None:
if not testNone:
return True
else:
if self.multigraph:
return None in self.adj[u][v]
else:
return self.adj[u][v] is None
else:
if self.multigraph:
return data in self.adj[u][v]
else:
return self.adj[u][v] == data
'''
We actually already have an another method: G.has_neighbor(u,v)
So let's make has_edge optionally include the data...
In the old XGraph API, we didn't provide for testing if data=None. I personally don't like the idea of using None in a data structure. It seems to me that Python added None to be able to see if a keyword argument was given or not. So I'm fine with having people check for edgedata=None on their own. I also know others don't agree with my view of None. :)
(In [1088]) Switch has_edge API back to previous version where it optionally checks for edge data.
Aric, if you prefer that it require edge data so users can test for data==None
please feel free to commit such.
Addresses #203
Or maybe I just one of those that like the None object.
I think None is appropriate for edge data to indicated the absence
of any data. So it bugs me that we would allow None as an edge
data without being able to check it with (the new) has_edge()
So for has_edge(), how about forcing using the data= keyword?
e.g. G.has_edge(u,v,data=d)
I believe that suffers from the same problem as the data=None
approach. Except you can't test if the edge data
is 1 in this case. e.g.
'''
G.add_edge(1,2,2)
G.has_edge(1,2,1) is True
}}
Hence the approach of using the ugly **kwds -
that way you don't have to specify a default
value.
I meant that has_edge should ''always'' check for data==1 unless another value is requested. This allows people to check for data==None, and also allows people not using edge data to use has_edge as expected.
For Graph (modifying [1088]),
'''
def has_edge(self, u, v, data=1):
try:
d=self.adj[u][v]
except KeyError:
return False
return data == d
'''
and for multigraphs,
'''
def has_edge(self, u, v, data=1):
try:
d=self.adj[u][v]
except KeyError:
return False
return data in d
'''
To eliminate surprise I can't see how to have any default value
(though I'm willing to let people be surprised if they use None ;)
The *_kwds method above doesn't allow G.has_edge(1,2,3) but if we check both *_kwds and *args it's OK. Code looks ugly, but with try/except on the initial test and if's for checking args and kwds, a quick test shows it is similar in speed to the other suggestions. Can do more speed testing if that's what people think is best.
That would be very confusing indeed! Hopefully the **kwds and *args idea works out. Even if it isn't as fast, I think the convenience it provides is probably worth it. As Aric mentioned, when faster checks are required, other methods do exist.
How important is it that there exist a default value? Ie, perhaps has_edge() should only be used when querying for (u,v,data) and the data must always be explicitly specified---no _args or *_kwds. If people aren't concerned about checking for data, then has_neighbor(u,v) and similar digraph friends are available. So:
has_edge(u,v,data)
But I suppose this causes consistency issues with:
get_edge(u,v,no_edge=None) # which does not have a required data argument
Part of the problem here is that we are explicitly not using an
edge object. So what is an edge? A two-tuple of nodes? A
three-tuple of nodes with data? Both?
For NetworkX, my opinion is that an edge is a two-tuple of nodes and
has_edge(u,v) should determine if such an edge exists in the graph.
There might be more than one of those edges (multigraph) in which case
you have to decide, if they are not identical, which one you mean. If
they have different data you can do that.
So if you want to know if there is a particular edge with particular
data then you must check both if the edge exists and if it has the
data you are looking for. Note that it is possible to put data on the
edge that doesn't have an eq() method so in that
you might want to use "is" instead of "==".
Is having has_edge() check for == with the data a "convenience"
and maybe not worth it? Should there be a different method?
I remember once saying "has_edge_with_data()" and decided
one_might_not_want_so_many_special_methods_with_long_names().
But I think something like that is a possible solution here.
Note that it is possible to put data on the
edge that doesn't have an eq() method so in that
you might want to use "is" instead of "==".
Is having has_edge() check for == with the data a "convenience"
and maybe not worth it?
Hmm. Now you have me leaning back the other way, towards the original .99 implementation of not allowing a data argument at all for has_edge(). When the default graph type is a weighted graph and thus, potentially floats as weights, it does seem like "==" and "is" could cause confusion.
So, I'm -1 on allowing data to be passed into has_edge() now. From now on, I'll just do something like:
'''
if G.has_edge(u,v):
if not G.multigraph:
pass # compare G[u][v] to something
else:
for data in G[u][v]:
pass # compare data to something
'''
Now, the only place where None is an issue still is with get_edge(), per your earlier comment. Personally, it won't effect me, as I don't use None as edge data, but others might.
One way to handle this is to always raise an exception, and force the user to pre-check with has_edge(u,v). If this is done, it seems like get_edge() could be removed altogether...in favor of the quicker G[u][v] access. If it is not removed, should get_edge() be renamed to get_edgedata(), or something similar, since it only returns the data and doesn't fit the notion of an edge as a 2-tuple?
Argh, sorry for shifting positions on this! In the end, I hope the conversations help you guys figure out what is best for NetworkX.
I think I prefer to have a function for checking 3-tuples and a different one for 2-tuples.
Could we use a name like has_edgedata(u,v,d) That was in some version at some point...
Perhaps we should allow some user comparison function to choose between ==, is, and
a user provided function (e.g. abs(difference)<1e-8)
'''
def has_edge(u,v):
try:
v in self.adj[u]
except KeyError:
return False
Graphs
def has_edgedata(u,v,data,cmp=None):
try:
d = self.adj[u][v]
except KeyError:
return False
if cmp is None:
return data == d
else:
return cmp(data,d)
Multi(Di)Graphs
def has_edgedata(u,v,data,cmp=None):
try:
d = self.adj[u][v]
except KeyError:
return False
if cmp is None:
return data in d
else:
for x in d:
if cmp(data,x):
return True
return False
def get_edgedata(self, u, v, default=None): # (Di)Graph
def get_edgedata(self, u, v, no_edge=None): # Multi(Di)Graph
# renamed get_edge()
# should 'no_edge' and 'default' have the same name?
'''
Separate functions seem to address the concerns for checking if the graph has edgedata which is None, and the cmp argument allows for flexibility while not adding bloat to has_edge().
A remaining issue for (Di)Graphs is how to distinguish edgedata which is None from the default=None when calling get_edgedata(). Perhaps it should just raise...but then again, why not just call G[u][v]?
For Multi(Di)Graphs, would default=[] be a better fit (keep it an iterable)?
For !DiGraph.get_edge(), a (shallow) copy of the edgedata is returned:
self.adj[u][v][:]
This is (undesirably?) different from if the user had called G[u][v].
That looks good to me. For get_edgedata() (and I think renaming
it to that is correct) I'd like (for Graph())
'''
def get_edgedata(self, u, v, default=None):
return self.adj[u].get(v,default)
'''
I guess there are only two sensible options for get_edgedata()
if there is no edge - raise an exception or return None.
In this case I think I'd like to return None.
Python dictionaries clearly use this so maybe it won't be a surprise.
'''
In [6]: d={1:None}
In [7]: d.get(1)
In [8]: print d.get(1)
None
In [9]: print d.get(2)
None
'''
First assume you don't use None for your edge data.
Then this will return a None if there is no edge (and thus
no edge data). Otherwise by default in NetworkX there is a 1 there
or something else that the user specified. That should cover
almost all users.
For None-lovers -if you use None as edge data -
you'll have use the default=keyword and be smart about what you set that to.
But it can be done.
def get_edgedata(self, u, v, default=None): # (Di)Graph
def get_edgedata(self, u, v, no_edge=None): # Multi(Di)Graph
# renamed get_edge()
# should 'no_edge' and 'default' have the same name?
'''
It should be "default".
For Multi(Di)Graphs, would default=[] be a better fit (keep it an iterable)?
Definitely don't want to do that. The default value is evaluated only
once when the function object is created.
For !DiGraph.get_edge(), a (shallow) copy of the edgedata is returned:
self.adj[u][v][:]
This is (undesirably?) different from if the user had called G[u][v].
It's a good question. For MultiGraph if we want G[u][v] to
be the same as G.get_edge(u,v) then something needs to be fixed.
Not sure which is the right way but it would be harder to
make G[u][v] return a copy.
I made the changes discussed for has_edge() and get_edge_data().
I also modified the behavior of get_edge_data() for MultiGraph so it
does not return a copy (same as G[u][v]).
Activity
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-09
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-09
You are right; this is tricky. Another approach would be to add another
method that does "has edge with data".
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @dschult, 2008-12-09
We actually already have an another method: G.has_neighbor(u,v)
So let's make has_edge optionally include the data...
In the old XGraph API, we didn't provide for testing if data=None. I personally don't like the idea of using None in a data structure. It seems to me that Python added None to be able to see if a keyword argument was given or not. So I'm fine with having people check for edgedata=None on their own. I also know others don't agree with my view of None. :)
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @dschult, 2008-12-09
(In [1088]) Switch has_edge API back to previous version where it optionally checks for edge data.
Aric, if you prefer that it require edge data so users can test for data==None
please feel free to commit such.
Addresses #203
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-09
Maybe this could use some more discussion.
Or maybe I just one of those that like the None object.
I think None is appropriate for edge data to indicated the absence
of any data. So it bugs me that we would allow None as an edge
data without being able to check it with (the new) has_edge()
So for has_edge(), how about forcing using the data= keyword?
e.g. G.has_edge(u,v,data=d)
'''
def has_edge(self, u, v, **kwds):
try:
d=self.adj[u][v]
except KeyError:
return False
try:
data=kwds['data']
return data==d
except:
return True
'''
I'd guess the fastest way to find if an edge (u,v) exists is to
check v in G[u]. You could also check G[u][v]==data.
A !KeyError means no edge.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-09
Replying to [comment:5 aric]:
[[BR]]
Another idea is that the method signature match the add_edge signature.
'''
def has_edge(u,v,data=1):
...
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-09
I believe that suffers from the same problem as the data=None
approach. Except you can't test if the edge data
is 1 in this case. e.g.
'''
G.add_edge(1,2,2)
G.has_edge(1,2,1) is True
}}
Hence the approach of using the ugly **kwds -
that way you don't have to specify a default
value.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-09
I meant that has_edge should ''always'' check for data==1 unless another value is requested. This allows people to check for data==None, and also allows people not using edge data to use has_edge as expected.
'''
G.add_edge(1,2) # default data=1
print G.has_edge(1,2) # True
G.add_edge(1,2,data=None)
print G.has_edge(1,2,data=None) # True
'''
For Graph (modifying [1088]),
'''
def has_edge(self, u, v, data=1):
try:
d=self.adj[u][v]
except KeyError:
return False
return data == d
'''
and for multigraphs,
'''
def has_edge(self, u, v, data=1):
try:
d=self.adj[u][v]
except KeyError:
return False
return data in d
'''
Does that work?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-09
Not quite
'''
I'm still interested in any other options besides using
**kwds or creating another method that requires the data
keyword.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @dschult, 2008-12-09
Might be a surprise with
'''
G.add_edge(1,2,3)
G.has_edge(1,2) #False!
'''
To eliminate surprise I can't see how to have any default value
(though I'm willing to let people be surprised if they use None ;)
The *_kwds method above doesn't allow G.has_edge(1,2,3) but if we check both *_kwds and *args it's OK. Code looks ugly, but with try/except on the initial test and if's for checking args and kwds, a quick test shows it is similar in speed to the other suggestions. Can do more speed testing if that's what people think is best.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-09
Ahh, I see.
'''
G.add_edge(1,2,3)
G.has_edge(1,2) #False
'''
That would be very confusing indeed! Hopefully the **kwds and *args idea works out. Even if it isn't as fast, I think the convenience it provides is probably worth it. As Aric mentioned, when faster checks are required, other methods do exist.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-09
OK - let's see the ugly code :-).
N.B. get_edge() also has this problem with the default= parameter.
You currently can't return a None as default.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-09
How important is it that there exist a default value? Ie, perhaps has_edge() should only be used when querying for (u,v,data) and the data must always be explicitly specified---no _args or *_kwds. If people aren't concerned about checking for data, then has_neighbor(u,v) and similar digraph friends are available. So:
has_edge(u,v,data)
But I suppose this causes consistency issues with:
get_edge(u,v,no_edge=None) # which does not have a required data argument
Note that there is a difference in API:
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-10
Part of the problem here is that we are explicitly not using an
edge object. So what is an edge? A two-tuple of nodes? A
three-tuple of nodes with data? Both?
For NetworkX, my opinion is that an edge is a two-tuple of nodes and
has_edge(u,v) should determine if such an edge exists in the graph.
There might be more than one of those edges (multigraph) in which case
you have to decide, if they are not identical, which one you mean. If
they have different data you can do that.
So if you want to know if there is a particular edge with particular
data then you must check both if the edge exists and if it has the
data you are looking for. Note that it is possible to put data on the
edge that doesn't have an eq() method so in that
you might want to use "is" instead of "==".
Is having has_edge() check for == with the data a "convenience"
and maybe not worth it? Should there be a different method?
I remember once saying "has_edge_with_data()" and decided
one_might_not_want_so_many_special_methods_with_long_names().
But I think something like that is a possible solution here.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-10
Replying to [comment:14 aric]:
Hmm. Now you have me leaning back the other way, towards the original .99 implementation of not allowing a data argument at all for has_edge(). When the default graph type is a weighted graph and thus, potentially floats as weights, it does seem like "==" and "is" could cause confusion.
So, I'm -1 on allowing data to be passed into has_edge() now. From now on, I'll just do something like:
'''
if G.has_edge(u,v):
if not G.multigraph:
pass # compare G[u][v] to something
else:
for data in G[u][v]:
pass # compare data to something
'''
Now, the only place where None is an issue still is with get_edge(), per your earlier comment. Personally, it won't effect me, as I don't use None as edge data, but others might.
One way to handle this is to always raise an exception, and force the user to pre-check with has_edge(u,v). If this is done, it seems like get_edge() could be removed altogether...in favor of the quicker G[u][v] access. If it is not removed, should get_edge() be renamed to get_edgedata(), or something similar, since it only returns the data and doesn't fit the notion of an edge as a 2-tuple?
Argh, sorry for shifting positions on this! In the end, I hope the conversations help you guys figure out what is best for NetworkX.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @dschult, 2008-12-10
Here's the ugly code I timed:
'''
def has_edge(u, v, _args, *_kwds):
try:
d=self.adj[u][v]
except KeyError:
return False
if args:
return d==args[0]
elif kwds:
try:
return d==kwds['data']
except KeyError:
raise(TypeError,'has_edge got an unexpected keyword argument. Expected data=')
return True
'''
I think I prefer to have a function for checking 3-tuples and a different one for 2-tuples.
Could we use a name like has_edgedata(u,v,d) That was in some version at some point...
Perhaps we should allow some user comparison function to choose between ==, is, and
a user provided function (e.g. abs(difference)<1e-8)
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-14
I like that last idea...
'''
def has_edge(u,v):
try:
v in self.adj[u]
except KeyError:
return False
Graphs
def has_edgedata(u,v,data,cmp=None):
try:
d = self.adj[u][v]
except KeyError:
return False
if cmp is None:
return data == d
else:
return cmp(data,d)
Multi(Di)Graphs
def has_edgedata(u,v,data,cmp=None):
try:
d = self.adj[u][v]
except KeyError:
return False
if cmp is None:
return data in d
else:
for x in d:
if cmp(data,x):
return True
return False
def get_edgedata(self, u, v, default=None): # (Di)Graph
def get_edgedata(self, u, v, no_edge=None): # Multi(Di)Graph
# renamed get_edge()
# should 'no_edge' and 'default' have the same name?
'''
Separate functions seem to address the concerns for checking if the graph has edgedata which is None, and the cmp argument allows for flexibility while not adding bloat to has_edge().
A remaining issue for (Di)Graphs is how to distinguish edgedata which is None from the default=None when calling get_edgedata(). Perhaps it should just raise...but then again, why not just call G[u][v]?
For Multi(Di)Graphs, would default=[] be a better fit (keep it an iterable)?
For !DiGraph.get_edge(), a (shallow) copy of the edgedata is returned:
self.adj[u][v][:]
This is (undesirably?) different from if the user had called G[u][v].
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-14
That looks good to me. For get_edgedata() (and I think renaming
it to that is correct) I'd like (for Graph())
'''
def get_edgedata(self, u, v, default=None):
return self.adj[u].get(v,default)
'''
I guess there are only two sensible options for get_edgedata()
if there is no edge - raise an exception or return None.
In this case I think I'd like to return None.
Python dictionaries clearly use this so maybe it won't be a surprise.
'''
In [6]: d={1:None}
In [7]: d.get(1)
In [8]: print d.get(1)
None
In [9]: print d.get(2)
None
'''
First assume you don't use None for your edge data.
Then this will return a None if there is no edge (and thus
no edge data). Otherwise by default in NetworkX there is a 1 there
or something else that the user specified. That should cover
almost all users.
For None-lovers -if you use None as edge data -
you'll have use the default=keyword and be smart about what you set that to.
But it can be done.
Mabye default=Ellipsis ;-/
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-14
Replying to [comment:17 cellison]:
It should be "default".
Definitely don't want to do that. The default value is evaluated only
once when the function object is created.
It's a good question. For MultiGraph if we want G[u][v] to
be the same as G.get_edge(u,v) then something needs to be fixed.
Not sure which is the right way but it would be harder to
make G[u][v] return a copy.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-22
(In [1099]) Rename get_edge() to get_edge_data(). Now returns None as
a default if no edge is found.
Change function of has_edge() to take two aguments (nodes).
It has exactly the same behavior now as has_neighbor()
Addresses #203
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2008-12-22
I made the changes discussed for has_edge() and get_edge_data().
I also modified the behavior of get_edge_data() for MultiGraph so it
does not return a copy (same as G[u][v]).
I'm still not sure about has_edge_data().
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @chebee7i, 2008-12-26
[1099] looks good. As to has_edge_data(), it seems fine to leave it out. One can always grab the data and do a custom check.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2009-02-08