-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_network.py
More file actions
53 lines (38 loc) · 1.79 KB
/
build_network.py
File metadata and controls
53 lines (38 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import pickle
import pandas as pd
import networkx as nx
data = pd.read_csv("cs_proj_enron.csv")
data['Date'] = pd.to_datetime(data['Date'], infer_datetime_format=True)
data['YearMonth'] = data['Date'].dt.to_period('M')
unique_months = data['YearMonth'].unique()
# Create a dictionary to store each monthly network
monthly_networks = {}
# Iterate over each unique month to create networks
for month in unique_months:
# Filter data for the current month
monthly_data = data[data['YearMonth'] == month]
# Create a directed graph
G = nx.MultiGraph()
# Iterate over each row in the filtered data
for _, row in monthly_data.iterrows():
from_email = row['From_copy']
to_email = row['To_copy']
from_position = row['from_position_copy']
to_position = row['to_position']
# Add sender and recipient nodes with email and position information
G.add_node(from_email, position=from_position)
G.add_node(to_email, position=to_position)
# Add a directed edge representing the email, with content and label as attributes
G.add_edge(from_email, to_email, content=row['content'], label=row['labels'])
# Store the graph in the dictionary with the month as the key
monthly_networks[str(month)] = G
# Output a message indicating that the networks have been created
print("Finished creating networks. Each network contains email information for one month.")
if __name__ == "__main__":
os.makedirs("monthly_networks", exist_ok=True)
for month, graph in monthly_networks.items():
file_path = f"monthly_networks/{month}.pkl"
with open(file_path, 'wb') as file:
pickle.dump(graph, file)
print("All networks have been saved as .pkl files.")