Skip to content

Commit 4048019

Browse files
add more TOM capabilities and feature storage
1 parent 3783cad commit 4048019

File tree

4 files changed

+178
-2
lines changed

4 files changed

+178
-2
lines changed

resspect/fit_lightcurves.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,30 @@ def fit_TOM(data_dic: dict, features_file: str,
304304
logging.info("Features have been saved to: %s", features_file)
305305

306306
def request_TOM_data(url: str = "https://desc-tom-2.lbl.gov", username: str = None,
307-
passwordfile: str = None, password: str = None):
307+
passwordfile: str = None, password: str = None, detected_since_mjd: float = None,
308+
detected_in_last_days: float = None,):
308309
tom = TomClient(url = url, username = username, passwordfile = passwordfile,
309310
password = password)
310-
res = tom.request( 'POST', 'elasticc2/gethotsne/10/' )
311+
dic = {}
312+
if detected_since_mjd is not None:
313+
dic['detected_since_mjd'] = detected_since_mjd
314+
if detected_in_last_days is not None:
315+
dic['detected_in_last_days'] = detected_in_last_days
316+
res = tom.post('elasticc2/gethotsne', dic)
311317
data_dic = res.json()
312318
return data_dic
313319

320+
def submit_queries_to_TOM(objectids: list, priorities: list, requester: str='resspect'):
321+
req = { 'requester': requester,
322+
'objectids': objectids,
323+
'priorities': priorities}
324+
res = TomClient.request( 'POST', 'elasticc2/askforspectrum', json=req )
325+
dic = res.json()
326+
if res.satus_code != 200:
327+
raise ValueError('Request failed, ' + res.text + ". Status code: " + str(res.status_code))
328+
329+
if dic['status'] == 'error':
330+
raise ValueError('Request failed, ' + dic.json()['error'])
314331

315332
def main():
316333
return None

