Graphs Data API

Data

The data module provides tools for generating and managing graph datasets.

graphcalc.graphs.data.data_generation.all_properties(graphs: list) DataFrame[source]

Compute the full knowledge table of graph properties and invariants.

This function evaluates all available invariants and Boolean properties implemented in the graphcalc package (as listed in GRAPHCALC_PROPERTY_LIST) on each graph in the input collection. The results are aggregated into a pandas DataFrame, where each row corresponds to a graph instance and each column corresponds to a specific property or invariant.

Parameters:

graphs (list of networkx.Graph) – A collection of NetworkX graphs.

Returns:

A DataFrame where:
  • Rows correspond to the input graphs.

  • Columns correspond to the full set of graphcalc invariants and Boolean properties defined in GRAPHCALC_PROPERTY_LIST.

Return type:

pandas.DataFrame

Raises:

Exception – If any property function raises an error during execution for any graph.

Notes

This is a convenience wrapper around compute_knowledge_table() that uses the complete list of invariants and properties available in the graphcalc package. Use this if you want a comprehensive “fingerprint” of each graph in your dataset.

Examples

>>> import graphcalc.graphs as gc
>>> from graphcalc.graphs.generators import cycle_graph, path_graph
>>> G1 = cycle_graph(6)
>>> G2 = path_graph(5)
>>> df = gc.all_properties([G1, G2])
>>> df.columns[:5]  # show a few property names
Index(['order', 'size', 'connected', 'diameter', 'radius'], dtype='object')
graphcalc.graphs.data.data_generation.append_graph_row(df: DataFrame, G) DataFrame[source]

Append a new row to an existing knowledge table with the properties of a new graph.

Parameters:
  • df (pandas.DataFrame) – Existing knowledge table (as returned by compute_full_knowledge_table or compute_knowledge_table).

  • G (networkx.Graph) – A new graph to analyze.

Returns:

A new DataFrame with the additional row for G.

Return type:

pandas.DataFrame

Examples

>>> import graphcalc.graphs as gc
>>> from graphcalc.graphs.generators import cycle_graph, path_graph
>>> df = gc.all_properties([cycle_graph(5)])
>>> df.shape[0]
1
>>> df = gc.append_graph_row(df, path_graph(4))
>>> df.shape[0]
2
graphcalc.graphs.data.data_generation.compute_graph_properties(function_names, graph, return_as_dict=True)[source]

Compute graph properties based on a list of function names.

This function takes a list of string function names (defined in either the graphcalc or networkx packages) and a NetworkX graph as input. It computes the values of these functions on the given graph and returns the results either as a dictionary (default) or a list.

Parameters:
  • function_names (list of str) – A list of function names (as strings) defined in the graphcalc or networkx packages.

  • graph (networkx.Graph) – The input graph on which the functions will be evaluated.

  • return_as_dict (bool, optional) – If True (default), returns a dictionary mapping function names to their computed values. If False, returns a list of computed values in the same order as the input function_names.

Returns:

By default, a dictionary where keys are function names and values are the computed values. If return_as_dict=False, a list of computed values is returned.

Return type:

dict or list

Raises:
  • AttributeError – If a function name in function_names does not exist in either graphcalc or networkx.

  • Exception – If any function in function_names raises an error during execution.

Examples

>>> import graphcalc.graphs as gc
>>> from graphcalc.graphs.generators import cycle_graph
>>> G = cycle_graph(6)  # A cycle graph with 6 nodes
>>> function_names = ["spectral_radius", "number_of_nodes"]
>>> dictionary_solution = gc.compute_graph_properties(function_names, G)
>>> list_solution = gc.compute_graph_properties(function_names, G, return_as_dict=False)
graphcalc.graphs.data.data_generation.compute_knowledge_table(function_names: list, graphs: list) DataFrame[source]

Compute graph properties for a collection of NetworkX graphs and return a pandas DataFrame.

This function takes a list of string function names (defined in the graphcalc package) and a collection of NetworkX graphs. It computes the specified properties for each graph and organizes the results in a DataFrame, where each row corresponds to a graph instance and each column corresponds to a function name and its computed value.

Parameters:
  • function_names (list of str) – A list of function names (as strings) defined in the graphcalc package.

  • graphs (list of networkx.Graph) – A collection of NetworkX graphs.

Returns:

A DataFrame where each row represents a graph and each column represents a computed graph property.

Return type:

pandas.DataFrame

Raises:
  • AttributeError – If a function name in function_names does not exist in the graphcalc package.

  • Exception – If any function in function_names raises an error during execution for any graph.

Examples

>>> import graphcalc.graphs as gc
>>> from graphcalc.graphs.generators import path_graph, cycle_graph
>>> G1 = cycle_graph(6)
>>> G2 = path_graph(5)
>>> function_names = ["spectral_radius", "algebraic_connectivity"]
>>> graphs = [G1, G2]
>>> df = gc.compute_knowledge_table(function_names, graphs)
graphcalc.graphs.data.data_generation.expand_list_columns(df: DataFrame)[source]

Expand columns with list entries into separate columns.

For each column in the dataframe that contains lists as entries, this function: 1. Finds the maximum length (N) of the lists in the column. 2. Creates new columns for each index in the list, named as “<column_name>[i]”. 3. Fills missing entries with 0 for lists shorter than N.

Parameters:

df (pandas.DataFrame) – The input dataframe with list-valued columns.

Returns:

A new dataframe with list-valued columns expanded into separate columns.

Return type:

pandas.DataFrame

Examples

>>> data = {'graph_id': [1, 2, 3],
...         'p_vector': [[3, 0, 1], [2, 1], []]}
>>> df = pd.DataFrame(data)
>>> new_df = expand_list_columns(df)