PropertyMap#
- class graph_tool.PropertyMap(pmap, g, key_type)[source]#
Bases:
object
This base class provides a mapping from vertices, edges or whole graphs to arbitrary properties.
See Property maps for more details.
The possible property value types are listed below.
Type name
Alias
bool
uint8_t
int16_t
short
int32_t
int
int64_t
long
,long long
double
float
long double
string
vector<bool>
vector<uint8_t>
vector<int16_t>
short
vector<int32_t>
vector<int>
vector<int64_t>
vector<long>
,vector<long long>
vector<double>
vector<float>
vector<long double>
vector<string>
python::object
object
Methods
coerce_type
([full])Return a copy of the property map with the most appropriate type, i.e. the simplest type necessary to accomodate all the values exactly.
copy
([value_type, full])Return a copy of the property map.
data_ptr
()Return the pointer to memory where the data resides.
get_2d_array
([pos, dtype])Return a two-dimensional array of shape
(M,N)
, whereN
is the number of vertices or edges, andM
is the size of each property vector, which contains a copy of all entries of the vector-valued property map.Get a
numpy.ndarray
subclass (PropertyArray
) pointint to the property values.Get the graph class to which the map refers.
Return True if the property is writable.
key_type
()Return the key type of the map.
Return the python-compatible value type of the map.
reserve
(size)Reserve enough space for
size
elements in underlying container.resize
(size)Resize the underlying container to contain exactly
size
elements.set_2d_array
(a[, pos])Set the entries of the vector-valued property map from a two-dimensional array
a
of shape(M,N)
, whereN
is the number of vertices or edges, andM
is the size of each property vector.set_value
(val)Sets all values in the property map to the same
val
.set_values
(vals)Sets values in the property map to the iterable
vals
.Shrink size of underlying container to accommodate only the necessary amount, and thus potentially freeing memory.
swap
(other)Swap internal storage with
other
.t
(f[, value_type, no_array, inplace])Alias to
transform()
.transform
(f[, value_type, no_array, inplace])Return a copy of the property map with the values transformed by the user-supplied function
f
.Return the value type of the map.
Attributes
Shortcut to the
get_array()
method as an attribute.The same as the
a
attribute, but instead an indexed array is returned, which contains only entries for vertices/edges which are not filtered out.The same as the
a
attribute, but instead anumpy.ma.MaskedArray
object is returned, which contains only entries for vertices/edges which are not filtered out.- coerce_type(full=True)[source]#
Return a copy of the property map with the most appropriate type, i.e. the simplest type necessary to accomodate all the values exactly. If
full == False
, in the case of filtered graphs only the unmasked values are copied (with the remaining ones taking the type-dependent default value).
- copy(value_type=None, full=True)[source]#
Return a copy of the property map. If
value_type
is specified, the value type is converted to the chosen type. Iffull == False
, in the case of filtered graphs only the unmasked values are copied (with the remaining ones taking the type-dependent default value).
- get_2d_array(pos=None, dtype=None)[source]#
Return a two-dimensional array of shape
(M,N)
, whereN
is the number of vertices or edges, andM
is the size of each property vector, which contains a copy of all entries of the vector-valued property map.The parameter
pos
must be a sequence of integers which specifies the indices of the property values which will be copied.If
pos
is not given (the default), then it will be assumed to correspond to the entire range of the first entry in the map.The parameter
dtype
determines the desired data-type for the array. If not given, it will be determined automatically (by applying promotion rules when necessary.)
- get_array()[source]#
Get a
numpy.ndarray
subclass (PropertyArray
) pointint to the property values.Note
An array is returned only if the value type of the property map is a scalar. For vector, string or object types,
None
is returned instead. For vector and string objects, indirect array access is provided via theget_2d_array()
andset_2d_array()
member functions.Warning
This function does not copy the data from the property map, and therefore runs in \(O(1)\) time.
As a consequence, the returned array does not own the data, which belongs to the property map. Therefore, if the graph changes, the array may become invalid. Do not store the array if the graph is to be modified; store a copy instead.
- reserve(size)[source]#
Reserve enough space for
size
elements in underlying container. If the original size is already equal or larger, nothing will happen.
- set_2d_array(a, pos=None)[source]#
Set the entries of the vector-valued property map from a two-dimensional array
a
of shape(M,N)
, whereN
is the number of vertices or edges, andM
is the size of each property vector. If given, the parameterpos
must be a sequence of integers which specifies the indices of the property values which will be set (i.e. rows if thea
matrix).
- shrink_to_fit()[source]#
Shrink size of underlying container to accommodate only the necessary amount, and thus potentially freeing memory.
- t(f, value_type=None, no_array=False, inplace=False)#
Alias to
transform()
.
- transform(f, value_type=None, no_array=False, inplace=False)[source]#
Return a copy of the property map with the values transformed by the user-supplied function
f
. If given,value_type
specifies the value type for the new property map.If the value type of the original map allows, the values will be passed to
f
as anumpy.ndarray
, unless no_array isTrue
. Otherwise, they will be passed individually.If
inplace == True
the transformation happens in-place, without copying the property map. In this case the parametervalue_type
is ignored.
- a#
Shortcut to the
get_array()
method as an attribute. This makes assignments more convenient, e.g.:>>> g = gt.Graph() >>> g.add_vertex(10) <...> >>> prop = g.new_vertex_property("double") >>> prop.a = np.random.random(10) # Assignment from array
- fa#
The same as the
a
attribute, but instead an indexed array is returned, which contains only entries for vertices/edges which are not filtered out. If there are no filters in place, the array is not indexed, and is identical to thea
attribute.Warning
Because advanced indexing is triggered, a copy of the array is returned, not a view, as is the case for the
a
attribute. Nevertheless, the assignment of values to the whole array at once works as expected.Importantly, this means that this operation runs in time \(O(N)\) or O(N + E) for node and edge properties, respectively, where \(N\) is the number of vertices and \(E\) the number of edges in the unfiltered graph, and therefore can be significantly slower than using the
a
attribute, which runs in time \(O(1)\).
- ma#
The same as the
a
attribute, but instead anumpy.ma.MaskedArray
object is returned, which contains only entries for vertices/edges which are not filtered out. If there are no filters in place, a regularPropertyArray
is returned, which is identical to thea
attribute.Warning
Unlike
fa
, the masked array does not copy the property map values.This function runs in time \(O(N)\) or O(N + E) for node and edge properties, respectively, where \(N\) is the number of vertices and \(E\) the number of edges in the unfiltered graph.