コード例 #1
0
        public override IObservable <NeuropixDataFrame> Generate()
        {
            return(Observable.Create <NeuropixDataFrame>((observer, cancellationToken) =>
            {
                return Task.Factory.StartNew(() =>
                {
                    var stopwatch = new Stopwatch();
                    using (var basestation = new NeuropixBasestation())
                        using (var sampleSignal = new ManualResetEvent(false))
                        {
                            basestation.Open(Path);
                            try
                            {
                                basestation.Mode = AsicMode.Recording;
                                basestation.DataMode = true;

                                var packets = new ElectrodePacket[BufferSize];
                                for (int i = 0; i < packets.Length; i++)
                                {
                                    packets[i] = new ElectrodePacket();
                                }

                                while (!cancellationToken.IsCancellationRequested)
                                {
                                    stopwatch.Restart();
                                    for (int i = 0; i < packets.Length; i++)
                                    {
                                        if (!basestation.ReadElectrodeData(packets[i]))
                                        {
                                            observer.OnCompleted();
                                            return;
                                        }
                                    }
                                    var result = new NeuropixDataFrame(packets, 0);
                                    observer.OnNext(result);

                                    var frequency = Frequency;
                                    var sampleInterval = frequency > 0 ? 1000.0 / Frequency : 0;
                                    var dueTime = Math.Max(0, (sampleInterval * packets.Length * NeuropixDataFrame.SampleCount) - stopwatch.Elapsed.TotalMilliseconds);
                                    if (dueTime > 0)
                                    {
                                        sampleSignal.WaitOne(TimeSpan.FromMilliseconds(dueTime));
                                    }
                                }
                            }
                            finally { basestation.Close(); }
                        }
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default);
            }));
        }
コード例 #2
0
ファイル: Probe.cs プロジェクト: medengineer/bonsai.neuropix
        public override IObservable <NeuropixDataFrame> Generate()
        {
            return(Observable.Create <NeuropixDataFrame>((observer, cancellationToken) =>
            {
                return Task.Factory.StartNew(() =>
                {
                    try
                    {
                        int rc = NeuropixAPI.openBS(3);
                        Console.WriteLine("Open basestation result: {0}", rc);
                        rc = NeuropixAPI.openProbe(3, 1);
                        Console.WriteLine("Open probe result: {0}", rc);
                        if (rc == 0)
                        {
                            rc = NeuropixAPI.init(3, 1);
                            Console.WriteLine("Init result: {0}", rc);
                            Console.WriteLine("Sleeping for {0} seconds", 2);
                            rc = NeuropixAPI.setTriggerInput(3, 0);
                            rc = NeuropixAPI.arm(3);
                            rc = NeuropixAPI.setSWTrigger(3);
                            int packetCounter = 0;
                            float bufferCapacity = 0;
                            var packets = new ElectrodePacket[1];
                            IntPtr requestedAmount = (IntPtr)packets.Length;
                            IntPtr actualAmount;

                            while (!cancellationToken.IsCancellationRequested)
                            {
                                rc = NeuropixAPI.readElectrodeData(3, 1, packets, out actualAmount, requestedAmount);
                                Console.WriteLine("Received: {0} samples", actualAmount);
                                //observer.OnNext(new NeuropixDataFrame(packets, 0));
                                for (int i = 0; i < actualAmount.ToInt32(); i++)
                                {
                                    observer.OnNext(new NeuropixDataFrame(packets[i], 0));
                                }
                            }
                            Console.WriteLine("Closing basestation...");
                            rc = NeuropixAPI.closeBS(3);
                            Console.WriteLine("CloseBS result: {0}", rc);
                        }
                    }
                    catch (System.Runtime.InteropServices.SEHException ex)
                    {
                        Console.WriteLine(ex);
                        throw;
                    }
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default);
            }));
        }
コード例 #3
0
        internal NeuropixDataFrame(ElectrodePacket packet, float bufferCapacity)
        {
            unsafe
            {
                var apData = new Mat(ElectrodePacket.PROBE_SUPERFRAMESIZE, ElectrodePacket.PROBE_CHANNEL_COUNT, Depth.S16, 1, (IntPtr)packet.apData);
                ApData = new Mat(ElectrodePacket.PROBE_CHANNEL_COUNT, ElectrodePacket.PROBE_SUPERFRAMESIZE, Depth.S16, 1);
                CV.Transpose(apData, ApData);

                var lfpData = new Mat(1, ElectrodePacket.PROBE_CHANNEL_COUNT, Depth.S16, 1, (IntPtr)packet.lfpData);
                LfpData = new Mat(ElectrodePacket.PROBE_CHANNEL_COUNT, 1, Depth.S16, 1);
                CV.Transpose(lfpData, LfpData);
            }
        }
コード例 #4
0
        public NeuropixDataFrame(ElectrodePacket packet, float bufferCapacity)
        {
            var startTrigger    = new Mat(1, SampleCount, Depth.U8, 1, packet.StartTrigger);
            var synchronization = new Mat(1, SampleCount, Depth.U16, 1, packet.Synchronization);
            var counters        = new Mat(SampleCount, SampleCount + 1, Depth.S32, 1, packet.Counters);
            var lfpData         = new Mat(ChannelCount, 1, Depth.F32, 1, packet.LfpData);
            var apData          = new Mat(SampleCount, ChannelCount, Depth.F32, 1, packet.ApData);

            StartTrigger    = startTrigger.Clone();
            Synchronization = synchronization.Clone();
            Counters        = Transpose(counters);
            LfpData         = lfpData.Clone();
            ApData          = Transpose(apData);
            BufferCapacity  = bufferCapacity;
        }
コード例 #5
0
        public override IObservable <NeuropixDataFrame> Generate()
        {
            return(Observable.Create <NeuropixDataFrame>((observer, cancellationToken) =>
            {
                return Task.Factory.StartNew(() =>
                {
                    try
                    {
                        using (var basestation = new NeuropixBasestation())
                        {
                            var version = basestation.GetAPIVersion();
                            Console.WriteLine("Neuropix API version: {0}.{1}", version.Major, version.Minor);
                            Console.WriteLine("Opening basestation data and configuration link...");
                            basestation.Open();

                            version = basestation.GetHardwareVersion();
                            Console.WriteLine("Neuropix FPGA bootcode version: {0}.{1}", version.Major, version.Minor);

                            version = basestation.GetBSVersion();
                            Console.WriteLine("Neuropix Basestation connect board version: {0}.{1}", version.Major, version.Minor);

                            var probeID = basestation.ReadID();
                            Console.WriteLine("Neuropix probe S/N: {0} Option {1}", probeID.SerialNumber, probeID.ProbeType);

                            Console.Write("Neuropix ADC calibration... ");
                            var comparatorCalibration = ComparatorCalibration;
                            var adcOffsetCalibration = AdcOffsetCalibration;
                            var adcSlopeCalibration = AdcSlopeCalibration;
                            if (!string.IsNullOrEmpty(comparatorCalibration) &&
                                !string.IsNullOrEmpty(adcOffsetCalibration) &&
                                !string.IsNullOrEmpty(adcSlopeCalibration))
                            {
                                basestation.ApplyAdcCalibrationFromCsv(comparatorCalibration, adcOffsetCalibration, adcSlopeCalibration);
                            }
                            else
                            {
                                basestation.ApplyAdcCalibrationFromEeprom();
                            }
                            Console.WriteLine("OK");

                            Console.WriteLine("Neuropix setting gain and filter bandwidth... ");
                            basestation.WriteAllAPGains(GainAP);
                            basestation.WriteAllLFPGains(GainLFP);
                            basestation.SetFilter(FilterBandwidth);

                            Console.Write("Neuropix Gain correction... ");
                            var gainCorrection = GainCorrection;
                            if (!string.IsNullOrEmpty(gainCorrection))
                            {
                                basestation.ApplyGainCalibrationFromCsv(gainCorrection);
                            }
                            //else basestation.ApplyGainCalibrationFromEeprom();
                            Console.WriteLine("OK");

                            basestation.LedOff(LedOff);
                            basestation.Mode = AsicMode.Recording;
                            basestation.DataMode = true;
                            basestation.TriggerMode = false;
                            basestation.SetNrst(false);
                            basestation.ResetDatapath();
                            var fileName = StreamingFileName;
                            if (!string.IsNullOrEmpty(fileName) && FileStreaming)
                            {
                                PathHelper.EnsureDirectory(fileName);
                                fileName = PathHelper.AppendSuffix(fileName, StreamingSuffix);
                                basestation.StartRecording(fileName);
                            }
                            Console.WriteLine("Neuropix recording armed.");

                            basestation.SetNrst(true);
                            basestation.NeuralStart();
                            Console.WriteLine("Neuropix recording start...");

                            int packetCounter = 0;
                            float bufferCapacity = 0;
                            var packet = new ElectrodePacket();
                            while (!cancellationToken.IsCancellationRequested)
                            {
                                if (packetCounter == 0)
                                {
                                    bufferCapacity = basestation.FifoFilling;
                                }
                                basestation.ReadElectrodeData(packet);
                                var result = new NeuropixDataFrame(packet, bufferCapacity);
                                observer.OnNext(result);
                                packetCounter = (packetCounter + 1) % 50;
                            }

                            Console.WriteLine("Neuropix stop recording...");
                            if (!string.IsNullOrEmpty(fileName) && FileStreaming)
                            {
                                basestation.StopRecording();
                            }
                            basestation.Close();
                        }
                    }
                    catch (System.Runtime.InteropServices.SEHException ex)
                    {
                        Console.WriteLine(ex);
                        throw;
                    }
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default);
            }));
        }