Exemplo n.º 1
0
        /// <summary>
        /// Unloads content.
        /// </summary>
        public override void UnloadContent()
        {
            if (thread.IsAlive)
            {
                try
                {
                    thread.Interrupt();
                }
                catch (Exception e)
                {
                    ServiceManager.Game.Console.DebugPrint(
                        "[ERROR] At LoadingScreenState#UnloadContent(): {0}",
                        e.Message);
                }
            }

            if (futureGame != null)
            {
                ServiceManager.Game.BackgroundMovie.Pause();
                ServiceManager.MP3Player.Stop();
                ServiceManager.MP3Player.PlayPlaylist();
                futureGame.OnGameFinished += new GamePlayState.GameFinishHandler(OnGameFinished);
            }

            currentMap         = null;
            currentMapInstance = null;
            thread             = null;
            server             = null;
            form           = null;
            futureGame     = null;
            clientCallback = null;
            buffer         = null;
        }
Exemplo n.º 2
0
        public void CanResolveSingleMissingEvent()
        {
            // arrange
            var eventStore = new EventStore().WithEventCount(2);

            using (var buffer = new EventBuffer(2))
                using (var manager = new EventManager(eventStore.SequenceResolver, eventStore.EventResolver, 20, buffer))
                    using (var cts = new CancellationTokenSource())
                    {
                        var events = new List <Event>().AlreadyHaving(2);
                        eventStore.WaitForCall();

                        // act
                        var task = new Task(
                            () =>
                        {
                            var @event = manager.Take(cts.Token);
                            events.Add(@event);
                        });

                        task.Start();
                        eventStore.HasAdditionalEventCount(1);
                        Task.WaitAll(task);

                        // assert
                        @events.ShouldContainEvents(3, 3);
                    }
        }
Exemplo n.º 3
0
        /// <summary>
        /// After the channel models (gmm's) have been created and trained, this function
        /// is used to classifty newly detected spikes for which a valide channel model exisits.
        /// </summary>
        /// <param name="newSpikes"> An EventBuffer conataining spikes to be classified</param>
        public void Classify(ref EventBuffer <SpikeEvent> newSpikes, ref List <double[]> projections2D)
        {
            // Make sure the channel models are trained.
            if (!trained)
            {
                throw new InvalidOperationException("The channel models were not yet trained so classification is not possible.");
            }

            // Sort the channels that need sorting
            for (int i = 0; i < channelsToSort.Count; ++i)
            {
                // Current channel
                int currentChannel = channelsToSort[i];

                // Get the spikes that belong to this channel
                List <SpikeEvent> spikesOnChan = newSpikes.Buffer.Where(x => x.Channel == currentChannel).ToList();

                // If there are no spikes on this channel
                if (spikesOnChan.Count == 0)
                {
                    continue;
                }

                // Get the channel model for this channel
                ChannelModel thisChannelModel = channelModels[channelsToSort.IndexOf(currentChannel)];

                // Project the spikes
                if (this.projectionType == "Maximum Voltage Inflection")
                {
                    thisChannelModel.MaxInflectProject(spikesOnChan.ToList(), inflectionSample);
                }
                else if (this.projectionType == "PCA")
                {
                    thisChannelModel.PCProject(spikesOnChan.ToList());
                }
                else if (this.projectionType == "Haar Wavelet")
                {
                    thisChannelModel.HaarProject(spikesOnChan.ToList());
                }

                // Get the projection
                projections2D = thisChannelModel.Return2DProjection();

                // Sort the spikes
                thisChannelModel.ClassifyThresh();

                // Update the newSpikes buffer
                for (int j = 0; j < spikesOnChan.Count; ++j)
                {
                    if (thisChannelModel.classes[j] < 0)
                    {
                        spikesOnChan[j].SetUnit((Int16)0);
                    }
                    else
                    {
                        spikesOnChan[j].SetUnit((Int16)(thisChannelModel.classes[j] + thisChannelModel.unitStartIndex + 1));
                    }
                }
            }
        }
Exemplo n.º 4
0
 private void SendAndResetDiagnostics(EventBuffer buffer)
 {
     if (_diagnosticStore != null)
     {
         Task.Run(() => SendDiagnosticEventAsync(_diagnosticStore.CreateEventAndReset()));
     }
 }
Exemplo n.º 5
0
 public SpikeSorter(SerializationInfo info, StreamingContext ctxt)
 {
     this.trained                     = (bool)info.GetValue("trained", typeof(bool));
     this.numberChannels              = (int)info.GetValue("numberChannels", typeof(int));
     this.minSpikes                   = (int)info.GetValue("minSpikes", typeof(int));
     this.maxK                        = (int)info.GetValue("maxK", typeof(int));
     this.inflectionSample            = (int)info.GetValue("inflectionSample", typeof(int));
     this.trainingSpikes              = (EventBuffer <SpikeEvent>)info.GetValue("trainingSpikes", typeof(EventBuffer <SpikeEvent>));
     this.channelsToSort              = (List <int>)info.GetValue("channelsToSort", typeof(List <int>));
     this.channelModels               = (List <ChannelModel>)info.GetValue("channelModels", typeof(List <ChannelModel>));
     this.unitDictionary              = (Hashtable)info.GetValue("unitDictionary", typeof(Hashtable));
     this.projectionType              = (string)info.GetValue("projectionType", typeof(string));
     this.projectionDimension         = (int)info.GetValue("projectionDimension", typeof(int));
     this.maxTrainingSpikesPerChannel = (int)info.GetValue("maxTrainingSpikesPerChannel", typeof(int));
     this.spikesCollectedPerChannel   = (Hashtable)info.GetValue("spikesCollectedPerChannel", typeof(Hashtable));
     this.totalNumberOfUnits          = (int)info.GetValue("totalNumberOfUnits", typeof(int));
     this.pValue                      = (double)info.GetValue("pValue", typeof(double));
     this.secondInflectionIndex       = (int)info.GetValue("secondInflectionIndex", typeof(int));
     try
     {
         this.unit2Channel = (Dictionary <int, int>)info.GetValue("unit2Channel", typeof(Dictionary <int, int>));
     }
     catch
     {
     }
 }
Exemplo n.º 6
0
        internal EventProcessorInternal(
            EventsConfiguration config,
            BlockingCollection <IEventMessage> messageQueue,
            IEventSender eventSender,
            IUserDeduplicator userDeduplicator,
            IDiagnosticStore diagnosticStore,
            Logger logger,
            Action testActionOnDiagnosticSend
            )
        {
            _config                     = config;
            _diagnosticStore            = diagnosticStore;
            _userDeduplicator           = userDeduplicator;
            _testActionOnDiagnosticSend = testActionOnDiagnosticSend;
            _flushWorkersCounter        = new CountdownEvent(1);
            _eventSender                = eventSender;
            _logger                     = logger;
            _random                     = new Random();

            EventBuffer buffer = new EventBuffer(config.EventCapacity > 0 ? config.EventCapacity : 1, _diagnosticStore, _logger);

            // Here we use TaskFactory.StartNew instead of Task.Run() because that allows us to specify the
            // LongRunning option. This option tells the task scheduler that the task is likely to hang on
            // to a thread for a long time, so it should consider growing the thread pool.
            Task.Factory.StartNew(
                () => RunMainLoop(messageQueue, buffer),
                TaskCreationOptions.LongRunning
                );
        }
