Repository: skypyproject/skypy
Pull request #204 (2020-06-30 14:03:29)

added abadie table iii model
## Description
Module for compact binary merger rates as a function of a galaxy's blue light luminosity, from https://arxiv.org/abs/1003.248 Table III.

Note that the test is not a good test, it was just all I could think of for now.

## Checklist
- [X] Follow the [Contributor Guidelines](https://github.com/skypyproject/skypy/blob/master/CONTRIBUTING.md)
- [X] Write unit tests
- [X] Write documentation strings
- [x] Assign someone from your working team to review this pull request
- [x] Assign someone from the infrastructure team to review this pull request


skypybot: Hello @itrharrison! Thanks for opening this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:

* In the file [`skypy/gravitational_wave/binary_merger_rate.py`](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py):

> [Line 1:1](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L1): [W293](https://duckduckgo.com/?q=pep8%20W293) blank line contains whitespace
> [Line 10:1](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L10): [E302](https://duckduckgo.com/?q=pep8%20E302) expected 2 blank lines, found 1
> [Line 10:80](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L10): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (81 > 79 characters)
> [Line 11:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L11): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 13:80](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L13): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (83 > 79 characters)
> [Line 18:80](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L18): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (84 > 79 characters)
> [Line 36:1](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L36): [W293](https://duckduckgo.com/?q=pep8%20W293) blank line contains whitespace
> [Line 63:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L63): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 64:34](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L64): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 64:43](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L64): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 65:49](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L65): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 66:44](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L66): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 67:43](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L67): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 68:34](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L68): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 68:43](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L68): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 69:49](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L69): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 70:44](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L70): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 71:34](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L71): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 71:43](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L71): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 72:49](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L72): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 73:44](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L73): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 76:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L76): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 76:80](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L76): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (84 > 79 characters)
> [Line 78:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L78): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 78:21](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/binary_merger_rate.py#L78): [W292](https://duckduckgo.com/?q=pep8%20W292) no newline at end of file

* In the file [`skypy/gravitational_wave/tests/test_binary_merger_rates.py`](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py):

> [Line 4:1](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py#L4): [E302](https://duckduckgo.com/?q=pep8%20E302) expected 2 blank lines, found 1
> [Line 6:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py#L6): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 9:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py#L9): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 13:3](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py#L13): [E111](https://duckduckgo.com/?q=pep8%20E111) indentation is not a multiple of four
> [Line 13:26](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py#L13): [E225](https://duckduckgo.com/?q=pep8%20E225) missing whitespace around operator
> [Line 13:45](https://github.com/skypyproject/skypy/blob/b2edf48d3e289c5e7eb95e5f4307d0c219f93017/skypy/gravitational_wave/tests/test_binary_merger_rates.py#L13): [W292](https://duckduckgo.com/?q=pep8%20W292) no newline at end of file



Patch:
diff --git a/skypy/gravitational_wave/__init__.py b/skypy/gravitational_wave/__init__.py
index 6013fc7..d0b89fb 100644
--- a/skypy/gravitational_wave/__init__.py
+++ b/skypy/gravitational_wave/__init__.py
@@ -1,4 +1,16 @@
 """
 This module contains methods that model the properties of gravitational wave
 populations.
+
+Merger Rates
+================
+
+.. autosummary::
+   :nosignatures:
+   :toctree: ../api/
+
+   b_band_merger_rate
+
 """
+
+from .merger_rate import *  # noqa F401,F403
diff --git a/skypy/gravitational_wave/merger_rate.py b/skypy/gravitational_wave/merger_rate.py
new file mode 100644
index 0000000..ba1ae6b
--- /dev/null
+++ b/skypy/gravitational_wave/merger_rate.py
@@ -0,0 +1,91 @@
+
+r"""Binary merger rate module.
+This module provides functions to calculate compact binary merger rates
+for individual galaxies.
+
+"""
+
+from astropy import constants, units
+
+
+__all__ = [
+    'b_band_merger_rate',
+]
+
+
+abadie_table_III = {
+    'NS-NS': {
+        'low': 0.6,
+        'realistic': 60,
+        'high': 600,
+        'max': 2000},
+    'NS-BH': {
+        'low': 0.03,
+        'realistic': 2,
+        'high': 60},
+    'BH-BH': {
+        'low': 0.006,
+        'realistic': 0.2,
+        'high': 20}
+}
+
+
+def b_band_merger_rate(luminosity,
+                       population='NS-NS',
+                       optimism='low'):
+
+    r"""Model of Abadie et al (2010), Table III
+
+    Compact binary merger rates as a linear function of a galaxies
+    B-band luminosity.
+
+    Parameters
+    ----------
+    luminosity : (ngal,) array-like
+        The B-band luminosity of the galaxies to generate merger
+        rates for, in units of solar luminosity.
+    population : {'NS-NS', 'NS-BH', 'BH-BH'}
+        Compact binary population to get rate for.
+        'NS-NS' is neutron star - neutron star
+        'NS-BH' is neutron star - black hole
+        'BH-BH' is black hole - black hole
+    optimism : {'low', 'realistic', 'high'}
+        Optimism of predicted merger rates.
+        For 'NS-NS' there is an extra option 'max'.
+
+    Returns
+    -------
+    merger_rate : array_like
+        Merger rates for the galaxies in units of year^-1
+
+    Notes
+    -----
+
+    References
+    ----------
+    .. Abadie et al. 2010, Classical and Quantum Gravity,
+        Volume 27, Issue 17, article id. 173001 (2010)
+        https://arxiv.org/abs/1003.2480
+
+    Examples
+    --------
+    >>> from skypy.galaxy.luminosity import herbel_luminosities
+    >>> from skypy.gravitational_wave import b_band_merger_rate
+
+    Sample 100 luminosity values at redshift z = 1.0 with
+    a_m = -0.9408582, b_m = -20.40492365, alpha = -1.3.
+
+    >>> luminosities = herbel_luminosities(1.0, -1.3, -0.9408582,
+    ...                                    -20.40492365, size=100)
+
+    Generate merger rates for these luminosities.
+
+    >>> rates = b_band_merger_rate(luminosities,
+    ...                            population='NS-NS',
+    ...                            optimism='low')
+
+    """
+
+    # Convert luminosity to units of L_10 defined in Abadie et. al. 2010
+    L_10 = luminosity * constants.L_sun.to_value('erg/s') / (1e10 * 2.16e33)
+    return abadie_table_III[population][optimism] * L_10 / units.year


Test patch:
diff --git a/skypy/gravitational_wave/tests/test_merger_rate.py b/skypy/gravitational_wave/tests/test_merger_rate.py
new file mode 100644
index 0000000..956e328
--- /dev/null
+++ b/skypy/gravitational_wave/tests/test_merger_rate.py
@@ -0,0 +1,21 @@
+import numpy as np
+from astropy import constants, units
+from skypy.galaxy.luminosity import herbel_luminosities
+from skypy.gravitational_wave import b_band_merger_rate
+from skypy.gravitational_wave.merger_rate import abadie_table_III
+
+
+def test_abadie_rates():
+
+    # Check the number of merger rates returned
+    z, alpha, a_m, b_m = 1.0, -1.3, -0.9408582, -20.40492365
+    luminosity = herbel_luminosities(z, alpha, a_m, b_m, size=1000)
+    rates = b_band_merger_rate(luminosity, population='NS-NS', optimism='low')
+    assert len(rates) == len(luminosity)
+
+    # Test that a luminosity of L_10 (in units of solar luminosity) returns a
+    # rate that matches the value in Abadie Table III
+    L_10 = 1e10 * 2.16e33 / constants.L_sun.to_value('erg/s')
+    L_B_rate = b_band_merger_rate(L_10, population='NS-NS', optimism='low')
+    table_value = abadie_table_III['NS-NS']['low']
+    assert np.isclose(L_B_rate.to_value(1/units.year), table_value, rtol=1e-5)