def main(): start = time.time() args = _clargs() dbParams = {"dbPath": args.dbPath, "timeout": DB_TIMEOUT} conn = connFromParams(dbParams) cursor = conn.cursor() _SELECT_SQL = "SELECT * FROM %s WHERE id=\"%s\"" % (TABLE_NAME, args.id) cursor.execute(_SELECT_SQL) try: row = next(cursor) except StopIteration: print("Found no LCs!") return conn.close() times, mag, err = deserLc(*row[2:]) times = times[START_SLICE:END_SLICE] mag = mag[START_SLICE:END_SLICE] err = err[START_SLICE:END_SLICE] i = 0 skipped = list() # fts = registered_extractors() fts = featuresByData(STANDARD_INPUT_DATA_TYPES) if args.feature: # Option to test specific feature only assert args.feature in fts fts = [args.feature] for i, featureName in enumerate(fts): fs = FeatureSpace(data=STANDARD_INPUT_DATA_TYPES, only=[featureName]) try: fts, values = fs.extract(times, mag, err) except BaseException as e: traceback.print_exc() print("failed for feature: %s with exception: %s" % (featureName, e)) break if len(fts) and len(values): msg = "OK" if np.all(np.isnan(values)) else "NOT FINITE!" print("%s %s: %s" % (msg, fts[0], values[0])) if len(values) != 1: print("lengths: features: %s values: %s" % (len(fts), len(values))) else: skipped.append(featureName) time.sleep(0.2) print("total %s skipped: %s" % (i, len(skipped))) print("skipped: %s" % skipped) print("elapsed: %s" % timedelta(seconds=time.time() - start))
def test_with_optional_data_call(mock_extractors_register): @register_extractor class A(Extractor): data = ["magnitude", "time"] optional = ["magnitude"] features = ["time_arg", "magnitude_arg"] def fit(self, time, magnitude): return {"time_arg": time, "magnitude_arg": magnitude} time, magnitude = [1, 2, 3], [4, 5, 6] fs = FeatureSpace(data=["time"]) result = fs.extract(time=time, magnitude=magnitude) np.testing.assert_array_equal(result["time_arg"], time) np.testing.assert_array_equal(result["magnitude_arg"], magnitude) result = fs.extract(time=time) np.testing.assert_array_equal(result["time_arg"], time) np.testing.assert_array_equal(result["magnitude_arg"], None)
def test_features_order(mock_extractors_register): @register_extractor class ReturnSame(Extractor): data = ["magnitude"] features = ["Same"] def fit(self, magnitude): return {"Same": magnitude[0]} space = FeatureSpace(only=["Same"]) for _ in range(200): data = np.unique(np.random.randint(1, 1000, 10)) np.random.shuffle(data) features, values_col = space.extract(magnitude=data) np.testing.assert_array_equal(data[0], values_col)
def test_lscargle_vs_feets(): # extract the module for make short code ext_lomb_scargle = extractors.ext_lomb_scargle # load the data path = os.path.join(DATA_PATH, "bad_results.pandas.pkl") tseries = pd.read_pickle(path) # the ls params ext_params = ext_lomb_scargle.LombScargle.get_default_params() lscargle_kwds = ext_params["lscargle_kwds"] # create the feature space fs = FeatureSpace(only=["PeriodLS"]) ls_periods, feets_periods = [], [] for src_id in tseries.bm_src_id.unique(): # extract the timeseries sobs = tseries[tseries.bm_src_id == src_id] time = sobs.pwp_stack_src_hjd.values magnitude = sobs.pwp_stack_src_mag3.values error = sobs.pwp_stack_src_mag_err3.values # "pure" lomb scargle (without the entire feets pipeline) frequency, power = ext_lomb_scargle.lscargle(time=time, magnitude=magnitude, error=error, **lscargle_kwds) fmax = np.argmax(power) ls_periods.append(1 / frequency[fmax]) # extract the period from the feets pipele rs = fs.extract(time=time, magnitude=magnitude, error=error) feets_periods.append(rs.values["PeriodLS"]) feets_periods = np.array(feets_periods).flatten() np.testing.assert_array_equal(ls_periods, feets_periods)
def test_F2f_extract_one_same_values(aligned_MACHO_by_FATS, FATS_results): lc = ( aligned_MACHO_by_FATS.time, aligned_MACHO_by_FATS.mag, aligned_MACHO_by_FATS.error, aligned_MACHO_by_FATS.mag2, aligned_MACHO_by_FATS.aligned_time, aligned_MACHO_by_FATS.aligned_mag, aligned_MACHO_by_FATS.aligned_mag2, aligned_MACHO_by_FATS.aligned_error, aligned_MACHO_by_FATS.aligned_error2, ) features, FATS_values = FATS_results.features, FATS_results.fvalues fs = FeatureSpace(SlottedA_length={"T": None}, StetsonKAC={"T": None}) result = fs.extract(*lc) feets_result = dict(zip(*result)) feets_result.update({ "PeriodLS": feets_result.pop("PeriodLS_0"), "Period_fit": feets_result.pop("Period_fit_0"), "Psi_eta": feets_result.pop("Psi_eta_0"), "Psi_CS": feets_result.pop("Psi_CS_0"), }) for feature in features: if feature not in feets_result: pytest.fail("Missing feature {}".format(feature)) # some features changes the values explicity and must # not be evaluates if "_harmonics_" in feature: continue feets_value = feets_result[feature] FATS_value = FATS_values[feature] params = get_feature_assert_params(feature) np.testing.assert_allclose(feets_value, FATS_value, **params)
def test_extract(): space = FeatureSpace(only=["Amplitude"]) magnitude = np.array( [ 0.46057565, 0.51372940, 0.70136533, 0.21454228, 0.54792300, 0.33433717, 0.44879870, 0.55571062, 0.24388037, 0.44793366, 0.30175873, 0.88326381, 0.12208977, 0.37088649, 0.59457310, 0.74705894, 0.24551664, 0.36009236, 0.80661981, 0.04961063, 0.87747311, 0.97388975, 0.95775496, 0.34195989, 0.54201036, 0.87854618, 0.07388174, 0.21543205, 0.59295337, 0.56771493, ] ) features, values = space.extract(magnitude=magnitude) assert len(features) == 1 and features[0] == "Amplitude" np.testing.assert_allclose(values[features == "Amplitude"], 0.45203809)
def tutorial(): plot = 0 lc = datasets.load_MACHO_example() if plot: plotLc(lc) # 69 features with ALL_DATA_TYPES in 6.15s # 64 features with time, magnitude, error in 6.14s # 58 features with time, magnitude in 3.3s # 22 features with magnitude in 0.02s basicData = ["time", "magnitude", "error"] basicData = ["time", "magnitude"] basicData = ["magnitude"] start = time.time() # remove points beyond 5 stds of the mean tm, mag, error = preprocess.remove_noise(**lc.bands.B) tm2, mag2, error2 = preprocess.remove_noise(**lc.bands.R) # N.B. skip for LSST work aTime, aMag, aMag2, aError, aError2 = preprocess.align(tm, tm2, mag, mag2, error, error2) lc = [tm, mag, error, mag2, aTime, aMag, aMag2, aError, aError2] # only calculate these features # fs = feets.FeatureSpace(only=['Std', 'StetsonL']) fs = FeatureSpace() # fs = FeatureSpace(data=basicData) features, values = fs.extract(*lc) elapsed = time.time() - start print("Computed %s features in %.02fs" % (len(features), elapsed)) if plot: g = plt.figure(2) plt.plot(lc[0], lc[1], "*-", alpha=0.6) plt.xlabel("Time") plt.ylabel("Magnitude") plt.gca().invert_yaxis() g.show() input() t = PrettyTable(["Feature", "Value"]) t.align = "l" for i, feat in enumerate(features): t.add_row([feat, values[i]]) if plot: print(t) fdict = dict(zip(features, values)) # Ploting the example lightcurve in phase T = 2 * fdict["PeriodLS"] new_b = np.mod(lc[0], T) / T idx = np.argsort(2 * new_b) plt.plot(new_b, lc[1], '*') plt.xlabel("Phase") plt.ylabel("Magnitude") plt.gca().invert_yaxis() plt.show()