Пример #1
0
    protected void LoadNIRx(object sender, EventArgs e)
    {
        Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog("Choose the NIRx *.wl1 file to open",
                                                             this, Gtk.FileChooserAction.Open,
                                                             "Cancel", Gtk.ResponseType.Cancel,
                                                             "Open", Gtk.ResponseType.Accept);

        fc.Filter = new FileFilter();
        fc.Filter.AddPattern("*.wl1");

        if (fc.Run() == (int)Gtk.ResponseType.Accept)
        {
            // use the NIRSdotNET toolbox to load
            data = nirs.io.readNIRx(fc.Filename);
        }
        //Destroy() to close the File Dialog
        fc.Destroy();

        // This adds some info to the bottom right tab menu in the GUI
        updateInfoText();

        // use the data and probe methods to handle the drawing code
        data.probe.draw(this.drawingareaSDG.GdkWindow);
        this.drawingareaSDG.QueueDraw();
        data.draw(this.drawingarea_main.GdkWindow);
        this.drawingarea_main.QueueDraw();
    }
Пример #2
0
    nirs.core.Data data; // this holds the NIRS data (time-course) class

    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
        data = new nirs.core.Data();


        // This sets the drawing functions for the time-course and probe windows
        this.drawingareaSDG.ExposeEvent   += sdgdraw;
        this.drawingarea_main.ExposeEvent += datadraw;
        this.drawingareaSDG.AddEvents((int)Gdk.EventMask.ButtonPressMask);
        this.drawingareaSDG.AddEvents((int)Gdk.EventMask.ButtonReleaseMask);
        this.drawingareaSDG.ButtonReleaseEvent += ClickSDG;
    }
Пример #3
0
    // This restores the last Study/probe used
    public void RegisterQuickStart(object sender, EventArgs e)
    {
        string path = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

        path = System.IO.Path.Combine(path, "LastSettings.xml");

        // Read the Config.xml file
        XmlDocument doc = new XmlDocument();

        doc.Load(path);
        XmlNodeList elemList;

        elemList = doc.GetElementsByTagName("Investigator");
        string investigator = elemList[0].InnerXml.Trim();

        elemList = doc.GetElementsByTagName("Study");
        string study = elemList[0].InnerXml.Trim();

        elemList = doc.GetElementsByTagName("probefile");
        string probefile = elemList[0].InnerXml.Trim();


        int[]           lambda = MainClass.win.settings.system_Info.wavelengths;
        nirs.core.Probe probe  = nirs.io.LoadProbe(probefile, lambda);
        // Add channels for Optical Density, HbO2, and HbR

        int cnt = probe.ChannelMap.Length;

        nirs.ChannelMap[] ChannelMap = new nirs.ChannelMap[cnt * 2 + 2 * cnt / probe.numWavelengths];
        for (int ii = 0; ii < cnt; ii++)
        {
            ChannelMap[ii] = probe.ChannelMap[ii];
        }
        for (int ii = 0; ii < cnt; ii++)
        {
            ChannelMap[ii + cnt]             = probe.ChannelMap[ii];
            ChannelMap[ii + cnt].datasubtype = string.Format("ΔOD {0}nm", ChannelMap[ii].wavelength);
        }
        for (int ii = 0; ii < cnt / probe.numWavelengths; ii++)
        {
            ChannelMap[ii + 2 * cnt]             = probe.ChannelMap[ii];
            ChannelMap[ii + 2 * cnt].datasubtype = "HbO2";
        }
        for (int ii = cnt / probe.numWavelengths; ii < cnt; ii++)
        {
            ChannelMap[ii + 2 * cnt]             = probe.ChannelMap[ii];
            ChannelMap[ii + 2 * cnt].datasubtype = "Hb";
        }

        probe.ChannelMap  = ChannelMap;
        probe.measlistAct = new bool[probe.ChannelMap.Length];
        for (int ii = 0; ii < probe.ChannelMap.Length; ii++)
        {
            probe.measlistAct[ii] = true;
        }
        Gdk.Color[] cmap = new Gdk.Color[probe.ChannelMap.Length];
        for (int ii = 0; ii < probe.numChannels; ii++)
        {
            cmap[ii] = probe.colormap[ii];
            cmap[probe.numChannels + ii]     = probe.colormap[ii];
            cmap[probe.numChannels * 2 + ii] = probe.colormap[ii];
        }
        probe.colormap = cmap;

        nirsdata = new List <nirs.core.Data>();

        for (int i = 0; i < MainClass.devices.Length; i++)
        {
            nirs.core.Data data = new nirs.core.Data
            {
                demographics = new nirs.Dictionary()
            };
            data.demographics.set("SubjID", "");
            data.demographics.set("Investigator", investigator);
            data.demographics.set("Study", study);
            data.demographics.set("Gender", "");
            data.demographics.set("Group", "");
            data.demographics.set("Age", "");
            data.demographics.set("Instrument", MainClass.win.settings.SYSTEM);
            data.demographics.set("head_circumference", "");
            data.demographics.set("Technician", "");
            data.demographics.set("comments", "");
            DateTime now = DateTime.Now;
            data.demographics.set("scan_date", now.ToString("F"));
            data.probe = probe.Clone();
            nirsdata.Add(data);
        }



        ListStore ClearList = new ListStore(typeof(string));

        MainClass.win._handles.whichdata.Model = ClearList;

        List <string> datatypes = new List <string>();

        for (int ii = 0; ii < probe.ChannelMap.Length; ii++)
        {
            datatypes.Add(probe.ChannelMap[ii].datasubtype);
        }
        datatypes = datatypes.Distinct().ToList();

        foreach (string s in datatypes)
        {
            MainClass.win._handles.whichdata.AppendText(s);
        }


        for (int dI = probe.numDet; dI < MainClass.win._handles.detectors.Count; dI++)
        {
            MainClass.win._handles.detectors[dI].frame.Sensitive  = false;
            MainClass.win._handles.detectors[dI].vScale.Sensitive = false;
            MainClass.win._handles.detectors[dI].vScale.Value     = 0;
            MainClass.win._handles.detectors[dI].led.Color        = new Gdk.Color(93, 93, 93);
        }
        for (int sI = probe.numSrc; sI < MainClass.win._handles.lasers.Count; sI++)
        {
            MainClass.win._handles.lasers[sI].frame.Sensitive = false;
            for (int wI = 0; wI < MainClass.win._handles.lasers[sI].buttons.Length; wI++)
            {
                MainClass.win._handles.lasers[sI].spinButtons[wI].Sensitive = false;
                MainClass.win._handles.lasers[sI].buttons[wI].Sensitive     = false;
            }
        }

#if ADDLSL
        MainClass.win.dataLSL = new LSL.liblsl.StreamOutlet[MainClass.devices.Length];
        for (int ii = 0; ii < MainClass.devices.Length; ii++)
        {
            int    fs   = MainClass.devices[ii].GetSampleRate();
            string name = string.Format("NIRSRecordIRData_{0}", ii + 1);
            LSL.liblsl.StreamInfo info = new LSL.liblsl.StreamInfo(name, "NIRS", MainClass.win.nirsdata[ii].probe.numChannels,
                                                                   (double)fs, LSL.liblsl.channel_format_t.cf_int32);
            MainClass.win.dataLSL[ii] = new LSL.liblsl.StreamOutlet(info);
        }
#endif

        comboboxdeviceDemo.Active = 0;

        MainClass.win._handles.whichdata.Active = 0;

        MainClass.win.EnableControls(true);


        MainClass.win._handles.SDGplot.QueueDraw();
    }
