|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from typing_extensions import Literal |
| 4 | + |
3 | 5 |
|
4 | 6 | class TimingMismatchError(RuntimeError): |
5 | 7 | """Exception used when appending waveforms with mismatched timing.""" |
6 | 8 |
|
7 | 9 | pass |
8 | 10 |
|
9 | 11 |
|
10 | | -def input_array_data_type_mismatch(input_dtype: object, waveform_dtype: object) -> TypeError: |
11 | | - """Create a TypeError for an input array data type mismatch.""" |
12 | | - return TypeError( |
13 | | - "The data type of the input array must match the waveform data type.\n\n" |
14 | | - f"Input array data type: {input_dtype}\n" |
15 | | - f"Waveform data type: {waveform_dtype}" |
| 12 | +def capacity_mismatch(capacity: int, array_length: int) -> ValueError: |
| 13 | + """Create a ValueError for an invalid capacity.""" |
| 14 | + return ValueError( |
| 15 | + f"The capacity must match the input array length.\n\n" |
| 16 | + f"Capacity: {capacity}\n" |
| 17 | + f"Array length: {array_length}" |
16 | 18 | ) |
17 | 19 |
|
18 | 20 |
|
19 | | -def input_waveform_data_type_mismatch(input_dtype: object, waveform_dtype: object) -> TypeError: |
20 | | - """Create a TypeError for an input waveform data type mismatch.""" |
| 21 | +def capacity_too_small(capacity: int, min_capacity: int, object_description: str) -> ValueError: |
| 22 | + """Create a ValueError for an invalid capacity argument.""" |
| 23 | + return ValueError( |
| 24 | + f"The capacity must be equal to or greater than the number of samples in the {object_description}.\n\n" |
| 25 | + f"Capacity: {capacity}\n" |
| 26 | + f"Number of samples: {min_capacity}" |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def data_type_mismatch( |
| 31 | + arg_description: Literal["input array", "input spectrum", "input waveform"], |
| 32 | + arg_dtype: object, |
| 33 | + other_description: Literal["requested", "spectrum", "waveform"], |
| 34 | + other_dtype: object, |
| 35 | +) -> TypeError: |
| 36 | + """Create a TypeError for a data type mismatch.""" |
| 37 | + arg_key = { |
| 38 | + "input array": "Input array data type", |
| 39 | + "input spectrum": "Input spectrum data type", |
| 40 | + "input waveform": "Input waveform data type", |
| 41 | + } |
| 42 | + other_key = { |
| 43 | + "requested": "Requested data type", |
| 44 | + "spectrum": "Spectrum data type", |
| 45 | + "waveform": "Waveform data type", |
| 46 | + } |
21 | 47 | return TypeError( |
22 | | - "The data type of the input waveform must match the waveform data type.\n\n" |
23 | | - f"Input waveform data type: {input_dtype}\n" |
24 | | - f"Waveform data type: {waveform_dtype}" |
| 48 | + f"The data type of the {arg_description} must match the {other_description} data type.\n\n" |
| 49 | + f"{arg_key[arg_description]}: {arg_dtype}\n" |
| 50 | + f"{other_key[other_description]}: {other_dtype}" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def irregular_timestamp_count_mismatch( |
| 55 | + irregular_timestamp_count: int, |
| 56 | + other_description: Literal["input array length", "number of samples in the waveform"], |
| 57 | + other: int, |
| 58 | + *, |
| 59 | + reversed: bool = False, |
| 60 | +) -> ValueError: |
| 61 | + """Create a ValueError for an irregular timestamp count mismatch.""" |
| 62 | + other_key = { |
| 63 | + "input array length": "Array length", |
| 64 | + "number of samples in the waveform": "Number of samples", |
| 65 | + } |
| 66 | + if reversed: |
| 67 | + raise ValueError( |
| 68 | + "The input array length must be equal to the number of irregular timestamps.\n\n" |
| 69 | + f"{other_key[other_description]}: {other}\n" |
| 70 | + f"Number of timestamps: {irregular_timestamp_count}" |
| 71 | + ) |
| 72 | + else: |
| 73 | + raise ValueError( |
| 74 | + f"The number of irregular timestamps must be equal to the {other_description}.\n\n" |
| 75 | + f"Number of timestamps: {irregular_timestamp_count}\n" |
| 76 | + f"{other_key[other_description]}: {other}" |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def start_index_too_large( |
| 81 | + start_index: int, |
| 82 | + capacity_description: Literal[ |
| 83 | + "capacity", |
| 84 | + "input array length", |
| 85 | + "number of samples in the spectrum", |
| 86 | + "number of samples in the waveform", |
| 87 | + ], |
| 88 | + capacity: int, |
| 89 | +) -> ValueError: |
| 90 | + """Create a ValueError for an invalid start index argument.""" |
| 91 | + capacity_key = { |
| 92 | + "capacity": "Capacity", |
| 93 | + "input array length": "Array length", |
| 94 | + "number of samples in the spectrum": "Number of samples", |
| 95 | + "number of samples in the waveform": "Number of samples", |
| 96 | + } |
| 97 | + return ValueError( |
| 98 | + f"The start index must be less than or equal to the {capacity_description}.\n\n" |
| 99 | + f"Start index: {start_index}\n" |
| 100 | + f"{capacity_key[capacity_description]}: {capacity}" |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def start_index_or_sample_count_too_large( |
| 105 | + start_index: int, |
| 106 | + sample_count: int, |
| 107 | + capacity_description: Literal[ |
| 108 | + "capacity", |
| 109 | + "input array length", |
| 110 | + "number of samples in the spectrum", |
| 111 | + "number of samples in the waveform", |
| 112 | + ], |
| 113 | + capacity: int, |
| 114 | +) -> ValueError: |
| 115 | + """Create a ValueError for an invalid start index or sample count argument.""" |
| 116 | + capacity_key = { |
| 117 | + "capacity": "Capacity", |
| 118 | + "input array length": "Array length", |
| 119 | + "number of samples in the spectrum": "Number of samples", |
| 120 | + "number of samples in the waveform": "Number of samples", |
| 121 | + } |
| 122 | + return ValueError( |
| 123 | + f"The sum of the start index and sample count must be less than or equal to the {capacity_description}.\n\n" |
| 124 | + f"Start index: {start_index}\n" |
| 125 | + f"Sample count: {sample_count}\n" |
| 126 | + f"{capacity_key[capacity_description]}: {capacity}" |
25 | 127 | ) |
26 | 128 |
|
27 | 129 |
|
|
0 commit comments