1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.IO;
|
---|
6 | using HeuristicLab.Modeling.Database;
|
---|
7 | using HeuristicLab.Modeling.Database.SQLServerCompact;
|
---|
8 |
|
---|
9 | namespace CedmaImporter {
|
---|
10 | public class Importer {
|
---|
11 |
|
---|
12 | private const int ID_COLUMN = 0;
|
---|
13 | private const int FILENAME_COLUMN = 1;
|
---|
14 | private const int TARGETVARIABLE_COLUMN = 2;
|
---|
15 | private const int ALGORITHM_COLUMN = 3;
|
---|
16 | private const int RESULTS_IDX = 4;
|
---|
17 | private Result[] results;
|
---|
18 | private string[] inputVariables;
|
---|
19 |
|
---|
20 | private HeuristicLab.CEDMA.Server.Problem problem;
|
---|
21 |
|
---|
22 |
|
---|
23 | public Importer(HeuristicLab.CEDMA.Server.Problem problem) {
|
---|
24 | this.problem = problem;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void Import(string fileName, string dirName) {
|
---|
28 | string outputFileName = Path.GetFileNameWithoutExtension(fileName) + ".sdf";
|
---|
29 | string connectionString = @"Data Source=" + outputFileName;
|
---|
30 |
|
---|
31 | DatabaseService database = new DatabaseService(connectionString);
|
---|
32 | Problem p = database.GetOrCreateProblem(problem.Dataset);
|
---|
33 | using (StreamReader reader = File.OpenText(fileName)) {
|
---|
34 | ReadResultsAndInputVariables(reader);
|
---|
35 | ImportAllModels(reader, database);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private void ReadResultsAndInputVariables(StreamReader reader) {
|
---|
40 | string[] columns = reader.ReadLine().Split(';');
|
---|
41 | results = Enumerable.Repeat<Result>(null, columns.Length).ToArray();
|
---|
42 | inputVariables = Enumerable.Repeat<string>(null, columns.Length).ToArray();
|
---|
43 | for (int i = RESULTS_IDX; i < columns.Length; i++) {
|
---|
44 | string resultColumn = columns[i].Trim();
|
---|
45 | if (resultColumn.Contains(":")) {
|
---|
46 | string[] tokens = resultColumn.Split(':');
|
---|
47 | string variableName = tokens[1].Trim();
|
---|
48 | string variableResultName = tokens[0].Trim();
|
---|
49 | inputVariables[i] = variableName;
|
---|
50 | results[i] = new Result(variableResultName);
|
---|
51 | } else {
|
---|
52 | // normal result value
|
---|
53 | results[i] = new Result(resultColumn);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void ImportAllModels(StreamReader reader, DatabaseService database) {
|
---|
59 | while (!reader.EndOfStream) {
|
---|
60 | string modelLine = reader.ReadLine();
|
---|
61 | string[] modelData = modelLine.Split(';');
|
---|
62 | int id = int.Parse(modelData[ID_COLUMN]);
|
---|
63 | string fileName = modelData[FILENAME_COLUMN];
|
---|
64 | string targetVariableName = modelData[TARGETVARIABLE_COLUMN];
|
---|
65 | string algoName = modelData[ALGORITHM_COLUMN];
|
---|
66 | Variable targetVariable = new Variable(targetVariableName);
|
---|
67 | Algorithm algorithm = new Algorithm(algoName);
|
---|
68 | Model model = new Model(targetVariable, algorithm);
|
---|
69 | model.TrainingSamplesStart = problem.TrainingSamplesStart;
|
---|
70 | model.TrainingSamplesEnd = problem.TrainingSamplesEnd;
|
---|
71 | model.ValidationSamplesStart = problem.ValidationSamplesStart;
|
---|
72 | model.ValidationSamplesEnd = problem.ValidationSamplesEnd;
|
---|
73 | model.TestSamplesStart = problem.TestSamplesStart;
|
---|
74 | model.TestSamplesEnd = problem.TestSamplesEnd;
|
---|
75 |
|
---|
76 | IEnumerable<ModelResult> qualityModelResults = GetModelResults(model, modelData);
|
---|
77 | IEnumerable<InputVariableResult> inputVariableResults = GetInputVariableResults(model, modelData);
|
---|
78 |
|
---|
79 | // TODO
|
---|
80 | //database.Persist(model);
|
---|
81 | //foreach (ModelResult modelResult in qualityModelResults)
|
---|
82 | // database.Persist(modelResult);
|
---|
83 | //foreach (InputVariableResult inputVariableResult in inputVariableResults)
|
---|
84 | // database.Persist(inputVariableResult);
|
---|
85 |
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private IEnumerable<InputVariableResult> GetInputVariableResults(Model model, string[] modelData) {
|
---|
90 | double temp;
|
---|
91 | return from i in Enumerable.Range(0, inputVariables.Count())
|
---|
92 | where inputVariables[i] != null && results[i] != null && double.TryParse(modelData[i], out temp)
|
---|
93 | select new InputVariableResult(new InputVariable(model, new Variable(inputVariables[i])), results[i], double.Parse(modelData[i]));
|
---|
94 | }
|
---|
95 |
|
---|
96 | private IEnumerable<ModelResult> GetModelResults(Model model, string[] modelData) {
|
---|
97 | return from i in Enumerable.Range(0, results.Count())
|
---|
98 | where results[i] != null
|
---|
99 | select new ModelResult(model, results[i], double.Parse(modelData[i]));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|