LALSimulation 6.2.0.1-eeff03c
errors.py
Go to the documentation of this file.
1import lal
2from functools import wraps
3from astropy import units as u
4
5
6##############################################################################################################
7# Mapping between LAL Errors and Python
8##############################################################################################################
9
10def mapexception(func):
11 """
12 Wrapper to map LAL erros to Python errors
13 """
14 @wraps(func)
15 def wrapper(*args, **kwargs):
16 exception = None
17 try:
18 return func(*args, **kwargs)
19 except RuntimeError as e:
20 lalerr = lal.GetBaseErrno()
21 exception = exceptionmap[lalerr] if lalerr in exceptionmap else e
22 raise exception from None
23 return wrapper
24
25# Map of errors raised by LAL to Python errors
26exceptionmap = {
27 lal.ENOENT: FileNotFoundError("No such file or directory"),
28 lal.EIO: OSError("I/O Error"),
29 lal.ENOMEM: MemoryError("Memory Allocation Error"),
30 lal.EFAULT: ValueError("Invalid pointer"),
31 lal.EINVAL: ValueError("Invalid Argument"),
32 lal.EDOM: ValueError("Input domain error"),
33 lal.ERANGE: ArithmeticError("Output range error"),
34 lal.ENOSYS: NotImplementedError("Function not implemented"),
35 lal.EFAILED: RuntimeError("Generic Failure"),
36 lal.EBADLEN: ValueError("Inconsistent or invalid length"),
37 lal.ESIZE: ValueError("Wrong Size"),
38 lal.EDIMS: ValueError("Wrong dimensions"),
39 lal.ETYPE: TypeError("Wrong or unknown type"),
40 lal.ETIME: ValueError("Invalid time"),
41 lal.EFREQ: ValueError("Invalid frequency"),
42 lal.EUNIT: u.UnitsError("Invalid units"),
43 lal.ENAME: ValueError("Wrong name"),
44 lal.EDATA: ValueError("Invalid data"),
45 lal.ESYS: SystemError("System Error"),
46 lal.EERR: RuntimeError("Internal Error"),
47 lal.EFPINVAL: FloatingPointError("IEEE Invalid floating point operation, eg sqrt(-1), 0/0"),
48 lal.EFPDIV0: ZeroDivisionError("IEEE Division by zero floating point error"),
49 lal.EFPOVRFLW: OverflowError("IEEE Floating point overflow error"),
50 lal.EFPUNDFLW: FloatingPointError("IEEE Floating point underflow error"),
51 lal.EFPINEXCT: FloatingPointError("IEEE Floating point inexact error"),
52 lal.EMAXITER: ArithmeticError("Exceeded maximum number of iterations"),
53 lal.EDIVERGE: ArithmeticError("Series is diverging"),
54 lal.ESING: ArithmeticError("Apparent singularity detected"),
55 lal.ETOL: ArithmeticError("Failed to reach specified tolerance"),
56 lal.ELOSS: ArithmeticError("Loss of accuracy"),
57 lal.EUSR0: RuntimeError("User defined Error"),
58 lal.EUSR1: RuntimeError("User defined Error"),
59 lal.EUSR2: RuntimeError("User defined Error"),
60 lal.EUSR3: RuntimeError("User defined Error"),
61 lal.EUSR4: RuntimeError("User defined Error"),
62 lal.EUSR5: RuntimeError("User defined Error"),
63 lal.EUSR6: RuntimeError("User defined Error"),
64 lal.EUSR7: RuntimeError("User defined Error"),
65 lal.EUSR8: RuntimeError("User defined Error"),
66 lal.EUSR9: RuntimeError("User defined Error")
67 }
68
69##############################################################################################################
70# Errors callable by GWSignal.
71# Each error should be subclasses from existing python errors, for eg., ValueError, Exception etc.
72##############################################################################################################
73
74class WaveformGenerationError(Exception):
75 """
76 Raise Error when there is an issue with waveform generation.
77
78 Parameters
79 ----------
80 message : Output message for the error.
81 Returns
82 -------
83 Raises waveform generation error exception
84
85 """
86 def __init__(self, message="Error during waveform generation"):
87 self.message = message
88 super().__init__(self.message)
89
90 def __str__(self):
91 return f'{self.message}'
92
93
94class DomainError(ValueError):
95 """
96 Raise Error when parameter passes is out of waveform domain
97
98 Parameters
99 ----------
100 parameter : Parameter in question
101 message : Output message for the error.
102
103 Returns
104 -------
105 Raises domain error exception
106 """
107 def __init__(self, parameter, message="Input parameter out of waveform domain"):
108 self.parameter = parameter
109 self.message = message
110 super().__init__(self.message)
111
112 def __str__(self):
113 return f'{self.parameter} -> {self.message}'
114
115
116class PathError(FileNotFoundError):
117 """
118 Raise Exception when the needed file is not at the given path
119
120 Parameters
121 ----------
122 file_name : Path to the file in question.
123 message : Output message for the error.
124
125 Returns
126 -------
127 Raises File not found at given path error excetion.
128
129 """
130 def __init__(self, file_path, message="File not found at "):
131 self.file_path = file_path
132 self.message = message
133 super().__init__(self.message)
134
135 def __str__(self):
136 return f'{self.message} -> {self.file_path}'
Raise Error when parameter passes is out of waveform domain.
Definition: errors.py:106
def __init__(self, parameter, message="Input parameter out of waveform domain")
Definition: errors.py:107
Raise Exception when the needed file is not at the given path.
Definition: errors.py:129
def __init__(self, file_path, message="File not found at ")
Definition: errors.py:130
def __init__(self, message="Error during waveform generation")
Definition: errors.py:86
def mapexception(func)
Mapping between LAL Errors and Python.
Definition: errors.py:13