-
Notifications
You must be signed in to change notification settings - Fork 27
/
export_data.py
59 lines (45 loc) · 1.46 KB
/
export_data.py
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
54
55
56
57
58
59
def export_net_json(net, net_type, indent='no-indent'):
''' export json string of dat '''
import json
from copy import deepcopy
if net_type == 'dat':
exp_dict = deepcopy(net.dat)
if type(exp_dict['mat']) is not list:
exp_dict['mat'] = exp_dict['mat'].tolist()
if 'mat_orig' in exp_dict:
exp_dict['mat_orig'] = exp_dict['mat_orig'].tolist()
elif net_type == 'viz':
exp_dict = net.viz
elif net_type == 'sim_row':
exp_dict = net.sim['row']
elif net_type == 'sim_col':
exp_dict = net.sim['col']
# make json
if indent == 'indent':
exp_json = json.dumps(exp_dict, indent=2)
else:
exp_json = json.dumps(exp_dict)
return exp_json
def write_matrix_to_tsv(net, filename=None, df=None):
'''
This will export the matrix in net.dat or a dataframe (optional df in
arguments) as a tsv file. Row/column categories will be saved as tuples in
tsv, which can be read back into the network object.
'''
import pandas as pd
if df is None:
df = net.dat_to_df()
return df['mat'].to_csv(filename, sep='\t')
def write_json_to_file(net, net_type, filename, indent='no-indent'):
exp_json = net.export_net_json(net_type, indent)
fw = open(filename, 'w')
fw.write(exp_json)
fw.close()
def save_dict_to_json(inst_dict, filename, indent='no-indent'):
import json
fw = open(filename, 'w')
if indent == 'indent':
fw.write(json.dumps(inst_dict, indent=2))
else:
fw.write(json.dumps(inst_dict))
fw.close()