Skip to content

Operations

Basic operations for this environment.

Operations for reading and writing files.

export_to_file ¤

export_to_file(bundle: Bundle, *, table_name: TableName, filename: PathStr, file_format: ExportFileFormat = csv)

Exports a DataFrame to a file.

PARAMETER DESCRIPTION
bundle

The bundle containing the DataFrame to export.

TYPE: Bundle

table_name

The name of the DataFrame in the bundle to export.

TYPE: TableName

filename

The name of the file to export to.

TYPE: PathStr

file_format

The format of the file to export to. Defaults to CSV.

TYPE: ExportFileFormat DEFAULT: csv

import_csv ¤

import_csv(*, filename: PathStr, columns: str = '<from file>', separator: str = '<auto>', table_name: str = 'records')

Imports a CSV file.

import_file ¤

import_file(*, file_path: PathStr, table_name: str, file_format: FileFormat = csv, **kwargs) -> Bundle

Read the contents of the a file into a Bundle.

PARAMETER DESCRIPTION
file_path

Path to the file to import.

TYPE: PathStr

table_name

Name to use for identifying the table in the bundle.

TYPE: str

file_format

Format of the file. Has to be one of the values in the FileFormat enum.

TYPE: FileFormat DEFAULT: csv

RETURNS DESCRIPTION
Bundle

Bundle with a single table with the contents of the file.

TYPE: Bundle

import_graphml ¤

import_graphml(*, filename: PathStr)

Imports a GraphML file.

import_parquet ¤

import_parquet(*, filename: PathStr)

Imports a Parquet file.

Operations for graphs.

connect_nodes ¤

connect_nodes(b: Bundle, *, source_table: TableName, source_id: ColumnNameForSource, source_attribute: ColumnNameForSource, target_table: TableName, target_id: ColumnNameForTarget, target_attribute: ColumnNameForTarget) -> Bundle

Creates edges between nodes from table1 and table2 if the two attributes of the node are equal.

Parameters: - source_table: Name of the first table - source_id: ID column in the first table - source_attribute: Attribute column in the first table used for matching - target_table: Name of the second table - target_id: ID column in the second table - target_attribute: Attribute column in the second table used for matching

define_edges ¤

define_edges(b: Bundle, *, relations: str = '')

Define edges between node tables

discard_loop_edges_in_relation ¤

discard_loop_edges_in_relation(b: Bundle, *, relation: RelationName)

Discards loop edges in the specified relation. :param b: the bundle :param relation: the relation

get_id ¤

get_id(b: Bundle, table_name: str) -> str

Returns the id column of a table.

merge ¤

merge(bundles: list[Bundle], *, merge_mode: BundleMergeMode = must_be_unique)

Merge multiple inputs

merge_nodes ¤

merge_nodes(b: Bundle, *, table_name: TableName, attribute: ColumnNameByTableName, add_suffixes: bool = False, aggregations: AggregationAdderByTableName) -> Bundle

