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

Update tool lightcurve-analysis to 0.0.1+galaxy0 #91

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions tools/lightcurve-analysis/.shed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
categories:
- Astronomy
description: lightcurve-analysis
homepage_url: null
long_description: lightcurve-analysis
name: lightcurve_analysis_astro_tool
owner: astroteam
remote_repository_url: https://github.com/esg-epfl-apc/tools-astro/tree/main/tools
type: unrestricted
88 changes: 88 additions & 0 deletions tools/lightcurve-analysis/correlate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env python
# coding: utf-8

# flake8: noqa

import json
import os
import shutil

from oda_api.json import CustomJSONEncoder

light_curve_1 = "spiacs_lc_query.fits" # http://odahub.io/ontology#POSIXPath
light_curve_2 = "spiacs_lc_query.fits" # http://odahub.io/ontology#POSIXPath
mode = (
"Background" # oda:String; oda:allowed_value 'Background','Peak','Focus'.
)

_galaxy_wd = os.getcwd()

with open("inputs.json", "r") as fd:
inp_dic = json.load(fd)
if "_data_product" in inp_dic.keys():
inp_pdic = inp_dic["_data_product"]
else:
inp_pdic = inp_dic

for vn, vv in inp_pdic.items():
if vn != "_selector":
globals()[vn] = type(globals()[vn])(vv)

from astropy.io import fits
from astropy.time import Time
from matplotlib import pylab as plt
from oda_api.data_products import LightCurveDataProduct, PictureProduct

lc1 = fits.open(light_curve_1)[1].data
lc2 = fits.open(light_curve_2)[1].data

plt.figure()

plt.plot(lc1["TIME"], lc1["RATE"])
plt.plot(lc2["TIME"], lc2["RATE"])

plt.savefig("lc.png")

outlcpng = PictureProduct.from_file("lc.png")
outlc = LightCurveDataProduct.from_arrays(
times=Time(lc2["TIME"], format="mjd"),
rates=lc2["RATE"],
errors=lc2["ERROR"],
)

detection_image = outlcpng # oda:ODAPictureProduct
processed_lc = outlc # oda:LightCurve

# output gathering
_galaxy_meta_data = {}
_oda_outs = []
_oda_outs.append(
(
"out_correlate_detection_image",
"detection_image_galaxy.output",
detection_image,
)
)
_oda_outs.append(
("out_correlate_processed_lc", "processed_lc_galaxy.output", processed_lc)
)

for _outn, _outfn, _outv in _oda_outs:
_galaxy_outfile_name = os.path.join(_galaxy_wd, _outfn)
if isinstance(_outv, str) and os.path.isfile(_outv):
shutil.move(_outv, _galaxy_outfile_name)
_galaxy_meta_data[_outn] = {"ext": "_sniff_"}
elif getattr(_outv, "write_fits_file", None):
_outv.write_fits_file(_galaxy_outfile_name)
_galaxy_meta_data[_outn] = {"ext": "fits"}
elif getattr(_outv, "write_file", None):
_outv.write_file(_galaxy_outfile_name)
_galaxy_meta_data[_outn] = {"ext": "_sniff_"}
else:
with open(_galaxy_outfile_name, "w") as fd:
json.dump(_outv, fd, cls=CustomJSONEncoder)
_galaxy_meta_data[_outn] = {"ext": "json"}

with open(os.path.join(_galaxy_wd, "galaxy.json"), "w") as fd:
json.dump(_galaxy_meta_data, fd)
print("*** Job finished successfully ***")
118 changes: 118 additions & 0 deletions tools/lightcurve-analysis/detect_impulsive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python
# coding: utf-8

# flake8: noqa

import json
import os
import shutil

from oda_api.json import CustomJSONEncoder

light_curve = "spiacs_lc_query.fits" # http://odahub.io/ontology#POSIXPath
time_column = "TIME"
rate_column = "FLUX"
rate_err_column = "FLUX_ERR"
mode = "Background" # oda:String; oda:allowed_value 'Background','Peak','Focus', 'Standartize'.

_galaxy_wd = os.getcwd()