resspect/time_domain_loop.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,19 @@ def process_next_day_loop(
799799
return light_curve_data
800800

801801

802+
def submit_queries_to_TOM(objectids: list, priorities: list, requester: str='resspect'):
803+
req = { 'requester': requester,
804+
'objectids': objectids,
805+
'priorities': priorities}
806+
res = TomClient.request( 'POST', 'elasticc2/askforspectrum', json=req )
807+
dic = res.json()
808+
if res.satus_code != 200:
809+
raise ValueError('Request failed, ' + res.text + ". Status code: " + str(res.status_code))
810+
811+
if dic['status'] == 'error':
812+
raise ValueError('Request failed, ' + dic.json()['error'])
813+
814+
802815
# TODO: Too many arguments. Refactor and update docs
803816
def run_time_domain_active_learning_loop(
804817
light_curve_data: DataBase, learning_days: list,

resspect/tom_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,16 @@ def request( self, method="GET", page=None, **kwargs ):
9595
9696
"""
9797
return self._rqs.request( method=method, url=f"{self._url}/{page}", **kwargs )
98+
99+
def post( self, page=None, **kwargs ):
100+
"""Shortand for TomClient.request( "POST", ... )"""
101+
return self.request( "POST", page, **kwargs )
102+
103+
def get( self, page=None, **kwargs ):
104+
"""Shortand for TomClient.request( "GET", ... )"""
105+
return self.request( "GET", page, **kwargs )
106+
107+
def put( self, page=None, **kwargs ):
108+
"""Shortand for TomClient.request( "PUT", ... )"""
109+
return self.request( "PUT", page, **kwargs )
110+

resspect/update_stashes.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Copyright 2020 resspect software
2+
# Author: Amanda Wasserman
3+
#
4+
# created on 18 March 2024
5+
#
6+
# Licensed GNU General Public License v3.0;
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# https://www.gnu.org/licenses/gpl-3.0.en.html
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
__all__ = ["update_pool_stash", "update_training_stash"]
19+
20+
### Need to remove old objs at some point (once they are no longer hot)
21+
# Need to remove objects that have been moved from pool to training
22+
def update_pool_stash(current_stash_path: str, new_night_path: str):
23+
#should we store old features somewhere? makes it easier to add training objs
24+
#would want to add current MJD, maybe first MJD, and peak MJD
25+
26+
#read in current stash as list of strings
27+
with open(current_stash_path, 'r') as f:
28+
current_stash = f.readlines()
29+
30+
#read in new night as list of strings
31+
with open(new_night_path, 'r') as f:
32+
new_night = f.readlines()
33+
34+
#if id is already in stash, replace it. else append it
35+
for obj in new_night:
36+
replaced = 0
37+
id = obj.split(',')[0]
38+
for old_obj in current_stash:
39+
if id in old_obj:
40+
current_stash[current_stash.index(old_obj)] = obj
41+
replaced = 1
42+
break
43+
if replaced == 0:
44+
current_stash.append(obj)
45+
46+
#write new stash
47+
with open(current_stash_path, 'w') as f:
48+
for obj in current_stash:
49+
f.write(obj)
50+
51+
def update_training_stash_with_new_classification(current_training_stash_path: str, new_obj_ids: list,
52+
new_obj_classes: list, new_obj_redshifts: list, current_stash_path: str):
53+
#add new obj id and class for each point on the training obj light curve going forward
54+
#(how do we want to do this? add A,B,C, etc to the end of the id?)
55+
with open(current_stash_path, 'r') as f:
56+
current_stash = f.readlines()
57+
58+
with open(current_training_stash_path, 'r') as f:
59+
training_stash = f.readlines()
60+
61+
#find obj in current stash
62+
for idx, obj in new_obj_ids:
63+
for old_obj in current_stash:
64+
if idx in old_obj:
65+
line = old_obj.split(',')
66+
break
67+
line[1] = new_obj_redshifts[idx]
68+
line[2] = new_obj_classes[idx]
69+
train_to_append = ','.join(line)
70+
training_stash.append(train_to_append)
71+
72+
#write new training stash
73+
with open(current_training_stash_path, 'w') as f:
74+
for obj in training_stash:
75+
f.write(obj)
76+
77+
78+
def update_training_stash_with_new_features(new_night_path: str, current_training_stash_path: str):
79+
#add new obj id and features for each point on the training obj light curve going forward
80+
#(how do we want to do this? add A,B,C, etc to the end of the id?)
81+
with open(new_night_path, 'r') as f:
82+
new_night = f.readlines()
83+
84+
with open(current_training_stash_path, 'r') as f:
85+
training_stash = f.readlines()
86+
87+
#append training set with new features
88+
for obj in new_night:
89+
id = obj.split(',')[0]
90+
for old_obj in training_stash:
91+
if id in old_obj:
92+
#append new features
93+
old_split = old_obj.split(',')
94+
split = [id+mjd, old_split[1], old_split[2], 'N/A', 'N/A'] ###we need mjd (or something) to differentiate the ids
95+
split.extend(obj.split(',')[5:])
96+
training_stash.append(','.join(split))
97+
98+
#write new stash
99+
with open(current_training_stash_path, 'w') as f:
100+
for obj in training_stash:
101+
f.write(obj)
102+
103+
def remove_training_from_pool(current_stash_path: str, new_obj_ids: list):
104+
#remove objects that have been moved from pool to training
105+
with open(current_stash_path, 'r') as f:
106+
current_stash = f.readlines()
107+
108+
for id in new_obj_ids:
109+
for obj in current_stash:
110+
if id in obj:
111+
current_stash.remove(obj)
112+
113+
#write new stash
114+
with open(current_stash_path, 'w') as f:
115+
for obj in current_stash:
116+
f.write(obj)
117+
118+
def remove_not_hot_objects(current_stash_path: str):
119+
#remove objects that are no longer hot
120+
#need MJDs
121+
#make sure we're not getting old objects from rob or we'll be wasting computing power adding/removing
122+
with open(current_stash_path, 'r') as f:
123+
current_stash = f.readlines()
124+
125+
for obj in current_stash:
126+
if obj[current_mjd-discovery_mjd] >5 0:
127+
current_stash.remove(obj)
128+
129+
#write new stash
130+
with open(current_stash_path, 'w') as f:
131+
for obj in current_stash:
132+
f.write(obj)
133+

0 commit comments

Comments
 (0)