private EEGDataListener GetTrialDataListener(out BlockingQueue <IArrayView <EEGDataEntry> > trialQueue) { var entryList = new List <EEGDataEntry>(); var queue = new BlockingQueue <IArrayView <EEGDataEntry> >(); trialQueue = queue; var invoker = new SingleThreadedInvoker(); return(new EEGDataListener(invoker, null, entries => { foreach (var entry in entries) { // got a new marker, so flush! if (entryList.Count > 0 && entry.Marker != entryList[0].Marker) { queue.Enqueue(entryList.AsIArray()); entryList = new List <EEGDataEntry>(entryList.Count); } // save stimulus entries if (entry.HasStimulusMarker()) { entryList.Add(entry); } } }, null, () => invoker.Dispose())); }
/// <summary> /// Groups entries using the sequences formed by their relative timestamps /// </summary> public static IEnumerable <IArrayView <EEGDataEntry> > SectionByStimulus(this IEnumerable <EEGDataEntry> entries) { if (entries.IsEmpty()) { yield break; } var section = new List <EEGDataEntry>() { entries.First() }; foreach (var entry in entries.Skip(1)) { if (entry.RelativeTimeStamp >= section.LastItem().RelativeTimeStamp) { section.Add(entry); } else { yield return(section.AsIArray()); section = new List <EEGDataEntry>() { entry }; } } yield return(section.AsIArray()); // return the last section }
/// <summary> /// Groups the entries by their respective time bins /// </summary> public static IArrayView <IArrayView <EEGDataEntry> > BinByTime(this IEnumerable <EEGDataEntry> entries, int binWidthMillis) { if (binWidthMillis <= 0) { throw new ArgumentOutOfRangeException("binWidthMillis"); } int baseTime = entries.First().TimeStamp; var bins = new List <IArrayView <EEGDataEntry> >(); var bin = new List <EEGDataEntry>(); foreach (var entry in entries) { // add it the the current bin if (entry.TimeStamp < baseTime + binWidthMillis) { bin.Add(entry); } else // save the old bin and add it to the new one { bins.Add(bin.AsIArray()); while (true) { baseTime += binWidthMillis; if (entry.TimeStamp < baseTime + binWidthMillis) { bin = new List <EEGDataEntry>() { entry }; break; } bins.Add(Arrays.New <EEGDataEntry>(0)); // empty array } } } // make sure to get the last bin! if (bin.Count > 0) { bins.Add(bin.AsIArray()); } return(bins.AsIArray()); }
/// <summary> /// Computes the average of each "feature" /// </summary> public static IArray<double> Averages(this IEnumerable<IEnumerable<double>> data) { var sums = new List<double>(data.First()); int count = 1, i; foreach (var enumerable in data.Skip(1)) { count++; i = 0; foreach (double d in enumerable) if (sums.Count <= i) break; else sums[i++] += d; for (int j = sums.Count - 1; j >= i; j--) sums.RemoveAt(j); } for (i = 0; i < sums.Count; i++) sums[i] /= count; return sums.AsIArray(); }
/// <summary> /// Computes the average of each "feature" /// </summary> public static IArray<double> Averages(this IEnumerable<IEnumerable<double>> data) { var sums = new List<double>(data.First()); int count = 1, i; foreach (var enumerable in data.Skip(1)) { count++; i = 0; foreach (double d in enumerable) if (sums.Count <= i) break; else sums[i++] += d; for (int j = sums.Count - 1; j >= i; j--) sums.RemoveAt(j); } for (i = 0; i < sums.Count; i++) sums[i] /= count; return sums.AsIArray(); }
public IEnumerable <View> RunCompTrial(string stimulus, int cls, StreamWriter dataWriter, StreamWriter logWriter, List <EEGDataEntry> currentTrialEntries) { //Rest yield return(new RestView(this.settings.BlinkTime)); //Fixate yield return(new FixationView(this.settings.FixationTime)); //Generate stimulus view var stimulusView = new TextView(stimulus, this.settings.DisplayTime, GUIUtils.Constants.DISPLAY_FONT_LARGE); stimulusView.DoOnDeploy(c => this.dataSource.Marker = cls); bool needToRerun = false; stimulusView.DoOnFinishing(() => { this.dataSource.Marker = EEGDataEntry.MARKER_DEFAULT; lock (currentTrialEntries) { if (this.settings.ArtifactDetectionSettings.HasMotionArtifact(currentTrialEntries)) { logWriter.WriteLine("Motion Artifact Detected"); needToRerun = true; } else { if (this.settings.SaveTrialData) { foreach (var entry in currentTrialEntries) { dataWriter.WriteLine(entry); } } //Add training trials to the classifier classifier.AddTrial(currentTrialEntries.AsIArray()); } currentTrialEntries.Clear(); } }); logWriter.WriteLine(stimulus); yield return(stimulusView); yield return(new TextView(stimulus + "*", settings.SpeakTime, GUIUtils.Constants.DISPLAY_FONT_LARGE)); //Check number of artifacts if (needToRerun) { if (cls == 1) { numArt1++; } if (cls == 2) { numArt2++; } numArt++; } if (numArt > 12) { numArt = 0; yield return(new TextView("Please keep face movements to a minimum", settings.InstructionTime, GUIUtils.Constants.DISPLAY_FONT_LARGE)); } }
/// <summary> /// Groups entries using the sequences formed by their relative timestamps /// </summary> public static IEnumerable<IArrayView<EEGDataEntry>> SectionByStimulus(this IEnumerable<EEGDataEntry> entries) { if (entries.IsEmpty()) yield break; var section = new List<EEGDataEntry>() { entries.First() }; foreach (var entry in entries.Skip(1)) { if (entry.RelativeTimeStamp >= section.LastItem().RelativeTimeStamp) section.Add(entry); else { yield return section.AsIArray(); section = new List<EEGDataEntry>() { entry }; } } yield return section.AsIArray(); // return the last section }
/// <summary> /// Groups the entries by their respective time bins /// </summary> public static IArrayView<IArrayView<EEGDataEntry>> BinByTime(this IEnumerable<EEGDataEntry> entries, int binWidthMillis) { if (binWidthMillis <= 0) throw new ArgumentOutOfRangeException("binWidthMillis"); int baseTime = entries.First().TimeStamp; var bins = new List<IArrayView<EEGDataEntry>>(); var bin = new List<EEGDataEntry>(); foreach (var entry in entries) // add it the the current bin if (entry.TimeStamp < baseTime + binWidthMillis) bin.Add(entry); else // save the old bin and add it to the new one { bins.Add(bin.AsIArray()); while (true) { baseTime += binWidthMillis; if (entry.TimeStamp < baseTime + binWidthMillis) { bin = new List<EEGDataEntry>() { entry }; break; } bins.Add(Arrays.New<EEGDataEntry>(0)); // empty array } } // make sure to get the last bin! if (bin.Count > 0) bins.Add(bin.AsIArray()); return bins.AsIArray(); }