Skip to content

Commit 241ed97

Browse files
cguo02Cherry GuoTeque5
authored
Reorder priority for conflicting datasets (#142)
* increment to `v1.7.2` * Reorder priority for conflicting datasets * add test to verify issue & increment patch --------- Co-authored-by: Cherry Guo <cguo@exptechinc.com> Co-authored-by: Teque5 <teque5@gmail.com>
1 parent 2b0621b commit 241ed97

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

sigmf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# SPDX-License-Identifier: LGPL-3.0-or-later
66

77
# version of this python module
8-
__version__ = "1.7.1"
8+
__version__ = "1.7.2"
99
# matching version of the SigMF specification
1010
__specification__ = "1.2.6"
1111

sigmf/sigmffile.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,22 +1217,19 @@ def get_dataset_filename_from_metadata(meta_fn, metadata=None):
12171217
12181218
Priority for conflicting datasets:
12191219
1220-
1. Use the file named ``<stem>.SIGMF_DATASET_EXT`` if it exists.
1221-
2. Use the file in the ``DATASET_KEY`` field (non-compliant dataset) if it exists.
1220+
1. Use the file in the ``DATASET_KEY`` field (non-compliant dataset) if it exists.
1221+
2. Use the file named ``<stem>.SIGMF_DATASET_EXT`` if it exists.
12221222
3. Return ``None`` (may be a metadata-only distribution).
12231223
"""
12241224
compliant_filename = get_sigmf_filenames(meta_fn)["data_fn"]
12251225
noncompliant_filename = metadata["global"].get(SigMFFile.DATASET_KEY, None)
12261226

1227-
if Path.is_file(compliant_filename):
1228-
if noncompliant_filename:
1227+
if noncompliant_filename:
1228+
if Path.is_file(compliant_filename):
12291229
warnings.warn(
1230-
f"Compliant Dataset `{compliant_filename}` exists but "
1231-
f"{SigMFFile.DATASET_KEY} is also defined; using `{compliant_filename}`"
1230+
f"{SigMFFile.DATASET_KEY} is defined but compliant dataset `{compliant_filename}` exists; "
1231+
f"using `{noncompliant_filename}` specified by {SigMFFile.DATASET_KEY}"
12321232
)
1233-
return compliant_filename
1234-
1235-
elif noncompliant_filename:
12361233
dir_path = Path(meta_fn).parent
12371234
noncompliant_data_file_path = Path.joinpath(dir_path, noncompliant_filename)
12381235
if Path.is_file(noncompliant_data_file_path):
@@ -1247,6 +1244,8 @@ def get_dataset_filename_from_metadata(meta_fn, metadata=None):
12471244
f"Non-Compliant Dataset `{noncompliant_filename}` is specified in {SigMFFile.DATASET_KEY} "
12481245
"but does not exist!"
12491246
)
1247+
elif Path.is_file(compliant_filename):
1248+
return compliant_filename
12501249
return None
12511250

12521251

tests/test_ncd.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import shutil
1111
import tempfile
1212
import unittest
13+
import warnings
1314
from pathlib import Path
1415

1516
import numpy as np
@@ -61,3 +62,38 @@ def test_load_ncd(self, subdir: str) -> None:
6162
Path.unlink(data_path)
6263
with self.assertRaises(SigMFFileError):
6364
_ = fromfile(meta_path)
65+
66+
def test_ncd_priority_over_conforming_dataset(self) -> None:
67+
"""test that NCD file specified in core:dataset is prioritized over .sigmf-data file"""
68+
base_name = "conflicting_dataset"
69+
meta_path = self.temp_dir / f"{base_name}.sigmf-meta"
70+
ncd_path = self.temp_dir / f"{base_name}.fleeb"
71+
conforming_path = self.temp_dir / f"{base_name}.sigmf-data"
72+
73+
# create two different datasets with distinct data for verification
74+
ncd_data = np.array([100, 200, 300, 400], dtype=np.float32)
75+
conforming_data = np.array([1, 2, 3, 4], dtype=np.float32)
76+
77+
# write both data files
78+
ncd_data.tofile(ncd_path)
79+
conforming_data.tofile(conforming_path)
80+
81+
# create metadata that references the ncd file
82+
ncd_metadata = copy.deepcopy(TEST_METADATA)
83+
ncd_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.DATASET_KEY] = f"{base_name}.fleeb"
84+
ncd_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.NUM_CHANNELS_KEY] = 1
85+
ncd_metadata[SigMFFile.GLOBAL_KEY][SigMFFile.DATATYPE_KEY] = "rf32_le"
86+
ncd_metadata[SigMFFile.GLOBAL_KEY].pop(SigMFFile.HASH_KEY, None)
87+
ncd_metadata[SigMFFile.ANNOTATION_KEY] = [{SigMFFile.LENGTH_INDEX_KEY: 4, SigMFFile.START_INDEX_KEY: 0}]
88+
89+
# write metadata file
90+
meta = SigMFFile(metadata=ncd_metadata)
91+
meta.tofile(meta_path, overwrite=True)
92+
93+
# verify warning is generated about conflicting datasets
94+
with self.assertWarns(UserWarning):
95+
loaded_meta = fromfile(meta_path)
96+
97+
# verify that the ncd data is loaded, not the conforming data
98+
loaded_data = loaded_meta.read_samples()
99+
self.assertTrue(np.array_equal(ncd_data, loaded_data), "NCD file should be prioritized over .sigmf-data")

0 commit comments

Comments
 (0)