|
9 | 9 | from .core.device_data import RecordingFrame |
10 | 10 |
|
11 | 11 |
|
| 12 | +def save_recording( |
| 13 | + data: np.ndarray, |
| 14 | + path: str | Path, |
| 15 | + sfreq: float | None = None, |
| 16 | + ch_names: list[str] | None = None, |
| 17 | + logger: Logger | None = None, |
| 18 | +) -> None: |
| 19 | + _log = logger or getLogger(__name__) |
| 20 | + dest = Path(path) |
| 21 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 22 | + |
| 23 | + arrays: dict[str, Any] = { |
| 24 | + "timestamps": np.array([time.time()]), |
| 25 | + "data": data, |
| 26 | + } |
| 27 | + if sfreq is not None: |
| 28 | + arrays["sfreq"] = np.float64(sfreq) |
| 29 | + if ch_names is not None: |
| 30 | + arrays["ch_names"] = np.array(ch_names, dtype=str) |
| 31 | + |
| 32 | + np.savez_compressed(dest, **arrays) |
| 33 | + _log.info("Saved recording to %s", dest) |
| 34 | + |
| 35 | + |
12 | 36 | class EEGRecorder: |
13 | 37 | """Rejestrator EEG wykorzystujący wysokowydajny format binarny NumPy.""" |
14 | 38 |
|
15 | 39 | def __init__( |
16 | 40 | self, |
17 | 41 | device: EEGDevice, |
18 | 42 | filename: str, |
| 43 | + output_dir: str | Path = "recordings", |
| 44 | + sfreq: float | None = None, |
| 45 | + ch_names: list[str] | None = None, |
19 | 46 | logger: Logger | None = None, |
20 | 47 | autosave: bool = True, |
21 | 48 | connect_device: bool = True, |
22 | 49 | ) -> None: |
23 | 50 | self._logger: Final[Logger] = logger or getLogger(__name__) |
24 | 51 | self._device: Final[EEGDevice] = device |
25 | 52 | self._filename: Final[str] = filename |
| 53 | + self._output_dir: Final[Path] = Path(output_dir) |
| 54 | + self._sfreq: float | None = sfreq |
| 55 | + self._ch_names: list[str] | None = ch_names |
26 | 56 | self._autosave: Final[bool] = autosave |
27 | 57 | self._connect_device: Final[bool] = connect_device |
28 | 58 | self._frames: list[RecordingFrame] = [] |
@@ -51,14 +81,19 @@ def save(self) -> None: |
51 | 81 | return |
52 | 82 |
|
53 | 83 | try: |
54 | | - output_dir: Final[Path] = Path("recordings") |
55 | | - output_dir.mkdir(exist_ok=True) |
56 | | - file_path: Final[Path] = output_dir / self._filename |
| 84 | + self._output_dir.mkdir(parents=True, exist_ok=True) |
| 85 | + file_path: Final[Path] = self._output_dir / self._filename |
57 | 86 |
|
58 | 87 | timestamps: Final[np.ndarray[Any, Any]] = np.array([f.timestamp for f in self._frames]) |
59 | 88 | data_blocks: Final[np.ndarray[Any, Any]] = np.concatenate([f.data for f in self._frames], axis=1) |
60 | 89 |
|
61 | | - np.savez_compressed(file_path, timestamps=timestamps, data=data_blocks) |
| 90 | + arrays: dict[str, Any] = {"timestamps": timestamps, "data": data_blocks} |
| 91 | + if self._sfreq is not None: |
| 92 | + arrays["sfreq"] = np.float64(self._sfreq) |
| 93 | + if self._ch_names is not None: |
| 94 | + arrays["ch_names"] = np.array(self._ch_names, dtype=str) |
| 95 | + |
| 96 | + np.savez_compressed(file_path, **arrays) |
62 | 97 |
|
63 | 98 | self._logger.info("Saved session to binary file: %s", file_path) |
64 | 99 |
|
|
0 commit comments