lisaanalysistools 1.0.10__cp312-cp312-macosx_10_13_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of lisaanalysistools might be problematic. Click here for more details.

Files changed (43) hide show
  1. lisaanalysistools-1.0.10.dist-info/LICENSE +201 -0
  2. lisaanalysistools-1.0.10.dist-info/METADATA +101 -0
  3. lisaanalysistools-1.0.10.dist-info/RECORD +43 -0
  4. lisaanalysistools-1.0.10.dist-info/WHEEL +5 -0
  5. lisaanalysistools-1.0.10.dist-info/top_level.txt +1 -0
  6. lisatools/__init__.py +0 -0
  7. lisatools/_version.py +4 -0
  8. lisatools/analysiscontainer.py +451 -0
  9. lisatools/cutils/__init__.py +0 -0
  10. lisatools/cutils/detector_cpu.cpython-312-darwin.so +0 -0
  11. lisatools/cutils/include/Detector.hpp +84 -0
  12. lisatools/cutils/include/__init__.py +0 -0
  13. lisatools/cutils/include/global.hpp +28 -0
  14. lisatools/cutils/src/Detector.cpp +307 -0
  15. lisatools/cutils/src/Detector.cu +307 -0
  16. lisatools/cutils/src/__init__.py +0 -0
  17. lisatools/cutils/src/pycppdetector.pyx +255 -0
  18. lisatools/datacontainer.py +309 -0
  19. lisatools/detector.py +704 -0
  20. lisatools/diagnostic.py +977 -0
  21. lisatools/sampling/__init__.py +0 -0
  22. lisatools/sampling/likelihood.py +882 -0
  23. lisatools/sampling/moves/__init__.py +0 -0
  24. lisatools/sampling/moves/skymodehop.py +110 -0
  25. lisatools/sampling/prior.py +646 -0
  26. lisatools/sampling/stopping.py +320 -0
  27. lisatools/sampling/utility.py +411 -0
  28. lisatools/sensitivity.py +899 -0
  29. lisatools/sources/__init__.py +6 -0
  30. lisatools/sources/bbh/__init__.py +1 -0
  31. lisatools/sources/bbh/waveform.py +91 -0
  32. lisatools/sources/defaultresponse.py +36 -0
  33. lisatools/sources/emri/__init__.py +1 -0
  34. lisatools/sources/emri/waveform.py +79 -0
  35. lisatools/sources/gb/__init__.py +1 -0
  36. lisatools/sources/gb/waveform.py +67 -0
  37. lisatools/sources/utils.py +456 -0
  38. lisatools/sources/waveformbase.py +41 -0
  39. lisatools/stochastic.py +291 -0
  40. lisatools/utils/__init__.py +0 -0
  41. lisatools/utils/constants.py +40 -0
  42. lisatools/utils/pointeradjust.py +106 -0
  43. lisatools/utils/utility.py +245 -0
