public static void AssertEpochsEqual(Epoch expected, IPersistentEpoch actual)
        {
            Assert.AreEqual((DateTimeOffset)expected.StartTime, actual.StartTime);
            Assert.AreEqual((DateTimeOffset)expected.StartTime + expected.Duration, actual.EndTime);
            CollectionAssert.AreEquivalent(expected.ProtocolParameters, actual.ProtocolParameters);
            CollectionAssert.AreEquivalent(expected.Properties, actual.Properties);
            CollectionAssert.AreEquivalent(expected.Keywords, actual.Keywords);

            // Backgrounds
            Assert.AreEqual(expected.Backgrounds.Count, actual.Backgrounds.Count());
            foreach (var kv in expected.Backgrounds)
            {
                var a = actual.Backgrounds.First(b => b.Device.Name == kv.Key.Name && b.Device.Manufacturer == kv.Key.Manufacturer);
                Assert.AreEqual(a.Epoch, actual);
                AssertBackgroundsEqual(kv.Value, a);
            }

            // Stimuli
            Assert.AreEqual(expected.Stimuli.Count, actual.Stimuli.Count());
            foreach (var kv in expected.Stimuli)
            {
                var a = actual.Stimuli.First(b => b.Device.Name == kv.Key.Name && b.Device.Manufacturer == kv.Key.Manufacturer);
                Assert.AreEqual(a.Epoch, actual);
                AssertStimuliEqual(kv.Value, a);
            }

            // Responses
            Assert.AreEqual(expected.Responses.Count, actual.Responses.Count());
            foreach (var kv in expected.Responses)
            {
                var a = actual.Responses.First(b => b.Device.Name == kv.Key.Name && b.Device.Manufacturer == kv.Key.Manufacturer);
                Assert.AreEqual(a.Epoch, actual);
                AssertResponsesEqual(kv.Value, a);
            }
        }
Ejemplo n.º 2
0
        public void IsIndefiniteIfAnyStimulusIsIndefinite()
        {
            Epoch e = new Epoch("");
            var dev = new UnitConvertingExternalDevice("name", "co", new Measurement(1.0m, "units"));
            var dev2 = new UnitConvertingExternalDevice("name2", "co", new Measurement(1.0m, "units"));

            Assert.That(e.IsIndefinite, Is.False);

            e.Stimuli[dev2] = new DelegatedStimulus("stimID", "units", new Measurement(1000, "Hz"),
                                                   new Dictionary<string, object>(),
                                                   (p, b) => null,
                                                   (p) => Option<TimeSpan>.Some(TimeSpan.FromMilliseconds(100)));

            Assert.That(e.IsIndefinite, Is.False);

            e.Stimuli[dev] = new DelegatedStimulus("stimID", "units", new Measurement(1000, "Hz"),
                                                   new Dictionary<string, object>(),
                                                   (p, b) => null,
                                                   (p) => Option<TimeSpan>.None());

            Assert.That(e.IsIndefinite, Is.True);

            e.Stimuli.Clear();

            Assert.That(e.IsIndefinite, Is.False);
        }
Ejemplo n.º 3
0
        public void OutputPipelineContinuity(
            [Values(1000, 5000, 10000, 15000, 20000)] double sampleRate,
            [Values(0.1, 0.5, 1, 5)] double blockDurationSeconds
            )
        {
            const double epochDuration = 2; //seconds
            var srate = new Measurement((decimal) sampleRate, "Hz");

            var daq = new TestDAQController();
            var outStream = new DAQOutputStream("OUT")
                                {
                                    SampleRate = srate,
                                    MeasurementConversionTarget = "V"
                                };
            var inStream = new DAQInputStream("IN")
                                {
                                    SampleRate = srate,
                                    MeasurementConversionTarget = "V"
                                };

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

            var dev = new UnitConvertingExternalDevice("dev", "co", controller, new Measurement(0, "V"))
                          {
                              Clock = daq,
                              MeasurementConversionTarget = "V",
                              OutputSampleRate = srate,
                              InputSampleRate = srate
                          };
            dev.BindStream(outStream).BindStream(inStream);

            // Setup Epoch
            var e = new Epoch("OutputPipelineContinuity");

            var nSamples = (int)TimeSpan.FromSeconds(epochDuration).Samples(srate);
            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();
            IOutputData stimOutputData = new OutputData(stimData, srate);

            var stim = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) new Dictionary<string, object>(),
                                            stimOutputData);
            e.Stimuli[dev] = stim;
            e.Responses[dev] = new Response();
            e.Backgrounds[dev] = new Background(new Measurement(0, "V"), srate);

            controller.EnqueueEpoch(e);

            var blockSpan = TimeSpan.FromSeconds(blockDurationSeconds);
            foreach (var stimBlock in stim.DataBlocks(blockSpan))
            {
                var cons = stimOutputData.SplitData(blockSpan);
                var expected = cons.Head.Data;
                stimOutputData = cons.Rest;

                Assert.That(stimBlock.Data, Is.EqualTo(expected));
            }
        }
Ejemplo n.º 4
0
        public void InstantiatesDictionaries()
        {
            Epoch e = new Epoch("Protocol", new Dictionary<string, object>());

            Assert.IsNotNull(e.ProtocolParameters);
            Assert.IsNotNull(e.Stimuli);
            Assert.IsNotNull(e.Responses);
            Assert.IsNotNull(e.ProtocolParameters);
            Assert.IsNotNull(e.Backgrounds);
        }
Ejemplo n.º 5
0
        public void ConvenienceConstructor()
        {
            Epoch e = new Epoch("Protocol");

            Assert.IsNotNull(e.ProtocolParameters);
            Assert.IsNotNull(e.Stimuli);
            Assert.IsNotNull(e.Responses);
            Assert.IsNotNull(e.ProtocolParameters);
            Assert.IsNotNull(e.Backgrounds);
            Assert.IsFalse(e.ShouldWaitForTrigger);
        }
Ejemplo n.º 6
0
        protected override void WriteBackgroundElement(Epoch e, IExternalDevice ed, Epoch.EpochBackground bg)
        {
            writer.WriteStartElement(ed.Name);

            writer.WriteStartElement("backgroundMeasurement");
            WriteMeasurement(bg.Background);
            writer.WriteEndElement();

            writer.WriteStartElement("sampleRate");
            WriteMeasurement(bg.SampleRate);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
Ejemplo n.º 7
0
        public void DurationsSumUpCorrectly()
        {
            Epoch e = new Epoch("Protocol");

            var ed = new UnitConvertingExternalDevice("TEST-DEVICE", "DEVICE-CO", new Measurement(0, "V"));

            IMeasurement UNUSED_SRATE = new Measurement(1000, "Hz");

            e.Stimuli[ed] = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) new Dictionary<string, object>(),
                 (IOutputData) new OutputData(new Measurement[] {
                                                                    new Measurement(0, "units"),
                                                                    new Measurement(1, "units")
                                                                }.ToList(), UNUSED_SRATE, false));

            // More to do here (later)
        }
