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

Reorganisation & visualisation #36

Merged
merged 13 commits into from
Jan 19, 2020
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ branch = false
parallel = true
data_file = ${FINK_CLIENT_HOME}/.coverage
omit=
*visualisation.py
app.py
*setup.py
*dist/*
*/lib/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Clone the GitHub repository
```bash
git clone https://github.com/astrolabsoftware/fink-client.git
cd fink-client && export FINK_CLIENT_HOME=$(PWD) >> ~/.bash_profile
cd fink-client && echo 'export FINK_CLIENT_HOME=${PWD}' >> ~/.bash_profile
```
The above expression will place the environment variable for `$FINK_CLIENT_HOME`
into your `~/.bash_profile` such that this variable should not be required to be set again.
Expand Down
114 changes: 114 additions & 0 deletions bin/fink_alert_viewer
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env python
# Copyright 2019-2020 AstroLab Software
# Author: Julien Peloton
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
""" Display cutouts and lightcurve from a ZTF alert """
import argparse

import matplotlib
import matplotlib.pyplot as plt

import numpy as np

from fink_client.avroUtils import AlertReader
from fink_client.visualisation import show_stamps
from fink_client.visualisation import extract_field

# For plots
font = {
'weight': 'bold',
'size': 22
}

matplotlib.rc('font', **font)

# Bands
filter_color = {1: '#1f77b4', 2: '#ff7f0e', 3: '#2ca02c'}
# [
# '#1f77b4', # muted blue
# '#ff7f0e', # safety orange
# '#2ca02c', # cooked asparagus green
# '#d62728', # brick red
# '#9467bd', # muted purple
# '#8c564b', # chestnut brown
# '#e377c2', # raspberry yogurt pink
# '#7f7f7f', # middle gray
# '#bcbd22', # curry yellow-green
# '#17becf' # blue-teal
# ]
filter_name = {1: 'g band', 2: 'r band', 3: 'i band'}

def main():
""" """
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-filename', type=str, default='',
help="Path to an alert data file (avro format)")
args = parser.parse_args(None)

r = AlertReader(args.filename)

# Display the cutouts contained in the alert
alert = r.to_list(size=1)[0]
print(alert['objectId'])
fig = plt.figure(num=0, figsize=(12, 4))
show_stamps(alert, fig)

# extract current and historical data as one vector
mag = extract_field(alert, 'magpsf')
error = extract_field(alert, 'sigmapsf')
upper = extract_field(alert, "diffmaglim")

# filter bands
fid = extract_field(alert, "fid")

# Rescale dates to end at 0
jd = extract_field(alert, "jd")
dates = np.array([i - jd[0] for i in jd])

# Title of the plot (alert ID)
title = alert["objectId"]

# loop over filters
fig = plt.figure(num=1, figsize=(12,4))

# Loop over each filter
for filt in filter_color.keys():
mask = np.where(fid == filt)[0]

# Skip if no data
if len(mask) == 0:
continue

# y data
maskNotNone = mag[mask] != None
plt.errorbar(
dates[mask][maskNotNone], mag[mask][maskNotNone],
yerr=error[mask][maskNotNone],
color=filter_color[filt], marker='o', ls='',
label=filter_name[filt], mew=4)
# Upper limits
plt.plot(
dates[mask][~maskNotNone], upper[mask][~maskNotNone],
color=filter_color[filt], marker='v', ls='', mew=4, alpha=0.5)
plt.title(title)
plt.legend()
plt.gca().invert_yaxis()
plt.xlabel('Days to candidate')
plt.ylabel('Difference magnitude')
plt.show()


if __name__ == "__main__":
main()
9 changes: 9 additions & 0 deletions bin/fink_client_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
# limitations under the License.
set -e


# Run the test suite on the modules
for i in ${FINK_CLIENT_HOME}/fink_client/*.py
do
coverage run \
--source=${FINK_CLIENT_HOME} \
--rcfile ${FINK_CLIENT_HOME}/.coveragerc $i
done

TEST_DIR=${FINK_CLIENT_HOME}/tests

# start Kafka in docker container
Expand Down
84 changes: 84 additions & 0 deletions bin/fink_consumer
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python
# Copyright 2019-2020 AstroLab Software
# Author: Julien Peloton
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
""" Kafka consumer to listen and archive Fink streams """
import argparse
import time

import matplotlib
import matplotlib.pyplot as plt

import numpy as np

from fink_client.consumer import AlertConsumer
import fink_client.fink_client_conf_cloud as fcc

def main():
""" """
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'-config', type=str, default='fink_client/fink_client_conf.py',
help="Path to your Fink configuration file.")
parser.add_argument(
'--display', action='store_true',
help="If specified, print on screen information about incoming alert.")
parser.add_argument(
'--save', action='store_true',
help="If specified, save alert data on disk (Avro). See also -outdir.")
parser.add_argument(
'-outdir', type=str, default='.',
help="Folder to store incoming alerts if --save is set. It must exist.")
args = parser.parse_args(None)

myconfig = {
"username": fcc.username,
'bootstrap.servers': fcc.servers,
'group_id': fcc.group_id}

if fcc.password is not None:
myconfig['password'] = fcc.password

# Instantiate a consumer
consumer = AlertConsumer(fcc.mytopics, myconfig, schema=fcc.schema)

# Time to wait before polling again if no alerts
maxtimeout = fcc.maxtimeout

# infinite loop
while True:
if args.save:
# Save alerts on disk
topic, alert = consumer.poll_and_write(
outdir=args.outdir,
timeout=maxtimeout,
overwrite=fcc.testmode)
else:
# TODO: this is useless to get it and done nothing
# why not thinking about handler like Comet?
topic, alert = consumer.poll(timeout=maxtimeout)

if args.display and topic is not None:
print("-" * 65)
row = [
time.ctime(), alert['timestamp'], topic, alert['objectId'],
alert['cdsxmatch'], alert['rfscore']
]
print("{}|{:<25}|{:<10}|{:<15}|{:<10}|{:<5}|".format(*row))
elif args.display:
print('No alerts the last {} seconds'.format(maxtimeout))


if __name__ == "__main__":
main()
191 changes: 191 additions & 0 deletions datatest/ZTF19acihgng.avro

Large diffs are not rendered by default.

129 changes: 129 additions & 0 deletions datatest/ZTF19acxxwrs.avro

Large diffs are not rendered by default.

175 changes: 175 additions & 0 deletions datatest/ZTF19acyfkzd.avro

Large diffs are not rendered by default.

153 changes: 153 additions & 0 deletions datatest/ZTF19acyjzeo.avro

Large diffs are not rendered by default.

97 changes: 0 additions & 97 deletions fink_client/alertUtils.py

This file was deleted.

Loading