def calc_source_features_obs( source_x, source_y, source_zd, source_az, pointing_position_zd, pointing_position_az, obstime, ): result = calc_source_features_common( source_x, source_y, source_zd, source_az, pointing_position_zd, pointing_position_az, ) result['ra_prediction'], result['dec_prediction'] = camera_to_equatorial( source_x, source_y, pointing_position_zd, pointing_position_az, obstime, ) return result
def test_there_and_back_again(): from fact.coordinates import camera_to_equatorial, equatorial_to_camera df = pd.DataFrame({ 'az_tracking': [0, 90, 270], 'zd_tracking': [0, 10, 20], 'timestamp': pd.date_range('2017-10-01 22:00Z', periods=3, freq='10min'), 'x': [-100, 0, 150], 'y': [0, 100, 0], }) ra, dec = camera_to_equatorial( df.x, df.y, df.zd_tracking, df.az_tracking, df.timestamp, ) x, y = equatorial_to_camera(ra, dec, df.zd_tracking, df.az_tracking, df.timestamp) assert np.allclose(x, df.x) assert np.allclose(y, df.y)
def sourcename(source_x, source_y, pointing_zd, pointing_az, timestamp): sourceList = camera_to_equatorial( source_x, source_y, pointing_zd, pointing_az, timestamp, ) source = SkyCoord(15 * sourceList[0], sourceList[1], unit="deg") df = pd.read_csv('fact_sources.csv') names = list(df.fSourceName) catalog = SkyCoord( ra=df.fRightAscension.values * u.hourangle, dec=df.fDeclination.values * u.deg, ) idx, sep, dst = source.match_to_catalog_sky(catalog) return names[idx]
def test_transforms(): from fact.coordinates import camera_to_equatorial df = pd.DataFrame({ 'az_tracking': [0, 90, 270], 'zd_tracking': [0, 10, 20], 'timestamp': pd.date_range('2017-10-01 22:00Z', periods=3, freq='10min'), 'x': [-100, 0, 150], 'y': [0, 100, 0], }) df['ra'], df['dec'] = camera_to_equatorial( df.x, df.y, df.zd_tracking, df.az_tracking, df.timestamp, )
def analysis(file_path): #Load paths config = KlaasConfig.from_yaml(configuration_path) separator_model = joblib.load(separator_model_path) energy_model = joblib.load(energy_model_path) disp_model = joblib.load(disp_model_path) sign_model = joblib.load(sign_model_path) t = Table.read(file_path) dd = t.to_pandas() #Create PandasDataframe #Apply Qualitycuts with open(quality_cuts_path) as f: quality_cuts = yaml.safe_load(f) quality_cuts_selection = quality_cuts.get('selection', {}) quality_cuts_mask = np.ones(len(dd), dtype=bool) for name, (operator, value) in quality_cuts_selection.items(): ncut = OPERATORS[operator](dd[name], value) quality_cuts_mask = np.logical_and(quality_cuts_mask, ncut) df = dd.loc[quality_cuts_mask] df.index = pd.RangeIndex(len(df.index)) columns = set(needed_columns) for model in ('separator', 'energy', 'disp'): model_config = getattr(config, model) columns.update(model_config.columns_to_read_apply) try: sources = df['source'].unique() source = SkyCoord.from_name(sources[0]) columns.update(['timestamp', 'night']) except (KeyError, OSError) as e: source = None #Gamaness prediction df_sep = feature_generation(df, config.separator.feature_generation) df['gamma_prediction'] = predict_separator( df_sep[config.separator.features], separator_model, ) #DISP prediction df_disp = feature_generation(df, config.disp.feature_generation) disp = predict_disp(df_disp[config.disp.features], disp_model, sign_model) #Energy prediction df_energy = feature_generation(df, config.energy.feature_generation) df['gamma_energy_prediction'] = predict_energy( df_energy[config.energy.features], energy_model, ) #calculate disp_prediction source_x = df.cog_x + disp * np.cos(df.delta) source_y = df.cog_y + disp * np.sin(df.delta) df['source_x_prediction'] = source_x df['source_y_prediction'] = source_y df['disp_prediction'] = disp #decode timestamp df['timestamp'] = pd.to_datetime(df['timestamp'].str.decode('ascii')) #Parallelized Computation if source: source_altaz = concat_results_altaz( parallelize_array_computation( partial(to_altaz, source=source), df['timestamp'], )) result = parallelize_array_computation( calc_source_features_obs, source_x, source_y, source_altaz.zen.deg, source_altaz.az.deg, df['pointing_position_zd'].values, df['pointing_position_az'].values, df['timestamp'], ) else: result = parallelize_array_computation( calc_source_features_sim, source_x, source_y, df['source_position_zd'].values, df['source_position_az'].values, df['pointing_position_zd'].values, df['pointing_position_az'].values, df['cog_x'].values, df['cog_y'].values, df['delta'].values, ) #Merge results together for k in result[0].keys(): df[k] = np.concatenate([r[k] for r in result]) #RA and Dec prediction df['ra_prediction'], df['dec_prediction'] = camera_to_equatorial( df['source_x_prediction'], df['source_y_prediction'], df['pointing_position_zd'], df['pointing_position_az'], df['timestamp'], ) #Source from Exel Table df['source'] = sourcename(df.source_position_x[0], df.source_position_y[0], df.pointing_position_zd[0], df.pointing_position_az[0], df.timestamp[0]) timefrom = df.timestamp[0] timeto = df.timestamp[len(df.timestamp) - 1] timebin = (timeto - timefrom).total_seconds() / 3600.0 #Auswahl der Events mask_gamma = df['gamma_prediction'] >= gammalimit mask_theta = df['theta_deg'] <= thetalimit for i in range(1, 6): mask_theta |= df[f'theta_deg_off_{i}'] <= thetalimit selected = df.loc[mask_gamma & mask_theta, db_features] #CalculateExess num_theta = selected['theta_deg'] <= thetalimitexcess for i in range(1, 6): num_theta_off = selected[f'theta_deg_off_{i}'] <= thetalimitexcess on = len(selected.loc[num_theta, db_features]) off = len(selected.loc[num_theta_off, db_features]) rate = (on - off * 0.2) / (timebin) excess = pd.DataFrame({ 'source': df.source[0], 'night': df.night[0], 'run_id': df.run_id[0], 'rate': [rate], 'timeto': [timeto], 'timefrom': [timefrom], 'n_on': on, 'n_off': off }) sql(selected, excess)