Beispiel #1
0
        private async Task <List <EITEntry> > WriteEITData(AcquisitionProtocolParameters parameters,
                                                           KamaAcquisitionFile file, int loop, int sleepsBetweenWritesMilliseconds)
        {
            var result = await Task.Factory.StartNew(() =>
            {
                Random random = new Random();
                List <ElectrodeFrame> samples = new List <ElectrodeFrame>();
                for (int index = 0; index < loop; index++)
                {
                    ElectrodeFrame frame       = new ElectrodeFrame();
                    frame.ComplexCurrentMatrix = Enumerable.Range(0, 2441)
                                                 .Select(i => ((float)random.NextDouble(), (float)random.NextDouble())).ToArray();
                    frame.ComplexVoltageMatrix = Enumerable.Range(0, 2441)
                                                 .Select(i => ((float)random.NextDouble(), (float)random.NextDouble())).ToArray();
                    frame.PacketId       = (ulong)index;
                    var timestamp        = DateTimeOffset.Now;
                    frame.timestamp      = timestamp.ToUnixTimeMilliseconds();
                    ulong saturationMask = (ulong)random.Next();
                    saturationMask       = (saturationMask << 32);
                    saturationMask       = saturationMask | (ulong)random.Next();
                    frame.SaturationMask = saturationMask;
                    Thread.Sleep(sleepsBetweenWritesMilliseconds);
                    file.AppendElectrodeSample(frame);
                    samples.Add(frame);
                }

                EITEntry entry = new EITEntry
                {
                    Configuration = parameters.AsJson(),
                    StartDateTime = DateTimeOffset.FromUnixTimeMilliseconds(samples.First().timestamp).DateTime,
                    EndDateTime   = DateTimeOffset.FromUnixTimeMilliseconds(samples.Last().timestamp).DateTime,
                    VoltagesReal  = new float[samples.Count, samples[0].ComplexVoltageMatrix.Length],
                    VoltagesIm    = new float[samples.Count, samples[0].ComplexVoltageMatrix.Length],
                    CurrentsIm    = new float[samples.Count, samples[0].ComplexVoltageMatrix.Length],
                    CurrentsReal  = new float[samples.Count, samples[0].ComplexVoltageMatrix.Length],
                    Saturation    = new ulong[samples.Count, 1],
                    Timestamps    = new long[samples.Count, 1]
                };

                for (var i = 0; i < samples.Count; i++)
                {
                    ElectrodeFrame electrodeFrame = samples[i];

                    for (int j = 0; j < electrodeFrame.ComplexVoltageMatrix.Length; j++)
                    {
                        entry.VoltagesReal[i, j] = electrodeFrame.ComplexVoltageMatrix[j].Re;
                        entry.VoltagesIm[i, j]   = electrodeFrame.ComplexVoltageMatrix[j].Im;
                    }

                    for (int j = 0; j < electrodeFrame.ComplexCurrentMatrix.Length; j++)
                    {
                        entry.CurrentsReal[i, j] = electrodeFrame.ComplexCurrentMatrix[j].Re;
                        entry.CurrentsIm[i, j]   = electrodeFrame.ComplexCurrentMatrix[j].Im;
                    }

                    entry.Saturation[i, 0] = electrodeFrame.SaturationMask;
                    entry.Timestamps[i, 0] = electrodeFrame.timestamp;
                }

                return(entry);
            });

            return(new List <EITEntry>
            {
                result
            });
        }
Beispiel #2
0
        public async Task CreatePatientInfoTest()
        {
            string data     = File.ReadAllText(AcquisitionScanProtocolPath);
            string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "h5test.h5");

            kama = new KamaAcquisitionFile(filename, AcquisitionInterface.Simulator, Logger);
            CreatePatientDetails(kama);
            UpdateSystemInformation(kama);


            AcquisitionProtocolParameters parameters = AcquisitionProtocolParameters.FromJson(data);
            await kama.StartLogging(parameters);


            string lineVoltage, lineCurrent, lineEcg;
            string directory   = AppDomain.CurrentDomain.BaseDirectory;
            string voltagePath = Path.Combine(directory, voltage);
            string currentPath = Path.Combine(directory, current);

            using StreamReader srVoltage = new StreamReader(voltagePath);
            using StreamReader srCurrent = new StreamReader(currentPath);
            int i = 0;

            while (!string.IsNullOrEmpty(lineVoltage = await srVoltage.ReadLineAsync()) &&
                   !string.IsNullOrEmpty(lineCurrent = await srCurrent.ReadLineAsync()))
            {
                ElectrodeFrame sample       = new ElectrodeFrame();
                var            itemsVoltage = lineVoltage.Split(',', StringSplitOptions.RemoveEmptyEntries);
                var            itemsCurrent = lineCurrent.Split(',', StringSplitOptions.RemoveEmptyEntries);
                sample.timestamp      = long.Parse(itemsCurrent.First());
                sample.SaturationMask = Convert.ToUInt64(itemsCurrent.Skip(1).Take(1).Single().Trim(), 16);
                var evenvals = itemsCurrent.Skip(2).Where((c, i) => i % 2 == 0).Select(float.Parse);
                var oddvals  = itemsCurrent.Skip(2).Where((c, i) => i % 2 != 0).Select(float.Parse);

                sample.ComplexCurrentMatrix = evenvals.Zip(oddvals).Select(itm => (itm.First, itm.Second)).ToArray();

                evenvals = itemsVoltage.Skip(2).Where((c, i) => i % 2 == 0).Select(float.Parse);
                oddvals  = itemsVoltage.Skip(2).Where((c, i) => i % 2 != 0).Select(float.Parse);

                sample.ComplexVoltageMatrix = evenvals.Zip(oddvals).Select(itm => (itm.First, itm.Second)).ToArray();
                kama.AppendElectrodeSample(sample);
            }

            using StreamReader srEcg = new StreamReader(ecg);

            var filteredFrameData = new List <List <float> > {
                new List <float>(), new List <float>()
            };
            var frameData = new List <List <float> > {
                new List <float>(), new List <float>()
            };
            var timestamps = new List <long>();

            while (!string.IsNullOrEmpty(lineEcg = await srEcg.ReadLineAsync()))
            {
                var items = lineEcg.Split(",", StringSplitOptions.RemoveEmptyEntries);
                var c1    = float.Parse(items[0]);
                var c2    = float.Parse(items[1]);
                var time  = long.Parse(items[2]);

                var c1_filtered = float.Parse(items[4]);
                var c2_filtered = float.Parse(items[5]);

                frameData[0].Add(c1);
                frameData[1].Add(c2);

                filteredFrameData[0].Add(c1_filtered);
                filteredFrameData[1].Add(c2_filtered);
                timestamps.Add(time);
            }

            ECGFrame frame = new ECGFrame(filteredFrameData, frameData, timestamps[0], 0);

            kama.AppendEcgSample(frame);
            await kama.StopProcedure();

            var       fileId   = Hdf5.OpenFile(filename);
            Stopwatch st       = Stopwatch.StartNew();
            var       readback = Hdf5.ReadDatasetToArray <float>(fileId, "/eit/d1/voltages.re");

            st.Stop();
            Console.WriteLine("read time: " + st.ElapsedMilliseconds);
            Hdf5.CloseFile(fileId);
            File.Delete(filename);
        }