Пример #4
0
        public static void writeSNIRF(core.Data data, string filename, int nirs_index = -1, int data_index = 0)
        {
            hid_t tmp;
            hid_t fileId = H5F.create(filename, H5F.ACC_TRUNC);
            hid_t IDnirs;

            if (nirs_index == -1)
            {
                IDnirs = H5G.create(fileId, "/nirs");
            }
            else
            {
                IDnirs = H5G.create(fileId, String.Format("/nirs{0}", nirs_index));
            }


            // formatVersion-  [string] = "1.0"
            tmp = nirs.io.AddDataString(fileId, "formatVersion", "1.0");


            // Metadatatags
            hid_t metaIdx = H5G.create(IDnirs, "metaDataTags");

            for (int i = 0; i < data.demographics.Keys.Count; i++)
            {
                tmp = nirs.io.AddDataString(metaIdx, data.demographics.Keys[i],
                                            string.Format("{0}", data.demographics.get(data.demographics.Keys[i])));
            }
            tmp = nirs.io.AddDataString(metaIdx, "SNIRF_createDate", DateTime.Now.ToString("YYYY-MM-dd"));
            tmp = nirs.io.AddDataString(metaIdx, "SNIRF_createTime", DateTime.Now.ToString("HH:mm:ss") + "Z");
            tmp = nirs.io.AddDataString(metaIdx, "LengthUnit", "mm");
            tmp = nirs.io.AddDataString(metaIdx, "TimeUnit", "s");
            if (!data.demographics.Keys.Contains("MeasurementDate"))
            {
                tmp = nirs.io.AddDataString(metaIdx, "MeasurementDate", "?");
            }
            if (!data.demographics.Keys.Contains("MeasurementTime"))
            {
                tmp = nirs.io.AddDataString(metaIdx, "MeasurementDate", "?");
            }
            if (!data.demographics.Keys.Contains("SubjectID"))
            {
                tmp = nirs.io.AddDataString(metaIdx, "SubjectID", "annonymous");
            }
            H5G.close(metaIdx);


            // Probe

            hid_t probeIdx = H5G.create(IDnirs, "probe"); // used to be IDdata


            List <double> lambda = new List <double>();

            for (int i = 0; i < data.probe.numChannels; i++)
            {
                if (!lambda.Contains(data.probe.ChannelMap[i].wavelength))
                {
                    lambda.Add(data.probe.ChannelMap[i].wavelength);
                }
            }
            double[] wav = new double[lambda.Count];
            for (int i = 0; i < lambda.Count; i++)
            {
                wav[i] = lambda[i];
            }
            // data#/probe/wavelengths [numeric]
            tmp = nirs.io.AddDataVector(probeIdx, "wavelengths", wav);

            // TODO data#/probe/wavelengthsEmissions [numeric]

            // data#/probe/sourcePos [numeric]
            tmp = nirs.io.AddDataArray(probeIdx, "sourcePos2D", data.probe.SrcPos);

            // data#/probe/detectorPos [numeric]
            tmp = nirs.io.AddDataArray(probeIdx, "detectorPos2D", data.probe.DetPos);

            if (data.probe.SrcPos3D != null)
            {
                // data#/probe/sourcePos [numeric]
                tmp = nirs.io.AddDataArray(probeIdx, "sourcePos3D", data.probe.SrcPos3D);
            }

            if (data.probe.DetPos3D != null)
            {
                // data#/probe/detectorPos [numeric]
                tmp = nirs.io.AddDataArray(probeIdx, "detectorPos3D", data.probe.DetPos3D);
            }

            // data#/probe/sourceLabels [string]
            if (data.probe.SourceLabels == null)
            {
                data.probe.SourceLabels = new string[data.probe.numSrc];
                for (int i = 0; i < data.probe.numSrc; i++)
                {
                    data.probe.SourceLabels[i] = string.Format("Source-{0}", i + 1);
                }
            }

            for (int i = 0; i < data.probe.numSrc; i++)
            {
                tmp = nirs.io.AddDataString(probeIdx, String.Format("sourceLabels{0}", i),
                                            data.probe.SourceLabels[i]);
            }

            // data#/probe/detectorLabels [string]
            if (data.probe.DetectorLabels == null)
            {
                data.probe.DetectorLabels = new string[data.probe.numDet];
                for (int i = 0; i < data.probe.numDet; i++)
                {
                    data.probe.DetectorLabels[i] = string.Format("Detector-{0}", i + 1);
                }
            }


            for (int i = 0; i < data.probe.numDet; i++)
            {
                tmp = nirs.io.AddDataString(probeIdx, String.Format("detectorLabels{0}", i),
                                            data.probe.DetectorLabels[i]);
            }
            // data#/probe/landmarkLabels [string]
            if (data.probe.LandmarkLabels != null)
            {
                for (int i = 0; i < data.probe.LandmarkLabels.Length; i++)
                {
                    tmp = nirs.io.AddDataString(probeIdx, String.Format("landmarkLabels{0}", i),
                                                data.probe.LandmarkLabels[i]);
                }
            }
            // data#/probe/landmark [numeric array]
            if (data.probe.LandmarkPos != null)
            {
                tmp = nirs.io.AddDataArray(probeIdx, "landmarkPos2D", data.probe.LandmarkPos);
            }



            if (data.probe.LandmarkPos3D != null)
            {
                tmp = nirs.io.AddDataArray(probeIdx, "landmarkPos3D", data.probe.LandmarkPos3D);
            }

            // data#/probe/useLocalIndex [int] = 0
            if (data.probe.uselocalIndex != null)
            {
                tmp = nirs.io.AddDataValue(probeIdx, "useLocalIndex", data.probe.uselocalIndex.Value);
            }
            H5G.close(probeIdx);


            hid_t IDdata = H5G.create(IDnirs, String.Format("data{0}", data_index + 1));

            double[,] d = new double[data.data.Length, data.data[0].Count];
            for (int i = 0; i < data.data.Length; i++)
            {
                for (int j = 0; j < data.data[i].Count; j++)
                {
                    d[i, j] = data.data[i][j];
                }
            }
            tmp = nirs.io.AddDataArray(IDdata, "dataTimeSeries", d);
            // data_#/time [numeric]  time x 1

            double[] time = new double[data.time.Count];
            for (int i = 0; i < data.time.Count; i++)
            {
                time[i] = data.time[i];
            }
            tmp = nirs.io.AddDataVector(IDdata, "time", time);



            // MeasurementList
            hid_t[] IDmeas = new hid_t[data.probe.numChannels];
            for (int ch = 0; ch < data.probe.numChannels; ch++)
            {
                IDmeas[ch] = H5G.create(IDdata, String.Format("measurementList{0}", ch + 1));
                //data_#/measurementList_#/sourceIndex [int; index from 1]
                tmp = nirs.io.AddDataValue(IDmeas[ch], "sourceIndex", data.probe.ChannelMap[ch].sourceindex + 1);
                //data_#/measurementList_#detectorIndex [int; index from 1]
                tmp = nirs.io.AddDataValue(IDmeas[ch], "detectorIndex", data.probe.ChannelMap[ch].detectorindex + 1);
                //data_#/measurementList_#/wavelengthIndex [int; index from 1]
                tmp = nirs.io.AddDataValue(IDmeas[ch], "wavelengthIndex",
                                           lambda.IndexOf(data.probe.ChannelMap[ch].wavelength) + 1);
                //data_#/measurementList_#/dataType [int]
                tmp = nirs.io.AddDataValue(IDmeas[ch], "dataType", (int)data.probe.ChannelMap[ch].datatype);
                //data_#/measurementList_#/dataTypeIndex [int; index from 1]
                tmp = nirs.io.AddDataValue(IDmeas[ch], "dataTypeIndex", data.probe.ChannelMap[ch].dataindex + 1);


                //{optional}
                //data_#/measurementList_#/sourcePower [int]
                if (data.probe.ChannelMap[ch].SourcePower != null)
                {
                    tmp = nirs.io.AddDataValue(IDmeas[ch], "sourcePower", data.probe.ChannelMap[ch].SourcePower.Value);
                }

                //data_#/measurementList_#/detectorGain [int]
                if (data.probe.ChannelMap[ch].DetectorGain != null)
                {
                    tmp = nirs.io.AddDataValue(IDmeas[ch], "detectorGain", data.probe.ChannelMap[ch].DetectorGain.Value);
                }
                //data_#/measurementList_#/moduleIndex [int]
                if (data.probe.ChannelMap[ch].moduleIndex != null)
                {
                    tmp = nirs.io.AddDataValue(IDmeas[ch], "moduleIndex", data.probe.ChannelMap[ch].moduleIndex.Value);
                }


                H5G.close(IDmeas[ch]);
            }
            H5G.close(IDdata);


            hid_t[] IDstim = new hid_t[data.stimulus.Count];
            for (int st = 0; st < data.stimulus.Count; st++)
            {
                IDstim[st] = H5G.create(IDnirs, String.Format("stim{0}", st + 1));
                // data_#/stim#/name [string]

                tmp = nirs.io.AddDataString(IDstim[st], "name",
                                            data.stimulus[st].name);

                // data_#/stim#/data [numeric] <#events x 3>  onset,dur,amp

                int n = data.stimulus[st].onsets.Count;
                double[,] evt = new double[n, 3];

                for (int i = 0; i < n; i++)
                {
                    evt[i, 0] = data.stimulus[st].onsets[i];
                    evt[i, 1] = data.stimulus[st].duration[i];
                    evt[i, 2] = data.stimulus[st].amplitude[i];
                }
                tmp = nirs.io.AddDataArray(IDstim[st], "data", evt);

                H5G.close(IDstim[st]);
            }



            if (data.auxillaries != null)
            {
                hid_t[] IDaux = new hid_t[data.auxillaries.Length];
                for (int st = 0; st < data.auxillaries.Length; st++)
                {
                    double[] daux = new double[data.auxillaries[st].data.Count];
                    double[] taux = new double[data.auxillaries[st].time.Count];
                    for (int i = 0; i < data.auxillaries[st].time.Count; i++)
                    {
                        daux[i] = data.auxillaries[st].data[i];
                        taux[i] = data.auxillaries[st].time[i];
                    }



                    IDaux[st] = H5G.create(IDnirs, String.Format("aux{0}", st + 1));
                    // data_#/aux#/name [string]
                    tmp = nirs.io.AddDataString(IDaux[st], "name", data.auxillaries[st].name);
                    // data_#/aux#/dataTimeSeries
                    tmp = nirs.io.AddDataVector(IDaux[st], "dataTimeSeries", daux);
                    // data_#/aux#/time
                    tmp = nirs.io.AddDataVector(IDaux[st], "time", taux);
                    // data_#/aux#/timeOffset
                    if (data.auxillaries[st].timeOffset != null)
                    {
                        tmp = nirs.io.AddDataValue(IDaux[st], "timeOffset", data.auxillaries[st].timeOffset.Value);
                    }


                    H5G.close(IDaux[st]);
                }
            }

            H5G.close(IDnirs);
            H5F.close(fileId);
            return;
        }