Ejemplo n.º 8
0
        public void ShouldSurfaceExceptionInPersistorTask()
        {
            var c = new Controller();
            bool evt = false;

            c.DAQController = new TestDAQController();
            c.Clock = c.DAQController as IClock;
            var persistor = new AggregateExceptionThrowingEpochPersistor();

            c.SavedEpoch += (co, args) =>
            {
                evt = true;
            };

            var dev = new UnitConvertingExternalDevice(UNUSED_DEVICE_NAME, UNUSED_DEVICE_MANUFACTURER, UNUSED_BACKGROUND)
            {
                Controller = c,
                MeasurementConversionTarget = "V"
            };

            var outStream = new DAQOutputStream("outStream") { MeasurementConversionTarget = "V" };

            var inStream = new DAQInputStream("inStream") { MeasurementConversionTarget = "V" };

            (c.DAQController as IMutableDAQController).AddStream(outStream);
            (c.DAQController as IMutableDAQController).AddStream(inStream);

            var srate = new Measurement(10, "Hz");
            outStream.SampleRate = srate;
            inStream.SampleRate = srate;

            dev.BindStream(outStream);
            dev.BindStream(inStream);

            var e = new Epoch(UNUSED_PROTOCOL);
            var samples = new List<IMeasurement> { new Measurement(1.0m, "V"), new Measurement(1.0m, "V"), new Measurement(1.0m, "V") };
            var data = new OutputData(samples,
                srate,
                true);

            e.Stimuli[dev] = new RenderedStimulus((string) "stimID",
                                                  (IDictionary<string, object>) new Dictionary<string, object>(),
                                                  (IOutputData) data);
            e.Responses[dev] = new Response();
            e.Background[dev] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);

            ((TestDAQController)c.DAQController).AddStreamMapping(outStream, inStream);

            Assert.That(() => c.RunEpoch(e, persistor), Throws.TypeOf<SymphonyControllerException>());
        }
Ejemplo n.º 9
0
        public void ShouldNotPersistEpochGivenNullPersistor()
        {
            var c = new Controller();
            bool evt = false;

            c.DAQController = new TestDAQController();
            c.Clock = c.DAQController as IClock;

            c.DiscardedEpoch += (co, args) => Assert.Fail("Run failed");

            var dev = new UnitConvertingExternalDevice(UNUSED_DEVICE_NAME, UNUSED_DEVICE_MANUFACTURER, UNUSED_BACKGROUND)
            {
                Controller = c,
                MeasurementConversionTarget = "V"
            };

            var outStream = new DAQOutputStream("outStream") { MeasurementConversionTarget = "V" };

            var inStream = new DAQInputStream("inStream") { MeasurementConversionTarget = "V" };

            (c.DAQController as IMutableDAQController).AddStream(outStream);
            (c.DAQController as IMutableDAQController).AddStream(inStream);

            var srate = new Measurement(10, "Hz");
            outStream.SampleRate = srate;
            inStream.SampleRate = srate;

            dev.BindStream(outStream);
            dev.BindStream(inStream);

            var e = new Epoch(UNUSED_PROTOCOL);
            var samples = new List<IMeasurement> { new Measurement(1.0m, "V"), new Measurement(1.0m, "V"), new Measurement(1.0m, "V") };
            var data = new OutputData(samples,
                srate,
                true);

            e.Stimuli[dev] = new RenderedStimulus((string) "stimID",
                                                  (IDictionary<string, object>) new Dictionary<string, object>(),
                                                  (IOutputData) data);
            e.Responses[dev] = new Response();
            e.Background[dev] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);

            ((TestDAQController)c.DAQController).AddStreamMapping(outStream, inStream);

            c.RunEpoch(e, null);

            Assert.Pass();
        }
Ejemplo n.º 10
0
        public void RunEpochThrowsGivenIndefiniteEpochWithResponses()
        {
            var c = new Controller();

            var e = new Epoch(UNUSED_PROTOCOL);
            var dev2 = new UnitConvertingExternalDevice("dev2", "co", c, new Measurement(0, "V"));

            var sampleRate = new Measurement(1, "Hz");

            e.Stimuli[dev2] = new DelegatedStimulus("ID2", "units",
                                                    new Dictionary<string, object>(),
                                                    (x, y) => null,
                                                    (p) => Option<TimeSpan>.None());
            e.Responses[dev2] = new Response();

            Assert.That(() => c.RunEpoch(e, new FakeEpochPersistor()), Throws.Exception.TypeOf<ArgumentException>());
        }
Ejemplo n.º 11
0
 protected override void WriteBackground(Epoch e, IDictionary <IExternalDevice, Epoch.EpochBackground> background)
 {
     writer.WriteStartElement("background");
     base.WriteBackground(e, background);
     writer.WriteEndElement();
 }
Ejemplo n.º 12
0
 protected virtual void WriteEpochEnd(Epoch e)
 {
     Console.WriteLine("Epoch end");
 }
