-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ComplexData-MILA/elahe
Updated new features
- Loading branch information
Showing
20 changed files
with
1,794 additions
and
1,980 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#dataset | ||
*.png | ||
>>>>>>> master | ||
*.cpython-39.pyc | ||
*.pyc | ||
*.xz | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,61 @@ | ||
import tgx | ||
from tgx.utils.graph_utils import list2csv | ||
from tgx.utils.plotting_utils import plot_for_snapshots | ||
|
||
""" | ||
1. load a dataset | ||
2. load into a graph | ||
3. discretize the graph | ||
4. save the graph back to a csv | ||
master example to show all visualization in tgx | ||
""" | ||
|
||
#! load the datasets | ||
# dataset = tgx.builtin.uci() #built in datasets | ||
# data_name = "uci" | ||
#* load built in datasets | ||
dataset = tgx.builtin.uci() #built in datasets | ||
|
||
data_name = "tgbn-token" #"tgbl-wiki" #"tgbl-review" | ||
dataset = tgx.tgb_data(data_name) #tgb datasets | ||
#* load the tgb datasets | ||
# data_name = "tgbl-wiki" #"tgbl-review" | ||
# dataset = tgx.tgb_data(data_name) #tgb datasets | ||
|
||
|
||
time_scale = "daily" #"hourly" | ||
ctdg = tgx.Graph(dataset) | ||
dtdg = ctdg.discretize(time_scale=time_scale) | ||
time_scale = "weekly" #"daily" | ||
dtdg = ctdg.discretize(time_scale=time_scale)[0] | ||
|
||
|
||
#! plotting the statistics, works | ||
tgx.degree_over_time(dtdg, network_name=data_name) | ||
tgx.nodes_over_time(dtdg, network_name=data_name) | ||
tgx.edges_over_time(dtdg, network_name=data_name) | ||
tgx.nodes_and_edges_over_time(dtdg, network_name=data_name) | ||
#* plotting the statistics | ||
tgx.degree_over_time(dtdg, network_name=dataset.name) | ||
tgx.nodes_over_time(dtdg, network_name=dataset.name) | ||
tgx.edges_over_time(dtdg, network_name=dataset.name) | ||
tgx.nodes_and_edges_over_time(dtdg, network_name=dataset.name) | ||
|
||
tgx.TET(dtdg, | ||
network_name=data_name, | ||
network_name=dataset.name, | ||
figsize = (9, 5), | ||
axis_title_font_size = 24, | ||
ticks_font_size = 24) | ||
|
||
|
||
tgx.TEA(dtdg, | ||
network_name=data_name) | ||
network_name=dataset.name) | ||
|
||
|
||
|
||
#! compute statistics | ||
#* compute statistics | ||
test_ratio = 0.15 | ||
tgx.get_reoccurrence(dtdg, test_ratio=test_ratio) | ||
tgx.get_surprise(dtdg, test_ratio=test_ratio) | ||
|
||
#* these two much faster on dtdgs | ||
tgx.get_avg_node_activity(dtdg) | ||
tgx.get_reoccurrence(ctdg, test_ratio=test_ratio) | ||
tgx.get_surprise(ctdg, test_ratio=test_ratio) | ||
tgx.get_novelty(dtdg) | ||
|
||
|
||
# Number of Connected Components | ||
tgx.connected_components_per_ts(dtdg, network_name=dataset.name) | ||
|
||
# Size of Largest Connected Component | ||
component_sizes = tgx.size_connected_components(dtdg) | ||
largest_component_sizes = [max(inner_list) if inner_list else 0 for inner_list in component_sizes] | ||
filename = f"{dataset.name}_largest_connected_component_size" | ||
plot_for_snapshots(largest_component_sizes, y_title="Size of Largest Connected Component", filename="./"+filename) | ||
|
||
# Average Node Engagement | ||
engagements = tgx.get_avg_node_engagement(dtdg) | ||
filename = f"{dataset.name}_average_node_engagement" | ||
plot_for_snapshots(engagements, y_title="Average Engagement", filename="./"+filename) | ||
|
||
# #! statistics to be updated and fixed | ||
# #TODO | ||
# tgx.degree_density() | ||
# tgx.connected_components_per_ts() | ||
# tgx.size_connected_components() | ||
# tgx.get_avg_node_engagement() | ||
# Degree Density | ||
tgx.degree_density(dtdg, k=3, network_name=dataset.name) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
from tgx.classes.graph import Graph | ||
|
||
from tgx.data.builtin import builtin | ||
from tgx.data.tgb import tgb_data | ||
|
||
from tgx.io.read import read_csv | ||
from tgx.io.write import write_csv | ||
|
||
from tgx.viz.TEA import TEA | ||
from tgx.viz.TET import TET | ||
|
||
from tgx.utils.stat import * | ||
from tgx.utils.graph_utils import * | ||
|
||
from tgx.classes.graph import Graph | ||
|
||
from tgx.data.builtin import builtin | ||
from tgx.data.tgb import tgb_data | ||
|
||
from tgx.io.read import read_csv | ||
from tgx.io.write import write_csv | ||
|
||
from tgx.viz.TEA import TEA | ||
from tgx.viz.TET import TET | ||
|
||
from tgx.utils.stat import * | ||
from tgx.utils.graph_utils import * | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,85 @@ | ||
import networkx as nx | ||
from typing import Optional | ||
|
||
|
||
class Graph(): | ||
def __init__(self, | ||
edgelist: Optional[dict] = None, | ||
discretized: Optional[bool] = True): | ||
""" | ||
Create a Graph object with specific characteristics | ||
Args: | ||
edgelist: a dictionary of temporal edges in the form of {t: {(u, v), freq}} | ||
discretized: whether the given edgelist was discretized or not | ||
""" | ||
|
||
self.edgelist = edgelist | ||
self.subsampled_graph = None | ||
if discretized: | ||
self.discrite_graph = self._generate_graph() | ||
self.discrite_edgelist = edgelist | ||
else: | ||
self.continuous_edgelist = edgelist | ||
|
||
|
||
def number_of_nodes(self, edgelist: dict = None) -> int: | ||
""" | ||
Calculate total number of nodes present in an edgelist | ||
""" | ||
if self.edgelist is None: | ||
return [] | ||
elif edgelist is None: | ||
edgelist = self.edgelist | ||
node_list = {} | ||
for _, edge_data in edgelist.items(): | ||
for (u,v), _ in edge_data.items(): | ||
if u not in node_list: | ||
node_list[u] = 1 | ||
if v not in node_list: | ||
node_list[v] = 1 | ||
return len(node_list.keys()) | ||
|
||
def nodes(self) -> list: | ||
""" | ||
Return a list of nodes present in an edgelist | ||
""" | ||
node_list = {} | ||
for _, edge_data in self.edgelist.items(): | ||
for (u,v), _ in edge_data.items(): | ||
if u not in node_list: | ||
node_list[u] = 1 | ||
if v not in node_list: | ||
node_list[v] = 1 | ||
|
||
self.node_list = list(node_list.keys()) | ||
return list(node_list.keys()) | ||
|
||
def _generate_graph(self, | ||
edgelist: Optional[dict] = None | ||
) -> list: | ||
''' | ||
Generate a list of graph snapshots. Each snapshot is a | ||
Networkx graph object. | ||
Parameters: | ||
edgelist: a dictionary containing in the form of {t: {(u, v), freq}} | ||
Returns: | ||
G_times: a list of networkx graphs | ||
''' | ||
if self.edgelist is None: | ||
return [] | ||
elif edgelist is None: | ||
edgelist = self.edgelist | ||
G_times = [] | ||
G = nx.Graph() | ||
cur_t = 0 | ||
for ts, edge_data in edgelist.items(): | ||
for (u,v), n in edge_data.items(): | ||
if (ts != cur_t): | ||
G_times.append(G) | ||
G = nx.Graph() | ||
cur_t = ts | ||
G.add_edge(u, v, freq=n) | ||
G_times.append(G) | ||
return G_times | ||
|
||
import networkx as nx | ||
from typing import Optional | ||
|
||
|
||
class Graph(): | ||
def __init__(self, | ||
edgelist: Optional[dict] = None, | ||
discretized: Optional[bool] = True): | ||
""" | ||
Create a Graph object with specific characteristics | ||
Args: | ||
edgelist: a dictionary of temporal edges in the form of {t: {(u, v), freq}} | ||
discretized: whether the given edgelist was discretized or not | ||
""" | ||
|
||
self.edgelist = edgelist | ||
self.subsampled_graph = None | ||
if discretized: | ||
self.discrite_graph = self._generate_graph() | ||
self.discrite_edgelist = edgelist | ||
else: | ||
self.continuous_edgelist = edgelist | ||
|
||
|
||
def number_of_nodes(self, edgelist: dict = None) -> int: | ||
""" | ||
Calculate total number of nodes present in an edgelist | ||
""" | ||
if self.edgelist is None: | ||
return [] | ||
elif edgelist is None: | ||
edgelist = self.edgelist | ||
node_list = {} | ||
for _, edge_data in edgelist.items(): | ||
for (u,v), _ in edge_data.items(): | ||
if u not in node_list: | ||
node_list[u] = 1 | ||
if v not in node_list: | ||
node_list[v] = 1 | ||
return len(node_list.keys()) | ||
|
||
def nodes(self) -> list: | ||
""" | ||
Return a list of nodes present in an edgelist | ||
""" | ||
node_list = {} | ||
for _, edge_data in self.edgelist.items(): | ||
for (u,v), _ in edge_data.items(): | ||
if u not in node_list: | ||
node_list[u] = 1 | ||
if v not in node_list: | ||
node_list[v] = 1 | ||
|
||
self.node_list = list(node_list.keys()) | ||
return list(node_list.keys()) | ||
|
||
def _generate_graph(self, | ||
edgelist: Optional[dict] = None | ||
) -> list: | ||
''' | ||
Generate a list of graph snapshots. Each snapshot is a | ||
Networkx graph object. | ||
Parameters: | ||
edgelist: a dictionary containing in the form of {t: {(u, v), freq}} | ||
Returns: | ||
G_times: a list of networkx graphs | ||
''' | ||
if self.edgelist is None: | ||
return [] | ||
elif edgelist is None: | ||
edgelist = self.edgelist | ||
G_times = [] | ||
G = nx.Graph() | ||
cur_t = 0 | ||
for ts, edge_data in edgelist.items(): | ||
for (u,v), n in edge_data.items(): | ||
if (ts != cur_t): | ||
G_times.append(G) | ||
G = nx.Graph() | ||
cur_t = ts | ||
G.add_edge(u, v, freq=n) | ||
G_times.append(G) | ||
return G_times | ||
|
||
|
Binary file not shown.
Oops, something went wrong.