Skip to content

Commit

Permalink
Merge pull request gwpy#1245 from rngeorge/master
Browse files Browse the repository at this point in the history
Changed fetch_catalog to work with new eventapi
  • Loading branch information
duncanmmacleod committed Jul 1, 2020
2 parents 4ba5b57 + 322fbd1 commit 3025316
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/install/index.rst
Expand Up @@ -51,7 +51,7 @@ GWpy has the following strict requirements:
|astropy|_ ``>=3.0.0``
|dqsegdb2|_
|gwdatafind|_
|gwosc-mod|_ ``>=0.4.0``
|gwosc-mod|_ ``>=0.5.3``
|h5py|_ ``>=1.3``
|ligo-segments|_ ``>=1.0.0``
|ligotimegps|_ ``>=1.2.1``
Expand Down
7 changes: 5 additions & 2 deletions examples/table/histogram.py
Expand Up @@ -31,9 +31,12 @@
from gwpy.table import EventTable
events = EventTable.fetch_open_data(
"GWTC-1-confident",
columns=("mass1", "mass2"),
columns=("mass_1_source", "mass_2_source"),
)
events.add_column(
events["mass_1_source"] + events["mass_2_source"],
name="mtotal"
)
events.add_column(events["mass1"] + events["mass2"], name="mtotal")

# and can generate a new `~gwpy.plot.Plot` using the
# :meth:`~EventTable.hist` method:
Expand Down
21 changes: 16 additions & 5 deletions examples/table/scatter.py
Expand Up @@ -33,23 +33,34 @@
from gwpy.table import EventTable
events = EventTable.fetch_open_data(
"GWTC-1-confident",
columns=("mass1", "mass2", "E_rad", "distance"),
columns=(
"mass_1_source",
"mass_2_source",
"chirp_mass_source",
"luminosity_distance"
),
)

# We can now make a scatter plot by specifying the x- and y-axis columns,
# and (optionally) the colour:

plot = events.scatter("mass1", "mass2", color="E_rad")
plot.colorbar(label="E_rad [{}]".format(r"M$_{\odot}$ c$^{2}$"))
plot = events.scatter(
"mass_1_source", "mass_2_source",
color="chirp_mass_source"
)
plot.colorbar(label="Chirp_mass [{}]".format(r"M$_{\odot}$"))
plot.show()

# We can similarly plot how the total event mass is distributed with
# distance. First we have to build the total mass (``'mtotal'``) column
# from the component masses:

events.add_column(events["mass1"] + events["mass2"], name="mtotal")
events.add_column(
events["mass_1_source"] + events["mass_2_source"],
name="mtotal"
)

# and now can make a new scatter plot:

plot = events.scatter("distance", "mtotal")
plot = events.scatter("luminosity_distance", "mtotal")
plot.show()
33 changes: 20 additions & 13 deletions gwpy/table/io/losc.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) Duncan Macleod (2019)
# Copyright (C) Duncan Macleod (2019-2020)
#
# This file is part of GWpy.
#
Expand All @@ -18,7 +18,6 @@

"""Fetch and parse an event catalog from GWOSC.
"""

import numbers
from collections import OrderedDict

Expand All @@ -28,7 +27,7 @@
from astropy import units

from gwosc.api import DEFAULT_URL as DEFAULT_GWOSC_URL
from gwosc.catalog import download as download_catalog
from gwosc.api import fetch_catalog_json

from .. import EventTable
from .utils import (
Expand Down Expand Up @@ -60,7 +59,7 @@

def _parse_unit(parameter):
try:
unit = parameter["unit_en"]
unit = parameter
except KeyError:
return
if unit in UNITS:
Expand Down Expand Up @@ -102,17 +101,24 @@ def _mask_column(col):
@read_with_columns
@read_with_selection
def fetch_catalog(catalog, host=DEFAULT_GWOSC_URL):
catalog = download_catalog(catalog, host=host)
data = catalog["data"]
parameters = catalog["parameters"]
catalog = fetch_catalog_json(catalog, host=host)
data = catalog["events"]

parameters = set(key for event in data.values() for key in event)
parameters = [x for x in parameters if not x.endswith('unit')]

unitlist = {}
for event in data.values():
dictpartial = {k: v for k, v in event.items() if k.endswith('unit')}
unitlist.update(dictpartial)

# unpack the catalogue data into a dict of columns
names = ["name"] + list(parameters.keys())
names = ["name"] + parameters
cols = {n: [] for n in names}
for event, plist in data.items():
cols["name"].append(event)
for n in names[1:]:
cols[n].append(plist[n]["best"])
cols[n].append(plist[n])

# rebuild the columns by replacing the masked values
mask = {}
Expand All @@ -130,11 +136,12 @@ def fetch_catalog(catalog, host=DEFAULT_GWOSC_URL):
)

# add column metadata
for name, parameter in parameters.items():
for name in parameters:
tab[name].mask = mask[name]
tab[name].description = parameter["name_en"]
unit = _parse_unit(parameter)
tab[name].unit = unit
tab[name].description = name
if (name+'_unit') in unitlist:
unit = _parse_unit(unitlist[name+'_unit'])
tab[name].unit = unit

# add an index on the event name
tab.add_index('name')
Expand Down
31 changes: 21 additions & 10 deletions gwpy/table/tests/test_table.py
Expand Up @@ -31,7 +31,6 @@
import pytest

from numpy import (random, isclose, dtype, asarray, all)
from numpy.ma.core import MaskedConstant

import h5py

Expand Down Expand Up @@ -742,21 +741,33 @@ def test_fetch_open_data(self):
except (URLError, SSLError) as exc: # pragma: no-cover
pytest.skip(str(exc))
assert len(table)
assert {"L_peak", "distance", "mass1"}.intersection(table.colnames)
assert {
"mass_1_source",
"luminosity_distance",
"chi_eff"
}.intersection(table.colnames)
# check unit parsing worked
assert table["distance"].unit == "Mpc"
# check that masking worked (needs table to be sorted)
gw170818 = table.loc["GW170818"]
assert isinstance(gw170818["snr_pycbc"], MaskedConstant)
assert table["luminosity_distance"].unit == "Mpc"

def test_fetch_open_data_kwargs(self):
try:
table = self.TABLE.fetch_open_data(
"GWTC-1-confident",
selection="mass1 < 5",
columns=["name", "mass1", "mass2", "distance"])
selection="mass_1_source < 5",
columns=[
"name",
"mass_1_source",
"mass_2_source",
"luminosity_distance"
]
)
except (URLError, SSLError) as exc: # pragma: no-cover
pytest.skip(str(exc))
assert len(table) == 1
assert table[0]["name"] == "GW170817"
assert set(table.colnames) == {"name", "mass1", "mass2", "distance"}
assert table[0]["name"] == "GW170817_R1"
assert set(table.colnames) == {
"name",
"mass_1_source",
"mass_2_source",
"luminosity_distance"
}
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -45,7 +45,7 @@
'astropy >= 3.0.0',
'dqsegdb2',
'gwdatafind',
'gwosc >= 0.4.0',
'gwosc >= 0.5.3',
'h5py >= 1.3',
'ligo-segments >= 1.0.0',
'ligotimegps >= 1.2.1',
Expand Down

0 comments on commit 3025316

Please sign in to comment.