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;
        }
Exemple #2
0
        private static void ValidateObjectGraph(string input)
        {
            var con = new Parser().ParseConfiguration(input);

            Assert.AreEqual(3, con.Devices.Count);

            var ledDevice = con.Devices.Where(ed => ed.Name == "LED").First();
            Assert.True(ledDevice.Streams.ContainsKey("Out0"));
            Assert.True(ledDevice.Streams["Out0"] is DAQOutputStream);
            Assert.AreEqual(new Measurement(1000, "Hz"), ledDevice.Streams["Out0"].SampleRate);

            // Validate and report the validation results
            Maybe<string> conVal = con.Validate();
            Assert.IsTrue(conVal, conVal);

            // These get assigned from Configuration at Validation
            Assert.AreEqual("units", ((UnitConvertingExternalDevice)ledDevice).MeasurementConversionTarget);
            Assert.AreEqual("units", ledDevice.Streams["Out0"].MeasurementConversionTarget);

            Assert.AreEqual(3, con.Devices.Count);

            foreach (ExternalDeviceBase ed in con.Devices)
            {
                if (ed.Name == "Amp")
                {
                    Assert.IsTrue(ed.Streams.Count() == 3);
                    Assert.IsTrue(ed is CoalescingDevice);
                    CoalescingDevice amp = (CoalescingDevice)ed;
                    Assert.IsTrue(amp.Coalesce == CoalescingDevice.OneItemCoalesce);
                }
                if (ed.Name == "LED")
                {
                    Assert.IsTrue(ed.Streams.Count() == 1);
                }
                if (ed.Name == "Temp")
                {
                    Assert.IsTrue(ed.Streams.Count() == 1);
                }
            }

            Assert.That(con.DAQController.Streams, Has.Count.GreaterThan(0));
            foreach (var s in con.DAQController.Streams)
            {
                //Tests that ApplyConfiguration is called
                Assert.That(s.MeasurementConversionTarget, Is.EqualTo("units"));
                Assert.That(s.Configuration["MeasurementConversionTarget"], Is.EqualTo("units"));
            }
        }