1 | # coding: utf-8
|
---|
2 | from collections import namedtuple
|
---|
3 | from sqlalchemy import create_engine
|
---|
4 | import pandas as pd
|
---|
5 | import numpy as np
|
---|
6 | from astropy.io import fits
|
---|
7 | import fact
|
---|
8 |
|
---|
9 | night_int = 20150721
|
---|
10 |
|
---|
11 | def create_DB_connection():
|
---|
12 | from ConfigParser import SafeConfigParser
|
---|
13 | config = SafeConfigParser()
|
---|
14 | config.optionxform = str # this make the parsing case sensitive
|
---|
15 | config.read('config.ini')
|
---|
16 |
|
---|
17 | factdb = create_engine(
|
---|
18 | "mysql+mysqldb://{user}:{pw}@{host}/{db}".format(
|
---|
19 | user=config.get('database', 'user'),
|
---|
20 | pw=config.get('database', 'password'),
|
---|
21 | host=config.get('database', 'host'),
|
---|
22 | db=config.get('database', 'database'),
|
---|
23 | )
|
---|
24 | )
|
---|
25 |
|
---|
26 | return factdb
|
---|
27 |
|
---|
28 |
|
---|
29 | def get_data_runs_from_run_db(night_int, factdb):
|
---|
30 | keys = [
|
---|
31 | 'fRunID',
|
---|
32 | 'fNight',
|
---|
33 | 'fRunStart',
|
---|
34 | 'fRunStop',
|
---|
35 | ]
|
---|
36 |
|
---|
37 | sql_query = """SELECT {comma_sep_keys}
|
---|
38 | FROM RunInfo WHERE fRunTypeKey=1 AND fNight={night:d};
|
---|
39 | """
|
---|
40 | sql_query = sql_query.format(
|
---|
41 | comma_sep_keys=', '.join(keys),
|
---|
42 | night=night_int,
|
---|
43 | )
|
---|
44 |
|
---|
45 | data_runs = pd.read_sql_query(
|
---|
46 | sql_query,
|
---|
47 | factdb,
|
---|
48 | parse_dates=['fRunStart', 'fRunStop'],
|
---|
49 | )
|
---|
50 |
|
---|
51 | return data_runs
|
---|
52 |
|
---|
53 |
|
---|
54 | def get_trigger_rates(night_int, base_path='other_aux_data/'):
|
---|
55 | fits_file = fits.open(
|
---|
56 | base_path+'{0}.FTM_CONTROL_TRIGGER_RATES.fits'.format(night_int)
|
---|
57 | )
|
---|
58 | trigger_rates = fits_file[1].data
|
---|
59 | return trigger_rates
|
---|
60 |
|
---|
61 |
|
---|
62 | def add_ratio_and_more_to_dataframe(data_runs, trigger_rates):
|
---|
63 |
|
---|
64 | # create new columns and pre-assign some hopefully good NULL-like-values
|
---|
65 | data_runs['number_of_measurements'] = 0
|
---|
66 | data_runs['median_of_board_rates'] = np.nan
|
---|
67 | data_runs['std_dev_of_board_rates'] = np.nan
|
---|
68 | data_runs['fBoardTriggerRateRatioAboveThreshold'] = np.nan
|
---|
69 |
|
---|
70 | for dataframe_index in data_runs.index:
|
---|
71 | this_run = data_runs.ix[dataframe_index]
|
---|
72 | mask = (
|
---|
73 | (trigger_rates['Time'] > fact.fjd(this_run['fRunStart'])) *
|
---|
74 | (trigger_rates['Time'] < fact.fjd(this_run['fRunStop']))
|
---|
75 | )
|
---|
76 | this_board_rates = trigger_rates['BoardRate'][mask]
|
---|
77 |
|
---|
78 | N = this_board_rates.shape[0]
|
---|
79 | data_runs.ix[dataframe_index, 'number_of_measurements'] = N
|
---|
80 |
|
---|
81 | left, med, right = np.percentile(
|
---|
82 | this_board_rates,
|
---|
83 | [50-20, 50, 50+20])
|
---|
84 | something_like_width = (right - left)/2.
|
---|
85 | # 40% of the area of the normal distrubution
|
---|
86 | # fall between -0.52*sigma and +0.52*sigma.
|
---|
87 | # The estimation of sigma in a distorted CDF
|
---|
88 | # is less affected near the median. (but of course less accurate as well.)
|
---|
89 | std_dev = something_like_width / 0.52
|
---|
90 |
|
---|
91 | median_board_rates = np.median(this_board_rates, axis=1)
|
---|
92 | over = (median_board_rates > med+3*std_dev).sum()
|
---|
93 |
|
---|
94 | data_runs.ix[dataframe_index, 'median_of_board_rates'] = med
|
---|
95 | data_runs.ix[dataframe_index, 'std_dev_of_board_rates'] = std_dev
|
---|
96 | data_runs.ix[dataframe_index, 'fBoardTriggerRateRatioAboveThreshold'] = float(over)/N
|
---|
97 |
|
---|
98 | def main(night_int):
|
---|
99 | factdb = create_DB_connection()
|
---|
100 | data_runs = get_data_runs_from_run_db(night_int, factdb)
|
---|
101 | trigger_rates = get_trigger_rates(night_int)
|
---|
102 | add_ratio_and_more_to_dataframe(data_runs, trigger_rates)
|
---|
103 | return data_runs
|
---|
104 |
|
---|
105 | if __name__ == '__main__':
|
---|
106 | data_runs = main(night_int)
|
---|
107 | data_runs.to_csv('foo.bar', index=False)
|
---|