- spectro_core.py: 共享核心(加载/标定/基线/寻峰/高斯拟合/双峰联合拟合/CSV-JSON) - peak_finder.py: 寻峰与峰信息导出,--pick 交互选峰(含 RAW 预览) - wavelength_calibration.py: 改用 core 寻峰,汞灯 577/579 双峰可分 - wavelength_player.py / hq_average.py: 复用 core 的标定与解码加载
66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import numpy as np
|
|
import wavelength_player as wp
|
|
|
|
|
|
def calibration(coeff=(.6, -8), span=(690, 927)):
|
|
data = dict(coefficients_descending=list(coeff), calibrated_pixel_range=list(span),
|
|
roi=[620, 620, 1034, 848])
|
|
with patch.object(Path, 'read_text', return_value=json.dumps(data)):
|
|
return wp.SavedCalibration('dummy.json')
|
|
|
|
|
|
class PlayerTests(unittest.TestCase):
|
|
def test_exact_saved_coefficients(self):
|
|
model = calibration()
|
|
np.testing.assert_array_equal(model.coeff, [.6, -8])
|
|
np.testing.assert_allclose(model([690, 742, 926.5]), [406, 437.2, 547.9])
|
|
|
|
def test_invalid_models(self):
|
|
for coeff, span in (([0, 3], [1, 2]), ([float('nan'), 1], [1, 2]),
|
|
([1], [1, 2]), ([1, 2], [4, 3])):
|
|
with self.assertRaises(ValueError):
|
|
calibration(coeff, span)
|
|
|
|
def test_quadratic_and_reverse_geometry(self):
|
|
source = SimpleNamespace(full_size=(2028, 1520), pattern='BGGR')
|
|
calibration([.00001, .5, 5]).validate_geometry(source)
|
|
calibration([-.5, 1000]).validate_geometry(source)
|
|
with self.assertRaises(ValueError):
|
|
calibration([.001, -1, 500]).validate_geometry(source)
|
|
|
|
def test_geometry_mismatch(self):
|
|
model = calibration()
|
|
model.data['recording'] = 'reference'
|
|
with patch.object(Path, 'is_file', return_value=True), patch.object(Path, 'read_text',
|
|
return_value=json.dumps(dict(width=4056, height=3040))):
|
|
with self.assertRaises(ValueError):
|
|
model.validate_geometry(SimpleNamespace(full_size=(2028, 1520), pattern='BGGR'))
|
|
|
|
def test_processing_signed_signal_and_sensor_coordinates(self):
|
|
hq = wp.load_player()
|
|
model = calibration()
|
|
wp.install_hooks(hq, model)
|
|
args = hq.parse_args(['--smooth', '1', '--black', '10'])
|
|
raw = np.full((16, 64), 8, dtype=np.uint16)
|
|
raw[:, 30:32] = 110
|
|
roi = hq.make_roi(10, 0, 60, 16, 64, 16)
|
|
result = hq.process_frame(raw, {}, roi, 'BGGR', args, model, (64, 16), 'SBGGR12', 12)
|
|
for c in hq.CHANNELS:
|
|
self.assertEqual(result.signal_raw[c][0], -2)
|
|
np.testing.assert_array_equal(result.signal[c], result.signal_raw[c])
|
|
np.testing.assert_allclose(result.x_plot[c], model(result.x_px[c]))
|
|
self.assertEqual(result.normalized[c].max(), 1)
|
|
self.assertGreaterEqual(result.x_px[c][0], 10)
|
|
|
|
def test_help(self):
|
|
self.assertEqual(wp.main(['--help']), 0)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|