3 from trial import Trial
7 def __init__(self, filename):
8 self.filename = filename
11 def export_data(self, trial_id, db):
12 trial_setting = db.get_trial_setting(trial_id)
13 trial = Trial(trial_setting)
14 subjects = db.get_subjects(trial_id)
16 if len(subjects) > 30:
17 ss = len(subjects) + 10
18 ui_pool = list(range(ss))
19 for subject in subjects:
20 if subject[2] in ui_pool:
21 ui_pool.remove(subject[2])
22 while len(ui_pool) > (ss - len(subjects)):
24 treatments = db.read_treatments(trial_id)
26 for treatment in treatments:
27 group = {'name': treatment[1], 'allocation_ratio': treatment[2]}
29 factors = db.read_factors(trial_id)
31 for factor in factors:
32 factor_levels = db.factor_levels(factor[0])
33 titles = [factor_level[1] for factor_level in factor_levels]
34 factor.append([factor_level[0] for factor_level in factor_levels])
35 levels = ','.join(titles)
36 variable = {'name': factor[1], 'weight': factor[2], 'levels': levels}
37 variables.append(variable)
39 for subject in subjects:
40 allocation = {'UI': subject[2]}
41 for treatment_index, treatment in enumerate(treatments):
42 if treatment[0] == subject[1]:
43 allocation['allocation'] = treatment_index
45 subject_levels = db.get_subject_levels(subject[0])
47 for subject_level in subject_levels:
48 levels.append(self.get_factor_level_index(factors, subject_level))
49 allocation['levels'] = levels
50 allocations.append(allocation)
51 if not db.has_preload(trial_id):
52 initial_freq_table = 0
54 preload = db.get_preload(trial_id)
55 initial_freq_table = [[[preload[(t[0], factor[0], level_id)] for level_id in factor[-1]] for factor in factors] for t in treatments]
56 self.data = {'trial_title': trial.title,
57 'trial_description': trial.title,
58 'trial_properties': [],
59 'high_prob': trial.base_prob,
60 'prob_method': trial.prob_method,
61 'distance_measure': trial.dist_method,
65 'variables': variables,
66 'allocations': allocations,
67 'initial_freq_table': initial_freq_table}
68 fp = open(self.filename, 'wb')
69 pickle.dump(self.data, fp, protocol=2)
74 def get_factor_level_index(self, factors, subject_level):
75 for factor_index, factor in enumerate(factors):
76 if factor[0] == subject_level[0]:
77 for factor_level_index, factor_level in enumerate(factor[-1]):
78 if factor_level == subject_level[1]:
79 return factor_level_index
81 def data_file_valid(self):
83 fp = open(self.filename, 'rb')
84 self.data = pickle.load(fp)
85 self.trial_title = self.data['trial_title']
86 self.allocations = self.data['allocations']
87 self.high_prob = self.data['high_prob']
88 self.initial_freq_table = self.data['initial_freq_table']
89 self.prob_method = self.data['prob_method']
90 self.distance_measure = self.data['distance_measure']
91 self.groups = self.data['groups']
92 self.variables = self.data['variables']
99 det.append('trial_title: {}'.format(self.trial_title))
101 for group in self.groups:
102 treatments.append(group['name'])
103 det.append('Treatments: ({})'.format(', '.join(treatments)))
105 for variable in self.variables:
106 factors.append('{}({})'.format(variable['name'], variable['levels']))
107 det.append('Factors: [{}]'.format(', '.join(factors)))
108 det.append('{} Subjects'.format(len(self.allocations)))
109 if self.initial_freq_table:
110 det.append('Trial has preload')
111 return '\n'.join(det)
113 def import_data(self, db):
114 trial_id = db.insert_trial(self.trial_title)
116 for group in self.groups:
117 treatment_id = db.insert_treatment(trial_id, group['name'])
118 treatments.append(treatment_id)
120 for variable in self.variables:
121 factor_id = db.insert_factor(trial_id, variable['name'])
124 levels = variable['levels'].split(',')
126 level_id = db.insert_level(trial_id, factor_id, level)
127 level_ids.append(level_id)
128 factor.append(level_ids)
129 factors.append(factor)
131 for allocation in self.allocations:
132 if str(allocation['UI']).isnumeric():
137 for allocation in self.allocations:
138 treatment_index = allocation['allocation']
139 treatment_id = treatments[treatment_index]
140 identifier_value = allocation['UI'] if all_numeric else id_value
143 subject_id = db.insert_subject(identifier_value, treatment_id, trial_id)
144 levels = allocation['levels']
146 for factor_index, level_index in enumerate(levels):
147 factor_id = factors[factor_index][0]
148 level_id = factors[factor_index][1][level_index]
149 subject_levels.append((factor_id, level_id))
150 db.insert_subject_levels(trial_id, subject_id, subject_levels)
151 if not self.initial_freq_table:
154 for treatment_index, row in enumerate(self.initial_freq_table):
155 t = treatments[treatment_index]
156 for factor_index, factor in enumerate(row):
157 f = factors[factor_index][0]
158 for level_index, count in enumerate(factor):
159 lv = factors[factor_index][1][level_index]
160 preload[(t, f, lv)] = count
161 db.save_preload(trial_id, preload)