Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17473 was 17473, checked in by dpiringe, 4 years ago

#3026:

  • refactored inheritance structure of json items, now the default JsonItem is an abstract class without properties Value and Range -> splitted up into new interfaces
  • updated view models for new json item structure
  • updated SingleLineArrayJsonWriter
File size: 4.1 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 dataInfo = dataset.GetType().GetField("variableValues", flags);
32      dataInfo.SetValue(dataset, dictTmp);
33      val.TargetVariable = ((StringJsonItem)data.Children[3]).Value;
34      val.TrainingPartition.Start = ((IntRangeJsonItem)data.Children[2]).MinValue;
35      val.TrainingPartition.End = ((IntRangeJsonItem)data.Children[2]).MaxValue;
36      val.TestPartition.Start = ((IntRangeJsonItem)data.Children[1]).MinValue;
37      val.TestPartition.End = ((IntRangeJsonItem)data.Children[1]).MaxValue;
38    }
39
40    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
41      IJsonItem item = new EmptyJsonItem() {
42        Name = value.ItemName,
43        Description = value.ItemDescription
44      };
45
46      dynamic val = (dynamic)value;
47      object dataset = (object)val.Dataset;
48      dynamic targetVariable = val.TargetVariable;
49     
50      FieldInfo dataInfo = dataset.GetType().GetField("variableValues", flags);
51     
52      if(dataInfo.GetValue(dataset) is Dictionary<string, IList> dict) {
53        int cols = dict.Count;
54        int rows = 0;
55        IList<string> rowNames = new List<string>();
56        double[][] mat = new double[cols][];
57        int c = 0;
58        foreach(var x in dict) {
59          rows = Math.Max(rows, x.Value.Count);
60          rowNames.Add(x.Key);
61          mat[c] = new double[rows];
62          int r = 0;
63          foreach(var rowValue in x.Value) {
64            // TODO: for integers and bools aswell
65            mat[c][r] = (double)rowValue;
66            ++r;
67          }
68          ++c;
69        }
70        item.AddChildren(new DoubleMatrixJsonItem() {
71          Name = "Dataset",
72          Value = mat,
73          RowNames = rowNames,
74          Minimum = double.MinValue,
75          Maximum = double.MaxValue
76        });
77      }
78
79      var trainingPartition = ((IntRange)val.TrainingPartition);
80      var testPartition = ((IntRange)val.TestPartition);
81
82      item.AddChildren(new IntRangeJsonItem() {
83        Name = "TestPartition",
84        MinValue = testPartition.Start,
85        MaxValue = testPartition.End,
86        Minimum = 0,
87        Maximum = Math.Max(testPartition.End, trainingPartition.End)
88      });
89
90     
91      item.AddChildren(new IntRangeJsonItem() {
92        Name = "TrainingPartition",
93        MinValue = trainingPartition.Start,
94        MaxValue = trainingPartition.End,
95        Minimum = 0,
96        Maximum = Math.Max(testPartition.End, trainingPartition.End)
97      });
98
99      IEnumerable<StringValue> variables = (IEnumerable<StringValue>)val.InputVariables;
100      item.AddChildren(new StringJsonItem() {
101        Name = "TargetVariable",
102        Value = (string)targetVariable,
103        ConcreteRestrictedItems = variables.Select(x => x.Value)
104      });
105
106      /*
107      item.AddChildren(new StringArrayJsonItem() {
108        Name = "AllowedInputVariables",
109        Value = (string[])val.AllowedInputVariables,
110        Range = variables.Select(x => x.Value)
111      });*/
112      return item;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.