1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Parameters;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.JsonInterface {
|
---|
10 | public class RegressionProblemDataConverter : BaseConverter {
|
---|
11 | #region Constants
|
---|
12 | private const string TestPartition = "TestPartition";
|
---|
13 | private const string TrainingPartition = "TrainingPartition";
|
---|
14 | private const string TargetVariable = "TargetVariable";
|
---|
15 | private const string AllowedInputVariables = "AllowedInputVariables";
|
---|
16 | private const string Dataset = "Dataset";
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | public override int Priority => 20;
|
---|
20 |
|
---|
21 | public override bool CanConvertType(Type t) => t == typeof(ValueParameter<IRegressionProblemData>);
|
---|
22 |
|
---|
23 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
24 | var parameter = item as ValueParameter<IRegressionProblemData>;
|
---|
25 |
|
---|
26 | DoubleMatrixJsonItem dataset = null;
|
---|
27 | StringJsonItem targetVariable = null;
|
---|
28 | IntRangeJsonItem testPartition = null;
|
---|
29 | IntRangeJsonItem trainingPartition = null;
|
---|
30 | StringArrayJsonItem allowedInputVariables = null;
|
---|
31 |
|
---|
32 | // get all child items
|
---|
33 | foreach (var child in data.Children) {
|
---|
34 | if (child.Path.EndsWith(Dataset))
|
---|
35 | dataset = child as DoubleMatrixJsonItem;
|
---|
36 | else if (child.Path.EndsWith(TargetVariable))
|
---|
37 | targetVariable = child as StringJsonItem;
|
---|
38 | else if (child.Path.EndsWith(TestPartition))
|
---|
39 | testPartition = child as IntRangeJsonItem;
|
---|
40 | else if (child.Path.EndsWith(TrainingPartition))
|
---|
41 | trainingPartition = child as IntRangeJsonItem;
|
---|
42 | else if (child.Path.EndsWith(AllowedInputVariables))
|
---|
43 | allowedInputVariables = child as StringArrayJsonItem;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // check data
|
---|
47 | if(!dataset.ColumnNames.Any(x => x == targetVariable.Value)) {
|
---|
48 | throw new Exception($"The value of the target variable ('{targetVariable.Value}') has no matching row name value of the dataset.");
|
---|
49 | }
|
---|
50 |
|
---|
51 | foreach(var v in allowedInputVariables.Value) {
|
---|
52 | if(!dataset.ColumnNames.Any(x => x == v))
|
---|
53 | throw new Exception($"The value of the input variable ('{v}') has no matching row name value of the dataset.");
|
---|
54 | }
|
---|
55 |
|
---|
56 | // create the new problemData object
|
---|
57 | var problemData = new RegressionProblemData(
|
---|
58 | new Dataset(dataset.ColumnNames, dataset.Value),
|
---|
59 | allowedInputVariables.Value,
|
---|
60 | targetVariable.Value);
|
---|
61 |
|
---|
62 | // set the new problemData
|
---|
63 | parameter.Value = problemData;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
67 | var parameter = value as ValueParameter<IRegressionProblemData>;
|
---|
68 | var problemData = parameter.Value;
|
---|
69 |
|
---|
70 | IJsonItem item = new EmptyJsonItem() {
|
---|
71 | Name = parameter.Name,
|
---|
72 | Description = problemData.ItemDescription
|
---|
73 | };
|
---|
74 |
|
---|
75 | item.AddChildren(GetDataset(parameter.Value.Dataset));
|
---|
76 | item.AddChildren(GetTestPartition(problemData.TestPartition));
|
---|
77 | item.AddChildren(GetTrainingPartition(problemData.TrainingPartition));
|
---|
78 | item.AddChildren(GetTargetVariable(problemData.TargetVariable, problemData.Dataset.VariableNames));
|
---|
79 | item.AddChildren(GetAllowedInputVariables(problemData.AllowedInputVariables, problemData.Dataset.VariableNames));
|
---|
80 |
|
---|
81 | return item;
|
---|
82 | }
|
---|
83 |
|
---|
84 | #region Extract Helper
|
---|
85 | private IJsonItem GetDataset(IDataset dataset) {
|
---|
86 | int variableCount = dataset.VariableNames.Count();
|
---|
87 | var matrix = new double[variableCount][];
|
---|
88 | int count = 0;
|
---|
89 |
|
---|
90 | foreach(var variable in dataset.VariableNames) {
|
---|
91 | if(count < variableCount) {
|
---|
92 | matrix[count] = dataset.GetDoubleValues(variable).ToArray();
|
---|
93 | count++;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | return new DoubleMatrixJsonItem() {
|
---|
98 | Name = Dataset,
|
---|
99 | Value = matrix,
|
---|
100 | ColumnNames = dataset.ColumnNames,
|
---|
101 | Minimum = double.MinValue,
|
---|
102 | Maximum = double.MaxValue
|
---|
103 | };
|
---|
104 | }
|
---|
105 |
|
---|
106 | private IJsonItem GetTestPartition(IntRange testPartition) =>
|
---|
107 | new IntRangeJsonItem() {
|
---|
108 | Name = TestPartition,
|
---|
109 | MinValue = testPartition.Start,
|
---|
110 | MaxValue = testPartition.End,
|
---|
111 | Minimum = 0,
|
---|
112 | Maximum = int.MaxValue
|
---|
113 | };
|
---|
114 |
|
---|
115 | private IJsonItem GetTrainingPartition(IntRange trainingPartition) =>
|
---|
116 | new IntRangeJsonItem() {
|
---|
117 | Name = TrainingPartition,
|
---|
118 | MinValue = trainingPartition.Start,
|
---|
119 | MaxValue = trainingPartition.End,
|
---|
120 | Minimum = 0,
|
---|
121 | Maximum = int.MaxValue
|
---|
122 | };
|
---|
123 |
|
---|
124 |
|
---|
125 | private IJsonItem GetTargetVariable(string targetVariable, IEnumerable<string> variables) =>
|
---|
126 | new StringJsonItem() {
|
---|
127 | Name = TargetVariable,
|
---|
128 | Value = targetVariable,
|
---|
129 | ConcreteRestrictedItems = variables
|
---|
130 | };
|
---|
131 |
|
---|
132 | private IJsonItem GetAllowedInputVariables(IEnumerable<string> allowedVariables, IEnumerable<string> variables) =>
|
---|
133 | new StringArrayJsonItem() {
|
---|
134 | Name = AllowedInputVariables,
|
---|
135 | Value = allowedVariables.ToArray(),
|
---|
136 | ConcreteRestrictedItems = variables
|
---|
137 | };
|
---|
138 | #endregion
|
---|
139 | }
|
---|
140 | } |
---|