-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimputer.py
More file actions
156 lines (141 loc) · 5.01 KB
/
imputer.py
File metadata and controls
156 lines (141 loc) · 5.01 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
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
#%%
import os
import torch
import argparse
import importlib
import pandas as pd
import modules
from modules import utility
from modules.utility import *
from modules.evaluation import evaluate
from modules.evaluation_multiple import multiple_evaluate
#%%
import sys
import subprocess
try:
import wandb
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "wandb"])
with open("../wandb_api.txt", "r") as f:
key = f.readlines()
subprocess.run(["wandb", "login"], input=key[0], encoding='utf-8')
import wandb
project = "u-vae" # put your WANDB project name
# entity = "" # put your WANDB username
run = wandb.init(
project=project,
# entity=entity,
tags=["imputation"], # put tags of this python project
)
#%%
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_args(debug):
parser = argparse.ArgumentParser('parameters')
parser.add_argument('--ver', type=int, default=0,
help='model version number')
parser.add_argument("--model", type=str, default="U-VAE")
parser.add_argument('--dataset', type=str, default='whitewine',
help="""
Dataset options:
abalone, anuran, banknote, breast, concrete,
kings, letter, loan, redwine, whitewine
""")
parser.add_argument("--missing_type", default="MAR", type=str,
help="how to generate missing: MCAR, MAR, MNARL, MNARQ")
parser.add_argument("--missing_rate", default=0.3, type=float,
help="missing rate")
parser.add_argument("--latent_dim", default=64, type=int,
help="the latent dimension size")
parser.add_argument('--beta', default=0.1, type=float,
help='scale parameter of asymmetric Laplace distribution')
parser.add_argument("--step", default=0.1, type=float,
help="interval size of quantile levels")
parser.add_argument("--M", default=100, type=int,
help="the number of multiple imputation")
if debug:
return parser.parse_args(args=[])
else:
return parser.parse_args()
#%%
def main():
#%%
config = vars(get_args(debug=False)) # default configuration
"""model load"""
model_name = f"{config['model']}_{config['missing_rate']}_{config['missing_type']}_{config['latent_dim']}_{config['beta']}_{config['step']}_{config['dataset']}"
artifact = wandb.use_artifact(
f"{project}/{model_name}:v{config['ver']}",
type='model')
for key, item in artifact.metadata.items():
config[key] = item
model_dir = artifact.download()
model_name = [x for x in os.listdir(model_dir) if x.endswith(f"{config['seed']}.pth")][0]
config["cuda"] = torch.cuda.is_available()
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
set_random_seed(config["seed"])
wandb.config.update(config)
assert config["missing_type"] != None
#%%
dataset_module = importlib.import_module('datasets.preprocess')
importlib.reload(dataset_module)
CustomDataset = dataset_module.CustomDataset
"""dataset"""
train_dataset = CustomDataset(
config,
train=True)
test_dataset = CustomDataset(
config,
scalers=train_dataset.scalers,
train=False)
#%%
model_module = importlib.import_module("modules.model")
importlib.reload(model_module)
model = getattr(model_module, "UVAE")(
config, train_dataset.EncodedInfo, device
).to(device)
if config["cuda"]:
model.load_state_dict(
torch.load(
model_dir + "/" + model_name
)
)
else:
model.load_state_dict(
torch.load(
model_dir + "/" + model_name,
map_location=torch.device("cpu"),
)
)
model.eval()
#%%
count_parameters = lambda model: sum(p.numel() for p in model.parameters())
num_params = count_parameters(model)
print(f"Number of Parameters: {num_params / 1000:.1f}K")
wandb.log({"Number of Parameters": num_params / 1000})
#%%
"""imputation"""
imputed = model.impute(train_dataset).astype(float)
#%%
results = evaluate(imputed, train_dataset, test_dataset, config, device)
for x, y in results._asdict().items():
print(f"{x}: {y:.3f}")
wandb.log({f"{x}": y})
#%%
results = multiple_evaluate(train_dataset, model, config)
for x, y in results._asdict().items():
print(f"{x}: {y:.3f}")
wandb.log({f"{x}": y})
#%%
wandb.config.update(config, allow_val_change=True)
wandb.run.finish()
#%%
if __name__ == "__main__":
main()
#%%