Skip to content

Commit 838e0b3

Browse files
committed
Truncate Stim+EEG files to the end of stimulation data
1 parent dd0ffe6 commit 838e0b3

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Directory.Build.props

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
<Authors>Tristan Stenner</Authors>
44
<Company>IMPS UKSH Kiel</Company>
55
<Product>NEDF Reader</Product>
6-
<Version>1.1.4</Version>
6+
<Version>1.1.5</Version>
77
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
8-
<BaseIntermediateOutputPath>$(SolutionDir)\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
9-
<OutputPath>$(SolutionDir)bin\$(Configuration)</OutputPath>
10-
<PublishDir>..\pub</PublishDir>
118
<TargetFramework>netcoreapp3.1</TargetFramework>
129
<LangVersion>8</LangVersion>
1310
</PropertyGroup>

nedfbvaddin/NedfDataReader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ void IEEGRawFileReader.CreateStorageEntries(
7575
try
7676
{
7777
var props = ComponentFactory.CreateChangeProperties();
78-
using var file = new NedfFile(filename, (warning)=>MessageBox.Show(filename, warning));
79-
var dataName = "Tristans Raw Data";
78+
using var file = new NedfFile(filename, (warning)=>MessageBox.Show(warning, filename));
79+
string dataName = "Tristans Raw Data";
8080
var markers = file.GetMarkerPairs().ToList();
8181
if (markers.Count > file.NSample / 10)
8282
dataName = "Corrupt Data File";
@@ -85,7 +85,7 @@ void IEEGRawFileReader.CreateStorageEntries(
8585
{
8686
var m = ComponentFactory.CreateChangeMarker();
8787
m.Channel = -1;
88-
(var pos, var val) = pair;
88+
(uint pos, var val) = pair;
8989
m.Position = pos;
9090
m.Points = 1;
9191
m.Type = "Stimulus";

nedfbvaddin/nedfbvaddin.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>net48</TargetFrameworks>
4+
<TargetFramework>net48</TargetFramework>
5+
<OutputPath>C:\Vision\Analyzer2\</OutputPath>
56
</PropertyGroup>
67
<ItemGroup>
78
<ProjectReference Include="..\nedfreader\nedfreader.csproj" />

nedfreader/NedfFile.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public sealed class NedfFile : IDisposable
7070
public readonly NedfHeader hdr;
7171
public readonly string headerxml;
7272
private readonly FileStream infile;
73-
public readonly uint NSampleStimPerEEG;
74-
public uint NSample => hdr.EEGSettings.NumberOfRecordsOfEEG;
73+
public readonly uint NSampleStimPerEEG = 0;
74+
public uint NSample;
7575
public uint SFreq => hdr.EEGSettings.EEGSamplingRate;
7676
public uint NChan => hdr.EEGSettings.TotalNumberOfChannels;
7777
public uint NAcc => hdr.NumberOfChannelsOfAccelerometer;
@@ -87,11 +87,14 @@ public NedfFile(string dataFile, Action<string> suspiciousFileCallback = null)
8787
if (dataFile is null)
8888
throw new ArgumentNullException(nameof(dataFile));
8989
infile = new FileStream(dataFile, FileMode.Open, FileAccess.Read);
90-
headerxml = Encoding.UTF8.GetString(new BinaryReader(infile).ReadBytes(10240)).Trim('\0');
91-
92-
if (headerxml[0] != '<')
90+
const int headersize = 10240;
91+
byte[] buf = new byte[headersize];
92+
infile.Read(buf, 0, headersize);
93+
if (buf[0] != '<')
9394
throw new ArgumentException($"'{dataFile}' does not begin with an xml header");
9495

96+
headerxml = Encoding.UTF8.GetString(buf).Trim('\0');
97+
9598
// The XML 1.0 spec contains a list of valid characters for tag names:
9699
// https://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameChar
97100
// Older NIC versions allowed any character in the root tag name, so we have to sanitize the "xml" to avoid parse errors
@@ -109,6 +112,8 @@ public NedfFile(string dataFile, Action<string> suspiciousFileCallback = null)
109112
if ((hdr.AdditionalChannelStatus ?? "OFF") != "OFF")
110113
throw new ArgumentException($"Unexpected value for AdditionalChannelStatus: {hdr.AdditionalChannelStatus}");
111114

115+
NSample = hdr.EEGSettings.NumberOfRecordsOfEEG;
116+
112117
string eegunits = hdr.EEGSettings.EEGUnits ?? "nV";
113118
if (eegunits != "nV")
114119
throw new ArgumentException($"Unknown EEG unit {eegunits ?? "null"}");
@@ -120,7 +125,10 @@ public NedfFile(string dataFile, Action<string> suspiciousFileCallback = null)
120125
SuspiciousFileCallback?.Invoke("Data files recorded by NIC 2.0.8 with stimulation channels are broken, proceed at your own risk!");
121126
uint numstimrec = node.NumberOfRecordsOfStimulation;
122127
if (numstimrec < 2 * NSample)
123-
throw new Exception($"Can't handle intermittent stimulation data {numstimrec} records, expected {2 * NSample}");
128+
{
129+
SuspiciousFileCallback?.Invoke($"Intermittent stimulation data {numstimrec} <= {2 * NSample}, EEG data will be truncated.");
130+
NSample = numstimrec / 2;
131+
}
124132
uint stimsrate = node.StimulationSamplingRate;
125133
if (stimsrate != 1000)
126134
throw new Exception($"Unexpected stimulation sampling rate ({stimsrate}!=1000)");

0 commit comments

Comments
 (0)