1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Reflection;
|
---|
6 | using System.Text;
|
---|
7 | using System.Threading.Tasks;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using Newtonsoft.Json.Linq;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.JsonInterface {
|
---|
13 | public class RegressionProblemDataConverter : BaseConverter {
|
---|
14 | private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
---|
15 | public override int Priority => 20;
|
---|
16 | public override Type ConvertableType => HEAL.Attic.Mapper.StaticCache.GetType(new Guid("EE612297-B1AF-42D2-BF21-AF9A2D42791C"));
|
---|
17 |
|
---|
18 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
19 | var dictTmp = new Dictionary<string, IList>();
|
---|
20 | DoubleMatrixJsonItem matrix = data.Children[0] as DoubleMatrixJsonItem;
|
---|
21 | if(matrix != null) {
|
---|
22 | int c = 0;
|
---|
23 | foreach(var col in matrix.RowNames) {
|
---|
24 | dictTmp.Add(col, new List<double>(matrix.Value[c]));
|
---|
25 | ++c;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | dynamic val = (dynamic)item;
|
---|
30 | object dataset = (object)val.Dataset;
|
---|
31 | var rows = dataset.GetType().GetField("rows", flags);
|
---|
32 | rows.SetValue(dataset, matrix.Value[0].Length);
|
---|
33 |
|
---|
34 | var variableNames = dataset.GetType().GetField("variableNames", flags);
|
---|
35 | variableNames.SetValue(dataset, matrix.RowNames);
|
---|
36 |
|
---|
37 | var dataInfo = dataset.GetType().GetField("variableValues", flags);
|
---|
38 | dataInfo.SetValue(dataset, dictTmp);
|
---|
39 | val.TargetVariable = ((StringJsonItem)data.Children[3]).Value;
|
---|
40 | val.TrainingPartition.Start = ((IntRangeJsonItem)data.Children[2]).MinValue;
|
---|
41 | val.TrainingPartition.End = ((IntRangeJsonItem)data.Children[2]).MaxValue;
|
---|
42 | val.TestPartition.Start = ((IntRangeJsonItem)data.Children[1]).MinValue;
|
---|
43 | val.TestPartition.End = ((IntRangeJsonItem)data.Children[1]).MaxValue;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
47 | IJsonItem item = new EmptyJsonItem() {
|
---|
48 | Name = value.ItemName,
|
---|
49 | Description = value.ItemDescription
|
---|
50 | };
|
---|
51 |
|
---|
52 | dynamic val = (dynamic)value;
|
---|
53 | object dataset = (object)val.Dataset;
|
---|
54 | dynamic targetVariable = val.TargetVariable;
|
---|
55 |
|
---|
56 | FieldInfo dataInfo = dataset.GetType().GetField("variableValues", flags);
|
---|
57 |
|
---|
58 | if(dataInfo.GetValue(dataset) is Dictionary<string, IList> dict) {
|
---|
59 | int cols = dict.Count;
|
---|
60 | int rows = 0;
|
---|
61 | IList<string> rowNames = new List<string>();
|
---|
62 | double[][] mat = new double[cols][];
|
---|
63 | int c = 0;
|
---|
64 | foreach(var x in dict) {
|
---|
65 | rows = Math.Max(rows, x.Value.Count);
|
---|
66 | rowNames.Add(x.Key);
|
---|
67 | mat[c] = new double[rows];
|
---|
68 | int r = 0;
|
---|
69 | foreach(var rowValue in x.Value) {
|
---|
70 | // TODO: for integers and bools aswell
|
---|
71 | mat[c][r] = (double)rowValue;
|
---|
72 | ++r;
|
---|
73 | }
|
---|
74 | ++c;
|
---|
75 | }
|
---|
76 | item.AddChildren(new DoubleMatrixJsonItem() {
|
---|
77 | Name = "Dataset",
|
---|
78 | Value = mat,
|
---|
79 | RowNames = rowNames,
|
---|
80 | Minimum = double.MinValue,
|
---|
81 | Maximum = double.MaxValue
|
---|
82 | });
|
---|
83 | }
|
---|
84 |
|
---|
85 | var trainingPartition = ((IntRange)val.TrainingPartition);
|
---|
86 | var testPartition = ((IntRange)val.TestPartition);
|
---|
87 |
|
---|
88 | item.AddChildren(new IntRangeJsonItem() {
|
---|
89 | Name = "TestPartition",
|
---|
90 | MinValue = testPartition.Start,
|
---|
91 | MaxValue = testPartition.End,
|
---|
92 | Minimum = 0,
|
---|
93 | Maximum = Math.Max(testPartition.End, trainingPartition.End)
|
---|
94 | });
|
---|
95 |
|
---|
96 |
|
---|
97 | item.AddChildren(new IntRangeJsonItem() {
|
---|
98 | Name = "TrainingPartition",
|
---|
99 | MinValue = trainingPartition.Start,
|
---|
100 | MaxValue = trainingPartition.End,
|
---|
101 | Minimum = 0,
|
---|
102 | Maximum = Math.Max(testPartition.End, trainingPartition.End)
|
---|
103 | });
|
---|
104 |
|
---|
105 | IEnumerable<StringValue> variables = (IEnumerable<StringValue>)val.InputVariables;
|
---|
106 | item.AddChildren(new StringJsonItem() {
|
---|
107 | Name = "TargetVariable",
|
---|
108 | Value = (string)targetVariable,
|
---|
109 | ConcreteRestrictedItems = variables.Select(x => x.Value)
|
---|
110 | });
|
---|
111 |
|
---|
112 | /*
|
---|
113 | item.AddChildren(new StringArrayJsonItem() {
|
---|
114 | Name = "AllowedInputVariables",
|
---|
115 | Value = (string[])val.AllowedInputVariables,
|
---|
116 | Range = variables.Select(x => x.Value)
|
---|
117 | });*/
|
---|
118 | return item;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|