@@ -0,0 +1,899 @@
1
+ from __future__ import annotations
2
+ import warnings
3
+ from abc import ABC
4
+ from typing import Any, Tuple, Optional, List
5
+ from copy import deepcopy
6
+ import os
7
+
8
+ import math
9
+ import numpy as np
10
+ from scipy import interpolate
11
+ import matplotlib.pyplot as plt
12
+
13
+ try:
14
+ import cupy as cp
15
+
16
+ except (ModuleNotFoundError, ImportError):
17
+ import numpy as cp
18
+
19
+ from . import detector as lisa_models
20
+ from .utils.utility import AET
21
+ from .utils.constants import *
22
+ from .stochastic import (
23
+ StochasticContribution,
24
+ FittedHyperbolicTangentGalacticForeground,
25
+ )
26
+
27
+ """
28
+ The sensitivity code is heavily based on an original code by Stas Babak, Antoine Petiteau for the LDC team.
29
+
30
+ """
31
+
32
+
33
+ class Sensitivity(ABC):
34
+ """Base Class for PSD information.
35
+
36
+ The initialization function is only needed if using a file input.
37
+
38
+ """
39
+
40
+ channel: str = None
41
+
42
+ @staticmethod
43
+ def transform(
44
+ f: float | np.ndarray,
45
+ Spm: float | np.ndarray,
46
+ Sop: float | np.ndarray,
47
+ **kwargs: dict,
48
+ ) -> float | np.ndarray:
49
+ """Transform from the base sensitivity functions to the TDI PSDs.
50
+
51
+ Args:
52
+ f: Frequency array.
53
+ Spm: Acceleration term.
54
+ Sop: OMS term.
55
+ **kwargs: For interoperability.
56
+
57
+ Returns:
58
+ Transformed TDI PSD values.
59
+
60
+ """
61
+ raise NotImplementedError
62
+
63
+ @classmethod
64
+ def get_Sn(
65
+ cls,
66
+ f: float | np.ndarray,
67
+ model: Optional[lisa_models.LISAModel | str] = lisa_models.scirdv1,
68
+ **kwargs: dict,
69
+ ) -> float | np.ndarray:
70
+ """Calculate the PSD
71
+
72
+ Args:
73
+ f: Frequency array.
74
+ model: Noise model. Object of type :class:`lisa_models.LISAModel`.
75
+ It can also be a string corresponding to one of the stock models.
76
+ The model object must include attributes for ``Soms_d`` (shot noise)
77
+ and ``Sa_a`` (acceleration noise) or a spline as attribute ``Sn_spl``.
78
+ In the case of a spline, this must be a dictionary with
79
+ channel names as keys and callable PSD splines. For example,
80
+ if using ``scipy.interpolate.CubicSpline``, an input option
81
+ can be:
82
+
83
+ ```
84
+ noise_model.Sn_spl = {
85
+ "A": CubicSpline(f, Sn_A)),
86
+ "E": CubicSpline(f, Sn_E)),
87
+ "T": CubicSpline(f, Sn_T))
88
+ }
89
+ ```
90
+ **kwargs: For interoperability.
91
+
92
+ Returns:
93
+ PSD values.
94
+
95
+ """
96
+ # spline or stock computation
97
+ if hasattr(model, "Sn_spl") and model.Sn_spl is not None:
98
+ spl = model.Sn_spl
99
+ if cls.channel not in spl:
100
+ raise ValueError("Calling a channel that is not available.")
101
+
102
+ Sout = spl[cls.channel](f)
103
+
104
+ else:
105
+ model = lisa_models.check_lisa_model(model)
106
+ assert hasattr(model, "Soms_d") and hasattr(model, "Sa_a")
107
+
108
+ # get noise values
109
+ Spm, Sop = model.lisanoises(f)
110
+
111
+ # transform as desired for TDI combination
112
+ Sout = cls.transform(f, Spm, Sop, **kwargs)
113
+
114
+ # will add zero if ignored
115
+ stochastic_contribution = cls.stochastic_transform(
116
+ f, cls.get_stochastic_contribution(f, **kwargs), **kwargs
117
+ )
118
+
119
+ Sout += stochastic_contribution
120
+ return Sout
121
+
122
+ @classmethod
123
+ def get_stochastic_contribution(
124
+ cls,
125
+ f: float | np.ndarray,
126
+ stochastic_params: Optional[tuple] = (),
127
+ stochastic_kwargs: Optional[dict] = {},
128
+ stochastic_function: Optional[StochasticContribution | str] = None,
129
+ ) -> float | np.ndarray:
130
+ """Calculate contribution from stochastic signal.
131
+
132
+ This function directs and wraps the calculation of and returns
133
+ the stochastic signal. The ``stochastic_function`` calculates the
134
+ sensitivity contribution. The ``transform_factor`` can transform that
135
+ output to the correct TDI contribution.
136
+
137
+ Args:
138
+ f: Frequency array.
139
+ stochastic_params: Parameters (arguments) to feed to ``stochastic_function``.
140
+ stochastic_kwargs: Keyword arguments to feeed to ``stochastic_function``.
141
+ stochastic_function: Stochastic class or string name of stochastic class. Takes ``stochastic_args`` and ``stochastic_kwargs``.
142
+ If ``None``, it uses :class:`FittedHyperbolicTangentGalacticForeground`.
143
+
144
+ Returns:
145
+ Contribution from stochastic signal.
146
+
147
+
148
+ """
149
+
150
+ if isinstance(f, float):
151
+ f = np.ndarray([f])
152
+ squeeze = True
153
+ else:
154
+ squeeze = False
155
+
156
+ sgal = np.zeros_like(f)
157
+
158
+ if (
159
+ (stochastic_params != () and stochastic_params is not None)
160
+ or (stochastic_kwargs != {} and stochastic_kwargs is not None)
161
+ or stochastic_function is not None
162
+ ):
163
+ if stochastic_function is None:
164
+ stochastic_function = FittedHyperbolicTangentGalacticForeground
165
+ assert len(stochastic_params) == 1
166
+
167
+ sgal[:] = stochastic_function.get_Sh(
168
+ f, *stochastic_params, **stochastic_kwargs
169
+ )
170
+
171
+ if squeeze:
172
+ sgal = sgal.squeeze()
173
+ return sgal
174
+
175
+ @staticmethod
176
+ def stochastic_transform(
177
+ f: float | np.ndarray, Sh: float | np.ndarray, **kwargs: dict
178
+ ) -> float | np.ndarray:
179
+ """Transform from the base stochastic functions to the TDI PSDs.
180
+
181
+ **Note**: If not implemented, the transform will return the input.
182
+
183
+ Args:
184
+ f: Frequency array.
185
+ Sh: Power spectral density in stochastic term.
186
+ **kwargs: For interoperability.
187
+
188
+ Returns:
189
+ Transformed TDI PSD values.
190
+
191
+ """
192
+ return Sh
193
+
194
+
195
+ class X1TDISens(Sensitivity):
196
+ channel: str = "X"
197
+
198
+ @staticmethod
199
+ def transform(
200
+ f: float | np.ndarray,
201
+ Spm: float | np.ndarray,
202
+ Sop: float | np.ndarray,
203
+ **kwargs: dict,
204
+ ) -> float | np.ndarray:
205
+ __doc__ = (
206
+ "Transform from the base sensitivity functions to the XYZ TDI PSDs.\n\n"
207
+ + Sensitivity.transform.__doc__.split("PSDs.\n\n")[-1]
208
+ )
209
+
210
+ x = 2.0 * np.pi * lisaLT * f
211
+ return 16.0 * np.sin(x) ** 2 * (2.0 * (1.0 + np.cos(x) ** 2) * Spm + Sop)
212
+
213
+ @staticmethod
214
+ def stochastic_transform(
215
+ f: float | np.ndarray, Sh: float | np.ndarray, **kwargs: dict
216
+ ) -> float | np.ndarray:
217
+ __doc__ = (
218
+ "Transform from the base stochastic functions to the XYZ stochastic TDI information.\n\n"
219
+ + Sensitivity.stochastic_transform.__doc__.split("PSDs.\n\n")[-1]
220
+ )
221
+ x = 2.0 * np.pi * lisaLT * f
222
+ t = 4.0 * x**2 * np.sin(x) ** 2
223
+ return Sh * t
224
+
225
+
226
+ class Y1TDISens(X1TDISens):
227
+ channel: str = "Y"
228
+ __doc__ = X1TDISens.__doc__
229
+ pass
230
+
231
+
232
+ class Z1TDISens(X1TDISens):
233
+ channel: str = "Z"
234
+ __doc__ = X1TDISens.__doc__
235
+ pass
236
+
237
+
238
+ class XY1TDISens(Sensitivity):
239
+ channel: str = "XY"
240
+
241
+ @staticmethod
242
+ def transform(
243
+ f: float | np.ndarray,
244
+ Spm: float | np.ndarray,
245
+ Sop: float | np.ndarray,
246
+ **kwargs: dict,
247
+ ) -> float | np.ndarray:
248
+ __doc__ = (
249
+ "Transform from the base sensitivity functions to the XYZ TDI PSDs.\n\n"
250
+ + Sensitivity.transform.__doc__.split("PSDs.\n\n")[-1]
251
+ )
252
+
253
+ x = 2.0 * np.pi * lisaLT * f
254
+ ## TODO Check the acceleration noise term
255
+ return -4.0 * np.sin(2 * x) * np.sin(x) * (Sop + 4.0 * Spm)
256
+
257
+ @staticmethod
258
+ def stochastic_transform(
259
+ f: float | np.ndarray, Sh: float | np.ndarray, **kwargs: dict
260
+ ) -> float | np.ndarray:
261
+ __doc__ = (
262
+ "Transform from the base stochastic functions to the XYZ stochastic TDI information.\n\n"
263
+ + Sensitivity.stochastic_transform.__doc__.split("PSDs.\n\n")[-1]
264
+ )
265
+ x = 2.0 * np.pi * lisaLT * f
266
+ # TODO: check these functions
267
+ # GB = -0.5 of X
268
+ t = -0.5 * (4.0 * x**2 * np.sin(x) ** 2)
269
+ return Sh * t
270
+
271
+
272
+ class ZX1TDISens(XY1TDISens):
273
+ channel: str = "ZX"
274
+ __doc__ = XY1TDISens.__doc__
275
+ pass
276
+
277
+
278
+ class YZ1TDISens(XY1TDISens):
279
+ channel: str = "YZ"
280
+ __doc__ = XY1TDISens.__doc__
281
+ pass
282
+
283
+
284
+ class X2TDISens(Sensitivity):
285
+ channel: str = "X"
286
+
287
+ @staticmethod
288
+ def transform(
289
+ f: float | np.ndarray,
290
+ Spm: float | np.ndarray,
291
+ Sop: float | np.ndarray,
292
+ **kwargs: dict,
293
+ ) -> float | np.ndarray:
294
+ __doc__ = (
295
+ "Transform from the base sensitivity functions to the XYZ TDI PSDs.\n\n"
296
+ + Sensitivity.transform.__doc__.split("PSDs.\n\n")[-1]
297
+ )
298
+
299
+ x = 2.0 * np.pi * lisaLT * f
300
+ ## TODO Check the acceleration noise term
301
+ return (64.0 * np.sin(x) ** 2 * np.sin(2 * x) ** 2 * Sop) + (
302
+ 256.0 * (3 + np.cos(2 * x)) * np.cos(x) ** 2 * np.sin(x) ** 4 * Spm
303
+ )
304
+
305
+ @staticmethod
306
+ def stochastic_transform(
307
+ f: float | np.ndarray, Sh: float | np.ndarray, **kwargs: dict
308
+ ) -> float | np.ndarray:
309
+ __doc__ = (
310
+ "Transform from the base stochastic functions to the XYZ stochastic TDI information.\n\n"
311
+ + Sensitivity.stochastic_transform.__doc__.split("PSDs.\n\n")[-1]
312
+ )
313
+ x = 2.0 * np.pi * lisaLT * f
314
+ # TODO: check these functions for TDI2
315
+ t = 4.0 * x**2 * np.sin(x) ** 2
316
+ return Sh * t
317
+
318
+
319
+ class Y2TDISens(X2TDISens):
320
+ channel: str = "Y"
321
+ __doc__ = X2TDISens.__doc__
322
+ pass
323
+
324
+
325
+ class Z2TDISens(X2TDISens):
326
+ channel: str = "Z"
327
+ __doc__ = X2TDISens.__doc__
328
+ pass
329
+
330
+
331
+ class A1TDISens(Sensitivity):
332
+ channel: str = "A"
333
+
334
+ @staticmethod
335
+ def transform(
336
+ f: float | np.ndarray,
337
+ Spm: float | np.ndarray,
338
+ Sop: float | np.ndarray,
339
+ **kwargs: dict,
340
+ ) -> float | np.ndarray:
341
+ __doc__ = (
342
+ "Transform from the base sensitivity functions to the A,E TDI PSDs.\n\n"
343
+ + Sensitivity.transform.__doc__.split("PSDs.\n\n")[-1]
344
+ )
345
+
346
+ x = 2.0 * np.pi * lisaLT * f
347
+ Sa = (
348
+ 8.0
349
+ * np.sin(x) ** 2
350
+ * (
351
+ 2.0 * Spm * (3.0 + 2.0 * np.cos(x) + np.cos(2 * x))
352
+ + Sop * (2.0 + np.cos(x))
353
+ )
354
+ )
355
+
356
+ return Sa
357
+
358
+ @staticmethod
359
+ def stochastic_transform(
360
+ f: float | np.ndarray, Sh: float | np.ndarray, **kwargs: dict
361
+ ) -> float | np.ndarray:
362
+ __doc__ = (
363
+ "Transform from the base stochastic functions to the XYZ stochastic TDI information.\n\n"
364
+ + Sensitivity.stochastic_transform.__doc__.split("PSDs.\n\n")[-1]
365
+ )
366
+ x = 2.0 * np.pi * lisaLT * f
367
+ t = 4.0 * x**2 * np.sin(x) ** 2
368
+ return 1.5 * (Sh * t)
369
+
370
+
371
+ class E1TDISens(A1TDISens):
372
+ channel: str = "E"
373
+ __doc__ = A1TDISens.__doc__
374
+ pass
375
+
376
+
377
+ class T1TDISens(Sensitivity):
378
+ channel: str = "T"
379
+
380
+ @staticmethod
381
+ def transform(
382
+ f: float | np.ndarray,
383
+ Spm: float | np.ndarray,
384
+ Sop: float | np.ndarray,
385
+ **kwargs: dict,
386
+ ) -> float | np.ndarray:
387
+ __doc__ = (
388
+ "Transform from the base sensitivity functions to the T TDI PSDs.\n\n"
389
+ + Sensitivity.transform.__doc__.split("PSDs.\n\n")[-1]
390
+ )
391
+
392
+ x = 2.0 * np.pi * lisaLT * f
393
+ return (
394
+ 16.0 * Sop * (1.0 - np.cos(x)) * np.sin(x) ** 2
395
+ + 128.0 * Spm * np.sin(x) ** 2 * np.sin(0.5 * x) ** 4
396
+ )
397
+
398
+ @staticmethod
399
+ def stochastic_transform(
400
+ f: float | np.ndarray, Sh: float | np.ndarray, **kwargs: dict
401
+ ) -> float | np.ndarray:
402
+ __doc__ = (
403
+ "Transform from the base stochastic functions to the XYZ stochastic TDI information.\n\n"
404
+ + Sensitivity.stochastic_transform.__doc__.split("PSDs.\n\n")[-1]
405
+ )
406
+ x = 2.0 * np.pi * lisaLT * f
407
+ t = 4.0 * x**2 * np.sin(x) ** 2
408
+ return 0.0 * (Sh * t)
409
+
410
+
411
+ class LISASens(Sensitivity):
412
+ @classmethod
413
+ def get_Sn(
414
+ cls,
415
+ f: float | np.ndarray,
416
+ model: Optional[lisa_models.LISAModel | str] = lisa_models.scirdv1,
417
+ average: bool = True,
418
+ **kwargs: dict,
419
+ ) -> float | np.ndarray:
420
+ """Compute the base LISA sensitivity function.
421
+
422
+ Args:
423
+ f: Frequency array.
424
+ model: Noise model. Object of type :class:`lisa_models.LISAModel`. It can also be a string corresponding to one of the stock models.
425
+ average: Whether to apply averaging factors to sensitivity curve.
426
+ Antenna response: ``av_resp = np.sqrt(5) if average else 1.0``
427
+ Projection effect: ``Proj = 2.0 / np.sqrt(3) if average else 1.0``
428
+ **kwargs: Keyword arguments to pass to :func:`get_stochastic_contribution`. # TODO: fix
429
+
430
+ Returns:
431
+ Sensitivity array.
432
+
433
+ """
434
+ model = lisa_models.check_lisa_model(model)
435
+ assert hasattr(model, "Soms_d") and hasattr(model, "Sa_a")
436
+
437
+ # get noise values
438
+ Sa_d, Sop = model.lisanoises(f, unit="displacement")
439
+
440
+ all_m = np.sqrt(4.0 * Sa_d + Sop)
441
+ ## Average the antenna response
442
+ av_resp = np.sqrt(5) if average else 1.0
443
+
444
+ ## Projection effect
445
+ Proj = 2.0 / np.sqrt(3) if average else 1.0
446
+
447
+ ## Approximative transfer function
448
+ f0 = 1.0 / (2.0 * lisaLT)
449
+ a = 0.41
450
+ T = np.sqrt(1 + (f / (a * f0)) ** 2)
451
+ sens = (av_resp * Proj * T * all_m / lisaL) ** 2
452
+
453
+ # will add zero if ignored
454
+ sens += cls.get_stochastic_contribution(f, **kwargs)
455
+ return sens
456
+
457
+
458
+ class CornishLISASens(LISASens):
459
+ """PSD from https://arxiv.org/pdf/1803.01944.pdf
460
+
461
+ Power Spectral Density for the LISA detector assuming it has been active for a year.
462
+ I found an analytic version in one of Niel Cornish's paper which he submitted to the arXiv in
463
+ 2018. I evaluate the PSD at the frequency bins found in the signal FFT.
464
+
465
+ PSD obtained from: https://arxiv.org/pdf/1803.01944.pdf
466
+
467
+ """
468
+
469
+ @staticmethod
470
+ def get_Sn(
471
+ f: float | np.ndarray, average: bool = True, **kwargs: dict
472
+ ) -> float | np.ndarray:
473
+ # TODO: documentation here
474
+
475
+ sky_averaging_constant = 20.0 / 3.0 if average else 1.0
476
+
477
+ L = 2.5 * 10**9 # Length of LISA arm
478
+ f0 = 19.09 * 10 ** (-3) # transfer frequency
479
+
480
+ # Optical Metrology Sensor
481
+ Poms = ((1.5e-11) * (1.5e-11)) * (1 + np.power((2e-3) / f, 4))
482
+
483
+ # Acceleration Noise
484
+ Pacc = (
485
+ (3e-15)
486
+ * (3e-15)
487
+ * (1 + (4e-4 / f) * (4e-4 / f))
488
+ * (1 + np.power(f / (8e-3), 4))
489
+ )
490
+
491
+ # constants for Galactic background after 1 year of observation
492
+ alpha = 0.171
493
+ beta = 292
494
+ k = 1020
495
+ gamma = 1680
496
+ f_k = 0.00215
497
+
498
+ # Galactic background contribution
499
+ Sc = (
500
+ 9e-45
501
+ * np.power(f, -7 / 3)
502
+ * np.exp(-np.power(f, alpha) + beta * f * np.sin(k * f))
503
+ * (1 + np.tanh(gamma * (f_k - f)))
504
+ )
505
+
506
+ # PSD
507
+ PSD = (sky_averaging_constant) * (
508
+ (10 / (3 * L * L))
509
+ * (Poms + (4 * Pacc) / (np.power(2 * np.pi * f, 4)))
510
+ * (1 + 0.6 * (f / f0) * (f / f0))
511
+ + Sc
512
+ )
513
+
514
+ return PSD
515
+
516
+
517
+ class FlatPSDFunction(LISASens):
518
+ """White Noise PSD function."""
519
+
520
+ @staticmethod
521
+ def get_Sn(f: float | np.ndarray, val: float, **kwargs: dict) -> float | np.ndarray:
522
+ # TODO: documentation here
523
+ out = np.full_like(f, val)
524
+ if isinstance(f, float):
525
+ out = out.item()
526
+ return out
527
+
528
+
529
+ class SensitivityMatrix:
530
+ """Container to hold sensitivity information.
531
+
532
+ Args:
533
+ f: Frequency array.
534
+ sens_mat: Input sensitivity list. The shape of the nested lists should represent the shape of the
535
+ desired matrix. Each entry in the list must be an array, :class:`Sensitivity`-derived object,
536
+ or a string corresponding to the :class:`Sensitivity` object.
537
+ **sens_kwargs: Keyword arguments to pass to :func:`Sensitivity.get_Sn`.
538
+
539
+ """
540
+
541
+ def __init__(
542
+ self,
543
+ f: np.ndarray,
544
+ sens_mat: (
545
+ List[List[np.ndarray | Sensitivity]]
546
+ | List[np.ndarray | Sensitivity]
547
+ | np.ndarray
548
+ | Sensitivity
549
+ ),
550
+ *sens_args: tuple,
551
+ **sens_kwargs: dict,
552
+ ) -> None:
553
+ self.frequency_arr = f
554
+ self.data_length = len(self.frequency_arr)
555
+ self.sens_args = sens_args
556
+ self.sens_kwargs = sens_kwargs
557
+ self.sens_mat = sens_mat
558
+
559
+ @property
560
+ def frequency_arr(self) -> np.ndarray:
561
+ return self._frequency_arr
562
+
563
+ @frequency_arr.setter
564
+ def frequency_arr(self, frequency_arr: np.ndarray) -> None:
565
+ assert frequency_arr.dtype == np.float64 or frequency_arr.dtype == float
566
+ assert frequency_arr.ndim == 1
567
+ self._frequency_arr = frequency_arr
568
+
569
+ def update_frequency_arr(self, frequency_arr: np.ndarray) -> None:
570
+ """Update class with new frequency array.
571
+
572
+ Args:
573
+ frequency_arr: Frequency array.
574
+
575
+ """
576
+ self.frequency_arr = frequency_arr
577
+ self.sens_mat = self.sens_mat_input
578
+
579
+ def update_model(self, model: lisa_models.LISAModel) -> None:
580
+ """Update class with new sensitivity model.
581
+
582
+ Args:
583
+ model: Noise model. Object of type :class:`lisa_models.LISAModel`. It can also be a string corresponding to one of the stock models.
584
+
585
+ """
586
+ self.sens_kwargs["model"] = model
587
+ self.sens_mat = self.sens_mat_input
588
+
589
+ def update_stochastic(self, **kwargs: dict) -> None:
590
+ """Update class with new stochastic function.
591
+
592
+ Args:
593
+ **kwargs: Keyword arguments update for :func:`lisatools.sensitivity.Sensitivity.get_stochastic_contribution`.
594
+ This operation will combine the new and old kwarg dictionaries, updating any
595
+ old information with any added corresponding new information. **Note**: any old information
596
+ that is not updated will remain in place.
597
+
598
+ """
599
+ self.sens_kwargs = {**self.sens_kwargs, **kwargs}
600
+ self.sens_mat = self.sens_mat_input
601
+
602
+ @property
603
+ def sens_mat(self) -> np.ndarray:
604
+ """Get sensitivity matrix."""
605
+ return self._sens_mat
606
+
607
+ @sens_mat.setter
608
+ def sens_mat(
609
+ self,
610
+ sens_mat: (
611
+ List[List[np.ndarray | Sensitivity]]
612
+ | List[np.ndarray | Sensitivity]
613
+ | np.ndarray
614
+ | Sensitivity
615
+ ),
616
+ ) -> None:
617
+ """Set sensitivity matrix."""
618
+ self.sens_mat_input = deepcopy(sens_mat)
619
+ self._sens_mat = np.asarray(sens_mat, dtype=object)
620
+
621
+ # not an
622
+ new_out = np.full(len(self._sens_mat.flatten()), None, dtype=object)
623
+ self.return_shape = self._sens_mat.shape
624
+ for i in range(len(self._sens_mat.flatten())):
625
+ current_sens = self._sens_mat.flatten()[i]
626
+ if hasattr(current_sens, "get_Sn") or isinstance(current_sens, str):
627
+ new_out[i] = get_sensitivity(
628
+ self.frequency_arr,
629
+ *self.sens_args,
630
+ sens_fn=current_sens,
631
+ **self.sens_kwargs,
632
+ )
633
+
634
+ elif isinstance(current_sens, np.ndarray) or isinstance(
635
+ current_sens, cp.ndarray
636
+ ):
637
+ new_out[i] = current_sens
638
+ else:
639
+ raise ValueError
640
+
641
+ # setup in array form
642
+ self._sens_mat = np.asarray(list(new_out), dtype=float).reshape(
643
+ self.return_shape + (-1,)
644
+ )
645
+
646
+ def __getitem__(self, index: Any) -> np.ndarray:
647
+ """Indexing the class indexes the array."""
648
+ return self.sens_mat[index]
649
+
650
+ @property
651
+ def ndim(self) -> int:
652
+ """Dimensionality of sens mat array."""
653
+ return self.sens_mat.ndim
654
+
655
+ def flatten(self) -> np.ndarray:
656
+ """Flatten sens mat array."""
657
+ return self.sens_mat.reshape(-1, self.sens_mat.shape[-1])
658
+
659
+ @property
660
+ def shape(self) -> tuple:
661
+ """Shape of sens mat array."""
662
+ return self.sens_mat.shape
663
+
664
+ def loglog(
665
+ self,
666
+ ax: Optional[plt.Axes] = None,
667
+ fig: Optional[plt.Figure] = None,
668
+ inds: Optional[int | tuple] = None,
669
+ char_strain: Optional[bool] = False,
670
+ **kwargs: dict,
671
+ ) -> Tuple[plt.Figure, plt.Axes]:
672
+ """Produce a log-log plot of the sensitivity.
673
+
674
+ Args:
675
+ ax: Matplotlib Axes objects to add plots. Either a list of Axes objects or a single Axes object.
676
+ fig: Matplotlib figure object.
677
+ inds: Integer index to select out which data to add to a single access.
678
+ A list can be provided if ax is a list. They must be the same length.
679
+ char_strain: If ``True``, plot in characteristic strain representation. **Note**: assumes the sensitivity
680
+ is input as power spectral density.
681
+ **kwargs: Keyword arguments to be passed to ``loglog`` function in matplotlib.
682
+
683
+ Returns:
684
+ Matplotlib figure and axes objects in a 2-tuple.
685
+
686
+
687
+ """
688
+ if (ax is None and fig is None) or (
689
+ ax is not None and (isinstance(ax, list) or isinstance(ax, np.ndarray))
690
+ ):
691
+ if ax is None and fig is None:
692
+ outer_shape = self.shape[:-1]
693
+ if len(outer_shape) == 2:
694
+ nrows = outer_shape[0]
695
+ ncols = outer_shape[1]
696
+ elif len(outer_shape) == 1:
697
+ nrows = 1
698
+ ncols = outer_shape[0]
699
+
700
+ fig, ax = plt.subplots(nrows, ncols, sharex=True, sharey=True)
701
+ try:
702
+ ax = ax.ravel()
703
+ except AttributeError:
704
+ ax = [ax] # just one axis object, no list
705
+
706
+ else:
707
+ assert len(ax) == np.prod(self.shape[:-1])
708
+
709
+ for i in range(np.prod(self.shape[:-1])):
710
+ plot_in = self.flatten()[i]
711
+ if char_strain:
712
+ plot_in = np.sqrt(self.frequency_arr * plot_in)
713
+ ax[i].loglog(self.frequency_arr, plot_in, **kwargs)
714
+
715
+ elif fig is not None:
716
+ raise NotImplementedError
717
+
718
+ elif isinstance(ax, plt.axes):
719
+ if inds is None:
720
+ raise ValueError(
721
+ "When passing a single axes object for `ax`, but also pass `inds` kwarg."
722
+ )
723
+ plot_in = self.sens_mat[inds]
724
+ if char_strain:
725
+ plot_in = np.sqrt(self.frequency_arr * plot_in)
726
+ ax.loglog(self.frequency_arr, plot_in, **kwargs)
727
+
728
+ else:
729
+ raise ValueError(
730
+ "ax must be a list of axes objects or a single axes object."
731
+ )
732
+
733
+ return (fig, ax)
734
+
735
+
736
+ class XYZ1SensitivityMatrix(SensitivityMatrix):
737
+ """Default sensitivity matrix for XYZ (TDI 1)
738
+
739
+ This is 3x3 symmetric matrix.
740
+
741
+ Args:
742
+ f: Frequency array.
743
+ **sens_kwargs: Keyword arguments to pass to :func:`Sensitivity.get_Sn`.
744
+
745
+ """
746
+
747
+ def __init__(self, f: np.ndarray, **sens_kwargs: dict) -> None:
748
+ sens_mat = [
749
+ [X1TDISens, XY1TDISens, ZX1TDISens],
750
+ [XY1TDISens, Y1TDISens, YZ1TDISens],
751
+ [ZX1TDISens, YZ1TDISens, Z1TDISens],
752
+ ]
753
+ super().__init__(f, sens_mat, **sens_kwargs)
754
+
755
+
756
+ class AET1SensitivityMatrix(SensitivityMatrix):
757
+ """Default sensitivity matrix for AET (TDI 1)
758
+
759
+ This is just an array because no cross-terms.
760
+
761
+ Args:
762
+ f: Frequency array.
763
+ **sens_kwargs: Keyword arguments to pass to :func:`Sensitivity.get_Sn`.
764
+
765
+ """
766
+
767
+ def __init__(self, f: np.ndarray, **sens_kwargs: dict) -> None:
768
+ sens_mat = [A1TDISens, E1TDISens, T1TDISens]
769
+ super().__init__(f, sens_mat, **sens_kwargs)
770
+
771
+
772
+ class AE1SensitivityMatrix(SensitivityMatrix):
773
+ """Default sensitivity matrix for AE (no T) (TDI 1)
774
+
775
+ Args:
776
+ f: Frequency array.
777
+ **sens_kwargs: Keyword arguments to pass to :func:`Sensitivity.get_Sn`.
778
+
779
+ """
780
+
781
+ def __init__(self, f: np.ndarray, **sens_kwargs: dict) -> None:
782
+ sens_mat = [A1TDISens, E1TDISens]
783
+ super().__init__(f, sens_mat, **sens_kwargs)
784
+
785
+
786
+ class LISASensSensitivityMatrix(SensitivityMatrix):
787
+ """Default sensitivity matrix adding :class:`LISASens` for the specified number of channels.
788
+
789
+ Args:
790
+ f: Frequency array.
791
+ nchannels: Number of channels.
792
+ **sens_kwargs: Keyword arguments to pass to :func:`Sensitivity.get_Sn`.
793
+
794
+ """
795
+
796
+ def __init__(self, f: np.ndarray, nchannels: int, **sens_kwargs: dict) -> None:
797
+ sens_mat = [LISASens for _ in range(nchannels)]
798
+ super().__init__(f, sens_mat, **sens_kwargs)
799
+
800
+
801
+ def get_sensitivity(
802
+ f: float | np.ndarray,
803
+ *args: tuple,
804
+ sens_fn: Optional[Sensitivity | str] = LISASens,
805
+ return_type="PSD",
806
+ **kwargs,
807
+ ) -> float | np.ndarray:
808
+ """Generic sensitivity generator
809
+
810
+ Same interface to many sensitivity curves.
811
+
812
+ Args:
813
+ f: Frequency array.
814
+ *args: Any additional arguments for the sensitivity function ``get_Sn`` method.
815
+ sens_fn: String or class that represents the name of the desired PSD function.
816
+ return_type: Described the desired output. Choices are ASD,
817
+ PSD, or char_strain (characteristic strain). Default is ASD.
818
+ **kwargs: Keyword arguments to pass to sensitivity function ``get_Sn`` method.
819
+
820
+ Return:
821
+ Sensitivity values.
822
+
823
+ """
824
+
825
+ if isinstance(sens_fn, str):
826
+ try:
827
+ sensitivity = globals()[sens_fn]
828
+ except KeyError:
829
+ raise ValueError(
830
+ f"{sens_fn} sensitivity is not available. Available stock sensitivities are"
831
+ )
832
+
833
+ elif hasattr(sens_fn, "get_Sn"):
834
+ sensitivity = sens_fn
835
+
836
+ else:
837
+ raise ValueError(
838
+ "sens_fn must be a string for a stock option or a class with a get_Sn method."
839
+ )
840
+
841
+ PSD = sensitivity.get_Sn(f, *args, **kwargs)
842
+
843
+ if return_type == "PSD":
844
+ return PSD
845
+
846
+ elif return_type == "ASD":
847
+ return PSD ** (1 / 2)
848
+
849
+ elif return_type == "char_strain":
850
+ return (f * PSD) ** (1 / 2)
851
+
852
+ else:
853
+ raise ValueError("return_type must be PSD, ASD, or char_strain.")
854
+
855
+
856
+ __stock_sens_options__ = [
857
+ "X1TDISens",
858
+ "Y1TDISens",
859
+ "Z1TDISens",
860
+ "XY1TDISens",
861
+ "YZ1TDISens",
862
+ "ZX1TDISens",
863
+ "A1TDISens",
864
+ "E1TDISens",
865
+ "T1TDISens",
866
+ "X2TDISens",
867
+ "Y2TDISens",
868
+ "Z2TDISens",
869
+ "LISASens",
870
+ "CornishLISASens",
871
+ "FlatPSDFunction",
872
+ ]
873
+
874
+
875
+ def get_stock_sensitivity_options() -> List[Sensitivity]:
876
+ """Get stock options for sensitivity curves.
877
+
878
+ Returns:
879
+ List of stock sensitivity options.
880
+
881
+ """
882
+ return __stock_sens_options__
883
+
884
+
885
+ __stock_sensitivity_mat_options__ = [
886
+ "XYZ1SensitivityMatrix",
887
+ "AET1SensitivityMatrix",
888
+ "AE1SensitivityMatrix",
889
+ ]
890
+
891
+
892
+ def get_stock_sensitivity_matrix_options() -> List[SensitivityMatrix]:
893
+ """Get stock options for sensitivity matrix.
894
+
895
+ Returns:
896
+ List of stock sensitivity matrix options.
897
+
898
+ """
899
+ return __stock_sensitivity_mat_options__