|
| 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 loading non-conforming datasets""" |
| 8 | + |
| 9 | +import os |
| 10 | +import numpy as np |
| 11 | +import pytest |
| 12 | +from sigmf.sigmffile import SigMFFile, fromfile |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + ["file_path"], |
| 17 | + [ |
| 18 | + ["b1.bin"], |
| 19 | + ["./b2.bin"], |
| 20 | + ["test_subdir/b3.bin"], # fails in the 1.2.3 version |
| 21 | + ["./test_subdir/b4.bin"], # fails in the 1.2.3 version |
| 22 | + ], |
| 23 | +) |
| 24 | +def test_load_ncd(file_path: str) -> None: |
| 25 | + dir_path, file_name = os.path.split(file_path) |
| 26 | + file_name_base, file_name_ext = os.path.splitext(file_name) |
| 27 | + if not dir_path: |
| 28 | + dir_path = "." # sets the correct path in the case file is only a filename |
| 29 | + meta_file_path = f"{dir_path}/{file_name_base}.sigmf-meta" |
| 30 | + |
| 31 | + # create dir |
| 32 | + try: |
| 33 | + os.makedirs(dir_path) |
| 34 | + except FileExistsError: |
| 35 | + pass |
| 36 | + |
| 37 | + # create dataset |
| 38 | + np.arange(10, dtype=np.int16).tofile(file_path) |
| 39 | + |
| 40 | + # create metadata file |
| 41 | + metadata = { |
| 42 | + SigMFFile.GLOBAL_KEY: { |
| 43 | + SigMFFile.DATATYPE_KEY: "ri16_le", |
| 44 | + SigMFFile.DATASET_KEY: file_name, |
| 45 | + }, |
| 46 | + SigMFFile.CAPTURE_KEY: [ |
| 47 | + { |
| 48 | + SigMFFile.START_INDEX_KEY: 0, |
| 49 | + } |
| 50 | + ], |
| 51 | + SigMFFile.ANNOTATION_KEY: [], |
| 52 | + } |
| 53 | + meta_file = SigMFFile(metadata=metadata, data_file=file_path) |
| 54 | + meta_file.tofile(meta_file_path) |
| 55 | + |
| 56 | + # load dataset |
| 57 | + data = fromfile(meta_file_path) |
| 58 | + |
| 59 | + assert np.array_equal( |
| 60 | + np.arange(10, dtype=np.int16), |
| 61 | + data.read_samples(autoscale=False), |
| 62 | + ) |
0 commit comments