Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/RegressionProblemDataConverter.cs @ 17485

Last change on this file since 17485 was 17484, checked in by dpiringe, 5 years ago

#3026:

  • fixed a bug with JsonItemMultiValueControl -> the size of the matrix should now be saved correctly
  • fixed a bug with RegressionProblemDataConverter -> should now set the row/col sizes correctly
  • simplified the code for saving matrix data in JsonItemMultiValueControl, MatrixValueVM and ArrayValueVM
  • removed unnecessary casts
File size: 4.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Reflection;
6using System.Text;
7using System.Threading.Tasks;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using Newtonsoft.Json.Linq;
11
12namespace 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}
Note: See TracBrowser for help on using the repository browser.