-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbread_funcs.py
255 lines (183 loc) · 5.66 KB
/
dbread_funcs.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import numpy as np
import os
import sqlite3
from astropy.io import fits
import pandas as pd
def guess_dbfname(dname='.'):
fnames = os.listdir(dname)
db_fnames = [fname for fname in fnames if '.db' in fname]
if len(db_fnames) == 1:
return db_fnames[0]
elif len(db_fnames) > 1:
print "More than one db fname to pick from"
return db_fnames
else:
print "Can't find a db fname"
return
def get_full_sqlite_table_as_df(conn, table_name):
"""
Gets a full sqlite table and returns it as a Pandas DataFrame
Should be just one row
Parameters:
conn: sqlite connection object
Returns:
df DataFrame: The sqlite table as a pandas DataFrame
"""
sql = "select * from %s" %(table_name)
df = pd.read_sql(sql, conn)
return df
def get_info_tab(conn):
"""
Gets the full DataInfo table
Should be just one row
Parameters:
conn: sqlite connection object
Returns:
DataInfo DataFrame: The DataInfo table in a pandas DataFrame
"""
df = pd.read_sql("select * from DataInfo", conn)
return df
def get_files_tab(conn):
"""
Gets the full Files table
Should be just one row
Parameters:
conn: sqlite connection object
Returns:
Files DataFrame: The Files table in a pandas DataFrame
"""
df = pd.read_sql("select * from Files", conn)
return df
def get_rate_fits_tab(conn):
"""
Gets the full RateFits table
Parameters:
conn: sqlite connection object
Returns:
RateFits DataFrame: The RateFits table in a pandas DataFrame
"""
df = pd.read_sql("select * from RateFits", conn)
return df
def get_twinds_tab(conn):
"""
Gets the full TimeWindows table
Parameters:
conn: sqlite connection object
Returns:
TimeWindow DataFrame: The TimeWindows table in a pandas DataFrame
"""
df = pd.read_sql("select * from TimeWindows", conn)
return df
def get_rates_tab(conn):
"""
Gets the full TimeWindows table
Parameters:
conn: sqlite connection object
Returns:
TimeWindow DataFrame: The TimeWindows table in a pandas DataFrame
"""
df = pd.read_sql("select * from Rates", conn)
return df
def get_top_rate_timeIDs(conn, N=10, TScut=None,\
ret_rate_tab=False, ret_TSs=False):
df = get_rates_tab(conn)
rates_twind_groups = df.groupby('timeID')
twind_maxs = rates_twind_groups.TS.aggregate('max')
if TScut is not None:
bl = (twind_maxs>=TScut)
top_twind_TSs = twind_maxs[bl].sort_values()
else:
top_twind_TSs = twind_maxs.sort_values().tail(N)
timeIDs = top_twind_TSs.index.values[::-1]
TSs = top_twind_TSs.values[::-1]
if ret_rate_tab:
return timeIDs, df
elif ret_TSs:
return timeIDs, TSs
return timeIDs
def get_blips_tab(conn):
"""
Gets the full Blips table
Parameters:
conn: sqlite connection object
Returns:
Blips DataFrame: The Blips table in a pandas DataFrame
"""
df = pd.read_sql("select * from Blips", conn)
return df
def query_blips_tab(conn, timeID=None, snr_min=-1e4,\
snr_max=1e4, duration=None, pc_min=0.0):
"""
Gets the Blips table with custom constrains
Parameters:
conn: sqlite connection object
timeID: int (optional) get blips with this timeID
snr_min: float (optional) get only the blips with >= this snr
snr_max: float (optional) get only the blips with <= this snr
duration: float (optional) only get blips with this duration (s)
pc_min: float (optional) get only the blips with >= this partial coding
Returns:
Blips DataFrame: The Blips table in a pandas DataFrame
"""
sql = "select * from Blips where "
if timeID is not None:
sql += "timeID=%d and " %(timeID)
if duration is not None:
sql += "duration between %.3f and %.3f and "\
%(duration-1e-3, duration+1e-3)
sql += "pc>=%.2f and snr between %.2f and %.2f"\
%(pc_min, snr_min, snr_max)
df = pd.read_sql(sql, conn)
return df
def get_twind_status_tab(conn):
"""
Gets the full TimeWindowStatus table
Parameters:
conn: sqlite connection object
Returns:
TimeWindowStatus DataFrame: The TimeWindowStatus table in a pandas DataFrame
"""
df = pd.read_sql("select * from TimeWindowStatus", conn)
return df
def get_seeds_tab(conn, proc_group=None):
"""
Gets the Seeds table
Parameters:
conn: sqlite connection object
proc_group: int (optional) what proc_group to get the seeds for
Returns:
Seeds DataFrame: The Seeds table in a pandas DataFrame
"""
sql = "select * from Seeds"
if proc_group is not None:
sql += " where proc_group=%d" %(proc_group)
df = pd.read_sql(sql, conn)
return df
def get_square_tab(conn, proc_group=None):
"""
Gets the JobSquare table
Parameters:
conn: sqlite connection object
proc_group: int (optional) what proc_group to get the seeds for
Returns:
JobSquare DataFrame: The JobSquare table in a pandas DataFrame
"""
sql = "select * from JobSquare"
if proc_group is not None:
sql += " where proc_group=%d" %(proc_group)
df = pd.read_sql(sql, conn)
return df
def get_imgsig_tab(conn, proc_group=None):
"""
Gets the Seeds table
Parameters:
conn: sqlite connection object
proc_group: int (optional) what proc_group to get the seeds for
Returns:
Seeds DataFrame: The Seeds table in a pandas DataFrame
"""
sql = "select * from ImageSigs"
if proc_group is not None:
sql += " where proc_group=%d" %(proc_group)
df = pd.read_sql(sql, conn)
return df