Ejemplo n.º 13
0
        public void ShouldAddProperties()
        {
            var e = new Epoch(UNUSED_PROTOCOL_ID);

            Assert.That(e.Properties, Is.Empty);
            e.Properties.Add("prop1", 3);
            e.Properties.Add("prop2", "hello");

            Assert.That(e.Properties, Contains.Item(new KeyValuePair<string, int>("prop1", 3)));
            Assert.That(e.Properties, Contains.Item(new KeyValuePair<string, string>("prop2", "hello")));
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Serialize an Epoch instance to some kind of persistent medium (file/database/etc).
 /// </summary>
 /// <param name="e">The Epoch to serialize.</param>
 public virtual void Serialize(Epoch e)
 {
     Serialize(e, null);
 }
Ejemplo n.º 15
0
 protected override void WriteEpochEnd(Epoch e)
 {
     writer.WriteEndElement();
     writer.Flush();
 }
Ejemplo n.º 16
0
 protected override void WriteResponsesEnd(Epoch e)
 {
     writer.WriteEndElement();
 }
Ejemplo n.º 17
0
 protected override void WriteResponsesStart(Epoch e)
 {
     writer.WriteStartElement("responses");
 }
Ejemplo n.º 18
0
 protected override void WriteStimuliEnd(Epoch e)
 {
     writer.WriteEndElement();
 }
Ejemplo n.º 19
0
 protected override void WriteStimuliStart(Epoch e)
 {
     writer.WriteStartElement("stimuli");
 }
Ejemplo n.º 20
0
 protected override void WriteProtocolParams(Epoch e, IDictionary <string, object> protoParams)
 {
     WriteDictionary("protocolParameters", protoParams);
 }
Ejemplo n.º 21
0
 protected override void WriteKeyword(Epoch e, string keyword)
 {
     writer.WriteStartElement("keyword");
     writer.WriteAttributeString("tag", keyword);
     writer.WriteEndElement();
 }
Ejemplo n.º 22
0
 public void SetCurrentEpoch(Epoch e)
 {
     this.EnqueueEpoch(e);
     this.NextEpoch();
 }
        public void ShouldAllowLongStringEpochParameters()
        {
            if (File.Exists("..\\..\\..\\ShouldAllowLongStringEpochParameters.h5"))
                File.Delete("..\\..\\..\\ShouldAllowLongStringEpochParameters.h5");

            var gID = new Guid("{2F2719F7-4A8C-4C22-9698-BE999DBC5385}");
            using (
                var exp = new EpochHDF5Persistor("..\\..\\..\\ShouldAllowLongStringEpochParameters.h5", null, () => gID)
                )
            {
                var time = new DateTimeOffset(1000, TimeSpan.Zero);
                var guid = new Guid("053C64D4-ED7A-4128-AA43-ED115716A97D");
                var props = new Dictionary<string, object>();
                props["key1"] =
                    "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
                    //100 elements
                props["key2"] = 2;

                const string protocolID = "Epoch.Fixture";
                Dictionary<string, object> parameters = props;

                dev1 = new UnitConvertingExternalDevice(dev1Name, "DEVICECO", new Measurement(0, "V"));
                dev2 = new UnitConvertingExternalDevice(dev2Name, "DEVICECO", new Measurement(0, "V"));

                var stream1 = new DAQInputStream("Stream1");
                var stream2 = new DAQInputStream("Stream2");

                var stimParameters = new Dictionary<string, object>();
                stimParameters[param1] = value1;
                stimParameters[param2] = value2;

                var srate = new Measurement(1000, "Hz");

                List<Measurement> samples =
                    Enumerable.Range(0, 10000).Select(i => new Measurement((decimal) Math.Sin((double) i/100), "mV")).
                        ToList();
                var stimData = new OutputData(samples, srate, false);

                var stim1 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                    //.Data does not need to be persisted
                var stim2 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                    //.Data does not need to be persisted

                var e = new Epoch(protocolID, parameters);
                e.Stimuli[dev1] = stim1;
                e.Stimuli[dev2] = stim2;

                DateTimeOffset start = DateTimeOffset.Parse("1/11/2011 6:03:29 PM -08:00");
                // Do this to match the XML stored in the EpochXML.txt resource
                e.StartTime = Maybe<DateTimeOffset>.Yes(start);

                e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), new Measurement(1000, "Hz"));
                e.Background[dev2] = new Epoch.EpochBackground(new Measurement(1, "V"), new Measurement(1000, "Hz"));

                e.Responses[dev1] = new Response();
                e.Responses[dev2] = new Response();

                var streamConfig = new Dictionary<string, object>();
                streamConfig[param1] = value1;

                var devConfig = new Dictionary<string, object>();
                devConfig[param2] = value2;

                IInputData responseData1 = new InputData(samples, srate, start)
                    .DataWithStreamConfiguration(stream1, streamConfig)
                    .DataWithExternalDeviceConfiguration(dev1, devConfig);
                IInputData responseData2 = new InputData(samples, srate, start)
                    .DataWithStreamConfiguration(stream2, streamConfig)
                    .DataWithExternalDeviceConfiguration(dev2, devConfig);

                e.Responses[dev1].AppendData(responseData1);
                e.Responses[dev2].AppendData(responseData2);

                e.Keywords.Add(kw1);
                e.Keywords.Add(kw2);

                exp.BeginEpochGroup("label", "source identifier", new[] {"keyword1", "keyword2"}, props, guid,
                                    time);

                exp.Serialize(e);

                exp.EndEpochGroup(time.AddMilliseconds(100));
                exp.Close();
            }

            H5.Close();

            var startInfo = new ProcessStartInfo(@"..\..\..\..\..\..\externals\HDF5\bin\h5dump",
                                                 @" --xml ..\..\..\ShouldAllowLongStringEpochParameters.h5");
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            Process proc = Process.Start(startInfo);

            Approvals.VerifyXml(proc.StandardOutput.ReadToEnd());
        }
Ejemplo n.º 24
0
 public override void Serialize(Epoch e)
 {
     ((LinkedList <Epoch>)PersistedEpochs).AddLast(e);
 }
Ejemplo n.º 25
0
        public void SetsProtocolID()
        {
            string expected = "ted rocks";
            Epoch e = new Epoch(expected, new Dictionary<string, object>());

            Assert.AreEqual(expected, e.ProtocolID);
        }
Ejemplo n.º 26
0
 public override void Serialize(Epoch e)
 {
     throw new AggregateException();
 }
Ejemplo n.º 27
0
 public void SimpleCreation()
 {
     Epoch epoch = new Epoch("Protocol", new Dictionary<string, object>());
     Assert.IsNotNull(epoch);
 }
Ejemplo n.º 28
0
 private void OnReceivedInputData(Epoch epoch)
 {
     FireEvent(ReceivedInputData, epoch);
 }
Ejemplo n.º 29
0
 protected virtual void WriteResponsesEnd(Epoch e)
 {
     Console.WriteLine("Epoch responses end");
 }
Ejemplo n.º 30
0
 private void OnSavedEpoch(Epoch epoch)
 {
     FireEvent(SavedEpoch, epoch);
 }
Ejemplo n.º 31
0
        public void RunEpochThrowsGivenEpochWithInconsistentStimulusDurations()
        {
            var c = new Controller();

            var e = new Epoch(UNUSED_PROTOCOL);
            var dev1 = new UnitConvertingExternalDevice("dev1", "co", c, new Measurement(0, "V"));
            var dev2 = new UnitConvertingExternalDevice("dev2", "co", c, new Measurement(0, "V"));

            var sampleRate = new Measurement(1, "Hz");

            e.Stimuli[dev1] = new RenderedStimulus((string) "ID1",
                                                   (IDictionary<string, object>) new Dictionary<string, object>(),
                                                   (IOutputData) new OutputData(new List<IMeasurement> { new Measurement(1, "V") },
                                                                                sampleRate, true));

            e.Stimuli[dev2] = new RenderedStimulus((string) "ID2",
                                                   (IDictionary<string, object>) new Dictionary<string, object>(),
                                                   (IOutputData) new OutputData(new List<IMeasurement> { new Measurement(1, "V"), new Measurement(1, "V") },
                                                                                sampleRate, true));

            Assert.That(() => c.RunEpoch(e, new FakeEpochPersistor()), Throws.Exception.TypeOf<ArgumentException>());

            e.Stimuli[dev2] = new DelegatedStimulus("ID2", "V",
                                                    new Dictionary<string, object>(),
                                                    (x, y) => null,
                                                    (p) => Option<TimeSpan>.None());

            Assert.That(() => c.RunEpoch(e, new FakeEpochPersistor()), Throws.Exception.TypeOf<ArgumentException>());
        }
Ejemplo n.º 32
0
 private void OnCompletedEpoch(Epoch epoch)
 {
     FireEvent(CompletedEpoch, epoch);
 }
Ejemplo n.º 33
0
        public void ShouldDiscardEpochsWithExceptionalStop()
        {
            var c = new Controller();
            bool evt = false;

            c.DAQController = new ExceptionThrowingDAQController();
            c.DAQController.Clock = c.DAQController as IClock;
            c.Clock = c.DAQController as IClock;
            var persistor = new FakeEpochPersistor();

            c.DiscardedEpoch += (co, args) => evt = true;

            var e = new Epoch(UNUSED_PROTOCOL);

            try
            {
                c.RunEpoch(e, persistor);
            }
            catch (Exception)
            { }

            Assert.True(evt);
            Assert.False(persistor.PersistedEpochs.ToList().Contains(e));
        }