Exemplo n.º 7
0
        public void CanResolveMultipleNonContiguousMissingEvents()
        {
            // arrange
            var eventStore = new EventStore().WithEventCount(2);

            using (var buffer = new EventBuffer(2))
                using (var manager = new EventManager(eventStore.SequenceResolver, eventStore.EventResolver, 20, buffer))
                    using (var cts = new CancellationTokenSource())
                    {
                        var events = new List <Event>().AlreadyHaving(2);
                        eventStore.WaitForCall();

                        // act
                        manager.Add(EventStore.Event.Number(5));

                        var task = new Task(
                            () =>
                        {
                            do
                            {
                                var @event = manager.Take(cts.Token);
                                events.Add(@event);
                            }while (@events.Count <= 10);
                        });

                        task.Start();
                        eventStore.HasAdditionalEventCount(8);
                        Task.WaitAll(task);

                        // assert
                        @events.ShouldContainEvents(3, 10);
                    }
        }
        public void HasUncommittedEvents_DiffrentEventValues_DisplayedInTheMessage()
        {
            using (CultureInfoScope.NewInvariant())
            {
                var buffer = EventBuffer.Empty(Guid.NewGuid())
                             .Add(new object[]
                {
                    new EmptyEvent(),
                    new Complex {
                        Value = 17, Message = "Same", Date = new DateTime(2017, 06, 11)
                    },
                    new SimpleEvent(),
                });

                var x = Assert.Catch <AssertionFailed>(() =>
                                                       AggregateRootAssert.HasUncommittedEvents(buffer,
                                                                                                new EmptyEvent(),
                                                                                                new Complex {
                    Value = 23, Message = "Same", Date = new DateTime(1980, 06, 30)
                },
                                                                                                new SimpleEvent()
                                                                                                ));

                Assert.AreEqual(@"Assertion failed:
[0] EmptyEvent
[1] Expected: { Value: 23, Date: 06/30/1980 00:00:00 }
    Actual:   { Value: 17, Date: 06/11/2017 00:00:00 }
[2] SimpleEvent
", x.Message);
            }
        }
Exemplo n.º 9
0
        public void CanForceReplayEvents()
        {
            // arrange
            var eventStore = new EventStore().WithEventCount(10);

            using (var buffer = new EventBuffer(10))
                using (var manager = new EventManager(eventStore.SequenceResolver, eventStore.EventResolver, Timeout.Infinite, buffer))
                    using (var cts = new CancellationTokenSource())
                    {
                        var events = new List <Event>()
                        {
                            null                      /* compensate for non zero-based */
                        };

                        // act
                        manager.ReplayEvents();
                        do
                        {
                            var @event = manager.Take(cts.Token);
                            events.Add(@event);
                        }
                        // assert
                        while (@events.Count <= 10);
                        @events.ShouldContainEvents(1, 10);
                    }
        }
Exemplo n.º 10
0
        public void with_uncommitted()
        {
            var buffer = EventBuffer.Empty(Guid.Parse("1F8B5071-C03B-457D-B27F-442C5AAC5785"))
                         .Add(new EmptyEvent());

            DebuggerDisplayAssert.HasResult("Version: 1 (Committed: 0), Aggregate: 1f8b5071-c03b-457d-b27f-442c5aac5785", buffer);
        }
Exemplo n.º 11
0
        private async Task PublishEventsAsync()
        {
            if (publishedEvents.Count > 0)
            {
                if (EventBuffer == null)
                {
                    throw new InvalidOperationException($"Cannot publish events from {this} because its {nameof(EventBuffer)} has not been set");
                }

                if (EventMessageFactory == null)
                {
                    throw new InvalidOperationException($"Cannot publish events from {this} because its {nameof(EventMessageFactory)} has not been set");
                }
            }

            foreach (var ev in publishedEvents)
            {
                var eventMessage = await EventMessageFactory.CreateMessageAsync(ev);

                if (eventMessage.Metadata.GetEventId() == null)
                {
                    eventMessage.SetMetadata(BasicEventMetadataNames.EventId, Guid.NewGuid().ToString());
                }

                EventBuffer.PushEvent(eventMessage);
            }
        }
        public void CollectionChangedIsCombinedWhenBufferingMultipleAddsAndRemoves()
        {
            var eventBuffer = new EventBuffer();
            var sut         = CreateSut(eventBuffer);

            sut.Add(3);
            eventBuffer.StartBuffering();
            var raised = false;

            sut.CollectionChanged += (sender, args) =>
            {
                Assert.AreEqual(new[] { -1, -2 }, args.AddedItems);
                Assert.AreEqual(new[] { 3 }, args.RemovedItems);
                raised = true;
            };

            sut.Add(-1);
            sut.Add(-1);
            sut.Add(-2);
            sut.Remove(-2);
            sut.Remove(3);
            sut.Add(-2);
            sut.Add(3);
            sut.Remove(3);
            eventBuffer.Flush();

            Assert.IsTrue(raised);
        }
Exemplo n.º 13
0
        private void StartCatchUpSubscription(Position?startPosition)
        {
            lock (LockObj)
            {
                _eventBuffer = new EventBuffer(_batchSize + 28);
            }
            _onCompletedFired = false;
            _isStarted        = true;
            var settings = new CatchUpSubscriptionSettings(_maxLiveQueueMessage, _batchSize, true, false);

            if (startPosition == null)
            {
                var slice =
                    _connection.Value.ReadAllEventsBackwardAsync(Position.End, 1, false, _userCredentials).Result;
                _lastAllPosition = slice.FromPosition;
            }
            _subscription = _connection.Value.SubscribeToAllFrom(
                startPosition ?? AllCheckpoint.AllStart,
                settings,
                EventAppeared,
                LiveProcessingStarted,
                SubscriptionDropped,
                _userCredentials);

            _trace.Info($"Catch-up subscription started from checkpoint {startPosition} at {DateTime.Now}.");
        }
    public static void HasUncommittedEvents <TId>(EventBuffer <TId> actualBuffer, params object[] expectedEvents)
    {
        Guard.NotNull(expectedEvents, nameof(expectedEvents));

        Assert.IsNotNull(actualBuffer, nameof(actualBuffer));

        var sb         = new StringBuilder();
        var failure    = false;
        var offset     = actualBuffer.CommittedVersion;
        var uncomitted = actualBuffer.Uncommitted.ToArray();
        var shared     = Math.Min(expectedEvents.Length, uncomitted.Length);

        for (var i = 0; i < shared; i++)
        {
            failure |= sb.AppendEvents(offset + i, expectedEvents[i], uncomitted[i]);
        }

        failure |= sb.AppendExtraEvents(uncomitted, offset, shared, "Extra:  ");
        failure |= sb.AppendExtraEvents(expectedEvents, offset, shared, "Missing: ");

        if (failure)
        {
            sb.Insert(0, $"Assertion failed:{Environment.NewLine}");
            Assert.Fail(sb.ToString());
        }
    }
        public void HasUncommittedEvents_WithMissingEvents_DisplayedInTheMessage()
        {
            var buffer = EventBuffer.Empty(Guid.NewGuid())
                         .Add(new object[]
            {
                new EmptyEvent(),
                new OtherEvent {
                    Value = 3
                }
            });

            var x = Assert.Catch <AssertionFailed>(() =>
                                                   AggregateRootAssert.HasUncommittedEvents(buffer,
                                                                                            new EmptyEvent(),
                                                                                            new OtherEvent {
                Value = 3
            },
                                                                                            new SimpleEvent {
                Value = 5
            },
                                                                                            new OtherEvent {
                Value = 9
            }
                                                                                            ));

            Assert.AreEqual(@"Assertion failed:
[0] EmptyEvent
[1] OtherEvent
[2] Missing:  SimpleEvent
[3] Missing:  OtherEvent
", x.Message);
        }
Exemplo n.º 16
0
        public void FromStorage_contains_all_events_as_committed()
        {
            var stored = new[] { new EmptyEvent(), new EmptyEvent(), new EmptyEvent() };
            var buffer = EventBuffer.FromStorage(Guid.NewGuid(), stored, (@event) => @event);

            buffer.Should().HaveCount(3)
            .And.Subject.As <EventBuffer <Guid> >().CommittedVersion.Should().Be(3);
        }
Exemplo n.º 17
0
        public void FromStorage_takes_initial_version_into_account()
        {
            var stored = new[] { new EmptyEvent(), new EmptyEvent(), new EmptyEvent() };
            var buffer = EventBuffer.FromStorage(Guid.NewGuid(), initialVersion: 6, stored, (@event) => @event);

            buffer.Should().HaveCount(3)
            .And.Subject.As <EventBuffer <Guid> >().CommittedVersion.Should().Be(9);
        }
        private void StartMessageBuffer()
        {
            byte[] buffer = new byte[2];

            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    await _stream.ReadAsync(buffer, 0, buffer.Length, CancellationToken);

                    short size = (short)(buffer[1] | buffer[2] << 8);
                    if (size > 0)
                    {
                        byte[] payLoad = new byte[size];
                        await _stream.ReadAsync(payLoad, 0, payLoad.Length);
                        ResponseBuffer.TryAdd(Response.GetId(payLoad), payLoad);
                    }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);

            Task.Factory.StartNew(async() =>
            {
                while (!CancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        Command command;
                        if (NoReplyCommandBuffer.TryDequeue(out command))
                        {
                            await _stream.WriteAsync(command.PayLoad, 0, command.PayLoad.Length);
                            _stream.Flush();
                            ResponseBuffer.TryAdd(command.Id, null);
                        }


                        if (CommandBuffer.TryDequeue(out command))
                        {
                            await _stream.WriteAsync(command.PayLoad, 0, command.PayLoad.Length);
                            _stream.Flush();

                            int retry = 0;
                            while (!ResponseBuffer.ContainsKey(command.Id) && retry < 20)
                            {
                                await Task.Delay(50, CancellationToken);
                                retry++;
                            }
                        }

                        if (EventBuffer.TryDequeue(out command))
                        {
                            await _stream.WriteAsync(command.PayLoad, 0, command.PayLoad.Length);
                            _stream.Flush();
                        }
                    }
                    catch (TaskCanceledException) { }
                }
            }, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
        }
Exemplo n.º 19
0
        public void BufferInvokesWhenNotBuffering()
        {
            var sut        = new EventBuffer();
            var bufferable = CreateImmediatelyInvokedEvent(1);

            sut.Buffer(bufferable.Object, 1);

            bufferable.Verify();
        }
Exemplo n.º 20
0
        private void AnalogInCallback_AuxDig(IAsyncResult ar)
        {
            lock (this)
            {
                try
                {
                    if (taskRunning)
                    {
                        //Read the available data from the channels
                        auxDigData = auxDigReader.EndReadMultiSamplePortUInt32(ar);
                        trackingDigReads++;

                        //Find changes in digital state
                        for (int i = 0; i < spikeBufferLength; ++i)
                        {
                            if (auxDigData[i] != lastDigState)
                            {
                                //int dt = DateTime.Now.Millisecond;
                                lastDigState = auxDigData[i];

                                // Create Digital data
                                if (Properties.Settings.Default.useDigDataBuffer)
                                {
                                    DigitalPortEvent thisPortEvent = new DigitalPortEvent((ulong)i, lastDigState);
                                    EventBuffer <DigitalPortEvent> thisDigitalEventBuffer = new EventBuffer <DigitalPortEvent>(Properties.Settings.Default.RawSampleFrequency);
                                    thisDigitalEventBuffer.Buffer.Add(thisPortEvent);

                                    // send to datSrv
                                    datSrv.AuxDigitalSrv.WriteToBufferRelative(thisDigitalEventBuffer, 0);
                                }

                                if (switch_record.Value && recordingSettings.recordAuxDig)
                                {
                                    recordingSettings.auxDigitalOut.write(i, lastDigState, trackingDigReads, spikeBufferLength);
                                }

                                //// Update led array
                                //bool[] boolLEDState = new bool[32];
                                //var ledState = new BitArray(new int[] { (int)auxDigData[i] });
                                //for (int j = 0; j < 32; j++)
                                //    boolLEDState[j] = ledState[j];
                                //ledArray_DigitalState.SetValues(boolLEDState, 0, 32);
                            }
                        }

                        // Start next read
                        auxDigReader.BeginReadMultiSamplePortUInt32(spikeBufferLength, auxDigCallback, auxDigReader);
                    }
                }
                catch (DaqException exception)
                {
                    //Display Errors
                    MessageBox.Show(exception.Message);
                    reset();
                }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Constructs a VTankBot.
        /// </summary>
        /// <param name="runner">Parent bot runner.</param>
        /// <param name="server">Target Echelon server.</param>
        /// <param name="auth">Authentication information.</param>
        public VTankBot(BotRunner runner, TargetServer server, AuthInfo auth)
        {
            BotRunner     = runner;
            ServerAddress = server;
            AuthInfo      = auth;
            Game          = new GameTracker(this);
            buffer        = new EventBuffer();

            CreateMasterCommunicator();
        }
Exemplo n.º 22
0
        public static Result <Game> Handle(object command, Buffer buffer = null)
        {
            buffer ??= EventBuffer.Empty(GameId);
            var processor = new TestProcessor(buffer, 17);
            var result    = processor.Send(command);

            return(result.IsValid
                ? Result.For(processor.Buffer.Load(), result.Messages)
                : Result.WithMessages <Game>(result.Messages));
        }
Exemplo n.º 23
0
        public void BufferDoesNotInvokeWhenBuffering()
        {
            var sut        = new EventBuffer();
            var bufferable = new Mock <IBufferableEvent <int> >();

            sut.StartBuffering();
            sut.Buffer(bufferable.Object, 1);

            bufferable.VerifyNoOtherCalls();
        }
Exemplo n.º 24
0
    public static TAggregate FromStorage <TAggregate, TId>(EventBuffer <TId> buffer)
        where TAggregate : AggregateRoot <TAggregate, TId>, new()
    {
        Guard.HasAny(buffer, nameof(buffer));

        var aggregate = new TAggregate();

        aggregate.Replay(buffer);
        return(aggregate);
    }
Exemplo n.º 25
0
 /// <summary>
 /// Hoard spikes to populate the buffer of spikes that will be used to train the classifiers
 /// </summary>
 /// <param name="newSpikes"> An EventBuffer contain spikes to add to the training buffer</param>
 public void HoardSpikes(EventBuffer <SpikeEvent> newSpikes)
 {
     for (int i = 0; i < newSpikes.Buffer.Count; ++i)
     {
         if ((int)spikesCollectedPerChannel[newSpikes.Buffer[i].Channel] < maxTrainingSpikesPerChannel)
         {
             spikesCollectedPerChannel[newSpikes.Buffer[i].Channel] = (int)spikesCollectedPerChannel[newSpikes.Buffer[i].Channel] + 1;
             trainingSpikes.Buffer.Add(newSpikes.Buffer[i]);
         }
     }
 }
Exemplo n.º 26
0
 public static Buffer BeneluxWithoutArmies(int roundLimit = 10)
 => EventBuffer.Empty(GameId)
 .Add(new SettingsInitialized(2, roundLimit, false))
 .Add(new MapInitialized(
          Continents: new[] { new ContinentInitialized("Benelux", 3, new[] { Netherlands, Belgium, Luxembourg }) },
          Countries: new[]
 {
     new CountryInitialized("Netherlands", new [] { Belgium }),
     new CountryInitialized("Belgium", new [] { Netherlands, Luxembourg }),
     new CountryInitialized("Luxembourg", new [] { Belgium }),
 }));
Exemplo n.º 27
0
 public Calculator(
     EventBuffer eventBuffer, IModifierCollection modifierCollection,
     ICalculationGraphPruner graphPruner, INodeRepository nodeRepository,
     INodeCollection <IStat> explicitlyRegisteredStats)
 {
     _eventBuffer              = eventBuffer;
     _modifierCollection       = modifierCollection;
     _graphPruner              = graphPruner;
     NodeRepository            = nodeRepository;
     ExplicitlyRegisteredStats = explicitlyRegisteredStats;
 }
Exemplo n.º 28
0
        public void FlushInvokesBufferedCall()
        {
            var sut        = new EventBuffer();
            var bufferable = CreateBufferInvokedEvent(1);

            sut.StartBuffering();

            sut.Buffer(bufferable.Object, 1);
            sut.Flush();

            bufferable.Verify();
        }
        public void HasUncommittedEvents_EqualArrayValues_IsTrue()
        {
            var buffer = EventBuffer.Empty(Guid.NewGuid())
                         .Add(new ArrayEvent {
                Numbers = new[] { 17 }
            });

            AggregateRootAssert.HasUncommittedEvents(buffer,
                                                     new ArrayEvent {
                Numbers = new[] { 17 }
            });
        }
Exemplo n.º 30
0
        public GamePlayState(GameCallback _callback, Map _map, EventBuffer _buffer)
            : base(false) // False meaning, do not load/draw the background.
        {
            map = _map;
            //form = new InGameMenu(ServiceManager.Game.Manager);
            ServiceManager.Game.FormManager.RemoveWindow(
                ServiceManager.Game.FormManager.currentWindow);

            callback = _callback;
            buffer   = _buffer;
            prevFrameScrollWheelValue = 0f;
        }
        public EventBuffer Build()
        {
            EventBuilder eventBuilder = new EventBuilder();

            EventBuffer buffer = new EventBuffer();
            Repeat.Times(eventCount).Action(() => buffer.Add(eventBuilder.Build()));

            foreach (var evnt in events)
            {
                buffer.Add(evnt);
            }

            return buffer;
        }
        private static ITimer StartPublishingTo(EventBuffer eventBuffer)
        {
            ITimer publishTimer = new ScheduledTimer(2000);
            int id = 0;

            publishTimer.TimerFired += ((o, a) =>
                                            {
                                                publishTimer.Stop();
                                                eventBuffer.Add(new Event(id++, "event: " + id, DateTime.Now, new EventBody("application/vnd.restbucks+xml", new Uri("http://product/" + id), new Product {Name = "product", Price = 10.0, Size = "1kg"})));
                                                publishTimer.Start();
                                            });
            publishTimer.Start();
            return publishTimer;
        }
        public FeedWriter(ITimer timer, EventBuffer buffer, IFileSystem fileSystem, FeedBuilder feedBuilder)
        {
            Check.IsNotNull(timer, "timer");
            Check.IsNotNull(buffer, "buffer");
            Check.IsNotNull(fileSystem, "fileSystem");
            Check.IsNotNull(feedBuilder, "feedBuilder");

            this.timer = timer;
            this.fileSystem = fileSystem;
            this.buffer = buffer;
            this.feedBuilder = feedBuilder;

            this.timer.TimerFired += TimerFiredHandler;

            reaper = new Reaper(fileSystem);
            FeedMappingsChanged += reaper.OnFeedMappingsChanged;
        }
        public void IfFeedFillsExactlyToQuotaShouldNotBeArchived()
        {
            FileName tempFileName = null;
            FeedBuilder feedBuilder = new FeedBuilder(SampleLinks.Instance);

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            Repeat.Times(3).Action(() => buffer.Add(new EventBuilder().Build()));

            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));
            feedWriter.FeedMappingsChanged += ((o, args) => tempFileName = new FileName(args.RecentEventsFeedStoreId));

            timer.Fire();

            Assert.AreEqual(1, fileSystem.FileCount(fileSystem.CurrentDirectory));
            Assert.AreEqual(0, fileSystem.FileCount(fileSystem.ArchiveDirectory));

            RecentEventsFeed rehydratedFeed = feedBuilder.LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(3, rehydratedFeed.GetSyndicationFeed().Items.Count());
        }
        public void WhenTriggeredShouldAddBatchOfEventsToCurrentFeed()
        {
            FileName tempFileName = null;

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            EventBuilder eventBuilder = new EventBuilder();
            Repeat.Times(2).Action(() => buffer.Add(eventBuilder.Build()));

            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));
            feedWriter.FeedMappingsChanged += ((o, args) => tempFileName = new FileName(args.RecentEventsFeedStoreId));

            timer.Fire();

            Assert.AreEqual(1, fileSystem.FileCount(fileSystem.CurrentDirectory));

            RecentEventsFeed rehydratedFeed = new FeedBuilder(SampleLinks.Instance).LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(2, rehydratedFeed.GetNumberOfEntries());
        }
        public void WhenTriggeredRepeatedlyShouldContinueToAddEventsToExistingFeed()
        {
            FileName tempFileName = null;
            FeedBuilder feedBuilder = new FeedBuilder(SampleLinks.Instance);

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            buffer.Add(new EventBuilder().Build());

            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));
            feedWriter.FeedMappingsChanged += ((o, args) => tempFileName = new FileName(args.RecentEventsFeedStoreId));

            timer.Fire();

            RecentEventsFeed rehydratedFeed = feedBuilder.LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(1, rehydratedFeed.GetSyndicationFeed().Items.Count());
            Assert.AreEqual("?page=1", rehydratedFeed.GetSyndicationFeed().GetViaLink().Uri.Query);

            buffer.Add(new EventBuilder().Build());

            timer.Fire();

            RecentEventsFeed secondRehydratedFeed = feedBuilder.LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(2, secondRehydratedFeed.GetSyndicationFeed().Items.Count());
            Assert.AreEqual("?page=1", secondRehydratedFeed.GetSyndicationFeed().GetViaLink().Uri.Query);
        }
        public void WhenThereAreNoOutstandingEventsShouldRetainTheCurrentTempFileName()
        {
            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();
            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));

            Repeat.Times(1).Action(() => buffer.Add(new EventBuilder().Build()));

            timer.Fire();

            Repeat.Times(0).Action(() => buffer.Add(new EventBuilder().Build()));

            timer.Fire();

            Repeat.Times(1).Action(() => buffer.Add(new EventBuilder().Build()));

            timer.Fire();

            Assert.AreEqual(2, fileSystem.FileCount(fileSystem.CurrentDirectory));
        }
        public void WhenThereAreNoOutstandingEventsShouldNotWriteFeedOrRaiseEvent()
        {
            int eventCount = 0;

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));
            feedWriter.FeedMappingsChanged += ((o, args) => eventCount++);

            timer.Fire();

            Assert.AreEqual(0, eventCount);
            Assert.AreEqual(0, fileSystem.FileCount(fileSystem.CurrentDirectory));
        }
        public void WhenNumberOfEventsExceedsQuotaShouldArchiveFeedAndBeginAnotherOne()
        {
            FileName tempFileName = null;
            FeedBuilder feedBuilder = new FeedBuilder(SampleLinks.Instance);

            InMemoryFileSystem fileSystem = new InMemoryFileSystem();
            EventBuffer buffer = new EventBuffer();

            EventBuilder eventBuilder = new EventBuilder();
            Repeat.Times(RecentEventsFeed.Quota + 1).Action(() => buffer.Add(eventBuilder.Build()));

            FakeTimer timer = new FakeTimer();
            FeedWriter feedWriter = new FeedWriter(timer, buffer, fileSystem, new FeedBuilder(SampleLinks.Instance));
            feedWriter.FeedMappingsChanged += ((o, args) => tempFileName = new FileName(args.RecentEventsFeedStoreId));

            timer.Fire();

            Assert.AreEqual(1, fileSystem.FileCount(fileSystem.CurrentDirectory));
            Assert.AreEqual(1, fileSystem.FileCount(fileSystem.ArchiveDirectory));

            ArchiveFeed archivedFeed = LoadArchiveFeed(fileSystem, new FileName("1"));
            Assert.AreEqual(10, archivedFeed.GetNumberOfEntries());
            Assert.AreEqual("?page=1", archivedFeed.GetSyndicationFeed().GetSelfLink().Uri.Query);
            Assert.AreEqual("?page=2", archivedFeed.GetSyndicationFeed().GetNextArchiveLink().Uri.Query);

            RecentEventsFeed recentEventsFeed = feedBuilder.LoadRecentEventsFeed(fileSystem, tempFileName);
            Assert.AreEqual(1, recentEventsFeed.GetSyndicationFeed().Items.Count());
            Assert.AreEqual("?page=2", recentEventsFeed.GetSyndicationFeed().GetViaLink().Uri.Query);
        }
