Problem with vertex masking

Hello:

Posting this here because I am having difficulty logging into the gitlab page to make a bug report.

The error is: After a vertex mask #1 is applied to a graph, and then the filters are cleared, and then a new vertex mask #2 is applied to the graph, the number of vertices in the graph reflects vertex mask #1 not #2, so that when you reference a vertex in mask #2 that is not in #1, there is a ValueError: invalid vertex.

At first I thought this might be due to clear_filters() not working. But it does appear to work (see example below). Instead, what appears to be the problem is that the new mask is not applied.

Is this a new behavior of clear_filters() or set_vertex_filter() that I’ve missed and need to update my code for? Or is this a bug?

Graph tool version 2.77 works
Graph tool version 2.80 works
Graph tool version 2.83 and beyond does not work

Tested below on Graph tool version 2.97

Thanks,
Justin.

Example…

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import graph_tool as gt
import numpy as np

# Create graph
graph = gt.Graph(directed = False)
graph.add_vertex(n = 500)

# Create mask
mask = np.append(np.ones(5), np.zeros(495))
vertex_condition = graph.new_vertex_property('bool', vals = mask)
graph.set_vertex_filter(vertex_condition)

# Check shapes of trues in mask, vertex property, and graph vertices
print(np.where(mask)[0].shape)
print(vertex_condition.get_array().shape)
print(graph.get_vertices().shape)

# Calculate degree
graph.get_total_degrees([4])

# Reset graph
graph.clear_filters()

# Create new mask
mask_new = np.append(np.ones(10), np.zeros(490))
vertex_condition_new = graph.new_vertex_property('bool', vals = mask_new)
graph.set_vertex_filter(vertex_condition_new)

# Check shapes of trues in mask, vertex property, and graph vertices
print(np.where(mask_new)[0].shape)
print(vertex_condition_new.get_array().shape)
print(graph.get_vertices().shape)

# Calculate degree
# ValueError: invalid vertex here
graph.get_total_degrees([8])

# Despite vertex 8 being True in mask_new
print(mask_new[8])

# What I see in my real code is that print(graph.get_vertices().shape) is one less than print(np.where(mask_new)[0].shape)
# One idea is that it's possible that clear_filters is not doing its job and it's keeping the same vertex mask as before
# Testing this idea...
graph.clear_filters()
print(graph.get_vertices().shape)

# But in fact, it does appear that clear_filters is doing its job
# But for some reason the new mask is not being applied?
print(mask[8])

Output from above…

(5,)
(500,)
(5,)
0
(10,)
(500,)
(5,) *** Note incorrect value here

ValueError                                Traceback (most recent call last)
Cell In[1], line 36
     32 print(graph.get_vertices().shape)
     34 # Calculate degree
     35 # ValueError: invalid vertex here
---> 36 graph.get_total_degrees([8])

File graph_tool/__init__.py:2656, in Graph.get_total_degrees(self, vs, eweight)
   2644 def get_total_degrees(self, vs, eweight=None):
   2645     """Return a :class:`numpy.ndarray` containing the total degrees (i.e. in- plus
   2646     out-degree) of vertex list ``vs``. If supplied, the degrees will be
   2647     weighted according to the edge :class:`~graph_tool.EdgePropertyMap`
   (...)   2654     array([40, 77], dtype=uint64)
   2655     """
-> 2656     return libcore.get_degree_list(self.__graph,
   2657                                    numpy.asarray(vs, dtype="uint64"),
   2658                                    _prop("e", self, eweight), 2)

ValueError: invalid vertex: 8

1.0
(500,)
0.0

Please open an issue about this, and I will investigate. Your login should be working now.

Thanks! I’ve opened an issue.

Justin.