Merges the nodes that have the same value for the given attribute. The aggregations parameter is a list of tuples (column_name, aggregation_function(https://pandas.pydata.org/pandas-docs/stable/reference/groupby.html#dataframegroupby-computations-descriptive-stats)) that specifies which other columns should be included in the new DataFrame and how to aggregate them. :param b: the bundle :param table_name: the name of the table :param attribute: the name of the attribute to merge on :param add_suffixes: whether to add suffixes to the aggregated columns :param aggregations: the aggregations to perform, specified as a list of tuples

merge_parallel_edges ¤

merge_parallel_edges(b: Bundle, *, table_name: TableName, source_key: ColumnNameByTableName, target_key: ColumnNameByTableName, aggregations: AggregationAdderByTableName) -> Bundle

Merges parallel edges, and aggregates the attributes with the specified functions(https://pandas.pydata.org/pandas-docs/stable/reference/groupby.html#dataframegroupby-computations-descriptive-stats). :param b: the bundle :param table_name: the name of the table :param source_key: the name of the key in the source table :param target_key: the name of the key in the target table :param aggregations: the aggregations to perform, specified as a list of tuples

merge_two_attributes ¤

merge_two_attributes(b: Bundle, *, table_name: TableName, new_attribute: str, primary_attribute: ColumnNameByTableName, secondary_attribute: ColumnNameByTableName) -> Bundle

An attribute may not be defined everywhere. This operation uses the secondary attribute to fill in the values where the primary attribute is undefined. If both are undefined then the result is undefined too. :param b: the bundle :param table_name: the name of the table :param new_attribute: the name of the new attribute :param primary_attribute: the primary attribute to use :param secondary_attribute: the secondary attribute to use

pcsf ¤

pcsf(b: Bundle, *, relation: RelationName, price_column: str, weight_column: str, root_cost_column: str, output_edge: str, output_node: str, output_root_nodes: str, output_profit: str)

The prize collecting Steiner tree is a problem that seeks a subtree of a graph that maximizes the total prize collected from the nodes minus the total weight of the edges.

The prize collecting Steiner Forest allows for multiple disjoint trees. This problem has multiple versions, in this case there are a set of nodes that can act as the root of the subtrees, and each such node has a cost for using it as the root of the tree it belongs to. Every subtree must have exactly 1 root.

A use case for this operation could be that we want to create a water supply network, where the water stations can act as the roots, and the houses have prizes, since they are the customers. The piping costs will be the weights of the edges.

This example can be seen in the "In Bruges" workspace in "examples/Peters lessons".

A small example of the PCSF problem:

We have a graph, with 5 nodes: A, B, C, D, E.

The edges with their weights: A-B: 10 B-C: 20 D-E: 40

The nodes with their prizes: A: 0 B: 30 C: 40 E: 25

The potential roots with the costs: A: 15 D: 35

The optimal solution: nodes: A, B, C edges: A-B, B-C roots: A profit: (0 + 30 + 40) - (10 + 20) - (15) = 25

This box provides an approximate solution for the PCSF problem, as it is NP-hard.

:param b: the bundle :param relation: the relation :param price_column: the column with the node prices :param weight_column: the column with the edge weights :param root_cost_column: the column with the root costs :param output_edge: the output column, 1.0 if the edge is part of the forest, None otherwise :param output_node: the output column, 1.0 if the node is part of the forest, None otherwise :param output_root_nodes: the output column, 1.0 if the node is a root node, None otherwise :param output_profit: a table with a single record: the profit

sample_graph ¤

sample_graph(graph: Graph, *, nodes: int = 100)

Takes a (preferably connected) subgraph.

shortest_distance ¤

shortest_distance(b: Bundle, *, relation: RelationName, edge_distances: str, attribute_name: str, starting_distance: str, undirected: bool) -> Bundle

Computes the shortest distance from each node to the starting nodes using the specified edge distances. :param b: the bundle :param relation: the relation to use for the graph :param edge_distances: the distances for the edges :param attribute_name: the name of the attribute for storing the shortest distances :param starting_distance: the name of the attribute for the starting distances :param undirected: whether to treat the graph as undirected or not

supplement_edges ¤

supplement_edges(b: Bundle, *, table_name: TableName) -> Bundle

Adds the attributes of the source and target nodes to the edges in the specified relation. :param b: the bundle :param table_name: the name of the edge table

update_relations ¤

update_relations(b: Bundle, table_name: TableName, new_id: str, mapping: Series) -> Bundle

Updates the relations to use the new id column instead of the old ones. :param b: The bundle :param table_name: The name of the node table that was modified. :param new_id: The name of the new id attribute. :param mapping: Maps the old ids to the new ones.

Operations for machine learning.

define_model ¤

define_model(bundle: Bundle, *, model_workspace: str, save_as: str = 'model')

Trains the selected model on the selected dataset. Most training parameters are set in the model definition.

model_inference ¤

model_inference(bundle: Bundle, *, model_name: PyTorchModelName = 'model', input_mapping: ModelInferenceInputMapping | None, output_mapping: ModelOutputMapping | None, batch_size: int = 1)

Executes a trained model.

train_model ¤

train_model(bundle: Bundle, *, model_name: PyTorchModelName = 'model', input_mapping: ModelTrainingInputMapping | None, epochs: int = 1, batch_size: int = 1)

Trains the selected model on the selected dataset. Training parameters specific to the model are set in the model definition, while parameters specific to the hardware environment and dataset are set here.

train_test_split ¤

train_test_split(bundle: Bundle, *, table_name: TableName, test_ratio: float = 0.1, seed=1234)

Splits a dataframe in the bundle into separate "_train" and "_test" dataframes.

train_test_val_split ¤

train_test_val_split(bundle: Bundle, *, table_name: TableName, test_ratio: float = 0.1, val_ratio: float = 0.1, seed=1234)

Splits a dataframe in the bundle into separate "_train", "_test" and "_val" dataframes.

PyKEEN graph embedding operations.

PyKEENModelName module-attribute ¤

PyKEENModelName = typing.Annotated[str, {'format': 'dropdown', 'metadata_query': "[].other.*[] | [?type == 'pykeen-model'].key"}]

A type annotation to be used for parameters of an operation. PyKEENModelName is rendered as a dropdown in the frontend, listing the PyKEEN models in the Bundle. The model name is passed to the operation as a string.

PyKEENModelWrapper ¤

Wrapper to add metadata method to PyKEEN models for dropdown queries, and to enable caching of model

def_pykeen_with_attributes ¤

def_pykeen_with_attributes(dataset: Bundle, *, interaction_name: PyKEENModel1D = TransE, combination_name: PyKEENCombinations = ConcatProjection, embedding_dim: int, loss_function: str, random_seed: int, save_as: str, **kwargs) -> Bundle

Defines a PyKEEN model capable of using numeric literals as node attributes.

define_pykeen_model ¤

define_pykeen_model(bundle: Bundle, *, model: PyKEENModelMoreD = MuRE, edge_data_table: TableName = 'edges', embedding_dim: int = 50, loss_function: PyKEENSupportedLosses = NSSALoss, seed: int = 42, save_as: str = 'PyKEENmodel')

Defines a PyKEEN model based on the selected model type.

evaluate ¤

evaluate(bundle: Bundle, *, model_name: PyKEENModelName = 'PyKEENmodel', evaluator_type: EvaluatorTypes = RankBasedEvaluator, eval_table: TableName = 'edges_test', additional_true_triples_table: TableName = 'edges_train', metrics_str: str = 'ALL', batch_size: int = 32)

Evaluates the given model on the test set using the specified evaluator type. Args: evaluator_type: The type of evaluator to use. Note: When using classification based methods, evaluation may be extremely slow. metrics_str: Comma separated list, "ALL" if all metrics are needed.

factory_to_df ¤

factory_to_df(factory: CoreTriplesFactory) -> DataFrame

Convert a TriplesFactory to a DataFrame with labeled columns.

full_predict ¤

full_predict(bundle: Bundle, *, model_name: PyKEENModelName = 'PyKEENmodel', k: int | None = None, inductive_setting: bool = False)

Warning: This prediction can be a very expensive operation!

PARAMETER DESCRIPTION
k

Pass "" to keep all scores

TYPE: int | None DEFAULT: None

get_inductive_model ¤

get_inductive_model(bundle: Bundle, *, triples_table: TableName, inference_table: TableName, interaction: PyKEENModel1D = DistMult, embedding_dim: int = 200, loss_function: str, num_tokens: int = 2, aggregation: PyTorchAggregationFunctions = MLP, use_GNN: bool = False, seed: int = 42, save_as: str = 'InductiveModel')

Defines an InductiveNodePiece model (with an optional GNN message passing layer) for inductive link prediction tasks.

PARAMETER DESCRIPTION
triples_table

The transductive edges of the graph.

TYPE: TableName

inference_table

The inductive edges of the graph.

TYPE: TableName

interaction

Type of interaction the model will use for link prediction scoring.

TYPE: PyKEENModel1D DEFAULT: DistMult

num_tokens

Number of hash tokens for each node representation, usually 66th percentiles of the number of unique incident relations per node.

TYPE: int DEFAULT: 2

aggregation

Aggregation of multiple token representations to a single entity representation. Pick a top-level torch function, or use 'mlp' for a two-layer built-in mlp aggregator.

TYPE: PyTorchAggregationFunctions DEFAULT: MLP

import_inductive_dataset ¤

import_inductive_dataset(*, dataset: InductiveDataset = ILPC2022Small)

Imports an inductive dataset from the PyKEEN library.

import_pykeen_dataset_path ¤

import_pykeen_dataset_path(self, *, dataset: PyKEENDataset = Nations) -> Bundle

Imports a dataset from the PyKEEN library.

inductively_split_dataset ¤

inductively_split_dataset(bundle: Bundle, *, dataset_table: TableName, entity_ratio: float = 0.5, training_ratio: float = 0.8, testing_ratio: float = 0.1, validation_ratio: float = 0.1, seed: int = 42)

Splits incoming data into 4 subsets. Transductive training on which training should be run, inductive inference on which during training inference is done. Inference testing and validation sets that can be used to evaluate model performance.

PARAMETER DESCRIPTION
entity_ratio

How many percent of the entities in the dataset should be in the transductive training graph. If 0 semi-inductive split is applied, else fully-inductive split is applied

TYPE: float DEFAULT: 0.5

training_ratio

When semi-inductive this is entity ratio, when fully-inductive this is the inference training split

TYPE: float DEFAULT: 0.8

testing_ratio

When semi-inductive this is entity ratio, when fully-inductive this is the inference testing split

TYPE: float DEFAULT: 0.1

validation_ratio

When semi-inductive this is entity ratio, when fully-inductive this is the inference validation split

TYPE: float DEFAULT: 0.1

prepare_triples ¤

prepare_triples(triples_df: DataFrame, entity_to_id: Optional[Mapping[str, int]] = None, relation_to_id: Optional[Mapping[str, int]] = None, inv_triples: bool = False, numeric_literals: Optional[DataFrame] = None) -> TriplesFactory | TriplesNumericLiteralsFactory

Prepare triples for PyKEEN from a DataFrame.

req_inverse_triples ¤

req_inverse_triples(model: Model | PyKEENModel1D | PyKEENModelMoreD) -> bool

Check if the model requires inverse triples.

target_predict ¤

target_predict(bundle: Bundle, *, model_name: PyKEENModelName = 'PyKEENmodel', head: str, relation: str, tail: str, inductive_setting: bool = False)

Leave the target prediction field empty

SQL and Cypher.

cypher ¤

cypher(bundle: Bundle, *, query: LongStr, save_as: str = 'results')

Run a Cypher query on the graph in the bundle. Save the results as a new DataFrame.

sql ¤

sql(bundle: Bundle, *, query: LongStr, save_as: str = 'results')

Run a SQL query on the DataFrames in the bundle. Save the results as a new DataFrame.

Visualizations.

binned_graph_visualization ¤

binned_graph_visualization(self, b: Bundle, *, x_property: str, y_property: str, x_bins=5, y_bins=5, show_loops: bool = False)

Nodes binned together by x and y are aggregated into one node. Edges between bins are aggregated into one edge.

visualize_graph ¤

visualize_graph(b: Bundle, *, chip_data: str = '')

Visualizes the graph using ECharts and allows the user to customize the visualization through "chips". :param b: the bundle :param chip_data: the frontend uses this parameter to store relevant data of the chips

Automatically wraps all NetworkX functions as LynxKite operations.