Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sigmf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later

# version of this python module
__version__ = "1.7.1"
__version__ = "1.7.2"
# matching version of the SigMF specification
__specification__ = "1.2.6"

Expand Down
17 changes: 8 additions & 9 deletions sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,22 +1217,19 @@ def get_dataset_filename_from_metadata(meta_fn, metadata=None):

Priority for conflicting datasets:

1. Use the file named ``<stem>.SIGMF_DATASET_EXT`` if it exists.
2. Use the file in the ``DATASET_KEY`` field (non-compliant dataset) if it exists.
1. Use the file in the ``DATASET_KEY`` field (non-compliant dataset) if it exists.
2. Use the file named ``<stem>.SIGMF_DATASET_EXT`` if it exists.
3. Return ``None`` (may be a metadata-only distribution).
"""
compliant_filename = get_sigmf_filenames(meta_fn)["data_fn"]
noncompliant_filename = metadata["global"].get(SigMFFile.DATASET_KEY, None)

if Path.is_file(compliant_filename):
if noncompliant_filename:
if noncompliant_filename:
if Path.is_file(compliant_filename):
warnings.warn(
f"Compliant Dataset `{compliant_filename}` exists but "
f"{SigMFFile.DATASET_KEY} is also defined; using `{compliant_filename}`"
f"{SigMFFile.DATASET_KEY} is defined but compliant dataset `{compliant_filename}` exists; "
f"using `{noncompliant_filename}` specified by {SigMFFile.DATASET_KEY}"
)
return compliant_filename

elif noncompliant_filename:
dir_path = Path(meta_fn).parent
noncompliant_data_file_path = Path.joinpath(dir_path, noncompliant_filename)
if Path.is_file(noncompliant_data_file_path):
Expand All @@ -1247,6 +1244,8 @@ def get_dataset_filename_from_metadata(meta_fn, metadata=None):
f"Non-Compliant Dataset `{noncompliant_filename}` is specified in {SigMFFile.DATASET_KEY} "
"but does not exist!"
)
elif Path.is_file(compliant_filename):
return compliant_filename
return None


Expand Down
36 changes: 36 additions & 0 deletions tests/test_ncd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shutil
import tempfile
import unittest
import warnings
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -61,3 +62,38 @@ def test_load_ncd(self, subdir: str) -> None:
Path.unlink(data_path)
with self.assertRaises(SigMFFileError):
_ = fromfile(meta_path)

def test_ncd_priority_over_conforming_dataset(self) -> None:
"""test that NCD file specified in core:dataset is prioritized over .sigmf-data file"""
base_name = "conflicting_dataset"
meta_path = self.temp_dir / f"{base_name}.sigmf-meta"
ncd_path = self.temp_dir / f"{base_name}.fleeb"
conforming_path = self.temp_dir / f"{base_name}.sigmf-data"

# create two different datasets with distinct data for verification
ncd_data = np.array([100, 200, 300, 400], dtype=np.float32)
conforming_data = np.array([1, 2, 3, 4], dtype=np.float32)

# write both data files
ncd_data.tofile(ncd_path)
conforming_data.tofile(conforming_path)

# create metadata that references the ncd file
ncd_metadata = copy.deepcopy(TEST_METADATA)
ncd_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.DATASET_KEY] = f"{base_name}.fleeb"
ncd_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.NUM_CHANNELS_KEY] = 1
ncd_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.DATATYPE_KEY] = "rf32_le"
ncd_metadata[SigMFFile.GLOBAL_KEY].pop(SigMFFile.HASH_KEY, None)
ncd_metadata[SigMFFile.ANNOTATION_KEY] = [{SigMFFile.LENGTH_INDEX_KEY: 4, SigMFFile.START_INDEX_KEY: 0}]

# write metadata file
meta = SigMFFile(metadata=ncd_metadata)
meta.tofile(meta_path, overwrite=True)

# verify warning is generated about conflicting datasets
with self.assertWarns(UserWarning):
loaded_meta = fromfile(meta_path)

# verify that the ncd data is loaded, not the conforming data
loaded_data = loaded_meta.read_samples()
self.assertTrue(np.array_equal(ncd_data, loaded_data), "NCD file should be prioritized over .sigmf-data")
Loading