|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pyarrow as pa |
| 5 | +import pyarrow.parquet as pq |
| 6 | + |
| 7 | +from birdnet.acoustic.inference.core.prediction.prediction_result import ( |
| 8 | + AcousticFilePredictionResult, |
| 9 | +) |
| 10 | +from birdnet_tests.acoustic_models.inference.predictions.prediction_result_py.test_prediction_to_structured_array import ( # noqa: E501 |
| 11 | + create_file_prediction_result, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def _create_result_with_float16_durations() -> AcousticFilePredictionResult: |
| 16 | + """Create a prediction result whose input_durations are float16.""" |
| 17 | + result = create_file_prediction_result( |
| 18 | + n_files=2, |
| 19 | + duration_s=12, |
| 20 | + top_k=3, |
| 21 | + segment_duration_s=3.0, |
| 22 | + overlap_duration_s=0.0, |
| 23 | + ) |
| 24 | + assert result.input_durations.dtype == np.float16 |
| 25 | + return result |
| 26 | + |
| 27 | + |
| 28 | +def _create_result_with_float32_durations() -> AcousticFilePredictionResult: |
| 29 | + """Create a prediction result whose input_durations are float32.""" |
| 30 | + result = create_file_prediction_result( |
| 31 | + n_files=1, |
| 32 | + duration_s=5000, |
| 33 | + top_k=1, |
| 34 | + segment_duration_s=3.0, |
| 35 | + overlap_duration_s=0.0, |
| 36 | + ) |
| 37 | + assert result.input_durations.dtype == np.float32 |
| 38 | + return result |
| 39 | + |
| 40 | + |
| 41 | +def _create_result_with_float64_durations() -> AcousticFilePredictionResult: |
| 42 | + """Create a prediction result whose input_durations are float64. |
| 43 | +
|
| 44 | + Uses a small duration for speed, then coerces dtype to float64 to exercise |
| 45 | + the Arrow type-promotion path without creating millions of segments. |
| 46 | + """ |
| 47 | + result = create_file_prediction_result( |
| 48 | + n_files=1, |
| 49 | + duration_s=12, |
| 50 | + top_k=1, |
| 51 | + segment_duration_s=3.0, |
| 52 | + overlap_duration_s=0.0, |
| 53 | + ) |
| 54 | + result._input_durations = result._input_durations.astype(np.float64) |
| 55 | + assert result.input_durations.dtype == np.float64 |
| 56 | + return result |
| 57 | + |
| 58 | + |
| 59 | +def test_arrow_table_time_columns_are_float32_when_durations_float16() -> None: |
| 60 | + result = _create_result_with_float16_durations() |
| 61 | + table = result.to_arrow_table() |
| 62 | + |
| 63 | + assert table.schema.field("start_time").type == pa.float32() |
| 64 | + assert table.schema.field("end_time").type == pa.float32() |
| 65 | + |
| 66 | + |
| 67 | +def test_arrow_table_time_columns_are_float32_when_durations_float32() -> None: |
| 68 | + result = _create_result_with_float32_durations() |
| 69 | + table = result.to_arrow_table() |
| 70 | + |
| 71 | + assert table.schema.field("start_time").type == pa.float32() |
| 72 | + assert table.schema.field("end_time").type == pa.float32() |
| 73 | + |
| 74 | + |
| 75 | +def test_arrow_table_time_columns_are_float64_when_durations_float64() -> None: |
| 76 | + result = _create_result_with_float64_durations() |
| 77 | + table = result.to_arrow_table() |
| 78 | + |
| 79 | + assert table.schema.field("start_time").type == pa.float64() |
| 80 | + assert table.schema.field("end_time").type == pa.float64() |
| 81 | + |
| 82 | + |
| 83 | +def test_parquet_roundtrip_schema_float16(tmp_path: Path) -> None: |
| 84 | + result = _create_result_with_float16_durations() |
| 85 | + out = tmp_path / "result.parquet" |
| 86 | + |
| 87 | + result.to_parquet(out, silent=True) |
| 88 | + table = pq.read_table(out) |
| 89 | + |
| 90 | + assert table.schema.field("start_time").type == pa.float32() |
| 91 | + assert table.schema.field("end_time").type == pa.float32() |
| 92 | + |
| 93 | + |
| 94 | +def test_parquet_roundtrip_values_float16(tmp_path: Path) -> None: |
| 95 | + result = _create_result_with_float16_durations() |
| 96 | + structured = result.to_structured_array() |
| 97 | + out = tmp_path / "result.parquet" |
| 98 | + |
| 99 | + expected_start = np.array(structured["start_time"], dtype=np.float64) |
| 100 | + expected_end = np.array(structured["end_time"], dtype=np.float64) |
| 101 | + expected_conf = np.array(structured["confidence"], dtype=np.float64) |
| 102 | + |
| 103 | + result.to_parquet(out, silent=True) |
| 104 | + table = pq.read_table(out) |
| 105 | + |
| 106 | + actual_start = np.array(table.column("start_time").to_pylist(), dtype=np.float64) |
| 107 | + actual_end = np.array(table.column("end_time").to_pylist(), dtype=np.float64) |
| 108 | + actual_conf = np.array(table.column("confidence").to_pylist(), dtype=np.float64) |
| 109 | + |
| 110 | + np.testing.assert_allclose(expected_start, actual_start, rtol=1e-3) |
| 111 | + np.testing.assert_allclose(expected_end, actual_end, rtol=1e-3) |
| 112 | + np.testing.assert_allclose(expected_conf, actual_conf, rtol=1e-3) |
| 113 | + |
| 114 | + |
| 115 | +def test_parquet_roundtrip_values_float32(tmp_path: Path) -> None: |
| 116 | + result = _create_result_with_float32_durations() |
| 117 | + structured = result.to_structured_array() |
| 118 | + out = tmp_path / "result.parquet" |
| 119 | + |
| 120 | + expected_start = np.array(structured["start_time"], dtype=np.float64) |
| 121 | + expected_end = np.array(structured["end_time"], dtype=np.float64) |
| 122 | + |
| 123 | + result.to_parquet(out, silent=True) |
| 124 | + table = pq.read_table(out) |
| 125 | + |
| 126 | + actual_start = np.array(table.column("start_time").to_pylist(), dtype=np.float64) |
| 127 | + actual_end = np.array(table.column("end_time").to_pylist(), dtype=np.float64) |
| 128 | + |
| 129 | + np.testing.assert_allclose(expected_start, actual_start, rtol=1e-6) |
| 130 | + np.testing.assert_allclose(expected_end, actual_end, rtol=1e-6) |
| 131 | + |
| 132 | + |
| 133 | +def test_parquet_roundtrip_values_float64(tmp_path: Path) -> None: |
| 134 | + result = _create_result_with_float64_durations() |
| 135 | + structured = result.to_structured_array() |
| 136 | + out = tmp_path / "result.parquet" |
| 137 | + |
| 138 | + expected_start = np.array(structured["start_time"], dtype=np.float64) |
| 139 | + expected_end = np.array(structured["end_time"], dtype=np.float64) |
| 140 | + |
| 141 | + result.to_parquet(out, silent=True) |
| 142 | + table = pq.read_table(out) |
| 143 | + |
| 144 | + actual_start = np.array(table.column("start_time").to_pylist(), dtype=np.float64) |
| 145 | + actual_end = np.array(table.column("end_time").to_pylist(), dtype=np.float64) |
| 146 | + |
| 147 | + np.testing.assert_allclose(expected_start, actual_start, rtol=1e-9) |
| 148 | + np.testing.assert_allclose(expected_end, actual_end, rtol=1e-9) |
| 149 | + |
| 150 | + |
| 151 | +def test_parquet_time_columns_no_halffloat(tmp_path: Path) -> None: |
| 152 | + """Ensure start_time and end_time never use halffloat in Parquet.""" |
| 153 | + result = _create_result_with_float16_durations() |
| 154 | + out = tmp_path / "result.parquet" |
| 155 | + |
| 156 | + result.to_parquet(out, silent=True) |
| 157 | + table = pq.read_table(out) |
| 158 | + |
| 159 | + for col_name in ("start_time", "end_time"): |
| 160 | + field = table.schema.field(col_name) |
| 161 | + assert field.type != pa.float16(), ( |
| 162 | + f"Column '{col_name}' uses halffloat (float16), " |
| 163 | + f"which is not interoperable across Arrow implementations" |
| 164 | + ) |
0 commit comments