Skip to content

Commit

Permalink
fixed sample count math per ke8ctn
Browse files Browse the repository at this point in the history
  • Loading branch information
Teque5 committed Jul 10, 2021
1 parent a43e871 commit 16db5fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ discussions, the more useful the standard is likely to be.
## Getting Started

This module can be installed the typical way:

```bash
pip install .
```

## Use Cases
## Load a SigMF archive; read all samples & metadata

### Load a SigMF archive; read all samples & metadata

```python
import sigmf
handle = sigmf.sigmffile.fromfile('example.sigmf')
Expand Down
45 changes: 23 additions & 22 deletions sigmf/sigmffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@


class SigMFFile():
"""API to manipulate SigMF files.
Parameters:
metadata -- Metadata. Either a string, or a dictionary.
data_file -- Path to the corresponding data file.
global_info -- Dictionary containing global header info.
"""
'''
API for SigMF I/O
Parameters
----------
metadata: str or dict, optional
Metadata for associated dataset.
data_file: str, optional
Path to associated dataset.
global_info: dict, optional
Set global field shortcut if creating new object.
skip_checksum: bool, default False
When True will skip calculating hash on data_file (if present) to check against metadata.
'''
START_INDEX_KEY = "core:sample_start"
LENGTH_INDEX_KEY = "core:sample_count"
START_OFFSET_KEY = "core:offset"
Expand Down Expand Up @@ -74,13 +79,7 @@ class SigMFFile():
CAPTURE_KEY = "captures"
ANNOTATION_KEY = "annotations"

def __init__(
self,
metadata=None,
data_file=None,
global_info=None,
skip_checksum=False,
):
def __init__(self, metadata=None, data_file=None, global_info=None, skip_checksum=False):
self.version = None
self.schema = None
if metadata is None:
Expand All @@ -102,7 +101,7 @@ def __str__(self):
return self.dumps()

def __repr__(self):
return "SigMFFile(%s)" % self
return f'SigMFFile({self})'

def _get_start_offset(self):
"""
Expand Down Expand Up @@ -273,14 +272,16 @@ def _count_samples(self):
else:
sample_count = 0
else:
file_size = path.getsize(self.data_file)
sample_size = self.get_sample_size()
file_size = path.getsize(self.data_file) # size of dataset in bytes
sample_size = self.get_sample_size() # size of a sample in bytes
num_channels = self.get_num_channels()
sample_count = file_size // sample_size // num_channels
if file_size % sample_count != 0:
warnings.warn("File '{}' does not contain an integer number of samples. It may be invalid data.".format(self.data_file))
if file_size % (sample_size * num_channels) != 0:
warnings.warn(f'File `{self.data_file}` does not contain an integer '
'number of samples across channels. It may be invalid data.')
if len(annotations) > 0 and annotations[-1][self.START_INDEX_KEY] + annotations[-1][self.LENGTH_INDEX_KEY] > sample_count:
warnings.warn("File '{}' ends before the final annotation in the corresponding SigMF metadata.".format(self.data_file))
warnings.warn(f'File `{self.data_file}` ends before the final annotation '
'in the corresponding SigMF metadata.')
self.sample_count = sample_count
return sample_count

Expand Down

0 comments on commit 16db5fb

Please sign in to comment.