|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +###################################### |
| 4 | +# Imports |
| 5 | +###################################### |
| 6 | + |
| 7 | +from adbnx_adapter import ADBNX_Adapter |
| 8 | +from arango import ArangoClient |
| 9 | +import hydra |
| 10 | +import networkx as nx |
| 11 | +from omegaconf import DictConfig |
| 12 | +from os.path import join as join_path |
| 13 | +import pandas as pd |
| 14 | + |
| 15 | + |
| 16 | +###################################### |
| 17 | +# Functions |
| 18 | +###################################### |
| 19 | + |
| 20 | + |
| 21 | +def log_results( |
| 22 | + tracking_uri: str, |
| 23 | + experiment_prefix: str, |
| 24 | + grn_name: str, |
| 25 | + feature_matrix: pd.DataFrame, |
| 26 | + edge_list: pd.DataFrame, |
| 27 | +) -> None: |
| 28 | + """ |
| 29 | + Log experiment results to the experiment tracker. |
| 30 | +
|
| 31 | + Args: |
| 32 | + tracking_uri (str): |
| 33 | + The tracking URI. |
| 34 | + experiment_prefix (str): |
| 35 | + The experiment name prefix. |
| 36 | + grn_name (str): |
| 37 | + The name of the GRN. |
| 38 | + feature_matrix (pd.DataFrame): |
| 39 | + The feature matrix. |
| 40 | + edge_list (pd.DataFrame): |
| 41 | + The edge list. |
| 42 | + """ |
| 43 | + import mlflow |
| 44 | + |
| 45 | + mlflow.set_tracking_uri(tracking_uri) |
| 46 | + |
| 47 | + experiment_name = f"{experiment_prefix}_process" |
| 48 | + existing_exp = mlflow.get_experiment_by_name(experiment_name) |
| 49 | + if not existing_exp: |
| 50 | + mlflow.create_experiment(experiment_name) |
| 51 | + mlflow.set_experiment(experiment_name) |
| 52 | + |
| 53 | + mlflow.set_tag("grn", grn_name) |
| 54 | + |
| 55 | + mlflow.log_param("grn", grn_name) |
| 56 | + |
| 57 | + mlflow.log_metric("num_features", len(feature_matrix.index)) |
| 58 | + mlflow.log_metric("num_nodes", len(feature_matrix.columns)) |
| 59 | + mlflow.log_metric("num_edges", len(edge_list.index)) |
| 60 | + |
| 61 | + mlflow.end_run() |
| 62 | + |
| 63 | + |
| 64 | +###################################### |
| 65 | +# Main |
| 66 | +###################################### |
| 67 | + |
| 68 | + |
| 69 | +def get_graph( |
| 70 | + feature_matrix: pd.DataFrame, edge_list: pd.DataFrame, from_col: str, to_col: str |
| 71 | +) -> nx.Graph: |
| 72 | + """ |
| 73 | + Construct a graph from edge list data. |
| 74 | +
|
| 75 | + Args: |
| 76 | + feature_matrix (pd.DataFrame): |
| 77 | + The feature matrix. |
| 78 | + edge_list (pd.DataFrame): |
| 79 | + The edge list. |
| 80 | + from_col (str): |
| 81 | + The "from" column name. |
| 82 | + to_col (str): |
| 83 | + The "to" column name. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + nx.Graph: |
| 87 | + The graph to write to the database. |
| 88 | + """ |
| 89 | + edges = edge_list.sort_values(from_col) |
| 90 | + |
| 91 | + G = nx.from_pandas_edgelist(edges, from_col, to_col, create_using=nx.Graph()) |
| 92 | + node_features = feature_matrix.to_dict() |
| 93 | + nx.set_node_attributes(G, node_features, "expression") |
| 94 | + |
| 95 | + return G |
| 96 | + |
| 97 | + |
| 98 | +def to_db( |
| 99 | + db_host: str, |
| 100 | + db_name: str, |
| 101 | + db_username: str, |
| 102 | + db_password: str, |
| 103 | + collection: str, |
| 104 | + G: nx.Graph, |
| 105 | +) -> None: |
| 106 | + """ |
| 107 | + Write the graph to the database. |
| 108 | +
|
| 109 | + Args: |
| 110 | + db_host (str): |
| 111 | + The database host. |
| 112 | + db_name (str): |
| 113 | + The database name. |
| 114 | + db_username (str): |
| 115 | + The database username. |
| 116 | + db_password (str): |
| 117 | + The database password. |
| 118 | + collection (str): |
| 119 | + The database collection. |
| 120 | + G (nx.Graph): |
| 121 | + The graph. |
| 122 | + """ |
| 123 | + sys_db = ArangoClient(hosts=db_host).db( |
| 124 | + "_system", username=db_username, password=db_password |
| 125 | + ) |
| 126 | + if not sys_db.has_database(db_name): |
| 127 | + sys_db.create_database(db_name) |
| 128 | + db = ArangoClient(hosts=db_host).db( |
| 129 | + db_name, username=db_username, password=db_password |
| 130 | + ) |
| 131 | + |
| 132 | + edges_collection = f"{collection}_edges" |
| 133 | + for db_collection in [collection, edges_collection]: |
| 134 | + if db.has_collection(db_collection): |
| 135 | + db.delete_collection(db_collection) |
| 136 | + |
| 137 | + if db.has_graph(collection): |
| 138 | + db.delete_graph(collection) |
| 139 | + |
| 140 | + graph_definitions = [ |
| 141 | + { |
| 142 | + "edge_collection": edges_collection, |
| 143 | + "from_vertex_collections": [collection], |
| 144 | + "to_vertex_collections": [collection], |
| 145 | + } |
| 146 | + ] |
| 147 | + |
| 148 | + adapter = ADBNX_Adapter(db) |
| 149 | + adapter.networkx_to_arangodb(collection, G, graph_definitions) |
| 150 | + |
| 151 | + |
| 152 | +@hydra.main(version_base=None, config_path="../conf", config_name="config") |
| 153 | +def main(config: DictConfig) -> None: |
| 154 | + """ |
| 155 | + The main entry point for the plotting pipeline. |
| 156 | +
|
| 157 | + Args: |
| 158 | + config (DictConfig): |
| 159 | + The pipeline configuration. |
| 160 | + """ |
| 161 | + # Constants |
| 162 | + DATA_DIR = config["dir"]["data_dir"] |
| 163 | + PROCESS_DIR = config["dir"]["processed_dir"] |
| 164 | + OUT_DIR = config["dir"]["out_dir"] |
| 165 | + |
| 166 | + GRN_NAME = config["grn"]["input_dir"] |
| 167 | + FEATURE_MATRIX_FILE = config["grn"]["feature_matrix"] |
| 168 | + EDGE_LIST_FILE = config["grn"]["edge_list"] |
| 169 | + FROM_COL = config["grn"]["from_col"] |
| 170 | + TO_COL = config["grn"]["to_col"] |
| 171 | + |
| 172 | + DB_HOST = config["db"]["host"] |
| 173 | + DB_NAME = config["db"]["name"] |
| 174 | + DB_USERNAME = config["db"]["username"] |
| 175 | + DB_PASSWORD = config["db"]["password"] |
| 176 | + |
| 177 | + input_dir = join_path(DATA_DIR, OUT_DIR, GRN_NAME, PROCESS_DIR) |
| 178 | + feature_matrix = pd.read_csv(join_path(input_dir, FEATURE_MATRIX_FILE)) |
| 179 | + edge_list = pd.read_csv(join_path(input_dir, EDGE_LIST_FILE)) |
| 180 | + |
| 181 | + G = get_graph(feature_matrix, edge_list, FROM_COL, TO_COL) |
| 182 | + to_db(DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD, GRN_NAME, G) |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + main() |
0 commit comments