Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dash outputs file, add tom client class, add fit_TOM function #1

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions resspect/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ def load_features_from_file(self, path_to_features_file: str, screen=False,
'rp1', 'rp2', 'rp3', 'rmax_flux',
'ip1', 'ip2', 'ip3', 'imax_flux',
'zp1', 'zp2', 'zp3', 'zmax_flux']

elif feature_extractor == 'malanchev':
self.features_names = ['ganderson_darling_normal','ginter_percentile_range_5',
'gchi2','gstetson_K','gweighted_mean','gduration',
Expand Down Expand Up @@ -310,12 +309,44 @@ def load_features_from_file(self, path_to_features_file: str, screen=False,
self.metadata_names = self.metadata_names + ['cost_' + name]

elif survey == 'LSST':
self.features_names = ['uA', 'uB', 'ut0', 'utfall', 'utrise',
'gA', 'gB', 'gt0', 'gtfall', 'gtrise',
'rA', 'rB', 'rt0', 'rtfall', 'rtrise',
'iA', 'iB', 'it0', 'itfall', 'itrise',
'zA', 'zB', 'zt0', 'ztfall', 'ztrise',
'YA', 'YB', 'Yt0', 'Ytfall', 'Ytrise']
if feature_extractor == "bazin":
self.features_names = ['uA', 'uB', 'ut0', 'utfall', 'utrise',
'gA', 'gB', 'gt0', 'gtfall', 'gtrise',
'rA', 'rB', 'rt0', 'rtfall', 'rtrise',
'iA', 'iB', 'it0', 'itfall', 'itrise',
'zA', 'zB', 'zt0', 'ztfall', 'ztrise',
'YA', 'YB', 'Yt0', 'Ytfall', 'Ytrise']
elif feature_extractor == "malanchev":
self.features_names = ['uanderson_darling_normal','uinter_percentile_range_5',
'uchi2','ustetson_K','uweighted_mean','uduration',
'uotsu_mean_diff','uotsu_std_lower', 'uotsu_std_upper',
'uotsu_lower_to_all_ratio', 'ulinear_fit_slope',
'ulinear_fit_slope_sigma','ulinear_fit_reduced_chi2',
'ganderson_darling_normal','ginter_percentile_range_5',
'gchi2','gstetson_K','gweighted_mean','gduration',
'gotsu_mean_diff','gotsu_std_lower', 'gotsu_std_upper',
'gotsu_lower_to_all_ratio', 'glinear_fit_slope',
'glinear_fit_slope_sigma','glinear_fit_reduced_chi2',
'randerson_darling_normal', 'rinter_percentile_range_5',
'rchi2', 'rstetson_K', 'rweighted_mean','rduration',
'rotsu_mean_diff','rotsu_std_lower', 'rotsu_std_upper',
'rotsu_lower_to_all_ratio', 'rlinear_fit_slope',
'rlinear_fit_slope_sigma','rlinear_fit_reduced_chi2',
'ianderson_darling_normal','iinter_percentile_range_5',
'ichi2', 'istetson_K', 'iweighted_mean','iduration',
'iotsu_mean_diff','iotsu_std_lower', 'iotsu_std_upper',
'iotsu_lower_to_all_ratio', 'ilinear_fit_slope',
'ilinear_fit_slope_sigma','ilinear_fit_reduced_chi2',
'zanderson_darling_normal','zinter_percentile_range_5',
'zchi2', 'zstetson_K', 'zweighted_mean','zduration',
'zotsu_mean_diff','zotsu_std_lower', 'zotsu_std_upper',
'zotsu_lower_to_all_ratio', 'zlinear_fit_slope',
'zlinear_fit_slope_sigma','zlinear_fit_reduced_chi2',
'Yanderson_darling_normal','Yinter_percentile_range_5',
'Ychi2', 'Ystetson_K', 'Yweighted_mean','Yduration',
'Yotsu_mean_diff','Yotsu_std_lower', 'Yotsu_std_upper',
'Yotsu_lower_to_all_ratio', 'Ylinear_fit_slope',
'Ylinear_fit_slope_sigma','Ylinear_fit_reduced_chi2']

if 'objid' in data.keys():
self.metadata_names = ['objid', 'redshift', 'type', 'code',
Expand Down
87 changes: 85 additions & 2 deletions resspect/fit_lightcurves.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2020 resspect software
# Author: Rupesh Durgesh and Emille Ishida
# Author: Rupesh Durgesh, Emille Ishida, and Amanda Wasserman
#
# created on 14 April 2022
#
Expand Down Expand Up @@ -32,12 +32,15 @@
from resspect.lightcurves_utils import get_resspect_header_data
from resspect.lightcurves_utils import read_plasticc_full_photometry_data
from resspect.lightcurves_utils import SNPCC_FEATURES_HEADER
from resspect.lightcurves_utils import TOM_FEATURES_HEADER
from resspect.lightcurves_utils import TOM_MALANCHEV_FEATURES_HEADER
from resspect.lightcurves_utils import SNPCC_MALANCHEV_FEATURES_HEADER
from resspect.lightcurves_utils import find_available_key_name_in_header
from resspect.lightcurves_utils import PLASTICC_TARGET_TYPES
from resspect.lightcurves_utils import PLASTICC_RESSPECT_FEATURES_HEADER
from resspect.tom_client import TomClient

__all__ = ["fit_snpcc", "fit_plasticc"]
__all__ = ["fit_snpcc", "fit_plasticc", "fit_TOM", "request_TOM_data"]


FEATURE_EXTRACTOR_MAPPING = {
Expand Down Expand Up @@ -238,6 +241,86 @@ def fit_plasticc(path_photo_file: str, path_header_file: str,
light_curve_data, plasticc_features_file)
logging.info("Features have been saved to: %s", output_file)

def _TOM_sample_fit(
obj_dic: dict, feature_extractor: str):
"""
Reads SNPCC file and performs fit.

Parameters
----------
id
SNID
feature_extractor
Function used for feature extraction.
Options are 'bazin', 'bump', or 'malanchev'.
"""
light_curve_data = FEATURE_EXTRACTOR_MAPPING[feature_extractor]()
light_curve_data.photometry = pd.DataFrame(obj_dic['photometry'])
light_curve_data.dataset_name = 'TOM'
light_curve_data.filters = ['u', 'g', 'r', 'i', 'z', 'Y']
light_curve_data.id = obj_dic['objectid']
light_curve_data.redshift = obj_dic['redshift']
light_curve_data.sntype = 'unknown'
light_curve_data.sncode = obj_dic['sncode']
light_curve_data.sample = 'N/A'

light_curve_data.fit_all()

return light_curve_data

def fit_TOM(data_dic: dict, output_features_file: str,
number_of_processors: int = 1,
feature_extractor: str = 'bazin'):
"""
Perform fit to all objects from the TOM data.

Parameters
----------
data_dic: str
Dictionary containing the photometry for all light curves.
output_features_file: str
Path to output file where results should be stored.
number_of_processors: int, default 1
Number of cpu processes to use.
feature_extractor: str, default bazin
Function used for feature extraction.
"""
if feature_extractor == 'bazin':
header = TOM_FEATURES_HEADER
elif feature_extractor == 'malanchev':
header = TOM_MALANCHEV_FEATURES_HEADER

multi_process = multiprocessing.Pool(number_of_processors)
logging.info("Starting TOM " + feature_extractor + " fit...")
with open(output_features_file, 'w') as TOM_features_file:
TOM_features_file.write(','.join(header) + '\n')

for light_curve_data in multi_process.starmap(
_TOM_sample_fit, zip(
data_dic, repeat(feature_extractor))):
if 'None' not in light_curve_data.features:
write_features_to_output_file(
light_curve_data, TOM_features_file)
logging.info("Features have been saved to: %s", output_features_file)

def request_TOM_data(url: str = "https://desc-tom-2.lbl.gov", username: str = None,
passwordfile: str = None, password: str = None, detected_since_mjd: float = None,
detected_in_last_days: float = None, mjdnow: float = None, cheat_gentypes: list = None):
tom = TomClient(url = url, username = username, passwordfile = passwordfile,
password = password)
dic = {}
if detected_since_mjd is not None:
dic['detected_since_mjd'] = detected_since_mjd
if detected_in_last_days is not None:
dic['detected_in_last_days'] = detected_in_last_days
if mjdnow is not None:
dic['mjd_now'] = mjdnow
if cheat_gentypes is not None:
dic['cheat_gentypes'] = cheat_gentypes
res = tom.post('elasticc2/gethottransients', json = dic)
data_dic = res.json()
return data_dic


def main():
return None
Expand Down
31 changes: 31 additions & 0 deletions resspect/lightcurves_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@
'zotsu_lower_to_all_ratio', 'zlinear_fit_slope', 'zlinear_fit_slope_sigma','zlinear_fit_reduced_chi2'
]

TOM_FEATURES_HEADER = [
'id', 'redshift', 'type', 'code', 'orig_sample',
'uA', 'uB', 'ut0', 'utfall', 'utrise',
'gA', 'gB', 'gt0', 'gtfall', 'gtrise', 'rA', 'rB',
'rt0', 'rtfall', 'rtrise', 'iA', 'iB', 'it0', 'itfall',
'itrise', 'zA', 'zB', 'zt0', 'ztfall', 'ztrise',
'YA', 'YB', 'Yt0', 'Ytfall', 'Ytrise'
]

TOM_MALANCHEV_FEATURES_HEADER = [
'id', 'redshift', 'type', 'code', 'orig_sample',
'uanderson_darling_normal','uinter_percentile_range_5',
'uchi2','ustetson_K','uweighted_mean','uduration', 'uotsu_mean_diff','uotsu_std_lower', 'uotsu_std_upper',
'uotsu_lower_to_all_ratio', 'ulinear_fit_slope', 'ulinear_fit_slope_sigma','ulinear_fit_reduced_chi2',
'ganderson_darling_normal','ginter_percentile_range_5',
'gchi2','gstetson_K','gweighted_mean','gduration', 'gotsu_mean_diff','gotsu_std_lower', 'gotsu_std_upper',
'gotsu_lower_to_all_ratio', 'glinear_fit_slope', 'glinear_fit_slope_sigma','glinear_fit_reduced_chi2',
'randerson_darling_normal', 'rinter_percentile_range_5',
'rchi2', 'rstetson_K', 'rweighted_mean','rduration', 'rotsu_mean_diff','rotsu_std_lower', 'rotsu_std_upper',
'rotsu_lower_to_all_ratio', 'rlinear_fit_slope', 'rlinear_fit_slope_sigma','rlinear_fit_reduced_chi2',
'ianderson_darling_normal','iinter_percentile_range_5',
'ichi2', 'istetson_K', 'iweighted_mean','iduration', 'iotsu_mean_diff','iotsu_std_lower', 'iotsu_std_upper',
'iotsu_lower_to_all_ratio', 'ilinear_fit_slope', 'ilinear_fit_slope_sigma','ilinear_fit_reduced_chi2',
'zanderson_darling_normal','zinter_percentile_range_5',
'zchi2', 'zstetson_K', 'zweighted_mean','zduration', 'zotsu_mean_diff','zotsu_std_lower', 'zotsu_std_upper',
'zotsu_lower_to_all_ratio', 'zlinear_fit_slope', 'zlinear_fit_slope_sigma','zlinear_fit_reduced_chi2',
'Yanderson_darling_normal','Yinter_percentile_range_5',
'Ychi2','Ystetson_K','Yweighted_mean','Yduration', 'Yotsu_mean_diff','Yotsu_std_lower', 'Yotsu_std_upper',
'Yotsu_lower_to_all_ratio', 'Ylinear_fit_slope', 'Ylinear_fit_slope_sigma','Ylinear_fit_reduced_chi2'
]

PLASTICC_RESSPECT_FEATURES_HEADER = [
'id', 'redshift', 'type', 'code', 'orig_sample', 'uA', 'uB', 'ut0',
'utfall', 'utrise', 'gA', 'gB', 'gt0', 'gtfall','gtrise', 'rA', 'rB',
Expand Down
43 changes: 43 additions & 0 deletions resspect/read_dash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2020 resspect software
# Author: Amanda Wasserman
#
# created on 11 March 2024
#
# Licensed GNU General Public License v3.0;
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gnu.org/licenses/gpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import csv

__all__ = ["get_id_type"]

def get_id_type(file: str):
#read text file
data = open(file, "r")

#retrieve lines with id and type
sns =[]
for line in data:
if '.txt' in line:
sns.append(line)

ids = [id[0:7] for id in sns]

type = []
#parse file for type
for obj in sns:
indxb = obj.find("('")
indxe = obj.find(") ")
temp = obj[indxb+2:indxe]
temp = temp.split(',')[0]
type.append(temp[0:-1])
return ids, type

17 changes: 16 additions & 1 deletion resspect/time_domain_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__all__ = ['time_domain_loop', 'load_dataset']
__all__ = ['time_domain_loop', 'load_dataset', 'submit_queries_to_TOM']

import os
from typing import Union, Tuple
Expand All @@ -24,6 +24,7 @@
import progressbar

from resspect import DataBase
from resspect.tom_client import TomClient


def load_dataset(file_names_dict: dict, survey_name: str = 'DES',
Expand Down Expand Up @@ -799,6 +800,20 @@ def process_next_day_loop(
return light_curve_data


def submit_queries_to_TOM(username, passwordfile, objectids: list, priorities: list, requester: str='resspect'):
tom = TomClient(url = "https://desc-tom-2.lbl.gov", username = username, passwordfile = passwordfile)
req = { 'requester': requester,
'objectids': objectids,
'priorities': priorities}
res = tom.request( 'POST', 'elasticc2/askforspectrum', json=req )
dic = res.json()
if res.satus_code != 200:
raise ValueError('Request failed, ' + res.text + ". Status code: " + str(res.status_code))

if dic['status'] == 'error':
raise ValueError('Request failed, ' + dic.json()['error'])


# TODO: Too many arguments. Refactor and update docs
def run_time_domain_active_learning_loop(
light_curve_data: DataBase, learning_days: list,
Expand Down
Loading
Loading