-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdha_example.py
123 lines (112 loc) · 4.33 KB
/
dha_example.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
#
# Copyright (c) 2010-2016, MIT Probabilistic Computing Project
#
# Lead Developers: Dan Lovell and Jay Baxter
# Authors: Dan Lovell, Baxter Eaves, Jay Baxter, Vikash Mansinghka
# Research Leads: Vikash Mansinghka, Patrick Shafto
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import os
import random
#
import numpy
#
import crosscat.utils.data_utils as du
import crosscat.utils.file_utils as fu
import crosscat.LocalEngine as LE
# parse input
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=str)
parser.add_argument('--inf_seed', default=0, type=int)
parser.add_argument('--gen_seed', default=0, type=int)
parser.add_argument('--num_chains', default=25, type=int)
parser.add_argument('--num_transitions', default=200, type=int)
args = parser.parse_args()
#
filename = args.filename
inf_seed = args.inf_seed
gen_seed = args.gen_seed
num_chains = args.num_chains
num_transitions = args.num_transitions
#
rng = random.Random(gen_seed)
get_next_seed = lambda: rng.randint(1, 2**31 - 1)
#
pkl_filename = 'dha_example_num_transitions_%s.pkl.gz' % num_transitions
def determine_Q(M_c, query_names, num_rows, impute_row=None):
name_to_idx = M_c['name_to_idx']
query_col_indices = [name_to_idx[colname] for colname in query_names]
row_idx = num_rows + 1 if impute_row is None else impute_row
Q = [(row_idx, col_idx) for col_idx in query_col_indices]
return Q
def determine_unobserved_Y(num_rows, M_c, condition_tuples):
name_to_idx = M_c['name_to_idx']
row_idx = num_rows + 1
Y = []
for col_name, col_value in condition_tuples:
col_idx = name_to_idx[col_name]
col_code = du.convert_value_to_code(M_c, col_idx, col_value)
y = (row_idx, col_idx, col_code)
Y.append(y)
return Y
# set everything up
T, M_r, M_c = du.read_model_data_from_csv(filename, gen_seed=gen_seed)
num_rows = len(T)
num_cols = len(T[0])
col_names = numpy.array([M_c['idx_to_name'][str(col_idx)] for col_idx in range(num_cols)])
# initialze and transition chains
engine = LE.LocalEngine(inf_seed)
X_L_list, X_D_list = engine.initialize(M_c, M_r, T, get_next_seed(), initialization='from_the_prior', n_chains=num_chains)
X_L_list, X_D_list = engine.analyze(M_c, T, X_L_list, X_D_list, get_next_seed(), n_steps=num_transitions)
# save the progress
to_pickle = dict(X_L_list=X_L_list, X_D_list=X_D_list)
fu.pickle(to_pickle, pkl_filename)
# to_pickle = fu.unpickle(pkl_filename)
# X_L_list = to_pickle['X_L_list']
# X_D_list = to_pickle['X_D_list']
engine = LE.LocalEngine(inf_seed)
# can we recreate a row given some of its values?
query_cols = [2, 6, 9]
query_names = col_names[query_cols]
Q = determine_Q(M_c, query_names, num_rows)
#
condition_cols = [3, 4, 10]
condition_names = col_names[condition_cols]
samples_list = []
for actual_row_idx in [1, 10, 100]:
actual_row_values = T[actual_row_idx]
condition_values = [actual_row_values[condition_col] for condition_col in condition_cols]
condition_tuples = zip(condition_names, condition_values)
Y = determine_unobserved_Y(num_rows, M_c, condition_tuples)
samples = engine.simple_predictive_sample(M_c, X_L_list, X_D_list, Y, Q, 10)
samples_list.append(samples)
round_1 = lambda value: round(value, 2)
# impute some values (as if they were missing)
for impute_row in [10, 20, 30, 40, 50, 60, 70, 80]:
impute_cols = [31, 32, 52, 60, 62]
#
actual_values = [T[impute_row][impute_col] for impute_col in impute_cols]
# conditions are immaterial
Y = []
imputed_list = []
for impute_col in impute_cols:
impute_names = [col_names[impute_col]]
Q = determine_Q(M_c, impute_names, num_rows, impute_row=impute_row)
#
imputed = engine.impute(M_c, X_L_list, X_D_list, Y, Q, get_next_seed(), 1000)
imputed_list.append(imputed)
print
print actual_values
print map(round_1, imputed_list)