ciga 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ciga/__init__.py +30 -0
- ciga/analysis/__init__.py +13 -0
- ciga/analysis/community_analysis.py +10 -0
- ciga/analysis/graph_analysis.py +138 -0
- ciga/analysis/tgraph_analysis.py +79 -0
- ciga/ciga.py +177 -0
- ciga/examples/__init__.py +0 -0
- ciga/examples/test.py +78 -0
- ciga/utils/__init__.py +1 -0
- ciga/utils/data_utils.py +121 -0
- ciga/utils/graph_utils.py +2 -0
- ciga/visualization/__init__.py +1 -0
- ciga/visualization/graph_plotter.py +26 -0
- ciga-0.0.2.dist-info/LICENSE +674 -0
- ciga-0.0.2.dist-info/METADATA +103 -0
- ciga-0.0.2.dist-info/RECORD +18 -0
- ciga-0.0.2.dist-info/WHEEL +5 -0
- ciga-0.0.2.dist-info/top_level.txt +1 -0
ciga/__init__.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from .ciga import TGraph
|
|
2
|
+
|
|
3
|
+
__version__ = '0.0.2'
|
|
4
|
+
|
|
5
|
+
from .visualization import (
|
|
6
|
+
iplot,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
from .analysis import (
|
|
10
|
+
graph_degree,
|
|
11
|
+
graph_betweenness,
|
|
12
|
+
graph_closeness,
|
|
13
|
+
tgraph_degree,
|
|
14
|
+
tgraph_betweenness,
|
|
15
|
+
tgraph_closeness
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from .utils import (
|
|
19
|
+
prepare_data,
|
|
20
|
+
segment,
|
|
21
|
+
calculate_weights,
|
|
22
|
+
agg_weights
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = ['TGraph', 'iplot',
|
|
26
|
+
'prepare_data', 'segment',
|
|
27
|
+
'calculate_weights', 'agg_weights',
|
|
28
|
+
'graph_degree', 'graph_betweenness', 'graph_closeness',
|
|
29
|
+
'tgraph_degree', 'tgraph_betweenness', 'tgraph_closeness'
|
|
30
|
+
]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .tgraph_analysis import (
|
|
2
|
+
tgraph_degree,
|
|
3
|
+
tgraph_betweenness,
|
|
4
|
+
tgraph_closeness
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
from .graph_analysis import (
|
|
8
|
+
graph_degree,
|
|
9
|
+
graph_betweenness,
|
|
10
|
+
graph_closeness
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = ['graph_degree', 'graph_betweenness', 'tgraph_degree', 'tgraph_betweenness']
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import igraph as ig
|
|
2
|
+
import pandas as pd
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Full graph analysis
|
|
7
|
+
def graph_degree(graph: ig.Graph, weighted=False, normalized=False) -> pd.DataFrame:
|
|
8
|
+
"""
|
|
9
|
+
Normalized degree is divided by n-1, where n is the number of vertices in the graph.
|
|
10
|
+
As networkx
|
|
11
|
+
:param graph:
|
|
12
|
+
:param weighted:
|
|
13
|
+
:param normalized:
|
|
14
|
+
:return:
|
|
15
|
+
"""
|
|
16
|
+
result = pd.DataFrame()
|
|
17
|
+
directed = graph.is_directed()
|
|
18
|
+
normalize_factor = graph.vcount() - 1
|
|
19
|
+
if directed:
|
|
20
|
+
in_degree = graph.degree(mode='in')
|
|
21
|
+
out_degree = graph.degree(mode='out')
|
|
22
|
+
all_degree = graph.degree(mode='all')
|
|
23
|
+
result['in_degree'] = in_degree
|
|
24
|
+
result['out_degree'] = out_degree
|
|
25
|
+
result['all_degree'] = all_degree
|
|
26
|
+
|
|
27
|
+
if weighted:
|
|
28
|
+
weighted_in_degree = graph.strength(mode='in', weights='weight')
|
|
29
|
+
weighted_out_degree = graph.strength(mode='out', weights='weight')
|
|
30
|
+
weighted_all_degree = graph.strength(mode='all', weights='weight')
|
|
31
|
+
result['weighted_in_degree'] = weighted_in_degree
|
|
32
|
+
result['weighted_out_degree'] = weighted_out_degree
|
|
33
|
+
result['weighted_all_degree'] = weighted_all_degree
|
|
34
|
+
else:
|
|
35
|
+
graph_degree = graph.degree()
|
|
36
|
+
result['degree'] = graph_degree
|
|
37
|
+
|
|
38
|
+
if weighted:
|
|
39
|
+
weighted_degree = graph.strength(weights='weight')
|
|
40
|
+
result['weighted_degree'] = weighted_degree
|
|
41
|
+
|
|
42
|
+
if normalized:
|
|
43
|
+
if directed:
|
|
44
|
+
result['normalized_in_degree'] = result['in_degree'] / normalize_factor
|
|
45
|
+
result['normalized_out_degree'] = result['out_degree'] / normalize_factor
|
|
46
|
+
result['normalized_all_degree'] = result['all_degree'] / normalize_factor
|
|
47
|
+
if weighted:
|
|
48
|
+
result['normalized_weighted_in_degree'] = result['weighted_in_degree'] / normalize_factor
|
|
49
|
+
result['normalized_weighted_out_degree'] = result['weighted_out_degree'] / normalize_factor
|
|
50
|
+
result['normalized_weighted_all_degree'] = result['weighted_all_degree'] / normalize_factor
|
|
51
|
+
else:
|
|
52
|
+
result['normalized_degree'] = result['degree'] / normalize_factor
|
|
53
|
+
if weighted:
|
|
54
|
+
result['normalized_weighted_degree'] = result['weighted_degree'] / normalize_factor
|
|
55
|
+
|
|
56
|
+
# print(result)
|
|
57
|
+
return result
|
|
58
|
+
|
|
59
|
+
def graph_betweenness(graph: ig.Graph, weighted=False, cutoff=None, sources=None, targets=None, normalized=False) -> pd.DataFrame:
|
|
60
|
+
result = pd.DataFrame()
|
|
61
|
+
directed = graph.is_directed()
|
|
62
|
+
betweenness = graph.betweenness(weights=None, directed=directed, cutoff=cutoff, sources=sources, targets=targets)
|
|
63
|
+
result['betweenness'] = betweenness
|
|
64
|
+
if weighted:
|
|
65
|
+
weighted_betweenness = graph.betweenness(weights='weight', directed=directed, cutoff=cutoff, sources=sources, targets=targets)
|
|
66
|
+
result['weighted_betweenness'] = weighted_betweenness
|
|
67
|
+
|
|
68
|
+
if normalized:
|
|
69
|
+
# Normalization factors based on whether the graph is directed
|
|
70
|
+
n = graph.vcount()
|
|
71
|
+
if directed:
|
|
72
|
+
normalize_factor = (n - 1) * (n - 2)
|
|
73
|
+
else:
|
|
74
|
+
normalize_factor = (n - 1) * (n - 2) / 2
|
|
75
|
+
|
|
76
|
+
result['normalized_betweenness'] = result['betweenness'] / normalize_factor
|
|
77
|
+
if weighted:
|
|
78
|
+
result['normalized_weighted_betweenness'] = result['weighted_betweenness'] / normalize_factor
|
|
79
|
+
|
|
80
|
+
return result
|
|
81
|
+
|
|
82
|
+
def graph_closeness(graph: ig.Graph, weighted, cutoff=None, normalized=False) -> pd.DataFrame:
|
|
83
|
+
result = pd.DataFrame()
|
|
84
|
+
directed = graph.is_directed()
|
|
85
|
+
|
|
86
|
+
if directed:
|
|
87
|
+
in_closeness = graph.closeness(weights=None, mode="in", cutoff=cutoff)
|
|
88
|
+
out_closeness = graph.closeness(weights=None, mode="out", cutoff=cutoff)
|
|
89
|
+
all_closeness = graph.closeness(weights=None, mode="all", cutoff=cutoff)
|
|
90
|
+
result['in_closeness'] = in_closeness
|
|
91
|
+
result['out_closeness'] = out_closeness
|
|
92
|
+
result['all_closeness'] = all_closeness
|
|
93
|
+
|
|
94
|
+
if weighted:
|
|
95
|
+
weighted_in_closeness = graph.closeness(weights='weight', mode="in", cutoff=cutoff)
|
|
96
|
+
weighted_out_closeness = graph.closeness(weights='weight', mode="out", cutoff=cutoff)
|
|
97
|
+
weighted_all_closeness = graph.closeness(weights='weight', mode="all", cutoff=cutoff)
|
|
98
|
+
result['weighted_in_closeness'] = weighted_in_closeness
|
|
99
|
+
result['weighted_out_closeness'] = weighted_out_closeness
|
|
100
|
+
result['weighted_all_closeness'] = weighted_all_closeness
|
|
101
|
+
else:
|
|
102
|
+
closeness = graph.closeness(weights=None, cutoff=cutoff)
|
|
103
|
+
result['closeness'] = closeness
|
|
104
|
+
|
|
105
|
+
if weighted:
|
|
106
|
+
weighted_closeness = graph.closeness(weights='weight', cutoff=cutoff)
|
|
107
|
+
result['weighted_closeness'] = weighted_closeness
|
|
108
|
+
|
|
109
|
+
if normalized:
|
|
110
|
+
if directed:
|
|
111
|
+
normalized_in_closeness = graph.closeness(weights=None, mode="in", cutoff=cutoff, normalized=True)
|
|
112
|
+
normalized_out_closeness = graph.closeness(weights=None, mode="out", cutoff=cutoff, normalized=True)
|
|
113
|
+
normalized_all_closeness = graph.closeness(weights=None, mode="all", cutoff=cutoff, normalized=True)
|
|
114
|
+
result['normalized_in_closeness'] = normalized_in_closeness
|
|
115
|
+
result['normalized_out_closeness'] = normalized_out_closeness
|
|
116
|
+
result['normalized_all_closeness'] = normalized_all_closeness
|
|
117
|
+
|
|
118
|
+
if weighted:
|
|
119
|
+
normalized_weighted_in_closeness = graph.closeness(weights='weight', mode="in", cutoff=cutoff, normalized=True)
|
|
120
|
+
normalized_weighted_out_closeness = graph.closeness(weights='weight', mode="out", cutoff=cutoff, normalized=True)
|
|
121
|
+
normalized_weighted_all_closeness = graph.closeness(weights='weight', mode="all", cutoff=cutoff, normalized=True)
|
|
122
|
+
result['normalized_weighted_in_closeness'] = normalized_weighted_in_closeness
|
|
123
|
+
result['normalized_weighted_out_closeness'] = normalized_weighted_out_closeness
|
|
124
|
+
result['normalized_weighted_all_closeness'] = normalized_weighted_all_closeness
|
|
125
|
+
else:
|
|
126
|
+
normalized_closeness = graph.closeness(weights=None, cutoff=cutoff, normalized=True)
|
|
127
|
+
result['normalized_closeness'] = normalized_closeness
|
|
128
|
+
|
|
129
|
+
if weighted:
|
|
130
|
+
normalized_weighted_closeness = graph.closeness(weights='weight', cutoff=cutoff, normalized=True)
|
|
131
|
+
result['normalized_weighted_closeness'] = normalized_weighted_closeness
|
|
132
|
+
|
|
133
|
+
return result
|
|
134
|
+
|
|
135
|
+
# Single node analysis
|
|
136
|
+
# def weighted_degree(graph: ig.Graph, node: int) -> float:
|
|
137
|
+
# if graph.is_directed():
|
|
138
|
+
# weighted_degree = graph.strength(node, weights='weight')
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
from typing import Callable, Dict, Any, List
|
|
3
|
+
from ..ciga import TGraph
|
|
4
|
+
from .graph_analysis import graph_degree, graph_betweenness, graph_closeness
|
|
5
|
+
from tqdm import tqdm
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def sequential_analysis(tg: TGraph,
|
|
9
|
+
analysis_func: Callable[..., Dict[str, List[float]]],
|
|
10
|
+
*,
|
|
11
|
+
start=None,
|
|
12
|
+
end=None,
|
|
13
|
+
**analysis_kwargs) -> pd.DataFrame:
|
|
14
|
+
"""
|
|
15
|
+
Perform analysis on a time-varying graph sequentially.
|
|
16
|
+
:param tg:
|
|
17
|
+
:param analysis_func:
|
|
18
|
+
:param start:
|
|
19
|
+
:param end:
|
|
20
|
+
:param analysis_kwargs:
|
|
21
|
+
:return:
|
|
22
|
+
"""
|
|
23
|
+
results = pd.DataFrame()
|
|
24
|
+
# add position columns to results
|
|
25
|
+
for col in tg._position:
|
|
26
|
+
results[col] = []
|
|
27
|
+
results['character'] = []
|
|
28
|
+
|
|
29
|
+
# get time steps
|
|
30
|
+
time_steps = tg.data.index.unique().tolist()
|
|
31
|
+
if start:
|
|
32
|
+
time_steps = [step for step in time_steps if step >= start]
|
|
33
|
+
if end:
|
|
34
|
+
time_steps = [step for step in time_steps if step <= end]
|
|
35
|
+
|
|
36
|
+
for step in tqdm(time_steps, desc='Processing time steps', unit='step'):
|
|
37
|
+
graph = tg.get_graph(step)
|
|
38
|
+
|
|
39
|
+
measures = analysis_func(graph, **analysis_kwargs)
|
|
40
|
+
for col, val in zip(tg._position, step):
|
|
41
|
+
measures[col] = val
|
|
42
|
+
measures['character'] = graph.vs['name']
|
|
43
|
+
|
|
44
|
+
results = pd.concat([results, measures], axis=0).reset_index(drop=True)
|
|
45
|
+
|
|
46
|
+
return results
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def tgraph_degree(tgraph: TGraph, *, start=None, end=None, weighted=False, normalized=False) -> pd.DataFrame:
|
|
50
|
+
return sequential_analysis(tgraph, analysis_func=graph_degree, start=start, end=end, weighted=weighted,
|
|
51
|
+
normalized=normalized)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def tgraph_betweenness(tgraph: TGraph, *, start=None, end=None, weighted=False, normalized=True,
|
|
55
|
+
cutoff=None, sources=None, targets=None) -> pd.DataFrame:
|
|
56
|
+
return sequential_analysis(tgraph, analysis_func=graph_betweenness, start=start, end=end, weighted=weighted,
|
|
57
|
+
normalized=normalized, cutoff=cutoff, sources=sources, targets=targets)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def tgraph_closeness(tgraph: TGraph, *, start=None, end=None, weighted=False, normalized=True,
|
|
61
|
+
cutoff=None) -> pd.DataFrame:
|
|
62
|
+
return sequential_analysis(tgraph, analysis_func=graph_closeness, start=start, end=end, weighted=weighted, normalized=normalized,
|
|
63
|
+
cutoff=cutoff)
|
|
64
|
+
|
|
65
|
+
# def eigenvector_centrality(tgraph: TGraph, *, start=None, end=None, max_iter=100, tol=1e-06, weight=None) -> pd.DataFrame:
|
|
66
|
+
# def eigenvector_centrality_func(graph: ig.Graph) -> Dict[str, List[float]]:
|
|
67
|
+
# if weight is not None:
|
|
68
|
+
# weights = graph.es[weight] # Assuming 'weight' is the name of the edge attribute
|
|
69
|
+
# else:
|
|
70
|
+
# weights = None
|
|
71
|
+
# eigenvector = graph.eigenvector_centrality(
|
|
72
|
+
# weights=weights,
|
|
73
|
+
# directed=graph.is_directed(),
|
|
74
|
+
# niter=max_iter,
|
|
75
|
+
# epsilon=tol
|
|
76
|
+
# )
|
|
77
|
+
# return {'eigenvector': eigenvector}
|
|
78
|
+
#
|
|
79
|
+
# return sequential_analysis(tgraph, eigenvector_centrality_func, start=start, end=end)
|
ciga/ciga.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import numpy as np
|
|
3
|
+
from igraph import Graph
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TGraph:
|
|
7
|
+
|
|
8
|
+
def __init__(self, init_graph=None, data=None, position=None, directed=True):
|
|
9
|
+
if data is None:
|
|
10
|
+
raise ValueError("Data must be provided.")
|
|
11
|
+
if position is None:
|
|
12
|
+
raise ValueError("Position must be provided.")
|
|
13
|
+
if 'weight' not in data.columns:
|
|
14
|
+
raise ValueError("Weight column not found in data. Run calculate_weights() first.")
|
|
15
|
+
|
|
16
|
+
self.data = data.copy()
|
|
17
|
+
self._position = position
|
|
18
|
+
self._layout = None
|
|
19
|
+
self._directed = directed
|
|
20
|
+
self._vnames = []
|
|
21
|
+
self.name_to_id = {}
|
|
22
|
+
self.id_to_name = {}
|
|
23
|
+
self._cache_graph = None
|
|
24
|
+
self._cache_time_point = tuple([-np.inf] * len(self._position))
|
|
25
|
+
|
|
26
|
+
# set multi index if not already
|
|
27
|
+
if not isinstance(self.data.index, pd.MultiIndex):
|
|
28
|
+
self.data.set_index(list(self._position), inplace=True)
|
|
29
|
+
|
|
30
|
+
if not self._directed:
|
|
31
|
+
idx = self.data['source'] > self.data['target']
|
|
32
|
+
self.data.loc[idx, ['source', 'target']] = self.data.loc[idx, ['target', 'source']].values
|
|
33
|
+
|
|
34
|
+
if init_graph is None:
|
|
35
|
+
self._init_graph = Graph(directed=self._directed)
|
|
36
|
+
self._init_graph.vs['name'] = []
|
|
37
|
+
else:
|
|
38
|
+
self._init_graph = init_graph
|
|
39
|
+
self._directed = init_graph.is_directed()
|
|
40
|
+
|
|
41
|
+
# create name to id mapping
|
|
42
|
+
self._vnames = self._init_graph.vs['name']
|
|
43
|
+
self._create_vnames()
|
|
44
|
+
|
|
45
|
+
self._cache_graph = self._init_graph.copy()
|
|
46
|
+
|
|
47
|
+
def _create_vnames(self):
|
|
48
|
+
self.name_to_id = {name: idx for idx, name in enumerate(self._vnames)}
|
|
49
|
+
self.id_to_name = {idx: name for idx, name in enumerate(self._vnames)}
|
|
50
|
+
|
|
51
|
+
return self._vnames
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def is_directed(self):
|
|
55
|
+
return self._directed
|
|
56
|
+
|
|
57
|
+
def get_graph(self, time_point=None):
|
|
58
|
+
if time_point is None:
|
|
59
|
+
time_point = tuple([np.inf] * len(self._position))
|
|
60
|
+
if len(time_point) < len(self._position):
|
|
61
|
+
time_point = tuple(list(time_point) + [np.inf] * (len(self._position) - len(time_point)))
|
|
62
|
+
|
|
63
|
+
if self._cache_time_point and self._cache_time_point < time_point:
|
|
64
|
+
start = list(self._cache_time_point)
|
|
65
|
+
start[-1] += 1
|
|
66
|
+
delta_data = self.take_interval(start=start, end=time_point)
|
|
67
|
+
else:
|
|
68
|
+
delta_data = self.take_interval(start=None, end=time_point)
|
|
69
|
+
self._cache_graph = self._init_graph.copy()
|
|
70
|
+
# also reset the name to id mapping
|
|
71
|
+
|
|
72
|
+
if delta_data.empty:
|
|
73
|
+
return self._cache_graph
|
|
74
|
+
self._update_graph(self._cache_graph, delta_data)
|
|
75
|
+
self._cache_time_point = time_point
|
|
76
|
+
return self._cache_graph
|
|
77
|
+
|
|
78
|
+
def get_delta_graph(self, start=None, end=None):
|
|
79
|
+
# this should not change any data in the tgraph
|
|
80
|
+
if start is not None and len(start) == len(self._position):
|
|
81
|
+
start = list(start)
|
|
82
|
+
start[-1] += 1
|
|
83
|
+
delta_data = self.take_interval(start=start, end=end)
|
|
84
|
+
if delta_data.empty:
|
|
85
|
+
interval_graph = Graph(directed=self._directed)
|
|
86
|
+
else:
|
|
87
|
+
agg_data = delta_data.groupby(['source', 'target'], as_index=False)['weight'].sum()
|
|
88
|
+
nodes = set(agg_data['source']).union(set(agg_data['target']))
|
|
89
|
+
temp_node_to_id = {node: idx for idx, node in enumerate(nodes)}
|
|
90
|
+
source_ids = agg_data['source'].map(temp_node_to_id)
|
|
91
|
+
target_ids = agg_data['target'].map(temp_node_to_id)
|
|
92
|
+
edges = list(zip(source_ids, target_ids))
|
|
93
|
+
interval_graph = Graph(edges=edges, directed=self._directed)
|
|
94
|
+
|
|
95
|
+
return interval_graph
|
|
96
|
+
|
|
97
|
+
def _update_graph(self, graph, data):
|
|
98
|
+
# print(data.head())
|
|
99
|
+
agg_data = data.groupby(['source', 'target'], as_index=False)['weight'].sum()
|
|
100
|
+
|
|
101
|
+
# NEW IMPLEMENTATION
|
|
102
|
+
existing_nodes = set(graph.vs['name'])
|
|
103
|
+
new_nodes = set(agg_data['source']).union(set(agg_data['target'])) - set(existing_nodes)
|
|
104
|
+
|
|
105
|
+
# add new nodes to graph, also to self._vnames
|
|
106
|
+
if new_nodes:
|
|
107
|
+
graph.add_vertices(list(new_nodes))
|
|
108
|
+
self._vnames = graph.vs['name'] # keep _vnames the newest
|
|
109
|
+
# update name to id mapping, start from the last index
|
|
110
|
+
for idx, node in enumerate(new_nodes, start=len(existing_nodes)):
|
|
111
|
+
self.name_to_id[node] = idx
|
|
112
|
+
self.id_to_name[idx] = node
|
|
113
|
+
|
|
114
|
+
# add columns for source and target ids
|
|
115
|
+
agg_data['source_id'] = agg_data['source'].map(self.name_to_id)
|
|
116
|
+
agg_data['target_id'] = agg_data['target'].map(self.name_to_id)
|
|
117
|
+
|
|
118
|
+
existing_edges = graph.get_edgelist()
|
|
119
|
+
edges_in_data = pd.DataFrame(existing_edges, columns=['source_id', 'target_id'])
|
|
120
|
+
|
|
121
|
+
# merge to identify existing and new edges
|
|
122
|
+
df_merged = pd.merge(
|
|
123
|
+
agg_data,
|
|
124
|
+
edges_in_data,
|
|
125
|
+
on=['source_id', 'target_id'],
|
|
126
|
+
how='left',
|
|
127
|
+
indicator=True
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
df_new_edges = df_merged[df_merged['_merge'] == 'left_only']
|
|
131
|
+
df_to_update = df_merged[df_merged['_merge'] == 'both']
|
|
132
|
+
|
|
133
|
+
new_edges = list(zip(df_new_edges['source_id'], df_new_edges['target_id']))
|
|
134
|
+
new_weights = df_new_edges['weight'].tolist()
|
|
135
|
+
edges_to_update = list(zip(df_to_update['source_id'], df_to_update['target_id']))
|
|
136
|
+
existing_edge_ids_to_update = graph.get_eids(pairs=edges_to_update, directed=graph.is_directed())
|
|
137
|
+
weight_updates = df_to_update['weight'].tolist()
|
|
138
|
+
|
|
139
|
+
# Add new edges in bulk
|
|
140
|
+
if new_edges:
|
|
141
|
+
graph.add_edges(new_edges)
|
|
142
|
+
# Set weights for new edges
|
|
143
|
+
new_edge_ids = graph.es[-len(new_edges):].indices
|
|
144
|
+
graph.es[new_edge_ids]['weight'] = new_weights
|
|
145
|
+
|
|
146
|
+
# Update weights of existing edges in bulk
|
|
147
|
+
if existing_edge_ids_to_update:
|
|
148
|
+
# Retrieve current weights
|
|
149
|
+
current_weights = graph.es[existing_edge_ids_to_update]['weight']
|
|
150
|
+
# Update weights
|
|
151
|
+
updated_weights = [cw + w for cw, w in zip(current_weights, weight_updates)]
|
|
152
|
+
graph.es[existing_edge_ids_to_update]['weight'] = updated_weights
|
|
153
|
+
|
|
154
|
+
return graph
|
|
155
|
+
|
|
156
|
+
def take_interval(self, start=None, end=None):
|
|
157
|
+
if start is None:
|
|
158
|
+
start = [-np.inf] * len(self._position)
|
|
159
|
+
else:
|
|
160
|
+
if len(start) < len(self._position):
|
|
161
|
+
start = list(start) + [None] * (len(self._position) - len(start))
|
|
162
|
+
start = [s if s is not None else -np.inf for s in start]
|
|
163
|
+
|
|
164
|
+
if end is None:
|
|
165
|
+
end = [np.inf] * len(self._position)
|
|
166
|
+
else:
|
|
167
|
+
if len(end) < len(self._position):
|
|
168
|
+
end = list(end) + [None] * (len(self._position) - len(end))
|
|
169
|
+
end = [e if e is not None else np.inf for e in end]
|
|
170
|
+
|
|
171
|
+
if len(self._position) > len(start) or len(self._position) > len(end):
|
|
172
|
+
raise ValueError("The length of 'start/end' is out of range.")
|
|
173
|
+
|
|
174
|
+
idx = pd.IndexSlice
|
|
175
|
+
filtered_data = self.data.loc[idx[tuple(start): tuple(end)], :].copy()
|
|
176
|
+
|
|
177
|
+
return filtered_data
|
|
File without changes
|
ciga/examples/test.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import ciga as cg
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
|
4
|
+
import igraph as ig
|
|
5
|
+
|
|
6
|
+
from google.cloud import language_v2
|
|
7
|
+
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
|
|
10
|
+
from viztracer import VizTracer
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# sid = SentimentIntensityAnalyzer()
|
|
14
|
+
# Example usage
|
|
15
|
+
|
|
16
|
+
df = pd.read_csv('../../bigbang_lines_with_sentiment_utf8.csv')
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# custom weight function
|
|
20
|
+
# input: interaction (str)
|
|
21
|
+
# output: weight (float)
|
|
22
|
+
# def weight_func(interaction):
|
|
23
|
+
# # return sid.polarity_scores(interaction)['neg']
|
|
24
|
+
# return 1
|
|
25
|
+
|
|
26
|
+
# tracer = VizTracer()
|
|
27
|
+
# tracer.start()
|
|
28
|
+
|
|
29
|
+
# load data
|
|
30
|
+
# interactions = cg.prepare_data(df, ('Season', 'Episode', 'Scene', 'Line'),
|
|
31
|
+
# source='Speaker', target='Listener', interaction='Words')
|
|
32
|
+
# sub_interactions = cg.segment(interactions, start=(1, 1, 1, 1), end=(2, 1, 1, 1))
|
|
33
|
+
# print(sub_interactions)
|
|
34
|
+
weights = cg.prepare_data(df, ('Season', 'Episode', 'Scene', 'Line'), source='Speaker', target='Listener', interaction='Words', weight='weight')
|
|
35
|
+
sub_weights = cg.segment(weights, start=(1, 1, 1, 1), end=(2, 1, 1, 1))
|
|
36
|
+
# weights = cg.get_weights(sub_interactions, weight_func=analyze_sentiment)
|
|
37
|
+
# print(weights.head())
|
|
38
|
+
# print(weights)
|
|
39
|
+
# agg_weights = cg.agg_weights(weights, ('Season', 'Episode', 'Scene', 'Line'), agg_func=lambda x: sum(x))
|
|
40
|
+
|
|
41
|
+
# print(agg_weights)
|
|
42
|
+
# agg_weights.set_index(['Season', 'Episode', 'Scene', 'Line'], inplace=True)
|
|
43
|
+
# print(agg_weights)
|
|
44
|
+
# idx = pd.IndexSlice
|
|
45
|
+
# print(agg_weights.loc[idx[-1, 1, 2, 1]: idx[1, 1, 2, 4], :])
|
|
46
|
+
|
|
47
|
+
# create network
|
|
48
|
+
tg = cg.TGraph(data=sub_weights, position=('Season', 'Episode', 'Scene', 'Line'), directed=True)
|
|
49
|
+
|
|
50
|
+
# print(tg.data)
|
|
51
|
+
# graph = tg.get_graph((1, 1, 2))'
|
|
52
|
+
# graph = tg.get_graph((1, 1, 2, 1))
|
|
53
|
+
graph = tg.get_graph()
|
|
54
|
+
|
|
55
|
+
# print('tg._vnames:', tg._vnames)
|
|
56
|
+
# print(graph.vs['name'])
|
|
57
|
+
|
|
58
|
+
# print(graph.is_directed())
|
|
59
|
+
# tg.data.head()
|
|
60
|
+
fig, ax = plt.subplots()
|
|
61
|
+
cg.iplot(graph, target=ax)
|
|
62
|
+
|
|
63
|
+
# tracer.stop()
|
|
64
|
+
# tracer.save()
|
|
65
|
+
|
|
66
|
+
# centrality analysis
|
|
67
|
+
# res = cg.tgraph_degree(tg, weighted=True, normalized=True)
|
|
68
|
+
res1 = cg.tgraph_degree(tg, weighted=True, normalized=True)
|
|
69
|
+
# res2 = cg.tgraph_betweenness(tg, weighted=True, normalized=True)
|
|
70
|
+
# res3 = cg.tgraph_closeness(tg, weighted=True, normalized=True)
|
|
71
|
+
# print(res)
|
|
72
|
+
# res.head()
|
|
73
|
+
|
|
74
|
+
res1.to_csv('sentiment_score_degree.csv', index=False)
|
|
75
|
+
# res2.to_csv('sentiment_score_betweenness.csv', index=False)
|
|
76
|
+
# res3.to_csv('sentiment_score_closeness.csv', index=False)
|
|
77
|
+
|
|
78
|
+
plt.show()
|
ciga/utils/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .data_utils import prepare_data, segment, calculate_weights, agg_weights
|
ciga/utils/data_utils.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
from typing import Tuple, Optional
|
|
3
|
+
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def prepare_data(
|
|
8
|
+
data: pd.DataFrame,
|
|
9
|
+
position: Tuple[str, ...],
|
|
10
|
+
source: str = 'source',
|
|
11
|
+
target: str = 'target',
|
|
12
|
+
interaction: str = 'interaction',
|
|
13
|
+
weight: Optional[str] = None,
|
|
14
|
+
):
|
|
15
|
+
"""
|
|
16
|
+
Prepare data, validation, and renaming columns
|
|
17
|
+
:param weight:
|
|
18
|
+
:param data:
|
|
19
|
+
:param position:
|
|
20
|
+
:param source:
|
|
21
|
+
:param target:
|
|
22
|
+
:param interaction:
|
|
23
|
+
:return:
|
|
24
|
+
"""
|
|
25
|
+
# check required columns
|
|
26
|
+
required_columns = list(position) + [source, target]
|
|
27
|
+
if weight:
|
|
28
|
+
required_columns.append(weight)
|
|
29
|
+
if not all(col in data.columns for col in required_columns):
|
|
30
|
+
raise ValueError(f"""Missing required columns.\nExpected: {required_columns}\nFound: {data.columns.tolist()}""")
|
|
31
|
+
|
|
32
|
+
# examine data
|
|
33
|
+
_check_numeric_position(data, position)
|
|
34
|
+
|
|
35
|
+
# sort by position
|
|
36
|
+
df = data.sort_values(by=list(position)).reset_index(drop=True)
|
|
37
|
+
|
|
38
|
+
# use multi-index for quick interval selection
|
|
39
|
+
df.set_index(list(position), inplace=True)
|
|
40
|
+
|
|
41
|
+
# rename columns
|
|
42
|
+
df = df.rename(columns={source: 'source', target: 'target'})
|
|
43
|
+
if interaction:
|
|
44
|
+
df = df.rename(columns={interaction: 'interaction'})
|
|
45
|
+
|
|
46
|
+
# Process 'source', 'target', 'observer' columns to ensure lists
|
|
47
|
+
df['source'] = _process_column(df['source'])
|
|
48
|
+
df['target'] = _process_column(df['target'])
|
|
49
|
+
|
|
50
|
+
if weight:
|
|
51
|
+
df = df.rename(columns={weight: 'weight'})
|
|
52
|
+
df['weight'] = df['weight'].astype(float)
|
|
53
|
+
df = _flatten_weights(df)
|
|
54
|
+
|
|
55
|
+
return df
|
|
56
|
+
|
|
57
|
+
def segment(data, start=None, end=None, position=None):
|
|
58
|
+
"""
|
|
59
|
+
Get interactions based on interval
|
|
60
|
+
:param data: dataframe
|
|
61
|
+
:param interval: interval
|
|
62
|
+
:return: a dataframe with interactions
|
|
63
|
+
"""
|
|
64
|
+
if (len(start) > 1 or len(end) > 1) and not isinstance(data.index, pd.MultiIndex):
|
|
65
|
+
if position is None:
|
|
66
|
+
raise ValueError("Position columns required for multi-level time step.")
|
|
67
|
+
data.set_index(list(position), inplace=True)
|
|
68
|
+
idx = pd.IndexSlice
|
|
69
|
+
interval = data.loc[idx[start:end], :].copy()
|
|
70
|
+
|
|
71
|
+
return interval
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def calculate_weights(data, weight_func=lambda x: len(x)):
|
|
75
|
+
"""
|
|
76
|
+
Calculate weights based on interaction
|
|
77
|
+
:param data: dataframe
|
|
78
|
+
:param weight_func: a function to calculate edge weights
|
|
79
|
+
:return: a dictionary of edge weights
|
|
80
|
+
"""
|
|
81
|
+
data['weight'] = data['interaction'].apply(weight_func)
|
|
82
|
+
data = _flatten_weights(data)
|
|
83
|
+
return data
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _flatten_weights(df):
|
|
87
|
+
"""
|
|
88
|
+
Flatten weights in the dataframe
|
|
89
|
+
:param df: dataframe
|
|
90
|
+
:return: a dataframe with flattened weights
|
|
91
|
+
"""
|
|
92
|
+
return df.explode('source').explode('target')
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def agg_weights(data, position, agg_func=lambda x: sum(x)):
|
|
96
|
+
# group by position and source, target, observer
|
|
97
|
+
# raise error if 'weight' column is not found
|
|
98
|
+
if 'weight' not in data.columns:
|
|
99
|
+
raise ValueError("No 'weight' column found. You should run calculate_weights() first.")
|
|
100
|
+
grouped = data.groupby(list(position) + ['source', 'target'])['weight'].agg(agg_func).reset_index()
|
|
101
|
+
return grouped
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _process_column(series):
|
|
105
|
+
def clean_cell(cell):
|
|
106
|
+
if isinstance(cell, list):
|
|
107
|
+
items = cell
|
|
108
|
+
elif isinstance(cell, str):
|
|
109
|
+
items = [item.strip() for item in cell.strip('[]').split(',')]
|
|
110
|
+
else:
|
|
111
|
+
items = [str(cell).strip()]
|
|
112
|
+
return pd.unique([str(item).strip() for item in items])
|
|
113
|
+
|
|
114
|
+
return series.apply(clean_cell)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _check_numeric_position(data, position):
|
|
118
|
+
# check required columns
|
|
119
|
+
for col in position:
|
|
120
|
+
if not pd.api.types.is_numeric_dtype(data[col]):
|
|
121
|
+
raise ValueError(f"Position column '{col}' must be numeric.")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .graph_plotter import iplot
|