def test_copy_data_to_tmp_dir(): """Test copy data to temp dir""" tmp_dir_path = cft.make_tmp_dir() copied_file_path1 = pjoin(tmp_dir_path, "custom_feature_defs.py") copied_file_path2 = pjoin(tmp_dir_path, "features_already_known.pkl") feats_known_dict = {"feat1": 0.215, "feat2": 0.311} ts_datafile = pjoin(DATA_PATH, "dotastro_215153.dat") t, m, e = ctt.parse_ts_data(ts_datafile) feats_known_dict['t'] = t feats_known_dict['m'] = m feats_known_dict['e'] = e for fpath in [copied_file_path1, copied_file_path2]: if os.path.exists(fpath): os.remove(fpath) assert(not os.path.exists(copied_file_path1)) cft.copy_data_to_tmp_dir(tmp_dir_path, pjoin(DATA_PATH, "testfeature1.py"), feats_known_dict) assert(os.path.exists(copied_file_path1)) assert(os.path.exists(copied_file_path2)) with open(copied_file_path2, "rb") as f: unpickled_dict = pickle.load(f) npt.assert_equal(unpickled_dict, feats_known_dict) shutil.rmtree(tmp_dir_path, ignore_errors=True)
def test_feature_generation(): """Compare generated features to reference values.""" this_dir = os.path.join(os.path.dirname(__file__)) test_files = [ os.path.join(this_dir, 'data/257141.dat'), os.path.join(this_dir, 'data/245486.dat'), os.path.join(this_dir, 'data/247327.dat'), ] features_extracted = None values_computed = None for i, ts_data_file_path in enumerate(test_files): t, m, e = ctt.parse_ts_data(ts_data_file_path) features = sft.generate_science_features(t, m, e, cfg.features_list_science) sorted_features = sorted(features.items()) if features_extracted is None: features_extracted = [f[0] for f in sorted_features] values_computed = np.zeros((len(test_files), len(features_extracted))) values_computed[i,:] = [f[1] for f in sorted_features] def features_from_csv(filename): with open(filename) as f: feature_names = f.readline().strip().split(",") feature_values = np.loadtxt(f, delimiter=',') return feature_names, feature_values this_dir = os.path.join(os.path.dirname(__file__)) features_expected, values_expected = features_from_csv( os.path.join(this_dir, "data/expected_features.csv")) npt.assert_equal(features_extracted, features_expected) npt.assert_array_almost_equal(values_computed, values_expected)
def test_docker_extract_features(): """Test main Docker extract features method""" script_fpath = pjoin(DATA_PATH, "testfeature1.py") ts_datafile = pjoin(DATA_PATH, "dotastro_215153.dat") t, m, e = ctt.parse_ts_data(ts_datafile) feats_known_dict = {'t': t, 'm': m, 'e': e} results = cft.docker_extract_features(script_fpath, feats_known_dict) assert(isinstance(results, dict)) npt.assert_almost_equal(results["avg_mag"], 10.347417647058824)
def test_extract_feats_in_docker_container(): """Test custom feature extraction in Docker container""" tmp_dir_path = cft.make_tmp_dir() feats_known_dict = {"feat1": 0.215, "feat2": 0.311} ts_datafile = pjoin(DATA_PATH, "dotastro_215153.dat") t, m, e = ctt.parse_ts_data(ts_datafile) feats_known_dict['t'] = t feats_known_dict['m'] = m feats_known_dict['e'] = e cft.copy_data_to_tmp_dir(tmp_dir_path, pjoin(DATA_PATH, "testfeature1.py"), feats_known_dict) results = cft.extract_feats_in_docker_container("test", tmp_dir_path) shutil.rmtree(tmp_dir_path, ignore_errors=True) cft.remove_tmp_files(tmp_dir_path) assert(isinstance(results, dict)) npt.assert_almost_equal(results["avg_mag"], 10.347417647058824)
def test_generate_custom_features(): """Test main generate custom features function""" t, m, e = ctt.parse_ts_data(pjoin(DATA_PATH, "dotastro_215153.dat")) feats = cft.generate_custom_features(pjoin(DATA_PATH, "testfeature1.py"), t, m, e) npt.assert_almost_equal(feats["avg_mag"], 10.347417647058824)