Ejemplo n.º 1
0
        private void PreloadStreams()
        {
            IDictionary <ChannelIdentifier, short[]> output = new Dictionary <ChannelIdentifier, short[]>();

            foreach (var s in ActiveOutputStreams.Cast <HekaDAQOutputStream>())
            {
                s.Reset();
                var outputSamples = new List <short>();
                while (TimeSpanExtensions.FromSamples((uint)outputSamples.Count(), s.SampleRate).TotalSeconds < PRELOAD_DURATION_SECONDS) // && s.HasMoreData
                {
                    var nextOutputDataForStream = NextOutputDataForStream(s);
                    var nextSamples             = nextOutputDataForStream.DataWithUnits(HekaDAQOutputStream.DAQCountUnits).Data.
                                                  Select(
                        (m) => (short)m.QuantityInBaseUnit);

                    outputSamples = outputSamples.Concat(nextSamples).ToList();
                }

                if (!outputSamples.Any())
                {
                    throw new HekaDAQException("Unable to pull data to preload stream " + s.Name);
                }


                output[new ChannelIdentifier {
                           ChannelNumber = s.ChannelNumber, ChannelType = (ushort)s.ChannelType
                       }] =
                    outputSamples.ToArray();
            }

            Device.Preload(output);
        }
Ejemplo n.º 2
0
        public void LongEpochPersistence(
            [Values(5, 60)] double epochDuration, //seconds
            [Values(2)] int nEpochs
            )
        {
            const decimal sampleRate = 10000m;

            const string h5Path = "..\\..\\..\\LongEpochPersistence.h5";

            if (File.Exists(h5Path))
            {
                File.Delete(h5Path);
            }

            Logging.ConfigureConsole();

            Converters.Clear();
            Converters.Register("V", "V",
                                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));

            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                try
                {
                    daq.InitHardware();
                    daq.SampleRate = new Measurement(sampleRate, "Hz");

                    var controller = new Controller {
                        Clock = daq, DAQController = daq
                    };

                    var dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller,
                                                                new Measurement(0, "V"))
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev0.BindStream((IDAQOutputStream)daq.GetStreams("ANALOG_OUT.0").First());
                    dev0.BindStream((IDAQInputStream)daq.GetStreams("ANALOG_IN.0").First());


                    for (int j = 0; j < nEpochs; j++)
                    {
                        // Setup Epoch
                        var e = new Epoch("HekaIntegration");

                        var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(daq.SampleRate);
                        IList <IMeasurement> stimData = (IList <IMeasurement>)Enumerable.Range(0, nSamples)
                                                        .Select(
                            i =>
                            new Measurement(
                                (decimal)
                                (8 *
                                 Math.Sin(((double)i) /
                                          (nSamples / 10.0))),
                                "V") as IMeasurement)
                                                        .ToList();

                        e.Stimuli[dev0] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                               (IOutputData) new OutputData(stimData, daq.SampleRate));
                        e.Responses[dev0]  = new Response();
                        e.Background[dev0] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);



                        //Run single epoch
                        using (var persistor = new EpochHDF5Persistor(h5Path, null, 9))
                        {
                            persistor.BeginEpochGroup("label", "source", new string[0], new Dictionary <string, object>(),
                                                      Guid.NewGuid(), DateTimeOffset.Now);

                            controller.RunEpoch(e, persistor);

                            persistor.EndEpochGroup();
                        }


                        Assert.That((bool)e.StartTime, Is.True);
                        Assert.That((DateTimeOffset)e.StartTime, Is.LessThanOrEqualTo(controller.Clock.Now));
                        Assert.That(e.Responses[dev0].Duration, Is.EqualTo(((TimeSpan)e.Duration))
                                    .Within(TimeSpanExtensions.FromSamples(1,
                                                                           daq.
                                                                           SampleRate)));
                        //Assert.That(e.Responses[dev1].Duration, Is.EqualTo(((TimeSpan) e.Duration))
                        //                                            .Within(TimeSpanExtensions.FromSamples(1,
                        //                                                                                   daq.
                        //                                                                                       SampleRate)));
                    }
                }
                finally
                {
                    if (File.Exists(h5Path))
                    {
                        File.Delete(h5Path);
                    }

                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void RenderedStimulus(
            [Values(10000, 20000, 50000)] double sampleRate,
            [Values(2)] int nEpochs
            )
        {
            Logging.ConfigureConsole();

            Converters.Clear();
            Converters.Register("V", "V",
                                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);
            HekaDAQInputStream.RegisterConverters();
            HekaDAQOutputStream.RegisterConverters();

            Assert.That(HekaDAQController.AvailableControllers().Count(), Is.GreaterThan(0));
            foreach (var daq in HekaDAQController.AvailableControllers())
            {
                const double epochDuration = 5; //s

                daq.InitHardware();
                try
                {
                    daq.SampleRate = new Measurement((decimal)sampleRate, "Hz");

                    var controller = new Controller {
                        Clock = daq, DAQController = daq
                    };

                    var dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller, new Measurement(0, "V"))
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev0.BindStream((IDAQOutputStream)daq.GetStreams("ANALOG_OUT.0").First());
                    dev0.BindStream((IDAQInputStream)daq.GetStreams("ANALOG_IN.0").First());

                    var dev1 = new UnitConvertingExternalDevice("Device1", "Manufacturer", controller, new Measurement(0, "V"))
                    {
                        MeasurementConversionTarget = "V",
                        Clock = daq
                    };
                    dev1.BindStream((IDAQOutputStream)daq.GetStreams("ANALOG_OUT.1").First());
                    dev1.BindStream((IDAQInputStream)daq.GetStreams("ANALOG_IN.1").First());

                    for (int j = 0; j < nEpochs; j++)
                    {
                        // Setup Epoch
                        var e = new Epoch("HekaIntegration");

                        var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(daq.SampleRate);
                        IList <IMeasurement> stimData = (IList <IMeasurement>)Enumerable.Range(0, nSamples)
                                                        .Select(i => new Measurement((decimal)(8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V") as IMeasurement)
                                                        .ToList();

                        e.Stimuli[dev0] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                               (IOutputData) new OutputData(stimData, daq.SampleRate));
                        e.Responses[dev0]  = new Response();
                        e.Background[dev0] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);

                        e.Stimuli[dev1] = new RenderedStimulus((string)"RenderedStimulus", (IDictionary <string, object>) new Dictionary <string, object>(),
                                                               (IOutputData) new OutputData(Enumerable.Range(0, nSamples)
                                                                                            .Select(i => new Measurement((decimal)(8 * Math.Sin(((double)i) / (nSamples / 10.0))), "V"))
                                                                                            .ToList(),
                                                                                            daq.SampleRate));
                        e.Responses[dev1]  = new Response();
                        e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), daq.SampleRate);


                        //Run single epoch
                        var fakeEpochPersistor = new FakeEpochPersistor();

                        controller.RunEpoch(e, fakeEpochPersistor);



                        Assert.That((bool)e.StartTime, Is.True);
                        Assert.That((DateTimeOffset)e.StartTime, Is.LessThanOrEqualTo(controller.Clock.Now));
                        Assert.That(e.Responses[dev0].Duration, Is.EqualTo(((TimeSpan)e.Duration))
                                    .Within(TimeSpanExtensions.FromSamples(1,
                                                                           daq.
                                                                           SampleRate)));
                        Assert.That(e.Responses[dev1].Duration, Is.EqualTo(((TimeSpan)e.Duration))
                                    .Within(TimeSpanExtensions.FromSamples(1,
                                                                           daq.
                                                                           SampleRate)));
                        Assert.That(fakeEpochPersistor.PersistedEpochs, Contains.Item(e));

                        var failures0 =
                            e.Responses[dev0].Data.Select(
                                (t, i) => new { index = i, diff = t.QuantityInBaseUnit - stimData[i].QuantityInBaseUnit })
                            .Where(dif => Math.Abs(dif.diff) > (decimal)MAX_VOLTAGE_DIFF);

                        foreach (var failure in failures0.Take(10))
                        {
                            Console.WriteLine("{0}: {1}", failure.index, failure.diff);
                        }


                        /*
                         * According to Telly @ Heka, a patch cable may introduce 3-4 offset points
                         */
                        Assert.That(failures0.Count(), Is.LessThanOrEqualTo(4));


                        /*
                         * //Since we only have one patch cable on the test rig,
                         * //we're not checking second device response values
                         *
                         * var failures1 =
                         *  e.Responses[dev1].Data.Data.Select(
                         *      (t, i) => new { index = i, diff = t.QuantityInBaseUnit - stimData[i].QuantityInBaseUnit })
                         *      .Where(dif => Math.Abs(dif.diff) > MAX_VOLTAGE_DIFF);
                         *
                         * foreach (var failure in failures1.Take(10))
                         *  Console.WriteLine("{0}: {1}", failure.index, failure.diff);
                         *
                         * Assert.That(failures1.Count(), Is.EqualTo(0));
                         */
                    }
                }
                finally
                {
                    if (daq.HardwareReady)
                    {
                        daq.CloseHardware();
                    }
                }
            }
        }