-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_branches.py
86 lines (59 loc) · 9.38 KB
/
test_branches.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from sem_model import SEMData, SEMModel
from sem_opt_phylo import SEMModelNode, SEMTree, SEMOptPhylo
from sem_opt_branch import SEMOptBranch
from sem_inspector_phylo import inspect
import numpy as np
path_model = 'blood/'
path_data = 'blood/'
path_tree = 'blood/'
path_res = path_data + 'res/'
file_model = 'mod_blood01.txt'
files_data_norm = ['CD4.txt', 'CD8.txt', 'MON.txt', 'NEU.txt',
'MRF.txt', 'DEN.txt', 'NKL.txt', 'B19.txt']
files_data_canc = ['L_B.txt', 'LT4.txt', 'LT8.txt']
files_data = files_data_norm + files_data_canc
file_tree = 'tree_544samples_4param_2.nwk'
mod_leaf = SEMModel(path_model + file_model)
mod_node = SEMModelNode(path_model + file_model)
dataset_norm = [SEMData(mod_leaf, path_data + f) for f in files_data_norm]
dataset_canc = [SEMData(mod_leaf, path_data + f) for f in files_data_canc]
datasets = dataset_norm + dataset_canc
mod_node.load_initial_dataset(dataset_norm[0])
tree = SEMTree(dataset_norm, path_tree + file_tree)
opt_phylo = SEMOptPhylo(mod_node, dataset_norm, tree)
opt_phylo.param_val = [0.203421568627451,0.288000000000000,0.276676470588235,0.0974215686274512,0.217254901960784,0.0826176470588237,0.0353921568627451,0.228156862745098,0.245843137254902,0.510803921568628,-0.263921568627451,0.269470588235294,0.384029411764706,0.0358137254901961,0.210166666666667,-0.157637254901961,0.200588235294118,0.0608137254901961,0.0607450980392157,0.113352941176471,0.0853627450980393,0.473705882352941,0.543186274509804,0.0409803921568627,0.123205882352941,0.325088235294118,0.157450980392157,1.28417647058824,0.233303921568627,0.335264705882353,0.297921568627451,-0.235519607843137,-0.204764705882353,-0.177029411764706,0.260509803921569,-0.196558823529412,-0.300911764705882,-0.397607843137255,-0.143549019607843,-0.148578431372549,0.468421568627451,-0.169058823529412,-0.210137254901961,-0.0298627450980392,-0.0925588235294118,-0.713303921568628,-0.450215686274510,-0.111480392156863,-0.162843137254902,-0.0644607843137255,0.0192647058823529,0.300715686274510,0.163352941176471,0.193333333333333,0.0149411764705882,0.205411764705882,0.233970588235294,-1.42961764705882,-0.292794117647059,0.267921568627451,0.0750882352941176,-0.139039215686274,0.181156862745098,-0.532686274509804,-0.220617647058824,-0.240901960784314,-0.171019607843137,-0.331558823529412,-0.303009803921569,-0.374294117647059,-0.260960784313726,-0.271794117647059,-0.128911764705882,-0.180862745098039,-0.335019607843137,-0.278107843137255,-0.257803921568628,0.145352941176471,0.00251960784313725,0.0815196078431373,0.0948431372549019,0.0270196078431373,0.177774509803922,0.0598823529411765,-0.0303333333333333,-0.230156862745098,0.00196078431372549,0.178941176470588,-0.617215686274510,-0.213245098039216,-0.141509803921569,-0.141068627450980,-0.136892156862745,-0.128941176470588,0.118843137254902,-0.150803921568627,0.0250098039215686,-0.233460784313725,-0.138686274509804,-0.370715686274510,0.0567450980392157,0.464617647058823,0.225598039215686,0.119558823529412,0.143833333333333,-0.0440098039215686,0.514666666666666,0.157098039215686,0.308509803921569,0.00491176470588235,0.135274509803922,0.170941176470588,0.234666666666667,0.463421568627451,0.421901960784314,0.661382352941177,0.662274509803922,0.652803921568628,0.636343137254902,-0.0901666666666666,-0.0928529411764705,-0.0973921568627452,-0.0781372549019607,0.703225490196078,0.701529411764706,0.689794117647058,0.665421568627452,0.156421568627451,0.162098039215686,0.185470588235294,0.136431372549020,-0.167186274509804,-0.154343137254902,-0.150019607843137,-0.0901862745098040,-0.0255784313725490,-0.0261666666666667,-0.0447745098039216,-0.0212843137254902,0.0175196078431372,0.0123235294117647,0.0197156862745098,0.0222058823529412,-0.0348921568627451,-0.0318333333333333,-0.0348333333333333,-0.0316666666666667,0.0145588235294118,0.0336764705882353,0.0388921568627451,0.0162352941176470,0.150696078431372,0.158833333333333,0.155039215686275,0.150500000000000,-0.132637254901961,-0.142921568627451,-0.142519607843137,-0.117529411764706,-0.0167254901960784,-0.0210686274509804,-0.0252352941176470,-0.0130588235294118,0.0285784313725490,0.0294607843137255,0.0348529411764706,0.0144313725490196,0.0299215686274510,0.0391078431372549,0.0461372549019608,0.0279607843137255,-0.00735294117647059,-0.000578431372549019,-0.00305882352941177,-0.00387254901960785,0.00731372549019608,0.00344117647058824,0.00739215686274510,0.00459803921568628,-0.000970588235294118,-0.00379411764705883,-0.00437254901960785,-0.000352941176470589,1.18966666666667,1.19693137254902,1.16466666666667,1.13880392156863,0.166676470588235,0.172666666666666,0.201264705882353,0.145656862745098,-0.276264705882353,-0.258774509803921,-0.239078431372549,-0.165588235294118,-0.0360196078431373,-0.0344411764705882,-0.0573627450980392,-0.0259803921568627,0.0230098039215686,0.0158137254901961,0.0272450980392157,0.0283627450980392,-0.0395392156862745,-0.0372843137254902,-0.0428431372549019,-0.0390784313725490,0.0157058823529412,0.0389803921568628,0.0440294117647058,0.0188431372549019,0.243156862745098,0.245460784313725,0.261794117647059,0.240735294117647,-0.0448039215686274,-0.0574019607843138,-0.0705686274509804,-0.0139901960784314,0.0267941176470588,0.0169117647058824,-0.00165686274509805,0.0382156862745098,0.0187254901960784,0.0151372549019608,0.0174411764705882,0.0177450980392157,-0.0404313725490196,-0.0386666666666667,-0.0424901960784314,-0.0392647058823529,0.0275980392156863,0.0401078431372549,0.0444705882352941,0.0327549019607843,0.347401960784314,0.351607843137255,0.334872549019608,0.324029411764706,0.00744117647058824,0.00608823529411765,0.0135000000000000,0.00464705882352942,0.0249019607843137,0.0300882352941176,0.0272647058823529,0.0321666666666667,0.0151960784313725,0.0133627450980392,0.0162745098039215,0.00853921568627452,-0.00423529411764706,-0.0112156862745098,-0.0145980392156863,-0.00712745098039216,0.285970588235294,0.297107843137255,0.296029411764706,0.281039215686275,-0.000519607843137254,0.00485294117647059,-0.00138235294117647,0.00294117647058824,-0.00395098039215687,-0.00313725490196079,0.00619607843137255,-0.00451960784313726,0.00540196078431373,0.00658823529411765,0.00215686274509804,0.00805882352941177,0.118617647058824,0.119166666666667,0.116990196078431,0.123882352941176,-0.0732549019607845,-0.0692058823529411,-0.0606764705882353,-0.0748921568627452,0.00957843137254903,0.0132254901960784,0.00737254901960785,0.00377450980392157,0.226392156862745,0.225107843137255,0.223990196078431,0.224970588235294,-0.00385294117647059,-0.00885294117647060,-0.00696078431372550,-0.00835294117647059,0.0870098039215685,0.0958823529411764,0.0907352941176471,0.0883627450980393,0.00132352941176471,0.00254901960784314,0.00329411764705883,0.00200000000000000,0.00411764705882353,0.00300000000000000,0.00300000000000000,0.00209803921568628,0.00177450980392157,0.00282352941176471,0.00100000000000000,0.00460784313725491,0.00135294117647059,0.00323529411764706,0.00128431372549020,0.00425490196078432,0.00100000000000000,0.00333333333333334,0.00160784313725490,0.00100000000000000,0.00100000000000000,0.00100000000000000,0.00209803921568628,0.00100000000000000,0.00192156862745098,0.00200000000000000,0.00487254901960785,0.00185294117647059,0.00503921568627451,0.00252941176470588,0.00200000000000000,0.00200000000000000,0.00180392156862745,0.00219607843137255,0.00200000000000000,0.00100000000000000,0.00100980392156863,0.00169607843137255,0.00144117647058824,0.00200000000000000,0.00100000000000000,0.00100000000000000,0.00205882352941177,0.00268627450980392,0.00100000000000000,0.00100000000000000,0.00264705882352941,0.000509803921568628,0.00230392156862745,0.00214705882352941,0.00100000000000000,0.00100000000000000,0.00100000000000000,0.00180392156862745]
tests = [(['N1', 'B19', 'L_B'], ['node', 'leaf', 'leaf']),
(['N0', 'N1', 'L_B'], ['node', 'node', 'leaf'])]
for node_names, node_attrs in tests:
datasets_branch = [data for data in datasets if data.name in node_names]
opt_branch = SEMOptBranch(mod_node, opt_phylo, datasets_branch, node_names, node_attrs)
# [(p.node_name, p.mx_type, p.id_opt, p.id_mod, opt_branch.param_val[p.id_opt])
# for p in opt_branch.param_pos]
opt_branch.optimise()
opt_branch.param_val = opt_branch.param_chain[-1]
for node_name in opt_phylo.tree.keys():
with open(path_res + file_model[:-4] + 'node_' + node_name + '_2.txt', 'w') as f:
inspect(mod_node, opt_phylo, node_name, f)
np.savetxt(path_res + 'chain' + '_' + node_names[0]+'_'+node_names[1]+'_'+ \
node_names[2] + '_'+ str(opt_branch.log_post_branch(
opt_branch.param_val)) +\
'.txt',
opt_branch.param_chain,
'%.3f')
#
# with open(path_res + file_model[:-4] + '_all_nodes' + '.txt', 'w') as f:
# for node_name in opt_phylo.tree.keys():
# params_node = opt_phylo.get_node_params(opt_phylo.param_val, node_name)
# params_beta = [params_node[i] for i, k in mod_node.param_pos.items()
# if k[0] == 'Beta']
# print(node_name, params_beta, file=f)
# pos = opt_branch.param_pos[208]
# params_opt = opt_branch.param_val
# constraint_func = opt_branch.constraint_branch
# mx_type = 'Star'
# node_name = 'Branch'
# from scipy.stats import invwishart, invgamma, wishart, norm, uniform, multivariate_normal
# log_prob = opt_branch.log_post_branch
#
[(p.node_name, p.mx_type, p.id_opt, p.id_mod, opt_phylo.param_val[p.id_opt])
for p in opt_phylo.param_pos]