Package: visual-graph-datasets
🖼️ Visual Graph DatasetsThis package supplies management and utilities forgraph datasetsused to traingraph neural networksand more specifically aimed atexplainable AI (XAI)methodsW.r.t to the structure and management of these datasets this package employs a different philosophy. Instead of the
usual minimal packaging to CSV files, a visual graph dataset (VGD) represents eachdataset as a folderwhere
each element is represented by two files:Ajsonfile containing metadata information, including thefull graph representationApngfile containing a canonical visualization of the graph.We believe that this dataset format provides the following examples:The computational load of pre-processing large datasets into graph representations for model training isshiftedtowards a data storage load.A VGD dataset provides acanonicalgraph represenation which makes downstream prediction results more
comparable since implementations do not differ w.r.t. pre-processing anymore.A VGD dataset provides acanonicalgraph visualization which makes downstreamexplanationresults more
comparable. Additionally, simply having to load a single image instead of re-computing the visualization
everytime makes the visualization of explanation results more computationally efficient.🔥 FeaturesDataset format specifically forXAIapplications based onGraphstructured data.Currently supports color graphs and molecular graphs.Standard multiple functions to visualize graph masks / attributional explanations on top of the
canonical graph visualizations in different styles.📦 Installation by SourceFirst clone this repository:git clone https://github/aimat-lab/visual_graph_datasets.gitThen navigate into the cloned directory and install it like this:cd visual_graph_datasets
pip3 install -e .Optional.Alternatively the package can also be installed usingpoetrypoetry install📦 Installation by PackageThe package is also published to PyPi and can directly be installed like this:pip3 install visual_graph_datasets🚀 QuickstartThe datasets are mainly intended to be used in combination with other packages, but this package provides
some basic utilities to load and explore the datasets themselves within python programs.importosimporttypingastimportmatplotlib.pyplotaspltfromvisual_graph_datasets.configimportConfigfromvisual_graph_datasets.webimportensure_datasetfromvisual_graph_datasets.dataimportVisualGraphDatasetReaderfromvisual_graph_datasets.visualization.baseimportdraw_imagefromvisual_graph_datasets.visualization.importancesimportplot_node_importances_borderfromvisual_graph_datasets.visualization.importancesimportplot_edge_importances_border# This object will load the settings from the main config file. This config file contains options# such as changing the default datasets folder and defining custom alternative file share providersconfig=Config()config.load()# First of all we need to make sure that the dataset exists locally, this function will download it from# the default file share provider if it does not exist.ensure_dataset('rb_dual_motifs',config)# Afterwards we can be sure that the datasets exists and can now load it from the default datasets path.# The data will be loaded as a dictionary whose int keys are the indices of the corresponding elements# and the values are dictionaries which contain all the relevant data about the dataset element,# (Dataset format is explained below)dataset_path=os.path.join(config.get_datasets_path(),'rb_dual_motifs')reader=VisualGraphDatasetReader(dataset_path)data_index_map:t.Dict[int,dict]=reader.read()# Using this information we can visualize the ground truth importance explanation annotations for one# element of the dataset like this.index=0data=data_index_map[index]# This is the dictionary which represents the graph structure of the dataset element. Descriptive# string keys and numpy array values.g=data['metadata']['graph']fig,ax=plt.subplots(ncols=1,nrows=1,figsize=(10,10))draw_image(ax,image_path=data['image_path'])plot_node_importances_border(ax=ax,g=g,node_positions=g['image_node_positions'],node_importances=g['node_importances_2'][:,0],)plot_edge_importances_border(ax=ax,g=g,node_positions=g['image_node_positions'],edge_importances=g['edge_importances_2'][:,0],)fig_path=os.path.join(os.getcwd(),'importances.pdf')fig.savefig(fig_path)📖 DocumentationThere does not yet exist a dedicated documentation, but to learn more about the package please refer to thevisual_graph_datasets/examplesfolder.
The example files listed there are supposed to chronologically illustrate the most important features of the
package by example. Additionally, each example module contains abundant comments that describe each new feature
or concept.🔄 Converting DatasetsSince datasets are not naturally given in the VGD format, one important feature is the conversion of
datasets from their domain-specific representations into such VGD dataset. This process will be described
in the following section. Generally, there exist some pre-implemented conversion functionality for some
common dataset formats.These standard implementations are in the format ofpycomexexperiment modules, which can beextendedby sub experiment modules. Within these new experiment modules, only the global configurations
variables have to be adapted to a new, custom dataset.Molecular Graph Datasets from SMILES CSV FilesOne particularly important class of graph datasets is based on molecular graphs, which are especially
relevant for the domains of chemistry and material science. Most often, datasets of molecular graphs are
given as CSV files which contain the string SMILES representation of a molecule and some additional
target value annotations to be trained on.To convert such a dataset, you can extend the base experimentgenerate_molecule_dataset_from_csv.pyas
shown in the code example below. For this purpose simply create aNEWexperiment file in the experiments
folder and inherit from the base experiment."""new file: generate_molecule_dataset_from_csv__custom.py"""importosimporttypingastfrompycomex.functional.experimentimportExperimentfrompycomex.utilsimportfolder_path,file_namespacefromvisual_graph_datasets.utilimportEXPERIMENTS_PATH# == CUSTOMIZE HERE ==# Insert the absolute path to your CSV hereCSV_FILE_NAME:str='../absolute/path/to/csv'# This has to be the string name of the CSV column which contains# the SMILES string representation of the moleculesSMILES_COLUMN_NAME:str='smiles'# Define whether the dataset is regression or classification.# NOTE: For classification, there has to be ONE COLUMN for EACH#       class, which contains a 0/1 boolean value for the class#       annotation.TARGET_TYPE:str='regression'# alternatively: 'classification'# This has to be the string name of the CSV column which contains the target value.# You may also give multiple column names here for a multi-regression dataset or a# classification dataset.TARGET_COLUMN_NAMES:t.List[str]=['value']# A unique string name for the datasetDATASET_NAME:str='custom'# == DO NOT CHANGE ==# This will invoke the actual implementation of the conversion. After this code has# finished executing, you can find the dataset folder within the# "results/generate_molecule_dataset_from_csv__custom" folder that has been createdexperiment=Experiment.extend(os.path.join(EXPERIMENTS_PATH,'generate_molecule_dataset_from_csv.py'),base_path=folder_path(__file__),namespace=file_namespace(__file__),glob=globals(),)experiment.run_if_main()📟 Command Line InterfaceDownload datasetsNOTE: Westronglyencourage to store datasets on an SSD instead of an HDD, as this can make a
difference of multiple hours(!) when loading especially large datasets (100k+ elements).Datasets can simply be downloaded by name by using thedownloadcommand:// Example for the dataset 'rb_dual_motifs'
python3 -m visual_graph_datasets.cli download "rb_dual_motifs"By default this dataset will be downloaded into the folder$HOME/.visual_graph_datasets/datasetswhere HOME is the current users home directory.The dataset download destination can be changed in a config file by using theconfigcommand:python3 -m visual_graph_datasets.cli configThis command will open the config file at$HOME/.visual_graph_datasets/config.yamlusing the systems
default text editor.List available datasetsYou can display a list of all the currently available datasets of the current remote file share provider
and some metadata information about them by using the commandlist:python3 -m visual_graph_datasets.cli listDataset FormatVisual Graph Datasets are represented asfolderscontaining multiple files. The primary content of these
dataset folders is made up of2files per element in the dataset:A PNG file. This is the canonical visualization of the graph which can subsequently be used to create
explanation visualizations as well. The pixel position of each node in the graph is attached as metadata
of the graph representation.A JSON file. Primarily contains thefullgraph representation consisting of node attributes, edge
attributes, an edge list etc. May also contain custom metadata for each graph depending on the dataset.Additionally, a dataset folder may also contain a.meta.ymlfile which contains additional metadata about
the dataset as a whole.Also, a dataset folder contains a python moduleprocess.pyit contains the standalone implementation
of the preprocessing procedure which turns a domain-specific graph representation (think of SMILES codes
for molecular graphs) into valid graph representations for that specific dataset.
This module can be imported and used directly from python code. Alternatively, the module can be used as a
standalone command line application for programming language agnostic preprocessing of elements.Element Metadata JSONOne such metadata file belonging to one element of the dataset may have the following nested structure:target: a 1d array containing the target values for the element. For classification this usually
a one-hot encoded vector of classes already. For multi-task regression this vector may have an
arbitrary number of continuous regression targets. For single-task regression this will still be a
vector, albeit with the shape (1, )index: The canonical index of this element within the dataset(train_splitoptional) A list of int indices, where each index represents a different split.
if the number “1” is for example part of this list, that means that the corresponding element is
considered to be part of the training set of split “1”. What each particular split is may be described
in the documentation of the dataset.(test_splitoptional) A list of int indices, where each index represents a different split.
if the number “1” is for example part of this list, that means that the corresponding element is
considered to be part of the test set of the split “1”.graph: A dictionary which contains the entire graph representation of this element.node_indices: array of shape (V, 1) with the integer node indices.node_attributes: array of shape (V, N)edge_indices: array of shape (E, 2) which are the tuples of integer node indices that
determine edgesedge_attributes: array of shape (E, M)node_positionsarray of shape (V, 2) which are the xy positions of each node in pixel
values within the corresponding image visualization of the element. This is the crucial
information which is required to use the existing image representations to visualize attributional
explanations!(node_importances_{K}_{suffix}optional) array of shape (V, K) containing ground truth node importance
explanations, which assign an importance value of 0 to 1 to each node of the graph across K channels.
One dataset element may have none or multiple such annotations with different suffixes
determining the number of explanation channels and origin.(edge_importances_{K}_{suffix}optional) array of shape (E, K) containing ground truth edge importance
explanations, which assign an importance value of 0 to 1 to each edge of the graph across K channels.
One dataset element may have none or multiple such annotations with different suffixes
determining the number of explanation channels and origin.Assuming the following shape definitions:V - the number of nodes in a graphE - the number of edges in a graphN - the number of node attributes / features associated with each nodeM - the number of edge attributes / features associated with each edgeK - the number of importance channelsDataset Metadata YMLOne such metadata file may have the following nested structure. Additionally, it may also contain custom
additional fields depending on each dataset.version: A string determining the current version of the datasetdescription: Short string description of what the dataset is about (for example where the data came
from, what types of graphs it consists of, what the prediction target is etc.)visualization_description: String description of what can be seen in the visualization of the graph.
There are many different types of graphs out there which may have very domain specific visualizations.
This string should provide a short description of how the visualizations may be interpreted.references: A list of strings, where each string is a short description of online resources which are
relevant to the dataset, usually including a URL. This could for example include references to scientific
publications where a dataset was first introduced.file_size: The integeraccumulatedsize of all the files that make up the dataset in bytes.num_elements: The integer number of elements in the datasetnum_targets: The size of the prediction target vectornum_node_attributes: The size of the node attribute vectornum_edge_attributes: The size of the edge attribute vector📊 DatasetsHere is a list of the datasets currently uploaded on the main file share provider.For more information about the individual datasets use thelistcommand in the CLI (see above).rb_dual_motifs- A synthetically created graph regression dataset consisting of randomly generated color graphs
which are seeded with special value-determining motifs. Each graph contains between 0 and 2 motifs, which each contribute
a fixed value to the overall graph target value.aqsoldb- A dataset of ~10k molecular graphs annotated with the experimentally determined water solubility values.🤝 CreditsPyComexis a micro framework that simplifies the creation, execution and managmenet of computation experiments
with python.