|
| 1 | +# Copyright: Multiple Authors |
| 2 | +# |
| 3 | +# This file is part of sigmf-python. https://github.com/sigmf/sigmf-python |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 6 | + |
| 7 | +"""Tests for Signal Hound Converter""" |
| 8 | + |
| 9 | +import tempfile |
| 10 | +import unittest |
| 11 | +import wave |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +import sigmf |
| 17 | +from sigmf.convert.signalhound import signalhound_to_sigmf |
| 18 | + |
| 19 | +from .test_convert_wav import _validate_ncd |
| 20 | +from .testdata import get_nonsigmf_path |
| 21 | + |
| 22 | + |
| 23 | +class TestSignalHoundWithNonSigMFRepo(unittest.TestCase): |
| 24 | + """Test Signal Hound converter with real example files if available.""" |
| 25 | + |
| 26 | + def setUp(self) -> None: |
| 27 | + """Find a non-SigMF dataset for testing.""" |
| 28 | + self.tmp_dir = tempfile.TemporaryDirectory() |
| 29 | + self.tmp_path = Path(self.tmp_dir.name) |
| 30 | + nonsigmf_path = get_nonsigmf_path(self) |
| 31 | + # glob all files in signal hound directory |
| 32 | + hound_dir = nonsigmf_path / "signal_hound" |
| 33 | + self.hound_paths = [] |
| 34 | + self.hound_paths.extend(hound_dir.glob("*.xml")) |
| 35 | + if not self.hound_paths: |
| 36 | + self.fail(f"No Signal Hound XML files found in {hound_dir}.") |
| 37 | + |
| 38 | + def tearDown(self) -> None: |
| 39 | + """Clean up temporary directory.""" |
| 40 | + self.tmp_dir.cleanup() |
| 41 | + |
| 42 | + def test_sigmf_pair(self): |
| 43 | + """test basic signal hound to sigmf conversion with file pairs""" |
| 44 | + for hound_path in self.hound_paths: |
| 45 | + sigmf_path = self.tmp_path / hound_path.stem |
| 46 | + meta = signalhound_to_sigmf(signalhound_path=hound_path, out_path=sigmf_path) |
| 47 | + self.assertIsInstance(meta, sigmf.SigMFFile) |
| 48 | + if not meta.get_global_field("core:metadata_only"): |
| 49 | + # check sample read consistency |
| 50 | + np.testing.assert_array_equal(meta.read_samples(count=10), meta[0:10]) |
| 51 | + |
| 52 | + def test_sigmf_archive(self): |
| 53 | + """test signal hound to sigmf conversion with archive output""" |
| 54 | + for hound_path in self.hound_paths: |
| 55 | + sigmf_path = self.tmp_path / f"{hound_path.stem}_archive" |
| 56 | + meta = signalhound_to_sigmf(signalhound_path=hound_path, out_path=sigmf_path, create_archive=True) |
| 57 | + self.assertIsInstance(meta, sigmf.SigMFFile) |
| 58 | + if not meta.get_global_field("core:metadata_only"): |
| 59 | + # check sample read consistency |
| 60 | + np.testing.assert_array_equal(meta.read_samples(count=10), meta[0:10]) |
| 61 | + |
| 62 | + def test_create_ncd(self): |
| 63 | + """test direct NCD conversion""" |
| 64 | + for hound_path in self.hound_paths: |
| 65 | + meta = signalhound_to_sigmf(signalhound_path=hound_path) |
| 66 | + _validate_ncd(self, meta, hound_path) |
| 67 | + if len(meta): |
| 68 | + # check sample read consistency |
| 69 | + np.testing.assert_array_equal(meta.read_samples(count=10), meta[0:10]) |
| 70 | + |
| 71 | + def test_fromfile_ncd(self): |
| 72 | + """test automatic NCD conversion with fromfile""" |
| 73 | + for hound_path in self.hound_paths: |
| 74 | + meta = sigmf.fromfile(hound_path) |
| 75 | + _validate_ncd(self, meta, hound_path) |
0 commit comments