Ejemplo n.º 34
0
 private void OnDiscardedEpoch(Epoch epoch)
 {
     FireEvent(DiscardedEpoch, epoch);
 }
Ejemplo n.º 35
0
        public void ShouldSupplyEpochBackgroundForExternalDevicesWithoutStimuli()
        {
            var c = new Controller();
            var dev1 = new UnitConvertingExternalDevice("dev1", "co", c, new Measurement(0, "V"));
            var dev2 = new UnitConvertingExternalDevice("dev2", "co", c, new Measurement(0, "V"));

            int baseSamples = 1000;
            IList<IMeasurement> data = (IList<IMeasurement>)Enumerable.Range(0, baseSamples)
                .Select(i => new Measurement(i, "V") as IMeasurement)
                .ToList();

            Measurement sampleRate = new Measurement(baseSamples, "Hz");
            var config = new Dictionary<string, object>();

            IOutputData data1 = new OutputData(data, sampleRate, true);

            var e = new Epoch("");
            e.Stimuli[dev1] = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) config, data1);

            var backgroundMeasurement = new Measurement(3.2m, "V");

            e.Background[dev2] = new Epoch.EpochBackground(backgroundMeasurement, sampleRate);

            c.EnqueueEpoch(e);
            c.NextEpoch();
            var out1 = c.PullOutputData(dev1, e.Duration);
            Assert.NotNull(out1);

            var out2 = c.PullOutputData(dev2, e.Duration);
            Assert.NotNull(out2);
            Assert.AreEqual((TimeSpan)e.Duration, out2.Duration);
            Assert.AreEqual(backgroundMeasurement, out2.Data.First());
        }
Ejemplo n.º 36
0
 private void FireEvent(EventHandler <TimeStampedEpochEventArgs> evt, Epoch epoch)
 {
     FireEvent(evt, new TimeStampedEpochEventArgs(Clock, epoch));
 }
Ejemplo n.º 37
0
        public void ShouldTruncateResponseAtEpochBoundary()
        {
            var c = new Controller();

            var e = new Epoch(UNUSED_PROTOCOL);
            var dev1 = new UnitConvertingExternalDevice("dev2", "co", c, new Measurement(0, "V"));

            var sampleRate = new Measurement(1, "Hz");

            var samples = new List<IMeasurement> { new Measurement(1, "V"), new Measurement(2, "V"), new Measurement(3, "V") };

            var data = new OutputData(samples,
                                      sampleRate, true);

            e.Stimuli[dev1] = new RenderedStimulus((string) "ID1",
                                                   (IDictionary<string, object>) new Dictionary<string, object>(),
                                                   (IOutputData) data);
            e.Responses[dev1] = new Response();

            c.EnqueueEpoch(e);
            c.NextEpoch();
            c.PushInputData(dev1, new InputData(samples.Concat(samples).ToList(),
                sampleRate,
                DateTimeOffset.Now)
                .DataWithStreamConfiguration(streamFake, new Dictionary<string, object>())
                .DataWithExternalDeviceConfiguration(devFake, new Dictionary<string, object>()));

            Assert.That(((TimeSpan)e.Responses[dev1].Duration), Is.EqualTo((TimeSpan)e.Duration));
        }
Ejemplo n.º 38
0
 /// <summary>
 /// Add an Epoch to the Controller's Epoch queue. Epochs are presented in FIFO order from
 /// this queue when running. You can use RunEpoch to bypass this queue, presenting an Epoch
 /// immediately
 /// </summary>
 /// <param name="e">Epoch to add to the queue</param>
 /// <see cref="RunEpoch"/>
 public void EnqueueEpoch(Epoch e)
 {
     ValidateEpoch(e);
     EpochQueue.Enqueue(e);
 }
 public TimeStampedEpochEventArgs(IClock clock, Epoch epoch)
     : base(clock)
 {
     Epoch = epoch;
 }
