Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • Runner now uses SymbolicDataAnalysisExpressionMATLABFormatter to save instances of ISymbolicRegressionSolution
  • refactored user controls for detail view of json items, now they do not inherit from JsonItemBaseControl -> JsonItemBaseControl uses now an extra control
    • this change was made for fluid repositioning of controls (e.g. when no ActualName is present)
File size: 4.0 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      DoubleNamedMatrixJsonItem matrix = data.Children[0] as DoubleNamedMatrixJsonItem;
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 = (string)data.Children[3].Value;
34      val.TrainingPartition.Start = ((IntRangeJsonItem)data.Children[2]).Value.First();
35      val.TrainingPartition.End = ((IntRangeJsonItem)data.Children[2]).Value.Last();
36      val.TestPartition.Start = ((IntRangeJsonItem)data.Children[1]).Value.First();
37      val.TestPartition.End = ((IntRangeJsonItem)data.Children[1]).Value.Last();
38    }
39
40    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
41      IJsonItem item = new JsonItem() {
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 DoubleNamedMatrixJsonItem() {
71          Name = "Dataset",
72          Value = mat,
73          RowNames = rowNames
74        });
75      }
76
77      var trainingPartition = ((IntRange)val.TrainingPartition);
78      var testPartition = ((IntRange)val.TestPartition);
79
80      item.AddChildren(new IntRangeJsonItem() {
81        Name = "TestPartition",
82        Value = new int[] { testPartition.Start, testPartition.End },
83        Range = new int[] { 0, Math.Max(testPartition.End, trainingPartition.End) }
84      });
85
86     
87      item.AddChildren(new IntRangeJsonItem() {
88        Name = "TrainingPartition",
89        Value = new int[] { trainingPartition.Start, trainingPartition.End },
90        Range = new int[] { 0, Math.Max(testPartition.End, trainingPartition.End)}
91      });
92
93      IEnumerable<StringValue> variables = (IEnumerable<StringValue>)val.InputVariables;
94      item.AddChildren(new JsonItem() {
95        Name = "TargetVariable",
96        Value = (object)targetVariable,
97        Range = variables.Select(x => x.Value)
98      });
99
100      /*
101      item.AddChildren(new StringArrayJsonItem() {
102        Name = "AllowedInputVariables",
103        Value = (string[])val.AllowedInputVariables,
104        Range = variables.Select(x => x.Value)
105      });*/
106      return item;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.