Skip to content

Commit

Permalink
pep8 requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
FusRoman committed Apr 7, 2023
1 parent f66aa4d commit 335ed75
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 106 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ per-file-ignores =
../Fink_GRB/fink_grb/online/ztf_join_gcn.py:W503,E402
../Fink_GRB/fink_grb/offline/spark_offline.py:W503,W605
../Fink_GRB/fink_grb/utils/fun_utils.py:F811
../Fink_GRB/fink_grb/distribution/distribution.py:W503
../Fink_GRB/fink_grb/distribution/distribution.py:W503
../Fink_GRB/fink_grb/observatory/__init__.py:E402
114 changes: 83 additions & 31 deletions fink_grb/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,112 @@ def init_test(doctest_namespace):
@pytest.fixture(autouse=True)
def init_fermi(doctest_namespace):

doctest_namespace["fermi_gbm_voevent_path"] = 'fink_grb/test/test_data/VODB/fermi/voevent_number=193.xml'
doctest_namespace["fermi_lat_voevent_path"] = 'fink_grb/test/test_data/VODB/fermi/voevent_number=2842.xml'
doctest_namespace[
"fermi_gbm_voevent_path"
] = "fink_grb/test/test_data/VODB/fermi/voevent_number=193.xml"
doctest_namespace[
"fermi_lat_voevent_path"
] = "fink_grb/test/test_data/VODB/fermi/voevent_number=2842.xml"

fermi_gbm = voevent_to_class(
load_voevent_from_path(doctest_namespace["fermi_gbm_voevent_path"])
)
fermi_lat = voevent_to_class(
load_voevent_from_path(doctest_namespace["fermi_lat_voevent_path"])
)

fermi_gbm = voevent_to_class(load_voevent_from_path(doctest_namespace["fermi_gbm_voevent_path"]))
fermi_lat = voevent_to_class(load_voevent_from_path(doctest_namespace["fermi_lat_voevent_path"]))


doctest_namespace["fermi_gbm"] = fermi_gbm
doctest_namespace["fermi_lat"] = fermi_lat


@pytest.fixture(autouse=True)
def init_swift(doctest_namespace):

doctest_namespace["swift_bat_voevent_path"] = 'fink_grb/test/test_data/VODB/swift/voevent_number=392.xml'
doctest_namespace["swift_xrt_voevent_path"] = 'fink_grb/test/test_data/VODB/swift/voevent_number=4554.xml'
doctest_namespace["swift_uvot_voevent_path"] = 'fink_grb/test/test_data/VODB/swift/voevent_number=8582.xml'

swift_bat = voevent_to_class(load_voevent_from_path(doctest_namespace["swift_bat_voevent_path"]))
swift_xrt = voevent_to_class(load_voevent_from_path(doctest_namespace["swift_xrt_voevent_path"]))
swift_uvot = voevent_to_class(load_voevent_from_path(doctest_namespace["swift_uvot_voevent_path"]))
doctest_namespace[
"swift_bat_voevent_path"
] = "fink_grb/test/test_data/VODB/swift/voevent_number=392.xml"
doctest_namespace[
"swift_xrt_voevent_path"
] = "fink_grb/test/test_data/VODB/swift/voevent_number=4554.xml"
doctest_namespace[
"swift_uvot_voevent_path"
] = "fink_grb/test/test_data/VODB/swift/voevent_number=8582.xml"

swift_bat = voevent_to_class(
load_voevent_from_path(doctest_namespace["swift_bat_voevent_path"])
)
swift_xrt = voevent_to_class(
load_voevent_from_path(doctest_namespace["swift_xrt_voevent_path"])
)
swift_uvot = voevent_to_class(
load_voevent_from_path(doctest_namespace["swift_uvot_voevent_path"])
)

doctest_namespace["swift_bat"] = swift_bat
doctest_namespace["swift_xrt"] = swift_xrt
doctest_namespace["swift_uvot"] = swift_uvot


@pytest.fixture(autouse=True)
def init_integral(doctest_namespace):