Ejemplo n.º 40
0
        /// <summary>
        /// The core entry point for the Controller Facade; push an Epoch in here, and when the
        /// Epoch is finished processing, control will be returned to you.
        ///
        /// <para>In other words, this
        /// method is blocking--the Controller cannot run more than one Epoch at a time.</para>
        /// </summary>
        ///
        /// <param name="e">Single Epoch to present</param>
        /// <param name="persistor">EpochPersistor for saving the data. May be null to indicate epoch should not be persisted</param>
        /// <exception cref="ValidationException">Validation failed for this Controller</exception>
        public void RunEpoch(Epoch e, EpochPersistor persistor)
        {
            var cts = new CancellationTokenSource();
            var cancellationToken = cts.Token;

            Task persistenceTask = null;

            if (!ValidateEpoch(e))
            {
                throw new ArgumentException("Epoch is not valid");
            }


            if (!Validate())
            {
                throw new ValidationException(Validate());
            }


            // Starting with this Epoch

            var cEpoch = CurrentEpoch;

            CurrentEpoch = e;

            EventHandler <TimeStampedEventArgs> nextRequested = (c, args) =>
            {
                DAQController.RequestStop();
                OnDiscardedEpoch(CurrentEpoch);
            };

            bool epochPersisted = false;
            EventHandler <TimeStampedEpochEventArgs> inputReceived = (c, args) =>
            {
                if (CurrentEpoch != null &&
                    CurrentEpoch.IsComplete)
                {
                    log.Debug("Epoch complete. Requesting DAQController stop.");
                    DAQController.RequestStop();
                    if (persistor != null && !epochPersisted)
                    {
                        Epoch completedEpoch = CurrentEpoch;
                        persistenceTask = Task.Factory.StartNew(() =>
                        {
                            log.DebugFormat("Saving completed Epoch ({0})...", completedEpoch.StartTime);
                            SaveEpoch(persistor, completedEpoch);
                        },
                                                                cancellationToken,
                                                                TaskCreationOptions.PreferFairness,
                                                                SerialTaskScheduler)
                                          .ContinueWith((task) =>
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            if (task.IsFaulted &&
                                task.Exception != null)
                            {
                                throw task.Exception;
                            }

                            OnCompletedEpoch(completedEpoch);
                        },
                                                        cancellationToken);

                        epochPersisted = true;
                    }
                }
            };

            EventHandler <TimeStampedExceptionEventArgs> exceptionalStop = (daq, args) =>
            {
                log.Debug(
                    "Discarding epoch due to exception");
                OnDiscardedEpoch(CurrentEpoch);
                throw new SymphonyControllerException(
                          "DAQ Controller stopped", args.Exception);
            };

            try
            {
                NextEpochRequested            += nextRequested;
                ReceivedInputData             += inputReceived;
                DAQController.ExceptionalStop += exceptionalStop;

                e.StartTime = Maybe <DateTimeOffset> .Some(this.Clock.Now);

                log.DebugFormat("Starting epoch: {0}", CurrentEpoch.ProtocolID);
                DAQController.Start(false);
            }
            finally
            {
                CurrentEpoch                   = cEpoch;
                NextEpochRequested            -= nextRequested;
                ReceivedInputData             -= inputReceived;
                DAQController.ExceptionalStop -= exceptionalStop;

                DAQController.WaitForInputTasks();

                //Clear remaining input
                UnusedInputData.Clear();
            }

            if (persistenceTask != null)
            {
                try
                {
                    persistenceTask.Wait();
                }
                catch (AggregateException ex)
                {
                    log.ErrorFormat("An error occurred while saving Epoch: {0}", ex);
                    throw new SymphonyControllerException("Unable to write Epoch data to persistor.",
                                                          ex.Flatten());
                }
            }
        }
        public void ShouldAllowNumericEpochParameters()
        {
            if (File.Exists("..\\..\\..\\ShouldAllowNumericEpochParameters.h5"))
                File.Delete("..\\..\\..\\ShouldAllowNumericEpochParameters.h5");

            var gID = new Guid("{2F2719F7-4A8C-4C22-9698-BE999DBC5385}");
            using (var exp = new EpochHDF5Persistor("..\\..\\..\\ShouldAllowNumericEpochParameters.h5", null, () => gID)
                )
            {
                var time = new DateTimeOffset(1000, TimeSpan.Zero);
                var guid = new Guid("053C64D4-ED7A-4128-AA43-ED115716A97D");
                var props = new Dictionary<string, object>();
                props["int"] = 2;
                props["float"] = 2.0f;
                props["double"] = 2.0d;
                props["decimal"] = 2.0m;
                props["array"] = new[] {1.0, 2.0, 3.0};
                props["short"] = (short) 2;
                props["unit16"] = (UInt16) 1;
                props["uint32"] = (UInt32) 2;
                props["byte"] = (byte) 1;
                props["bool"] = true;

                const string protocolID = "Epoch.Fixture";
                Dictionary<string, object> parameters = props;

                dev1 = new UnitConvertingExternalDevice(dev1Name, "DEVICECO", new Measurement(0, "V"));
                dev2 = new UnitConvertingExternalDevice(dev2Name, "DEVICECO", new Measurement(0, "V"));

                var stream1 = new DAQInputStream("Stream1");
                var stream2 = new DAQInputStream("Stream2");

                var stimParameters = new Dictionary<string, object>();
                stimParameters[param1] = value1;
                stimParameters[param2] = value2;

                var srate = new Measurement(1000, "Hz");

                List<Measurement> samples =
                    Enumerable.Range(0, 10000).Select(i => new Measurement((decimal) Math.Sin((double) i/100), "V")).
                        ToList();
                var stimData = new OutputData(samples, srate, false);

                var stim1 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                    //.Data does not need to be persisted
                var stim2 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                    //.Data does not need to be persisted

                var e = new Epoch(protocolID, parameters);
                e.Stimuli[dev1] = stim1;
                e.Stimuli[dev2] = stim2;

                DateTimeOffset start = DateTimeOffset.Parse("1/11/2011 6:03:29 PM -08:00");
                // Do this to match the XML stored in the EpochXML.txt resource
                e.StartTime = Maybe<DateTimeOffset>.Yes(start);

                e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), new Measurement(1000, "Hz"));
                e.Background[dev2] = new Epoch.EpochBackground(new Measurement(1, "V"), new Measurement(1000, "Hz"));

                e.Responses[dev1] = new Response();
                e.Responses[dev2] = new Response();

                var streamConfig = new Dictionary<string, object>();
                streamConfig[param1] = value1;

                var devConfig = new Dictionary<string, object>();
                devConfig[param2] = value2;

                IInputData responseData1 = new InputData(samples, srate, start)
                    .DataWithStreamConfiguration(stream1, streamConfig)
                    .DataWithExternalDeviceConfiguration(dev1, devConfig);
                IInputData responseData2 = new InputData(samples, srate, start)
                    .DataWithStreamConfiguration(stream2, streamConfig)
                    .DataWithExternalDeviceConfiguration(dev2, devConfig);

                e.Responses[dev1].AppendData(responseData1);
                e.Responses[dev2].AppendData(responseData2);

                e.Keywords.Add(kw1);
                e.Keywords.Add(kw2);

                exp.BeginEpochGroup("label", "source identifier", new[] {"keyword1", "keyword2"}, props, guid,
                                    time);
                exp.Serialize(e);
                exp.EndEpochGroup();
                exp.Close();
            }

            H5.Close();
            Approvals.VerifyFile("..\\..\\..\\ShouldAllowNumericEpochParameters.h5");
        }
Ejemplo n.º 42
0
 private void SaveEpoch(EpochPersistor persistor, Epoch e)
 {
     persistor.Serialize(e);
     OnSavedEpoch(e);
 }
