|
| 1 | +import neo4j |
| 2 | +import re |
| 3 | +from pathlib import Path |
| 4 | +import os |
| 5 | +from dotenv import load_dotenv |
| 6 | +load_dotenv() # Load environment variables from .env file |
| 7 | + |
| 8 | +from utils.neo4jdownloader import Neo4JDownloader |
| 9 | +from utils.builder_dataframe import neo4j_to_dataframe |
| 10 | +from utils.builder_models import df_to_pydantic_models |
| 11 | +from utils.visualization import visualize_graph |
| 12 | +from utils.visualization import visualize_clusters |
| 13 | + |
| 14 | +# --------------------------- |
| 15 | +# EXTRACT DATA FROM NEO4J |
| 16 | +# --------------------------- |
| 17 | + |
| 18 | +# Define your nodes |
| 19 | + |
| 20 | +nodes = ["user", "repo", "org"] |
| 21 | + |
| 22 | +# Define your relationships (edges) |
| 23 | + |
| 24 | +relationships = { |
| 25 | + "member_of": {"type1": {"source": "user", "target": "org"}}, |
| 26 | + "owner_of": { |
| 27 | + "type1": {"source": "user", "target": "repo"}, |
| 28 | + "type2": {"source": "org", "target": "repo"}, |
| 29 | + }, |
| 30 | + "contributor_of": { |
| 31 | + "type1": {"source": "user", "target": "repo"}, |
| 32 | + "type2": {"source": "org", "target": "repo"}, |
| 33 | + }, |
| 34 | + "parent_of": { |
| 35 | + "type1": {"source": "repo", "target": "repo"}, |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +def get_downloader(): |
| 40 | + |
| 41 | + NEO4J_URI = os.environ.get("NEO4J_URI") |
| 42 | + NEO4J_USERNAME = os.environ.get("NEO4J_USER") |
| 43 | + NEO4J_PASSWORD = os.environ.get("NEO4J_PASSWORD") |
| 44 | + NEO4J_DATABASE = os.environ.get("NEO4J_DATABASE") |
| 45 | + |
| 46 | + print(NEO4J_URI) |
| 47 | + |
| 48 | + return Neo4JDownloader(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD, NEO4J_DATABASE) |
| 49 | + |
| 50 | +def extract_data(nodes, relationships): |
| 51 | + downloader = get_downloader() |
| 52 | + |
| 53 | + try: |
| 54 | + nodes_ids, nodes_features = downloader.retrieve_nodes(nodes) |
| 55 | + edges_indices, edges_attributes = downloader.retrieve_edges(relationships) |
| 56 | + |
| 57 | + return nodes_ids, nodes_features, edges_indices, edges_attributes |
| 58 | + finally: |
| 59 | + downloader.close() |
| 60 | + |
| 61 | + |
| 62 | +nodes_ids, nodes_features, edges_indices, edges_attributes = extract_data(nodes, relationships) |
| 63 | +# example of looking at the output |
| 64 | +# print(nodes_ids["org"]) |
| 65 | +# print(nodes_features["org"]) |
| 66 | +# print(edges_indices) |
| 67 | + |
| 68 | +# ------------------------------------------- |
| 69 | +# MAKE NEO4J DATA INTO A PANDAS DATAFRAME |
| 70 | +# ------------------------------------------- |
| 71 | + |
| 72 | +df = neo4j_to_dataframe(nodes_ids, nodes_features, edges_indices, relationships) |
| 73 | +print("Dataframe constructed, shape is :", df.shape) |
| 74 | + |
| 75 | +# ------------------------------------------- |
| 76 | +# EXPLORE / FILTER PANDAS DATAFRAME |
| 77 | +# ------------------------------------------- |
| 78 | + |
| 79 | +# Define your pattern and filter the dataframe |
| 80 | + |
| 81 | +epfl_pattern = r"EPFL" |
| 82 | +epfl_df = df[ |
| 83 | + df['source'].astype(str).str.contains(epfl_pattern, flags=re.IGNORECASE, na=False) | |
| 84 | + df['target'].astype(str).str.contains(epfl_pattern, flags=re.IGNORECASE, na=False) |
| 85 | +] |
| 86 | +print(epfl_df.head()) |
| 87 | +print(epfl_df.shape) |
| 88 | + |
| 89 | +sdsc_pattern = r"(SwissDataScienceCenter|SDSC)" |
| 90 | +sdsc_df = df[ |
| 91 | + df["source"].astype(str).str.contains(sdsc_pattern, flags=re.IGNORECASE, na=False) | |
| 92 | + df["target"].astype(str).str.contains(sdsc_pattern, flags=re.IGNORECASE, na=False) |
| 93 | +] |
| 94 | +print(sdsc_df.head()) |
| 95 | +print(sdsc_df.shape) |
| 96 | + |
| 97 | +# ----------------------------------------------------------------------- |
| 98 | +# FEED YOUR DATAFRAME TO THE PYDANTIC MODELS AND VISUALIZE THE GRAPH |
| 99 | +# ----------------------------------------------------------------------- |
| 100 | + |
| 101 | +# From Dataframes to Graphs (via Pydantic) |
| 102 | +graph = df_to_pydantic_models(sdsc_df, relationships) |
| 103 | +sdsc_graph = df_to_pydantic_models(sdsc_df, relationships) |
| 104 | +epfl_graph = df_to_pydantic_models(epfl_df, relationships) |
| 105 | + |
| 106 | +# Full Graphs |
| 107 | + |
| 108 | +output_path = Path("plots/graphs/graph_200_visualization.png") |
| 109 | +visualize_graph(graph, output_path) |
| 110 | + |
| 111 | +output_path = Path("plots/graphs/sdsc_graph.png") |
| 112 | +visualize_graph(sdsc_graph, output_path) |
| 113 | + |
| 114 | +output_path = Path("plots/graphs/epfl_graph.png") |
| 115 | +visualize_graph(epfl_graph, output_path) |
| 116 | + |
| 117 | +# Clusters |
| 118 | + |
| 119 | +output_dir = Path("plots/clusters/") |
| 120 | + |
| 121 | +cluster_prefix_name = "200_first_nodes" |
| 122 | +visualize_clusters(graph, output_dir, cluster_prefix_name) |
| 123 | + |
| 124 | +cluster_prefix_name = "sdsc" |
| 125 | +visualize_clusters(sdsc_graph, output_dir, cluster_prefix_name) |
| 126 | + |
| 127 | +cluster_prefix_name = "epfl" |
| 128 | +visualize_clusters(epfl_graph, output_dir, cluster_prefix_name) |
| 129 | + |
| 130 | +# ----------------------------------------------------------------------- |
| 131 | +# DEMO FOLLOW UP |
| 132 | + |
| 133 | +# We can see for EPFL that just a string matching does not manage to find many of the EPFL affiliated repositories. |
| 134 | +# How can we complement with other tools and other approaches to find a better EPFL graph ? |
| 135 | +# Your turn to play around, good luck ! |
| 136 | + |
| 137 | +# ----------------------------------------------------------------------- |
| 138 | + |
| 139 | + |
0 commit comments