Skip to content

Commit e690303

Browse files
committed
remove private method, set only remaining getter to deprecation warning
1 parent d8ba5d8 commit e690303

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

sigmf/sigmffile.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,15 @@ def __getitem__(self, sli):
322322
raise ValueError("unhandled ndim in SigMFFile.__getitem__(); this shouldn't happen")
323323
return ray[0] if isinstance(sli, int) else ray # return element instead of 1-element array
324324

325-
def _get_start_offset(self):
326-
"""
327-
Return the offset of the first sample.
328-
"""
329-
return self.get_global_field(self.START_OFFSET_KEY, 0)
330-
331325
def get_num_channels(self):
332-
"""Returns integer number of channels if present, otherwise 1"""
333-
return self.get_global_field(self.NUM_CHANNELS_KEY, 1)
326+
"""Return integer number of channels."""
327+
warnings.warn(
328+
"get_num_channels() is deprecated and will be removed in a future version of sigmf. "
329+
"Use the 'num_channels' attribute instead.",
330+
DeprecationWarning,
331+
stacklevel=2,
332+
)
333+
return self.num_channels
334334

335335
def _is_conforming_dataset(self):
336336
"""
@@ -380,9 +380,11 @@ def set_metadata(self, metadata):
380380
else:
381381
raise SigMFError("Unable to interpret provided metadata.")
382382

383-
# if num_channels missing, default to 1
383+
# ensure fields required for parsing are present or use defaults
384384
if self.get_global_field(self.NUM_CHANNELS_KEY) is None:
385385
self.set_global_field(self.NUM_CHANNELS_KEY, 1)
386+
if self.get_global_field(self.START_OFFSET_KEY) is None:
387+
self.set_global_field(self.START_OFFSET_KEY, 0)
386388

387389
# set version to current implementation
388390
self.set_global_field(self.VERSION_KEY, __specification__)
@@ -420,7 +422,8 @@ def add_capture(self, start_index, metadata=None):
420422
If there is already capture info for this index, metadata will be merged
421423
with the existing metadata, overwriting keys if they were previously set.
422424
"""
423-
assert start_index >= self._get_start_offset()
425+
if start_index < self.offset:
426+
raise SigMFFileError("Capture start_index cannot be less than dataset start offset.")
424427
capture_list = self._metadata[self.CAPTURE_KEY]
425428
new_capture = metadata or {}
426429
new_capture[self.START_INDEX_KEY] = start_index
@@ -446,12 +449,13 @@ def get_captures(self):
446449

447450
def get_capture_info(self, index):
448451
"""
449-
Returns a dictionary containing all the capture information at sample
450-
'index'.
452+
Returns a dictionary containing all the capture information at sample index.
451453
"""
452-
assert index >= self._get_start_offset()
454+
if index < self.offset:
455+
raise SigMFFileError("Sample index cannot be less than dataset start offset.")
453456
captures = self._metadata.get(self.CAPTURE_KEY, [])
454-
assert len(captures) > 0
457+
if len(captures) == 0:
458+
raise SigMFFileError("No captures in metadata.")
455459
cap_info = captures[0]
456460
for capture in captures:
457461
if capture[self.START_INDEX_KEY] > index:
@@ -484,9 +488,7 @@ def get_capture_byte_boundarys(self, index):
484488
prev_start_sample = 0
485489
for ii, capture in enumerate(self.get_captures()):
486490
start_byte += capture.get(self.HEADER_BYTES_KEY, 0)
487-
start_byte += (
488-
(self.get_capture_start(ii) - prev_start_sample) * self.get_sample_size() * self.get_num_channels()
489-
)
491+
start_byte += (self.get_capture_start(ii) - prev_start_sample) * self.get_sample_size() * self.num_channels
490492
prev_start_sample = self.get_capture_start(ii)
491493
if ii >= index:
492494
break
@@ -498,15 +500,16 @@ def get_capture_byte_boundarys(self, index):
498500
end_byte += (
499501
(self.get_capture_start(index + 1) - self.get_capture_start(index))
500502
* self.get_sample_size()
501-
* self.get_num_channels()
503+
* self.num_channels
502504
)
503505
return (start_byte, end_byte)
504506

