Skip to content

Commit 60a3bfa

Browse files
committed
Minor emmendments
- Instead of showing first JSON parse error, print all of them to stderr - Realized that static functions in the NeuropixelsV1Helper.cs should really accept a path instead of a StreamReader as their first arguement because StreamReader is to general because cal data is always provided in a file and this function call expects to start from the beginning of the stream. This will require a change in the public API. I will open a new issue about this.
1 parent c6c64e1 commit 60a3bfa

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

OpenEphys.Onix1.Design/DesignHelper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ static class DesignHelper
3333

3434
if (errors.Count > 0)
3535
{
36-
MessageBox.Show($"Warning: There were {errors.Count} errors found while deserializing the JSON string.\n" +
37-
$"The first error was '{errors.First()}'.", "Deserializer Error");
36+
MessageBox.Show($"There were errors encountered while parsing a JSON string. Check the console " +
37+
$"for an error log.", "JSON Parse Error");
38+
39+
foreach (var e in errors)
40+
{
41+
Console.Error.WriteLine(e);
42+
}
43+
3844
return default;
3945
}
4046

OpenEphys.Onix1/NeuropixelsV1Helper.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,24 @@ public static class NeuropixelsV1Helper
2020
/// <exception cref="InvalidOperationException"></exception>
2121
public static NeuropixelsV1eAdcCalibration ParseAdcCalibrationFile(StreamReader file)
2222
{
23+
// TODO: "file" input argument should either be a FileStream or a path string. StreamReader is to
24+
// general because cal data is always provided in a file and this function call expects to start
25+
// from the beginning of the stream. This will require a change in the public API.
26+
2327
if (file == null || file.EndOfStream)
2428
{
2529
throw new ArgumentException("Incoming stream reader is not pointing to a valid ADC calibration file.");
2630
}
2731

28-
var adcSerialNumber = ulong.Parse(file.ReadLine());
32+
string path = (file.BaseStream as FileStream)?.Name;
33+
34+
if (!ulong.TryParse(file.ReadLine(), out ulong adcSerialNumber))
35+
{
36+
throw new ArgumentException($"The calibration file {path} specified is " +
37+
$"incorrectly formatted.");
38+
}
2939

30-
NeuropixelsV1eAdc[] adcs = new NeuropixelsV1eAdc[NeuropixelsV1e.AdcCount];
40+
var adcs = new NeuropixelsV1eAdc[NeuropixelsV1e.AdcCount];
3141

3242
for (var i = 0; i < NeuropixelsV1e.AdcCount; i++)
3343
{
@@ -64,12 +74,22 @@ public static NeuropixelsV1eAdcCalibration ParseAdcCalibrationFile(StreamReader
6474
/// <exception cref="InvalidOperationException"></exception>
6575
public static NeuropixelsV1eGainCorrection ParseGainCalibrationFile(StreamReader file, NeuropixelsV1Gain apGain, NeuropixelsV1Gain lfpGain)
6676
{
77+
// TODO: "file" input argument should either be a FileStream or a path string. StreamReader is to
78+
// general because cal data is always provided in a file and this function call expects to start
79+
// from the beginning of the stream. This will require a change in the public API.
80+
6781
if (file == null || file.EndOfStream)
6882
{
6983
throw new ArgumentException("Incoming stream reader is not pointing to a valid gain calibration file.");
7084
}
7185

72-
var serialNumber = ulong.Parse(file.ReadLine());
86+
string path = (file.BaseStream as FileStream)?.Name;
87+
88+
if (!ulong.TryParse(file.ReadLine(), out ulong serialNumber))
89+
{
90+
throw new ArgumentException($"The calibration file {path} specified is " +
91+
$"incorrectly formatted.");
92+
}
7393

7494
var gainCorrections = file.ReadLine().Split(',').Skip(1);
7595

OpenEphys.Onix1/NeuropixelsV1eRegisterContext.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ public NeuropixelsV1eRegisterContext(DeviceContext deviceContext, uint i2cAddres
3838
$"{probeSerialNumber}");
3939
}
4040

41-
StreamReader adcFile = new(adcCalibrationFile);
42-
4341
// parse ADC calibration file
44-
var adcCalibration = NeuropixelsV1Helper.ParseAdcCalibrationFile(adcFile);
42+
var adcCalibration = NeuropixelsV1Helper.ParseAdcCalibrationFile(new StreamReader(adcCalibrationFile));
4543

4644
if (adcCalibration.SN != probeSerialNumber)
4745
{
4846
throw new ArgumentException($"The probe serial number ({probeSerialNumber}) does not " +
4947
$"match the ADC calibration file serial number: {adcCalibration.SN}.");
5048
}
5149

52-
StreamReader gainFile = new(gainCalibrationFile);
53-
50+
5451
// parse gain correction file
55-
var gainCorrection = NeuropixelsV1Helper.ParseGainCalibrationFile(gainFile, probeConfiguration.SpikeAmplifierGain, probeConfiguration.LfpAmplifierGain);
52+
var gainCorrection = NeuropixelsV1Helper.ParseGainCalibrationFile(new StreamReader(gainCalibrationFile),
53+
probeConfiguration.SpikeAmplifierGain, probeConfiguration.LfpAmplifierGain);
5654

5755
if (gainCorrection.SN != probeSerialNumber)
5856
{

OpenEphys.Onix1/NeuropixelsV2.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ static class NeuropixelsV2
3535
public const int DummyRegisterCount = 4;
3636
public const int RegistersPerShank = ElectrodePerShank + ReferencePixelCount + DummyRegisterCount;
3737

38-
3938
internal static BitArray[] GenerateShankBits(NeuropixelsV2QuadShankProbeConfiguration probe)
4039
{
4140
BitArray[] shankBits =
@@ -124,7 +123,11 @@ internal static double ReadGainCorrection(string gainCalibrationFile, ulong prob
124123
}
125124

126125
var gainFile = new StreamReader(gainCalibrationFile);
127-
var sn = ulong.Parse(gainFile.ReadLine());
126+
if(!ulong.TryParse(gainFile.ReadLine(), out ulong sn))
127+
{
128+
throw new ArgumentException($"The calibration file {gainCalibrationFile} specified for {probe} is " +
129+
$"incorrectly formatted.");
130+
}
128131

129132
if (probeSerialNumber != sn)
130133
{

0 commit comments

Comments
 (0)