-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightcurve.py
90 lines (62 loc) · 2.55 KB
/
lightcurve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from rta_model import Signal, FactRun
from fact import analysis, instrument
import pandas as pd
from fact import plotting
import matplotlib.pyplot as plt
import dateutil.parser
theta_off_keys = ['theta_off_{}'.format(i) for i in range(1, 6)]
def fetch_data(start, end, source=None):
print('Start: {}'.format(start))
print('End: {}'.format(end))
print('Source: {}'.format(source))
query = FactRun.select() \
.where(FactRun.health == 'OK')\
.where(FactRun.start_time >= start)\
.where(FactRun.end_time <= end)
if source:
query = query.where(FactRun.source == source)
runs = pd.DataFrame(list(query.dicts()))
query = Signal.select().where(Signal.event_timestamp >= start & Signal.analysis_timestamp <= end)
events = pd.DataFrame(list(query.dicts()))
if len(runs) == 0:
return runs, events
# rename columns to fit the expected column names for pyfact stuff
runs = runs.rename(columns={'on_time':'ontime', 'run':'run_id', 'start_time':'run_start', 'end_time':'run_stop'})
events = events.rename(columns={'run':'run_id'})
# for some reason the dates will get parsed as strings by peewee.
# so I create a pandas datime thing by hand
runs['run_start'] = pd.to_datetime(runs['run_start'])
runs['run_stop'] = pd.to_datetime(runs['run_stop'])
# provide theta in the right unit and name
# use pyfact to get from mm in camera coordinates to degree.
for t in ['theta_on'] + theta_off_keys:
events[t] = events[t].apply(instrument.camera_distance_mm_to_deg)
return runs, events
def excess(runs, events, prediction_threshold=0.9, theta2_cut=0.02, bin_width_minutes=20):
if events.empty:
return pd.DataFrame()
s = analysis.calc_run_summary_source_independent(
events,
runs,
prediction_threshold=prediction_threshold,
theta2_cut=theta2_cut,
prediction_key='prediction',
theta_key='theta_on',
theta_off_keys=theta_off_keys
)
def f(df):
return analysis.ontime_binning(df, bin_width_minutes=bin_width_minutes)
d = analysis.bin_runs(s, binning_function=f)
return d
def main():
start = dateutil.parser.parse('2013-11-03')
end = dateutil.parser.parse('2023-11-04')
runs, events = fetch_data(start, end)
d = excess(runs, events, bin_width_minutes=60)
if d.empty:
print('No events in db for given period')
return
plotting.analysis.plot_excess_rate(d)
plt.show()
if __name__ == '__main__':
main()