505507
def add_annotation(self, start_index, length=None, metadata=None):
506508
"""
507509
Insert annotation at start_index with length (if != None).
508510
"""
509-
assert start_index >= self._get_start_offset()
511+
if start_index < self.offset:
512+
raise SigMFFileError("Annotation start_index cannot be less than dataset start offset.")
510513

511514
new_annot = metadata or {}
512515
new_annot[self.START_INDEX_KEY] = start_index
@@ -559,7 +562,7 @@ def get_sample_size(self):
559562
Determines the size of a sample, in bytes, from the datatype of this set.
560563
For complex data, a 'sample' includes both the real and imaginary part.
561564
"""
562-
return dtype_info(self.get_global_field(self.DATATYPE_KEY))["sample_size"]
565+
return dtype_info(self.datatype)["sample_size"]
563566

564567
def _count_samples(self):
565568
"""
@@ -576,7 +579,7 @@ def _count_samples(self):
576579
file_size = self.data_file.stat().st_size if self.data_size_bytes is None else self.data_size_bytes
577580
file_data_size = file_size - self.get_global_field(self.TRAILING_BYTES_KEY, 0) - header_bytes # bytes
578581
sample_size = self.get_sample_size() # size of a sample in bytes
579-
num_channels = self.get_num_channels()
582+
num_channels = self.num_channels
580583
sample_count = file_data_size // sample_size // num_channels
581584
if file_data_size % (sample_size * num_channels) != 0:
582585
warnings.warn(
@@ -653,7 +656,7 @@ def set_data_file(
653656

654657
dtype = dtype_info(self.get_global_field(self.DATATYPE_KEY))
655658
self.is_complex_data = dtype["is_complex"]
656-
num_channels = self.get_num_channels()
659+
num_channels = self.num_channels
657660
self.ndim = 1 if (num_channels < 2) else 2
658661

659662
complex_int_separates = dtype["is_complex"] and dtype["is_fixedpoint"]
@@ -749,7 +752,7 @@ def read_samples_in_capture(self, index=0, autoscale=True):
749752
Samples are returned as an array of float or complex, with number of dimensions equal to NUM_CHANNELS_KEY.
750753
"""
751754
cb = self.get_capture_byte_boundarys(index)
752-
if (cb[1] - cb[0]) % (self.get_sample_size() * self.get_num_channels()):
755+
if (cb[1] - cb[0]) % (self.get_sample_size() * self.num_channels):
753756
warnings.warn(
754757
f"Capture `{index}` in `{self.data_file}` does not contain "
755758
"an integer number of samples across channels. It may be invalid."
@@ -788,11 +791,11 @@ def read_samples(self, start_index=0, count=-1, autoscale=True, raw_components=F
788791
raise SigMFFileError("Cannot read samples from a metadata only distribution.")
789792
else:
790793
raise SigMFFileError("No signal data file has been associated with the metadata.")
791-
first_byte = start_index * self.get_sample_size() * self.get_num_channels()
794+
first_byte = start_index * self.get_sample_size() * self.num_channels
792795

793796
if not self._is_conforming_dataset():
794797
warnings.warn(f"Recording dataset appears non-compliant, resulting data may be erroneous")
795-
return self._read_datafile(first_byte, count * self.get_num_channels(), autoscale, False)
798+
return self._read_datafile(first_byte, count * self.num_channels, autoscale, False)
796799

797800
def _read_datafile(self, first_byte, nitems, autoscale, raw_components):
798801
"""
@@ -807,7 +810,7 @@ def _read_datafile(self, first_byte, nitems, autoscale, raw_components):
807810
component_size = dtype["component_size"]
808811

809812
data_type_out = np.dtype("f4") if not self.is_complex_data else np.dtype("f4, f4")
810-
num_channels = self.get_num_channels()
813+
num_channels = self.num_channels
811814

812815
if self.data_file is not None:
813816
fp = open(self.data_file, "rb")

tests/testdata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
SigMFFile.DATATYPE_KEY: "rf32_le",
2020
SigMFFile.HASH_KEY: "f4984219b318894fa7144519185d1ae81ea721c6113243a52b51e444512a39d74cf41a4cec3c5d000bd7277cc71232c04d7a946717497e18619bdbe94bfeadd6",
2121
SigMFFile.NUM_CHANNELS_KEY: 1,
22+
SigMFFile.START_OFFSET_KEY: 0,
2223
SigMFFile.VERSION_KEY: __specification__,
2324
},
2425
}

0 commit comments

Comments
 (0)