From 3d70da287f491054532ea23f069cc622b7fc5066 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 13 Mar 2026 20:47:41 +0000 Subject: [PATCH 1/4] Initial plan From 7967e4bd3fea3164cb818506b8d9ad9d7e275103 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 13 Mar 2026 21:08:53 +0000 Subject: [PATCH 2/4] Fix interp2d removal and add support for new HyperBB data/calibration formats Co-authored-by: doizuc <6456289+doizuc@users.noreply.github.com> --- .../cfg/HBB8005_Cal_new_format_example.mat | Bin 0 -> 24656 bytes inlinino/gui.py | 8 + inlinino/instruments/hyperbb.py | 166 ++++++++--- inlinino/resources/setup_hyperbb.ui | 5 + test/test_hyperbb.py | 267 ++++++++++++++++++ 5 files changed, 410 insertions(+), 36 deletions(-) create mode 100644 inlinino/cfg/HBB8005_Cal_new_format_example.mat create mode 100644 test/test_hyperbb.py diff --git a/inlinino/cfg/HBB8005_Cal_new_format_example.mat b/inlinino/cfg/HBB8005_Cal_new_format_example.mat new file mode 100644 index 0000000000000000000000000000000000000000..5b76fbb35bc66b59c78d136df199f0cd958ea3c9 GIT binary patch literal 24656 zcmeFZcU)D!7B46W2vS5;P*6eXC`w0BSci@X(wh|Nz4tD?BfTTiq)2a41Vcwqnh1h4 zMMRK}2qLI+@aNBaKX2x~nRnmJ{PoK@IXPKLcCyyWPO{fGJF>#cGQuKsyq7rWWQCR4 zjm<2L=;ST+9F1-4tpw<7Z5+&8FVczH8|gV38PeHU3(!f}o6*VY+0$`y({XVMaPSIn zbJ1~eaPb}ZFC5MdSu$*~XLk-9Ai#!%SdaVrGWfsaIN0!SPmKLq+WcE= z|F4zPzZ&Q8E6vJDLeId_#{RF^UwLGV43&+nY=5Qi|2Udi896xW{Ts!_@=xOLKL$2N z#>W365gXsv!xgOmTMzr;zWcw@!#-Y7wvQ-#C^YC2r|5$!MzVH51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0;wV^R4(s_Zzuw2Y-~NmKv@3Q+`)_0= z-Y?jH_c`0nrc25Dx9)H>i!zSbe>K_0dDp+J{d<4-#NX#m-v4oJj{571iTf?wzs=iv zDGxY*+;K@UC3FAsV>KD)RDtw5*4e7d6ZhLp|KPemt$6>|76z45wnh8vO6NUQI+VAc zw{@puQC8vpooD}FU-%O^AE#54D08l4fBb5@Q}xV+`{l%bI_%NS-miH2o)iag-f-sg z$qY_^iuOx9yQ=bt9XS7B*&Vwmy?B56_G$g?k4pB(wu_m4OD)`J9sdFXU6E zO=+mvzv1PbO9$rG@8{b3*z==n^Zv}eaj(ACezISEgyBZWzmN9c?@cRNapDKN>}MQf@i|%X#Xf4zvq^h-zS;u- DL!1tR literal 0 HcmV?d00001 diff --git a/inlinino/gui.py b/inlinino/gui.py index 1823b9b..e662f5b 100644 --- a/inlinino/gui.py +++ b/inlinino/gui.py @@ -1077,6 +1077,14 @@ def act_save(self): except FileNotFoundError: self.notification(f"No such calibration file: {self.cfg['calibration_file']}") return + elif self.cfg['module'] == 'hyperbb': + self.cfg['manufacturer'] = 'Sequoia' + self.cfg['model'] = 'HyperBB' + # Temperature file is optional when using a new combined calibration file + if 'Temperature File' in empty_fields: + empty_fields.remove('Temperature File') + if not self.cfg.get('temperature_file'): + self.cfg.pop('temperature_file', None) # Update global instrument cfg CFG.read() # Update local cfg if other instance updated cfg CFG.instruments[self.cfg_uuid] = self.cfg.copy() diff --git a/inlinino/instruments/hyperbb.py b/inlinino/instruments/hyperbb.py index c1038ea..bd3a283 100644 --- a/inlinino/instruments/hyperbb.py +++ b/inlinino/instruments/hyperbb.py @@ -6,7 +6,7 @@ import numpy as np from scipy.io import loadmat -from scipy.interpolate import interp2d, splrep, splev # , pchip_interpolate +from scipy.interpolate import RegularGridInterpolator, splrep, splev # , pchip_interpolate from inlinino.instruments import Instrument @@ -49,11 +49,10 @@ def setup(self, cfg): # Set HyperBB specific attributes if 'plaque_file' not in cfg.keys(): raise ValueError('Missing calibration plaque file (*.mat)') - if 'temperature_file' not in cfg.keys(): - raise ValueError('Missing calibration temperature file (*.mat)') if 'data_format' not in cfg.keys(): cfg['data_format'] = 'advanced' - self._parser = HyperBBParser(cfg['plaque_file'], cfg['temperature_file'], cfg['data_format']) + temperature_file = cfg.get('temperature_file', None) + self._parser = HyperBBParser(cfg['plaque_file'], temperature_file, cfg['data_format']) self.signal_reconstructed = np.empty(len(self._parser.wavelength)) * np.nan # Overload cfg with received data prod_var_names = ['beta_u', 'bb'] @@ -152,9 +151,10 @@ def update_active_timeseries_variables(self, name, state): LEGACY_DATA_FORMAT = 0 ADVANCED_DATA_FORMAT = 1 LIGHT_DATA_FORMAT = 2 +STANDARD_DATA_FORMAT = 3 # New data format (firmware v2.x) class HyperBBParser(): - def __init__(self, plaque_cal_file, temperature_cal_file, data_format='advanced'): + def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='advanced'): # Frame Parser if data_format.lower() == 'legacy': self.data_format = LEGACY_DATA_FORMAT @@ -162,6 +162,8 @@ def __init__(self, plaque_cal_file, temperature_cal_file, data_format='advanced' self.data_format = ADVANCED_DATA_FORMAT elif data_format.lower() == 'light': self.data_format = LIGHT_DATA_FORMAT + elif data_format.lower() == 'standard': + self.data_format = STANDARD_DATA_FORMAT else: raise ValueError('Data format not recognized.') if self.data_format == LEGACY_DATA_FORMAT: # Manual version 1.2 @@ -208,6 +210,17 @@ def __init__(self, plaque_cal_file, temperature_cal_file, data_format='advanced' self.FRAME_PRECISIONS = ['%s'] * len(self.FRAME_VARIABLES) for x in self.FRAME_VARIABLES: setattr(self, f'idx_{x}', self.FRAME_VARIABLES.index(x)) + elif self.data_format == STANDARD_DATA_FORMAT: # New standard format (firmware v2.x) + # Simplified output with net signals pre-computed by firmware + self.FRAME_VARIABLES = ['ScanIdx', 'DataIdx', 'Date', 'Time', 'StepPos', 'wl', 'LedPwr', 'PmtGain', + 'NetRef', 'NetSig1', 'NetSig2', 'NetSig3', + 'LedTemp', 'WaterTemp', 'Depth', 'SupplyVolt', 'Saturation', 'CalPlaqueDist'] + self.FRAME_TYPES = [int, int, str, str, int, int, int, int, + float, float, float, float, float, + float, float, float, int, int] + self.FRAME_PRECISIONS = ['%s'] * len(self.FRAME_VARIABLES) + for x in self.FRAME_VARIABLES: + setattr(self, f'idx_{x}', self.FRAME_VARIABLES.index(x)) else: raise ValueError('Firmware version not supported.') @@ -220,33 +233,104 @@ def __init__(self, plaque_cal_file, temperature_cal_file, data_format='advanced' self.saturation_level = 4000 self.theta = 135 # calls theta setter which sets Xp - # Load Temperature calibration file - t = loadmat(temperature_cal_file, simplify_cells=True) - self.wavelength = t['cal_temp']['wl'] - self.cal_t_coef = t['cal_temp']['coeff'] + # Load calibration files (supports old two-file format and new combined file format) + p_cal, t_cal = self._load_calibration(plaque_cal_file, temperature_cal_file) - # Load plaque calibration file - p = loadmat(plaque_cal_file, simplify_cells=True) - self.pmt_ref_gain = p['cal']['pmtRefGain'] - self.pmt_gamma = p['cal']['pmtGamma'] - self.gain12 = p['cal']['gain12'] - self.gain23 = p['cal']['gain23'] + self.wavelength = t_cal['wl'] + self.cal_t_coef = t_cal['coeff'] + self.pmt_ref_gain = p_cal['pmtRefGain'] + self.pmt_gamma = p_cal['pmtGamma'] + self.gain12 = p_cal['gain12'] + self.gain23 = p_cal['gain23'] # Check wavelength match in all calibration files - if np.any(p['cal']['darkCalWavelength'] != p['cal']['muWavelengths']) or \ - np.any(p['cal']['darkCalWavelength'] != t['cal_temp']['wl']): + if np.any(p_cal['darkCalWavelength'] != p_cal['muWavelengths']) or \ + np.any(p_cal['darkCalWavelength'] != t_cal['wl']): raise ValueError('Wavelength from calibration files don\'t match.') + # Pre-compute temperature correction grid over an extensive temperature range + _led_t_grid = np.arange(0, 50.01, 0.1) + _t_corr_grid = np.empty((len(self.wavelength), len(_led_t_grid))) + for k in range(len(self.wavelength)): + _t_corr_grid[k, :] = np.polyval(self.cal_t_coef[k, :], _led_t_grid) + self._f_t_correction = RegularGridInterpolator( + (self.wavelength.astype(float), _led_t_grid), + _t_corr_grid, method='linear', bounds_error=False, fill_value=None) + # Prepare interpolation tables for dark offsets - self.f_dark_cal_scat_1 = interp2d(p['cal']['darkCalPmtGain'], p['cal']['darkCalWavelength'], - p['cal']['darkCalScat1'], kind='linear') - self.f_dark_cal_scat_2 = interp2d(p['cal']['darkCalPmtGain'], p['cal']['darkCalWavelength'], - p['cal']['darkCalScat2'], kind='linear') - self.f_dark_cal_scat_3 = interp2d(p['cal']['darkCalPmtGain'], p['cal']['darkCalWavelength'], - p['cal']['darkCalScat3'], kind='linear') + _gain_vals = p_cal['darkCalPmtGain'].astype(float) + _wl_vals = p_cal['darkCalWavelength'].astype(float) + self.f_dark_cal_scat_1 = RegularGridInterpolator( + (_wl_vals, _gain_vals), p_cal['darkCalScat1'], + method='linear', bounds_error=False, fill_value=None) + self.f_dark_cal_scat_2 = RegularGridInterpolator( + (_wl_vals, _gain_vals), p_cal['darkCalScat2'], + method='linear', bounds_error=False, fill_value=None) + self.f_dark_cal_scat_3 = RegularGridInterpolator( + (_wl_vals, _gain_vals), p_cal['darkCalScat3'], + method='linear', bounds_error=False, fill_value=None) # mu calibration corrected for temperature - self.mu = p['cal']['muFactors'] * self.compute_temperature_coefficients(p['cal']['muWavelengths'], - p['cal']['muLedTemp']) + self.mu = p_cal['muFactors'] * self.compute_temperature_coefficients(p_cal['muWavelengths'], + p_cal['muLedTemp']) + + @staticmethod + def _load_calibration(plaque_cal_file, temperature_cal_file=None): + """Load calibration data from old two-file format or new combined single-file format. + + Old format: Two separate .mat files, one with a 'cal' struct (plaque) and one with a + 'cal_temp' struct (temperature). + + New combined format: A single .mat file containing both plaque and temperature calibration + data. The file must contain a top-level struct (e.g. 'calibration') that includes all + plaque calibration fields as well as 'wl' and 'coeff' fields for temperature calibration. + + :param plaque_cal_file: Path to plaque calibration .mat file (old format) or combined + calibration .mat file (new format). + :param temperature_cal_file: Path to temperature calibration .mat file (old format only). + Set to None when using a new combined calibration file. + :return: Tuple (p_cal, t_cal) where p_cal is a dict of plaque calibration parameters and + t_cal is a dict with keys 'wl' and 'coeff' for temperature calibration. + :raises ValueError: If required calibration fields are missing. + """ + mat = loadmat(plaque_cal_file, simplify_cells=True) + # Remove MATLAB metadata keys + data_keys = [k for k in mat.keys() if not k.startswith('__')] + + if 'cal' in mat: + # Old two-file format: plaque file has 'cal' struct + p_cal = mat['cal'] + if temperature_cal_file is None: + raise ValueError( + 'Missing temperature calibration file (*.mat). ' + 'The old calibration format requires two separate files.') + t_mat = loadmat(temperature_cal_file, simplify_cells=True) + if 'cal_temp' not in t_mat: + raise ValueError( + f"Temperature calibration file '{temperature_cal_file}' does not contain " + "'cal_temp' struct. Check that the correct file is specified.") + t_cal = t_mat['cal_temp'] + elif len(data_keys) == 1: + # New combined format: single struct containing all calibration data + combined = mat[data_keys[0]] + _required_plaque = {'pmtRefGain', 'pmtGamma', 'gain12', 'gain23', + 'darkCalWavelength', 'darkCalPmtGain', + 'darkCalScat1', 'darkCalScat2', 'darkCalScat3', + 'muWavelengths', 'muFactors', 'muLedTemp'} + _required_temp = {'wl', 'coeff'} + _missing = (_required_plaque | _required_temp) - set(combined.keys()) + if _missing: + raise ValueError( + f"Combined calibration file '{plaque_cal_file}' is missing required " + f"fields: {', '.join(sorted(_missing))}. " + "Ensure the file is a valid HyperBB calibration file.") + p_cal = combined + t_cal = {'wl': combined['wl'], 'coeff': combined['coeff']} + else: + raise ValueError( + f"Calibration file '{plaque_cal_file}' format not recognized. " + "Expected either 'cal' struct (old format) or a single combined struct " + f"(new format). Found keys: {data_keys}.") + return p_cal, t_cal @property def theta(self) -> float: @@ -261,14 +345,9 @@ def theta(self, value) -> None: self.Xp = float(splev(self.theta, splrep(theta_ref, Xp_ref))) def compute_temperature_coefficients(self, wl, t): - # Generate temperature correction grid - led_t = np.arange(np.min(t), np.max(t) + 0.1001, 0.1) # TODO optimize by creating grid for extensive range of value once - t_correction = np.empty((len(self.wavelength), len(led_t))) - for k in range(len(self.wavelength)): - t_correction[k, :] = np.polyval(self.cal_t_coef[k, :], led_t) - # mu temperature correction - t_correction = interp2d(led_t, self.wavelength, t_correction, kind='linear')(t, wl) - return np.diag(t_correction) if t_correction.ndim > 1 else t_correction + wl_arr = np.atleast_1d(np.asarray(wl, dtype=float)) + t_arr = np.atleast_1d(np.asarray(t, dtype=float)) + return self._f_t_correction(np.column_stack([wl_arr, t_arr])) def parse(self, raw): tmp = raw.decode().split() @@ -320,6 +399,20 @@ def calibrate(self, raw): gain[np.isnan(raw[:, self.idx_SigOn3])] = 2 gain[np.isnan(raw[:, self.idx_SigOn2])] = 1 gain[np.isnan(raw[:, self.idx_SigOn1])] = 0 # All signals saturated + elif self.data_format == STANDARD_DATA_FORMAT: # New standard format: net signals pre-computed + net_ref_zero_flag = np.any(raw[:, self.idx_NetRef] == 0) + # Saturation field: 0=none, 1=ch1 saturated, 2=ch1+ch2, 3=all channels saturated + raw[raw[:, self.idx_Saturation] >= 1, self.idx_NetSig1] = np.nan + raw[raw[:, self.idx_Saturation] >= 2, self.idx_NetSig2] = np.nan + raw[raw[:, self.idx_Saturation] >= 3, self.idx_NetSig3] = np.nan + scat1 = raw[:, self.idx_NetSig1] / raw[:, self.idx_NetRef] + scat2 = raw[:, self.idx_NetSig2] / raw[:, self.idx_NetRef] + scat3 = raw[:, self.idx_NetSig3] / raw[:, self.idx_NetRef] + # Keep Gain setting + gain = np.ones((len(raw), 1)) * 3 + gain[raw[:, self.idx_Saturation] == 3] = 2 + gain[raw[:, self.idx_Saturation] == 2] = 1 + gain[raw[:, self.idx_Saturation] == 1] = 0 # All signals saturated else: # Light Format net_ref_zero_flag = np.any(raw[:, self.idx_NetRef] == 0) raw[raw[:, self.idx_ChSaturated] == 1, self.idx_NetSig1] = np.nan @@ -334,9 +427,10 @@ def calibrate(self, raw): gain[raw[:, self.idx_ChSaturated] == 2] = 1 gain[raw[:, self.idx_ChSaturated] == 1] = 0 # All signals saturated # Subtract dark offset - scat1_dark_removed = scat1 - self.f_dark_cal_scat_1(raw[:, self.idx_PmtGain], wl) - scat2_dark_removed = scat2 - self.f_dark_cal_scat_2(raw[:, self.idx_PmtGain], wl) - scat3_dark_removed = scat3 - self.f_dark_cal_scat_3(raw[:, self.idx_PmtGain], wl) + _pts = np.column_stack([wl, raw[:, self.idx_PmtGain]]) + scat1_dark_removed = scat1 - self.f_dark_cal_scat_1(_pts) + scat2_dark_removed = scat2 - self.f_dark_cal_scat_2(_pts) + scat3_dark_removed = scat3 - self.f_dark_cal_scat_3(_pts) # Apply PMT and front end gain factors g_pmt = (raw[:, self.idx_PmtGain] / self.pmt_ref_gain) ** self.pmt_gamma scat1_gain_corrected = scat1_dark_removed * self.gain12 * self.gain23 * g_pmt diff --git a/inlinino/resources/setup_hyperbb.ui b/inlinino/resources/setup_hyperbb.ui index 1dae3ad..e12b558 100644 --- a/inlinino/resources/setup_hyperbb.ui +++ b/inlinino/resources/setup_hyperbb.ui @@ -278,6 +278,11 @@ advanced + + + standard + + light diff --git a/test/test_hyperbb.py b/test/test_hyperbb.py new file mode 100644 index 0000000..43a3587 --- /dev/null +++ b/test/test_hyperbb.py @@ -0,0 +1,267 @@ +""" +Tests for HyperBB instrument parser. + +Tests the following: +- Old two-file calibration format (plaque + temperature) +- New combined single-file calibration format +- All data output formats (legacy, advanced, light, standard) +- Calibration math (calibrate function) +- Frame parsing +""" +import os +import sys + +import numpy as np +import pytest + +# Allow running from repo root or test/ directory +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from inlinino.instruments.hyperbb import ( + HyperBBParser, + LEGACY_DATA_FORMAT, + ADVANCED_DATA_FORMAT, + LIGHT_DATA_FORMAT, + STANDARD_DATA_FORMAT, +) + +CFG_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), + 'inlinino', 'cfg') +PLAQUE_CAL = os.path.join(CFG_DIR, 'HBB8005_CalPlaque_20210315.mat') +TEMP_CAL = os.path.join(CFG_DIR, 'HBB8005_CalTemp_20210315.mat') +COMBINED_CAL = os.path.join(CFG_DIR, 'HBB8005_Cal_new_format_example.mat') + + +def _skip_if_no_cal_files(): + """Skip test if calibration files are not available.""" + return pytest.mark.skipif( + not os.path.exists(PLAQUE_CAL) or not os.path.exists(TEMP_CAL), + reason='Calibration files not available' + ) + + +class TestHyperBBParserCreation: + """Test HyperBBParser can be created with various configurations.""" + + @_skip_if_no_cal_files() + def test_old_format_advanced(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + assert parser.data_format == ADVANCED_DATA_FORMAT + assert len(parser.FRAME_VARIABLES) == 31 + assert len(parser.wavelength) > 0 + + @_skip_if_no_cal_files() + def test_old_format_legacy(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'legacy') + assert parser.data_format == LEGACY_DATA_FORMAT + assert len(parser.FRAME_VARIABLES) == 30 + + @_skip_if_no_cal_files() + def test_old_format_light(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light') + assert parser.data_format == LIGHT_DATA_FORMAT + assert len(parser.FRAME_VARIABLES) == 14 + + @_skip_if_no_cal_files() + def test_old_format_standard(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'standard') + assert parser.data_format == STANDARD_DATA_FORMAT + assert len(parser.FRAME_VARIABLES) == 18 + + @_skip_if_no_cal_files() + def test_new_combined_format(self): + """New combined calibration file (single file with both plaque and temperature data).""" + if not os.path.exists(COMBINED_CAL): + pytest.skip('Combined calibration file not available') + parser = HyperBBParser(COMBINED_CAL, None, 'advanced') + assert parser.data_format == ADVANCED_DATA_FORMAT + assert len(parser.wavelength) > 0 + + @_skip_if_no_cal_files() + def test_new_combined_format_matches_old(self): + """Combined format should produce same calibration as old two-file format.""" + if not os.path.exists(COMBINED_CAL): + pytest.skip('Combined calibration file not available') + parser_old = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + parser_new = HyperBBParser(COMBINED_CAL, None, 'advanced') + np.testing.assert_array_almost_equal(parser_old.mu, parser_new.mu) + np.testing.assert_array_equal(parser_old.wavelength, parser_new.wavelength) + + def test_invalid_format_raises(self): + with pytest.raises(ValueError, match='Data format not recognized'): + HyperBBParser.__new__(HyperBBParser) + parser = object.__new__(HyperBBParser) + # Call init with dummy files (expect format error before file loading) + try: + HyperBBParser('dummy.mat', 'dummy.mat', 'invalid_format') + except ValueError as e: + if 'Data format not recognized' in str(e): + raise + raise AssertionError(f'Wrong error: {e}') + + @_skip_if_no_cal_files() + def test_old_format_without_temp_file_raises(self): + """Old two-file format requires temperature file.""" + with pytest.raises(ValueError, match='Missing temperature calibration file'): + HyperBBParser(PLAQUE_CAL, None, 'advanced') + + +class TestHyperBBParserParse: + """Test frame parsing for all data formats.""" + + @_skip_if_no_cal_files() + def test_parse_advanced_frame(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + # 31 space-separated fields + packet = (b'1 1 2021-01-01 12:00:00 100 450 255 2000 100 ' + b'500 10 1000 20 400 10 900 20 ' + b'600 15 700 20 500 15 600 20 ' + b'36.6 20.0 10.0 12.0 0 1') + data = parser.parse(packet) + assert len(data) == len(parser.FRAME_VARIABLES) + assert data[parser.idx_wl] == float('nan') or data[parser.idx_wl] == 450 + + @_skip_if_no_cal_files() + def test_parse_light_frame(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light') + # 14 space-separated fields + packet = b'1 2021-01-01 12:00:00 450 2000 1000.0 100.0 90.0 80.0 36.6 20.0 10.0 12.0 0' + data = parser.parse(packet) + assert len(data) == len(parser.FRAME_VARIABLES) + + @_skip_if_no_cal_files() + def test_parse_standard_frame(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'standard') + # 18 space-separated fields + packet = b'1 1 2021-01-01 12:00:00 100 450 255 2000 1000.0 100.0 90.0 80.0 36.6 20.0 10.0 12.0 0 1' + data = parser.parse(packet) + assert len(data) == len(parser.FRAME_VARIABLES) + + @_skip_if_no_cal_files() + def test_parse_wrong_field_count_returns_empty(self): + """Short packet should return empty list.""" + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + packet = b'1 2 3' + data = parser.parse(packet) + assert data == [] + + @_skip_if_no_cal_files() + def test_parse_empty_packet_returns_empty(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + data = parser.parse(b'') + assert data == [] + + +class TestHyperBBCalibrate: + """Test calibration calculations.""" + + @_skip_if_no_cal_files() + def test_calibrate_advanced_single_frame(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + n = len(parser.FRAME_VARIABLES) + raw = np.zeros((1, n)) + raw[0, parser.idx_wl] = 450.0 + raw[0, parser.idx_PmtGain] = 2000.0 + raw[0, parser.idx_NetSig1] = 100.0 + raw[0, parser.idx_SigOn1] = 500.0 + raw[0, parser.idx_SigOn2] = 600.0 + raw[0, parser.idx_SigOn3] = 700.0 + raw[0, parser.idx_SigOff1] = 400.0 + raw[0, parser.idx_SigOff2] = 500.0 + raw[0, parser.idx_SigOff3] = 600.0 + raw[0, parser.idx_RefOn] = 1000.0 + raw[0, parser.idx_RefOff] = 900.0 + raw[0, parser.idx_LedTemp] = 36.6 + beta_u, bb, wl, gain, net_ref_zero_flag = parser.calibrate(raw.copy()) + assert beta_u.shape == (1,) + assert bb.shape == (1,) + assert not np.isnan(beta_u[0]) + assert not np.isnan(bb[0]) + assert bb[0] > 0 + + @_skip_if_no_cal_files() + def test_calibrate_standard_single_frame(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'standard') + n = len(parser.FRAME_VARIABLES) + raw = np.zeros((1, n)) + raw[0, parser.idx_wl] = 450.0 + raw[0, parser.idx_PmtGain] = 2000.0 + raw[0, parser.idx_NetRef] = 1000.0 + raw[0, parser.idx_NetSig1] = 100.0 + raw[0, parser.idx_NetSig2] = 90.0 + raw[0, parser.idx_NetSig3] = 80.0 + raw[0, parser.idx_LedTemp] = 36.6 + raw[0, parser.idx_Saturation] = 0 + beta_u, bb, wl, gain, net_ref_zero_flag = parser.calibrate(raw.copy()) + assert beta_u.shape == (1,) + assert bb.shape == (1,) + assert not np.isnan(bb[0]) + assert bb[0] > 0 + + @_skip_if_no_cal_files() + def test_calibrate_light_single_frame(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light') + n = len(parser.FRAME_VARIABLES) + raw = np.zeros((1, n)) + raw[0, parser.idx_wl] = 450.0 + raw[0, parser.idx_PmtGain] = 2000.0 + raw[0, parser.idx_NetRef] = 1000.0 + raw[0, parser.idx_NetSig1] = 100.0 + raw[0, parser.idx_NetSig2] = 90.0 + raw[0, parser.idx_NetSig3] = 80.0 + raw[0, parser.idx_LedTemp] = 36.6 + raw[0, parser.idx_ChSaturated] = 0 + beta_u, bb, wl, gain, net_ref_zero_flag = parser.calibrate(raw.copy()) + assert not np.isnan(bb[0]) + assert bb[0] > 0 + + @_skip_if_no_cal_files() + def test_calibrate_new_format_matches_old(self): + """Combined calibration format should produce same calibration results as old format.""" + if not os.path.exists(COMBINED_CAL): + pytest.skip('Combined calibration file not available') + parser_old = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') + parser_new = HyperBBParser(COMBINED_CAL, None, 'advanced') + n = len(parser_old.FRAME_VARIABLES) + raw = np.zeros((1, n)) + raw[0, parser_old.idx_wl] = 450.0 + raw[0, parser_old.idx_PmtGain] = 2000.0 + raw[0, parser_old.idx_NetSig1] = 100.0 + raw[0, parser_old.idx_SigOn1] = 500.0 + raw[0, parser_old.idx_SigOn2] = 600.0 + raw[0, parser_old.idx_SigOn3] = 700.0 + raw[0, parser_old.idx_SigOff1] = 400.0 + raw[0, parser_old.idx_SigOff2] = 500.0 + raw[0, parser_old.idx_SigOff3] = 600.0 + raw[0, parser_old.idx_RefOn] = 1000.0 + raw[0, parser_old.idx_RefOff] = 900.0 + raw[0, parser_old.idx_LedTemp] = 36.6 + beta_u_old, bb_old, _, _, _ = parser_old.calibrate(raw.copy()) + beta_u_new, bb_new, _, _, _ = parser_new.calibrate(raw.copy()) + np.testing.assert_array_almost_equal(bb_old, bb_new) + + +if __name__ == '__main__': + # Run tests without pytest for quick validation + import traceback + tests = [ + TestHyperBBParserCreation(), + TestHyperBBParserParse(), + TestHyperBBCalibrate(), + ] + passed = failed = skipped = 0 + for test_obj in tests: + for method_name in [m for m in dir(test_obj) if m.startswith('test_')]: + method = getattr(test_obj, method_name) + try: + method() + print(f'PASS: {type(test_obj).__name__}.{method_name}') + passed += 1 + except pytest.skip.Exception as e: + print(f'SKIP: {type(test_obj).__name__}.{method_name}: {e}') + skipped += 1 + except Exception as e: + print(f'FAIL: {type(test_obj).__name__}.{method_name}: {e}') + traceback.print_exc() + failed += 1 + print(f'\nTotal: {passed} passed, {failed} failed, {skipped} skipped') From d6bde0d1c67a301da03a33ab4ccbee566c176389 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 13 Mar 2026 21:11:09 +0000 Subject: [PATCH 3/4] Address code review comments: simplify test, fix skip exception handling, add doc comment Co-authored-by: doizuc <6456289+doizuc@users.noreply.github.com> --- inlinino/instruments/hyperbb.py | 5 ++++- test/test_hyperbb.py | 23 ++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/inlinino/instruments/hyperbb.py b/inlinino/instruments/hyperbb.py index bd3a283..2ec951f 100644 --- a/inlinino/instruments/hyperbb.py +++ b/inlinino/instruments/hyperbb.py @@ -248,7 +248,10 @@ def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='adva np.any(p_cal['darkCalWavelength'] != t_cal['wl']): raise ValueError('Wavelength from calibration files don\'t match.') - # Pre-compute temperature correction grid over an extensive temperature range + # Pre-compute temperature correction grid over an extensive temperature range. + # The grid spans 0–50°C, covering the full operational range of the LED. + # RegularGridInterpolator uses fill_value=None which extrapolates beyond the grid + # edges; log a warning if data temperatures fall outside this range. _led_t_grid = np.arange(0, 50.01, 0.1) _t_corr_grid = np.empty((len(self.wavelength), len(_led_t_grid))) for k in range(len(self.wavelength)): diff --git a/test/test_hyperbb.py b/test/test_hyperbb.py index 43a3587..31ae6f6 100644 --- a/test/test_hyperbb.py +++ b/test/test_hyperbb.py @@ -89,15 +89,7 @@ def test_new_combined_format_matches_old(self): def test_invalid_format_raises(self): with pytest.raises(ValueError, match='Data format not recognized'): - HyperBBParser.__new__(HyperBBParser) - parser = object.__new__(HyperBBParser) - # Call init with dummy files (expect format error before file loading) - try: - HyperBBParser('dummy.mat', 'dummy.mat', 'invalid_format') - except ValueError as e: - if 'Data format not recognized' in str(e): - raise - raise AssertionError(f'Wrong error: {e}') + HyperBBParser('dummy.mat', 'dummy.mat', 'invalid_format') @_skip_if_no_cal_files() def test_old_format_without_temp_file_raises(self): @@ -257,11 +249,12 @@ def test_calibrate_new_format_matches_old(self): method() print(f'PASS: {type(test_obj).__name__}.{method_name}') passed += 1 - except pytest.skip.Exception as e: - print(f'SKIP: {type(test_obj).__name__}.{method_name}: {e}') - skipped += 1 except Exception as e: - print(f'FAIL: {type(test_obj).__name__}.{method_name}: {e}') - traceback.print_exc() - failed += 1 + if 'Skipped' in type(e).__name__ or 'skip' in type(e).__name__.lower(): + print(f'SKIP: {type(test_obj).__name__}.{method_name}: {e}') + skipped += 1 + else: + print(f'FAIL: {type(test_obj).__name__}.{method_name}: {e}') + traceback.print_exc() + failed += 1 print(f'\nTotal: {passed} passed, {failed} failed, {skipped} skipped') From 82f42d2ced214d36742e43de788b150db29e7598 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 00:29:37 +0000 Subject: [PATCH 4/4] Add binary .hbb_cal/.hbb_tcal file support, remove standard format, add explicit cal_format UI Co-authored-by: doizuc <6456289+doizuc@users.noreply.github.com> --- inlinino/gui.py | 11 +- inlinino/instruments/hyperbb.py | 205 +++++++++------ inlinino/resources/setup_hyperbb.ui | 61 ++++- test/test_hyperbb.py | 391 +++++++++++++++++++--------- 4 files changed, 455 insertions(+), 213 deletions(-) diff --git a/inlinino/gui.py b/inlinino/gui.py index e662f5b..64e9bc2 100644 --- a/inlinino/gui.py +++ b/inlinino/gui.py @@ -758,12 +758,14 @@ def act_browse_zsc_file(self): def act_browse_plaque_file(self): file_name, selected_filter = QtGui.QFileDialog.getOpenFileName( - caption='Choose plaque calibration file', filter='Plaque File (*.mat)') + caption='Choose plaque calibration file', + filter='Calibration File (*.mat *.hbb_cal);;Legacy MAT File (*.mat);;Current Binary (*.hbb_cal)') self.le_plaque_file.setText(file_name) def act_browse_temperature_file(self): file_name, selected_filter = QtGui.QFileDialog.getOpenFileName( - caption='Choose temperature calibration file', filter='Temperature File (*.mat)') + caption='Choose temperature calibration file', + filter='Temperature Calibration File (*.mat *.hbb_tcal);;Legacy MAT File (*.mat);;Current Binary (*.hbb_tcal)') self.le_temperature_file.setText(file_name) def act_browse_px_reg_prt(self): @@ -1080,11 +1082,6 @@ def act_save(self): elif self.cfg['module'] == 'hyperbb': self.cfg['manufacturer'] = 'Sequoia' self.cfg['model'] = 'HyperBB' - # Temperature file is optional when using a new combined calibration file - if 'Temperature File' in empty_fields: - empty_fields.remove('Temperature File') - if not self.cfg.get('temperature_file'): - self.cfg.pop('temperature_file', None) # Update global instrument cfg CFG.read() # Update local cfg if other instance updated cfg CFG.instruments[self.cfg_uuid] = self.cfg.copy() diff --git a/inlinino/instruments/hyperbb.py b/inlinino/instruments/hyperbb.py index 2ec951f..b9dc8f1 100644 --- a/inlinino/instruments/hyperbb.py +++ b/inlinino/instruments/hyperbb.py @@ -51,8 +51,11 @@ def setup(self, cfg): raise ValueError('Missing calibration plaque file (*.mat)') if 'data_format' not in cfg.keys(): cfg['data_format'] = 'advanced' + if 'cal_format' not in cfg.keys(): + cfg['cal_format'] = 'legacy' temperature_file = cfg.get('temperature_file', None) - self._parser = HyperBBParser(cfg['plaque_file'], temperature_file, cfg['data_format']) + self._parser = HyperBBParser(cfg['plaque_file'], temperature_file, cfg['data_format'], + cfg['cal_format']) self.signal_reconstructed = np.empty(len(self._parser.wavelength)) * np.nan # Overload cfg with received data prod_var_names = ['beta_u', 'bb'] @@ -151,10 +154,9 @@ def update_active_timeseries_variables(self, name, state): LEGACY_DATA_FORMAT = 0 ADVANCED_DATA_FORMAT = 1 LIGHT_DATA_FORMAT = 2 -STANDARD_DATA_FORMAT = 3 # New data format (firmware v2.x) class HyperBBParser(): - def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='advanced'): + def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='advanced', cal_format='legacy'): # Frame Parser if data_format.lower() == 'legacy': self.data_format = LEGACY_DATA_FORMAT @@ -162,8 +164,6 @@ def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='adva self.data_format = ADVANCED_DATA_FORMAT elif data_format.lower() == 'light': self.data_format = LIGHT_DATA_FORMAT - elif data_format.lower() == 'standard': - self.data_format = STANDARD_DATA_FORMAT else: raise ValueError('Data format not recognized.') if self.data_format == LEGACY_DATA_FORMAT: # Manual version 1.2 @@ -210,17 +210,6 @@ def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='adva self.FRAME_PRECISIONS = ['%s'] * len(self.FRAME_VARIABLES) for x in self.FRAME_VARIABLES: setattr(self, f'idx_{x}', self.FRAME_VARIABLES.index(x)) - elif self.data_format == STANDARD_DATA_FORMAT: # New standard format (firmware v2.x) - # Simplified output with net signals pre-computed by firmware - self.FRAME_VARIABLES = ['ScanIdx', 'DataIdx', 'Date', 'Time', 'StepPos', 'wl', 'LedPwr', 'PmtGain', - 'NetRef', 'NetSig1', 'NetSig2', 'NetSig3', - 'LedTemp', 'WaterTemp', 'Depth', 'SupplyVolt', 'Saturation', 'CalPlaqueDist'] - self.FRAME_TYPES = [int, int, str, str, int, int, int, int, - float, float, float, float, float, - float, float, float, int, int] - self.FRAME_PRECISIONS = ['%s'] * len(self.FRAME_VARIABLES) - for x in self.FRAME_VARIABLES: - setattr(self, f'idx_{x}', self.FRAME_VARIABLES.index(x)) else: raise ValueError('Firmware version not supported.') @@ -233,8 +222,8 @@ def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='adva self.saturation_level = 4000 self.theta = 135 # calls theta setter which sets Xp - # Load calibration files (supports old two-file format and new combined file format) - p_cal, t_cal = self._load_calibration(plaque_cal_file, temperature_cal_file) + # Load calibration files (supports legacy two-file format and current single-file format) + p_cal, t_cal = self._load_calibration(plaque_cal_file, temperature_cal_file, cal_format) self.wavelength = t_cal['wl'] self.cal_t_coef = t_cal['coeff'] @@ -277,64 +266,144 @@ def __init__(self, plaque_cal_file, temperature_cal_file=None, data_format='adva p_cal['muLedTemp']) @staticmethod - def _load_calibration(plaque_cal_file, temperature_cal_file=None): - """Load calibration data from old two-file format or new combined single-file format. - - Old format: Two separate .mat files, one with a 'cal' struct (plaque) and one with a - 'cal_temp' struct (temperature). - - New combined format: A single .mat file containing both plaque and temperature calibration - data. The file must contain a top-level struct (e.g. 'calibration') that includes all - plaque calibration fields as well as 'wl' and 'coeff' fields for temperature calibration. - - :param plaque_cal_file: Path to plaque calibration .mat file (old format) or combined - calibration .mat file (new format). - :param temperature_cal_file: Path to temperature calibration .mat file (old format only). - Set to None when using a new combined calibration file. - :return: Tuple (p_cal, t_cal) where p_cal is a dict of plaque calibration parameters and - t_cal is a dict with keys 'wl' and 'coeff' for temperature calibration. - :raises ValueError: If required calibration fields are missing. + def _load_calibration(plaque_cal_file, temperature_cal_file=None, cal_format='legacy'): + """Load calibration data from legacy (.mat) or current binary format. + + Legacy format: Two separate .mat files — a plaque calibration file (containing a 'cal' + struct) and a temperature calibration file (containing a 'cal_temp' struct). + + Current format: Binary plaque calibration file (.hbb_cal) and binary temperature + calibration file (.hbb_tcal) as produced by Hbb_ConvertCalibrations.m (Sequoia, 2024). + Both files are required for live data temperature correction. + + :param plaque_cal_file: Path to plaque .mat file (legacy) or .hbb_cal binary file (current). + :param temperature_cal_file: Path to temperature .mat file (legacy) or .hbb_tcal binary + file (current). Required for both calibration formats. + :param cal_format: 'legacy' for MATLAB .mat file pair, 'current' for binary .hbb_cal/.hbb_tcal. + :return: Tuple (p_cal, t_cal) where p_cal and t_cal are dicts of calibration parameters. + :raises ValueError: If required files are missing or the file format is unexpected. """ - mat = loadmat(plaque_cal_file, simplify_cells=True) - # Remove MATLAB metadata keys - data_keys = [k for k in mat.keys() if not k.startswith('__')] - - if 'cal' in mat: - # Old two-file format: plaque file has 'cal' struct - p_cal = mat['cal'] + if cal_format == 'legacy': + p_mat = loadmat(plaque_cal_file, simplify_cells=True) + if 'cal' not in p_mat: + raise ValueError( + f"Plaque calibration file '{plaque_cal_file}' does not contain 'cal' struct. " + "Ensure the correct legacy format plaque file is specified.") + p_cal = p_mat['cal'] if temperature_cal_file is None: raise ValueError( 'Missing temperature calibration file (*.mat). ' - 'The old calibration format requires two separate files.') + 'The legacy calibration format requires two separate files.') t_mat = loadmat(temperature_cal_file, simplify_cells=True) if 'cal_temp' not in t_mat: raise ValueError( f"Temperature calibration file '{temperature_cal_file}' does not contain " "'cal_temp' struct. Check that the correct file is specified.") t_cal = t_mat['cal_temp'] - elif len(data_keys) == 1: - # New combined format: single struct containing all calibration data - combined = mat[data_keys[0]] - _required_plaque = {'pmtRefGain', 'pmtGamma', 'gain12', 'gain23', - 'darkCalWavelength', 'darkCalPmtGain', - 'darkCalScat1', 'darkCalScat2', 'darkCalScat3', - 'muWavelengths', 'muFactors', 'muLedTemp'} - _required_temp = {'wl', 'coeff'} - _missing = (_required_plaque | _required_temp) - set(combined.keys()) - if _missing: + elif cal_format == 'current': + if temperature_cal_file is None: raise ValueError( - f"Combined calibration file '{plaque_cal_file}' is missing required " - f"fields: {', '.join(sorted(_missing))}. " - "Ensure the file is a valid HyperBB calibration file.") - p_cal = combined - t_cal = {'wl': combined['wl'], 'coeff': combined['coeff']} + 'Missing temperature calibration file (*.hbb_tcal). ' + 'The current calibration format requires both a plaque (.hbb_cal) ' + 'and a temperature (.hbb_tcal) calibration file.') + p_cal = HyperBBParser._read_binary_plaque_cal(plaque_cal_file) + t_cal = HyperBBParser._read_binary_temp_cal(temperature_cal_file) else: raise ValueError( - f"Calibration file '{plaque_cal_file}' format not recognized. " - "Expected either 'cal' struct (old format) or a single combined struct " - f"(new format). Found keys: {data_keys}.") + f"Calibration format '{cal_format}' not recognized. Use 'legacy' or 'current'.") return p_cal, t_cal + @staticmethod + def _read_binary_plaque_cal(filename): + """Read a HyperBB binary plaque calibration file (.hbb_cal) as produced by + Hbb_ConvertCalibrations.m (Sequoia Scientific, 2024). + + The file may contain multiple calibration records appended sequentially; the last + (most recent) record is returned, consistent with MATLAB's ``cal = cal(end)``. + + Returns a dict with the same field names as the legacy .mat 'cal' struct so the rest + of the calibration pipeline requires no changes. + """ + records = [] + with open(filename, 'rb') as fid: + fid.seek(0, 2) + eof = fid.tell() + fid.seek(0) + while fid.tell() < eof: + fid.read(24) # ID string (24 chars, e.g. 'Sequoia Hyper-bb Cal') + fid.read(2) # calLengthBytes (uint16) — read but not needed for seeking + fid.read(2) # processingVersion (uint16 / 100) + fid.read(2) # serialNumber (uint16) + fid.read(6) # cal date (6 × uint8: year-1900, month, day, hour, min, sec) + fid.read(6) # tempCalDate + pmt_ref_gain = int(np.frombuffer(fid.read(2), dtype=' float: return self._theta @@ -402,20 +471,6 @@ def calibrate(self, raw): gain[np.isnan(raw[:, self.idx_SigOn3])] = 2 gain[np.isnan(raw[:, self.idx_SigOn2])] = 1 gain[np.isnan(raw[:, self.idx_SigOn1])] = 0 # All signals saturated - elif self.data_format == STANDARD_DATA_FORMAT: # New standard format: net signals pre-computed - net_ref_zero_flag = np.any(raw[:, self.idx_NetRef] == 0) - # Saturation field: 0=none, 1=ch1 saturated, 2=ch1+ch2, 3=all channels saturated - raw[raw[:, self.idx_Saturation] >= 1, self.idx_NetSig1] = np.nan - raw[raw[:, self.idx_Saturation] >= 2, self.idx_NetSig2] = np.nan - raw[raw[:, self.idx_Saturation] >= 3, self.idx_NetSig3] = np.nan - scat1 = raw[:, self.idx_NetSig1] / raw[:, self.idx_NetRef] - scat2 = raw[:, self.idx_NetSig2] / raw[:, self.idx_NetRef] - scat3 = raw[:, self.idx_NetSig3] / raw[:, self.idx_NetRef] - # Keep Gain setting - gain = np.ones((len(raw), 1)) * 3 - gain[raw[:, self.idx_Saturation] == 3] = 2 - gain[raw[:, self.idx_Saturation] == 2] = 1 - gain[raw[:, self.idx_Saturation] == 1] = 0 # All signals saturated else: # Light Format net_ref_zero_flag = np.any(raw[:, self.idx_NetRef] == 0) raw[raw[:, self.idx_ChSaturated] == 1, self.idx_NetSig1] = np.nan diff --git a/inlinino/resources/setup_hyperbb.ui b/inlinino/resources/setup_hyperbb.ui index e12b558..b5a484d 100644 --- a/inlinino/resources/setup_hyperbb.ui +++ b/inlinino/resources/setup_hyperbb.ui @@ -10,7 +10,7 @@ 0 0 515 - 366 + 395 @@ -194,6 +194,50 @@ 0 + + + Cal. Format + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + legacy + + + + legacy + + + + + current + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + Plaque File @@ -203,7 +247,7 @@ - + -1 @@ -227,7 +271,7 @@ - + Temperature File @@ -237,7 +281,7 @@ - + -1 @@ -261,7 +305,7 @@ - + Data Format @@ -271,18 +315,13 @@ - + advanced - - - standard - - light diff --git a/test/test_hyperbb.py b/test/test_hyperbb.py index 31ae6f6..a10b2ba 100644 --- a/test/test_hyperbb.py +++ b/test/test_hyperbb.py @@ -2,14 +2,18 @@ Tests for HyperBB instrument parser. Tests the following: -- Old two-file calibration format (plaque + temperature) -- New combined single-file calibration format -- All data output formats (legacy, advanced, light, standard) +- Legacy calibration format (two separate plaque + temperature .mat files) +- Current calibration format (binary .hbb_cal and .hbb_tcal files produced by + Hbb_ConvertCalibrations.m, Sequoia Scientific 2024) +- All data output formats (legacy, advanced, light) - Calibration math (calibrate function) - Frame parsing """ +import io import os +import struct import sys +import tempfile import numpy as np import pytest @@ -22,80 +26,258 @@ LEGACY_DATA_FORMAT, ADVANCED_DATA_FORMAT, LIGHT_DATA_FORMAT, - STANDARD_DATA_FORMAT, ) CFG_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'inlinino', 'cfg') PLAQUE_CAL = os.path.join(CFG_DIR, 'HBB8005_CalPlaque_20210315.mat') -TEMP_CAL = os.path.join(CFG_DIR, 'HBB8005_CalTemp_20210315.mat') -COMBINED_CAL = os.path.join(CFG_DIR, 'HBB8005_Cal_new_format_example.mat') +TEMP_CAL = os.path.join(CFG_DIR, 'HBB8005_CalTemp_20210315.mat') def _skip_if_no_cal_files(): - """Skip test if calibration files are not available.""" + """Skip test if calibration .mat files are not available.""" return pytest.mark.skipif( not os.path.exists(PLAQUE_CAL) or not os.path.exists(TEMP_CAL), reason='Calibration files not available' ) +# --------------------------------------------------------------------------- +# Helpers: write binary calibration files from existing .mat data +# --------------------------------------------------------------------------- + +def _write_hbb_cal(p_cal, path): + """Serialise a plaque calibration dict to an .hbb_cal binary file. + + Follows the format expected by Hbb_ReadBinaryCalFile.m (Sequoia, 2024): + MATLAB writes 2-D arrays column-major, so we use Fortran order when + flattening NumPy arrays before writing. + """ + dark_wl = np.asarray(p_cal['darkCalWavelength'], dtype=float) + dark_gain = np.asarray(p_cal['darkCalPmtGain'], dtype=' 0 @_skip_if_no_cal_files() - def test_old_format_legacy(self): - parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'legacy') + def test_legacy_cal_legacy_data(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'legacy', cal_format='legacy') assert parser.data_format == LEGACY_DATA_FORMAT assert len(parser.FRAME_VARIABLES) == 30 @_skip_if_no_cal_files() - def test_old_format_light(self): - parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light') + def test_legacy_cal_light_data(self): + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light', cal_format='legacy') assert parser.data_format == LIGHT_DATA_FORMAT assert len(parser.FRAME_VARIABLES) == 14 @_skip_if_no_cal_files() - def test_old_format_standard(self): - parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'standard') - assert parser.data_format == STANDARD_DATA_FORMAT - assert len(parser.FRAME_VARIABLES) == 18 - - @_skip_if_no_cal_files() - def test_new_combined_format(self): - """New combined calibration file (single file with both plaque and temperature data).""" - if not os.path.exists(COMBINED_CAL): - pytest.skip('Combined calibration file not available') - parser = HyperBBParser(COMBINED_CAL, None, 'advanced') + def test_current_binary_format(self): + """Current format: binary .hbb_cal + .hbb_tcal files.""" + from scipy.io import loadmat + p_mat = loadmat(PLAQUE_CAL, simplify_cells=True)['cal'] + t_mat = loadmat(TEMP_CAL, simplify_cells=True)['cal_temp'] + with tempfile.TemporaryDirectory() as tmp: + hbb_cal = os.path.join(tmp, 'test.hbb_cal') + hbb_tcal = os.path.join(tmp, 'test.hbb_tcal') + _write_hbb_cal(p_mat, hbb_cal) + _write_hbb_tcal(t_mat, hbb_tcal) + parser = HyperBBParser(hbb_cal, hbb_tcal, 'advanced', cal_format='current') assert parser.data_format == ADVANCED_DATA_FORMAT assert len(parser.wavelength) > 0 @_skip_if_no_cal_files() - def test_new_combined_format_matches_old(self): - """Combined format should produce same calibration as old two-file format.""" - if not os.path.exists(COMBINED_CAL): - pytest.skip('Combined calibration file not available') - parser_old = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') - parser_new = HyperBBParser(COMBINED_CAL, None, 'advanced') - np.testing.assert_array_almost_equal(parser_old.mu, parser_new.mu) - np.testing.assert_array_equal(parser_old.wavelength, parser_new.wavelength) - - def test_invalid_format_raises(self): + def test_current_cal_matches_legacy_cal(self): + """Binary current format should yield the same calibration as legacy .mat format.""" + from scipy.io import loadmat + p_mat = loadmat(PLAQUE_CAL, simplify_cells=True)['cal'] + t_mat = loadmat(TEMP_CAL, simplify_cells=True)['cal_temp'] + with tempfile.TemporaryDirectory() as tmp: + hbb_cal = os.path.join(tmp, 'test.hbb_cal') + hbb_tcal = os.path.join(tmp, 'test.hbb_tcal') + _write_hbb_cal(p_mat, hbb_cal) + _write_hbb_tcal(t_mat, hbb_tcal) + parser_legacy = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced', cal_format='legacy') + parser_current = HyperBBParser(hbb_cal, hbb_tcal, 'advanced', cal_format='current') + np.testing.assert_array_almost_equal(parser_legacy.mu, parser_current.mu, decimal=4) + np.testing.assert_array_almost_equal(parser_legacy.wavelength, parser_current.wavelength, decimal=1) + + def test_invalid_data_format_raises(self): with pytest.raises(ValueError, match='Data format not recognized'): HyperBBParser('dummy.mat', 'dummy.mat', 'invalid_format') + def test_invalid_cal_format_raises(self): + with pytest.raises(ValueError, match="Calibration format 'invalid' not recognized"): + HyperBBParser('dummy.mat', 'dummy.mat', 'advanced', cal_format='invalid') + @_skip_if_no_cal_files() - def test_old_format_without_temp_file_raises(self): - """Old two-file format requires temperature file.""" + def test_legacy_cal_without_temp_file_raises(self): + with pytest.raises(ValueError, match='Missing temperature calibration file'): + HyperBBParser(PLAQUE_CAL, None, 'advanced', cal_format='legacy') + + def test_current_cal_without_temp_file_raises(self): with pytest.raises(ValueError, match='Missing temperature calibration file'): - HyperBBParser(PLAQUE_CAL, None, 'advanced') + # dummy .hbb_cal — error should fire before the file is read + with tempfile.NamedTemporaryFile(suffix='.hbb_cal', delete=False) as tmp: + tmp_path = tmp.name + try: + HyperBBParser(tmp_path, None, 'advanced', cal_format='current') + finally: + os.unlink(tmp_path) + + +class TestHyperBBBinaryFileReaders: + """Verify that the binary file readers round-trip data correctly.""" + + @_skip_if_no_cal_files() + def test_plaque_cal_roundtrip(self): + from scipy.io import loadmat + p_mat = loadmat(PLAQUE_CAL, simplify_cells=True)['cal'] + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, 'round.hbb_cal') + _write_hbb_cal(p_mat, path) + p_bin = HyperBBParser._read_binary_plaque_cal(path) + np.testing.assert_allclose(p_bin['pmtRefGain'], p_mat['pmtRefGain']) + np.testing.assert_allclose(p_bin['pmtGamma'], p_mat['pmtGamma'], rtol=1e-5) + np.testing.assert_allclose(p_bin['gain12'], p_mat['gain12'], rtol=1e-3) + np.testing.assert_allclose(p_bin['gain23'], p_mat['gain23'], rtol=1e-3) + np.testing.assert_allclose(p_bin['muWavelengths'], p_mat['muWavelengths'], atol=0.1) + np.testing.assert_allclose(p_bin['muFactors'], p_mat['muFactors'], rtol=1e-5) + np.testing.assert_allclose(p_bin['muLedTemp'], p_mat['muLedTemp'], atol=0.01) + np.testing.assert_allclose(p_bin['darkCalWavelength'], p_mat['darkCalWavelength'], atol=0.1) + np.testing.assert_allclose(p_bin['darkCalPmtGain'], p_mat['darkCalPmtGain']) + np.testing.assert_allclose(p_bin['darkCalScat1'], p_mat['darkCalScat1'], rtol=1e-5) + np.testing.assert_allclose(p_bin['darkCalScat2'], p_mat['darkCalScat2'], rtol=1e-5) + np.testing.assert_allclose(p_bin['darkCalScat3'], p_mat['darkCalScat3'], rtol=1e-5) + + @_skip_if_no_cal_files() + def test_temp_cal_roundtrip(self): + from scipy.io import loadmat + t_mat = loadmat(TEMP_CAL, simplify_cells=True)['cal_temp'] + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, 'round.hbb_tcal') + _write_hbb_tcal(t_mat, path) + t_bin = HyperBBParser._read_binary_temp_cal(path) + np.testing.assert_allclose(t_bin['wl'], t_mat['wl'], atol=0.1) + np.testing.assert_allclose(t_bin['coeff'], t_mat['coeff'], rtol=1e-5) + + @_skip_if_no_cal_files() + def test_plaque_cal_dark_array_orientation(self): + """darkCalScat arrays must have shape (num_wl, num_gain) — wl as rows.""" + from scipy.io import loadmat + p_mat = loadmat(PLAQUE_CAL, simplify_cells=True)['cal'] + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, 'orient.hbb_cal') + _write_hbb_cal(p_mat, path) + p_bin = HyperBBParser._read_binary_plaque_cal(path) + n_wl = len(p_bin['darkCalWavelength']) + n_gain = len(p_bin['darkCalPmtGain']) + assert p_bin['darkCalScat1'].shape == (n_wl, n_gain) + assert p_bin['darkCalScat2'].shape == (n_wl, n_gain) + assert p_bin['darkCalScat3'].shape == (n_wl, n_gain) + + @_skip_if_no_cal_files() + def test_multiple_records_returns_last(self): + """When a .hbb_cal file contains multiple records, the last is returned.""" + from scipy.io import loadmat + p_mat = loadmat(PLAQUE_CAL, simplify_cells=True)['cal'] + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, 'multi.hbb_cal') + # Write the same record twice — second should be returned + _write_hbb_cal(p_mat, path) + with open(path, 'rb') as f: + first_record = f.read() + # Modify pmtRefGain in the "second" record — PMTReferenceGain is at byte offset 42 + modified = bytearray(first_record) + struct.pack_into(' 0 - - @_skip_if_no_cal_files() - def test_calibrate_standard_single_frame(self): - parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'standard') - n = len(parser.FRAME_VARIABLES) - raw = np.zeros((1, n)) - raw[0, parser.idx_wl] = 450.0 - raw[0, parser.idx_PmtGain] = 2000.0 - raw[0, parser.idx_NetRef] = 1000.0 - raw[0, parser.idx_NetSig1] = 100.0 - raw[0, parser.idx_NetSig2] = 90.0 - raw[0, parser.idx_NetSig3] = 80.0 + raw[0, parser.idx_RefOn] = 1000.0 + raw[0, parser.idx_RefOff] = 900.0 raw[0, parser.idx_LedTemp] = 36.6 - raw[0, parser.idx_Saturation] = 0 beta_u, bb, wl, gain, net_ref_zero_flag = parser.calibrate(raw.copy()) - assert beta_u.shape == (1,) assert bb.shape == (1,) assert not np.isnan(bb[0]) assert bb[0] > 0 @_skip_if_no_cal_files() def test_calibrate_light_single_frame(self): - parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light') + parser = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'light', cal_format='legacy') n = len(parser.FRAME_VARIABLES) raw = np.zeros((1, n)) - raw[0, parser.idx_wl] = 450.0 - raw[0, parser.idx_PmtGain] = 2000.0 - raw[0, parser.idx_NetRef] = 1000.0 - raw[0, parser.idx_NetSig1] = 100.0 - raw[0, parser.idx_NetSig2] = 90.0 - raw[0, parser.idx_NetSig3] = 80.0 - raw[0, parser.idx_LedTemp] = 36.6 + raw[0, parser.idx_wl] = 450.0 + raw[0, parser.idx_PmtGain] = 2000.0 + raw[0, parser.idx_NetRef] = 1000.0 + raw[0, parser.idx_NetSig1] = 100.0 + raw[0, parser.idx_NetSig2] = 90.0 + raw[0, parser.idx_NetSig3] = 80.0 + raw[0, parser.idx_LedTemp] = 36.6 raw[0, parser.idx_ChSaturated] = 0 - beta_u, bb, wl, gain, net_ref_zero_flag = parser.calibrate(raw.copy()) + _, bb, _, _, _ = parser.calibrate(raw.copy()) assert not np.isnan(bb[0]) assert bb[0] > 0 @_skip_if_no_cal_files() - def test_calibrate_new_format_matches_old(self): - """Combined calibration format should produce same calibration results as old format.""" - if not os.path.exists(COMBINED_CAL): - pytest.skip('Combined calibration file not available') - parser_old = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced') - parser_new = HyperBBParser(COMBINED_CAL, None, 'advanced') - n = len(parser_old.FRAME_VARIABLES) + def test_calibrate_current_binary_matches_legacy(self): + """Binary current format should produce the same calibration output as legacy .mat.""" + from scipy.io import loadmat + p_mat = loadmat(PLAQUE_CAL, simplify_cells=True)['cal'] + t_mat = loadmat(TEMP_CAL, simplify_cells=True)['cal_temp'] + with tempfile.TemporaryDirectory() as tmp: + hbb_cal = os.path.join(tmp, 'test.hbb_cal') + hbb_tcal = os.path.join(tmp, 'test.hbb_tcal') + _write_hbb_cal(p_mat, hbb_cal) + _write_hbb_tcal(t_mat, hbb_tcal) + parser_legacy = HyperBBParser(PLAQUE_CAL, TEMP_CAL, 'advanced', cal_format='legacy') + parser_current = HyperBBParser(hbb_cal, hbb_tcal, 'advanced', cal_format='current') + n = len(parser_legacy.FRAME_VARIABLES) raw = np.zeros((1, n)) - raw[0, parser_old.idx_wl] = 450.0 - raw[0, parser_old.idx_PmtGain] = 2000.0 - raw[0, parser_old.idx_NetSig1] = 100.0 - raw[0, parser_old.idx_SigOn1] = 500.0 - raw[0, parser_old.idx_SigOn2] = 600.0 - raw[0, parser_old.idx_SigOn3] = 700.0 - raw[0, parser_old.idx_SigOff1] = 400.0 - raw[0, parser_old.idx_SigOff2] = 500.0 - raw[0, parser_old.idx_SigOff3] = 600.0 - raw[0, parser_old.idx_RefOn] = 1000.0 - raw[0, parser_old.idx_RefOff] = 900.0 - raw[0, parser_old.idx_LedTemp] = 36.6 - beta_u_old, bb_old, _, _, _ = parser_old.calibrate(raw.copy()) - beta_u_new, bb_new, _, _, _ = parser_new.calibrate(raw.copy()) - np.testing.assert_array_almost_equal(bb_old, bb_new) + raw[0, parser_legacy.idx_wl] = 450.0 + raw[0, parser_legacy.idx_PmtGain] = 2000.0 + raw[0, parser_legacy.idx_NetSig1] = 100.0 + raw[0, parser_legacy.idx_SigOn1] = 500.0 + raw[0, parser_legacy.idx_SigOn2] = 600.0 + raw[0, parser_legacy.idx_SigOn3] = 700.0 + raw[0, parser_legacy.idx_SigOff1] = 400.0 + raw[0, parser_legacy.idx_SigOff2] = 500.0 + raw[0, parser_legacy.idx_SigOff3] = 600.0 + raw[0, parser_legacy.idx_RefOn] = 1000.0 + raw[0, parser_legacy.idx_RefOff] = 900.0 + raw[0, parser_legacy.idx_LedTemp] = 36.6 + _, bb_legacy, _, _, _ = parser_legacy.calibrate(raw.copy()) + _, bb_current, _, _, _ = parser_current.calibrate(raw.copy()) + np.testing.assert_array_almost_equal(bb_legacy, bb_current, decimal=4) if __name__ == '__main__': - # Run tests without pytest for quick validation import traceback - tests = [ - TestHyperBBParserCreation(), - TestHyperBBParserParse(), - TestHyperBBCalibrate(), - ] + tests = [TestHyperBBParserCreation(), TestHyperBBBinaryFileReaders(), + TestHyperBBParserParse(), TestHyperBBCalibrate()] passed = failed = skipped = 0 for test_obj in tests: for method_name in [m for m in dir(test_obj) if m.startswith('test_')]: