### Setup Development Environment with uv Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/installation.md Commands for setting up a development environment from source using the uv package manager, including cloning, syncing, and testing. ```bash $ curl -Ls https://astral.sh/uv/install.sh | sh $ git clone https://github.com/deepgraph/deepgraph.git $ cd deepgraph $ uv sync $ uv sync --extra $ uv sync --all-extras $ uv run pytest . $ cd doc $ make html ``` -------------------------------- ### Install and Initialize DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/README.rst Instructions for installing the DeepGraph package via pip or conda, followed by the standard import and help access pattern. ```bash pip install deepgraph conda install -c conda-forge deepgraph ``` ```python import deepgraph as dg help(dg) ``` -------------------------------- ### Install DeepGraph via pip Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/installation.md Commands to install the DeepGraph package using pip, including options for installing specific or all optional dependencies. ```bash $ pip install deepgraph $ pip install deepgraph[plot,basemap,tables,scipy,networkx] $ pip install deepgraph[all] ``` -------------------------------- ### Partition Edges by Multiple Source Features (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This example partitions edges using both 'color' and 'size' source features, aggregating 'same_color' and 'inds'. ```python g.partition_edges(source_features=['color', 'size'], relation_funcs={'same_color': 'sum', 'inds': lambda x: tuple(x)}) ``` -------------------------------- ### Partition Edges by Source and Target Features (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This example partitions edges using the 'color' of source nodes and the 'size' of target nodes, aggregating 'same_color' and 'inds'. ```python g.partition_edges(source_features='color', target_features='size', relation_funcs={'same_color': 'sum', 'inds': lambda x: tuple(x)}) ``` -------------------------------- ### Install DeepGraph via Conda Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/installation.md Commands to install DeepGraph using the conda package manager and how to manage optional dependencies manually. ```bash $ conda install -c conda-forge deepgraph $ conda install scipy ``` -------------------------------- ### Partition Edges by Relation 'larger_than' (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This example partitions the graph's edges based on the 'larger_than' relation and applies aggregation functions ('mean', 'std' for 'dx', 'sum' for 'same_color'). ```python g.partition_edges(relations='larger_than', relation_funcs={'dx': ['mean', 'std'], 'same_color': 'sum'}) ``` -------------------------------- ### Configure Conda Environment with YAML Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/installation.md A YAML configuration file for creating a complete DeepGraph development environment with all necessary dependencies. ```yaml name: deepgraph-env channels: - conda-forge dependencies: - python>=3.9 - numpy>=1.21.6 - pandas>=1.2 - matplotlib>=3.1 - basemap>=2.0 - pytables>=3.7 - scipy>=1.5.4 - networkx>=2.4 - graph-tool>=2.27 - deepgraph ``` -------------------------------- ### Partition a graph into a supergraph using DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_graph.md This example demonstrates how to use the partition_graph method to aggregate nodes and edges based on specific features. It shows how to pass aggregation dictionaries and toggle the inclusion of node/edge counts. ```python import deepgraph as dg # Assuming g is an instance of DeepGraph # Partition nodes by 'category' and edges by 'type' sv, se = g.partition_graph( features='category', feature_funcs={'value': 'mean'}, relation_funcs={'weight': 'sum'}, n_nodes=True, n_edges=True ) ``` -------------------------------- ### Display Node Data in DeepGraph (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This example demonstrates how to access and display the node data within a DeepGraph object. It's a simple display operation. ```python g.v ``` -------------------------------- ### Setup and Data Preparation for Parallel Correlation Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/pairwise_correlations.md Initializes the environment by importing necessary libraries and generating a synthetic dataset. The data is whitened and saved to disk in binary format to facilitate memory-mapped access during parallel processing. ```python import os from multiprocessing import Pool import numpy as np import pandas as pd import deepgraph as dg from numpy.random import RandomState prng = RandomState(0) n_features = int(5e3) n_samples = int(1e2) X = prng.randint(100, size=(n_features, n_samples)).astype(np.float64) # Whiten variables for fast parallel computation X = (X - X.mean(axis=1, keepdims=True)) / X.std(axis=1, keepdims=True) # Save in binary format np.save('samples', X) ``` -------------------------------- ### Using HDFStore with Fast-Track Edge Creation (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md This Python example illustrates how to use create_edges_ft with data stored in an HDFStore. It shows how to put a DataFrame into an HDFStore, initialize DeepGraph with the store, and then use create_edges_ft, where only necessary data is loaded from the store during computation. ```python import pandas as pd import deepgraph as dg v = pd.DataFrame({'time': [-3.6,-1.1,1.4,4., 6.3], 'x': [-3.,3.,1.,12.,7.]}) vstore = pd.HDFStore('vstore.h5') vstore.put('node_table', v, format='t', data_columns=True, index=False) g = dg.DeepGraph(vstore) # Example of using create_edges_ft with the HDFStore instance # g.create_edges_ft(ft_feature=('time', 5)) # This line would follow to create edges ``` -------------------------------- ### Parallel Edge Creation with Fast-Track (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md This Python example shows how to use the from_pos and to_pos arguments in create_edges_ft for parallel processing. It computes relations only for a specified range of source nodes, demonstrating control over the computation scope. ```python import pandas as pd import deepgraph as dg v = pd.DataFrame({'time': [-3.6,-1.1,1.4,4., 6.3], 'x': [-3.,3.,1.,12.,7.]}) g = dg.DeepGraph(v) g.create_edges_ft(ft_feature=('time', 5), from_pos=1, to_pos=3) ``` -------------------------------- ### Initialize DeepGraph with HDFStore for Node Data Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges.md Illustrates how to initialize a DeepGraph instance using an HDFStore containing the node table. This method is beneficial for large node tables and parallel computation as only requested data is loaded. The example shows storing a DataFrame in HDF5 and then initializing DeepGraph with it. ```python >>> vstore = pd.HDFStore('vstore.h5') >>> vstore.put('node_table', v, format='t', index=False) >>> g = dg.DeepGraph(vstore) >>> g.v File path: vstore.h5 /node_table frame_table (typ->appendable,nrows->3,ncols->2, indexers->[index]) >>> g.create_edges(connectors=time_difference) >>> g.e dt s t 0 1 2 2 9 1 2 7 ``` -------------------------------- ### Import Libraries and Setup for DeepGraph Tutorial Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.ipynb Imports necessary Python libraries for data manipulation, plotting, and the deepgraph library. It also configures pandas display options for better readability of DataFrames. ```python # data i/o import os import subprocess import zipfile # for plots import matplotlib.pyplot as plt # the usual import numpy as np import pandas as pd import deepgraph as dg # notebook display %matplotlib inline pd.options.display.max_rows = 10 pd.set_option('expand_frame_repr', False) ``` -------------------------------- ### Creating Edges with Fast-Track Feature (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md This Python example demonstrates the basic usage of create_edges_ft. It initializes a DeepGraph with a pandas DataFrame and then calls create_edges_ft, providing a tuple for the ft_feature. This creates edges where the time difference between nodes is less than or equal to the specified threshold. ```python import pandas as pd import deepgraph as dg v = pd.DataFrame({'time': [-3.6,-1.1,1.4,4., 6.3], 'x': [-3.,3.,1.,12.,7.]}) g = dg.DeepGraph(v) g.create_edges_ft(ft_feature=('time', 5)) ``` -------------------------------- ### Initialize Environment and Dependencies Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.ipynb Imports essential libraries for data manipulation, visualization, and the DeepGraph framework. Configures notebook display settings for optimal data exploration. ```python import os import xarray import matplotlib.pyplot as plt import numpy as np import pandas as pd import deepgraph as dg from IPython.display import HTML %matplotlib inline plt.rcParams['figure.figsize'] = 8, 6 pd.options.display.max_rows = 10 pd.set_option('expand_frame_repr', False) ``` -------------------------------- ### GET /return_nx_graph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.md Returns a networkx.DiGraph representation of the graph. ```APIDOC ## GET /return_nx_graph ### Description Converts the DeepGraph object into a networkx directed graph. ### Method GET ### Endpoint /return_nx_graph ### Parameters #### Query Parameters - **features** (list) - Optional - Node or edge features to include. - **relations** (list) - Optional - Relations to include. - **dropna** (bool) - Optional - Whether to drop NaN values. ### Response #### Success Response (200) - **graph** (networkx.DiGraph) - The networkx directed graph object. ``` -------------------------------- ### GET /return_cs_graph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.md Returns a scipy.sparse.coo_matrix representation of the graph. ```APIDOC ## GET /return_cs_graph ### Description Returns the graph structure as a scipy.sparse.coo_matrix. ### Method GET ### Endpoint /return_cs_graph ### Parameters #### Query Parameters - **relations** (list) - Optional - List of relations to include. - **dropna** (bool) - Optional - Whether to drop NaN values. ### Response #### Success Response (200) - **matrix** (scipy.sparse.coo_matrix) - The sparse matrix representation of the graph. ``` -------------------------------- ### Access help documentation for output_names Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges.md Retrieves the help documentation for the output_names decorator to understand its usage and parameters. ```python help(dg.output_names) ``` -------------------------------- ### GET /DeepGraph/return_nx_graph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/api_reference.md Converts the internal DeepGraph structure into a NetworkX DiGraph object. ```APIDOC ## GET /DeepGraph/return_nx_graph ### Description Exports the current graph structure as a networkx.DiGraph object for use in external network analysis tools. ### Method GET ### Endpoint /DeepGraph/return_nx_graph ### Parameters #### Query Parameters - **features** (list) - Optional - Node features to include in the graph. ### Response #### Success Response (200) - **graph** (object) - A networkx.DiGraph instance. ### Response Example { "graph_type": "networkx.DiGraph", "node_count": 100 } ``` -------------------------------- ### Create Edges using FastTrack in DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/intro_to_deepgraph.md Demonstrates how to sort the node DataFrame and utilize the create_edges_ft method for efficient edge generation based on a feature threshold. ```python g.v.sort_values('x', inplace=True) g.create_edges_ft(ft_feature=('x', 1000)) g ``` -------------------------------- ### Create Edges using FastTrack with 'x' Feature (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/intro_to_deepgraph.ipynb Creates edges using the FastTrack method, optimizing for the 'x' feature with 1000 bins. This is a quick way to establish connections based on spatial proximity. ```python g.create_edges_ft(ft_feature=('x', 1000)) g ``` -------------------------------- ### Partition DeepGraph by 'color' and 'size' (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_graph.md Illustrates partitioning a DeepGraph using multiple features ('color' and 'size'). It also shows how to customize relation functions for the partitioning process. ```python sv, se = g.partition_graph( ['color', 'size'], relation_funcs={'inds': lambda x: tuple(x)}) print(sv) print(se) ``` -------------------------------- ### Specify Output Names for Connector Function (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.output_names.md This example demonstrates how to use the @dg.output_names decorator to explicitly name the output variables ('dx', 'dt', 'v') of a connector function that calculates velocity. ```python import deepgraph as dg @dg.output_names("dx", "dt", "v"): def velocity(x_s, x_t, time_s, time_t): dx = x_t - x_s dt = time_t - time_s v = dx/dt return dx, dt, v ``` -------------------------------- ### Get Unique Node Layers Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.ipynb Extracts and prints the unique layer names present in the node table. This helps in understanding the different categories or groups within the multilayer network. ```python print(g.v.layer.unique()) ``` -------------------------------- ### Specify Output Names for Selector Function (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.output_names.md This example shows the usage of the @dg.output_names decorator to define output variable names ('sources', 'targets', 'dx') for a selector function that filters based on distance. ```python import deepgraph as dg @dg.output_names("sources", "targets", "dx") def distance_selector(x_s, x_t, sources, targets): dx = x_t - x_s sources = sources[dx <= 5] targets = targets[dx <= 5] return sources, targets, dx ``` -------------------------------- ### Partition Edges by Source Feature 'color' (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This snippet partitions edges based on the 'color' feature of the source nodes and aggregates the 'same_color' relation. ```python g.partition_edges(source_features='color', relation_funcs={'same_color': 'sum'}) ``` -------------------------------- ### Get Unique Edge Types Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.ipynb Lists the unique types of edges present in the edge table. In this context, edge types often correspond to the layers of the connected nodes, indicating intra-layer connections. ```python g.e['type'].unique() ``` -------------------------------- ### Partition Nodes and Aggregate Features Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.md Demonstrates how to partition graph nodes based on specific IDs and apply custom aggregation functions to features like volume, latitude, and longitude. ```python def count(cp): return len(set(cp[cp != 0])) feature_funcs = {'cp': [count], 'vol': [np.sum], 'lat': np.min, 'lon': np.min} gv = g.partition_nodes('g_id', feature_funcs) gv.rename(columns={'lat_amin': 'lat', 'lon_amin': 'lon'}, inplace=True) ``` -------------------------------- ### Append Family Membership Column Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.ipynb Appends a family membership column ('F') to a DataFrame 'v', initializing it with -1. It then groups by 'F' and applies a lambda function to get unique index values for each group. ```python # create F col v['F'] = np.ones(len(v), dtype=int) * -1 gcpv = cpv.groupby('F') it = gcpv.apply(lambda x: x.index.values) for F in range(len(it)): cp_index = v.cp.isin(it.iloc[F]) v.loc[cp_index, 'F'] = F ``` -------------------------------- ### Partition nodes in DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_nodes.md Demonstrates how to initialize a DeepGraph object and use partition_nodes to aggregate data by single or multiple features, including custom aggregation functions. ```python import pandas as pd import deepgraph as dg import numpy as np # Initialize node table v = pd.DataFrame({'x': [-3.4, 2.1, -1.1, 0.9, 2.3], 'time': [0, 0, 2, 2, 9], 'color': ['g', 'g', 'b', 'g', 'r'], 'size': [1, 3, 2, 3, 1]}) g = dg.DeepGraph(v) # Partition by single feature sv1 = g.partition_nodes('color') # Partition by multiple features sv2 = g.partition_nodes(['color', 'size']) # Partition with custom aggregation sv3 = g.partition_nodes('color', {'time': [lambda x: list(x), np.max], 'x': [np.mean, np.sum, np.std]}) ``` -------------------------------- ### Partition Edges by Target Features (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This code partitions edges based on the 'color' and 'size' features of the target nodes, aggregating 'same_color' and 'inds'. ```python g.partition_edges(target_features=['color', 'size'], relation_funcs={'same_color': 'sum', 'inds': lambda x: tuple(x)}) ``` -------------------------------- ### Visualize nodes and edges in 2D using DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.plot_2d.md This example demonstrates how to call plot_2d to visualize node coordinates and edge connections. It highlights the use of column names for positioning and optional parameters for customizing the quiver plot appearance. ```python import deepgraph as dg # Assuming 'dg_obj' is an initialized DeepGraph instance # Plot nodes based on 'x_coord' and 'y_coord' columns # and include edges with custom scatter styling plot_results = dg_obj.plot_2d( x='x_coord', y='y_coord', edges=True, kwds_scatter={'s': 50, 'c': 'blue'}, kwds_quiver={'color': 'gray', 'alpha': 0.5} ) ``` -------------------------------- ### Create Video from Image Frames using FFmpeg Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.md This command uses FFmpeg to compile a sequence of PNG images into a video file. It specifies the input frame rate, codec, output frame rate, and video resolution. The output is an MP4 video file. Dependencies include FFmpeg. ```bash # cmd = "ffmpeg -y -r 5 -i tmp/pcp_%03d.png -c:v libx264 -r 20 -vf scale=2052:1004 {}.mp4" # os.system(cmd.format('precipitation_files/pcp')) ``` -------------------------------- ### Advanced Indexing on Partitioned Graph Edges Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.ipynb This example demonstrates advanced indexing on the edges of a partitioned DeepGraph using pandas' hierarchical indexing capabilities. It retrieves specific edges based on a combination of node and layer indices. ```python print(nlg.e.loc[2, 'Communication', :, 'Communication']) ``` -------------------------------- ### Benchmark FastTrack Edge Creation (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/intro_to_deepgraph.ipynb Measures the execution time of the `create_edges_ft` method using `%%timeit`. This demonstrates the performance advantage of the FastTrack approach. ```python %timeit -n3 -r3 g.create_edges_ft(ft_feature=('x', 1000)) ``` -------------------------------- ### Initialize DeepGraph Environment Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.md Imports essential data science and visualization libraries, including deepgraph, pandas, and matplotlib, and configures notebook display settings for optimal data exploration. ```python import os import subprocess import zipfile import matplotlib.pyplot as plt import numpy as np import pandas as pd import deepgraph as dg %matplotlib inline pd.options.display.max_rows = 10 pd.set_option('expand_frame_repr', False) ``` -------------------------------- ### Get SFDP Layout Positions and Plot Graph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.ipynb This snippet demonstrates how to obtain SFDP layout positions for a graph and then plot the graph using these positions. It configures node and edge appearance and sets plot limits. Dependencies include matplotlib and deepgraph. ```python # get sfdp layout postitions pos = gtd.sfdp_layout(gtg) pos = pos.get_2d_array([0, 1]) ng.v['x'] = pos[0] ng.v['y'] = pos[1] # configure nodes kwds_scatter = {'s': 100, 'c': 'k'} # configure edges # split edges with only one type of connection C_split_0 = ng.e['n_edges'].values.copy() C_split_0[C_split_0 == 1] = 0 # edges with one type of connection kwds_quiver_0 = {'alpha': .3, 'width': .001} # edges with more than one type kwds_quiver = {'headwidth': 1, 'width': .003, 'alpha': .7, 'cmap': 'Blues', 'clim': (1, ng.e.n_edges.max())} # create plot fig, ax = plt.subplots(figsize=(15,15)) ng.plot_2d('x', 'y', edges=True, C_split_0=C_split_0, kwds_scatter=kwds_scatter, kwds_quiver_0=kwds_quiver_0, kwds_quiver=kwds_quiver, ax=ax) # turn axis off, set x/y-lim and name nodes ax.axis('off') ax.set_xlim(ng.v.x.min() - 1, ng.v.x.max() + 1) ax.set_ylim(ng.v.y.min() - 1, ng.v.y.max() + 1) for i in ng.v.index: plt.text(ng.v.at[i, 'x'], ng.v.at[i, 'y'] + .3, i, fontsize=12) ``` -------------------------------- ### Partition DeepGraph by 'color' with Aggregated Features (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_graph.md Demonstrates partitioning a DeepGraph by 'color' while aggregating other features like 'time' and calculating sums for relation attributes like 'larger_than' and 'same_color'. ```python sv, se = g.partition_graph('color', feature_funcs={'time': lambda x: list(x)}, relation_funcs={'larger_than': 'sum', 'same_color': 'sum'}) print(sv) print(se) ``` -------------------------------- ### Fast-Track Selector Function Example (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md This Python code defines an internal selector function used by create_edges_ft. It calculates the fast-track relation (ft_r) between source and target fast-track features and filters based on a threshold (ftt). It returns filtered sources, targets, and the computed fast-track relation. ```python def ft_selector(ftf_s, ftf_t, ftt, sources, targets): ft_r = ftf_t - ftf_s sources = sources[ft_r <= ftt] targets = targets[ft_r <= ftt] return sources, targets, ft_r ``` -------------------------------- ### Define Custom Connectors and Selectors for Edge Creation Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/intro_to_deepgraph.md Shows how to define custom distance functions and selector logic to filter edges based on spatial and temporal constraints during FastTrack edge creation. ```python def y_dist(y_s, y_t): dy = y_t - y_s return dy def time_dist(time_t, time_s): dt = time_t - time_s return dt def y_dist_selector(dy, sources, targets): dya = np.abs(dy) sources = sources[dya <= 100] targets = targets[dya <= 100] return sources, targets def time_dist_selector(dt, sources, targets): dta = np.abs(dt) sources = sources[dta <= 1] targets = targets[dta <= 1] return sources, targets g.create_edges_ft(ft_feature=('x', 100), connectors=[y_dist, time_dist], selectors=[y_dist_selector, time_dist_selector]) ``` -------------------------------- ### Partition Edges by Multiple Relations (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md This code partitions edges using two relations ('larger_than', 'same_color') and applies different aggregation functions to 'dx' and 'dt'. ```python g.partition_edges(relations=['larger_than', 'same_color'], relation_funcs={'dx': ['mean', 'std'], 'dt': lambda x: tuple(x)}) ``` -------------------------------- ### Transfer Features to Edges in DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges.md Demonstrates how to transfer features from a node table 'v' to an edge table 'e' during edge creation. This is achieved using the 'transfer_features' argument in the 'create_edges' method. The example shows transferring 'x' and 'time' features and the resulting edge table 'e'. ```python >>> g.create_edges(connectors=time_difference, ... transfer_features=['x', 'time']) >>> g.e dt time_s time_t x_s x_t s t 0 1 2 0 2 3 1 2 9 0 9 3 12 1 2 7 2 9 1 12 ``` -------------------------------- ### Create PyTables Index for Fast-Track Feature Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md Demonstrates creating a full pytables index on the 'time' column of a node table within a sorted HDFStore. This is recommended to speed up internal queries on the fast-track feature. ```python >>> s_vstore.create_table_index('node_table', columns=['time'], ... kind='full') ``` -------------------------------- ### Benchmark Standard Edge Creation (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/intro_to_deepgraph.ipynb Measures the execution time of the standard `create_edges` method using `%%timeit`. This provides a baseline for performance comparison. ```python %timeit -n3 -r3 g.create_edges(connectors=x_dist, selectors=x_dist_selector) ``` -------------------------------- ### Initialize DeepGraph and Dependencies Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/pairwise_correlations.ipynb Imports necessary libraries for data manipulation, parallel processing, and the DeepGraph framework. ```python import os from multiprocessing import Pool import numpy as np import pandas as pd import deepgraph as dg ``` -------------------------------- ### Partition Edges by Relation and Features Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_edges.md Demonstrates the use of the partition_edges method to group edges based on a specific relation type ('larger_than') and source/target features. It utilizes a custom relation function to format the indices of the resulting partitions. ```pycon >>> g.partition_edges(relations='larger_than', ... source_features='color', target_features='size', ... relation_funcs={'inds': lambda x: tuple(x)}) ``` -------------------------------- ### Download Precipitation Data Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.ipynb Uses system-level wget commands to download precipitation datasets based on a provided input file containing URLs. Requires authentication cookies for NASA Earthdata access. ```python os.system("wget --content-disposition --directory-prefix=tmp --load-cookies ~/.urs_cookies --save-cookies ~/.urs_cookies --auth-no-challenge=on --keep-session-cookies -i SSW_download_2016-05-03T20_19_28_23621_2oIe06xp.inp") ``` -------------------------------- ### Initialize DeepGraph Class Source: https://context7.com/deepgraph/deepgraph/llms.txt Demonstrates how to initialize a DeepGraph object using a pandas DataFrame of node features. It also shows how to access basic graph properties and group nodes using supernode labels. ```python import pandas as pd import deepgraph as dg # Create a node table with features v = pd.DataFrame({ 'time': [0., 2., 9., 15., 20.], 'x': [3., 1., 12., 8., 5.], 'y': [10., 15., 8., 12., 20.], 'color': ['red', 'blue', 'red', 'green', 'blue'] }) # Initialize DeepGraph with node table g = dg.DeepGraph(v) # Access graph properties print(f"Number of nodes: {g.n}") print(f"Number of edges: {g.m}") print(f"Feature types:\n{g.f}") # Initialize with supernode labels (groups nodes by feature values) g = dg.DeepGraph(v, supernode_labels_by={'color_group': 'color'}) print(g.v) ``` -------------------------------- ### DeepGraph Initialization Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.md Initializes the DeepGraph object with nodes, edges, and optional supernode labels. ```APIDOC ## DeepGraph.__init__ ### Description Initializes the DeepGraph object. ### Method `__init__` ### Endpoint N/A (Constructor) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **v** (DataFrame) - Node table. * **e** (DataFrame) - Edge table. * **supernode_labels_by** (list, optional) - Columns to use for creating supernodes. ### Request Example ```json { "v": "", "e": "", "supernode_labels_by": ["column1", "column2"] } ``` ### Response #### Success Response (200) * **DeepGraph object** - The initialized DeepGraph instance. #### Response Example ```json { "message": "DeepGraph initialized successfully" } ``` ``` -------------------------------- ### Prepare Network Visualization Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.md Configures graph layout coordinates using the SFDP algorithm and prepares styling parameters for nodes and edges for plotting. ```python import graph_tool.draw as gtd gtg = g.return_gt_graph() gtg.set_directed(False) pos = gtd.sfdp_layout(gtg, gamma=.5) pos = pos.get_2d_array([0, 1]) g.v['x'] = pos[0] g.v['y'] = pos[1] kwds_scatter = {'s': 1, 'c': 'k'} kwds_quiver = {'headwidth': 1, 'alpha': .3, 'cmap': 'prism'} C = g.e.groupby('type').grouper.group_info[0] ``` -------------------------------- ### Partition nodes by family and component Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.md Demonstrates how to group graph nodes by specific attributes (e.g., 'F' and 'g_id' or 'cp' and 'g_id') and apply aggregation functions to summarize node features. The resulting dataframes are renamed for clarity and prepared for spatial analysis. ```python fgv = g.partition_nodes(['F', 'g_id'], feature_funcs=feature_funcs) fgv.rename(columns={'lat_amin': 'lat', 'lon_amin': 'lon', 'cp_n_cp_nodes': 'n_cp_nodes'}, inplace=True) feature_funcs = {'vol': [np.sum], 'lat': np.min, 'lon': np.min} gcpv = g.partition_nodes(['cp', 'g_id'], feature_funcs) gcpv.rename(columns={'lat_amin': 'lat', 'lon_amin': 'lon'}, inplace=True) ``` -------------------------------- ### Partition Graph and Create Supergraph in DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/terrorists.md Partitions a graph into a supergraph based on node features, aggregates edge weights by type, and initializes a new DeepGraph object. ```python # partition by MLN's node indices nv, ne, gv, ge = g.partition_graph('V_N', return_gve=True) # for each superedge, get types of edges and their weights def type_weights(group): index = group['type'].values data = group['weight'].values return pd.Series(data=data, index=index) ne_weights = ge.apply(type_weights).unstack() ne = pd.concat((ne, ne_weights), axis=1) # create graph ng = dg.DeepGraph(nv, ne) ng ``` -------------------------------- ### Partitioning Nodes by Features Source: https://context7.com/deepgraph/deepgraph/llms.txt Demonstrates the `partition_nodes` method for grouping nodes into supernodes based on feature values. It covers simple partitioning by a single feature, intersection partitioning by multiple features, and partitioning with custom aggregation functions. ```python import pandas as pd import numpy as np import deepgraph as dg v = pd.DataFrame({ 'x': [-3.4, 2.1, -1.1, 0.9, 2.3], 'time': [0, 0, 2, 2, 9], 'color': ['green', 'green', 'blue', 'green', 'red'], 'size': [1, 3, 2, 3, 1] }) g = dg.DeepGraph(v) # Simple partition by color sv = g.partition_nodes('color') print(sv) # Intersection partition by multiple features sv = g.partition_nodes(['color', 'size']) print(sv) # Partition with aggregation functions sv = g.partition_nodes( 'color', feature_funcs={ 'x': [np.mean, np.std], 'time': lambda x: list(x) } ) print(sv) # Get the groupby object for additional operations sv, gv = g.partition_nodes('color', return_gv=True) print(gv.apply(lambda x: x['x'].max() - x['x'].min())) ``` -------------------------------- ### Sort Node Table on Disk using ptrepack Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md Shows the command-line usage of `ptrepack` to sort an HDFStore file based on a specific column ('time'). This is a crucial step when the node table is too large to be sorted in memory. ```bash >>> !ptrepack --chunkshape=auto --sortby=time vstore.h5 s_vstore.h5 ``` -------------------------------- ### Preprocess Data and Initialize DeepGraph Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.ipynb Renames columns in a DataFrame for consistency and initializes a DeepGraph object to manage the dataset. ```python v.rename(columns={'pcp': 'r', 'latitude': 'lat', 'longitude': 'lon', 'time': 'dtime', 'itime': 'time'}, inplace=True) print(v) g = dg.DeepGraph(v) ``` -------------------------------- ### Create Full Index on Node Table Column with PyTables Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges_ft.md Illustrates how to create a full pytables index on a specified column ('time') of a node table stored in an HDFStore. This is useful for sorting large tables on disk or speeding up internal queries. ```python >>> vstore.create_table_index('node_table', columns=['time'], ... kind='full') ``` -------------------------------- ### Generate Video from Visualization Frames Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.ipynb Compiles a series of saved image frames into an MP4 video file using ffmpeg and provides an HTML snippet for embedding the resulting video in a notebook environment. ```python cmd = "ffmpeg -y -r 5 -i tmp/cp1_ortho_%03d.png -c:v libx264 -r 20 -vf scale=1919:1406 {}.mp4" os.system(cmd.format('precipitation_files/cp1_ortho')) HTML(""" """) ``` -------------------------------- ### Register connector outputs with output_names decorator Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.create_edges.md Shows how to use the @dg.output_names decorator to explicitly define output variable names for a connector function, preventing OSError when source code inspection is unavailable. ```python import deepgraph as dg @dg.output_names("dx", "dt", "v") def velocity(x_s, x_t, time_s, time_t): dx = x_t - x_s dt = time_t - time_s v = dx/dt return dx, dt, v ``` -------------------------------- ### Partition DeepGraph by 'color' (Python) Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/generated/deepgraph.DeepGraph.partition_graph.md Demonstrates partitioning a DeepGraph based on the 'color' feature of its nodes. It returns super-vertices (sv) and super-edges (se) representing the partitioned graph. ```python sv, se = g.partition_graph('color') ``` -------------------------------- ### Create Video from Image Sequences Source: https://github.com/deepgraph/deepgraph/blob/master/doc/source/tutorials/precipitation.md Uses FFmpeg via system command to compile a sequence of PNG images into an MP4 video file. ```python cmd = "ffmpeg -y -r 5 -i tmp/cp1_ortho_%03d.png -c:v libx264 -r 20 -vf scale=1919:1406 {}.mp4" os.system(cmd.format('precipitation_files/cp1_ortho')) ```