with open("inputs.json", "r") as fd:
inp_dic = json.load(fd)
if "_data_product" in inp_dic.keys():
inp_pdic = inp_dic["_data_product"]
else:
inp_pdic = inp_dic

for vn, vv in inp_pdic.items():
if vn != "_selector":
globals()[vn] = type(globals()[vn])(vv)

import numpy as np
from astropy.io import fits
from oda_api.data_products import LightCurveDataProduct, PictureProduct

lc = fits.open(light_curve)[1].data

lc

lc.columns.names

def get_one_of_names(d, names):
for n in names:
if n in lc.columns.names:
return lc[n]

t = get_one_of_names(lc, ["TIME", "Tmean[MJD]", time_column])
rate = get_one_of_names(lc, ["RATE", "FLUX", "Flux[counts/cm2s]", rate_column])
rate_err = get_one_of_names(
lc, ["ERROR", "Flux_error[counts/cm2s]", rate_err_column]
)

bkg = np.mean(rate)

peak_i = rate.argmax()

if mode == "Background":
rate -= bkg

from matplotlib import pylab as plt

plt.figure()

plt.errorbar(t, rate, rate_err)

plt.axvline(t[peak_i], c="r")

plt.xlabel("seconds since T$_0$")
plt.ylabel("counts/s")

plt.savefig("lc.png")

from astropy.time import Time

outlcpng = PictureProduct.from_file("lc.png")
outlc = LightCurveDataProduct.from_arrays(
times=Time(t, format="mjd"), rates=rate, errors=rate_err
)

detection_image = outlcpng # oda:ODAPictureProduct
processed_lc = outlc # oda:LightCurve

# output gathering
_galaxy_meta_data = {}
_oda_outs = []
_oda_outs.append(
(
"out_detect_impulsive_detection_image",
"detection_image_galaxy.output",
detection_image,
)
)
_oda_outs.append(
(
"out_detect_impulsive_processed_lc",
"processed_lc_galaxy.output",
processed_lc,
)
)

for _outn, _outfn, _outv in _oda_outs:
_galaxy_outfile_name = os.path.join(_galaxy_wd, _outfn)
if isinstance(_outv, str) and os.path.isfile(_outv):
shutil.move(_outv, _galaxy_outfile_name)
_galaxy_meta_data[_outn] = {"ext": "_sniff_"}
elif getattr(_outv, "write_fits_file", None):
_outv.write_fits_file(_galaxy_outfile_name)
_galaxy_meta_data[_outn] = {"ext": "fits"}
elif getattr(_outv, "write_file", None):
_outv.write_file(_galaxy_outfile_name)
_galaxy_meta_data[_outn] = {"ext": "_sniff_"}
else:
with open(_galaxy_outfile_name, "w") as fd:
json.dump(_outv, fd, cls=CustomJSONEncoder)
_galaxy_meta_data[_outn] = {"ext": "json"}

