From 8035ccd7e395928f33381a860686f3f3344319b6 Mon Sep 17 00:00:00 2001 From: GiulioRossetti Date: Sun, 19 May 2024 14:52:05 +0200 Subject: [PATCH] :art: documentation --- cdlib/lifecycles/algorithms/event_analysis.py | 22 +++++++++---------- cdlib/lifecycles/classes/event.py | 9 ++++---- cdlib/test/test_events.py | 19 ++++++++++++++++ docs/reference/events.rst | 3 +-- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/cdlib/lifecycles/algorithms/event_analysis.py b/cdlib/lifecycles/algorithms/event_analysis.py index 9a20cac..a705d84 100644 --- a/cdlib/lifecycles/algorithms/event_analysis.py +++ b/cdlib/lifecycles/algorithms/event_analysis.py @@ -26,9 +26,9 @@ def _analyze_one_struct(target, reference) -> dict: # ids_for_entropy.extend(newels_ids) return { - "U": facet_unicity(ids_for_entropy), - "I": facet_identity(target, reference), - "O": facet_outflow(target, reference), + "Unicity": facet_unicity(ids_for_entropy), + "Identity": facet_identity(target, reference), + "Outflow": facet_outflow(target, reference), "size": len(target), } @@ -69,14 +69,14 @@ def event_weights_from_flow(analyzed_flows: dict, direction: str) -> dict: def _compute_event_scores(analyzed_flow: dict) -> list: return [ - (analyzed_flow["U"]) * (1 - analyzed_flow["I"]) * analyzed_flow["O"], - (1 - analyzed_flow["U"]) * (1 - analyzed_flow["I"]) * analyzed_flow["O"], - (analyzed_flow["U"]) * analyzed_flow["I"] * analyzed_flow["O"], - (1 - analyzed_flow["U"]) * analyzed_flow["I"] * analyzed_flow["O"], - (analyzed_flow["U"]) * analyzed_flow["I"] * (1 - analyzed_flow["O"]), - (1 - analyzed_flow["U"]) * analyzed_flow["I"] * (1 - analyzed_flow["O"]), - (analyzed_flow["U"]) * (1 - analyzed_flow["I"]) * (1 - analyzed_flow["O"]), - (1 - analyzed_flow["U"]) * (1 - analyzed_flow["I"]) * (1 - analyzed_flow["O"]), + (analyzed_flow["Unicity"]) * (1 - analyzed_flow["Identity"]) * analyzed_flow["Outflow"], + (1 - analyzed_flow["Unicity"]) * (1 - analyzed_flow["Identity"]) * analyzed_flow["Outflow"], + (analyzed_flow["Unicity"]) * analyzed_flow["Identity"] * analyzed_flow["Outflow"], + (1 - analyzed_flow["Unicity"]) * analyzed_flow["Identity"] * analyzed_flow["Outflow"], + (analyzed_flow["Unicity"]) * analyzed_flow["Identity"] * (1 - analyzed_flow["Outflow"]), + (1 - analyzed_flow["Unicity"]) * analyzed_flow["Identity"] * (1 - analyzed_flow["Outflow"]), + (analyzed_flow["Unicity"]) * (1 - analyzed_flow["Identity"]) * (1 - analyzed_flow["Outflow"]), + (1 - analyzed_flow["Unicity"]) * (1 - analyzed_flow["Identity"]) * (1 - analyzed_flow["Outflow"]), ] diff --git a/cdlib/lifecycles/classes/event.py b/cdlib/lifecycles/classes/event.py index 995fd32..d93c61e 100644 --- a/cdlib/lifecycles/classes/event.py +++ b/cdlib/lifecycles/classes/event.py @@ -190,8 +190,8 @@ def compute_events_from_explicit_matching(self): } for e in lifecycle: - xtid = e[0].split("_")[0] - ytid = e[1].split("_")[0] + xtid = int(e[0].split("_")[0]) + ytid = int(e[1].split("_")[0]) if xtid < ytid: flows["+"][e[0]][e[1]] = set( self.clustering.get_community(e[0]) @@ -288,8 +288,8 @@ def compute_events_with_custom_matching( } for e in lifecycle: - xtid = e[0].split("_")[0] - ytid = e[1].split("_")[0] + xtid = int(e[0].split("_")[0]) + ytid = int(e[1].split("_")[0]) if e[2] > threshold: if xtid < ytid: flows["+"][e[0]][e[1]] = set( @@ -321,7 +321,6 @@ def __instantiate_events(self, flows, events): self.events[cid].set_out_flow(flows["+"][cid]) for cid in flows["-"]: - print(cid) if cid not in self.events: self.events[cid] = CommunityEvent(cid) self.events[cid].set_in_flow(flows["-"][cid]) diff --git a/cdlib/test/test_events.py b/cdlib/test/test_events.py index b714bb3..44425df 100644 --- a/cdlib/test/test_events.py +++ b/cdlib/test/test_events.py @@ -6,6 +6,7 @@ import networkx as nx from networkx.generators.community import LFR_benchmark_graph import matplotlib.pyplot as plt +import dynetx as dn import os from cdlib.viz import ( plot_flow, @@ -78,6 +79,10 @@ def test_custom_matching(self): c = events.analyze_flows("+") self.assertIsInstance(c, dict) + events.compute_events_with_custom_matching(jaccard, two_sided=False, threshold=0) + c = events.analyze_flows("+") + self.assertIsInstance(c, dict) + def test_polytree(self): tc = TemporalClustering() for t in range(0, 10): @@ -155,6 +160,20 @@ def test_viz(self): plt.savefig("td.pdf") os.remove("td.pdf") + def test_explicit(self): + + dg = dn.DynGraph() + for x in range(10): + g = nx.erdos_renyi_graph(200, 0.05) + dg.add_interactions_from(list(g.edges()), t=x) + coms = algorithms.tiles(dg, 2) + + events = LifeCycle(coms) + events.compute_events_from_explicit_matching() + + c = events.analyze_flows("+") + self.assertIsInstance(c, dict) + def test_node_attributes(self): import random diff --git a/docs/reference/events.rst b/docs/reference/events.rst index 7d1bc69..2ca8bbc 100644 --- a/docs/reference/events.rst +++ b/docs/reference/events.rst @@ -38,8 +38,7 @@ To analyze such pre-computed events apply the following snippet: dg.add_interactions_from(list(g.edges()), t=x) coms = algorithms.tiles(dg, 2) - lc = LifeCycle() - lc.from_temporal_clustering(coms) + lc = LifeCycle(coms) lc.compute_events_from_explicit_matching()