def test_read_table(self): pre = PrepareData('test', '../../data/demo_data/all') admissions = pre.read_table('admissions') # Check data type of table self.assertEqual(type(admissions), pd.core.frame.DataFrame, 'Read table is not of type Pandas.DataFrame.') # Check if there is data in the table self.assertEqual(admissions['DIAGNOSIS'][2], 'SEPSIS', 'Data not read correctly') # Check if data is loaded correctly and if datetimes are marked correcly in table self.assertEqual( (admissions['DISCHTIME'] - admissions['ADMITTIME'])[3], pd.Timedelta('8 days 01:23:00'), 'Timedates not correctly marked in table.') # Test pickling files: First create empty file, use save-function of read_table, check if stored correctly file_name_pickle = '../../data/demo_data/all/pickle/admissions.p' empty_table = pd.DataFrame() pickle.dump(empty_table, open(file_name_pickle, "wb")) admissions = pre.read_table('admissions', True) loaded_table = pickle.load(open(file_name_pickle, 'rb')) print(loaded_table) self.assertEqual(loaded_table['DIAGNOSIS'][2], 'SEPSIS', 'Data is not correctly pickled.')
def test_load_table_pickle(self): pre_store = PrepareData('test1', '../../data/demo_data/all') pre_store.read_table('admissions', True) pre_load = PrepareData('test2', '../../data/demo_data/all') pre_load.load_table_pickle('admissions') self.assertEqual(pre_load.admissions['DIAGNOSIS'][2], 'SEPSIS', 'Not loaded table from pickle file.')
def test__get_drug_feature(self): pre = PrepareData('test', '../../data/demo_data/all') pre.read_table('prescriptions') drug_enc = DrugEncoderV2(48, None) drug_enc.fit(pre.prescriptions) unique_drugs = drug_enc._get_drug_feature(pre.prescriptions) self.assertEqual( len(unique_drugs), len(set(unique_drugs)), 'Error in DrugEncocder._get_drug_feature(); list elements are not unique;' )
def test_fit(self): pre = PrepareData('test', '../../data/demo_data/all') pre.read_table('prescriptions') drug_enc = DrugEncoderV2() drug_enc.fit(pre.prescriptions) self.assertEqual(drug_enc.drug_mapping['NS500'], int(1), 'Drug mapping not correct.') self.assertEqual(drug_enc.drug_mapping['MORPH100'], int(797), 'Drug mapping not correct.') # Test Encoder Parameters pre = PrepareData('test', '../../data/demo_data/all') pre.read_table('prescriptions') drug_enc = DrugEncoderV2(48, 13) drug_enc.fit(pre.prescriptions) self.assertEqual(len(drug_enc.drug_mapping), int(13), 'max_number_of_drug_features does not work.')