private static nirs.core.Probe loadSDprobe(string file) { MatFileReader mfr = new MatFileReader(file); double[][] lambda2; 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); } lambda2 = MLlambda.GetArray(); int[] lambda = new int[lambda2[0].Length]; for (int i = 0; i < lambda2[0].Length; i++) { lambda[i] = (int)lambda2[0][i]; } return(loadSDprobe(file, lambda)); }
public (List <double> time, List <double> gyrox, List <double> gyroy, List <double> gyroz, List <double> accx, List <double> accy, List <double> accz) Load() { IMatFile matFile; using (var fileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open)) { var reader = new MatFileReader(fileStream); matFile = reader.Read(); } IArray itime = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["time", 0]; IArray igyrox = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["gyrox", 0]; IArray igyroy = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["gyroy", 0]; IArray igyroz = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["gyroz", 0]; IArray iaccx = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["accx", 0]; IArray iaccy = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["accy", 0]; IArray iaccz = (((matFile["data"].Value as IStructureArray)["raw", 0] as IStructureArray)["imu0", 0] as IStructureArray)["accz", 0]; List <double> time = itime.ConvertToDoubleArray().ToList(); List <double> gyrox = igyrox.ConvertToDoubleArray().ToList(); List <double> gyroy = igyroy.ConvertToDoubleArray().ToList(); List <double> gyroz = igyroz.ConvertToDoubleArray().ToList(); List <double> accx = iaccx.ConvertToDoubleArray().ToList(); List <double> accy = iaccy.ConvertToDoubleArray().ToList(); List <double> accz = iaccz.ConvertToDoubleArray().ToList(); return(time, gyrox, gyroy, gyroz, accx, accy, accz); }
private void loadDSL() { OpenFileDialog fo = new OpenFileDialog(); fo.Filter = "MATLAB file (*.mat)|*.mat"; if (fo.ShowDialog() == DialogResult.OK) { matfile = fo.FileName; try { MatFileReader r = new MatFileReader(fo.FileName); Dictionary <string, MLArray> content = r.Content; if (content["DSL"] is MLStructure) { MLStructure matStr = (MLStructure)content["DSL"]; for (int i = 0; i < matStr.N; i++) { //MLDouble attack = (MLDouble)matStr["attack\0",i]; MLDouble attack = (MLDouble)matStr["@", i]; //MLDouble attack = (MLDouble)matStr["", 0]; } } } catch (Exception ex) { MessageBox.Show("Error reading .MAT file" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("You did not select a file! Using current parameter values", "No selection", MessageBoxButtons.OK, MessageBoxIcon.Information); Filename = ""; } toolStripStatusLabel1.Text = "Loaded DSL prescription " + Filename; }
/// <summary> /// Read and parse data from a .mat file. /// </summary> /// <param name="stream">Input stream.</param> /// <returns>Parsed contents of the file.</returns> protected override IMatFile ReadDataFromStream(Stream stream) { var matFileReader = new MatFileReader(stream); var matFile = matFileReader.Read(); return(matFile); }
void btnRead_Click(object sender, EventArgs e) { DialogResult dRes = openFileDialog.ShowDialog(); if (dRes == DialogResult.OK) { var fileName = openFileDialog.FileName; txtOutput.Text = txtOutput.Text + "Attempting to read the file '" + fileName + "'..."; try { var mfr = new MatFileReader(fileName); txtOutput.Text += "Done!\nMAT-file contains the following:\n"; txtOutput.Text += mfr.MatFileHeader + "\n"; foreach (var mla in mfr.Data) { txtOutput.Text = txtOutput.Text + mla.ContentToString() + "\n"; } } catch (IOException) { txtOutput.Text = txtOutput.Text + "Invalid MAT-file!\n"; MessageBox.Show("Invalid binary MAT-file! Please select a valid binary MAT-file.", "Invalid MAT-file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
public void Open() { using (var fileStream = new FileStream(_FileName, FileMode.Open)) { var reader = new MatFileReader(fileStream); _MatFile = reader.Read(); } }
public WiderFaces(string pathImages, string pathMat) { WiderFaces._pathMat = pathMat; this._pathImages = pathImages; var mat = new MatFileReader(_pathMat); _allFiles = mat.Content["file_list"] as MLCell; _allBoxes = mat.Content["face_bbx_list"] as MLCell; }
private double[][] LoadDict(string dictionaryPath) { // Read in dictionary. MatFileReader mfr = new MatFileReader(dictionaryPath); MLDouble ml = mfr.Content["dic"] as MLDouble; this.dict_dimension = ml.Dimensions; return(ml.GetArray()); }
public static int GetInt(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsInt32) { throw new InvalidCastException("data is not of type Int32"); } var n = marray.Size; var darray = (MLInt32)marray; return(darray.GetReal(0)); }
public static UInt64 GetUInt64(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsUInt64) { throw new InvalidCastException("data is not of type UInt64"); } var n = marray.Size; var darray = (MLUInt64)marray; return(darray.GetReal(0)); }
public static double GetDouble(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsDouble) { throw new InvalidCastException("data is not of type double"); } var n = marray.Size; var darray = (MLDouble)marray; return(darray.GetReal(0)); }
public void RoundTrip(string matFileName) { var reader = new MatFileReader(matFileName); _tempFileName = Path.GetTempFileName(); var writer = new MatFileWriter(_tempFileName, reader.Data, false); var roundTrip = new MatFileReader(_tempFileName); foreach (var(mla1, mla2) in reader.Data.Zip(roundTrip.Data, (mla1, mla2) => (mla1: mla1, mla2 :mla2))) { Compare(mla1, mla2); } }
public void TrainModel(string featureFilePath) { MatFileReader mfr = new MatFileReader(featureFilePath); MLSingle ml = mfr.Content["rgbdfea"] as MLSingle; int[] dimension = ml.Dimensions; double[][] mlArray = MatrixUtil.Transpose(ml.GetArray()); MLDouble label = mfr.Content["rgbdclabel"] as MLDouble; double[] labels = label.GetArray()[0]; this.TrainModel(labels, mlArray); }
public static double[] GetDoubleArray(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsDouble) { throw new InvalidCastException("data is not of type double"); } var n = marray.Size; var darray = (MLDouble)marray; var data = new double[n]; for (var i = 0; i < n; ++i) { data[i] = darray.GetReal(i); } return(data); }
public static uint[] GetUInt32Array(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsUInt32) { throw new InvalidCastException("data is not of type UInt32"); } var n = marray.Size; var darray = (MLUInt32)marray; var data = new uint[n]; for (var i = 0; i < n; ++i) { data[i] = darray.GetReal(i); } return(data); }
public void UpdateMatlabFigure() { string _path = Application.streamingAssetsPath + Path.DirectorySeparatorChar + fileSelectionDropDown.Text; MatReader matFileReader = new MatReader(_path); MatNode matFile = matFileReader.Fields[matFileReader.FieldNames[0]]; foreach (var field in matFile.Fields) { MatNode _struct = matFile.Fields[field.Key]; // this is a bit of a silly workaround to get a string read in to be the key "_type", but using strings as keys for _matTypes is going to maake this whole script much more readable. // Accord throws an error if _struct is a Matlab struct with both numeric arrays and strings // so type is a struct with one field. The field contains no data but the FieldName is the sting I am trying to pass. string _type = new List <string>(_struct.Fields["type"].Fields.Keys)[0]; MatFileReader hmm = new MatFileReader(_struct); _matTypes[_type](_struct); } }
private void CompareTestDataWithWritingOptions( IMatFile expected, IMatFile actual, MatFileWriterOptions?maybeOptions) { byte[] buffer; using (var stream = new MemoryStream()) { var writer = maybeOptions is MatFileWriterOptions options ? new MatFileWriter(stream, options) : new MatFileWriter(stream); writer.Write(actual); buffer = stream.ToArray(); } using (var stream = new MemoryStream(buffer)) { var reader = new MatFileReader(stream); var actualRead = reader.Read(); CompareMatFiles(expected, actualRead); } }
public static int[,] GetInt32Array2D(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsInt32) { throw new InvalidCastException("data is not of type Int32"); } var m = marray.M; var n = marray.N; var darray = (MLInt32)marray; var data = new int[m, n]; for (var i = 0; i < m; ++i) { for (var j = 0; j < n; ++j) { data[i, j] = darray.GetReal(i, j); } } return(data); }
internal ArrayReader(string fileName) { try { MatFileReader mfr = new MatFileReader(fileName); A = ((MLDouble)(mfr.GetMLArray("A"))).GetArray(); PP = ((MLDouble)(mfr.GetMLArray("PP"))).GetArray(); double[][] Q = ((MLDouble)(mfr.GetMLArray("q"))).GetArray(); q = new double[Q.Length]; for (int i = 0; i < Q.Length; i++) { q[i] = Q[i][0]; } double[][] P = ((MLDouble)mfr.GetMLArray("p")).GetArray(); p = new double[P.Length]; for (int i = 0; i < P.Length; i++) { p[i] = P[i][0]; } double[][] V = ((MLDouble)mfr.GetMLArray("v")).GetArray(); v = new double[V.Length]; for (int i = 0; i < V.Length; i++) { v[i] = V[i][0]; } double[][] WW = ((MLDouble)mfr.GetMLArray("w")).GetArray(); w = new double[WW.Length]; for (int i = 0; i < WW.Length; i++) { w[i] = WW[i][0]; } } catch (System.IO.IOException) { throw new MatlabIOException("Cannot read file"); } }
public void ImportMatCurve(string path) { var mfr = new MatFileReader(path); var h = (mfr.GetMLArray("H") as MLDouble)?.GetArray(); var q = (mfr.GetMLArray("Q") as MLDouble)?.GetArray(); var hOpt = (mfr.GetMLArray("H_Opt") as MLDouble)?.GetArray(); var qOpt = (mfr.GetMLArray("Q_Opt") as MLDouble)?.GetArray(); var curve = new List <PumpPerformancePoint>(); if (qOpt != null && hOpt != null && qOpt[0].Count() == hOpt[0].Count()) { for (int i = 0; i < qOpt[0].Count(); i++) { curve.Add(new PumpPerformancePoint(hOpt[0][i], qOpt[0][i])); } } else if (hOpt != null && q != null && hOpt[0].Count() == q[0].Count()) { for (int i = 0; i < q.Count(); i++) { curve.Add(new PumpPerformancePoint(hOpt[0][i], q[0][i])); } } else if (h != null && q != null && h[0].Count() == q[0].Count()) { for (int i = 0; i < q[0].Count(); i++) { curve.Add(new PumpPerformancePoint(h[0][i], q[0][i])); } } else { throw new InvalidDataException("Keine gültigen Daten in MAT-File vorhanden."); } PerformanceCurve = curve.ToArray(); }
public static float[] GetSingleArray(this MatFileReader reader, string name) { var marray = reader.GetMLArray(name); if (!marray.IsSingle) { throw new InvalidCastException("data is not of type float"); } var m = marray.M; var n = marray.N; var l = m * n; var darray = (MLSingle)marray; var data = new float[l]; for (var i = 0; i < m; ++i) { for (var j = 0; j < n; ++j) { data[i * n + j] = darray.GetReal(i, j); } } return(data); }
private void ConvertMatlabFiles() { string[] events = Directory.GetFiles(@"C:\Users\Brent\Desktop\Neuro Imaging\BrentExport\sz_fmri"); foreach (var evt in events) { var fileName = System.IO.Path.GetFileName(evt); var subjId = fileName.Split('_')[0]; using (StreamWriter sw = new StreamWriter(@"C:\Users\Brent\Desktop\tmp\" + subjId + "_fMRI_mo-adj-mtx.txt")) { MatFileReader mtx = new MatFileReader(evt); foreach (var mla in mtx.Data) { var itms = (MLDouble)mla; for (int m = 0; m < 90; m++) { for (int n = 90; n < 180; n += 1) { var val = itms.Get(m, n); if (n == 90) { sw.Write(val.ToString()); } else { sw.Write("\t" + val.ToString()); } } sw.WriteLine(); } } } } }
static void Main(string[] args) { csmatio.io.MatFileReader mfr = new MatFileReader(@"E:\Users\Administrator\Source\Repos\TinyEKF\TinyEKFDLLTest\TinyEKFDLLTest\big-circle-ekf-dll.mat"); var x = (mfr.GetMLArray("x") as MLDouble).GetArray().ToList(); var y = (mfr.GetMLArray("y") as MLDouble).GetArray().ToList(); var orth_theta = (mfr.GetMLArray("orth_theta") as MLDouble).GetArray().ToList(); var delta_odom = (mfr.GetMLArray("delta_odom") as MLDouble).GetArray().ToList(); int length = mfr.Data[0].Size; List <double []> z = new List <double []>(length); List <double []> x_fusion = new List <double[]>(length); for (int i = 0; i < length; i++) { //var temp = x[i][0]; double[] temp = new[] { x[i][0], y[i][0], orth_theta[i][0] }; z.Add(temp); } x_fusion.Add(new[] { z[0][0], z[0][1], z[0][2] }); TinyEKFWrapper.ekf_t ekf = new TinyEKFWrapper.ekf_t(); IntPtr ekfIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ekf)); try { Marshal.StructureToPtr(ekf, ekfIntPtr, false); TinyEKFWrapper.EKFInit(ekfIntPtr, 3, 3); TinyEKFWrapper.Init(ekfIntPtr, z[0]); //var a = (TinyEKFWrapper.ekf_t)Marshal.PtrToStructure(ekfIntPtr, ekf.GetType()); for (int i = 1; i < length; i++) { TinyEKFWrapper.Model(ekfIntPtr, delta_odom[i][0]); //a = (TinyEKFWrapper.ekf_t)Marshal.PtrToStructure(ekfIntPtr, ekf.GetType()); TinyEKFWrapper.EKFStep(ekfIntPtr, z[i]); //a = (TinyEKFWrapper.ekf_t)Marshal.PtrToStructure(ekfIntPtr, ekf.GetType()); var temp = (TinyEKFWrapper.ekf_t)Marshal.PtrToStructure(ekfIntPtr, ekf.GetType()); x_fusion.Add(new[] { temp.x[0], temp.x[1], temp.x[2] }); } } finally { Marshal.DestroyStructure(ekfIntPtr, ekf.GetType()); } StreamWriter file = new StreamWriter(@"E:\Document\EyooProject\2.Vanke\odom\b-circle-dll.dat"); foreach (var data in x_fusion) { foreach (var d in data) { file.Write(d.ToString() + ","); } file.WriteLine(); } file.Close(); // TinyEKFWrapper.Print(); Console.WriteLine("the last postion [{0}\t{1}\t{2}]", x_fusion[length - 1][0], x_fusion[length - 1][1], x_fusion[length - 1][2]); Console.ReadLine(); }
public void ReadTestData(string matFileName) { var reader = new MatFileReader(matFileName); Assert.That(reader.Data, Is.Not.Empty); }
{ // 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); }
{ // 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); }
public static void TestLstmAgainstReferenceResults() { var mfr = new MatFileReader(@"lstm_small.mat"); var inputSize = mfr.GetInt("InputSize"); var seqLength = mfr.GetInt("SeqLength"); var hiddenSize = mfr.GetInt("HiddenSize"); var batchSize = mfr.GetInt("BatchSize"); var x = Variable <float>(PartialShape.Create(seqLength, batchSize, inputSize)); var lstm = new Lstm <float>(x, hiddenSize); var ctx = Context.GpuContext(0); var exe = new Executor(ctx, lstm.Y); exe.Initalize(); var h0 = mfr.GetDoubleArray("h0").Select(n => (float)n).ToArray(); var c0 = mfr.GetDoubleArray("c0").Select(n => (float)n).ToArray(); exe.AssignTensor(lstm.CX, c0.AsTensor(Shape.Create(batchSize, hiddenSize))); exe.AssignTensor(lstm.HX, h0.AsTensor(Shape.Create(batchSize, hiddenSize))); var input = mfr.GetDoubleArray("X").Select(n => (float)n).ToArray(); exe.AssignTensor(x, input.AsTensor(Shape.Create(seqLength, batchSize, inputSize))); var w = mfr.GetDoubleArray("W").Select(n => (float)n).ToArray(); w.AsTensor(Shape.Create(inputSize + hiddenSize + 1, 4 * hiddenSize)).Print(); exe.AssignTensor(lstm.W, w.AsTensor(Shape.Create(inputSize + hiddenSize + 1, 4 * hiddenSize))); exe.Forward(); var H = mfr.GetDoubleArray("H").Select(n => (float)n).ToArray(); H.AsTensor(Shape.Create(seqLength * batchSize, hiddenSize)).Print(); var myH = exe.GetTensor(lstm.Y).ToArray(); myH.AsTensor(Shape.Create(seqLength * batchSize, hiddenSize)).Print(); AreClose(H, myH, 1e-6); var CN = mfr.GetDoubleArray("cn").Select(n => (float)n).ToArray(); CN.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); var myCN = exe.GetTensor(lstm.CY).ToArray(); myCN.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); AreClose(CN, myCN, 1e-6); var HN = mfr.GetDoubleArray("hn").Select(n => (float)n).ToArray(); HN.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); var myHN = exe.GetTensor(lstm.HY).ToArray(); myHN.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); AreClose(HN, myHN, 1e-6); var dH = mfr.GetDoubleArray("dH").Select(n => (float)n).ToArray(); exe.AssignGradient(lstm.Y, dH.AsTensor(Shape.Create(seqLength, batchSize, hiddenSize)), replace: true); exe.Backward(); var dX = mfr.GetDoubleArray("dX").Select(n => (float)n).ToArray(); dX.AsTensor(Shape.Create(seqLength * batchSize, inputSize)).Print(); var dXmy = exe.GetGradient(lstm.X).ToArray(); dXmy.AsTensor(Shape.Create(seqLength * batchSize, inputSize)).Print(); AreClose(dX, dXmy, 1e-6); var dW = mfr.GetDoubleArray("dW").Select(n => (float)n).ToArray(); dW.AsTensor(Shape.Create(inputSize + hiddenSize + 1, 4 * hiddenSize)).Print(); var dWmy = exe.GetGradient(lstm.W).ToArray(); dWmy.AsTensor(Shape.Create(lstm.W.Shape.AsArray)).Print(); AreClose(dW, dWmy, 1e-6); var dc0 = mfr.GetDoubleArray("dc0").Select(n => (float)n).ToArray(); dc0.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); var dc0my = exe.GetGradient(lstm.CX).ToArray(); dc0my.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); AreClose(dc0, dc0my, 1e-6); var dh0 = mfr.GetDoubleArray("dh0").Select(n => (float)n).ToArray(); dh0.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); var dh0my = exe.GetGradient(lstm.HX).ToArray(); dh0my.AsTensor(Shape.Create(batchSize, hiddenSize)).Print(); AreClose(dh0, dh0my, 1e-6); ctx.ToGpuContext().Stream.Synchronize(); }
public CRUISEData ReadCRUISEData(string filename_mat, int emtimeconst, int icetimeconst) { double fs = 20; var result = new CRUISEData { error = "ok" }; try { var NVHclass = new NVHFunctions(); bool success = false; var mfr = new MatFileReader(filename_mat); if (!(mfr.Content.Count == 1 && mfr.Content.TryGetValue(Path.GetFileName(filename_mat).Substring(0, Path.GetFileName(filename_mat).Length - 4), out csmatio.types.MLArray test))) { result.error = "Matlab File needs to contain exactly one structure with the same name as the file!"; return(result); } var mainstruct = mfr.Content[Path.GetFileName(filename_mat).Substring(0, Path.GetFileName(filename_mat).Length - 4)] as csmatio.types.MLStructure; success = FindKey(mainstruct, "Time"); if (!success) { result.error = "Could not find \"Time\" Signal in .mat-File!"; return(result); } var time_signal = mainstruct["Time"] as csmatio.types.MLStructure; if (time_signal == null) { result.error = "Time Signal has no member \"Data\""; return(result); } var time_data = time_signal["Data"] as csmatio.types.MLDouble; success = FindKey(mainstruct, "EM_Tq"); if (!success) { result.error = "Could not find \"EM_Tq\" Signal in .mat-File!"; return(result); } var emachine_torque = mainstruct["EM_Tq"] as csmatio.types.MLStructure; if (emachine_torque == null) { result.error = "EM_Tq Signal has no member \"Data\""; return(result); } var emachine_torque_data = emachine_torque["Data"] as csmatio.types.MLDouble; success = FindKey(mainstruct, "EM_Spd"); if (!success) { result.error = "Could not find \"EM_Spd\" Signal in .mat-File!"; return(result); } var emachine_speed = mainstruct["EM_Spd"] as csmatio.types.MLStructure; if (emachine_speed == null) { result.error = "EM_Spd Signal has no member \"Data\""; return(result); } var emachine_speed_data = emachine_speed["Data"] as csmatio.types.MLDouble; success = FindKey(mainstruct, "ICE_Tq"); if (!success) { result.error = "Could not find \"ICE_Tq\" Signal in .mat-File!"; return(result); } var ice_torque = mainstruct["ICE_Tq"] as csmatio.types.MLStructure; if (ice_torque == null) { result.error = "ICE_Tq Signal has no member \"Data\""; return(result); } var ice_torque_data = ice_torque["Data"] as csmatio.types.MLDouble; success = FindKey(mainstruct, "ICE_Spd"); if (!success) { result.error = "Could not find \"ICE_Spd\" Signal in .mat-File!"; return(result); } var ice_speed = mainstruct["ICE_Spd"] as csmatio.types.MLStructure; if (ice_speed == null) { result.error = "ICE_Spd Signal has no member \"Data\""; return(result); } var ice_speed_data = ice_speed["Data"] as csmatio.types.MLDouble; success = FindKey(mainstruct, "Veh_Vel"); if (!success) { result.error = "Could not find \"Veh_Vel\" Signal in .mat-File!"; return(result); } var veh_vel = mainstruct["Veh_Vel"] as csmatio.types.MLStructure; if (veh_vel == null) { result.error = "Veh_Vel Signal has no member \"Data\""; return(result); } var veh_vel_data = veh_vel["Data"] as csmatio.types.MLDouble; var time_tmp = new double[time_data.Size]; for (int ii = 0; ii < time_tmp.Length; ii++) { time_tmp[ii] = time_data.Get(ii); } result.Time = NVHclass.DeltaArray(0, 1 / fs, time_tmp[time_tmp.Length - 1]); var etorque_tmp = new double[emachine_torque_data.Size]; for (int ii = 0; ii < etorque_tmp.Length; ii++) { etorque_tmp[ii] = emachine_torque_data.Get(ii); } var etorque = NVHclass.Interp1_linear(time_tmp, etorque_tmp, result.Time); etorque = NVHclass.RemoveNaN(etorque, etorque); var espeed_tmp = new double[emachine_speed_data.Size]; for (int ii = 0; ii < espeed_tmp.Length; ii++) { espeed_tmp[ii] = emachine_speed_data.Get(ii); } var espeed = NVHclass.Interp1_linear(time_tmp, espeed_tmp, result.Time); espeed = NVHclass.RemoveNaN(espeed, espeed); var icetorque_tmp = new double[ice_torque_data.Size]; for (int ii = 0; ii < icetorque_tmp.Length; ii++) { icetorque_tmp[ii] = ice_torque_data.Get(ii); } var icetorque = NVHclass.Interp1_linear(time_tmp, icetorque_tmp, result.Time); icetorque = NVHclass.RemoveNaN(icetorque, icetorque); var icespeed_tmp = new double[ice_speed_data.Size]; for (int ii = 0; ii < icespeed_tmp.Length; ii++) { icespeed_tmp[ii] = ice_speed_data.Get(ii); } var icespeed = NVHclass.Interp1_linear(time_tmp, icespeed_tmp, result.Time); icespeed = NVHclass.RemoveNaN(icespeed, icespeed); var velocity_tmp = new double[veh_vel_data.Size]; for (int ii = 0; ii < velocity_tmp.Length; ii++) { velocity_tmp[ii] = veh_vel_data.Get(ii); } var velocity = NVHclass.Interp1_linear(time_tmp, velocity_tmp, result.Time); result.VehVel = NVHclass.RemoveNaN(velocity, velocity); // Moving Average Filter //int ma = 33; result.EMTq = NVHclass.Moving(etorque, emtimeconst); result.EMSpd = NVHclass.Moving(espeed, emtimeconst); result.ICETq = NVHclass.Moving(icetorque, icetimeconst); result.ICESpd = NVHclass.Moving(icespeed, icetimeconst); } catch { result.error = "Corrupt file!"; } return(result); }
public static void Initialize(IUnitOfWork unitOfWork, IConfiguration configuration) { var userRepository = unitOfWork.GetRepository <User>(); var postRepository = unitOfWork.GetRepository <Post>(); if (userRepository.Get().Any() || postRepository.Get().Any()) { return; } var trainedDataPath = configuration["Data:TrainedDataPath"]; var trainedDataReader = new MatFileReader(trainedDataPath); var featuresVariable = configuration["Data:TrainedDataFeaturesVariable"]; var mlFeatures = trainedDataReader.Content[featuresVariable] as MLDouble; double[][] features = mlFeatures.GetArray(); var meanPostsRatesValuesVariable = configuration["Data:TrainedDataMeanPostsRatesValuesVariable"]; var mlMeanPostsRatesValues = trainedDataReader.Content[meanPostsRatesValuesVariable] as MLDouble; double[][] meanPostsRatesValues = mlMeanPostsRatesValues.GetArray(); var postsDataPath = configuration["Data:PostsPath"]; var postsData = File.ReadAllLines(postsDataPath); var posts = new List <Post>(postsData.Count()); for (int i = 0; i < postsData.Count(); i++) { var post = new Post { Id = i.ToString(), Title = postsData[i], PostedOn = DateTime.Now, Features = features[i].ToArray(), TrainedMeanRateValue = meanPostsRatesValues[i][0] }; posts.Add(post); } postRepository.Add(posts); unitOfWork.SaveChanges(); var usersRatingsDataPath = configuration["Data:UsersRatingsPath"]; var userRatingsDataReader = new MatFileReader(usersRatingsDataPath); var usersRatingsDataVariable = configuration["Data:UsersRatingsVariable"]; var mlUsersRaringsData = userRatingsDataReader.Content[usersRatingsDataVariable] as MLDouble; double[][] usersRatingsData = mlUsersRaringsData.GetArray(); var isRatedMatrixVariable = configuration["Data:IsRatedMatrixVariable"]; var mlIsRatedMatrix = userRatingsDataReader.Content[isRatedMatrixVariable] as MLUInt8; byte[][] isRatedMatrix = mlIsRatedMatrix.GetArray(); var weightsVariable = configuration["Data:TrainedDataWeightsVariable"]; var mlWeights = trainedDataReader.Content[weightsVariable] as MLDouble; double[][] weights = mlWeights.GetArray(); var users = new List <User>(usersRatingsData[0].Count()); for (int i = 0; i < usersRatingsData[0].Count(); i++) { var userName = GetUserName(i); var userEmail = GetUserEmail(i); var passwordHash = GetUserPasswordHash(); var rates = new List <Rate>(usersRatingsData.Count()); for (int j = 0; j < usersRatingsData.Count(); j++) { if (isRatedMatrix[j][i] == 0) { continue; } var rate = new Rate { PostId = j.ToString(), Value = (int)usersRatingsData[j][i] }; rates.Add(rate); } var user = new User { Name = userName, Email = userEmail, PasswordHash = passwordHash, IsSubscribed = false, Rates = rates, Weights = weights[i].ToArray() }; users.Add(user); } userRepository.Add(users); unitOfWork.SaveChanges(); }
private static nirs.core.Probe loadSDprobe(string file, int[] lambda) { nirs.core.Probe probe = new core.Probe(); MatFileReader mfr = new MatFileReader(file); MLStructure SD = (mfr.Content["SD"] as MLStructure); 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(); probe.DetPos = new double[detpos.Length, 3]; probe.DetectorLabels = new string[detpos.Length]; for (int i = 0; i < detpos.Length; i++) { probe.DetPos[i, 0] = (float)detpos[i][0]; probe.DetPos[i, 1] = (float)detpos[i][1]; probe.DetPos[i, 2] = (float)detpos[i][2]; probe.DetectorLabels[i] = String.Format("Detector-{0}", +1); } probe.numDet = detpos.Length; } if (MLsrcpos != null) { double[][] srcpos = MLsrcpos.GetArray(); probe.SrcPos = new double[srcpos.Length, 3]; probe.SourceLabels = new string[srcpos.Length]; for (int i = 0; i < srcpos.Length; i++) { probe.SrcPos[i, 0] = (float)srcpos[i][0]; probe.SrcPos[i, 1] = (float)srcpos[i][1]; probe.SrcPos[i, 2] = (float)srcpos[i][2]; probe.SourceLabels[i] = String.Format("Source-{0}", i + 1); } probe.numSrc = srcpos.Length; } MLDouble mlMeasList = null; if (mfr.Content.ContainsKey("ml")) { mlMeasList = (mfr.Content["ml"] as MLDouble); } else { mlMeasList = (SD["MeasList"] as MLDouble); } if (mlMeasList != null) { double[][] ml = mlMeasList.GetArray(); probe.ChannelMap = new ChannelMap[ml.Length]; for (int i = 0; i < ml.Length; i++) { probe.ChannelMap[i] = new ChannelMap(); probe.ChannelMap[i].sourceindex = (int)ml[i][0] - 1; probe.ChannelMap[i].detectorindex = (int)ml[i][1] - 1; probe.ChannelMap[i].channelname = String.Format("Src{0}-Det{1}", probe.ChannelMap[i].sourceindex + 1, probe.ChannelMap[i].detectorindex + 1); probe.ChannelMap[i].wavelength = lambda[(int)ml[i][3] - 1]; probe.ChannelMap[i].datasubtype = String.Format("{0}nm", probe.ChannelMap[i].wavelength); } } probe.numChannels = probe.ChannelMap.Length; probe.measlistAct = new bool[probe.numChannels]; for (int i = 0; i < probe.numChannels; i++) { probe.measlistAct[i] = false; } probe.measlistAct[0] = true; probe.numWavelengths = lambda.Length; return(probe); }