from typing import List, Dict, Optional from app.api.explorer import AlgoProfile, DatasetMeta, is_algorithm_compatible, ALGORITHMS import hashlib def simple_hash(value: str) -> int: return int(hashlib.md5(value.encode()).hexdigest(), 16) def generate_edge_scores(edge, dataset_meta: DatasetMeta) -> Dict[str, Optional[float]]: scores = {} for algo in ALGORITHMS: if not is_algorithm_compatible(algo, dataset_meta): scores[algo.name] = None continue # undirected constraint if algo.undirected and edge["source"] > edge["target"]: scores[algo.name] = None continue # TF constraint if algo.requiresTFList and not edge.get("isTFEdge", False): scores[algo.name] = None continue # penalties gene_penalty = 0.85 if dataset_meta.nGenes > 15000 else 1 cell_penalty = 0.9 if dataset_meta.nCells < 200 else 1 h = simple_hash(f"{edge['source']}-{edge['target']}-{algo.name}") base_score = 0.45 + ((h % 400) / 1000) adjusted = base_score * gene_penalty * cell_penalty scores[algo.name] = round(adjusted, 3) # fallback if not any(v is not None for v in scores.values()): fallback = next( (a for a in ALGORITHMS if is_algorithm_compatible(a, dataset_meta)), None ) if fallback: h = simple_hash(f"{edge['source']}-{edge['target']}-{fallback.name}") scores[fallback.name] = round(0.5 + (h % 300) / 1000, 3) return scores def compute_best_algorithm(scores: Dict[str, Optional[float]]): best_algo = "" best_mean = float("-inf") for algo, score in scores.items(): if score is not None and score > best_mean: best_mean = score best_algo = algo return best_algo, (None if best_mean == float("-inf") else best_mean) def enrich_edges(edges: List[dict], dataset_meta: DatasetMeta): enriched = [] for edge in edges: scores = generate_edge_scores(edge, dataset_meta) best_algo, best_mean = compute_best_algorithm(scores) enriched.append({ **edge, "scores": scores, "bestAlgo": best_algo, "bestMean": best_mean }) return enriched def build_nodes(edges: List[dict]): node_map = {} for e in edges: node_map[e["source"]] = node_map.get(e["source"], 0) + 1 node_map[e["target"]] = node_map.get(e["target"], 0) + 1 nodes = [] for node_id, degree in node_map.items(): neighbors = [ e["target"] if e["source"] == node_id else e["source"] for e in edges if e["source"] == node_id or e["target"] == node_id ] nodes.append({ "id": node_id, "label": node_id, "degree": len(neighbors), "importance": degree, "neighbors": neighbors }) return nodes def compute_node_best_algo(nodes, edges): for node in nodes: related = [e for e in edges if e["source"] == node["id"] or e["target"] == node["id"]] algo_scores = {} for edge in related: if edge["bestAlgo"] and edge["bestMean"] is not None: algo = edge["bestAlgo"] algo_scores.setdefault(algo, []).append(edge["bestMean"]) best_algo = "" best_mean = 0 for algo, scores in algo_scores.items(): mean = sum(scores) / len(scores) if mean > best_mean: best_mean = mean best_algo = algo node["bestAlgo"] = best_algo node["bestMean"] = round(best_mean, 4) return nodes def compute_score_range(edges, nodes): edge_scores = [e["bestMean"] for e in edges if e["bestMean"] is not None] min_edge = min(edge_scores) if edge_scores else 0 max_edge = max(edge_scores) if edge_scores else 0 node_scores = [n["bestMean"] for n in nodes if n.get("bestMean")] min_node = min(node_scores) if node_scores else 0 max_node = max(node_scores) if node_scores else 0 return { "edges": [min_edge, max_edge], "nodes": [min_node, max_node] } def generate_edge_scores_v2(edge, dataset_meta): scores = {} for algo in ALGORITHMS: compatible = is_algorithm_compatible(algo, dataset_meta) # synthetic override if getattr(dataset_meta, "isSynthetic", False): compatible = True if not compatible: scores[algo.name] = None continue if algo.requiresTFList and not edge.get("isTFEdge", False): if not getattr(dataset_meta, "isSynthetic", False): scores[algo.name] = None continue if algo.undirected and edge["source"] > edge["target"]: if not getattr(dataset_meta, "isSynthetic", False): scores[algo.name] = None continue h = simple_hash(f"{edge['source']}-{edge['target']}-{algo.name}") probability = (h % 1000) / 1000 sparsity = algo.edgeSparsity if getattr(dataset_meta, "isSynthetic", False): sparsity = min(sparsity + 0.1, 0.99) if probability > sparsity: scores[algo.name] = None continue gene_penalty = 0.85 if dataset_meta.nGenes > 15000 else 1 cell_penalty = 0.9 if dataset_meta.nCells < 200 else 1 base = 0.45 + ((h % 400) / 1000) scores[algo.name] = round(base * gene_penalty * cell_penalty, 3) return scores