Base Model für CITT erstellt, PTM Dortmund ergänzt, Tests hinzugefügt

This commit is contained in:
Markus Clauß
2023-02-28 13:56:11 +01:00
parent b248a7e9b1
commit e861dbf10e
17 changed files with 917 additions and 103 deletions

0
tests/__init__.py Normal file
View File

View File

View File

@@ -0,0 +1,50 @@
import logging
import os
import toml
from src.paveit.helper import read_file_to_bytesio
from src.paveit.labtest.citt import CITT_PTMDortmund
logger = logging.getLogger(__name__)
def test_base_class():
pass
def test_citt_ptmdortmund():
data_path = 'tests/data/citt/PTM_Dortmund'
res_dict = toml.load(os.path.join(data_path, 'meta.toml'))
logger.info(res_dict)
for filename, meta in res_dict.items():
logger.info(f'run test on: {filename}, {meta}')
file = os.path.join(data_path, filename)
buf = read_file_to_bytesio(file)
metadata = {'org': 'pytest_ptm_dortmund'}
res = CITT_PTMDortmund(filename, metadata, archive=False,
data=buf)
res.run()
fit = res.fit.reset_index()
assert len(fit) == 5
m = res_dict[filename]
for col in ['F', 's_hor_sum', 's_hor_1', 's_hor_2']:
assert all(fit[f'fit_{col}_r2'] >= m['min_r2'])
sel = fit[(fit['f']==10.0) & (fit['sigma']==0.2) & (fit['T']==20.0)].iloc[0]
Emin = (1-m['max_diff'])*m['stiffness_10Hz']
Emax = (1+m['max_diff'])*m['stiffness_10Hz']
assert Emin <= sel['E'] <= Emax

116
tests/analysis/sine_test.py Normal file
View File

@@ -0,0 +1,116 @@
from random import uniform
import numpy as np
from paveit.analysis.regression import fit_cos, fit_cos_eval
def fit(freq: float = 10,
ampl: float = 100.0,
offset: float = 20.0,
slope: float = 0.1,
phase: float = 0.05,
error: float = 0.001) -> None:
N: int = 5
num_samples_per_cycle: int = 50
t = np.linspace(0, N / freq, N * num_samples_per_cycle)
y = ampl * np.cos(2 * np.pi * freq * t + phase) + slope * t + offset
r = fit_cos(t, y)
error_min = (1 - error)
error_max = (1 + error)
# ampltude
rel_error = (r['amp'] / ampl)
assert error_min <= rel_error <= error_max
# offset
rel_error = (r['offset'] / offset)
assert error_min <= rel_error <= error_max
# slope
rel_error = (r['slope'] / slope)
assert error_min <= rel_error <= error_max
# phase
rel_error = (r['phase'] / phase)
assert error_min <= rel_error <= error_max
# freq
rel_error = (r['freq'] / freq)
assert error_min <= rel_error <= error_max
def test_fit_simple_sine(ntest: int = 50) -> None:
"""
fit a simple sine signal and evaluate amplitude
error: percentage error of ampl, Error max 0.1 %
"""
fit()
#run multiple tests with random parameters
for i in range(ntest):
fit(
ampl=uniform(1e-3, 1000),
offset=uniform(1e-3, 1),
slope=uniform(1e-5, 1),
phase=uniform(1e-5, 1),
)
def fit_noise(freq: float = 10,
ampl: float = 100.0,
offset: float = 20.0,
slope: float = 0.1,
phase: float = 0.05,
noise_level: float = 0.01,
error: float = 0.01) -> None:
N: int = 5
num_samples_per_cycle: int = 50
t = np.linspace(0, N / freq, N * num_samples_per_cycle)
y = ampl * np.cos(2 * np.pi * freq * t + phase) + slope * t + offset
y_noise = np.random.normal(0, noise_level * ampl, len(t))
y = y + y_noise
r = fit_cos(t, y)
error_min = (1 - error)
error_max = (1 + error)
# ampltude
rel_error = (r['amp'] / ampl)
assert error_min <= rel_error <= error_max
# freq
rel_error = (r['freq'] / freq)
assert error_min <= rel_error <= error_max
def test_fit_simple_sine_with_noise(ntest: int = 50) -> None:
"""
fit a simple sine signal and evaluate amplitude
error: percentage error of ampl, Error max 0.1 %
"""
fit_noise()
#run multiple tests with random parameters
for i in range(ntest):
fit_noise(
ampl=uniform(1e-3, 1000),
offset=uniform(1e-3, 1),
slope=uniform(1e-5, 1),
phase=uniform(1e-5, 1),
noise_level=uniform(0.01, 0.1),
error=0.02,
)

View File

@@ -0,0 +1,4 @@
["sample_01.xlsm"]
min_r2 = 0.993
max_diff = 0.005 #%
stiffness_10Hz = 2269.0 #MPa

Binary file not shown.

0
tests/helper/__init__.py Normal file
View File

View File

@@ -0,0 +1,24 @@
import glob
import logging
import os
from src.paveit.helper import read_file_to_bytesio
logger = logging.getLogger(__name__)
data_path = 'tests/data/citt/PTM_Dortmund'
def test_read_file_compare_filesize():
files = glob.glob(os.path.join(data_path, '*.xlsm'))
for file in files:
file_stat = os.stat(file)
file_size = file_stat.st_size
buf = read_file_to_bytesio(file)
buf_size = buf.getbuffer().nbytes
assert file_size == buf_size