protected void MarkStim(object sender, EventArgs e) { if (!MainClass.devices[0].isrunning()) { return; } double time = nirsdata[0].time[nirsdata[0].time.Count - 1]; string condname = comboboxentry_stimtype.ActiveText; #if ADDLSL if (checkbutton_LSLStimOutlet.Active) { string[] stim = new string[2]; stim[0] = string.Format("{0}:{1}", condname, "Event"); stim[1] = string.Format("{0}", time); stimulusLSL.push_sample(stim); } #endif int index = 0; bool found = false; nirs.Stimulus ev = new nirs.Stimulus();; for (int i = 0; i < nirsdata[0].stimulus.Count; i++) { if (nirsdata[0].stimulus[i].name.Equals(condname)) { ev = nirsdata[0].stimulus[i]; ev.onsets.Add(time); ev.amplitude.Add(1); ev.duration.Add(1); found = true; nirsdata[0].stimulus[i] = ev; index = i; } } if (!found) { ev = new nirs.Stimulus { onsets = new List <double>(), duration = new List <double>(), amplitude = new List <double>(), name = condname }; ev.onsets.Add(time); ev.amplitude.Add(1); ev.duration.Add(1); nirsdata[0].stimulus.Add(ev); index = ev.duration.Count; } _handles.stimListStore.AppendValues(condname, ev.onsets[ev.onsets.Count - 1], ev.duration[ev.duration.Count - 1], ev.amplitude[ev.amplitude.Count - 1]); label_numstim.Text = string.Format("Marks: {0}", ev.amplitude.Count); _handles.StimTree.QueueDraw(); }
private void EditStimTable(object sender, Gtk.EditedArgs args, string type) { Gtk.TreeIter iter; _handles.stimListStore.GetIter(out iter, new TreePath(args.Path)); string Oldname = (string)_handles.stimListStore.GetValue(iter, 0); double Oldonset = (double)_handles.stimListStore.GetValue(iter, 1); double Olddur = (double)_handles.stimListStore.GetValue(iter, 2); double Oldamp = (double)_handles.stimListStore.GetValue(iter, 3); string name = (string)_handles.stimListStore.GetValue(iter, 0); double onset = (double)_handles.stimListStore.GetValue(iter, 1); double dur = (double)_handles.stimListStore.GetValue(iter, 2); double amp = (double)_handles.stimListStore.GetValue(iter, 3); _handles.stimListStore.Remove(ref iter); if (type.Equals("name")) { name = args.NewText; } else if (type.Equals("onset")) { onset = Convert.ToDouble(args.NewText); } else if (type.Equals("dur")) { dur = Convert.ToDouble(args.NewText); } else if (type.Equals("amp")) { amp = Convert.ToDouble(args.NewText); } _handles.stimListStore.AppendValues(name, onset, dur, amp); _handles.stimListStore.SetSortColumnId(1, SortType.Ascending); nirs.Stimulus ev; nirs.Stimulus ev2 = new nirs.Stimulus();; for (int i = 0; i < nirsdata[0].stimulus.Count; i++) { ev = nirsdata[0].stimulus[i]; if (ev.name == Oldname) { for (int j = 0; j < ev.onsets.Count; j++) { if (ev.onsets[j] == Oldonset) { if (type.Equals("name")) { ev.onsets.RemoveAt(j); ev.duration.RemoveAt(j); ev.amplitude.RemoveAt(j); nirsdata[0].stimulus[i] = ev; bool found = false; for (int k = 0; k < nirsdata[0].stimulus.Count; k++) { ev2 = nirsdata[0].stimulus[k]; if (ev2.name == name) { ev2.amplitude.Add(amp); ev2.duration.Add(dur); ev2.onsets.Add(onset); nirsdata[0].stimulus[k] = ev2; found = true; } } if (!found) { ev2.name = name; ev2.amplitude = new List <double>(); ev2.duration = new List <double>(); ev2.onsets = new List <double>(); ev2.amplitude.Add(amp); ev2.duration.Add(dur); ev2.onsets.Add(onset); nirsdata[0].stimulus.Add(ev2); } } else { ev.onsets[j] = onset; ev.duration[j] = dur; ev.amplitude[j] = amp; nirsdata[0].stimulus[i] = ev; } } } } } drawingarea_Data.QueueDraw(); drawingarea_Data2.QueueDraw(); return; }
{ // 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 }
{ // 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); }