sigmf.sigmffile.SigMFFile
- class sigmf.sigmffile.SigMFFile(metadata=None, data_file=None, global_info=None, skip_checksum=False, map_readonly=True, autoscale=True)
Bases:
SigMFMetafileMethods
Insert annotation at start_index with length (if != None).
Insert capture info for sample starting at start_index.
Dump contents to SigMF archive format.
Calculates the hash of the data file and adds it to the global section.
Write metadata to a file.
Get a string representation of the metadata.
Get relevant annotations from metadata.
Returns a tuple of the file byte range in a dataset of a given SigMF capture of the form [start, stop).
get_capture_byte_boundarysReturns a dictionary containing all the capture information at sample index.
Returns a the start sample index of a given capture, will raise SigMFAccessError if this field is missing.
Returns a list of dictionaries representing all captures.
Return a field from the global info, or default if the field is not set.
Returns a dictionary with all the global info.
Return integer number of channels.
Determines the size of a sample, in bytes, from the datatype of this set.
Return a schema object valid for the current metadata
Get a nicer representation of _metadata.
Reads the specified number of samples starting at the specified index from the associated data file.
Reads samples from the specified captures segment in its entirety.
Set the datafile path, then recalculate sample count.
Inserts a value into the global field.
Recursively override existing global metadata with new global metadata.
Read provided metadata as either None (empty), string, bytes, or dictionary.
Write metadata file or archive based on file extension.
Check schema and throw error if issue.
Attributes
ANNOTATION_KEYAUTHOR_KEYCAPTURE_KEYCOLLECTION_KEYCOMMENT_KEYDATASET_KEYDATATYPE_KEYDATA_DOI_KEYDATETIME_KEYDESCRIPTION_KEYEXTENSIONS_KEYFREQUENCY_KEYFREQ_LOWER_EDGE_KEYFREQ_UPPER_EDGE_KEYGENERATOR_KEYGEOLOCATION_KEYGLOBAL_INDEX_KEYGLOBAL_KEYHEADER_BYTES_KEYHW_KEYLABEL_KEYLATITUDE_KEYLICENSE_KEYLONGITUDE_KEYMETADATA_ONLY_KEYMETA_DOI_KEYNUM_CHANNELS_KEYOFFSET_KEYRECORDER_KEYSAMPLE_COUNT_KEYSAMPLE_RATE_KEYSAMPLE_START_KEYSHA512_KEYTRAILING_BYTES_KEYUUID_KEYVALID_ANNOTATION_KEYSVALID_CAPTURE_KEYSVALID_GLOBAL_KEYSVALID_KEYSVERSION_KEYversion that may have been present in the metadata when this SigMFFile was loaded, before normalization to the current spec.
- add_annotation(start_index, length=None, metadata=None)
Insert annotation at start_index with length (if != None).
- add_capture(start_index, metadata=None)
Insert capture info for sample starting at start_index. If there is already capture info for this index, metadata will be merged with the existing metadata, overwriting keys if they were previously set.
- archive(name=None, fileobj=None, compression=None, overwrite=False)
Dump contents to SigMF archive format.
name and fileobj are passed to SigMFArchive and are defined there.
- Parameters:
name (str, optional) – Name of the archive file to create. If None, a temporary file will be created.
fileobj (file-like object, optional) – A file-like object to write the archive to. If None, a file will be created at name.
compression (str, optional) – Compression type: “gz”, “xz”, “zip”, or None (default). If None and name has a recognized compressed extension, compression is auto-detected from the extension.
overwrite (bool, default False) – If False, raise exception if archive file already exists.
- calculate_hash()
Calculates the hash of the data file and adds it to the global section. Also returns a string representation of the hash.
- property declared_version: str | None
version that may have been present in the metadata when this SigMFFile was loaded, before normalization to the current spec.
- Type:
Return the core
- dump(filep, pretty=True)
Write metadata to a file.
- dumps(pretty=True)
Get a string representation of the metadata.
- Parameters:
pretty (bool, default True) – When True will write more human-readable output, otherwise will be flat JSON.
- Returns:
String representation of the metadata using json formatter.
- Return type:
string
- get_annotations(index=None)
Get relevant annotations from metadata.
- get_capture_byte_boundaries(index)
Returns a tuple of the file byte range in a dataset of a given SigMF capture of the form [start, stop). This function works on either compliant or noncompliant SigMF Recordings.
- get_capture_info(index)
Returns a dictionary containing all the capture information at sample index.
- get_capture_start(index)
Returns a the start sample index of a given capture, will raise SigMFAccessError if this field is missing.
- get_captures()
Returns a list of dictionaries representing all captures.
- get_global_field(key, default=None)
Return a field from the global info, or default if the field is not set.
- get_global_info()
Returns a dictionary with all the global info.
- get_num_channels()
Return integer number of channels.
- get_sample_size()
Determines the size of a sample, in bytes, from the datatype of this set. For complex data, a ‘sample’ includes both the real and imaginary part.
- get_schema()
Return a schema object valid for the current metadata
- ordered_metadata()
Get a nicer representation of _metadata. Will sort keys, but put the top-level fields ‘global’, ‘captures’, ‘annotations’ in front.
- Returns:
ordered_meta – Cleaner representation of _metadata with top-level keys correctly ordered and the rest of the keys sorted.
- Return type:
OrderedDict
- read_samples(start_index=0, count=-1)
Reads the specified number of samples starting at the specified index from the associated data file.
- Parameters:
- Returns:
data – Samples are returned as an array of float or complex, with number of dimensions equal to NUM_CHANNELS_KEY. Scaling behavior depends on the autoscale parameter set during construction.
- Return type:
ndarray
- read_samples_in_capture(index=0)
Reads samples from the specified captures segment in its entirety.
- Parameters:
- Returns:
data – Samples are returned as an array of float or complex, with number of dimensions equal to NUM_CHANNELS_KEY.
- Return type:
ndarray
- set_data_file(data_file=None, data_buffer=None, skip_checksum=False, offset=0, size_bytes=None, map_readonly=True)
Set the datafile path, then recalculate sample count. Update the hash and return the hash string if enabled.
- set_global_field(key, value)
Inserts a value into the global field.
- set_global_info(new_global)
Recursively override existing global metadata with new global metadata.
- set_metadata(metadata)
Read provided metadata as either None (empty), string, bytes, or dictionary.
If an existing core:version is present, it is preserved in the declared_version property, but the metadata will always report the current library version.
- tofile(file_path, pretty=True, toarchive=False, compression=None, skip_validate=False, overwrite=False)
Write metadata file or archive based on file extension.
The file extension determines the output format: - No extension or other extension → .sigmf-meta file (and .sigmf-data if data_buffer exists) - .sigmf → uncompressed archive - .sigmf.gz, .sigmf.xz, .sigmf.zip → compressed archive
- Parameters:
file_path (string) – Location to save. Extension determines output format.
pretty (bool, default True) – When True will write human-readable JSON, otherwise flat JSON.
toarchive (bool, default False) – If True, forces archive creation (writes metadata and data to archive) regardless of file extension.
compression (str, optional) – Compression type: “gz”, “xz”, “zip”, or None. If specified, must match file extension if extension implies compression. If no archive extension is present, creates a compressed archive.
skip_validate (bool, default False) – Skip validation of metadata before writing.
overwrite (bool, default False) – If False, raise exception if output file already exists.
Examples
>>> import numpy as np >>> import tempfile >>> from pathlib import Path >>> import sigmf >>> meta = sigmf.fromarray(np.arange(16, dtype=np.float32)) >>> tmpdir = Path(tempfile.mkdtemp()) >>> meta.tofile(tmpdir / 'recording') # creates recording.sigmf-meta and recording.sigmf-data pair >>> meta.tofile(tmpdir / 'recording.sigmf') # creates recording.sigmf (archive) >>> meta.tofile(tmpdir / 'recording.sigmf.gz') # creates recording.sigmf.gz (compressed) >>> meta.tofile(tmpdir / 'other', compression='xz') # creates other.sigmf.xz
- validate()
Check schema and throw error if issue.