@@ -70,8 +70,8 @@ public sealed class NedfFile : IDisposable
70
70
public readonly NedfHeader hdr ;
71
71
public readonly string headerxml ;
72
72
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 ;
75
75
public uint SFreq => hdr . EEGSettings . EEGSamplingRate ;
76
76
public uint NChan => hdr . EEGSettings . TotalNumberOfChannels ;
77
77
public uint NAcc => hdr . NumberOfChannelsOfAccelerometer ;
@@ -87,11 +87,14 @@ public NedfFile(string dataFile, Action<string> suspiciousFileCallback = null)
87
87
if ( dataFile is null )
88
88
throw new ArgumentNullException ( nameof ( dataFile ) ) ;
89
89
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 ] != '<' )
93
94
throw new ArgumentException ( $ "'{ dataFile } ' does not begin with an xml header") ;
94
95
96
+ headerxml = Encoding . UTF8 . GetString ( buf ) . Trim ( '\0 ' ) ;
97
+
95
98
// The XML 1.0 spec contains a list of valid characters for tag names:
96
99
// https://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameChar
97
100
// 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)
109
112
if ( ( hdr . AdditionalChannelStatus ?? "OFF" ) != "OFF" )
110
113
throw new ArgumentException ( $ "Unexpected value for AdditionalChannelStatus: { hdr . AdditionalChannelStatus } ") ;
111
114
115
+ NSample = hdr . EEGSettings . NumberOfRecordsOfEEG ;
116
+
112
117
string eegunits = hdr . EEGSettings . EEGUnits ?? "nV" ;
113
118
if ( eegunits != "nV" )
114
119
throw new ArgumentException ( $ "Unknown EEG unit { eegunits ?? "null" } ") ;
@@ -120,7 +125,10 @@ public NedfFile(string dataFile, Action<string> suspiciousFileCallback = null)
120
125
SuspiciousFileCallback ? . Invoke ( "Data files recorded by NIC 2.0.8 with stimulation channels are broken, proceed at your own risk!" ) ;
121
126
uint numstimrec = node . NumberOfRecordsOfStimulation ;
122
127
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
+ }
124
132
uint stimsrate = node . StimulationSamplingRate ;
125
133
if ( stimsrate != 1000 )
126
134
throw new Exception ( $ "Unexpected stimulation sampling rate ({ stimsrate } !=1000)") ;
0 commit comments