Ejemplo n.º 43
0
        public void MaybeHasStartTime()
        {
            Epoch e = new Epoch("");

            Assert.False(e.StartTime);

            var dev = new UnitConvertingExternalDevice("name", "co", new Measurement(1.0m, "units"));

            var stim = new DelegatedStimulus("stimID", "units", new Measurement(1000, "Hz"),
                                             new Dictionary<string, object>(),
                                             (p, b) => null,
                                             (p) => Option<TimeSpan>.None());

            e.Stimuli[dev] = stim;

            var expected = DateTimeOffset.Now;

            stim.DidOutputData(expected, TimeSpan.FromSeconds(1), null);

            Assert.AreEqual(expected, (DateTimeOffset)e.StartTime);
        }
        public void ShouldAllowLongStringEpochParameters()
        {
            if (File.Exists("..\\..\\..\\ShouldAllowLongStringEpochParameters.h5"))
            {
                File.Delete("..\\..\\..\\ShouldAllowLongStringEpochParameters.h5");
            }

            var gID = new Guid("{2F2719F7-4A8C-4C22-9698-BE999DBC5385}");

            using (
                var exp = new EpochHDF5Persistor("..\\..\\..\\ShouldAllowLongStringEpochParameters.h5", null, () => gID)
                )
            {
                var time  = new DateTimeOffset(1000, TimeSpan.Zero);
                var guid  = new Guid("053C64D4-ED7A-4128-AA43-ED115716A97D");
                var props = new Dictionary <string, object>();
                props["key1"] =
                    "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
                //100 elements
                props["key2"] = 2;

                const string protocolID = "Epoch.Fixture";
                Dictionary <string, object> parameters = props;

                dev1 = new UnitConvertingExternalDevice(dev1Name, "DEVICECO", new Measurement(0, "V"));
                dev2 = new UnitConvertingExternalDevice(dev2Name, "DEVICECO", new Measurement(0, "V"));

                var stream1 = new DAQInputStream("Stream1");
                var stream2 = new DAQInputStream("Stream2");

                var stimParameters = new Dictionary <string, object>();
                stimParameters[param1] = value1;
                stimParameters[param2] = value2;

                var srate = new Measurement(1000, "Hz");

                List <Measurement> samples =
                    Enumerable.Range(0, 10000).Select(i => new Measurement((decimal)Math.Sin((double)i / 100), "mV")).
                    ToList();
                var stimData = new OutputData(samples, srate, false);

                var stim1 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                //.Data does not need to be persisted
                var stim2 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                //.Data does not need to be persisted

                var e = new Epoch(protocolID, parameters);
                e.Stimuli[dev1] = stim1;
                e.Stimuli[dev2] = stim2;

                DateTimeOffset start = DateTimeOffset.Parse("1/11/2011 6:03:29 PM -08:00");
                // Do this to match the XML stored in the EpochXML.txt resource
                e.StartTime = Maybe <DateTimeOffset> .Yes(start);

                e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), new Measurement(1000, "Hz"));
                e.Background[dev2] = new Epoch.EpochBackground(new Measurement(1, "V"), new Measurement(1000, "Hz"));

                e.Responses[dev1] = new Response();
                e.Responses[dev2] = new Response();

                var streamConfig = new Dictionary <string, object>();
                streamConfig[param1] = value1;

                var devConfig = new Dictionary <string, object>();
                devConfig[param2] = value2;

                IInputData responseData1 = new InputData(samples, srate, start)
                                           .DataWithStreamConfiguration(stream1, streamConfig)
                                           .DataWithExternalDeviceConfiguration(dev1, devConfig);
                IInputData responseData2 = new InputData(samples, srate, start)
                                           .DataWithStreamConfiguration(stream2, streamConfig)
                                           .DataWithExternalDeviceConfiguration(dev2, devConfig);

                e.Responses[dev1].AppendData(responseData1);
                e.Responses[dev2].AppendData(responseData2);

                e.Keywords.Add(kw1);
                e.Keywords.Add(kw2);

                exp.BeginEpochGroup("label", "source identifier", new[] { "keyword1", "keyword2" }, props, guid,
                                    time);

                exp.Serialize(e);

                exp.EndEpochGroup(time.AddMilliseconds(100));
                exp.Close();
            }

            H5.Close();

            var startInfo = new ProcessStartInfo(@"..\..\..\..\..\..\externals\HDF5\bin\h5dump",
                                                 @" --xml ..\..\..\ShouldAllowLongStringEpochParameters.h5");

            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute        = false;
            Process proc = Process.Start(startInfo);

            Approvals.VerifyXml(proc.StandardOutput.ReadToEnd());
        }
Ejemplo n.º 45
0
        public void ShouldAddKeywords()
        {
            var e = new Epoch(UNUSED_PROTOCOL_ID);
            var kw1 = "test";
            var kw2 = "other";

            Assert.That(e.Keywords, Is.Empty);
            e.Keywords.Add(kw1);
            e.Keywords.Add(kw2);

            Assert.That(e.Keywords, Contains.Item(kw1));
            Assert.That(e.Keywords, Contains.Item(kw2));
        }
        public void Setup()
        {
            const string protocolID = "Epoch.Fixture";
            var          parameters = new Dictionary <string, object>();

            parameters[param1] = value1;
            parameters[param2] = value2;

            dev1 = new UnitConvertingExternalDevice(dev1Name, "DEVICECO", new Measurement(0, "V"));
            dev2 = new UnitConvertingExternalDevice(dev2Name, "DEVICECO", new Measurement(0, "V"));

            var stream1 = new DAQInputStream("Stream1");
            var stream2 = new DAQInputStream("Stream2");

            var stimParameters = new Dictionary <string, object>();

            stimParameters[param1] = value1;
            stimParameters[param2] = value2;

            var srate = new Measurement(1000, "Hz");

            List <Measurement> samples =
                Enumerable.Range(0, 10000).Select(i => new Measurement((decimal)Math.Sin((double)i / 100), "V")).ToList();
            var stimData = new OutputData(samples, srate, false);

            var stim1 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
            //.Data does not need to be persisted
            var stim2 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
            //.Data does not need to be persisted

            var e = new Epoch(protocolID, parameters);

            e.Stimuli[dev1] = stim1;
            e.Stimuli[dev2] = stim2;

            DateTimeOffset start = DateTimeOffset.Parse("1/11/2011 6:03:29 PM -08:00");

            // Do this to match the XML stored in the EpochXML.txt resource
            e.StartTime = Maybe <DateTimeOffset> .Yes(start);

            e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), new Measurement(1000, "Hz"));
            e.Background[dev2] = new Epoch.EpochBackground(new Measurement(1, "V"), new Measurement(1000, "Hz"));

            e.Responses[dev1] = new Response();
            e.Responses[dev2] = new Response();

            var streamConfig = new Dictionary <string, object>();

            streamConfig[param1] = value1;

            var devConfig = new Dictionary <string, object>();

            devConfig[param2] = value2;

            IInputData responseData1 = new InputData(samples, srate, start)
                                       .DataWithStreamConfiguration(stream1, streamConfig)
                                       .DataWithExternalDeviceConfiguration(dev1, devConfig);
            IInputData responseData2 = new InputData(samples, srate, start)
                                       .DataWithStreamConfiguration(stream2, streamConfig)
                                       .DataWithExternalDeviceConfiguration(dev2, devConfig);

            e.Responses[dev1].AppendData(responseData1);
            e.Responses[dev2].AppendData(responseData2);

            e.Keywords.Add(kw1);
            e.Keywords.Add(kw2);

            testEpoch = e;
        }
Ejemplo n.º 47
0
        public void ShouldUniqueKeywords()
        {
            var e = new Epoch(UNUSED_PROTOCOL_ID);
            var kw1 = "test";

            e.Keywords.Add(kw1);
            e.Keywords.Add(kw1);

            Assert.That(e.Keywords, Contains.Item(kw1));
            Assert.That(e.Keywords.Count(), Is.EqualTo(1));
        }
