Note
Click here to download the full example code
Custom feature extraction¶
Following examples shows how to compute custom features and save them in the transient feature database (trfdb) to visualize them in VisualAE.
The feature extraction can be live during acquisition. VisualAE will be notified, that a writer to the trfdb is active and waits for the features to be computed. Therefore, the computed features can be visualized in real time.
import os
from tempfile import gettempdir
import numpy as np
import vallenae as vae
HERE = os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()
TRADB = os.path.join(HERE, "steel_plate/sample_plain.tradb")
# TRFDB = os.path.join(HERE, "steel_plate/sample.trfdb")
TRFDB_TMP = os.path.join(gettempdir(), "sample_custom.trfdb") # use a temp file for demo
Custom feature extraction algorithms¶
def rms(data: np.ndarray) -> float:
"""Root mean square (RMS)."""
return np.sqrt(np.mean(data ** 2))
def crest_factor(data: np.ndarray) -> float:
"""Crest factor (ratio of peak amplitude and RMS)."""
return np.max(np.abs(data)) / rms(data)
def spectral_peak_frequency(spectrum_: np.ndarray, samplerate: int) -> float:
"""
Peak frequency in a spectrum.
Args:
spectrum: FFT amplitudes
samplerate: Sample rate of the spectrum in Hz
Returns:
Peak frequency in Hz
"""
def bin_to_hz(samplerate: int, samples: int, index: int):
return 0.5 * samplerate * index / (samples - 1)
peak_index = np.argmax(spectrum_)
return bin_to_hz(samplerate, len(spectrum_), peak_index)
Open tradb and trfdb¶
Helper function to notify VisualAE, that the transient feature database is active/closed
def set_file_status(trfdb_: vae.io.TrfDatabase, status: int):
"""Notify VisualAE that trfdb is active/closed."""
trfdb_.connection().execute(
f"UPDATE trf_globalinfo SET Value = {status} WHERE Key == 'FileStatus'"
)
Read tra records, compute features and save to trfdb¶
The vallenae.io.TraDatabase.listen
method will read the tradb row by row and can be used during
acquisition. Only if the acquisition is closed and no new records are available, the function
returns.
set_file_status(trfdb, 2) # 2 = active
for tra in tradb.listen(existing=True, wait=False):
spectrum = np.fft.rfft(tra.data)
features = vae.io.FeatureRecord(
trai=tra.trai,
features={
"RMS": rms(tra.data),
"CrestFactor": crest_factor(tra.data),
"SpectralPeakFreq": spectral_peak_frequency(spectrum, tra.samplerate),
}
)
trfdb.write(features)
set_file_status(trfdb, 0) # 0 = closed
Write field infos to trfdb¶
Field infos can be written with vallenae.io.TrfDatabase.write_fieldinfo
:
trfdb.write_fieldinfo("RMS", {"Unit": "[V]", "LongName": "Root mean square"})
trfdb.write_fieldinfo("CrestFactor", {"Unit": "[]", "LongName": "Crest factor"})
trfdb.write_fieldinfo("SpectralPeakFreq", {"Unit": "[Hz]", "LongName": "Spectral peak frequency"})
Read results from trfdb¶
Trf: 0%| | 0/4 [00:00<?, ?it/s]
Trf: 100%|##########| 4/4 [00:00<00:00, 46474.28it/s]
RMS CrestFactor SpectralPeakFreq
trai
1 0.003761 12.359648 134751.414713
2 0.003316 17.926798 139678.030303
3 0.002585 13.149483 166379.238697
4 0.002555 11.397170 137914.672388
Total running time of the script: ( 0 minutes 0.083 seconds)