コード例 #1
0
        public void AppendToExistingHDF5()
        {
            if (File.Exists("AppendToExistingHDF5.h5"))
                File.Delete("AppendToExistingHDF5.h5");

            Epoch e1;
            Epoch e2;

            using (var persistor = H5EpochPersistor.Create("AppendToExistingHDF5.h5"))
            {
                var time = new DateTimeOffset(2011, 8, 22, 11, 12, 0, 0, TimeSpan.FromHours(-6));

                var src = persistor.AddSource("source1", null);
                persistor.BeginEpochGroup("label1", src, time);

                e1 = RunSingleEpoch(5000, 2, persistor);
                persistor.EndEpochGroup(time.AddMilliseconds(100));

                persistor.Close();
            }

            using (var persistor = new H5EpochPersistor("AppendToExistingHDF5.h5"))
            {
                var time = new DateTimeOffset(2011, 8, 22, 11, 12, 0, 0, TimeSpan.FromHours(-6));

                var src = persistor.AddSource("source2", null);
                persistor.BeginEpochGroup("label2", src, time);

                e2 = RunSingleEpoch(5000, 2, persistor);
                persistor.EndEpochGroup(time.AddMilliseconds(100));

                persistor.Close();
            }

            using (var persistor = new H5EpochPersistor("AppendToExistingHDF5.h5"))
            {
                Assert.AreEqual(2, persistor.Experiment.EpochGroups.Count());

                var eg1 = persistor.Experiment.EpochGroups.First(g => g.Label == "label1");

                Assert.AreEqual(1, eg1.EpochBlocks.Count());
                Assert.AreEqual(1, eg1.EpochBlocks.First().Epochs.Count());
                PersistentEpochAssert.AssertEpochsEqual(e1, eg1.EpochBlocks.First().Epochs.First());

                var eg2 = persistor.Experiment.EpochGroups.First(g => g.Label == "label2");

                Assert.AreEqual(1, eg2.EpochBlocks.Count());
                Assert.AreEqual(1, eg2.EpochBlocks.First().Epochs.Count());
                PersistentEpochAssert.AssertEpochsEqual(e2, eg2.EpochBlocks.First().Epochs.First());
            }
        }
コード例 #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);

            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));

            using (var persistor = H5EpochPersistor.Create(h5Path))
            {
                persistor.AddSource("source", null);
            }

            foreach (var daq in HekaDAQController.AvailableControllers())
            {

                try
                {

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

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

                    var dev0 = new UnitConvertingExternalDevice("Device0", "Manufacturer", controller,
                                                                new Measurement(0, "V"))
                                   {
                                       MeasurementConversionTarget = "V",
                                       Clock = daq.Clock,
                                       OutputSampleRate = daq.SampleRate,
                                       InputSampleRate = daq.SampleRate
                                   };
                    dev0.BindStream((IDAQOutputStream) daq.GetStreams("ao0").First());
                    dev0.BindStream((IDAQInputStream) daq.GetStreams("ai0").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.Backgrounds[dev0] = new Background(new Measurement(0, "V"), daq.SampleRate);

                        //Run single epoch
                        using (var persistor = new H5EpochPersistor(h5Path))
                        {
                            var source = persistor.Experiment.Sources.First();

                            persistor.BeginEpochGroup("label", source, DateTimeOffset.Now);
                            persistor.BeginEpochBlock(e.ProtocolID, e.ProtocolParameters, DateTimeOffset.Now);

                            controller.RunEpoch(e, persistor);

                            persistor.EndEpochBlock(DateTimeOffset.Now);
                            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.IsHardwareReady)
                        daq.CloseHardware();

                }
            }
        }