Ejemplo n.º 48
0
        public void Setup()
        {
            const string protocolID = "Epoch.Fixture";
            var parameters = new Dictionary<string, object>();
            parameters[param1] = value1;
            parameters[param2] = value2;

            dev1 = new UnitConvertingExternalDevice(dev1Name, "DEVICECO", new Measurement(0, "V"));
            dev2 = new UnitConvertingExternalDevice(dev2Name, "DEVICECO", new Measurement(0, "V"));

            var stream1 = new DAQInputStream("Stream1");
            var stream2 = new DAQInputStream("Stream2");

            var stimParameters = new Dictionary<string, object>();
            stimParameters[param1] = value1;
            stimParameters[param2] = value2;

            var srate = new Measurement(1000, "Hz");

            var samples = Enumerable.Range(0, 1000).Select(i => new Measurement((decimal)Math.Sin((double)i / 100), "V")).ToList();
            var stimData = new OutputData(samples, srate, false);

            RenderedStimulus stim1 = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) stimParameters, (IOutputData) stimData); //.Data does not need to be persisted
            RenderedStimulus stim2 = new RenderedStimulus((string) "RenderedStimulus", (IDictionary<string, object>) stimParameters, (IOutputData) stimData); //.Data does not need to be persisted

            Epoch e = new Epoch(protocolID, parameters);
            e.Stimuli[dev1] = stim1;
            e.Stimuli[dev2] = stim2;

            var start = DateTimeOffset.Parse("1/11/2011 6:03:29 PM -08:00");
            // Do this to match the XML stored in the EpochXML.txt resource
            e.StartTime = Maybe<DateTimeOffset>.Yes(start);

            e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), new Measurement(1000, "Hz"));
            e.Background[dev2] = new Epoch.EpochBackground(new Measurement(1, "V"), new Measurement(1000, "Hz"));

            e.Responses[dev1] = new Response();
            e.Responses[dev2] = new Response();

            var streamConfig = new Dictionary<string, object>();
            streamConfig[param1] = value1;

            var devConfig = new Dictionary<string, object>();
            devConfig[param2] = value2;

            var responseData1 = new InputData(samples, srate, start)
                .DataWithStreamConfiguration(stream1, streamConfig)
                .DataWithExternalDeviceConfiguration(dev1, devConfig);
            var responseData2 = new InputData(samples, srate, start)
                .DataWithStreamConfiguration(stream2, streamConfig)
                .DataWithExternalDeviceConfiguration(dev2, devConfig);

            e.Responses[dev1].AppendData(responseData1);
            e.Responses[dev2].AppendData(responseData2);

            e.Keywords.Add(kw1);
            e.Keywords.Add(kw2);

            testEpoch = e;
        }
Ejemplo n.º 49
0
 public void BlowAwayTestEpoch()
 {
     testEpoch = null;
     dev1 = dev2 = null;
 }
Ejemplo n.º 50
0
        private static Controller SetupController(double sampleRate, out Epoch e, out IExternalDevice dev0, out RenderedStimulus stim1, out IExternalDevice dev1, out RenderedStimulus stim2, out IList<IMeasurement> stimData, int nChannels)
        {
            Converters.Clear();
            Converters.Register("V", "V",
                // just an identity conversion for now, to pass Validate()
                                (IMeasurement m) => m);

            var streamNameMap = new Dictionary<string, string>();
            streamNameMap["Out0"] = "In0";
            if (nChannels > 1)
                streamNameMap["Out1"] = "In1";

            Controller controller = new Parser().ParseConfiguration(Resources.LowGainConfig);

            var daq = (SimulationDAQController)controller.DAQController;
            daq.Clock = daq;
            foreach (var stream in daq.Streams)
            {
                stream.SampleRate = new Measurement((decimal) sampleRate, "Hz");
            }

            daq.SimulationRunner += (output, timestep) =>
                                        {
                                            var input = new ConcurrentDictionary<IDAQInputStream, IInputData>();

                                            Parallel.ForEach(output, (kv) =>
                                                                         {
                                                                             var outData = kv.Value;
                                                                             var outStream = kv.Key;
                                                                             var inStream = daq.InputStreams.Where((s) =>
                                                                                                                   s.Name == streamNameMap[outStream.Name]).First();

                                                                             var data = outData.DataWithUnits("V").Data;
                                                                             var inData = new InputData(data,
                                                                                                        outData.SampleRate,
                                                                                                        DateTimeOffset.Now)
                                                                                                        .DataWithNodeConfiguration("SimulationController",daq.Configuration);

                                                                             input[inStream] = inData;
                                                                         }
                                                );

                                            return input;
                                        };

            var protocolParams = new Dictionary<string, object>(1);
            protocolParams["key1"] = "value1";

            e = new Epoch("LowGainSimulation", protocolParams);
            dev0 = controller.GetDevice("Device0");
            dev1 = controller.GetDevice("Device1");

            if (nChannels == 1)
            {
                dev1.UnbindStream(dev1.Streams.Values.First().Name);
            }

            stimData = Enumerable.Range(0, (int)(10 * sampleRate))
                                   .Select(i => new Measurement(i, -3, "V") as IMeasurement)
                                   .ToList();
            var srate = new Measurement((decimal) sampleRate, "Hz");

            stim1 = new RenderedStimulus((string) "RenderedStimulus",
                                         (IDictionary<string, object>) new Dictionary<string, object>(),
                                         (IOutputData) new OutputData(stimData, srate, false));
            stim2 = new RenderedStimulus((string) "RenderedStimulus",
                                         (IDictionary<string, object>) new Dictionary<string, object>(),
                                         (IOutputData) new OutputData(stimData, srate, false));

            e.Stimuli[dev0] = stim1;

            if (nChannels > 1)
                e.Stimuli[dev1] = stim2;

            e.Responses[dev0] = new Response();
            if (nChannels > 1)
                e.Responses[dev1] = new Response();

            e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);
            e.Background[dev0] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);

            return controller;
        }
Ejemplo n.º 51
0
 public override IPersistentEpoch Serialize(Epoch e)
 {
     throw new AggregateException();
 }
Ejemplo n.º 52
0
 protected virtual void WriteResponse(Epoch e, IExternalDevice ed, Response r)
 {
     // write r.Data
 }
        public void ShouldAllowNumericEpochParameters()
        {
            if (File.Exists("..\\..\\..\\ShouldAllowNumericEpochParameters.h5"))
            {
                File.Delete("..\\..\\..\\ShouldAllowNumericEpochParameters.h5");
            }

            var gID = new Guid("{2F2719F7-4A8C-4C22-9698-BE999DBC5385}");

            using (var exp = new EpochHDF5Persistor("..\\..\\..\\ShouldAllowNumericEpochParameters.h5", null, () => gID)
                   )
            {
                var time  = new DateTimeOffset(1000, TimeSpan.Zero);
                var guid  = new Guid("053C64D4-ED7A-4128-AA43-ED115716A97D");
                var props = new Dictionary <string, object>();
                props["int"]     = 2;
                props["float"]   = 2.0f;
                props["double"]  = 2.0d;
                props["decimal"] = 2.0m;
                props["array"]   = new[] { 1.0, 2.0, 3.0 };
                props["short"]   = (short)2;
                props["unit16"]  = (UInt16)1;
                props["uint32"]  = (UInt32)2;
                props["byte"]    = (byte)1;
                props["bool"]    = true;

                const string protocolID = "Epoch.Fixture";
                Dictionary <string, object> parameters = props;

                dev1 = new UnitConvertingExternalDevice(dev1Name, "DEVICECO", new Measurement(0, "V"));
                dev2 = new UnitConvertingExternalDevice(dev2Name, "DEVICECO", new Measurement(0, "V"));

                var stream1 = new DAQInputStream("Stream1");
                var stream2 = new DAQInputStream("Stream2");

                var stimParameters = new Dictionary <string, object>();
                stimParameters[param1] = value1;
                stimParameters[param2] = value2;

                var srate = new Measurement(1000, "Hz");

                List <Measurement> samples =
                    Enumerable.Range(0, 10000).Select(i => new Measurement((decimal)Math.Sin((double)i / 100), "V")).
                    ToList();
                var stimData = new OutputData(samples, srate, false);

                var stim1 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                //.Data does not need to be persisted
                var stim2 = new RenderedStimulus("RenderedStimulus", stimParameters, stimData);
                //.Data does not need to be persisted

                var e = new Epoch(protocolID, parameters);
                e.Stimuli[dev1] = stim1;
                e.Stimuli[dev2] = stim2;

                DateTimeOffset start = DateTimeOffset.Parse("1/11/2011 6:03:29 PM -08:00");
                // Do this to match the XML stored in the EpochXML.txt resource
                e.StartTime = Maybe <DateTimeOffset> .Yes(start);

                e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), new Measurement(1000, "Hz"));
                e.Background[dev2] = new Epoch.EpochBackground(new Measurement(1, "V"), new Measurement(1000, "Hz"));

                e.Responses[dev1] = new Response();
                e.Responses[dev2] = new Response();

                var streamConfig = new Dictionary <string, object>();
                streamConfig[param1] = value1;

                var devConfig = new Dictionary <string, object>();
                devConfig[param2] = value2;

                IInputData responseData1 = new InputData(samples, srate, start)
                                           .DataWithStreamConfiguration(stream1, streamConfig)
                                           .DataWithExternalDeviceConfiguration(dev1, devConfig);
                IInputData responseData2 = new InputData(samples, srate, start)
                                           .DataWithStreamConfiguration(stream2, streamConfig)
                                           .DataWithExternalDeviceConfiguration(dev2, devConfig);

                e.Responses[dev1].AppendData(responseData1);
                e.Responses[dev2].AppendData(responseData2);

                e.Keywords.Add(kw1);
                e.Keywords.Add(kw2);

                exp.BeginEpochGroup("label", "source identifier", new[] { "keyword1", "keyword2" }, props, guid,
                                    time);
                exp.Serialize(e);
                exp.EndEpochGroup();
                exp.Close();
            }

            H5.Close();
            Approvals.VerifyFile("..\\..\\..\\ShouldAllowNumericEpochParameters.h5");
        }