Exemplo n.º 40
0
        /// <summary>
        /// Hoard spikes to populate the buffer of spikes that will be used to train the classifiers
        /// </summary>
        /// <param name="newSpikes"> An EventBuffer contain spikes to add to the training buffer</param>
        public void HoardSpikes(EventBuffer<SpikeEvent> newSpikes)
        {
            for (int i = 0; i < newSpikes.Buffer.Count; ++i)
            {

                if ((int)spikesCollectedPerChannel[newSpikes.Buffer[i].Channel] < maxTrainingSpikesPerChannel)
                {
                    spikesCollectedPerChannel[newSpikes.Buffer[i].Channel] = (int)spikesCollectedPerChannel[newSpikes.Buffer[i].Channel] + 1;
                    trainingSpikes.Buffer.Add(newSpikes.Buffer[i]);
                }
            }
        }
Exemplo n.º 41
0
        private void AnalogInCallback_AuxDig(IAsyncResult ar)
        {
            lock (this)
            {
                try
                {
                    if (taskRunning)
                    {
                        //Read the available data from the channels
                        auxDigData = auxDigReader.EndReadMultiSamplePortUInt32(ar);
                        trackingDigReads++;

                        //Find changes in digital state
                        for (int i = 0; i < spikeBufferLength; ++i)
                        {
                            if (auxDigData[i] != lastDigState)
                            {
                                //int dt = DateTime.Now.Millisecond;
                                lastDigState = auxDigData[i];

                                // Create Digital data
                                if (Properties.Settings.Default.useDigDataBuffer)
                                {
                                    DigitalPortEvent thisPortEvent = new DigitalPortEvent((ulong)i, lastDigState);
                                    EventBuffer<DigitalPortEvent> thisDigitalEventBuffer = new EventBuffer<DigitalPortEvent>(Properties.Settings.Default.RawSampleFrequency);
                                    thisDigitalEventBuffer.Buffer.Add(thisPortEvent);

                                    // send to datSrv
                                    datSrv.AuxDigitalSrv.WriteToBufferRelative(thisDigitalEventBuffer, 0);
                                }

                                if (switch_record.Value && recordingSettings.recordAuxDig)
                                {
                                    recordingSettings.auxDigitalOut.write(i, lastDigState, trackingDigReads, spikeBufferLength);
                                }

                                //// Update led array
                                //bool[] boolLEDState = new bool[32];
                                //var ledState = new BitArray(new int[] { (int)auxDigData[i] });
                                //for (int j = 0; j < 32; j++)
                                //    boolLEDState[j] = ledState[j];
                                //ledArray_DigitalState.SetValues(boolLEDState, 0, 32);
                            }
                        }

                        // Start next read
                        auxDigReader.BeginReadMultiSamplePortUInt32(spikeBufferLength, auxDigCallback, auxDigReader);
                    }
                }
                catch (DaqException exception)
                {
                    //Display Errors
                    MessageBox.Show(exception.Message);
                    reset();
                }
            }
        }
