From 80564bdcbf1f3c75ecb6941206ba6e4f54cb33c8 Mon Sep 17 00:00:00 2001 From: bparks13 Date: Wed, 29 Oct 2025 18:10:43 -0400 Subject: [PATCH 1/3] Refactor gain correction handling to ensure non-null values for probes --- OpenEphys.Onix1/NeuropixelsV2eBetaData.cs | 6 +++--- OpenEphys.Onix1/NeuropixelsV2eData.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs b/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs index 846e1a5d..f96950f6 100644 --- a/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs +++ b/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs @@ -70,10 +70,10 @@ public unsafe override IObservable Generate() .Where(frame => NeuropixelsV2eBetaDataFrame.GetProbeIndex(frame) == (int)ProbeIndex); var invertPolarity = info.InvertPolarity; - var gainCorrection = ProbeIndex switch + double gainCorrection = ProbeIndex switch { - NeuropixelsV2Probe.ProbeA => (double)info.GainCorrectionA, - NeuropixelsV2Probe.ProbeB => (double)info.GainCorrectionB, + NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), + NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") }; diff --git a/OpenEphys.Onix1/NeuropixelsV2eData.cs b/OpenEphys.Onix1/NeuropixelsV2eData.cs index c01a0d15..c7a75dff 100644 --- a/OpenEphys.Onix1/NeuropixelsV2eData.cs +++ b/OpenEphys.Onix1/NeuropixelsV2eData.cs @@ -74,10 +74,10 @@ public unsafe override IObservable Generate() .Where(frame => NeuropixelsV2eDataFrame.GetProbeIndex(frame) == (int)ProbeIndex); var invertPolarity = info.InvertPolarity; - var gainCorrection = ProbeIndex switch + double gainCorrection = ProbeIndex switch { - NeuropixelsV2Probe.ProbeA => (double)info.GainCorrectionA, - NeuropixelsV2Probe.ProbeB => (double)info.GainCorrectionB, + NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), + NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") }; From 0d70da904eb40b9d4ce2408bcdc6d1a486c1c2c4 Mon Sep 17 00:00:00 2001 From: bparks13 Date: Mon, 3 Nov 2025 13:59:28 -0500 Subject: [PATCH 2/3] Indicate null state is an error, not incorrect user action --- OpenEphys.Onix1/NeuropixelsV2eBetaData.cs | 4 ++-- OpenEphys.Onix1/NeuropixelsV2eData.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs b/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs index f96950f6..0ceb863c 100644 --- a/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs +++ b/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs @@ -72,8 +72,8 @@ public unsafe override IObservable Generate() double gainCorrection = ProbeIndex switch { - NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), - NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), + NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), + NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") }; diff --git a/OpenEphys.Onix1/NeuropixelsV2eData.cs b/OpenEphys.Onix1/NeuropixelsV2eData.cs index c7a75dff..0b732db2 100644 --- a/OpenEphys.Onix1/NeuropixelsV2eData.cs +++ b/OpenEphys.Onix1/NeuropixelsV2eData.cs @@ -76,8 +76,8 @@ public unsafe override IObservable Generate() double gainCorrection = ProbeIndex switch { - NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), - NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"No gain correction value found for {ProbeIndex}. Ensure that the probe is connected and the gain correction file is selected."), + NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), + NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") }; From 6c9790a41e78dc5cc46f14b8aa02fb08a1717db9 Mon Sep 17 00:00:00 2001 From: bparks13 Date: Thu, 13 Nov 2025 17:10:48 -0500 Subject: [PATCH 3/3] Consolidate assignment and error checking --- OpenEphys.Onix1/NeuropixelsV2eBetaData.cs | 19 ++++++++----------- OpenEphys.Onix1/NeuropixelsV2eData.cs | 19 ++++++++----------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs b/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs index 0ceb863c..b347ceb4 100644 --- a/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs +++ b/OpenEphys.Onix1/NeuropixelsV2eBetaData.cs @@ -51,10 +51,10 @@ public unsafe override IObservable Generate() return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo => { var info = (NeuropixelsV2eDeviceInfo)deviceInfo; - var metadata = ProbeIndex switch + var (metadata, gainCorrection) = ProbeIndex switch { - NeuropixelsV2Probe.ProbeA => info.ProbeMetadataA, - NeuropixelsV2Probe.ProbeB => info.ProbeMetadataB, + NeuropixelsV2Probe.ProbeA => (info.ProbeMetadataA, info.GainCorrectionA), + NeuropixelsV2Probe.ProbeB => (info.ProbeMetadataB, info.GainCorrectionB), _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") }; @@ -62,6 +62,10 @@ public unsafe override IObservable Generate() { throw new InvalidOperationException($"{ProbeIndex} is not detected. Ensure that the flex connection is properly seated."); } + else if (gainCorrection == null) + { + throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."); + } var device = info.GetDeviceContext(typeof(NeuropixelsV2eBeta)); var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x)); @@ -70,13 +74,6 @@ public unsafe override IObservable Generate() .Where(frame => NeuropixelsV2eBetaDataFrame.GetProbeIndex(frame) == (int)ProbeIndex); var invertPolarity = info.InvertPolarity; - double gainCorrection = ProbeIndex switch - { - NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), - NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), - _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") - }; - return Observable.Create(observer => { var sampleIndex = 0; @@ -89,7 +86,7 @@ public unsafe override IObservable Generate() frame => { var payload = (NeuropixelsV2BetaPayload*)frame.Data.ToPointer(); - NeuropixelsV2eBetaDataFrame.CopyAmplifierBuffer(payload->SuperFrame, amplifierBuffer, frameCounter, sampleIndex, gainCorrection, invertPolarity); + NeuropixelsV2eBetaDataFrame.CopyAmplifierBuffer(payload->SuperFrame, amplifierBuffer, frameCounter, sampleIndex, gainCorrection.Value, invertPolarity); hubClockBuffer[sampleIndex] = payload->HubClock; clockBuffer[sampleIndex] = frame.Clock; if (++sampleIndex >= bufferSize) diff --git a/OpenEphys.Onix1/NeuropixelsV2eData.cs b/OpenEphys.Onix1/NeuropixelsV2eData.cs index 0b732db2..12625c9d 100644 --- a/OpenEphys.Onix1/NeuropixelsV2eData.cs +++ b/OpenEphys.Onix1/NeuropixelsV2eData.cs @@ -55,10 +55,10 @@ public unsafe override IObservable Generate() return DeviceManager.GetDevice(DeviceName).SelectMany(deviceInfo => { var info = (NeuropixelsV2eDeviceInfo)deviceInfo; - var metadata = ProbeIndex switch + var (metadata, gainCorrection) = ProbeIndex switch { - NeuropixelsV2Probe.ProbeA => info.ProbeMetadataA, - NeuropixelsV2Probe.ProbeB => info.ProbeMetadataB, + NeuropixelsV2Probe.ProbeA => (info.ProbeMetadataA, info.GainCorrectionA), + NeuropixelsV2Probe.ProbeB => (info.ProbeMetadataB, info.GainCorrectionB), _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") }; @@ -66,6 +66,10 @@ public unsafe override IObservable Generate() { throw new InvalidOperationException($"{ProbeIndex} is not detected. Ensure that the flex connection is properly seated."); } + else if (gainCorrection == null) + { + throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."); + } var device = info.GetDeviceContext(typeof(NeuropixelsV2e)); var passthrough = device.GetPassthroughDeviceContext(typeof(DS90UB9x)); @@ -74,13 +78,6 @@ public unsafe override IObservable Generate() .Where(frame => NeuropixelsV2eDataFrame.GetProbeIndex(frame) == (int)ProbeIndex); var invertPolarity = info.InvertPolarity; - double gainCorrection = ProbeIndex switch - { - NeuropixelsV2Probe.ProbeA => info.GainCorrectionA ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), - NeuropixelsV2Probe.ProbeB => info.GainCorrectionB ?? throw new NullReferenceException($"Gain correction value is null for {ProbeIndex}."), - _ => throw new InvalidEnumArgumentException($"Unexpected {nameof(ProbeIndex)} value: {ProbeIndex}") - }; - return Observable.Create(observer => { var sampleIndex = 0; @@ -92,7 +89,7 @@ public unsafe override IObservable Generate() frame => { var payload = (NeuropixelsV2Payload*)frame.Data.ToPointer(); - NeuropixelsV2eDataFrame.CopyAmplifierBuffer(payload->AmplifierData, amplifierBuffer, sampleIndex, gainCorrection, invertPolarity); + NeuropixelsV2eDataFrame.CopyAmplifierBuffer(payload->AmplifierData, amplifierBuffer, sampleIndex, gainCorrection.Value, invertPolarity); hubClockBuffer[sampleIndex] = payload->HubClock; clockBuffer[sampleIndex] = frame.Clock; if (++sampleIndex >= bufferSize)