@@ -323,15 +323,15 @@ def __getitem__(self, sli):
323323 raise ValueError ("unhandled ndim in SigMFFile.__getitem__(); this shouldn't happen" )
324324 return ray [0 ] if isinstance (sli , int ) else ray # return element instead of 1-element array
325325
326- def _get_start_offset (self ):
327- """
328- Return the offset of the first sample.
329- """
330- return self .get_global_field (self .START_OFFSET_KEY , 0 )
331-
332326 def get_num_channels (self ):
333- """Returns integer number of channels if present, otherwise 1"""
334- return self .get_global_field (self .NUM_CHANNELS_KEY , 1 )
327+ """Return integer number of channels."""
328+ warnings .warn (
329+ "get_num_channels() is deprecated and will be removed in a future version of sigmf. "
330+ "Use the 'num_channels' attribute instead." ,
331+ DeprecationWarning ,
332+ stacklevel = 2 ,
333+ )
334+ return self .num_channels
335335
336336 def _is_conforming_dataset (self ):
337337 """
@@ -382,9 +382,11 @@ def set_metadata(self, metadata):
382382 else :
383383 raise SigMFError ("Unable to interpret provided metadata." )
384384
385- # if num_channels missing, default to 1
385+ # ensure fields required for parsing are present or use defaults
386386 if self .get_global_field (self .NUM_CHANNELS_KEY ) is None :
387387 self .set_global_field (self .NUM_CHANNELS_KEY , 1 )
388+ if self .get_global_field (self .START_OFFSET_KEY ) is None :
389+ self .set_global_field (self .START_OFFSET_KEY , 0 )
388390
389391 # set version to current implementation
390392 self .set_global_field (self .VERSION_KEY , __specification__ )
@@ -422,10 +424,8 @@ def add_capture(self, start_index, metadata=None):
422424 If there is already capture info for this index, metadata will be merged
423425 with the existing metadata, overwriting keys if they were previously set.
424426 """
425- if start_index < self ._get_start_offset ():
426- raise SigMFAccessError (
427- "`start_index` {} must be >= the global start index {}" .format (start_index , self ._get_start_offset ())
428- )
427+ if start_index < self .offset :
428+ raise SigMFAccessError ("Capture start_index cannot be less than dataset start offset." )
429429 capture_list = self ._metadata [self .CAPTURE_KEY ]
430430 new_capture = metadata or {}
431431 new_capture [self .START_INDEX_KEY ] = start_index
@@ -451,16 +451,13 @@ def get_captures(self):
451451
452452 def get_capture_info (self , index ):
453453 """
454- Returns a dictionary containing all the capture information at sample
455- 'index'.
454+ Returns a dictionary containing all the capture information at sample index.
456455 """
457- if index < self ._get_start_offset ():
458- raise SigMFAccessError (
459- "`start_index` {} must be >= the global start index {}" .format (index , self ._get_start_offset ())
460- )
456+ if index < self .offset :
457+ raise SigMFAccessError ("Sample index cannot be less than dataset start offset." )
461458 captures = self ._metadata .get (self .CAPTURE_KEY , [])
462459 if len (captures ) == 0 :
463- raise SigMFAccessError ("No captures are present! " )
460+ raise SigMFAccessError ("No captures in metadata. " )
464461 cap_info = captures [0 ]
465462 for capture in captures :
466463 if capture [self .START_INDEX_KEY ] > index :
@@ -493,9 +490,7 @@ def get_capture_byte_boundarys(self, index):
493490 prev_start_sample = 0
494491 for ii , capture in enumerate (self .get_captures ()):
495492 start_byte += capture .get (self .HEADER_BYTES_KEY , 0 )
496- start_byte += (
497- (self .get_capture_start (ii ) - prev_start_sample ) * self .get_sample_size () * self .get_num_channels ()
498- )
493+ start_byte += (self .get_capture_start (ii ) - prev_start_sample ) * self .get_sample_size () * self .num_channels
499494 prev_start_sample = self .get_capture_start (ii )
500495 if ii >= index :
501496 break
@@ -507,19 +502,16 @@ def get_capture_byte_boundarys(self, index):
507502 end_byte += (
508503 (self .get_capture_start (index + 1 ) - self .get_capture_start (index ))
509504 * self .get_sample_size ()
510- * self .get_num_channels ()
505+ * self .num_channels
511506 )
512507 return (start_byte , end_byte )
513508
514509 def add_annotation (self , start_index , length = None , metadata = None ):
515510 """
516511 Insert annotation at start_index with length (if != None).
517512 """
518-
519- if start_index < self ._get_start_offset ():
520- raise SigMFAccessError (
521- "`start_index` {} must be >= the global start index {}" .format (start_index , self ._get_start_offset ())
522- )
513+ if start_index < self .offset :
514+ raise SigMFAccessError ("Annotation start_index cannot be less than dataset start offset." )
523515
524516 new_annot = metadata or {}
525517 new_annot [self .START_INDEX_KEY ] = start_index
@@ -573,7 +565,7 @@ def get_sample_size(self):
573565 Determines the size of a sample, in bytes, from the datatype of this set.
574566 For complex data, a 'sample' includes both the real and imaginary part.
575567 """
576- return dtype_info (self .get_global_field ( self . DATATYPE_KEY ) )["sample_size" ]
568+ return dtype_info (self .datatype )["sample_size" ]
577569
578570 def _count_samples (self ):
579571 """
@@ -590,7 +582,7 @@ def _count_samples(self):
590582 file_size = self .data_file .stat ().st_size if self .data_size_bytes is None else self .data_size_bytes
591583 file_data_size = file_size - self .get_global_field (self .TRAILING_BYTES_KEY , 0 ) - header_bytes # bytes
592584 sample_size = self .get_sample_size () # size of a sample in bytes
593- num_channels = self .get_num_channels ()
585+ num_channels = self .num_channels
594586 sample_count = file_data_size // sample_size // num_channels
595587 if file_data_size % (sample_size * num_channels ) != 0 :
596588 warnings .warn (
@@ -667,7 +659,7 @@ def set_data_file(
667659
668660 dtype = dtype_info (self .get_global_field (self .DATATYPE_KEY ))
669661 self .is_complex_data = dtype ["is_complex" ]
670- num_channels = self .get_num_channels ()
662+ num_channels = self .num_channels
671663 self .ndim = 1 if (num_channels < 2 ) else 2
672664
673665 complex_int_separates = dtype ["is_complex" ] and dtype ["is_fixedpoint" ]
@@ -763,7 +755,7 @@ def read_samples_in_capture(self, index=0, autoscale=True):
763755 Samples are returned as an array of float or complex, with number of dimensions equal to NUM_CHANNELS_KEY.
764756 """
765757 cb = self .get_capture_byte_boundarys (index )
766- if (cb [1 ] - cb [0 ]) % (self .get_sample_size () * self .get_num_channels () ):
758+ if (cb [1 ] - cb [0 ]) % (self .get_sample_size () * self .num_channels ):
767759 warnings .warn (
768760 f"Capture `{ index } ` in `{ self .data_file } ` does not contain "
769761 "an integer number of samples across channels. It may be invalid."
@@ -802,11 +794,11 @@ def read_samples(self, start_index=0, count=-1, autoscale=True, raw_components=F
802794 raise SigMFFileError ("Cannot read samples from a metadata only distribution." )
803795 else :
804796 raise SigMFFileError ("No signal data file has been associated with the metadata." )
805- first_byte = start_index * self .get_sample_size () * self .get_num_channels ()
797+ first_byte = start_index * self .get_sample_size () * self .num_channels
806798
807799 if not self ._is_conforming_dataset ():
808800 warnings .warn (f"Recording dataset appears non-compliant, resulting data may be erroneous" )
809- return self ._read_datafile (first_byte , count * self .get_num_channels () , autoscale , False )
801+ return self ._read_datafile (first_byte , count * self .num_channels , autoscale , False )
810802
811803 def _read_datafile (self , first_byte , nitems , autoscale , raw_components ):
812804 """
@@ -821,7 +813,7 @@ def _read_datafile(self, first_byte, nitems, autoscale, raw_components):
821813 component_size = dtype ["component_size" ]
822814
823815 data_type_out = np .dtype ("f4" ) if not self .is_complex_data else np .dtype ("f4, f4" )
824- num_channels = self .get_num_channels ()
816+ num_channels = self .num_channels
825817
826818 if self .data_file is not None :
827819 fp = open (self .data_file , "rb" )
0 commit comments