|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +###################################### |
| 4 | +# Imports |
| 5 | +###################################### |
| 6 | + |
| 7 | +import hydra |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import networkx as nx |
| 10 | +import numpy as np |
| 11 | +from omegaconf import DictConfig |
| 12 | +from os.path import join as join_path |
| 13 | +import pandas as pd |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +###################################### |
| 17 | +# Functions |
| 18 | +###################################### |
| 19 | + |
| 20 | + |
| 21 | +def process_network( |
| 22 | + feature_matrix: pd.DataFrame, edge_list: pd.DataFrame, from_col: str, to_col: str, |
| 23 | + len_component: int = 5 |
| 24 | +) -> tuple[pd.DataFrame, pd.DataFrame]: |
| 25 | + """ |
| 26 | + Construct a graph from edge list data. |
| 27 | +
|
| 28 | + Args: |
| 29 | + feature_matrix (pd.DataFrame): |
| 30 | + The feature matrix. |
| 31 | + edge_list (pd.DataFrame): |
| 32 | + The edge list. |
| 33 | + from_col (str): |
| 34 | + The "from" column name. |
| 35 | + to_col (str): |
| 36 | + The "to" column name. |
| 37 | + len_component (int, optional): |
| 38 | + The minimum size of a subgraph to filter out. Defaults to 5. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + tuple[pd.DataFrame, pd.DataFrame]: |
| 42 | + The processed graph as a feature matrix and edge list. |
| 43 | + """ |
| 44 | + edges = edge_list.sort_values(from_col) |
| 45 | + |
| 46 | + G = nx.from_pandas_edgelist(edges, from_col, to_col, create_using=nx.Graph()) |
| 47 | + |
| 48 | + for component in list(nx.connected_components(G)): |
| 49 | + if len(component) <= len_component: |
| 50 | + for node in component: |
| 51 | + G.remove_node(node) |
| 52 | + |
| 53 | + nodes = list(G.nodes) |
| 54 | + filtered_feature_matrix = feature_matrix[nodes] |
| 55 | + filtered_edge_list = nx.to_pandas_edgelist(G, source=from_col, target=to_col) |
| 56 | + return filtered_feature_matrix, filtered_edge_list |
| 57 | + |
| 58 | + |
| 59 | +def log_results( |
| 60 | + tracking_uri: str, |
| 61 | + experiment_prefix: str, |
| 62 | + grn_name: str, |
| 63 | + feature_matrix: pd.DataFrame, |
| 64 | + edge_list: pd.DataFrame |
| 65 | +) -> None: |
| 66 | + """ |
| 67 | + Log experiment results to the experiment tracker. |
| 68 | +
|
| 69 | + Args: |
| 70 | + tracking_uri (str): |
| 71 | + The tracking URI. |
| 72 | + experiment_prefix (str): |
| 73 | + The experiment name prefix. |
| 74 | + grn_name (str): |
| 75 | + The name of the GRN. |
| 76 | + feature_matrix (pd.DataFrame): |
| 77 | + The feature matrix. |
| 78 | + edge_list (pd.DataFrame): |
| 79 | + The edge list. |
| 80 | + """ |
| 81 | + import mlflow |
| 82 | + |
| 83 | + mlflow.set_tracking_uri(tracking_uri) |
| 84 | + |
| 85 | + experiment_name = f"{experiment_prefix}_process" |
| 86 | + existing_exp = mlflow.get_experiment_by_name(experiment_name) |
| 87 | + if not existing_exp: |
| 88 | + mlflow.create_experiment(experiment_name) |
| 89 | + mlflow.set_experiment(experiment_name) |
| 90 | + |
| 91 | + mlflow.set_tag("grn", grn_name) |
| 92 | + |
| 93 | + mlflow.log_param("grn", grn_name) |
| 94 | + |
| 95 | + mlflow.log_metric("num_features", len(feature_matrix.index)) |
| 96 | + mlflow.log_metric("num_nodes", len(feature_matrix.columns)) |
| 97 | + mlflow.log_metric("num_1st_order_relationships", len(edge_list.index)) |
| 98 | + |
| 99 | + mlflow.end_run() |
| 100 | + |
| 101 | +###################################### |
| 102 | +# Main |
| 103 | +###################################### |
| 104 | + |
| 105 | +@hydra.main(version_base=None, config_path="../conf", config_name="config") |
| 106 | +def main(config: DictConfig) -> None: |
| 107 | + """ |
| 108 | + The main entry point for the plotting pipeline. |
| 109 | +
|
| 110 | + Args: |
| 111 | + config (DictConfig): |
| 112 | + The pipeline configuration. |
| 113 | + """ |
| 114 | + # Constants |
| 115 | + EXPERIMENT_PREFIX = config["experiment"]["name"] |
| 116 | + |
| 117 | + DATA_DIR = config["dir"]["data_dir"] |
| 118 | + PREPROCESS_DIR = config["dir"]["preprocessed_dir"] |
| 119 | + OUT_DIR = config["dir"]["out_dir"] |
| 120 | + |
| 121 | + GRN_NAME = config["grn"]["input_dir"] |
| 122 | + FEATURE_MATRIX_FILE = config["grn"]["feature_matrix"] |
| 123 | + EDGE_LIST_FILE = config["grn"]["edge_list"] |
| 124 | + FROM_COL = config["grn"]["from_col"] |
| 125 | + TO_COL = config["grn"]["to_col"] |
| 126 | + |
| 127 | + TRACKING_URI = config["experiment_tracking"]["tracking_uri"] |
| 128 | + ENABLE_TRACKING = config["experiment_tracking"]["enabled"] |
| 129 | + |
| 130 | + input_dir = join_path(DATA_DIR, PREPROCESS_DIR, GRN_NAME) |
| 131 | + feature_matrix = pd.read_csv(join_path(input_dir, FEATURE_MATRIX_FILE)) |
| 132 | + edge_list = pd.read_csv(join_path(input_dir, EDGE_LIST_FILE)) |
| 133 | + |
| 134 | + filtered_feature_matrix, filtered_edge_list = process_network(feature_matrix, edge_list, FROM_COL, TO_COL) |
| 135 | + |
| 136 | + output_dir = join_path(DATA_DIR, OUT_DIR, GRN_NAME, "process") |
| 137 | + Path(output_dir).mkdir(parents=True, exist_ok=True) |
| 138 | + |
| 139 | + filtered_feature_matrix.to_csv(join_path(output_dir, FEATURE_MATRIX_FILE)) |
| 140 | + filtered_edge_list.to_csv(join_path(output_dir, EDGE_LIST_FILE), index=False) |
| 141 | + |
| 142 | + if ENABLE_TRACKING: |
| 143 | + log_results( |
| 144 | + TRACKING_URI, |
| 145 | + EXPERIMENT_PREFIX, |
| 146 | + GRN_NAME, |
| 147 | + filtered_feature_matrix, |
| 148 | + filtered_edge_list, |
| 149 | + ) |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments