Ejemplo n.º 1
0
        internal static void BuildFile(object sender, DoWorkEventArgs e)
        {
            Parameters parameters = (Parameters)e.Argument;

            DirectoryInfo di = new DirectoryInfo(parameters.directoryPath);
            try
            {
                if (!di.Exists) di.Create();
            }
            catch (Exception io)
            {
                throw new Exception("Directory creation failed: " + io.ToString());
            }

            parameters.fileName = di.FullName + Path.DirectorySeparatorChar + parameters.fileName;

            /* ***** Create new BDF/EDF file ***** */
            BDFEDFFileWriter file = new BDFEDFFileWriter(
                File.Open(parameters.fileName + (parameters.BDFFormat ? ".bdf" : ".edf"), FileMode.Create, FileAccess.ReadWrite),
                parameters.nChan + 1, // add one to include Status
                parameters.recordDuration,
                parameters.samplingRate,
                parameters.BDFFormat);

            file.LocalRecordingId = parameters.LocalRecordingId;
            file.LocalSubjectId = parameters.LocalSubjectId;
            for (int ichan = 0; ichan < parameters.nChan; ichan++)
            {
                file.channelLabel(ichan, parameters.ChannelLabelPrefix + " " + (ichan + 1).ToString("G"));
                file.transducer(ichan, parameters.TransducerString);
                file.dimension(ichan, parameters.PhysicalDimensionString);
                file.pMin(ichan, (int)Math.Ceiling(parameters.pMin));
                file.pMax(ichan, (int)Math.Ceiling(parameters.pMax));
                file.dMin(ichan, parameters.dMin);
                file.dMax(ichan, parameters.dMax);
                file.prefilter(ichan, parameters.PrefilterString);
            }
            file.channelLabel(parameters.nChan, "Status");
            file.transducer(parameters.nChan, "None");
            file.dimension(parameters.nChan, "None");
            file.pMin(parameters.nChan, -Math.Pow(2D, 23D));
            file.pMax(parameters.nChan, Math.Pow(2D, 23D) - 1);
            file.dMin(parameters.nChan, (int)file.pMin(parameters.nChan));
            file.dMax(parameters.nChan, (int)file.pMax(parameters.nChan));
            file.prefilter(parameters.nChan, "None");

            /* ***** Create Electrode position file ***** */
            double[] phi;
            double[] theta;
            setElectrodeLocations(parameters.nChan, out phi, out theta); // assign locations
            ElectrodeFileStream.ElectrodeOutputFileStream efs = new ElectrodeFileStream.ElectrodeOutputFileStream(
                File.Open(parameters.fileName + ".etr", FileMode.Create, FileAccess.Write),typeof(PhiThetaRecord));
            for (int i = 0; i < parameters.nChan; i++)
            {
                string sName = parameters.ChannelLabelPrefix + " " + (i + 1).ToString("0");
                PhiThetaRecord xy = new PhiThetaRecord(sName, phi[i], theta[i]);
                xy.write(efs, "");
            }
            efs.Close();

            /* ***** Create new HDR file ***** */
            new HeaderFileWriter(
                File.Open(parameters.fileName + ".hdr", FileMode.Create, FileAccess.Write),
                parameters.head);

            /* ***** Create new Event file and initialize ***** */
            EventFileWriter events = new EventFileWriter(
                File.Open(parameters.fileName + ".evt", FileMode.Create, FileAccess.Write));
            int lastN = 0; // last used index of Event
            int lastG = 0; // last used grayCode

            /* ***** Other preliminaries ***** */
            int nRec = (int)Math.Ceiling((double)parameters.totalFileLength / (double)parameters.recordDuration);
            int nPts = parameters.recordDuration * parameters.samplingRate;
            DateTime dt = DateTime.Now; // base time for events
            double T = 0D;
            deltaT = 1D / Convert.ToDouble(parameters.samplingRate);
            int[] statusChannel = new int[nPts];

            /* ***** Main loop ***** */
            for (int rec = 0; rec < nRec; rec++ ) //for each required record
            {
                for (int p = 0; p < nPts; p++) //for each point in a record
                {
                    foreach (Event evt in parameters.eventList) //loop through each Event definition
                    {
                        if (evt.IsNow(T)) // is next occurence of Event at this tick?
                        {
                            lastN = (lastN % ((1 << parameters.nBits) - 2)) + 1; // get next index value
                            OutputEvent oe = new OutputEvent(evt.EDEntry, dt.AddSeconds(T), lastN);
                            lastG = oe.GC; // get the corresponding grayCode value; calculated as OutputEvent created
                            int n = evt.GVs.Count;
                            oe.GVValue = new string[n]; // assign group variable values
                            for (int i = 0; i < n; i++)
                            {
                                oe.GVValue[i] = evt.oldGVValues[i].ToString("0");
                            }
                            events.writeRecord(oe); // write out new Event record
                        }
                    }

                    statusChannel[p] = lastG; //Status channel
                    double eventcontribution = calculateEventSignal(parameters);
                    for (int chan = 1; chan <= parameters.nChan; chan++)
                    {
                        file.putSample(chan - 1, p, parameters.window.Calculate(T, chan) + eventcontribution);
                    }
                    T += deltaT;
                }
                file.putStatus(statusChannel);
                file.write();
                if (Window1.bw.CancellationPending)
                {
                    file.Close();
                    events.Close();
                    e.Cancel = true;
                    return;
                }
                Window1.bw.ReportProgress(Convert.ToInt32(100D * (double)rec / (double)nRec));
            }
            events.Close();
            file.Close();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructor for stream to read Electrode Position file, based on another stream;
 /// reads in entire file, creating dictionary of eletrode positions by name and then
 /// closing the stream
 /// </summary>
 /// <param name="str">Stream on which this stream is based</param>
 public ElectrodeInputFileStream(Stream str)
 {
     if (str == null || !str.CanRead) return; //return empty Dictionary
     XmlReaderSettings settings = new XmlReaderSettings();
     settings.IgnoreWhitespace = true;
     settings.IgnoreComments = true;
     settings.IgnoreProcessingInstructions = true;
     XmlReader xr;
     string nameSpace;
     string type;
     try
     {
         xr = XmlReader.Create(str, settings);
         if (xr.MoveToContent() != XmlNodeType.Element) throw new XmlException("Not a valid electrode file");
         nameSpace = xr.NamespaceURI;
         type = xr["Type", nameSpace];
         xr.ReadStartElement("Electrodes");
     }
     catch (Exception x)
     {
         throw new Exception("ElectrodeFileStream: " + x.Message);
     }
     ElectrodeRecord etrRecord;
     while (xr.Name == "Electrode")
     {
         try
         {
             if (type == "PhiTheta") etrRecord = new PhiThetaRecord();
             else if (type == "XY") etrRecord = new XYRecord();
             else if (type == "XYZ") etrRecord = new XYZRecord();
             else throw new Exception("Invalid electrode type");
             etrRecord.read(xr, nameSpace);
         }
         catch (XmlException e)
         {
             throw new Exception("ElectrodeFileStream: " + e.Message);
         }
         etrPositions.Add(etrRecord.Name, etrRecord);
     }
     xr.Close();
 }