Example #1
0
 /// <summary>
 /// Removes the given ExternalDevice from this stream.
 /// </summary>
 /// <param name="device">Device to remove</param>
 public void RemoveDevice(ExternalDeviceBase device)
 {
     if (Device == device)
     {
         Device.UnbindStream(Name);
         Device = null;
     }
 }
        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;
        }
        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);
        }