Ejemplo n.º 54
0
 protected override void WriteKeywords(Epoch epoch, ISet <string> keywords)
 {
     writer.WriteStartElement("keywords");
     base.WriteKeywords(epoch, keywords);
     writer.WriteEndElement();
 }
Ejemplo n.º 55
0
 public TimeStampedEpochEventArgs(IClock clock, Epoch epoch)
     : base(clock)
 {
     Epoch = epoch;
 }
Ejemplo n.º 56
0
        public void ShouldPersistCompletedEpochs()
        {
            var  c   = new Controller();
            bool evt = false;

            c.DAQController = new TestDAQController();
            c.Clock         = c.DAQController as IClock;
            var persistor = new FakeEpochPersistor();

            c.SavedEpoch += (co, args) =>
            {
                evt = true;
            };

            var dev = new UnitConvertingExternalDevice(UNUSED_DEVICE_NAME, UNUSED_DEVICE_MANUFACTURER, UNUSED_BACKGROUND)
            {
                Controller = c,
                MeasurementConversionTarget = "V"
            };

            var outStream = new DAQOutputStream("outStream")
            {
                MeasurementConversionTarget = "V"
            };

            var inStream = new DAQInputStream("inStream")
            {
                MeasurementConversionTarget = "V"
            };

            (c.DAQController as IMutableDAQController).AddStream(outStream);
            (c.DAQController as IMutableDAQController).AddStream(inStream);

            var srate = new Measurement(10, "Hz");

            outStream.SampleRate = srate;
            inStream.SampleRate  = srate;

            dev.BindStream(outStream);
            dev.BindStream(inStream);

            var e       = new Epoch(UNUSED_PROTOCOL);
            var samples = new List <IMeasurement> {
                new Measurement(1.0m, "V"), new Measurement(1.0m, "V"), new Measurement(1.0m, "V")
            };
            var data = new OutputData(samples,
                                      srate,
                                      true);

            e.Stimuli[dev] = new RenderedStimulus((string)"stimID",
                                                  (IDictionary <string, object>) new Dictionary <string, object>(),
                                                  (IOutputData)data);
            e.Responses[dev]  = new Response();
            e.Background[dev] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);

            ((TestDAQController)c.DAQController).AddStreamMapping(outStream, inStream);

            c.RunEpoch(e, persistor);

            Assert.That(evt, Is.True.After(10 * 1000, 100));
            Assert.That(persistor.PersistedEpochs, Has.Member(e));
        }
Ejemplo n.º 57
0
        public void MultipleEpochs(
            [Values(1000, 5000, 10000, 20000)] double sampleRate
            )
        {
            Epoch e;
            IExternalDevice dev0;
            RenderedStimulus stim1;
            IExternalDevice dev1;
            RenderedStimulus stim2;
            IList<IMeasurement> stimData;
            var controller = SetupController(sampleRate, out e, out dev0, out stim1, out dev1, out stim2, out stimData, 2);

            controller.RunEpoch(e, new FakeEpochPersistor());

            var inputData = e.Responses[dev0].Data;
            const double MAX_VOLTAGE_DIFF = 0.001;
            int failures = inputData.Select((t, i) => t.QuantityInBaseUnit - stimData[i].QuantityInBaseUnit)
                .Count(dif => Math.Abs(dif) > (decimal) MAX_VOLTAGE_DIFF);

            Assert.AreEqual(0, failures);

            e = new Epoch("LowGainSimulation");
            dev0 = controller.GetDevice("Device0");
            dev1 = controller.GetDevice("Device1");

            var srate = new Measurement((decimal) sampleRate, "Hz");

            stim1 = new RenderedStimulus((string) "RenderedStimulus",
                                         (IDictionary<string, object>) new Dictionary<string, object>(),
                                         (IOutputData) new OutputData(stimData, srate, false));
            stim2 = new RenderedStimulus((string) "RenderedStimulus",
                                         (IDictionary<string, object>) new Dictionary<string, object>(),
                                         (IOutputData) new OutputData(stimData, srate, false));

            e.Stimuli[dev0] = stim1;
            e.Stimuli[dev1] = stim2;

            e.Responses[dev0] = new Response();
            e.Responses[dev1] = new Response();

            e.Background[dev1] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);
            e.Background[dev0] = new Epoch.EpochBackground(new Measurement(0, "V"), srate);

            Assert.DoesNotThrow(() => controller.RunEpoch(e, new FakeEpochPersistor()));

            inputData = e.Responses[dev0].Data;
            failures = inputData.Select((t, i) => t.QuantityInBaseUnit - stimData[i].QuantityInBaseUnit)
                .Count(dif => Math.Abs(dif) > (decimal) MAX_VOLTAGE_DIFF);

            Assert.AreEqual(0, failures);
        }
Ejemplo n.º 58
0
 public void SetCurrentEpoch(Epoch e)
 {
     this.EnqueueEpoch(e);
     this.NextEpoch();
 }
Ejemplo n.º 59
0
 public virtual IPersistentEpoch Serialize(Epoch e)
 {
     ((LinkedList<Epoch>)PersistedEpochs).AddLast(e);
     return null;
 }
Ejemplo n.º 60
0
 protected virtual void WriteResponsesStart(Epoch e)
 {
     Console.WriteLine("Epoch responses start");
 }