Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/25/20 17:18:00 (4 years ago)
Author:
dpiringe
Message:

#3026:

  • renamed ResultItem to ResultJsonItem
  • implemented RegressionProblemDataConverter
  • added support for "named matrices" (named = rows and columns have names) -> INamedMatrixJsonItem incl. base classes and views
  • added a bool property Active in IJsonItem -> marks items, which should be written into the template
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface
Files:
2 added
14 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/AlgorithmConverter.cs

    r17444 r17451  
    2727      IAlgorithm algorithm = value as IAlgorithm;
    2828      foreach (var res in algorithm.Results) {
    29         item.AddChildren(new ResultItem() {
     29        item.AddChildren(new ResultJsonItem() {
    3030          Name = res.Name,
    3131          Description = res.Description
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/RegressionProblemDataConverter.cs

    r17433 r17451  
    11using System;
     2using System.Collections;
    23using System.Collections.Generic;
    34using System.Linq;
     
    78using HeuristicLab.Core;
    89using HeuristicLab.Data;
     10using Newtonsoft.Json.Linq;
    911
    1012namespace HeuristicLab.JsonInterface {
     
    1517
    1618    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    17       // TODO: inject data
    18       throw new NotImplementedException();
     19      //Dictionary<string, IList<double>> dict = null;
     20      var dictTmp = new Dictionary<string, IList>();
     21      /*if (data.Children[0].Value is JObject jObj) {
     22        dict = jObj.ToObject<Dictionary<string, IList<double>>>();
     23        foreach (var x in dict) {
     24          dictTmp.Add(x.Key, (IList)x.Value);
     25        }
     26      } else if(data.Children[0].Value is Dictionary<string, IList<double>> d) {
     27        dict = d;
     28      } else {
     29        dictTmp = (Dictionary<string, IList>)data.Children[0].Value;
     30      }
     31      */
     32      DoubleNamedMatrixJsonItem matrix = data.Children[0] as DoubleNamedMatrixJsonItem;
     33      if(matrix != null) {
     34        int c = 0;
     35        foreach(var col in matrix.RowNames) {
     36          dictTmp.Add(col, new List<double>(matrix.Value[c]));
     37          ++c;
     38        }
     39      }
     40
     41      dynamic val = (dynamic)item;
     42      object dataset = (object)val.Dataset;
     43      var dataInfo = dataset.GetType().GetField("variableValues", flags);
     44      dataInfo.SetValue(dataset, dictTmp);
     45      val.TargetVariable = (string)data.Children[3].Value;
     46      val.TrainingPartition.Start = ((IntRangeJsonItem)data.Children[2]).Value.First();
     47      val.TrainingPartition.End = ((IntRangeJsonItem)data.Children[2]).Value.Last();
     48      val.TestPartition.Start = ((IntRangeJsonItem)data.Children[1]).Value.First();
     49      val.TestPartition.End = ((IntRangeJsonItem)data.Children[1]).Value.Last();
    1950    }
    2051
     
    2859      object dataset = (object)val.Dataset;
    2960      dynamic targetVariable = val.TargetVariable;
    30       FieldInfo dataInfo = dataset.GetType().GetField("storableData", flags);
    31       // TODO: aufteilen in trainings und test daten abschnitte
    32       item.AddChildren(new JsonItem() {
    33         Name = "Dataset",
    34         Value = dataInfo.GetValue(dataset)
     61     
     62      FieldInfo dataInfo = dataset.GetType().GetField("variableValues", flags);
     63     
     64      if(dataInfo.GetValue(dataset) is Dictionary<string, IList> dict) {
     65        int cols = dict.Count;
     66        int rows = 0;
     67        IList<string> rowNames = new List<string>();
     68        double[][] mat = new double[cols][];
     69        int c = 0;
     70        foreach(var x in dict) {
     71          rows = Math.Max(rows, x.Value.Count);
     72          rowNames.Add(x.Key);
     73          mat[c] = new double[rows];
     74          int r = 0;
     75          foreach(var rowValue in x.Value) {
     76            mat[c][r] = (double)rowValue;
     77            ++r;
     78          }
     79          ++c;
     80        }
     81        item.AddChildren(new DoubleNamedMatrixJsonItem() {
     82          Name = "Dataset",
     83          Value = mat,
     84          RowNames = rowNames
     85        });
     86      }
     87
     88      var trainingPartition = ((IntRange)val.TrainingPartition);
     89      var testPartition = ((IntRange)val.TestPartition);
     90
     91      item.AddChildren(new IntRangeJsonItem() {
     92        Name = "TestPartition",
     93        Value = new int[] { testPartition.Start, testPartition.End },
     94        Range = new int[] { 0, Math.Max(testPartition.End, trainingPartition.End) }
     95      });
     96
     97     
     98      item.AddChildren(new IntRangeJsonItem() {
     99        Name = "TrainingPartition",
     100        Value = new int[] { trainingPartition.Start, trainingPartition.End },
     101        Range = new int[] { 0, Math.Max(testPartition.End, trainingPartition.End)}
    35102      });
    36103
     
    42109      });
    43110
    44 
    45       item.AddChildren(new JsonItem() {
     111      /*
     112      item.AddChildren(new StringArrayJsonItem() {
    46113        Name = "AllowedInputVariables",
    47         Value = (object)val.AllowedInputVariables,
     114        Value = (string[])val.AllowedInputVariables,
    48115        Range = variables.Select(x => x.Value)
    49       });
     116      });*/
    50117      return item;
    51118    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ResultParameterConverter.cs

    r17446 r17451  
    1515    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
    1616      IResultParameter res = value as IResultParameter;
    17       return new ResultItem() {
     17      return new ResultJsonItem() {
    1818        Name = res.ActualName,
    1919        ActualName = res.ActualName,
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueLookupParameterConverter.cs

    r17433 r17451  
    2727        item = tmp;
    2828      } else {
    29         item.Range = new object[] { GetMinValue(param.DataType), GetMaxValue(param.DataType) };
     29        var min = GetMinValue(param.DataType);
     30        var max = GetMaxValue(param.DataType);
     31        if (min != null && max != null)
     32          item.Range = new object[] { min, max };
     33        else
     34          item.Range = null;
    3035      }
    3136      item.Name = param.Name;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueRangeConverter.cs

    r17433 r17451  
    1717    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    1818      IntRange range = item as IntRange;
    19       IntArrayJsonItem cdata = data as IntArrayJsonItem;
     19      IntRangeJsonItem cdata = data as IntRangeJsonItem;
    2020      range.Start = cdata.Value[0];
    2121      range.End = cdata.Value[1];
     
    3939    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    4040      DoubleRange range = item as DoubleRange;
    41       DoubleArrayJsonItem cdata = data as DoubleArrayJsonItem;
     41      DoubleRangeJsonItem cdata = data as DoubleRangeJsonItem;
    4242      range.Start = cdata.Value[0];
    4343      range.End = cdata.Value[1];
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs

    r17433 r17451  
    4040    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    4141      DoubleArray arr = item as DoubleArray;
    42       double[] d = CastValue<double[]>(data);
     42      DoubleArrayJsonItem doubleItem = data as DoubleArrayJsonItem;
     43      //double[] d = CastValue<double[]>(data);
    4344      bool resizeTmp = arr.Resizable;
    4445      arr.Resizable = true;
    45       arr.Length = d.Length;
    46       for (int i = 0; i < d.Length; ++i)
    47         arr[i] = d[i];
     46      //arr.Length = d.Length;
     47      arr.Length = doubleItem.Value.Length;
     48      for (int i = 0; i < doubleItem.Value.Length; ++i)
     49        arr[i] = doubleItem.Value[i];
    4850      arr.Resizable = resizeTmp;
    4951    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs

    r17433 r17451  
    3333    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    3434      DoubleMatrix mat = item as DoubleMatrix;
    35       DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
     35      DoubleNamedMatrixJsonItem d = data as DoubleNamedMatrixJsonItem;
    3636      CopyMatrixData(mat, d.Value);
    3737    }
    3838
    3939    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
    40       new DoubleMatrixJsonItem() {
     40      new DoubleNamedMatrixJsonItem() {
    4141        Name = "[OverridableParamName]",
    4242        Description = value.ItemDescription,
    4343        Value = Transform((DoubleMatrix)value),
    44         Range = new double[] { double.MinValue, double.MaxValue }
     44        Range = new double[] { double.MinValue, double.MaxValue },
     45        RowNames = ((DoubleMatrix)value).RowNames,
     46        ColumnNames = ((DoubleMatrix)value).ColumnNames
    4547      };
    4648  }
     
    9092    #region Helper
    9193    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
    92       var rows = data.Length;
    93       var cols = data.Length > 0 ? data[0].Length : 0;
     94      var cols = data.Length;
     95      var rows = data.Length > 0 ? data[0].Length : 0;
    9496
    9597      var rowInfo = matrix.GetType().GetProperty("Rows");
     
    100102      for (int x = 0; x < rows; ++x) {
    101103        for (int y = 0; y < cols; ++y) {
    102           matrix[x, y] = data[x][y];
     104          matrix[x, y] = data[y][x];
    103105        }
    104106      }
     
    106108
    107109    protected T[][] Transform(MatrixType matrix) {
    108       T[][] m = new T[matrix.Rows][];
    109       for (int r = 0; r < matrix.Rows; ++r) {
    110         m[r] = new T[matrix.Columns];
    111         for (int c = 0; c < matrix.Columns; ++c) {
    112           m[r][c] = matrix[r, c];
     110      T[][] m = new T[matrix.Columns][];
     111      for (int column = 0; column < matrix.Columns; ++column) {
     112        m[column] = new T[matrix.Rows];
     113        for (int row = 0; row < matrix.Rows; ++row) {
     114          m[column][row] = matrix[row, column];
    113115        }
    114116      }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj

    r17446 r17451  
    7171    <Compile Include="Interfaces\IJsonItemValidator.cs" />
    7272    <Compile Include="Interfaces\IMatrixJsonItem.cs" />
     73    <Compile Include="Interfaces\INamedMatrixJsonItem.cs" />
    7374    <Compile Include="Models\ArrayJsonItemBase.cs" />
    7475    <Compile Include="Models\BoolJsonItems.cs" />
     
    7980    <Compile Include="Models\JsonItem.cs" />
    8081    <Compile Include="Models\MatrixJsonItemBase.cs" />
    81     <Compile Include="Models\ResultItem.cs" />
     82    <Compile Include="Models\NamedMatrixJsonItemBase.cs" />
     83    <Compile Include="Models\ResultJsonItem.cs" />
    8284    <Compile Include="Models\StringJsonItem.cs" />
    8385    <Compile Include="Models\UnsupportedJsonItem.cs" />
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IJsonItem.cs

    r17446 r17451  
    99namespace HeuristicLab.JsonInterface {
    1010  public interface IJsonItem {
     11    bool Active { get; set; }
    1112    string Name { get; set; }
    1213
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs

    r17439 r17451  
    4141      // filter result items
    4242      foreach (var item in jsonItems) {
    43         if (item is ResultItem)
     43        if (item is ResultJsonItem)
    4444          resultItems.Add(Serialize(item));
    4545        else
     
    6060    // serializes ParameterizedItems and saves them in list "JsonItems".
    6161    private void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
    62       IEnumerable<IJsonItem> tmpParameter = item.Children;
     62      IEnumerable<IJsonItem> children = item.Children;
    6363     
    64       if (item.Value != null || item.Range != null || item is ResultItem || item.ActualName != null) {
     64      if (item.Active && (item.Value != null || item.Range != null || item is ResultJsonItem || item.ActualName != null)) {
    6565        jsonItems.Add(item);
    6666      }
    6767
    68       if (tmpParameter != null) {
    69         foreach (var p in tmpParameter) {
     68      if (children != null) {
     69        foreach (var p in children) {
    7070          PopulateJsonItems(p, jsonItems);
    7171        }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DoubleJsonItems.cs

    r17446 r17451  
    2323    }
    2424  }
     25
     26  public class DoubleNamedMatrixJsonItem : NamedMatrixJsonItemBase<double> {
     27    protected override bool IsInRange() {
     28      for (int c = 0; c < Value.Length; ++c) {
     29        for (int r = 0; r < Value[c].Length; ++r) {
     30          if (Value[c][r] < Range.First() && Range.Last() < Value[c][r])
     31            return false;
     32        }
     33      }
     34      return true;
     35    }
     36  }
    2537}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntJsonItems.cs

    r17446 r17451  
    66
    77namespace HeuristicLab.JsonInterface {
    8   public class IntJsonItem : JsonItem<int> { }
     8  public class IntJsonItem : JsonItem<int> {
     9    /*I
     10    public int MinValue { get; set; }
     11    public int MaxValue { get; set; }
     12    */
     13  }
    914  public class IntArrayJsonItem : ArrayJsonItemBase<int> { }
    1015  public class IntRangeJsonItem : ArrayJsonItemBase<int> {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs

    r17446 r17451  
    6666    public virtual IEnumerable<object> Range { get; set; }
    6767   
     68    // TODO eigene items für LookUp?
    6869    public virtual string ActualName { get; set; }
     70
     71    // TODO jsonIgnore dataType?
    6972
    7073    [JsonIgnore]
     
    7376    [JsonIgnore]
    7477    public virtual IJsonItem Parent { get; set; }
     78
     79    [JsonIgnore]
     80    public virtual bool Active { get; set; }
    7581
    7682    #region Constructors
     
    8187    }
    8288    #endregion
    83 
    84     #region Public Static Methods
    85     public static void Merge(JsonItem target, JsonItem from) {
    86       target.Name = from.Name ?? target.Name;
    87       target.Range = from.Range ?? target.Range;
    88       target.Value = from.Value ?? target.Value;
    89       target.ActualName = from.ActualName ?? target.ActualName;
    90       if(target.Children != null) {
    91         if (from.Children != null)
    92           ((List<IJsonItem>)from.Children).AddRange(target.Children);
    93       } else {
    94         target.Children = from.Children;
    95       }
    96     }
    97     #endregion
    98 
     89   
    9990    #region Public Methods
    10091    public void AddChildren(params IJsonItem[] childs) =>
     
    124115
    125116    #region Helper
     117    /*
     118     * TODO protected abstract bool Validate();
     119     */
     120
    126121    protected virtual bool IsInRange() {
    127122      bool b1 = true, b2 = true;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ResultJsonItem.cs

    r17450 r17451  
    66
    77namespace HeuristicLab.JsonInterface {
    8   public class ResultItem : JsonItem {
     8  public class ResultJsonItem : JsonItem {
    99
    1010  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/StringJsonItem.cs

    r17446 r17451  
    77namespace HeuristicLab.JsonInterface {
    88  public class StringJsonItem : JsonItem<string> { }
     9  public class StringArrayJsonItem : JsonItem<string[], string> { }
    910}
Note: See TracChangeset for help on using the changeset viewer.