TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
GibbsLDA-GAScript.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using RPlugin.Core;
18 using RPlugin.Exceptions;
19 using System;
20 using System.Collections.Generic;
21 using System.IO;
22 using System.Linq;
23 using System.Reflection;
24 using System.Text;
25 using TraceLab.Components.RPlugin.Properties;
26 using TraceLab.Components.Types.Tracers.InformationRetrieval;
27 using TraceLabSDK.Types;
28 
29 namespace TraceLab.Components.DevelopmentKit.Tracers.InformationRetrieval
30 {
34  public class GibbsLDA_GAScript : RScript
35  {
36  private readonly string _baseScript = Settings.Default.Resources + "GibbsLDA-GA.R";
37  private readonly string[] _requiredPackages = new string[] { "GA", "slam", "tm", "topicmodels" };
38 
39  private TLArtifactsCollection _source;
40  private TLArtifactsCollection _target;
41  private GibbsLDA_GAConfig _config;
42  private string _outputFile;
43 
47  public override string BaseScript
48  {
49  get
50  {
51  return _baseScript;
52  }
53  }
54 
58  public override string[] RequiredPackages
59  {
60  get
61  {
62  return _requiredPackages;
63  }
64  }
65 
72  public GibbsLDA_GAScript(TLArtifactsCollection source, TLArtifactsCollection target, GibbsLDA_GAConfig config)
73  {
74  _source = source;
75  _target = target;
76  _config = config;
77  }
78 
82  public override void PreCompute()
83  {
84  RUtil.RegisterScript(Assembly.GetExecutingAssembly(), _baseScript);
85  DirectoryInfo sourceInfo = SaveArtifactsToCache(_source, "GibbsLDA-GA.source");
86  DirectoryInfo targetInfo = SaveArtifactsToCache(_target, "GibbsLDA-GA.target");
87  _outputFile = RUtil.ReserveCacheFile("GibbsLDA-GA.out");
88  _arguments = new List<object>();
89  _arguments.Add(sourceInfo.FullName);
90  _arguments.Add(targetInfo.FullName);
91  _arguments.Add(_outputFile);
92  _arguments.Add(100);
93  _arguments.Add(_config.GibbsIterations);
94  _arguments.Add(_config.Alpha);
95  _arguments.Add(_config.Beta);
96  _arguments.Add(_config.GA_Iterations);
97  _arguments.Add(_config.Run);
98  _arguments.Add(_config.PermutationRate);
99  _arguments.Add(_config.Population);
100  _arguments.Add(_config.Elitism);
101  _arguments.Add(_config.Seed);
102  }
103 
109  public override object ImportResults(RScriptResult result)
110  {
111  TextReader resultFile = new StreamReader(_outputFile);
112  string[] results = resultFile.ReadToEnd().Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
113  if (results.Length != 4)
114  {
115  throw new RDataException("Results file in incorrect format (" + _outputFile + ")");
116  }
117  return new GibbsLDAConfig
118  {
119  NumTopics = Convert.ToInt32(Math.Round(Convert.ToDouble(results[0]))),
120  GibbsIterations = Convert.ToInt32(Math.Round(Convert.ToDouble(results[1]))),
121  Alpha = Convert.ToDouble(results[2]),
122  Beta = Convert.ToDouble(results[3]),
123  Seed = _config.Seed,
124  };
125  }
126 
132  private DirectoryInfo SaveArtifactsToCache(TLArtifactsCollection artifacts, string name)
133  {
134  DirectoryInfo info = RUtil.CreateCacheDirectory(name);
135  foreach (TLArtifact artifact in artifacts.Values)
136  {
137  TextWriter tw = new StreamWriter(Path.Combine(info.FullName, artifact.Id));
138  tw.Write(artifact.Text);
139  tw.Flush();
140  tw.Close();
141  }
142  return info;
143  }
144  }
145 }