doctest_namespace["integral_weak_voevent_path"] = 'fink_grb/test/test_data/VODB/integral/voevent_number=737.xml'
doctest_namespace["integral_wakeup_voevent_path"] = 'fink_grb/test/test_data/VODB/integral/voevent_number=18790.xml'
doctest_namespace["integral_refined_voevent_path"] = 'fink_grb/test/test_data/VODB/integral/voevent_number=18791.xml'

integral_weak = voevent_to_class(load_voevent_from_path(doctest_namespace["integral_weak_voevent_path"]))
integral_wakeup = voevent_to_class(load_voevent_from_path(doctest_namespace["integral_wakeup_voevent_path"]))
integral_refined = voevent_to_class(load_voevent_from_path(doctest_namespace["integral_refined_voevent_path"]))
doctest_namespace[
"integral_weak_voevent_path"
] = "fink_grb/test/test_data/VODB/integral/voevent_number=737.xml"
doctest_namespace[
"integral_wakeup_voevent_path"
] = "fink_grb/test/test_data/VODB/integral/voevent_number=18790.xml"
doctest_namespace[
"integral_refined_voevent_path"
] = "fink_grb/test/test_data/VODB/integral/voevent_number=18791.xml"

integral_weak = voevent_to_class(
load_voevent_from_path(doctest_namespace["integral_weak_voevent_path"])
)
integral_wakeup = voevent_to_class(
load_voevent_from_path(doctest_namespace["integral_wakeup_voevent_path"])
)
integral_refined = voevent_to_class(
load_voevent_from_path(doctest_namespace["integral_refined_voevent_path"])
)

doctest_namespace["integral_weak"] = integral_weak
doctest_namespace["integral_wakeup"] = integral_wakeup
doctest_namespace["integral_refined"] = integral_refined


@pytest.fixture(autouse=True)
def init_icecube(doctest_namespace):

doctest_namespace["icecube_cascade_voevent_path"] = 'fink_grb/test/test_data/VODB/icecube/voevent_number=825.xml'
doctest_namespace["icecube_bronze_voevent_path"] = 'fink_grb/test/test_data/VODB/icecube/voevent_number=3028.xml'
doctest_namespace["icecube_gold_voevent_path"] = 'fink_grb/test/test_data/VODB/icecube/voevent_number=45412.xml'

icecube_cascade = voevent_to_class(load_voevent_from_path(doctest_namespace["icecube_cascade_voevent_path"]))
icecube_bronze = voevent_to_class(load_voevent_from_path(doctest_namespace["icecube_bronze_voevent_path"]))
icecube_gold = voevent_to_class(load_voevent_from_path(doctest_namespace["icecube_gold_voevent_path"]))
doctest_namespace[
"icecube_cascade_voevent_path"
] = "fink_grb/test/test_data/VODB/icecube/voevent_number=825.xml"
doctest_namespace[
"icecube_bronze_voevent_path"
] = "fink_grb/test/test_data/VODB/icecube/voevent_number=3028.xml"
doctest_namespace[
"icecube_gold_voevent_path"
] = "fink_grb/test/test_data/VODB/icecube/voevent_number=45412.xml"

icecube_cascade = voevent_to_class(
load_voevent_from_path(doctest_namespace["icecube_cascade_voevent_path"])
)
icecube_bronze = voevent_to_class(
load_voevent_from_path(doctest_namespace["icecube_bronze_voevent_path"])
)
icecube_gold = voevent_to_class(
load_voevent_from_path(doctest_namespace["icecube_gold_voevent_path"])
)

doctest_namespace["icecube_cascade"] = icecube_cascade
doctest_namespace["icecube_bronze"] = icecube_bronze
doctest_namespace["icecube_gold"] = icecube_gold


@pytest.fixture(autouse=True, scope="session")
def init_spark(doctest_namespace):

from astropy.time import Time
import os

online_output_tempdir = tempfile.TemporaryDirectory()
doctest_namespace["online_output_tempdir"] = online_output_tempdir
Expand All @@ -88,13 +134,14 @@ def init_spark(doctest_namespace):