Exemplo n.º 42
0
        private void AnalogInCallback_spikes(IAsyncResult ar)
        {
            try
            {
                if (taskRunning)
                {
                    int taskNumber = (int)ar.AsyncState;
                    // Debugger.Write(taskNumber.ToString() + ":spike callback start");
                    trackingReads[taskNumber]++;

                    #region Stim_Timing_Acquisition
                    if (Properties.Settings.Default.UseStimulator && Properties.Settings.Default.RecordStimTimes)
                    {
                        lock (numStimReadsLock)
                        {
                            bool getStimData = true;
                            for (int i = 0; i < numStimReads.Count; ++i)
                            {
                                if (numStimReads[taskNumber] < numStimReads[i]) //Test if all stim reads are equal
                                {
                                    getStimData = false;
                                    ++numStimReads[taskNumber];
                                    break;
                                }
                            }

                            if (getStimData)
                            {
                                // This holds everything up since it does not have a callback or anything.
                                //This read handles both stim data and optional aux analog in data. Both are stored in stimDataTmp and parsed out later
                                //int numSampRead;
                                double[,] stimData = stimTimeReader.ReadMultiSample(spikeBufferLength);

                                //stimTimeReader.MemoryOptimizedReadMultiSample(spikeBufferLength, ref stimDataTmp, out numSampRead);

                                //double[,] stimDataTmp = new double[stimTimeChanSet.numericalChannels.Length, spikeBufferLength];

                                //Read the available data from the channels
                                if (twoAITasksOnSingleBoard)
                                {

                                    //Array.Copy(stimData, 0, stimDataTmp, stimTimeChanSet.numericalChannels[0] * spikeBufferLength,
                                    //  stimTimeChanSet.numericalChannels.Length * spikeBufferLength);

                                    AuxAnalogFromStimData(stimData);
                                }

                                //Copy new data into prepended data, to deal with edge effects
                                double[] prependedData = new double[spikeBufferLength + STIM_BUFFER_LENGTH];
                                for (int i = 0; i < STIM_BUFFER_LENGTH; ++i)
                                    prependedData[i] = stimDataBuffer[i];
                                for (int i = 0; i < spikeBufferLength; ++i)
                                    prependedData[i + STIM_BUFFER_LENGTH] = stimData[0, i];

                                int startTimeStim = numStimReads[taskNumber] * spikeBufferLength - STIM_BUFFER_LENGTH; //Used to mark stim time for file

                                //Encoding is [v1 v2 v3], each lasting 200us
                                //'v1' and 'v2' encode channel number, 'v3' is the stim voltage
                                //'v1' says "which group of eight" was stimulated, 'v2' says
                                //     "which electrode in the group of eight".  E.g., electrode
                                //     16 would have v1=2 and v2=8.  'v1' and 'v2' are always in
                                //     the range of 1-8 volts
                                EventBuffer<ElectricalStimEvent> tempStimBuff = new EventBuffer<ElectricalStimEvent>(Properties.Settings.Default.RawSampleFrequency);

                                lock (stimIndices)
                                    for (int i = 0; i < spikeBufferLength; ++i)
                                    {
                                        //Check for stimIndices (this uses a different buffer, so it's synced to each buffer read
                                        if (stimData[0, i] > 0.9)
                                            stimIndices.Add(new StimTick(i, numStimReads[taskNumber]));

                                        //Get appropriate data and write to file
                                        if (prependedData[i] > 0.8 && prependedData[i + (int)stimJump] > 0.8 && prependedData[i + (int)(2 * stimJump)] > 0.8)
                                        {

                                            // Create ElectricalStimEvent Buffer and add it datSrv
                                            ElectricalStimEvent tempStimEvent = new ElectricalStimEvent((ulong)(startTimeStim + i),
                                                (short)((Convert.ToInt16((prependedData[i + 1] + prependedData[i + (int)stimJump]) / 2) -
                                                (short)1) * (short)8 +
                                                Convert.ToInt16((prependedData[i + (int)(2 * stimJump) + 1] + prependedData[i + (int)(3 * stimJump)]) / 2)),
                                                prependedData[i + (int)(5 * stimJump)], //Stim voltage
                                                prependedData[i + (int)(7 * stimJump)]);
                                            tempStimBuff.Buffer.Add(tempStimEvent);

                                            // send to datSrv

                                            if (switch_record.Value && recordingSettings.recordStim)
                                            {
                                                recordingSettings.stimOut.write(startTimeStim, prependedData, stimJump, i);
                                            }

                                            //Overwrite data as 0s, to prevent detecting the middle of a stim pulse in the next buffer cycle
                                            for (int j = 0; j < (int)(8 * stimJump) + 1; ++j)
                                                prependedData[j + i] = 0;
                                            i += (int)(9 * stimJump); //Jump past rest of waveform
                                        }

                                    }

                                datSrv.StimSrv.WriteToBuffer(tempStimBuff, 0);

                                if (!inTrigger) //Assumes trigger lasts longer than refresh time
                                {
                                    for (int i = 0; i < stimData.GetLength(1); ++i)
                                    {
                                        //Check if there's a trigger change
                                        if (stimData[1, i] > 2.5)
                                        {
                                            triggerStartTime = i + numStimReads[taskNumber] * spikeBufferLength;
                                            triggerStopTime = double.PositiveInfinity; //Do this to ensure that we capture spikes till
                                            inTrigger = true;

            #if (DEBUG1)
                                            logFile.WriteLine("Trigger start time: " + triggerStartTime);
            #endif
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    for (int i = 0; i < stimData.GetLength(1); ++i)
                                    {
                                        if (stimData[1, i] < 2.5)
                                        {
                                            triggerStopTime = i + numStimReads[taskNumber] * spikeBufferLength;
                                            inTrigger = false;
            #if (DEBUG1)
                                            logFile.WriteLine("Trigger stop time: " + triggerStopTime);
            #endif
                                            break;
                                        }
                                    }
                                }

                                for (int i = spikeBufferLength; i < spikeBufferLength + STIM_BUFFER_LENGTH; ++i)
                                    stimDataBuffer[i - spikeBufferLength] = prependedData[i];

                                //Clear out expired stimIndices
                                if (stimIndices.Count > 0)
                                {
                                    int oldestStimRead = numStimReads[0];
                                    for (int i = 1; i < numStimReads.Count; ++i)
                                    {
                                        if (numStimReads[i] < oldestStimRead)
                                            oldestStimRead = numStimReads[i];
                                    }
                                    lock (stimIndices)
                                        for (int i = stimIndices.Count - 1; i >= 0; --i)
                                            if (stimIndices[i].numStimReads < oldestStimRead - 1) //Add -1 to buy us some breating room
                                                stimIndices.RemoveAt(i);
                                }
                                ++numStimReads[taskNumber];
                            }
                        }
                    }
                    #endregion
                    bwIsRunning[taskNumber] = true;
                    if (!bwSpikes[taskNumber].IsBusy)
                        bwSpikes[taskNumber].RunWorkerAsync(new Object[] { taskNumber, ar });
                    else
                    {
                        DateTime errortime = DateTime.Now;
                        string format = "HH:mm:ss";    // Use this format
                        Console.WriteLine("Warning: bwSpikes was busy at: " + errortime.ToString(format));  // Write to console
                    }
                    //Debugger.Write(taskNumber.ToString() + ":spike callback stop");
                }
            }
            catch (DaqException exception)
            {
                //Display Errors
                MessageBox.Show(exception.Message);
                reset();
            }
        }
Exemplo n.º 43
0
 private TollDataGenerator(Random r)
 {
     random = r;
     eventBuffer = new EventBuffer();
     commercialVehicleRegistration = File.ReadAllLines(@"Data\Registration.csv");
 }
Exemplo n.º 44
0
 public SpikeSorter(SerializationInfo info, StreamingContext ctxt)
 {
     this.trained = (bool)info.GetValue("trained", typeof(bool));
     this.numberChannels = (int)info.GetValue("numberChannels", typeof(int));
     this.minSpikes = (int)info.GetValue("minSpikes", typeof(int));
     this.maxK = (int)info.GetValue("maxK", typeof(int));
     this.inflectionSample = (int)info.GetValue("inflectionSample", typeof(int));
     this.trainingSpikes = (EventBuffer<SpikeEvent>)info.GetValue("trainingSpikes", typeof(EventBuffer<SpikeEvent>));
     this.channelsToSort = (List<int>)info.GetValue("channelsToSort", typeof(List<int>));
     this.channelModels = (List<ChannelModel>)info.GetValue("channelModels", typeof(List<ChannelModel>));
     this.unitDictionary = (Hashtable)info.GetValue("unitDictionary", typeof(Hashtable));
     this.projectionType = (string)info.GetValue("projectionType", typeof(string));
     this.projectionDimension = (int)info.GetValue("projectionDimension", typeof(int));
     this.maxTrainingSpikesPerChannel = (int)info.GetValue("maxTrainingSpikesPerChannel", typeof(int));
     this.spikesCollectedPerChannel = (Hashtable)info.GetValue("spikesCollectedPerChannel", typeof(Hashtable));
     this.totalNumberOfUnits = (int)info.GetValue("totalNumberOfUnits", typeof(int));
     this.pValue = (double)info.GetValue("pValue", typeof(double));
     this.secondInflectionIndex = (int)info.GetValue("secondInflectionIndex", typeof(int));
     try
     {
         this.unit2Channel = (Dictionary<int, int>)info.GetValue("unit2Channel", typeof(Dictionary<int, int>));
     }
     catch
     {
     }
 }
Exemplo n.º 45
0
        //DateTime spkClk;
        private void bwSpikes_DoWork(object sender, DoWorkEventArgs e)
        {
            //spkClk = DateTime.Now;
            Object[] state = (Object[])e.Argument;
            int taskNumber = (int)state[0];
            //Debugger.Write(taskNumber.ToString() + ": dowork begin");
            trackingProc[taskNumber]++;
            //double[][] filtSpikeData;
            //Copy data into a new buffer
            for (int i = 0; i < numChannelsPerDev; ++i)
                spikeData[taskNumber][i].GetRawData(0, spikeBufferLength, filtSpikeData[taskNumber * numChannelsPerDev + i], 0);
            //Debugger.Write(taskNumber.ToString() + ": raw data read");

            // Increment the number of times the DAQ has been polled for spike data
            ++(numSpikeReads[taskNumber]);
            ulong startTime = (ulong)(numSpikeReads[taskNumber] - 1) * (ulong)spikeBufferLength; //Used to mark spike time for *.spk file

            //Account for Pre-amp gain
            double ampdec = (1 / Properties.Settings.Default.PreAmpGain);
            for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                for (int j = 0; j < spikeBufferLength; ++j)
                    filtSpikeData[i][j] = ampdec * filtSpikeData[i][j];

            // Send filtSpikeData to datSrv
            if (Properties.Settings.Default.useRawDataBuffer)
                datSrv.RawElectrodeSrv.WriteToBuffer(filtSpikeData, taskNumber, numChannelsPerDev);

            //  Debugger.Write(taskNumber.ToString() + ": raw data sent to rawsrv");
            #region Write RAW data
            //Write data to file

            if (switch_record.Value && recordingSettings.recordRaw && spikeTask != null)
            {

                lock (recordingSettings.rawOut)
                {

                    for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                    {
                        // Temporary storage for converte data
                        Int16[] tempBuff;

                        // Convert raw data to 16-bit int
                        tempBuff = neuralDataScaler.ConvertSoftRawRowToInt16(ref filtSpikeData[i]);

                        // Send data to file writer
                        for (int j = 0; j < spikeBufferLength; ++j)
                        {
                            recordingSettings.rawOut.read(tempBuff[j], i);
                        }
                    }
                }
            }

            #endregion

            // Debugger.Write(taskNumber.ToString() + ": raw written");

            #region Raw Spike Detection

            if (recordingSettings.recordRawSpike)
            {
                lock (rawSpikeObj)
                {
                    //newWaveforms: 0 based indexing for internal NR processing (datSrv, plotData)
                    EventBuffer<SpikeEvent> newWaveformsRaw = new EventBuffer<SpikeEvent>(Properties.Settings.Default.RawSampleFrequency);
                    switch (spikeDet.detectorType)
                    {
                        case 0:
                            for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                                newWaveformsRaw.Buffer.AddRange(spikeDet.spikeDetectorRaw.DetectSpikes(filtSpikeData[i], i, startTime));
                            break;
                        case 1:
                            for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                                newWaveformsRaw.Buffer.AddRange(spikeDet.spikeDetectorRaw.DetectSpikesSimple(filtSpikeData[i], i, startTime));
                            break;
                    }

                    //Extract waveforms, convert to 1 based indexing
                    EventBuffer<SpikeEvent> toRawsrvRaw = new EventBuffer<SpikeEvent>(spikeSamplingRate);
                    if (Properties.Settings.Default.ChannelMapping != "invitro" )//|| numChannels != 64) //check this first, so we don't have to check it for each spike
                    {
                        for (int j = 0; j < newWaveformsRaw.Buffer.Count; ++j) //For each threshold crossing
                        {
                            SpikeEvent tmp = (SpikeEvent)newWaveformsRaw.Buffer[j].DeepClone();
                            tmp.Channel = InVivoChannelMappings.Channel2LinearCR(tmp.Channel);
                            toRawsrvRaw.Buffer.Add(tmp);
                        }
                    }
                    else //in vitro mappings
                    {
                        for (int j = 0; j < newWaveformsRaw.Buffer.Count; ++j) //For each threshold crossing
                        {
                            SpikeEvent tmp = (SpikeEvent)newWaveformsRaw.Buffer[j].DeepClone();
                            tmp.Channel = MEAChannelMappings.channel2LinearCR(tmp.Channel);
                            toRawsrvRaw.Buffer.Add(tmp);
                        }
                    }

                    // Record spike waveforms
                    if (switch_record.Value)
                    {
                        for (int j = 0; j < newWaveformsRaw.Buffer.Count; ++j) //For each threshold crossing
                        {
                            SpikeEvent tmp = toRawsrvRaw.Buffer[j];
                            lock (recordingSettings.spkOutRaw) //Lock so another NI card doesn't try writing at the same time
                            {
                                recordingSettings.spkOutRaw.WriteSpikeToFile((short)tmp.Channel, (int)tmp.SampleIndex,
                                    tmp.Threshold, tmp.Waveform, tmp.Unit);
                            }
                        }
                    }

                }

            }

                #endregion

            // Debugger.Write(taskNumber.ToString() + ": raw spikes filtered");

            #region LFP_Filtering
            //Filter for LFPs
            if (!Properties.Settings.Default.SeparateLFPBoard && Properties.Settings.Default.UseLFPs)
            {
                //Copy to new array
                for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                    for (int j = 0; j < spikeBufferLength; ++j)
                        filtLFPData[i][j] = filtSpikeData[i][j];

                #region ArtiFilt (interpolation filtering)
                if (checkBox_artiFilt.Checked)
                    artiFilt.filter(ref filtLFPData, stimIndices, taskNumber * numChannelsPerDev, numChannelsPerDev, numStimReads[taskNumber] - 1);
                #endregion

                if (checkBox_LFPsFilter.Checked)
                    for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                        lfpFilter[i].filterData(filtLFPData[i]);

                //Downsample for LFPs
                double dsFactor = (double)spikeSamplingRate / (double)lfpSamplingRate;
                if (dsFactor % 1 == 0) //If it's an integer
                {
                    for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                        for (int j = 0; j < lfpBufferLength; ++j)
                            finalLFPData[i][j] = filtLFPData[i][(int)(dsFactor * j)];
                }
                else
                {
                    for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                        for (int j = 0; j < lfpBufferLength; ++j)
                            finalLFPData[i][j] = filtLFPData[i][(int)(Math.Round(dsFactor * j))];
                }

                //Do IISZapper stuff
                if (IISDetected != null) IISDetected(this, finalLFPData, numSpikeReads[taskNumber]);

                // Send to datSrv
                if (Properties.Settings.Default.useLFPDataBuffer)
                    datSrv.LFPSrv.WriteToBuffer(finalLFPData, 0, numChannels);

                #region WriteLFPFile
                if (switch_record.Value && recordingSettings.recordLFP && spikeTask != null) //Convert to 16-bit ints, then write to file
                {
                    for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                    {
                        // Temporary storage for converte data
                        Int16[] tempLFPBuff;

                        // Convert raw data to 16-bit int
                        tempLFPBuff = neuralDataScaler.ConvertSoftRawRowToInt16(ref finalLFPData[i]);

                        // Send data to file writer
                        for (int j = 0; j < lfpBufferLength; ++j)
                        {
                            recordingSettings.lfpOut.read((short)tempLFPBuff[j], i);
                        }
                    }

                }
                #endregion

                //Digital ref LFP signals
                if (!checkBox_digRefLFPs.Checked) { /* Do nothing, since prefetch makes if faster than else */ }
                else
                {
                    int refChan = Convert.ToInt16(numericUpDown_digRefLFPs.Value) - 1;
                    for (int i = 0; i < refChan; ++i)
                        for (int j = 0; j < lfpBufferLength; ++j)
                            finalLFPData[i][j] -= finalLFPData[refChan][j];
                    for (int i = refChan + 1; i < numChannels; ++i)
                        for (int j = 0; j < lfpBufferLength; ++j)
                            finalLFPData[i][j] -= finalLFPData[refChan][j];
                }

                //Post to PlotData buffer
                lfpPlotData.write(finalLFPData, taskNumber * numChannelsPerDev, numChannelsPerDev);
            }
            #endregion

            // Debugger.Write(taskNumber.ToString() + ": lfp filtered");

            #region SALPA Filtering
            lock (stimIndices)
                if (checkBox_SALPA.Checked && numStimReads == null) //Account for those not using the stimulator and stimulus coding scheme
                {
                    SALPAFilter.filter(ref filtSpikeData, taskNumber * numChannelsPerDev, numChannelsPerDev, stimIndices, 0);

                    // Send filtSpikeData to datSrv
                    if (Properties.Settings.Default.useSALPADataBuffer)
                        datSrv.SalpaElectrodeSrv.WriteToBuffer(filtSpikeData, taskNumber, numChannelsPerDev);

                }
                else if (checkBox_SALPA.Checked)
                {
                    SALPAFilter.filter(ref filtSpikeData, taskNumber * numChannelsPerDev, numChannelsPerDev, stimIndices, numStimReads[taskNumber] - 1);

                    // Send filtSpikeData to datSrv
                    if (Properties.Settings.Default.useSALPADataBuffer)
                        datSrv.SalpaElectrodeSrv.WriteToBuffer(filtSpikeData, taskNumber, numChannelsPerDev);

                }

            if (switch_record.Value && recordingSettings.recordSALPA && spikeTask != null)
            {
                lock (recordingSettings.salpaOut)
                {
                    int startIdx;

                    if (firstRawWrite[taskNumber]) // account for SALPA delay
                    {
                        if (!recordingSettings.recordSpikeFilt)
                            firstRawWrite[taskNumber] = false;
                        startIdx = SALPAFilter.offset();
                    }
                    else
                    {
                        startIdx = 0;
                    }

                    for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                    {
                        // Temporary storage for converte data
                        Int16[] tempBuff;

                        // Convert raw data to 16-bit int
                        tempBuff = neuralDataScaler.ConvertSoftRawRowToInt16(ref filtSpikeData[i]);

                        // Send data to file writer
                        for (int j = startIdx; j < spikeBufferLength; ++j)
                        {
                            recordingSettings.salpaOut.read((short)tempBuff[j], i);
                        }
                    }
                }
            }

            #endregion SALPA Filtering

            //  Debugger.Write(taskNumber.ToString() + ": salpa filtered");

            #region SALPA Spike Detection
            if (recordingSettings.recordSalpaSpike)
            {
                object salpaSpike = new object();

                lock (salpaSpikeObj)
                {

                    //newWaveforms: 0 based indexing for internal NR processing (datSrv, plotData)
                    EventBuffer<SpikeEvent> newWaveformsSalpa = new EventBuffer<SpikeEvent>(Properties.Settings.Default.RawSampleFrequency);
                    switch (spikeDet.detectorType)
                    {
                        case 0:
                            for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                                newWaveformsSalpa.Buffer.AddRange(spikeDet.spikeDetectorSalpa.DetectSpikes(filtSpikeData[i], i, startTime));
                            break;
                        case 1:
                            for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                                newWaveformsSalpa.Buffer.AddRange(spikeDet.spikeDetectorSalpa.DetectSpikesSimple(filtSpikeData[i], i, startTime));
                            break;
                    }

                    //Extract waveforms, convert to 1 based indexing
                    EventBuffer<SpikeEvent> toRawsrvSalpa = new EventBuffer<SpikeEvent>(spikeSamplingRate);
                    if (Properties.Settings.Default.ChannelMapping != "invitro" || numChannels != 64) //check this first, so we don't have to check it for each spike
                    {
                        for (int j = 0; j < newWaveformsSalpa.Buffer.Count; ++j) //For each threshold crossing
                        {
                            SpikeEvent tmp = (SpikeEvent)newWaveformsSalpa.Buffer[j].DeepClone();

                            if (checkBox_SALPA.Checked)
                                if (tmp.SampleIndex >= (ulong)SALPAFilter.offset())
                                    tmp.SampleIndex -= (ulong)SALPAFilter.offset(); //To account for delay of SALPA filter
                                else
                                    continue; //skip that one

                            tmp.Channel = InVivoChannelMappings.Channel2LinearCR(tmp.Channel);
                            toRawsrvSalpa.Buffer.Add(tmp);
                        }
                    }
                    else //in vitro mappings
                    {
                        for (int j = 0; j < newWaveformsSalpa.Buffer.Count; ++j) //For each threshold crossing
                        {
                            SpikeEvent tmp = (SpikeEvent)newWaveformsSalpa.Buffer[j].DeepClone();

                            if (checkBox_SALPA.Checked)
                                if (tmp.SampleIndex >= (ulong)SALPAFilter.offset() )
                                    tmp.SampleIndex -= (ulong)SALPAFilter.offset(); //To account for delay of SALPA filter
                                else
                                    continue; //skip that one

                            tmp.Channel = MEAChannelMappings.channel2LinearCR(tmp.Channel);
                            toRawsrvSalpa.Buffer.Add(tmp);
                        }
                    }

                    // Record spike waveforms
                    if (switch_record.Value)
                    {
                        for (int j = 0; j < newWaveformsSalpa.Buffer.Count; ++j) //For each threshold crossing
                        {
                            SpikeEvent tmp = toRawsrvSalpa.Buffer[j];

                            lock (recordingSettings.spkOutSalpa) //Lock so another NI card doesn't try writing at the same time
                            {
                                recordingSettings.spkOutSalpa.WriteSpikeToFile((short)tmp.Channel, (int)tmp.SampleIndex,
                                    tmp.Threshold, tmp.Waveform, tmp.Unit);
                            }
                        }
                    }

                }
            }
            #endregion

            // Debugger.Write(taskNumber.ToString() + ": salpa spikes filtered");

            #region Band Pass Filtering
            //Filter spike data
            if (checkBox_spikesFilter.Checked)
            {

                // Filter data
                for (int i = numChannelsPerDev * taskNumber; i < numChannelsPerDev * (taskNumber + 1); ++i)
                    spikeFilter[i].filterData(filtSpikeData[i]);

                // Send filtSpikeData to datSrv
                if (Properties.Settings.Default.useSALPADataBuffer)
                    datSrv.SpikeBandSrv.WriteToBuffer(filtSpikeData, taskNumber, numChannelsPerDev);

                if (switch_record.Value && recordingSettings.recordSpikeFilt && (spikeTask != null))
                {
                    lock (recordingSettings.spkFiltOut)
                    {

                        int startIdx;
                        if (firstRawWrite[taskNumber] && checkBox_SALPA.Checked) // account for SALPA delay
                        {
                            firstRawWrite[taskNumber] = false;
                            startIdx = SALPAFilter.offset();
                        }
                        else
                        {
                            startIdx = 0;
                        }

                        for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                        {
                            // Temporary storage for converte data
                            Int16[] tempBuff;

                            // Convert raw data to 16-bit int
                            tempBuff = neuralDataScaler.ConvertSoftRawRowToInt16(ref filtSpikeData[i]);

                            // Send data to file writer
                            for (int j = startIdx; j < spikeBufferLength; ++j)
                            {
                                recordingSettings.spkFiltOut.read((short)tempBuff[j], i);
                            }
                        }
                    }
                }

            }
            #endregion

            //  Debugger.Write(taskNumber.ToString() + ": spike filtered");

            //NEED TO FIX FOR MULTI DEVS
            #region Digital_Referencing_Spikes

            //Digital ref spikes signals
            if (checkBox_digRefSpikes.Checked)
            {
                int refChan = Convert.ToInt16(numericUpDown_digRefSpikes.Value) - 1;
                for (int i = 0; i < refChan; ++i)
                    for (int j = 0; j < spikeBufferLength; ++j)
                        filtSpikeData[i][j] -= filtSpikeData[refChan][j];
                for (int i = refChan + 1; i < numChannels; ++i)
                    for (int j = 0; j < spikeBufferLength; ++j)
                        filtSpikeData[i][j] -= filtSpikeData[refChan][j];
            }

            //Common average or median referencing
            if (referncer != null)
            {
                lock (referncer)
                    referncer.reference(filtSpikeData, taskNumber * numChannelsPerDev, numChannelsPerDev);
            }
            #endregion

            //   Debugger.Write(taskNumber.ToString() + ": digital referencing spikes");

            lock (finalSpikeObj)
            {
                #region Final Spike Detection

                //newWaveforms: 0 based indexing for internal NR processing (datSrv, plotData)
                EventBuffer<SpikeEvent> newWaveforms = new EventBuffer<SpikeEvent>(Properties.Settings.Default.RawSampleFrequency);
                switch (spikeDet.detectorType)
                {
                    case 0:
                        for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                            newWaveforms.Buffer.AddRange(spikeDet.spikeDetector.DetectSpikes(filtSpikeData[i], i, startTime));
                        break;
                    case 1:
                        for (int i = taskNumber * numChannelsPerDev; i < (taskNumber + 1) * numChannelsPerDev; ++i)
                            newWaveforms.Buffer.AddRange(spikeDet.spikeDetector.DetectSpikesSimple(filtSpikeData[i], i, startTime));
                        break;
                }

                //Extract waveforms, convert to 1 based indexing
                EventBuffer<SpikeEvent> toRawsrv = new EventBuffer<SpikeEvent>(spikeSamplingRate);
                if (Properties.Settings.Default.ChannelMapping != "invitro" || numChannels != 64) //check this first, so we don't have to check it for each spike
                {
                    for (int j = 0; j < newWaveforms.Buffer.Count; ++j) //For each threshold crossing
                    {
                        SpikeEvent tmp = (SpikeEvent)newWaveforms.Buffer[j].DeepClone();

                        if (checkBox_SALPA.Checked)
                            if (tmp.SampleIndex >= (ulong)SALPAFilter.offset())
                                tmp.SampleIndex -= (ulong)SALPAFilter.offset(); //To account for delay of SALPA filter
                            else
                                continue; //skip that one

                        tmp.Channel = InVivoChannelMappings.Channel2LinearCR(tmp.Channel);
                        toRawsrv.Buffer.Add(tmp);
                    }
                }
                else //in vitro mappings
                {
                    for (int j = 0; j < newWaveforms.Buffer.Count; ++j) //For each threshold crossing
                    {
                        SpikeEvent tmp = (SpikeEvent)newWaveforms.Buffer[j].DeepClone();

                        if (checkBox_SALPA.Checked)
                            if (tmp.SampleIndex >= (ulong)SALPAFilter.offset() )
                                tmp.SampleIndex -= (ulong)SALPAFilter.offset(); //To account for delay of SALPA filter
                            else
                                continue; //skip that one

                        tmp.Channel = MEAChannelMappings.channel2LinearCR(tmp.Channel);
                        toRawsrv.Buffer.Add(tmp);
                    }
                }

                #endregion

                //  Debugger.Write(taskNumber.ToString() + ": spikes detected");

                # region Spike Sorting
                // Spike Sorting - Hoarding
                if (spikeDet.IsHoarding)
                {
                    // Send spikes to the sorter's internal buffer
                    spikeDet.spikeSorter.HoardSpikes(toRawsrv);
                    spikeDet.UpdateCollectionBar();
                }

                // Spike Detection - Classification
                if (spikeDet.IsEngaged)
                {
                    spikeDet.spikeSorter.Classify(ref toRawsrv);
                }

                // Provide new spike data to persistent buffer
                if (Properties.Settings.Default.useSpikeDataBuffer)
                {
                    datSrv.SpikeSrv.WriteToBuffer(toRawsrv, taskNumber);
                }

                // Record spike waveforms
                if (switch_record.Value && Properties.Settings.Default.recordSpikes)
                {
                    for (int j = 0; j < toRawsrv.Buffer.Count; ++j) //For each threshold crossing
                    {

                        SpikeEvent tmp = toRawsrv.Buffer[j];

                        lock (recordingSettings.spkOut) //Lock so another NI card doesn't try writing at the same time
                        {
                            recordingSettings.spkOut.WriteSpikeToFile((short)tmp.Channel, (int)tmp.SampleIndex,
                                tmp.Threshold, tmp.Waveform, tmp.Unit);
                        }
                    }
                }
                # endregion

                //  Debugger.Write(taskNumber.ToString() + ": spikes sorted");

                # region Spike Plotting
                //Post to PlotData
                if (spikeDet.IsEngaged)
                {
                    waveformPlotData.write(toRawsrv.Buffer, spikeDet.spikeSorter.unitDictionary);
                }
                else
                {
                    waveformPlotData.write(toRawsrv.Buffer, null);
                }
                //Clear new ones, since we're done with them.
                newWaveforms.Buffer.Clear();

                # endregion

                //  Debugger.Write(taskNumber.ToString() + ": spikes plotted");
            }

            #region BNC_Output
            //Send selected channel to BNC
            if (Properties.Settings.Default.UseSingleChannelPlayback)
            {
                int ch = (int)(channelOut.Value) - 1;
                if (ch >= numChannelsPerDev * taskNumber && ch < numChannelsPerDev * (taskNumber + 1))
                    //spikeOutWriter.BeginWriteMultiSample(true, filtSpikeData[ch], null, null);
                    BNCOutput.write(filtSpikeData[ch]);
                //spikeOutWriter.WriteMultiSample(true, filtSpikeData[ch - 1]);
            }
            #endregion

            //   Debugger.Write(taskNumber.ToString() + ": bnc output");
            //Write to PlotData buffer
            spikePlotData.write(filtSpikeData, taskNumber * numChannelsPerDev, numChannelsPerDev);
            //   Debugger.Write(taskNumber.ToString() + ": spikes plotted");

            #region MUA
            if (Properties.Settings.Default.ProcessMUA)
            {
                muaFilter.Filter(filtSpikeData, taskNumber * numChannelsPerDev, numChannelsPerDev, ref muaData, checkBox_MUAFilter.Checked);

                //Write to plot buffer
                muaPlotData.write(muaData, taskNumber * numChannelsPerDev, numChannelsPerDev);
            }
            #endregion

            //    Debugger.Write(taskNumber.ToString() + ": multi unit activity done/processing done");
            e.Result = taskNumber;

            //Console.WriteLine(DateTime.Now.Subtract(spkClk).TotalMilliseconds.ToString());
        }
Exemplo n.º 46
0
        /// <summary>
        /// After the channel models (gmm's) have been created and trained, this function
        /// is used to classifty newly detected spikes for which a valide channel model exisits.
        /// </summary>
        /// <param name="newSpikes"> An EventBuffer conataining spikes to be classified</param>
        public void Classify(ref EventBuffer<SpikeEvent> newSpikes, ref List<double[]> projections2D)
        {
            // Make sure the channel models are trained.
            if (!trained)
            {
                throw new InvalidOperationException("The channel models were not yet trained so classification is not possible.");
            }

            // Sort the channels that need sorting
            for (int i = 0; i < channelsToSort.Count; ++i)
            {
                // Current channel
                int currentChannel = channelsToSort[i];

                // Get the spikes that belong to this channel
                List<SpikeEvent> spikesOnChan = newSpikes.Buffer.Where(x => x.Channel == currentChannel).ToList();

                // If there are no spikes on this channel
                if (spikesOnChan.Count == 0)
                    continue;

                // Get the channel model for this channel
                ChannelModel thisChannelModel = channelModels[channelsToSort.IndexOf(currentChannel)];

                // Project the spikes
                if (this.projectionType == "Maximum Voltage Inflection")
                    thisChannelModel.MaxInflectProject(spikesOnChan.ToList(), inflectionSample);
                else if (this.projectionType == "PCA")
                    thisChannelModel.PCProject(spikesOnChan.ToList());
                else if (this.projectionType == "Haar Wavelet")
                    thisChannelModel.HaarProject(spikesOnChan.ToList());

                // Get the projection
                projections2D = thisChannelModel.Return2DProjection();

                // Sort the spikes
                thisChannelModel.ClassifyThresh();

                // Update the newSpikes buffer
                for (int j = 0; j < spikesOnChan.Count; ++j)
                {
                    if (thisChannelModel.classes[j] < 0)
                        spikesOnChan[j].SetUnit((Int16)0);
                    else
                        spikesOnChan[j].SetUnit((Int16)(thisChannelModel.classes[j] + thisChannelModel.unitStartIndex + 1));
                }

            }
        }
Exemplo n.º 47
0
 private TollDataGenerator(Random r)
 {
     random = r;
     eventBuffer = new EventBuffer();
     commercialVehicleRegistration = JsonConvert.DeserializeObject<Registration[]>(File.ReadAllText(@"Data\Registration.json"));
 }