Пример #5
0
    {  // methods devoted to file I/O
        public static core.Data[] readSNIRF(string filename)
        {
            List <nirs.HDF5info> info = nirs.io.SNIRFinfo(filename);

            hid_t  fileId        = H5F.open(filename, H5F.ACC_RDONLY);
            string formatVersion = nirs.io.ReadDataString(fileId, "/formatVersion");


            List <string> dataLst = new List <string>();
            List <string> nirsLst = new List <string>();

            for (int i = 0; i < info.Count; i++)
            {
                if (info[i].field.Contains("/nirs") & info[i].field.Contains("/data"))
                {
                    int cnt = info[i].field.IndexOf("/");

                    cnt = info[i].field.IndexOf("/", cnt + 1);
                    int cnt2 = cnt;
                    cnt = info[i].field.IndexOf("/", cnt + 1);  // find the third instance

                    string str  = info[i].field.Substring(0, cnt);
                    string str2 = info[i].field.Substring(0, cnt2);

                    if (str.Contains("/nirs") & str.Contains("/data"))
                    {
                        if (!dataLst.Contains(str))
                        {
                            dataLst.Add(str);
                        }
                    }
                    if (str2.Contains("/nirs"))
                    {
                        if (!nirsLst.Contains(str2))
                        {
                            nirsLst.Add(str2);
                        }
                    }
                }
            }


            core.Probe[]      probes       = new core.Probe[nirsLst.Count];
            Dictionary[]      demographics = new Dictionary[nirsLst.Count];
            core.Data[]       data         = new core.Data[dataLst.Count];
            List <Stimulus>[] stimuli      = new List <Stimulus> [nirsLst.Count];
            auxillary[][]     aux          = new auxillary[nirsLst.Count][];

            for (int i = 0; i < nirsLst.Count; i++)
            {
                demographics[i] = new Dictionary();
                for (int j = 0; j < info.Count; j++)
                {
                    if (info[j].field.Contains(string.Format("{0}/metaDataTags", nirsLst[i])))
                    {
                        string fld = info[j].field.Substring(string.Format("{0}/metaDataTags", nirsLst[i]).Length + 1);

                        if (nirs.io.IsHDF5String(fileId, info[j].field))
                        {
                            string val = nirs.io.ReadDataString(fileId, info[j].field);
                            demographics[i].set(fld, val);
                        }
                        else
                        {
                            double val = nirs.io.ReadDataValue(fileId, info[j].field);
                            demographics[i].set(fld, val);
                        }
                    }
                }

                //aux events
                List <string> auxList = new List <string>();
                for (int j = 0; j < info.Count; j++)
                {
                    if (info[j].field.Contains(string.Format("{0}/aux", nirsLst[i])) &
                        info[j].field.Contains("/name"))
                    {
                        string str = info[j].field.Substring(0, info[j].field.LastIndexOf("/"));
                        auxList.Add(str);
                    }
                }
                aux[i] = new auxillary[auxList.Count];
                for (int j = 0; j < auxList.Count; j++)
                {
                    double[] daux = nirs.io.ReadDataVector(fileId, string.Format("{0}/dataTimeSeries", auxList[j]));
                    double[] taux = nirs.io.ReadDataVector(fileId, string.Format("{0}/time", auxList[j]));

                    aux[i][j]            = new auxillary();
                    aux[i][j].name       = nirs.io.ReadDataString(fileId, string.Format("{0}/name", auxList[j]));
                    aux[i][j].timeOffset = nirs.io.ReadDataValue(fileId, string.Format("{0}/timeOffset", auxList[j]));
                    aux[i][j].data       = new List <double>();
                    aux[i][j].time       = new List <double>();
                    for (int k = 0; k < daux.Length; k++)
                    {
                        aux[i][j].data.Add(daux[k]);
                        aux[i][j].time.Add(taux[k]);
                    }
                }


                // stim events
                stimuli[i] = new List <Stimulus>();
                for (int j = 0; j < info.Count; j++)
                {
                    if (info[j].field.Contains(string.Format("{0}/stim", nirsLst[i])) &
                        info[j].field.Contains("/name"))
                    {
                        string str      = info[j].field.Substring(0, info[j].field.LastIndexOf("/"));
                        string stimname = nirs.io.ReadDataString(fileId, string.Format("{0}/name", str));

                        double[,] times = nirs.io.ReadDataArray(fileId, string.Format("{0}/data", str));
                        if (times.GetLength(0) == 3 & times.GetLength(1) != 3)
                        {
                            times = nirs.io.ReadDataArray(fileId, string.Format("{0}/data", str), true);
                        }
                        Stimulus stim = new Stimulus();
                        stim.name      = stimname;
                        stim.onsets    = new List <double>();
                        stim.duration  = new List <double>();
                        stim.amplitude = new List <double>();

                        for (int ii = 0; ii < times.GetLength(0); ii++)
                        {
                            stim.onsets.Add(times[ii, 0]);
                            stim.duration.Add(times[ii, 1]);
                            stim.amplitude.Add(times[ii, 2]);
                        }
                        stimuli[i].Add(stim);
                    }
                }


                // read the probe
                probes[i] = new core.Probe();

                double[] wav;
                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/wavelengths", nirsLst[i])))
                {
                    wav = nirs.io.ReadDataVector(fileId, String.Format("{0}/probe/wavelengths", nirsLst[i]));
                }
                else
                {
                    wav = new double[0];
                }


                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/sourcePos2D", nirsLst[i])))
                {
                    probes[i].SrcPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/sourcePos2D", nirsLst[i]));
                    if (probes[i].SrcPos.GetLength(0) == 3 & probes[i].SrcPos.GetLength(1) != 3)
                    {
                        probes[i].SrcPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/sourcePos2D", nirsLst[i]), true);
                    }
                }
                else
                {
                    probes[i].SrcPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/sourcePos", nirsLst[i]));
                    if (probes[i].SrcPos.GetLength(0) == 3 & probes[i].SrcPos.GetLength(1) != 3)
                    {
                        probes[i].SrcPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/sourcePos", nirsLst[i]), true);
                    }
                }

                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/detectorPos2D", nirsLst[i])))
                {
                    probes[i].DetPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/detectorPos2D", nirsLst[i]));
                    if (probes[i].DetPos.GetLength(0) == 3 & probes[i].SrcPos.GetLength(1) != 3)
                    {
                        probes[i].DetPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/detectorPos2D", nirsLst[i]), true);
                    }
                }
                else
                {
                    probes[i].DetPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/detectorPos", nirsLst[i]));
                    if (probes[i].DetPos.GetLength(0) == 3 & probes[i].DetPos.GetLength(1) != 3)
                    {
                        probes[i].DetPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/detectorPos", nirsLst[i]), true);
                    }
                }


                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/landmarkPos2D", nirsLst[i])))
                {
                    probes[i].LandmarkPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/landmarkPos2D", nirsLst[i]));
                    if (probes[i].LandmarkPos.GetLength(0) == 3 & probes[i].LandmarkPos.GetLength(1) != 3)
                    {
                        probes[i].LandmarkPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/landmarkPos2D", nirsLst[i]), true);
                    }
                }
                else if (nirs.io.groupexists(fileId, String.Format("{0}/probe/landmarkPos", nirsLst[i])))
                {
                    probes[i].LandmarkPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/landmarkPos", nirsLst[i]));
                    if (probes[i].LandmarkPos.GetLength(0) == 3 & probes[i].LandmarkPos.GetLength(1) != 3)
                    {
                        probes[i].LandmarkPos = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/landmarkPos", nirsLst[i]), true);
                    }
                }


                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/sourcePos3D", nirsLst[i])))
                {
                    probes[i].SrcPos3D = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/sourcePos3D", nirsLst[i]));
                    if (probes[i].SrcPos3D.GetLength(0) == 3 & probes[i].SrcPos3D.GetLength(1) != 3)
                    {
                        probes[i].SrcPos3D = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/sourcePos3D", nirsLst[i]), true);
                    }
                }
                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/detectorPos3D", nirsLst[i])))
                {
                    probes[i].DetPos3D = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/detectorPos3D", nirsLst[i]));
                    if (probes[i].DetPos3D.GetLength(0) == 3 & probes[i].DetPos3D.GetLength(1) != 3)
                    {
                        probes[i].DetPos3D = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/detectorPos3D", nirsLst[i]), true);
                    }
                }
                if (nirs.io.groupexists(fileId, String.Format("{0}/probe/landmarkPos3D", nirsLst[i])))
                {
                    if (nirs.io.groupexists(fileId, String.Format("{0}/probe/landmarkPos3D", nirsLst[i])))
                    {
                        probes[i].LandmarkPos3D = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/landmarkPos3D", nirsLst[i]));
                        if (probes[i].LandmarkPos3D.GetLength(0) == 3 & probes[i].LandmarkPos3D.GetLength(1) != 3)
                        {
                            probes[i].LandmarkPos3D = nirs.io.ReadDataArray(fileId, String.Format("{0}/probe/landmarkPos3D", nirsLst[i]), true);
                        }
                    }
                }


                probes[i].numDet         = probes[i].DetPos.GetLength(0);
                probes[i].numSrc         = probes[i].SrcPos.GetLength(0);
                probes[i].numWavelengths = wav.Length;


                probes[i].SourceLabels = new string[probes[i].SrcPos.GetLength(0)];
                for (int j = 0; j < probes[i].SrcPos.GetLength(0); j++)
                {
                    if (nirs.io.groupexists(fileId, String.Format("{0}/probe/sourceLabels{1}", nirsLst[i], j + 1)))
                    {
                        probes[i].SourceLabels[j] = nirs.io.ReadDataString(fileId, String.Format("{0}/probe/sourceLabels{1}", nirsLst[i], j + 1));
                    }
                    else
                    {
                        probes[i].SourceLabels[j] = string.Format("Source-{0}", j + 1);
                    }
                }
                probes[i].DetectorLabels = new string[probes[i].DetPos.GetLength(0)];
                for (int j = 0; j < probes[i].DetPos.GetLength(0); j++)
                {
                    if (nirs.io.groupexists(fileId, String.Format("{0}/probe/detectorLabels{1}", nirsLst[i], j + 1)))
                    {
                        probes[i].DetectorLabels[j] = nirs.io.ReadDataString(fileId, String.Format("{0}/probe/detectorLabels{1}", nirsLst[i], j + 1));
                    }
                    else
                    {
                        probes[i].DetectorLabels[j] = string.Format("Detector-{0}", j + 1);
                    }
                }

                if (probes[i].LandmarkPos != null)
                {
                    probes[i].LandmarkLabels = new string[probes[i].LandmarkPos.GetLength(0)];
                    for (int j = 0; j < probes[i].LandmarkPos.GetLength(0); j++)
                    {
                        if (nirs.io.groupexists(fileId, String.Format("{0}/probe/landmarkLabels{1}", nirsLst[i], j + 1)))
                        {
                            probes[i].LandmarkLabels[j] = nirs.io.ReadDataString(fileId, String.Format("{0}/probe/landmarkLabels{1}", nirsLst[i], j + 1));
                        }
                        else
                        {
                            probes[i].LandmarkLabels[j] = string.Format("Landmark-{0}", j + 1);
                        }
                    }
                }
            }

            // Now, load the data
            for (int i = 0; i < dataLst.Count; i++)
            {
                double[] wav = new double[0];

                data[i]             = new core.Data();
                data[i].description = dataLst[i];


                for (int j = 0; j < nirsLst.Count; j++)
                {
                    if (dataLst[i].Contains(string.Format("{0}/data", nirsLst[j])))
                    {
                        data[i].probe        = probes[j];
                        data[i].demographics = demographics[j];

                        if (nirs.io.groupexists(fileId, String.Format("{0}/probe/wavelengths", nirsLst[j])))
                        {
                            wav = nirs.io.ReadDataVector(fileId, String.Format("{0}/probe/wavelengths", nirsLst[j]));
                        }
                        else
                        {
                            wav = new double[0];
                        }

                        data[i].stimulus    = stimuli[j];
                        data[i].auxillaries = aux[i];
                    }
                }

                double[] t = nirs.io.ReadDataVector(fileId, String.Format("{0}/time", dataLst[i]));
                data[i].time = new List <double>();
                for (int ii = 0; ii < t.Length; ii++)
                {
                    data[i].time.Add(t[ii]);
                }


                double[,] d = nirs.io.ReadDataArray(fileId, String.Format("{0}/dataTimeSeries", dataLst[i]));
                if (d.GetLength(0) == t.Length)
                {
                    d = nirs.io.ReadDataArray(fileId, String.Format("{0}/dataTimeSeries", dataLst[i]), true);
                }

                data[i].data = new List <double> [d.GetLength(0)];
                for (int ii = 0; ii < d.GetLength(0); ii++)
                {
                    data[i].data[ii] = new List <double>();
                    for (int jj = 0; jj < d.GetLength(1); jj++)
                    {
                        data[i].data[ii].Add(d[ii, jj]);
                    }
                }

                data[i].numsamples        = data[i].time.Count;
                data[i].probe.numChannels = data[i].data.Length;
                data[i].probe.ChannelMap  = new ChannelMap[data[i].probe.numChannels];

                for (int j = 0; j < data[i].probe.numChannels; j++)
                {
                    data[i].probe.ChannelMap[j]             = new ChannelMap();
                    data[i].probe.ChannelMap[j].sourceindex = (int)nirs.io.ReadDataValue(fileId,
                                                                                         String.Format("{0}/measurementList{1}/sourceIndex", dataLst[i], j + 1)) - 1;
                    data[i].probe.ChannelMap[j].detectorindex = (int)nirs.io.ReadDataValue(fileId,
                                                                                           String.Format("{0}/measurementList{1}/detectorIndex", dataLst[i], j + 1)) - 1;

                    if (wav.Length > 0)
                    {
                        data[i].probe.ChannelMap[j].wavelength = wav[(int)nirs.io.ReadDataValue(fileId,
                                                                                                String.Format("{0}/measurementList{1}/wavelengthIndex", dataLst[i], j + 1)) - 1];
                    }

                    int datatypeIdx = (int)nirs.io.ReadDataValue(fileId, String.Format("{0}/measurementList{1}/dataType", dataLst[i], j + 1));


                    data[i].probe.ChannelMap[j].datatype = (datatype)Enum.ToObject(typeof(datatype), datatypeIdx);

                    if (wav.Length > 0)
                    {
                        data[i].probe.ChannelMap[j].datasubtype = String.Format("{0}nm", data[i].probe.ChannelMap[j].wavelength);
                    }
                    else
                    {
                        data[i].probe.ChannelMap[j].datasubtype = nirs.io.ReadDataString(fileId,
                                                                                         String.Format("{0}/measurementList{1}/dataTypeLabel", dataLst[i], j + 1));
                    }

                    data[i].probe.ChannelMap[j].channelname = String.Format("Src{0}-Det{1}", data[i].probe.ChannelMap[j].sourceindex + 1,
                                                                            data[i].probe.ChannelMap[j].detectorindex + 1);

                    //{optional}
                    if (nirs.io.groupexists(fileId, String.Format("{0}/measurementList{1}/moduleIndex", dataLst[i], j + 1)))
                    {
                        data[i].probe.ChannelMap[j].moduleIndex = (int)nirs.io.ReadDataValue(fileId, String.Format("{0}/measurementList{1}/moduleIndex", dataLst[i], j + 1));
                    }
                    if (nirs.io.groupexists(fileId, String.Format("{0}/measurementList{1}/detectorGain", dataLst[i], j + 1)))
                    {
                        data[i].probe.ChannelMap[j].DetectorGain = nirs.io.ReadDataValue(fileId, String.Format("{0}/measurementList{1}/detectorGain", dataLst[i], j + 1));
                    }
                    if (nirs.io.groupexists(fileId, String.Format("{0}/measurementList{1}/sourcePower", dataLst[i], j + 1)))
                    {
                        data[i].probe.ChannelMap[j].SourcePower = nirs.io.ReadDataValue(fileId, String.Format("{0}/measurementList{1}/sourcePower", dataLst[i], j + 1));
                    }
                }
            }


            return(data);

            // data#/probe/useLocalIndex [int] = 0
        }
Пример #6
0
    {  // methods devoted to file I/O
        /// <subjid>.wl1  - wavelength #1 data
        /// <subjid>.wl2  - wavelength #2 data
        /// <subjid>_config.txt   - config file
        /// <subjid>.evt  - stimulus events(data taken from config file)
        /// <subjid>_probeInfo.mat - probe file
        /// <subjid>.tpl -topology file(data taken from config file)
        ///
        public static core.Data readNIRx(string filename)
        {
            core.Data data = new core.Data();

            filename = filename.Substring(0, filename.IndexOf(".wl1", StringComparison.Ordinal));

            // Read the header file
            List <string> hdrFields = new List <string>();
            List <string> hdrValues = new List <string>();
            string        line;

            System.IO.StreamReader file = new System.IO.StreamReader(filename + ".hdr");
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains("="))
                {
                    int found = line.IndexOf("=", StringComparison.Ordinal);
                    hdrFields.Add(line.Substring(0, found));
                    string value = line.Substring(found + 1);
                    if (value.Contains("#"))
                    {
                        value = "";
                        while ((line = file.ReadLine()) != null)
                        {
                            if (line.Contains("#"))
                            {
                                break;
                            }
                            value = value + "\r" + line;
                        }
                    }
                    if (value.Contains("\""))
                    {
                        value = value.Substring(1, value.Length - 2);
                    }
                    hdrValues.Add(value);
                }
            }
            file.Close();

            string targetDirectory = Path.GetDirectoryName(filename + ".hdr");

            string[] fileEntries = Directory.GetFiles(targetDirectory);
            string   probeFile   = Path.Combine(targetDirectory, "Standard_probeInfo.mat");

            foreach (string i in fileEntries)
            {
                if (i.Contains("probeInfo.mat"))
                {
                    probeFile = Path.Combine(targetDirectory, i);
                    break;
                }
            }


            // Now, read the Probe_info.mat file
            MatFileReader mfr       = new MatFileReader(probeFile);
            MLStructure   probeInfo = (mfr.Content["probeInfo"] as MLStructure);
            MLStructure   probes    = (probeInfo["probes"] as MLStructure);

            MLDouble coords_s2 = (probes["coords_s2"] as MLDouble);
            MLDouble coords_d2 = (probes["coords_d2"] as MLDouble);
            MLDouble coords_c2 = (probes["coords_c2"] as MLDouble);

            double[][] srcpos  = coords_s2.GetArray();
            double[][] detpos  = coords_d2.GetArray();
            double[][] landpos = coords_c2.GetArray();

            // TODO read all the 3D stuff too
            MLDouble coords_s3 = (probes["coords_s3"] as MLDouble);
            MLDouble coords_d3 = (probes["coords_d3"] as MLDouble);
            MLDouble coords_c3 = (probes["coords_c3"] as MLDouble);

            double[][] srcpos3D  = coords_s3.GetArray();
            double[][] detpos3D  = coords_d3.GetArray();
            double[][] landpos3D = coords_c3.GetArray();


            data.probe.numSrc = srcpos.Length;
            data.probe.numDet = detpos.Length;


            data.probe.DetPos         = new double[detpos.Length, 3];
            data.probe.DetectorLabels = new string[detpos.Length];
            for (int i = 0; i < detpos.Length; i++)
            {
                data.probe.DetPos[i, 0]      = (float)detpos[i][0];
                data.probe.DetPos[i, 1]      = (float)detpos[i][1];
                data.probe.DetPos[i, 2]      = 0;
                data.probe.DetectorLabels[i] = string.Format("Detector-{0}", i + 1);
            }

            data.probe.SrcPos       = new double[srcpos.Length, 3];
            data.probe.SourceLabels = new string[srcpos.Length];
            for (int i = 0; i < srcpos.Length; i++)
            {
                data.probe.SrcPos[i, 0]    = (float)srcpos[i][0];
                data.probe.SrcPos[i, 1]    = (float)srcpos[i][1];
                data.probe.SrcPos[i, 2]    = 0;
                data.probe.SourceLabels[i] = string.Format("Source-{0}", i + 1);
            }

            data.probe.LandmarkPos    = new double[landpos.Length, 3];
            data.probe.LandmarkLabels = new string[landpos.Length];
            for (int i = 0; i < landpos.Length; i++)
            {
                data.probe.LandmarkPos[i, 0] = (float)landpos[i][0];
                data.probe.LandmarkPos[i, 1] = (float)landpos[i][1];
                data.probe.LandmarkPos[i, 2] = 0;
                data.probe.LandmarkLabels[i] = string.Format("Landmark(temp)-{0}", i + 1);
            }


            data.probe.DetPos3D = new double[detpos3D.Length, 3];
            for (int i = 0; i < detpos3D.Length; i++)
            {
                data.probe.DetPos3D[i, 0] = (double)detpos3D[i][0];
                data.probe.DetPos3D[i, 1] = (double)detpos3D[i][1];
                data.probe.DetPos3D[i, 2] = (double)detpos3D[i][1];
            }

            data.probe.SrcPos3D = new double[srcpos3D.Length, 3];
            for (int i = 0; i < srcpos3D.Length; i++)
            {
                data.probe.SrcPos3D[i, 0] = (double)srcpos3D[i][0];
                data.probe.SrcPos3D[i, 1] = (double)srcpos3D[i][1];
                data.probe.SrcPos3D[i, 2] = (double)srcpos3D[i][2];
            }
            data.probe.LandmarkPos3D = new double[landpos.Length, 3];
            for (int i = 0; i < landpos.Length; i++)
            {
                data.probe.LandmarkPos3D[i, 0] = (float)landpos3D[i][0];
                data.probe.LandmarkPos3D[i, 1] = (float)landpos3D[i][1];
                data.probe.LandmarkPos3D[i, 2] = (float)landpos3D[i][2];
            }
            data.probe.isregistered = true;



            int LambdaIdx = hdrFields.IndexOf("Wavelengths");

            string[] lam    = hdrValues[LambdaIdx].Split('\t');
            double[] lambda = new double[lam.Length];
            for (int i = 0; i < lam.Length; i++)
            {
                lambda[i] = Convert.ToDouble(lam[i]);
            }

            int SDmaskIdx = hdrFields.IndexOf("S-D-Mask");

            string[] mask = hdrValues[SDmaskIdx].Split('\r');
            bool[,] SDMask = new bool[data.probe.numSrc, data.probe.numDet];
            for (int i = 1; i < data.probe.numSrc + 1; i++)
            {
                string[] mask2 = mask[i].Split('\t');
                for (int j = 0; j < data.probe.numDet; j++)
                {
                    SDMask[i - 1, j] = false;
                    if (mask2[j].Contains("1"))
                    {
                        SDMask[i - 1, j] = true;
                    }
                }
            }

            int cnt = 0;

            for (int i = 0; i < SDMask.GetLength(0); i++)
            {
                for (int j = 0; j < SDMask.GetLength(1); j++)
                {
                    if (SDMask[i, j])
                    {
                        cnt++;
                    }
                }
            }


            data.probe.ChannelMap = new ChannelMap[cnt * lambda.Length];
            cnt = 0;

            List <int> ChanIdx = new List <int>();
            int        cnt2    = 0;

            for (int w = 0; w < lambda.Length; w++)
            {
                for (int i = 0; i < SDMask.GetLength(0); i++)
                {
                    for (int j = 0; j < SDMask.GetLength(1); j++)
                    {
                        if (SDMask[i, j])
                        {
                            data.probe.ChannelMap[cnt]               = new ChannelMap();
                            data.probe.ChannelMap[cnt].sourceindex   = i;
                            data.probe.ChannelMap[cnt].detectorindex = j;
                            data.probe.ChannelMap[cnt].channelname   = String.Format("Src{0}-Det{1}",
                                                                                     data.probe.ChannelMap[cnt].sourceindex + 1,
                                                                                     data.probe.ChannelMap[cnt].detectorindex + 1);
                            data.probe.ChannelMap[cnt].wavelength  = lambda[w];
                            data.probe.ChannelMap[cnt].datasubtype = String.Format("{0}nm", data.probe.ChannelMap[cnt].wavelength);
                            cnt++;
                            if (w == 0)
                            {
                                ChanIdx.Add(cnt2);
                            }
                        }
                        cnt2++;
                    }
                }
            }

            data.probe.numChannels = data.probe.ChannelMap.Length;
            data.probe.measlistAct = new bool[data.probe.numChannels];
            for (int i = 0; i < data.probe.numChannels; i++)
            {
                data.probe.measlistAct[i] = false;
            }
            data.probe.measlistAct[0] = true;



            // read the actual data


            System.IO.StreamReader file2 = new System.IO.StreamReader(filename + ".wl1");
            string lines = file2.ReadToEnd();

            string[] tpts = lines.Split('\r');
            data.data = new List <double> [data.probe.numChannels];
            for (int i = 0; i < data.data.Length; i++)
            {
                data.data[i] = new List <double>();
            }
            for (int i = 0; i < tpts.Length - 1; i++)
            {
                if (tpts[i].Contains("\n"))
                {
                    tpts[i] = tpts[i].Substring(1, tpts[i].Length - 1);
                }
                string[] pts = tpts[i].Split(' ');
                for (int j = 0; j < ChanIdx.Count; j++)
                {
                    data.data[j].Add(Convert.ToDouble(pts[ChanIdx[j]]));
                }
            }
            file2 = new System.IO.StreamReader(filename + ".wl2");
            lines = file2.ReadToEnd();
            tpts  = lines.Split('\r');
            for (int i = 0; i < tpts.Length - 1; i++)
            {
                if (tpts[i].Contains("\n"))
                {
                    tpts[i] = tpts[i].Substring(1, tpts[i].Length - 1);
                }
                string[] pts = tpts[i].Split(' ');
                for (int j = 0; j < ChanIdx.Count; j++)
                {
                    data.data[j + data.probe.numChannels / 2].Add(Convert.ToDouble(pts[ChanIdx[j]]));
                }
            }

            // finally, the time vector
            int fsIdx = hdrFields.IndexOf("SamplingRate");

            double fs = Convert.ToDouble(hdrValues[fsIdx]);

            data.numsamples = data.data.GetLength(1);
            data.time       = new List <double>();
            for (int i = 0; i < data.numsamples; i++)
            {
                data.time.Add(i / fs);
            }


            // TODO add stimulus information
            int EventIdx = hdrFields.IndexOf("Events");

            string[] eventline = hdrValues[EventIdx].Split('\r');
            double[,] events = new double[eventline.Length - 1, 3];

            for (int i = 1; i < eventline.Length; i++)
            {
                string[] eventline2 = eventline[i].Split('\t');
                for (int j = 0; j < 2; j++)
                {
                    events[i - 1, j] = Convert.ToDouble(eventline2[j]);
                }
            }
            List <double> uniqEvents = new List <double>();

            int[] uniqEventCount = new int[events.GetLength(0)];
            for (int i = 0; i < events.GetLength(0); i++)
            {
                uniqEventCount[i] = 0;
                if (!uniqEvents.Contains(events[i, 1]))
                {
                    uniqEvents.Add(events[i, 1]);
                }
                int ii = uniqEvents.IndexOf(events[i, 1]);
                uniqEventCount[ii]++;
            }

            data.stimulus = new List <Stimulus>();
            for (int i = 0; i < uniqEvents.Count; i++)
            {
                Stimulus stimulus = new Stimulus();

                stimulus.name      = String.Format("Event-{0}", uniqEvents[i]);
                stimulus.onsets    = new List <double>();
                stimulus.duration  = new List <double>();
                stimulus.amplitude = new List <double>();

                int n = 0;
                for (int j = 0; j < events.GetLength(0); j++)
                {
                    if (Math.Abs(events[j, 1] - uniqEvents[i]) < 0.0001)
                    {
                        stimulus.onsets.Add(events[j, 0]);
                        stimulus.duration.Add(1);
                        stimulus.amplitude.Add(1);
                        n++;
                    }
                }
                data.stimulus.Add(stimulus);
            }



            //add demographics info
            List <string> Fields = new List <string>()
            {
                "Subject", "notes", "FileName", "Date", "Device", "Source",
                "Mod", "APD", "NIRStar", "Mod Amp"
            };

            for (int i = 0; i < Fields.Count; i++)
            {
                int idx = hdrFields.IndexOf(Fields[i]);
                if (idx > -1)
                {
                    data.demographics.set(Fields[i], hdrValues[idx]);
                }
            }


            data.description = targetDirectory;


            return(data);
        }
