Skip to content

Commit

Permalink
BUG: fix version issue with TypeAlias (not included in 3.9), deprecat…
Browse files Browse the repository at this point in the history
…ed utils.nyquist_freqs func
  • Loading branch information
lzkelley committed Jan 30, 2024
1 parent 435a46c commit 8518a3e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion holodeck/gravwaves.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def is_above_hc_curve(self, ff, hc):
sel = (hc > sens_at_ff)
return sel

@utils.copy_docstring(is_above_hc_curve)
# @utils.copy_docstring(is_above_hc_curve)
def __call__(self, ff, hc):
return self.is_above_hc_curve(ff, hc)

Expand Down
4 changes: 2 additions & 2 deletions holodeck/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_minmax():

return


'''
class Test__nyquist_freqs:
def test_basic(self):
Expand Down Expand Up @@ -47,7 +47,7 @@ def test_trim_raises(self):
utils.nyquist_freqs(dur, cad, trim=trim)
return

'''

class Test_GW_Methods:

Expand Down
78 changes: 62 additions & 16 deletions holodeck/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import subprocess
import warnings
from pathlib import Path
from typing import Optional, Tuple, Union, List, Callable, TypeVar, Any, TypeAlias # , Sequence,
from typing import Optional, Tuple, Union, List, Callable, TypeVar, Any # , TypeAlias # , Sequence,

try:
from typing import ParamSpec
except ImportError:
from typing_extensions import ParamSpec
# try:
# from typing import ParamSpec
# except ImportError:
# from typing_extensions import ParamSpec

import h5py
import numba
Expand Down Expand Up @@ -73,21 +73,22 @@ def modify(self, base: object):
pass


T = TypeVar('T')
P = ParamSpec('P')
WrappedFuncDeco: TypeAlias = Callable[[Callable[P, T]], Callable[P, T]]
# T = TypeVar('T')
# P = ParamSpec('P')
# WrappedFuncDeco: TypeAlias = Callable[[Callable[P, T]], Callable[P, T]]
# WrappedFuncDeco: 'TypeAlias' = Tuple[float, float]

def copy_docstring(copy_func: Callable[..., Any]) -> WrappedFuncDeco[P, T]:
"""Copies the doc string of the given function to the wrapped function.
# def copy_docstring(copy_func: Callable[..., Any]) -> WrappedFuncDeco[P, T]:
# """Copies the doc string of the given function to the wrapped function.

see: https://stackoverflow.com/a/68901244/230468
"""
# see: https://stackoverflow.com/a/68901244/230468
# """

def wrapped(func: Callable[P, T]) -> Callable[P, T]:
func.__doc__ = copy_func.__doc__
return func
# def wrapped(func: Callable[P, T]) -> Callable[P, T]:
# func.__doc__ = copy_func.__doc__
# return func

return wrapped
# return wrapped


# =================================================================================================
Expand Down Expand Up @@ -834,6 +835,35 @@ def ndinterp(xx, xvals, yvals, xlog=False, ylog=False):


def pta_freqs(dur=16.03*YR, num=40, cad=None):
"""Get Fourier frequency bin specifications for the given parameters.
Arguments
---------
dur : float,
Total observing duration, which determines the minimum sensitive frequency, ``1/dur``.
Typically `dur` should be given in units of [sec], such that the returned frequencies are
in units of [1/sec] = [Hz]
num : int,
Number of frequency bins. If `cad` is not None, then the number of frequency bins is
determined by `cad` and the `num` value is disregarded.
cad : float or `None`,
Cadence of observations, which determines the maximum sensitive frequency (i.e. the Nyquist
frequency). If `cad` is not given, then `num` frequency bins are constructed.
Returns
-------
cents : (F,) ndarray
Bin-center frequencies for `F` bins. The frequency bin centers are at:
``F_i = (i + 1.5) / dur`` for i between 0 and `num-1`.
The number of frequency bins, `F` is the argument `num`,
or determined by `cad` if it is given.
edges : (F+1,) ndarray
Bin-edge frequencies for `F` bins, i.e. `F+1` bin edges. The frequency bin edges are at:
``F_i = (i + 1) / dur`` for i between 0 and `num`.
The number of frequency bins, `F` is the argument `num`,
or determined by `cad` if it is given.
"""
fmin = 1.0 / dur
if cad is not None:
num = dur / (2.0 * cad)
Expand Down Expand Up @@ -2389,3 +2419,19 @@ def _get_subclass_instance(value, default, superclass):
return value


#! DEPRECATED
def nyquist_freqs(dur, cad):
"""DEPRECATED. Use `holodeck.utils.pta_freqs` instead.
"""
msg = ""
old_name = "nyquist_freqs"
new_name = "pta_freqs"
_frame = inspect.currentframe().f_back
file_name = inspect.getfile(_frame.f_code)
fline = _frame.f_lineno
msg = f"{file_name}({fline}):{old_name} ==> {new_name}" + (len(msg) > 0) * " | " + msg
warnings.warn_explicit(msg, category=DeprecationWarning, filename=file_name, lineno=fline)
log.warning(f"DEPRECATION: {msg}", exc_info=True)
return pta_freqs(dur=dur, num=None, cad=cad)[0]


8 changes: 4 additions & 4 deletions notebooks/semi-analytic-models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"# Plot the median GWB spectrum\n",
"ax.plot(xx, np.median(gwb, axis=-1), 'k-')\n",
"\n",
"# Plot `nsamp` random spectra \n",
"# Plot `nsamp` random spectra\n",
"nsamp = np.min([nsamp, gwb.shape[1]])\n",
"idx = np.random.choice(gwb.shape[1], nsamp, replace=False)\n",
"ax.plot(xx, gwb[:, idx], 'k-', lw=1.0, alpha=0.1)\n",
Expand All @@ -148,7 +148,7 @@
" percs = pp / 2\n",
" percs = [50 - percs, 50 + percs]\n",
" ax.fill_between(xx, *np.percentile(gwb, percs, axis=-1), alpha=0.25, color='b')\n",
" \n",
"\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -301,7 +301,7 @@
" cc, = ax.plot(norm_list, med, label=aa)\n",
" cc = cc.get_color()\n",
" ax.fill_between(norm_list, *np.percentile(dd, [25, 75], axis=-1), color=cc, alpha=0.15)\n",
" \n",
"\n",
"plt.legend(title='M-MBulge Slope')\n",
"plt.show()"
]
Expand Down Expand Up @@ -392,7 +392,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
"version": "3.11.7"
},
"toc": {
"base_numbering": 1,
Expand Down

0 comments on commit 8518a3e

Please sign in to comment.