|
5 | 5 | from collections import Counter |
6 | 6 | from datetime import datetime |
7 | 7 |
|
| 8 | + |
8 | 9 | def load_json_remote(url): |
9 | 10 | """Load JSON data from a remote URL.""" |
10 | 11 | response = requests.get(url) |
11 | 12 | response.raise_for_status() |
12 | 13 | return response.json() |
13 | 14 |
|
14 | | -def find_participant_lists(obj): |
| 15 | + |
| 16 | +def find_field_combinations(obj): |
15 | 17 | """ |
16 | | - Recursively find all lists of strings in a JSON-like object. |
17 | | - Returns a list of lists (each list being a potential participant group). |
| 18 | + Recursively extract all field (key) names and record their co-occurrence within objects. |
| 19 | + Returns a list of sets — each set contains field names that appear together. |
18 | 20 | """ |
19 | | - results = [] |
| 21 | + cooccurrences = [] |
20 | 22 |
|
21 | 23 | if isinstance(obj, dict): |
| 24 | + keys = set(obj.keys()) |
| 25 | + if len(keys) > 1: |
| 26 | + cooccurrences.append(keys) |
| 27 | + |
22 | 28 | for value in obj.values(): |
23 | | - results.extend(find_participant_lists(value)) |
| 29 | + cooccurrences.extend(find_field_combinations(value)) |
| 30 | + |
24 | 31 | elif isinstance(obj, list): |
25 | | - # Check if this list looks like a participant list (all strings) |
26 | | - if all(isinstance(x, str) for x in obj) and len(obj) > 1: |
27 | | - results.append(obj) |
28 | | - else: |
29 | | - for item in obj: |
30 | | - results.extend(find_participant_lists(item)) |
| 32 | + for item in obj: |
| 33 | + cooccurrences.extend(find_field_combinations(item)) |
31 | 34 |
|
32 | | - return results |
| 35 | + return cooccurrences |
33 | 36 |
|
34 | | -def build_coattendance_graph(meetings): |
35 | | - """Build an undirected co-attendance graph from all detected participant lists.""" |
36 | | - G = nx.Graph() |
37 | 37 |
|
38 | | - for meeting in meetings: |
39 | | - participant_lists = find_participant_lists(meeting) |
40 | | - # Merge all string lists found in this record |
41 | | - participants = set() |
42 | | - for lst in participant_lists: |
43 | | - participants.update(lst) |
| 38 | +def build_field_graph(meetings): |
| 39 | + """Build a co-occurrence graph where nodes are JSON field names.""" |
| 40 | + G = nx.Graph() |
44 | 41 |
|
45 | | - if len(participants) < 2: |
46 | | - continue |
| 42 | + cooccurrence_sets = find_field_combinations(meetings) |
47 | 43 |
|
48 | | - for p in participants: |
49 | | - G.add_node(p) |
50 | | - for u, v in combinations(participants, 2): |
| 44 | + for field_set in cooccurrence_sets: |
| 45 | + for field in field_set: |
| 46 | + G.add_node(field) |
| 47 | + for u, v in combinations(field_set, 2): |
51 | 48 | if G.has_edge(u, v): |
52 | | - G[u][v]['weight'] += 1 |
| 49 | + G[u][v]["weight"] += 1 |
53 | 50 | else: |
54 | 51 | G.add_edge(u, v, weight=1) |
55 | 52 |
|
56 | 53 | return G |
57 | 54 |
|
| 55 | + |
58 | 56 | def degree_analysis(G): |
59 | 57 | """Compute degree metrics for the graph.""" |
60 | 58 | degree_dict = dict(G.degree()) |
61 | 59 | degree_counts = Counter(degree_dict.values()) |
62 | 60 | return degree_dict, degree_counts |
63 | 61 |
|
| 62 | + |
64 | 63 | def write_markdown_report(degree_dict, degree_counts, output_file): |
65 | 64 | """Write results to a Markdown file.""" |
66 | 65 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
67 | 66 | with open(output_file, "w", encoding="utf-8") as f: |
68 | | - f.write(f"# Degree Analysis Report\n") |
| 67 | + f.write(f"# JSON Field Degree Analysis Report\n") |
69 | 68 | f.write(f"**Generated on:** {timestamp}\n\n") |
70 | 69 |
|
71 | 70 | # Summary |
72 | 71 | f.write("## Summary Statistics\n") |
73 | | - f.write(f"- Total Nodes: {len(degree_dict)}\n") |
| 72 | + f.write(f"- Total Unique Fields: {len(degree_dict)}\n") |
74 | 73 | f.write(f"- Maximum Degree: {max(degree_dict.values()) if degree_dict else 0}\n") |
75 | 74 | f.write(f"- Minimum Degree: {min(degree_dict.values()) if degree_dict else 0}\n\n") |
76 | 75 |
|
77 | | - # Top 10 nodes |
78 | | - f.write("## Top 10 Nodes by Degree\n") |
79 | | - f.write("| Rank | Node | Degree |\n|------|-------|---------|\n") |
80 | | - for i, (node, deg) in enumerate(sorted(degree_dict.items(), key=lambda x: x[1], reverse=True)[:10], 1): |
81 | | - f.write(f"| {i} | {node} | {deg} |\n") |
| 76 | + # Top 15 fields by degree |
| 77 | + f.write("## Top 15 JSON Fields by Degree\n") |
| 78 | + f.write("| Rank | Field Name | Degree |\n|------|-------------|---------|\n") |
| 79 | + for i, (field, deg) in enumerate(sorted(degree_dict.items(), key=lambda x: x[1], reverse=True)[:15], 1): |
| 80 | + f.write(f"| {i} | {field} | {deg} |\n") |
82 | 81 | f.write("\n") |
83 | 82 |
|
84 | 83 | # Degree distribution |
85 | 84 | f.write("## Degree Distribution\n") |
86 | | - f.write("| Degree | Count of Nodes |\n|---------|----------------|\n") |
| 85 | + f.write("| Degree | Count of Fields |\n|---------|-----------------|\n") |
87 | 86 | for degree, count in sorted(degree_counts.items()): |
88 | 87 | f.write(f"| {degree} | {count} |\n") |
89 | 88 |
|
90 | 89 | print(f"✅ Markdown report saved to: {output_file}") |
91 | 90 |
|
92 | | -def main(): |
93 | | - url = "https://raw.githubusercontent.com/SingularityNET-Archive/SingularityNET-Archive/refs/heads/main/Data/Snet-Ambassador-Program/Meeting-Summaries/2025/meeting-summaries-array.json" |
94 | | - output_file = "degree_analysis_report.md" |
95 | 91 |
|
96 | | - print("📡 Fetching data from remote source...") |
| 92 | +def main(): |
| 93 | + url = ( |
| 94 | + "https://raw.githubusercontent.com/SingularityNET-Archive/" |
| 95 | + "SingularityNET-Archive/refs/heads/main/Data/Snet-Ambassador-Program/" |
| 96 | + "Meeting-Summaries/2025/meeting-summaries-array.json" |
| 97 | + ) |
| 98 | + output_file = "degree_analysis_by_field.md" |
| 99 | + |
| 100 | + print("📡 Fetching JSON from remote source...") |
97 | 101 | data = load_json_remote(url) |
98 | | - print(f"✅ Downloaded {len(data)} meeting records.") |
| 102 | + print("✅ JSON file downloaded.") |
99 | 103 |
|
100 | | - print("🔍 Detecting participant lists recursively...") |
101 | | - G = build_coattendance_graph(data) |
| 104 | + print("🔍 Building field co-occurrence graph...") |
| 105 | + G = build_field_graph(data) |
102 | 106 |
|
103 | 107 | if len(G.nodes) == 0: |
104 | | - print("⚠️ No participant lists found — please check JSON structure manually.") |
| 108 | + print("⚠️ No JSON field structure detected.") |
105 | 109 | return |
106 | 110 |
|
107 | | - print(f"📊 Built graph with {len(G.nodes)} nodes and {len(G.edges)} edges.") |
| 111 | + print(f"📊 Built graph with {len(G.nodes)} fields and {len(G.edges)} relationships.") |
108 | 112 |
|
109 | 113 | degree_dict, degree_counts = degree_analysis(G) |
110 | 114 | write_markdown_report(degree_dict, degree_counts, output_file) |
111 | 115 |
|
| 116 | + |
112 | 117 | if __name__ == "__main__": |
113 | 118 | main() |
114 | 119 |
|
| 120 | + |
0 commit comments