/// <summary> /// Calls RScript executable and runs the given script as a hidden process /// </summary> /// <param name="RScriptPath">Path to RScript executable</param> /// <param name="script">Script location</param> /// <param name="args">Script arguments</param> /// <returns>Script output</returns> internal static RScriptResult RunScript(string RScriptPath, string script, params object[] args) { RequireSetupRun(); ProcessStartInfo info = new ProcessStartInfo { FileName = RScriptPath, Arguments = "--verbose \"" + Path.Combine(Scripts, script) + "\" " + String.Join(" ", args), UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, }; RScriptResult result = new RScriptResult(); Process R = new Process(); R.StartInfo = info; R.OutputDataReceived += result.ScriptOutputHandler; R.ErrorDataReceived += result.ScriptErrorHandler; R.Start(); R.BeginOutputReadLine(); R.BeginErrorReadLine(); R.WaitForExit(); result.ExitCode = R.ExitCode; return(result); }
/// <summary> /// Executes an R script /// </summary> /// <param name="script">R script</param> /// <returns>Script results</returns> public object Execute(IRScript script) { RController.RequirePackages(_path, script.RequiredPackages); script.PreCompute(); RScriptResult result = RController.RunScript(_path, script.BaseScript, script.Arguments); if (result.ExitCode != 0) { throw new RExecutionException(script.BaseScript, script.Arguments, result.Output, result.Error); } return(script.ImportResults(result)); }
internal static void RequirePackages(string RScriptPath, params string[] args) { RequireSetupRun(); // check packages RScriptResult RCheck = RunScript(RScriptPath, Path.Combine(Scripts, Settings.Default.Resources + "PackageInstaller.R"), "0 " + String.Join(" ", args)); if (RCheck.ExitCode != 0) { HasSetupRun = false; throw new Exception("There was an error in the application:\n" + RCheck.Error); } // install missing packages if (!String.IsNullOrWhiteSpace(RCheck.Output.Trim())) { RScriptResult RInstall = RunScriptAdmin(RScriptPath, Path.Combine(Scripts, Settings.Default.Resources + "PackageInstaller.R"), "1 " + RCheck.Output.Trim()); if (RInstall.ExitCode != 0) { HasSetupRun = false; throw new Exception("There was an error installing packages."); } } }
/// <summary> /// Import script results /// </summary> /// <param name="result">RScriptResults object</param> /// <returns>Script results</returns> public override object ImportResults(RScriptResult result) { return Similarities.Import(_outputFile); }
/// <summary> /// Imports script results /// </summary> /// <param name="result">RScriptResult object</param> /// <returns>Script results</returns> public abstract object ImportResults(RScriptResult result);
/// <summary> /// Import script results /// </summary> /// <param name="result">RScriptResults object</param> /// <returns>Script results</returns> public override object ImportResults(RScriptResult result) { TextReader rfile = new StreamReader(_outputFile); string rawdata = rfile.ReadToEnd(); rfile.Close(); TLSimilarityMatrix matrix = new TLSimilarityMatrix(); string[] sims = rawdata.Remove(0, 2).Replace(")", String.Empty).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[] edges = Generics.ImportStrings(_info.Edges); if (sims.Length != edges.Length) throw new RDataException("Results are incorrect size: " + sims.Length + " vs " + edges.Length); for (int i = 0; i < sims.Length; i++) { string[] split = edges[i].Split(); matrix.AddLink(_corpus.Map[Convert.ToInt32(split[0])], _corpus.Map[Convert.ToInt32(split[1])], Convert.ToDouble(sims[i])); } //int src = 0; //int tgt = _source.DocMap.Count; //if (sims.Length != _source.DocMap.Count * _target.DocMap.Count) //{ // throw new RDataException("Results are incorrect size: " + sims.Length + " vs " + (_source.DocMap.Count * _target.DocMap.Count)); //} //foreach (string sim in sims) //{ // matrix.AddLink(_source.DocMap[src], _target.DocMap[tgt - _source.DocMap.Count], Convert.ToDouble(sim.Trim())); // tgt++; // if (tgt == _source.DocMap.Count + _target.DocMap.Count) // { // tgt = _source.DocMap.Count; // src++; // } //} return matrix; }
/// <summary> /// Returns an ideal GibbsLDA configuration object /// </summary> /// <param name="result">REngine results object</param> /// <returns>GibbsLDAConfig object</returns> public override object ImportResults(RScriptResult result) { TextReader resultFile = new StreamReader(_outputFile); string[] results = resultFile.ReadToEnd().Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); if (results.Length != 4) { throw new RDataException("Results file in incorrect format (" + _outputFile + ")"); } return new GibbsLDAConfig { NumTopics = Convert.ToInt32(Math.Round(Convert.ToDouble(results[0]))), GibbsIterations = Convert.ToInt32(Math.Round(Convert.ToDouble(results[1]))), Alpha = Convert.ToDouble(results[2]), Beta = Convert.ToDouble(results[3]), Seed = _config.Seed, }; }
/// <summary> /// Imports script results /// </summary> /// <param name="result">RScriptResults object</param> /// <returns>Script results</returns> public override object ImportResults(RScriptResult result) { TextReader rfile = new StreamReader(_outputFile); string rawdata = rfile.ReadToEnd(); rfile.Close(); TLSimilarityMatrix matrix = new TLSimilarityMatrix(); string[] sims = rawdata.Remove(0,2).Replace(")", String.Empty).Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries); int src = 0; int tgt = _source.DocMap.Count; if (sims.Length != _source.DocMap.Count * _target.DocMap.Count) { throw new RDataException("Results are incorrect size: " + sims.Length + " vs " + (_source.DocMap.Count * _target.DocMap.Count)); } foreach (string sim in sims) { matrix.AddLink(_source.DocMap[src], _target.DocMap[tgt - _source.DocMap.Count], Convert.ToDouble(sim.Trim())); tgt++; if (tgt == _source.DocMap.Count + _target.DocMap.Count) { tgt = _source.DocMap.Count; src++; } } return matrix; }
/// <summary> /// Calls RScript executable and runs the given script as a hidden process /// </summary> /// <param name="RScriptPath">Path to RScript executable</param> /// <param name="script">Script location</param> /// <param name="args">Script arguments</param> /// <returns>Script output</returns> internal static RScriptResult RunScript(string RScriptPath, string script, params object[] args) { RequireSetupRun(); ProcessStartInfo info = new ProcessStartInfo { FileName = RScriptPath, Arguments = "--verbose \"" + Path.Combine(Scripts, script) + "\" " + String.Join(" ", args), UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, }; RScriptResult result = new RScriptResult(); Process R = new Process(); R.StartInfo = info; R.OutputDataReceived += result.ScriptOutputHandler; R.ErrorDataReceived += result.ScriptErrorHandler; R.Start(); R.BeginOutputReadLine(); R.BeginErrorReadLine(); R.WaitForExit(); result.ExitCode = R.ExitCode; return result; }
/// <summary> /// Import script results /// </summary> /// <param name="result">RScriptResults object</param> /// <returns>Script results</returns> public override object ImportResults(RScriptResult result) { // index = id - 1 string[] ids = Generics.ImportStrings(_mapFile); TextReader resultsMatrix = new StreamReader(_outputFile); TLSimilarityMatrix matrix = new TLSimilarityMatrix(); string[] sources = resultsMatrix.ReadLine().Split(); string line; while ((line = resultsMatrix.ReadLine()) != null) { if (String.IsNullOrWhiteSpace(line)) continue; // [0] target id, [x+] source sims index = x - 1 string[] entries = line.Split(); string entry = ids[Convert.ToInt32(entries[0]) - 1]; for (int i = 0; i < sources.Length; i++) { matrix.AddLink(ids[Convert.ToInt32(sources[i]) - 1], entry, Convert.ToDouble(entries[i + 1])); } } resultsMatrix.Close(); return matrix; }
/// <summary> /// Returns an array containing the contributions of each matrix in the order they were added /// </summary> /// <param name="result">REngine result object</param> /// <returns>double[]</returns> public override object ImportResults(RScriptResult result) { List<double> pca = new List<double>(); TextReader file = new StreamReader(_outputFile); file.ReadLine(); string line = String.Empty; while ((line = file.ReadLine()) != null) { string[] entries = line.Split(','); if (entries.Length != 3) { throw new RDataException("Error in PCA output."); } pca.Add(Convert.ToDouble(entries[2])); } if (pca.Count != _matrices.Length) { throw new RDataException("PCA returned the incorrect number of entries."); } return pca.ToArray(); }
/// <summary> /// Import script results /// </summary> /// <param name="result">RScriptResults object</param> /// <returns>Script results</returns> public override object ImportResults(RScriptResult result) { IEnumerable<double> ranks = Generics.ImportDoubles(_outputFile, false); IEnumerable<string> map = Generics.ImportStrings(_mappingFile); if (ranks.Count() != map.Count()) { throw new RDataException("Results file in incorrect format: incorrect number of entries"); } TLSimilarityMatrix rankList = new TLSimilarityMatrix(); for (int i = 0; i < map.Count(); i++) { rankList.AddLink(_traceID, map.ElementAt(i), ranks.ElementAt(i)); } return rankList; }
/// <summary> /// Imports script results /// </summary> /// <param name="result">RScriptResults object</param> /// <returns>Script results</returns> public override object ImportResults(RScriptResult result) { IEnumerable<double> authorities = Generics.ImportDoubles(_authorityFile, false); IEnumerable<double> hubs = Generics.ImportDoubles(_hubFile, false); IEnumerable<string> map = Generics.ImportStrings(_mappingFile); HITSResult results = new HITSResult(); for (int i = 0; i < map.Count(); i++) { results.Hubs.AddLink(_traceID, map.ElementAt(i), hubs.ElementAt(i)); results.Authorities.AddLink(_traceID, map.ElementAt(i), authorities.ElementAt(i)); } return results; }