-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (21 loc) · 1.03 KB
/
utils.py
File metadata and controls
28 lines (21 loc) · 1.03 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
import numpy as np
from scipy import sparse
def text_to_dict(path):
""" Read in a text file as a dictionary where keys are text and values are indices """
keys = np.loadtxt(path, dtype=str)
indices = np.arange(len(keys))
return dict(zip(keys, indices))
def get_anc_and_desc_dict(all_phe_hpo_anc, phe_plus_dict):
phe_anc_mat = sparse.csr_matrix(all_phe_hpo_anc)
phe_desc_mat = sparse.csc_matrix(all_phe_hpo_anc)
phe_anc_dict = {}
phe_parent_dict = {}
for phe, phe_idx in phe_plus_dict.items():
phe_anc_dict[phe] = list(phe_anc_mat[phe_idx].indices)
phe_parent_dict[phe] = list(phe_anc_mat[phe_idx].indices[np.where(phe_anc_mat[phe_idx].data == 1)])
phe_desc_dict = {}
phe_child_dict = {}
for phe, phe_idx in phe_plus_dict.items():
phe_desc_dict[phe] = list(phe_desc_mat[:, phe_idx].indices)
phe_child_dict[phe] = list(phe_desc_mat[:, phe_idx].indices[np.where(phe_desc_mat[:, phe_idx].data == 1)])
return phe_anc_dict, phe_parent_dict, phe_desc_dict, phe_child_dict