Пример #7
0
        public static void writeDOTnirs(core.Data data, string filename, int startIdx = 0, int endIdx = Int32.MaxValue)
        {
            // Store the data into the *.nirs matlab format
            try
            {
                int numsamples = data.numsamples;
                int numch      = data.probe.numChannels;
                int naux       = data.auxillaries.Length;

                numsamples = Math.Min(endIdx - startIdx, numsamples - startIdx);


                // save the structure as mat file using MatFileWriter
                List <MLArray> mlList = new List <MLArray>();

                double[][] d   = new double[numsamples][];
                double[][] t   = new double[numsamples][];
                double[][] aux = new double[numsamples][];

                for (int j = startIdx; j < startIdx + numsamples; j++)
                {
                    double[] dloc = new double[numch];

                    for (int i = 0; i < numch; i++)
                    {
                        dloc[i] = data.data[i][j];
                    }
                    try
                    {
                        if (naux > 0)
                        {
                            double[] aloc = new double[naux];
                            for (int ii = 0; ii < naux; ii++)
                            {
                                aloc[ii] = data.auxillaries[ii].data[j];
                            }
                            aux[j - startIdx] = aloc;
                        }
                        else
                        {
                            double[] aa = new double[1];
                            aa[0]             = 0;
                            aux[j - startIdx] = aa;
                        }
                    }
                    catch
                    {
                        //  Console.WriteLine("error writing aux");
                        double[] aa = new double[1];
                        aa[0]             = 0;
                        aux[j - startIdx] = aa;
                    }
                    double[] tt = new double[1];
                    double[] ss = new double[1];
                    ss[0]           = 0;
                    tt[0]           = data.time[j];
                    t[j - startIdx] = tt;
                    d[j - startIdx] = dloc;
                }

                MLDouble mldata = new MLDouble("d", d);
                mlList.Add(mldata);
                MLDouble mlaux = new MLDouble("aux", aux);
                mlList.Add(mlaux);
                MLDouble mltime = new MLDouble("t", t);
                mlList.Add(mltime);

                double[][] s = new double[numsamples][];

                for (int j = startIdx; j < startIdx + numsamples; j++)
                {
                    double[] dloc     = new double[data.stimulus.Count];
                    double   thistime = data.time[j];
                    for (int i = 0; i < data.stimulus.Count; i++)
                    {
                        dloc[i] = 0;
                        for (int k = 0; k < data.stimulus[i].onsets.Count; k++)
                        {
                            double onset = data.stimulus[i].onsets[k];
                            double dur   = data.stimulus[i].duration[k];
                            if (thistime >= onset & thistime <= onset + dur)
                            {
                                dloc[i] = data.stimulus[i].amplitude[k];
                            }
                        }
                    }
                    s[j - startIdx] = dloc;
                }


                MLDouble mls = new MLDouble("s", s);
                mlList.Add(mls);

                MLCell condNames = new MLCell("CondNames", new int[] { 1, data.stimulus.Count });
                for (int i = 0; i < data.stimulus.Count; i++)
                {
                    condNames[0, i] = new MLChar(null, data.stimulus[i].name);
                }
                mlList.Add(condNames);



                // Probe
                MLStructure mlSD = new MLStructure("SD", new int[] { 1, 1 });


                List <string> datasubtype = new List <string>();
                for (int i = 0; i < data.probe.numChannels; i++)
                {
                    if (!datasubtype.Contains(data.probe.ChannelMap[i].datasubtype))
                    {
                        datasubtype.Add(data.probe.ChannelMap[i].datasubtype);
                    }
                }
                double[] lambda = new double[datasubtype.Count];
                for (int i = 0; i < data.probe.numChannels; i++)
                {
                    lambda[datasubtype.IndexOf(data.probe.ChannelMap[i].datasubtype)] = data.probe.ChannelMap[i].wavelength;
                }

                double[][] srcpos = new double[data.probe.numSrc][];
                for (int j = 0; j < data.probe.numSrc; j++)
                {
                    double[] slo = new double[3];
                    for (int i = 0; i < 2; i++)
                    {
                        slo[i] = data.probe.SrcPos[j, i];
                    }
                    slo[2]    = 0;
                    srcpos[j] = slo;
                }


                double[][] detpos = new double[data.probe.numDet][];
                for (int j = 0; j < data.probe.numDet; j++)
                {
                    double[] dlo = new double[3];
                    for (int i = 0; i < 2; i++)
                    {
                        dlo[i] = data.probe.DetPos[j, i];
                    }
                    dlo[2]    = 0;
                    detpos[j] = dlo;
                }



                mlSD["NumDet", 0] = new MLDouble("", new double[] { data.probe.numDet }, 1);
                mlSD["NumSrc", 0] = new MLDouble("", new double[] { data.probe.numSrc }, 1);
                mlSD["Lambda", 0] = new MLDouble("", lambda, 1);
                mlSD["SrcPos", 0] = new MLDouble("", srcpos);
                mlSD["DetPos", 0] = new MLDouble("", detpos);

                if (data.probe.isregistered)
                {
                    double[][] srcpos3d = new double[data.probe.numSrc][];
                    for (int j = 0; j < data.probe.numSrc; j++)
                    {
                        double[] slo = new double[3];
                        for (int i = 0; i < 3; i++)
                        {
                            slo[i] = data.probe.SrcPos3D[j, i];
                        }
                        srcpos3d[j] = slo;
                    }


                    double[][] detpos3d = new double[data.probe.numDet][];
                    for (int j = 0; j < data.probe.numDet; j++)
                    {
                        double[] dlo = new double[3];
                        for (int i = 0; i < 3; i++)
                        {
                            dlo[i] = data.probe.DetPos3D[j, i];
                        }
                        detpos3d[j] = dlo;
                    }
                    mlSD["SrcPos3D", 0] = new MLDouble("", srcpos3d);
                    mlSD["DetPos3D", 0] = new MLDouble("", detpos3d);
                }

                // fixes for HOMER2
                mlSD["SpatialUnit"] = new MLChar("", "mm");



                // Add demographics as a struct
                MLStructure demo = new MLStructure("demographics", new int[] { 1, 1 });
                for (int i = 0; i < data.demographics.Keys.Count; i++)
                {
                    object val    = data.demographics.get(data.demographics.Keys[i]);
                    string valstr = string.Format("{0}", val);

                    demo[data.demographics.Keys[i], 0] = new MLChar("", valstr);
                }
                mlList.Add(demo);

                double[][] ml = new double[data.probe.numChannels][];
                for (int i = 0; i < data.probe.numChannels; i++)
                {
                    double[] m = new double[4];
                    m[0]  = data.probe.ChannelMap[i].sourceindex + 1;
                    m[1]  = data.probe.ChannelMap[i].detectorindex + 1;
                    m[2]  = 0;
                    m[3]  = 1 + datasubtype.IndexOf(data.probe.ChannelMap[i].datasubtype);
                    ml[i] = m;
                }

                MLDouble mlml = new MLDouble("ml", ml);
                mlList.Add(mlml);


                mlSD["MeasList", 0] = new MLDouble("", ml);
                mlList.Add(mlSD);


                MLStructure mlStim = new MLStructure("StimDesign", new int[] { data.stimulus.Count, 1 });
                for (int i = 0; i < data.stimulus.Count; i++)
                {
                    mlStim["name", i] = new MLChar("", data.stimulus[i].name);


                    double[] onset = new double[data.stimulus[i].onsets.Count];
                    for (int ii = 0; ii < data.stimulus[i].onsets.Count; ii++)
                    {
                        onset[ii] = data.stimulus[i].onsets[ii];
                    }
                    double[] dur = new double[data.stimulus[i].duration.Count];
                    for (int ii = 0; ii < data.stimulus[i].duration.Count; ii++)
                    {
                        dur[ii] = data.stimulus[i].duration[ii];
                    }
                    double[] amp = new double[data.stimulus[i].amplitude.Count];
                    for (int ii = 0; ii < data.stimulus[i].amplitude.Count; ii++)
                    {
                        amp[ii] = data.stimulus[i].amplitude[ii];
                    }


                    mlStim["onset", i] = new MLDouble("", onset, 1);
                    mlStim["dur", i]   = new MLDouble("", dur, 1);
                    mlStim["amp", i]   = new MLDouble("", amp, 1);
                }
                if (data.stimulus.Count > 0)
                {
                    mlList.Add(mlStim);
                }

                new MatFileWriter(filename, mlList, false);
            }
            catch
            {
                Console.WriteLine("Unable to save .nirs file");
            }
            return;
        }
