From ddf445fd4d2e0f2b5b770c8e6a2026cb87857bc3 Mon Sep 17 00:00:00 2001 From: madrigal Date: Wed, 22 Oct 2025 10:55:06 -0400 Subject: [PATCH] Moved auto filename generator from data.recording to io.recording --- src/ria_toolkit_oss/datatypes/recording.py | 36 ------------------- src/ria_toolkit_oss/io/recording.py | 40 ++++++++++++++++++++-- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/ria_toolkit_oss/datatypes/recording.py b/src/ria_toolkit_oss/datatypes/recording.py index b2bef0e..f606aca 100644 --- a/src/ria_toolkit_oss/datatypes/recording.py +++ b/src/ria_toolkit_oss/datatypes/recording.py @@ -1,7 +1,6 @@ from __future__ import annotations import copy -import datetime import hashlib import json import os @@ -12,7 +11,6 @@ from typing import Any, Iterator, Optional import numpy as np from numpy.typing import ArrayLike -from quantiphy import Quantity from ria_toolkit_oss.datatypes.annotation import Annotation @@ -598,40 +596,6 @@ class Recording: scaled_data = self.data / np.max(abs(self.data)) return Recording(data=scaled_data, metadata=self.metadata, annotations=self.annotations) - def generate_filename(self, tag: Optional[str] = "rec"): - """Generate a filename from metadata. - - :param tag: The string at the beginning of the generated filename. Default is "rec". - :type tag: str, optional - - :return: A filename without an extension. - :rtype: str - """ - # TODO: This method should be refactored to use the first 7 characters of the 'rec_id' field. - - tag = tag + "_" - source = self.metadata.get("source", "") - if source != "": - source = source + "_" - - # converts 1000 to 1k for example - center_frequency = str(Quantity(self.metadata.get("center_frequency", 0))) - if center_frequency != "0": - num = center_frequency[:-1] - suffix = center_frequency[-1] - num = int(np.round(float(num))) - else: - num = 0 - suffix = "" - center_frequency = str(num) + suffix + "Hz_" - - timestamp = int(self.timestamp) - timestamp = datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d_%H-%M-%S") + "_" - - # Add first seven characters of rec_id for uniqueness - rec_id = self.rec_id[0:7] - return tag + source + center_frequency + timestamp + rec_id - def __len__(self) -> int: """The length of a recording is defined by the number of complex samples in each channel of the recording.""" return self.shape[1] diff --git a/src/ria_toolkit_oss/io/recording.py b/src/ria_toolkit_oss/io/recording.py index eacbf68..d1d6105 100644 --- a/src/ria_toolkit_oss/io/recording.py +++ b/src/ria_toolkit_oss/io/recording.py @@ -2,6 +2,7 @@ Utilities for input/output operations on the ria_toolkit_oss.datatypes.Recording object. """ +import datetime import datetime as dt import os from datetime import timezone @@ -9,6 +10,7 @@ from typing import Optional import numpy as np import sigmf +from quantiphy import Quantity from sigmf import SigMFFile, sigmffile from sigmf.utils import get_data_type_str @@ -126,7 +128,7 @@ def to_sigmf( if filename is not None: filename, _ = os.path.splitext(filename) else: - filename = recording.generate_filename() + filename = generate_filename(recording=recording) if path is None: path = "recordings" @@ -294,7 +296,7 @@ def to_npy( if filename is not None: filename, _ = os.path.splitext(filename) else: - filename = recording.generate_filename() + filename = generate_filename(recording=recording) filename = filename + ".npy" if path is None: @@ -351,3 +353,37 @@ def from_npy(file: os.PathLike | str) -> Recording: recording = Recording(data=data, metadata=metadata, annotations=annotations) return recording + + +def generate_filename(recording: Recording, tag: Optional[str] = "rec"): + """Generate a filename from metadata. + + :param tag: The string at the beginning of the generated filename. Default is "rec". + :type tag: str, optional + + :return: A filename without an extension. + :rtype: str + """ + + tag = tag + "_" + source = recording.metadata.get("source", "") + if source != "": + source = source + "_" + + # converts 1000 to 1k for example + center_frequency = str(Quantity(recording.metadata.get("center_frequency", 0))) + if center_frequency != "0": + num = center_frequency[:-1] + suffix = center_frequency[-1] + num = int(np.round(float(num))) + else: + num = 0 + suffix = "" + center_frequency = str(num) + suffix + "Hz_" + + timestamp = int(recording.timestamp) + timestamp = datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d_%H-%M-%S") + "_" + + # Add first seven characters of rec_id for uniqueness + rec_id = recording.rec_id[0:7] + return tag + source + center_frequency + timestamp + rec_id