Skip to content

Commit

Permalink
Modified cython code with own version of likelihood_parts function fo…
Browse files Browse the repository at this point in the history
…r waveforms whcih compute own det response
  • Loading branch information
ConWea committed Sep 13, 2022
1 parent 01c6265 commit d571721
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
35 changes: 22 additions & 13 deletions pycbc/inference/models/relbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

from .gaussian_noise import BaseGaussianNoise
from .relbin_cpu import (likelihood_parts, likelihood_parts_v,
likelihood_parts_multi, likelihood_parts_multi_v)
likelihood_parts_multi, likelihood_parts_multi_v,
likelihood_parts_det)
from .tools import DistMarg


Expand Down Expand Up @@ -346,7 +347,10 @@ def setup_antenna(self, earth_rotation, fedges):
self.mlik = likelihood_parts_multi_v
else:
atimes = self.fid_params["tc"]
self.lik = likelihood_parts
if self.det_response:
self.lik = likelihood_parts_det
else:
self.lik = likelihood_parts
self.mlik = likelihood_parts_multi
return atimes

Expand Down Expand Up @@ -491,26 +495,31 @@ def _loglr(self):
end_time = self.end_time[ifo]
times = self.antenna_time[ifo]

# project waveform to detector frame
# FIXME: Don't want to do this for LISA!!
# project waveform to detector frame if waveform does not deal
# with detector response. Otherwise, skip detector response.

hp, hc = wfs[ifo]
if self.det_response:
fp, fc = (1, 0)
dt = 0
fp = 1
dtc = -end_time
hp, hc = wfs[ifo]
hdp, hhp = self.lik(freqs, fp, dtc,
hp, hc, h00,
sdat['a0'], sdat['a1'],
sdat['b0'], sdat['b1'])
self._current_wf_parts[ifo] = (fp, dtc, hp, hc, h00)
else:
det = self.det[ifo]
fp, fc = det.antenna_pattern(p["ra"], p["dec"],
p["polarization"], times)
dt = det.time_delay_from_earth_center(p["ra"], p["dec"], times)
dtc = p["tc"] + dt - end_time

hp, hc = wfs[ifo]
hdp, hhp = self.lik(freqs, fp, fc, dtc,
hp, hc, h00,
sdat['a0'], sdat['a1'],
sdat['b0'], sdat['b1'])

self._current_wf_parts[ifo] = (fp, fc, dtc, hp, hc, h00)
hdp, hhp = self.lik(freqs, fp, fc, dtc,
hp, hc, h00,
sdat['a0'], sdat['a1'],
sdat['b0'], sdat['b1'])
self._current_wf_parts[ifo] = (fp, fc, dtc, hp, hc, h00)
hd += hdp
hh += hhp
return self.marginalize_loglr(hd, hh)
Expand Down
32 changes: 32 additions & 0 deletions pycbc/inference/models/relbin_cpu.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@ cpdef likelihood_parts(double [::1] freqs,
x0 = x0n
return hd, hh

cpdef likelihood_parts_det(double [::1] freqs,
double fp,
double dtc,
double complex[::1] hp,
double complex[::1] hc,
double complex[::1] h00,
double complex[::1] a0,
double complex[::1] a1,
double [::1] b0,
double [::1] b1,
) :
cdef size_t i
cdef double complex hd=0, r0, r0n, r1, x0, x1, x0n;
cdef double hh=0

N = freqs.shape[0]

This comment has been minimized.

Copy link
@spxiwh

spxiwh Sep 13, 2022

Contributor

You need to declare that N is an int or the code will be significantly slower.

This comment has been minimized.

Copy link
@spxiwh

spxiwh Sep 13, 2022

Contributor

(It's possible that Cython will realise this and optimize itself, but I wouldn't assume that)

for i in range(N):
r0n = (exp(-2.0j * 3.141592653 * dtc * freqs[i])
* (fp * hp[i])) / h00[i]
r1 = r0n - r0

x0n = norm(r0n)
x1 = x0n - x0

if i > 0:
hd += a0[i-1] * r0 + a1[i-1] * r1
hh += real(b0[i-1] * x0 + b1[i-1] * x1)

r0 = r0n
x0 = x0n
return hd, hh

cpdef likelihood_parts_v(double [::1] freqs,
double[::1] fp,
double[::1] fc,
Expand Down

0 comments on commit d571721

Please sign in to comment.