with open(os.path.join(_galaxy_wd, "galaxy.json"), "w") as fd:
json.dump(_galaxy_meta_data, fd)
print("*** Job finished successfully ***")
91 changes: 91 additions & 0 deletions tools/lightcurve-analysis/lightcurve_analysis_astro_tool.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<tool id="lightcurve_analysis_astro_tool" name="lightcurve-analysis" version="0.0.1+galaxy0" profile="23.0">
<requirements>
<requirement type="package" version="2.2.2">pandas</requirement>
<requirement type="package" version="1.2.20">oda-api</requirement>
<requirement type="package" version="3.9.0">matplotlib</requirement>
<requirement type="package" version="2.32.3">requests</requirement>
<requirement type="package" version="0.4.7">astroquery</requirement>
<requirement type="package" version="7.0.0">rdflib</requirement>
<requirement type="package" version="4.12.3">beautifulsoup4</requirement>
<requirement type="package" version="2.0.0">sparqlwrapper</requirement>
<requirement type="package" version="3.3">networkx</requirement>
<requirement type="package" version="1.14.0">scipy</requirement>
<requirement type="package" version="8.26.0">ipython</requirement>
<requirement type="package" version="6.1.1">astropy</requirement>
<requirement type="package" version="2.0.0">numpy</requirement>
<!--Requirements string 'nb2workflow' can't be converted automatically. Please add the galaxy/conda requirement manually or modify the requirements file!-->
</requirements>
<command detect_errors="exit_code">python '$__tool_directory__/${_data_product._selector}.py'</command>
<configfiles>
<inputs name="inputs" filename="inputs.json" data_style="paths" />
</configfiles>
<inputs>
<conditional name="_data_product">
<param name="_selector" type="select" label="Data Product">
<option value="correlate" selected="true">correlate</option>
<option value="detect_impulsive" selected="false">detect_impulsive</option>
</param>
<when value="correlate">
<param name="light_curve_1" type="data" label="light_curve_1" format="data" />
<param name="light_curve_2" type="data" label="light_curve_2" format="data" />
<param name="mode" type="select" label="mode">
<option value="Background" selected="true">Background</option>
<option value="Focus">Focus</option>
<option value="Peak">Peak</option>
</param>
</when>
<when value="detect_impulsive">
<param name="light_curve" type="data" label="light_curve" format="data" />
<param name="time_column" type="text" value="TIME" label="time_column" />
<param name="rate_column" type="text" value="FLUX" label="rate_column" />
<param name="rate_err_column" type="text" value="FLUX_ERR" label="rate_err_column" />
<param name="mode" type="select" label="mode">
<option value="Background" selected="true">Background</option>
<option value="Focus">Focus</option>
<option value="Peak">Peak</option>
<option value="Standartize">Standartize</option>
</param>
</when>
</conditional>
</inputs>
<outputs>
<data label="${tool.name} -&gt; correlate detection_image" name="out_correlate_detection_image" format="auto" from_work_dir="detection_image_galaxy.output">
<filter>_data_product['_selector'] == 'correlate'</filter>
</data>
<data label="${tool.name} -&gt; correlate processed_lc" name="out_correlate_processed_lc" format="auto" from_work_dir="processed_lc_galaxy.output">
<filter>_data_product['_selector'] == 'correlate'</filter>
</data>
<data label="${tool.name} -&gt; detect_impulsive detection_image" name="out_detect_impulsive_detection_image" format="auto" from_work_dir="detection_image_galaxy.output">
<filter>_data_product['_selector'] == 'detect_impulsive'</filter>
</data>
<data label="${tool.name} -&gt; detect_impulsive processed_lc" name="out_detect_impulsive_processed_lc" format="auto" from_work_dir="processed_lc_galaxy.output">
<filter>_data_product['_selector'] == 'detect_impulsive'</filter>
</data>
</outputs>
<tests>
<test expect_num_outputs="2">
<conditional name="_data_product">
<param name="_selector" value="correlate" />
<param name="light_curve_1" location="https://gitlab.renkulab.io/astronomy/mmoda/lightcurve-analysis/-/raw/fdeef1d7e716c836601f41dc961ebfb288fa456f/spiacs_lc_query.fits" />
<param name="light_curve_2" location="https://gitlab.renkulab.io/astronomy/mmoda/lightcurve-analysis/-/raw/fdeef1d7e716c836601f41dc961ebfb288fa456f/spiacs_lc_query.fits" />
<param name="mode" value="Background" />
</conditional>
<assert_stdout>
<has_text text="*** Job finished successfully ***" />
</assert_stdout>
</test>
<test expect_num_outputs="2">
<conditional name="_data_product">
<param name="_selector" value="detect_impulsive" />
<param name="light_curve" location="https://gitlab.renkulab.io/astronomy/mmoda/lightcurve-analysis/-/raw/fdeef1d7e716c836601f41dc961ebfb288fa456f/spiacs_lc_query.fits" />
<param name="time_column" value="TIME" />
<param name="rate_column" value="FLUX" />
<param name="rate_err_column" value="FLUX_ERR" />
<param name="mode" value="Background" />
</conditional>
<assert_stdout>
<has_text text="*** Job finished successfully ***" />
</assert_stdout>
</test>
</tests>
</tool>
Loading