-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.py
175 lines (143 loc) · 6.35 KB
/
test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import pickle
import random
from collections import deque
from sys import exit
from typing import Dict, Any, Union
from torch_geometric.utils import degree
import numpy as np
import pandas as pd
import torch
import wandb
from scipy.stats import stats
from sklearn.metrics import roc_auc_score, accuracy_score, f1_score, classification_report, r2_score
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from torch_geometric.data import DataLoader
from xgboost import XGBClassifier, XGBRegressor, XGBModel
from datasets import BrainDataset, HCPDataset, UKBDataset, FlattenCorrsDataset
from main_loop import generate_dataset, create_fold_generator, get_empty_metrics_dict, generate_st_model, training_step
from model import SpatioTemporalModel
from utils import create_name_for_brain_dataset, create_name_for_model, Normalisation, ConnType, ConvStrategy, \
StratifiedGroupKFold, PoolingStrategy, AnalysisType, merge_y_and_others, EncodingStrategy, create_best_encoder_name, \
SweepType, DatasetType, get_freer_gpu, free_gpu_info, create_name_for_flattencorrs_dataset, \
create_name_for_xgbmodel, calculate_indegree_histogram
wandb.init(project='st_extra')
run_cfg: Dict[str, Any] = {
'analysis_type': AnalysisType('st_unimodal'),
'dataset_type': DatasetType('ukb'),
'num_nodes': 68,
'param_conn_type': ConnType('fmri'),
'split_to_test': 2,
'target_var': 'gender',
'time_length': 490,
}
if run_cfg['analysis_type'] in [AnalysisType.ST_UNIMODAL, AnalysisType.ST_MULTIMODAL, AnalysisType.ST_UNIMODAL_AVG, AnalysisType.ST_MULTIMODAL_AVG]:
run_cfg['batch_size'] = 23
run_cfg['device_run'] = f'cuda:{get_freer_gpu()}'
run_cfg['early_stop_steps'] = 33
run_cfg['edge_weights'] = True
run_cfg['model_with_sigmoid'] = True
run_cfg['num_epochs'] = 150
run_cfg['param_activation'] = 'relu'
run_cfg['param_channels_conv'] = 8
run_cfg['param_conv_strategy'] = ConvStrategy('none')
run_cfg['param_dropout'] = 0.1
run_cfg['param_encoding_strategy'] = EncodingStrategy('none')
run_cfg['param_lr'] = 4.2791529866e-06
run_cfg['param_normalisation'] = Normalisation('subject_norm')
run_cfg['param_num_gnn_layers'] = 1
run_cfg['param_pooling'] = PoolingStrategy('concat')
run_cfg['param_threshold'] = 10
run_cfg['param_weight_decay'] = 0.046926
run_cfg['sweep_type'] = SweepType('node_meta')
run_cfg['temporal_embed_size'] = 16
run_cfg['ts_spit_num'] = int(4800 / run_cfg['time_length'])
# Not sure whether this makes a difference with the cuda random issues, but it was in the examples :(
#kwargs_dataloader = {'num_workers': 1, 'pin_memory': True} if run_cfg['device_run'].startswith('cuda') else {}
# Definitions depending on sweep_type
run_cfg['param_gat_heads'] = 0
#if run_cfg['sweep_type'] == SweepType.GAT:
# run_cfg['param_gat_heads'] = config.gat_heads
run_cfg['tcn_depth'] = 3
run_cfg['tcn_kernel'] = 7
run_cfg['tcn_hidden_units'] = 8
run_cfg['tcn_final_transform_layers'] = 1
run_cfg['tcn_norm_strategy'] = 'batchnorm'
run_cfg['nodemodel_aggr'] = 'all'
run_cfg['nodemodel_scalers'] = 'none'
run_cfg['nodemodel_layers'] = 3
run_cfg['final_mlp_layers'] = 1
run_cfg['dp_perc_retaining'] = 0.7
run_cfg['dp_norm'] = 'batchnorm'
N_OUT_SPLITS: int = 5
N_INNER_SPLITS: int = 5
run_cfg['multimodal_size'] = 0
print('Resulting run_cfg:', run_cfg)
dataset = generate_dataset(run_cfg)
skf_outer_generator = create_fold_generator(dataset, run_cfg, N_OUT_SPLITS)
outer_split_num: int = 0
for train_index, test_index in skf_outer_generator:
outer_split_num += 1
# Only run for the specific fold defined in the script arguments.
if outer_split_num != run_cfg['split_to_test']:
continue
X_train_out = dataset[torch.tensor(train_index)]
X_test_out = dataset[torch.tensor(test_index)]
break
skf_inner_generator = create_fold_generator(X_train_out, run_cfg, N_INNER_SPLITS)
overall_metrics: Dict[str, list] = get_empty_metrics_dict(run_cfg)
inner_loop_run: int = 0
for inner_train_index, inner_val_index in skf_inner_generator:
inner_loop_run += 1
X_train_in = X_train_out[torch.tensor(inner_train_index)]
X_val_in = X_train_out[torch.tensor(inner_val_index)]
run_cfg['dataset_indegree'] = calculate_indegree_histogram(X_train_in)
#model: SpatioTemporalModel = generate_st_model(run_cfg)
model = SpatioTemporalModel(run_cfg=run_cfg,
encoding_model=None
).to(run_cfg['device_run'])
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Number of trainable params:", trainable_params)
break
# Number of trainable params: 164597203 (more complex mean, 8 hidden units)
# Number of trainable params: 164618923 (more complex mean, 32 hidden units)
# Number of trainable params: 164795453 (more complex DP, 32 hidden)
#############################################################
############################################################
out_fold_num=run_cfg['split_to_test']
in_fold_num=inner_loop_run
run_cfg=run_cfg
model=model
X_train_in=X_train_in
X_val_in=X_val_in
label_scaler=None
train_in_loader = DataLoader(X_train_in, batch_size=run_cfg['batch_size'], shuffle=True)
val_loader = DataLoader(X_val_in, batch_size=run_cfg['batch_size'], shuffle=False)
for data in train_in_loader:
data = data.to(run_cfg['device_run'])
#print(model(data).shape)
output = model(data)
print(output.shape)
#print(output[0].shape, output[1].shape, output[2].shape)
break
############################
# For ICA exploration
from sklearn.decomposition import FastICA
from torch_geometric.utils import to_dense_batch
import matplotlib.pyplot as plt
n_components = 8
X, batch_mask = to_dense_batch(data.x, data.batch)
ica = FastICA(n_components=n_components, max_iter=1000)
X_transformed = ica.fit_transform(X.permute((0, 2, 1))[20, :].cpu())
fig, axs = plt.subplots(n_components, sharey=True, sharex=True)
for i in range(n_components):
axs[i].plot(X_transformed[:, i])
fig.text(0.5, 0.04, 'Time series', ha='center')
fig.text(0.04, 0.5, 'ICA components', va='center', rotation='vertical')
#plt.tight_layout()
plt.savefig('figures/ica_8_components.pdf', bbox_inches = 'tight', pad_inches = 0)
plt.show()
plt.close()
############################
# For TCN kernels