grb_data = "fink_grb/test/test_data/gcn_test/raw/year=2019/month=09/day=03"
join_data = "fink_grb/test/test_data/join_raw_datatest.parquet"
alert_data = "fink_grb/test/test_data/ztf_test/online/science/year=2019/month=09/day=03/"
alert_data = (
"fink_grb/test/test_data/ztf_test/online/science/year=2019/month=09/day=03/"
)

doctest_namespace["grb_data"] = grb_data
doctest_namespace["join_data"] = join_data
doctest_namespace["alert_data"] = alert_data


ztf_datatest = "fink_grb/test/test_data/ztf_test/online"
gcn_datatest = "fink_grb/test/test_data/gcn_test"
join_data_test = "fink_grb/test/test_data/grb_join_output.parquet"
Expand All @@ -113,7 +160,11 @@ def init_spark(doctest_namespace):
import tabulate

maxtimeout = 10
myconfig = {"username": "rlm", "bootstrap.servers": "localhost:9092", "group_id": "rlm_fink"}
myconfig = {
"username": "rlm",
"bootstrap.servers": "localhost:9092",
"group_id": "rlm_fink",
}
topics = ["fink_grb_bronze"]

headers = [
Expand Down Expand Up @@ -167,5 +218,6 @@ def init_spark(doctest_namespace):
doctest_namespace["spark"] = spark

import numpy as np

if np.__version__ >= "1.14.0":
np.set_printoptions(legacy="1.13")
12 changes: 8 additions & 4 deletions fink_grb/gcn_stream/gcn_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from lxml.objectify import ObjectifiedElement


def load_voevent_from_path(file_path: str, verbose:bool = False) -> ObjectifiedElement:
def load_voevent_from_path(file_path: str, verbose: bool = False) -> ObjectifiedElement:
"""
Load a voevent from a file object.
Raise an exception if the voevent cannot be parse.
Expand All @@ -29,17 +29,21 @@ def load_voevent_from_path(file_path: str, verbose:bool = False) -> ObjectifiedE
'ivo://nasa.gsfc.gcn/Fermi#GBM_Fin_Pos2022-07-29T10:17:31.51_680782656_0-655'
"""
try:
with open(file_path, 'rb') as f:
with open(file_path, "rb") as f:
return vp.load(f)
except Exception as e: # pragma: no cover
if verbose:
print(
"failed to load the voevent:\n\tlocation={}\n\tcause={}".format(file_path, e)
"failed to load the voevent:\n\tlocation={}\n\tcause={}".format(
file_path, e
)
)
raise e


def load_voevent_from_file(file: io.BufferedReader, verbose:bool = False) -> ObjectifiedElement:
def load_voevent_from_file(
file: io.BufferedReader, verbose: bool = False
) -> ObjectifiedElement:
"""
Load a voevent from a file object.
Raise an exception if the voevent cannot be parse.
Expand Down
26 changes: 12 additions & 14 deletions fink_grb/gcn_stream/gcn_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from fink_grb.utils.fun_utils import return_verbose_level, get_hdfs_connector
from fink_grb.observatory import voevent_to_class, TOPICS


def signal_handler(signal, frame): # pragma: no cover
"""
The signal handler function for the gcn stream.
Expand Down Expand Up @@ -51,7 +52,7 @@ def load_and_parse_gcn(gcn, gcn_rawdatapath, logger, logs=False, gcn_fs=None):
Examples
--------
>>> f = open('fink_grb/test/test_data/voevent_number=9897.xml').read().encode("UTF-8")
>>> logger = init_logging()
>>> with tempfile.TemporaryDirectory() as tmp_dir_gcn:
Expand Down Expand Up @@ -125,28 +126,25 @@ def start_gcn_stream(arguments):

try:
consumer_config = {
'group.id': 'fink_mm',
'auto.offset.reset': 'earliest',
'enable.auto.commit': False
"group.id": "fink_mm",
"auto.offset.reset": "earliest",
"enable.auto.commit": False,
}

consumer = Consumer(
config=consumer_config,
client_id=config["CLIENT"]["id"],
client_secret=config["CLIENT"]["secret"]
client_id=config["CLIENT"]["id"],
client_secret=config["CLIENT"]["secret"],
)

if arguments["--test"]:
consumer_config = {
'group.id': '',
'auto.offset.reset': 'earliest'
}
consumer_config = {"group.id": "", "auto.offset.reset": "earliest"}

consumer = Consumer(
config=consumer_config,
client_id=config["CLIENT"]["id"],
client_id=config["CLIENT"]["id"],
client_secret=config["CLIENT"]["secret"],
domain='test.gcn.nasa.gov'
domain="test.gcn.nasa.gov",
)
except Exception as e:
logger.error("Config entry not found \n\t {}".format(e))
Expand Down Expand Up @@ -191,7 +189,7 @@ def start_gcn_stream(arguments):

while True:
message = consumer.consume(timeout=2)

if len(message) != 0:
for gcn in message:
if logs:
Expand Down
11 changes: 6 additions & 5 deletions fink_grb/observatory/Fermi/Fermi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import voeventparse as vp
import os.path as path

Expand All @@ -7,6 +6,7 @@
from fink_grb.observatory import OBSERVATORY_PATH
from fink_grb.observatory.observatory import Observatory, BadInstrument


class Fermi(Observatory):
"""
Fermi instrument
Expand Down Expand Up @@ -47,8 +47,8 @@ def get_trigger_id(self):
def err_to_arcminute(self):
"""
Return the error box of the voevent in arcminute
Example
Example
-------
>>> fermi_gbm.err_to_arcminute()
680.4
Expand All @@ -57,7 +57,7 @@ def err_to_arcminute(self):
"""
instrument = self.detect_instruments()
coords = vp.get_event_position(self.voevent)
err = 1/60 if coords.err == 0.0 else coords.err
err = 1 / 60 if coords.err == 0.0 else coords.err

if instrument == "GBM":
return err * 60
Expand All @@ -66,7 +66,8 @@ def err_to_arcminute(self):
else:
raise BadInstrument("{} is not a Fermi instrument".format(instrument))


if __name__ == "__main__":

fermi = Fermi()
print(fermi)
print(fermi)
10 changes: 6 additions & 4 deletions fink_grb/observatory/IceCube/IceCube.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def __init__(self, voevent):
>>> type(obs)
<class 'IceCube.IceCube'>
"""
super().__init__(path.join(OBSERVATORY_PATH, "IceCube", "icecube.json"), voevent)
super().__init__(
path.join(OBSERVATORY_PATH, "IceCube", "icecube.json"), voevent
)

def get_trigger_id(self):
"""
Expand All @@ -53,8 +55,8 @@ def detect_instruments(self):
def err_to_arcminute(self):
"""
Return the error box of the voevent in arcminute
Example
Example
-------
>>> icecube_cascade.err_to_arcminute()
773.778
Expand Down Expand Up @@ -147,4 +149,4 @@ def voevent_to_df(self):
df["month"] = df["triggerTimeUTC"].dt.strftime("%m")
df["day"] = df["triggerTimeUTC"].dt.strftime("%d")

return df
return df
11 changes: 6 additions & 5 deletions fink_grb/observatory/Integral/Integral.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import voeventparse as vp
import os.path as path

Expand Down Expand Up @@ -26,7 +25,9 @@ def __init__(self, voevent):
>>> type(obs)
<class 'Integral.Integral'>
"""
super().__init__(path.join(OBSERVATORY_PATH, "Integral", "integral.json"), voevent)
super().__init__(
path.join(OBSERVATORY_PATH, "Integral", "integral.json"), voevent
)

def get_trigger_id(self):
"""
Expand All @@ -48,8 +49,8 @@ def get_trigger_id(self):
def err_to_arcminute(self):
"""
Return the error box of the voevent in arcminute
Example
Example
-------
>>> integral_weak.err_to_arcminute()
0.0656
Expand All @@ -64,4 +65,4 @@ def err_to_arcminute(self):
if instrument == "Weak" or instrument == "Wakeup" or instrument == "Refined":
return coords.err
else:
raise BadInstrument("{} is not a IceCube events".format(instrument))
raise BadInstrument("{} is not a IceCube events".format(instrument))
Loading

0 comments on commit 335ed75

Please sign in to comment.