def test_group_targets(self): data = pd.read_csv(self.path_data_B) meta = json.load(open(self.path_meta_B)) weight = '_'.join(['weights', self.scheme_name_A3]) # Run weights for scheme_A3 engine_B = WeightEngine(data=data, meta=meta) engine_B.add_scheme(scheme=self.scheme_A3, key='identity', verbose=False) engine_B.run() data_A3 = engine_B.dataframe("scheme_name_A3") # check identical weighted column frequencies df = data_A3.pivot_table(values=[weight], index=['profile_gender'], columns=['age_group'], aggfunc='sum') for column in df.columns.tolist(): self.assertTrue( numpy.allclose(df[column].values, numpy.array([1.645, 1.855]))) # check weighted group frequencies have equal proportions values = data_A3.pivot_table(values=[weight], index=['age_group'], aggfunc='sum').values self.assertTrue(numpy.allclose(values, 3.5))
def test_wdf_structure(self): data = pd.read_csv(self.path_data_exA) engine = WeightEngine(data) targets_gender = [45.6, 54.4] targets_locality = [10, 15, 20, 25, 30] weight_targets = [{ 'gender': {code: prop for code, prop in enumerate(targets_gender, start=1)} }, { 'locality': { code: prop for code, prop in enumerate(targets_locality, start=1) } }] scheme = Rim('complex_filter') scheme.add_group(name='W1, male', filter_def='Wave==1 & religion==1', targets=weight_targets) scheme.add_group(name='W2, female', filter_def='Wave==2 & religion==2', targets=weight_targets) engine.add_scheme(scheme, key='unique_id', verbose=False) engine.run() wdf = engine.dataframe('complex_filter') self.assertEqual( sorted(wdf.columns.tolist()), sorted([ 'unique_id', 'gender', 'locality', 'weights_complex_filter', 'Wave', 'religion' ])) self.assertTrue(len(wdf.index) == 596)