Пример #8
0
    {  // methods devoted to file I/O
        public static core.Data readDOTnirs(string filename)
        {
            core.Data data = new core.Data();

            MatFileReader mfr = new MatFileReader(filename);

            MLDouble mlD = (mfr.Content["d"] as MLDouble);

            if (mlD != null)
            {
                double[][] d = mlD.GetArray();

                double[] dd = d[0];
                data.data = new List <double> [dd.Length];
                for (int i = 0; i < dd.Length; i++)
                {
                    data.data[i] = new List <double>();
                }


                for (int i = 0; i < d.Length; i++)
                {
                    double[] dd3 = d[i];
                    for (int j = 0; j < dd3.Length; j++)
                    {
                        data.data[j].Add(dd3[j]);
                    }
                }
            }
            MLDouble mlT = (mfr.Content["t"] as MLDouble);

            if (mlT != null)
            {
                double[][] t = mlT.GetArray();
                data.time = new List <double>();
                for (int i = 0; i < t.Length; i++)
                {
                    data.time.Add(t[i][0]);
                }
            }
            data.numsamples = data.time.Count;

            double[][]  lambda;
            MLStructure SD = (mfr.Content["SD"] as MLStructure);

            MLDouble MLlambda = null;

            if (SD.Keys.Contains("Lambda"))
            {
                MLlambda = (SD["Lambda"] as MLDouble);
            }
            else if (SD.Keys.Contains("lambda"))
            {
                MLlambda = (SD["lambda"] as MLDouble);
            }

            MLDouble MLsrcpos = null;

            if (SD.Keys.Contains("SrcPos"))
            {
                MLsrcpos = (SD["SrcPos"] as MLDouble);
            }
            else if (SD.Keys.Contains("srcpos"))
            {
                MLsrcpos = (SD["srcpos"] as MLDouble);
            }

            MLDouble MLdetpos = null;

            if (SD.Keys.Contains("DetPos"))
            {
                MLdetpos = (SD["DetPos"] as MLDouble);
            }
            else if (SD.Keys.Contains("detpos"))
            {
                MLdetpos = (SD["detpos"] as MLDouble);
            }

            if (MLdetpos != null)
            {
                double[][] detpos = MLdetpos.GetArray();
                data.probe.DetPos         = new double[detpos.Length, 3];
                data.probe.DetectorLabels = new string[detpos.Length];

                for (int i = 0; i < detpos.Length; i++)
                {
                    data.probe.DetPos[i, 0] = (float)detpos[i][0];
                    data.probe.DetPos[i, 1] = (float)detpos[i][1];
                    data.probe.DetPos[i, 2] = (float)detpos[i][2];

                    data.probe.DetectorLabels[i] = String.Format("Detector-{0}", +1);
                }
                data.probe.numDet = detpos.Length;
            }

            if (MLsrcpos != null)
            {
                double[][] srcpos = MLsrcpos.GetArray();
                data.probe.SrcPos       = new double[srcpos.Length, 3];
                data.probe.SourceLabels = new string[srcpos.Length];
                for (int i = 0; i < srcpos.Length; i++)
                {
                    data.probe.SrcPos[i, 0]    = (float)srcpos[i][0];
                    data.probe.SrcPos[i, 1]    = (float)srcpos[i][1];
                    data.probe.SrcPos[i, 2]    = (float)srcpos[i][2];
                    data.probe.SourceLabels[i] = String.Format("Source-{0}", i + 1);
                }
                data.probe.numSrc = srcpos.Length;
            }


            if (MLlambda != null)
            {
                lambda = MLlambda.GetArray();



                MLDouble mlMeasList = (mfr.Content["ml"] as MLDouble);
                if (mlMeasList != null)
                {
                    double[][] ml = mlMeasList.GetArray();
                    data.probe.ChannelMap = new ChannelMap[ml.Length];
                    for (int i = 0; i < ml.Length; i++)
                    {
                        data.probe.ChannelMap[i]               = new ChannelMap();
                        data.probe.ChannelMap[i].sourceindex   = (int)ml[i][0] - 1;
                        data.probe.ChannelMap[i].detectorindex = (int)ml[i][1] - 1;
                        data.probe.ChannelMap[i].channelname   = String.Format("Src{0}-Det{1}",
                                                                               data.probe.ChannelMap[i].sourceindex + 1,
                                                                               data.probe.ChannelMap[i].detectorindex + 1);
                        data.probe.ChannelMap[i].wavelength  = lambda[0][(int)ml[i][3] - 1];
                        data.probe.ChannelMap[i].datasubtype = String.Format("{0}nm", data.probe.ChannelMap[i].wavelength);
                    }
                }
            }

            data.probe.numChannels = data.probe.ChannelMap.Length;
            data.probe.measlistAct = new bool[data.probe.numChannels];
            for (int i = 0; i < data.probe.numChannels; i++)
            {
                data.probe.measlistAct[i] = false;
            }
            data.probe.measlistAct[0] = true;

            // TODO add stimulus information
            // TODO add 3D probe information
            // TODO add demographics info
            // TODO add auxillary information
            // TODO exceptipon catches for case-sensitive variable names

            data.description = filename;


            return(data);
        }