Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/02/18 13:14:16 (7 years ago)
Author:
rhanghof
Message:

#2913:

  • Added the support for importing different Matlab datatypes.
  • Added some classes and changed the import dialog for importing double arrays.
Location:
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/IMatlabConnector.cs

    r15912 r15926  
    99namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api {
    1010  public interface IMatlabConnector : IDisposable {
     11    event Action<string> ErrorOccuredEvent;
     12
    1113    /// <summary>
    1214    /// Executes a given matlab script.
     
    2527    /// </summary>
    2628    /// <returns></returns>
    27     IDictionary<string, MLDatatype> GetVariablesFromScript(string script);
     29    IEnumerable<MLVariableInfo> GetVariablesFromScript(string script);
    2830
    2931    /// <summary>
     
    4749    /// <returns></returns>
    4850    MLDouble GetDouble(string varName);
     51
     52    /// <summary>
     53    ///
     54    /// </summary>
     55    /// <param name="varName"></param>
     56    /// <returns></returns>
     57    MLDoubleArray GetDoubleArray(string varName);
    4958  }
    5059}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/MatlabConnector.cs

    r15919 r15926  
    3131    private MLApp.MLApp matLabApi;
    3232    private string workspace;
     33    private object locker = new object();
     34
     35    public event Action<string> ErrorOccuredEvent;
    3336
    3437    public MatlabConnector(bool showcommandWindow = false, string workspace = "base") {
     
    6871
    6972
    70     public IDictionary<string, MLDatatype> GetVariablesFromScript(string script) {
    71       var variables = new Dictionary<string, MLDatatype>();
    72       ExecuteScript(script);
     73    public IEnumerable<MLVariableInfo> GetVariablesFromScript(string script) {
     74      var variables = new List<MLVariableInfo>();
     75      try {
     76        ExecuteScript(script);
     77      } catch(InvalidOperationException ex) {
     78        if (ErrorOccuredEvent != null) {
     79          ErrorOccuredEvent.Invoke(ex.Message);
     80        }
     81      }
    7382
    7483      Execute("variables=whos();nrOfVariables=size(variables)");
     
    7786      for (int i = 1; i <= len; i++) {
    7887        var varName = GetVariable(string.Format("variables({0}).name", i)) as string;
    79         var varSize = GetVariable(string.Format("variables({0}).size", i));
     88        var varSize = GetVariable(string.Format("variables({0}).size", i)) as double[,];
    8089        var varClass = GetVariable(string.Format("variables({0}).class", i)) as string;
    81         // At the moment only timeseries are supported.
    82         if (varClass == "timeseries") {
    83           variables.Add(varName, MLDatatype.Timeseries);
    84         }
     90       
     91        GetVariablesFromScriptRec(variables, varName, varSize, varClass);
    8592      }
    8693
    8794      return variables;
    8895    }
    89    
     96
     97    private void GetVariablesFromScriptRec(IList<MLVariableInfo> variables, string varName, double[,] varSize, string varClass) {
     98      var variable = new MLVariableInfo() {
     99        VarName = varName,
     100        Row = varSize[0, 0],
     101        Column = varSize[0, 1]
     102      };
     103      if (varClass == "timeseries") {
     104        variable.Datatype = MLDatatype.Timeseries;
     105        variables.Add(variable);
     106      } else if (varClass == "double") {
     107        if (variable.Row == 1 && variable.Column == 1) {
     108          variable.Datatype = MLDatatype.Double;
     109        } else {
     110          variable.Datatype = MLDatatype.DoubleArray;
     111        }
     112        variables.Add(variable);
     113      } else if (varClass == "struct") {
     114        Execute(string.Format("mlFieldNames = fieldnames({0})", varName));
     115        var fields = GetVariable("mlFieldNames") as object[,];
     116
     117        for (int i = 0; i < fields.GetLength(0); i++) {
     118          var newVarName = string.Format("{0}.{1}", varName, fields[i, 0] as string);
     119          Execute(string.Format("mlClass = class({0}); mlSize = size({0});", newVarName));
     120          var newVarClass = GetVariable("mlClass") as string;
     121          var newVarSize = GetVariable("mlSize") as double[,];
     122          GetVariablesFromScriptRec(variables, newVarName, newVarSize, newVarClass);
     123        }
     124      }
     125    }
    90126
    91127    public MLTimeseries GetTimeseries(string varName) {
    92       object times = GetVariable(varName + ".Time");
    93       object data = GetVariable(varName + ".Data");
    94       if (times == null || data == null) {
    95         return null;
     128      lock (locker) {
     129        object times = GetVariable(varName + ".Time");
     130        object data = GetVariable(varName + ".Data");
     131
     132        if (times == null || data == null) {
     133          return null;
     134        }
     135        return new MLTimeseries(varName, times, data);
    96136      }
    97 
    98       return new MLTimeseries(varName, times, data);
    99137    }
    100138
    101139    public MLDouble GetDouble(string varName) {
    102140      return new MLDouble(varName, GetVariable(varName));
     141    }
     142
     143
     144    public MLDoubleArray GetDoubleArray(string varName) {
     145      return new MLDoubleArray(varName, GetVariable(varName));
    103146    }
    104147
     
    142185      }
    143186    }
    144 
    145187  }
    146188}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDatatype.cs

    r15912 r15926  
    99        Double,
    1010        DoubleArray,
    11         Timeseries
     11        Timeseries,
     12        Struct,
     13        Undefinded
    1214    }
    1315}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDoubleArray.cs

    r15919 r15926  
    2727      Name = string.Format("{0};{1}", a1.Name, a2.Name);
    2828
    29       if (!ConcatFeasible(a1, a2)) {
     29      if (!ConcatIsPossible(a1, a2)) {
    3030        throw new ArgumentException(string.Format("Cannot concat {0} and {1}. Different number of rows.", a1.Name, a2.Name));
    3131      }
    3232
    33       var numberOfColumns = a1.Columns + a2.Columns;
    34       _dataHeaders = new string[numberOfColumns];
    35       for (int i = 0; i < numberOfColumns; i++) {
     33      var columns = a1.Columns + a2.Columns;
     34      _dataHeaders = new string[columns];
     35      for (int i = 0; i < columns; i++) {
    3636        if (i < a1.Columns) {
    3737          _dataHeaders[i] = a1.DataHeader[i];
     
    4141      }
    4242
    43       Data = new double[a1.Rows, numberOfColumns];
     43      Data = new double[a1.Rows, columns];
    4444      for (int i = 0; i < a1.Rows; i++) {
    45         for (int j = 0; j < numberOfColumns; j++) {
     45        for (int j = 0; j < columns; j++) {
    4646          if (j < a1.Columns) {
    4747            Data[i, j] = a1.Data[i, j];
    4848          } else {
    4949            Data[i, j] = a1.Data[i, j - a1.Columns];
     50          }
     51        }
     52      }
     53    }
     54
     55    private MLDoubleArray(IEnumerable<MLDoubleArray> arrays) {
     56      StringBuilder sb = new StringBuilder();
     57      foreach (var array in arrays) {
     58        sb.Append(array.Name + ";");
     59      }
     60      Name = sb.ToString();
     61
     62      var columns = 0;
     63      foreach (var array in arrays) {
     64        columns += array.Columns;
     65      }
     66
     67      var dataHeaders = new List<string>();
     68      foreach (var array in arrays) {
     69        foreach (var header in array.DataHeader) {
     70          dataHeaders.Add(header);
     71        }
     72      }
     73      _dataHeaders = dataHeaders.ToArray();
     74
     75      var rows = arrays.First().Rows;
     76      Data = new double[rows, columns];
     77      for (int i = 0; i < rows; i++) {
     78        var j = 0;
     79        foreach (var array in arrays) {
     80          for (int k = 0; k < array.Columns; k++) {
     81            Data[i, j] = array.Data[i, k];
     82            j++;
    5083          }
    5184        }
     
    5790    /// <summary>
    5891    /// Concats two given MLDoubleArrays if their number of rows equals.
    59     /// If they can't be concated, this method returns null.
     92    /// If throws an InvalidOperationException if they can't be concatenated
    6093    /// </summary>
    6194    /// <param name="a1"></param>
     
    6396    /// <returns></returns>
    6497    public static MLDoubleArray Concat(MLDoubleArray a1, MLDoubleArray a2) {
    65       if (ConcatFeasible(a1, a2)) {
    66         return null;
     98      if (ConcatIsPossible(a1, a2)) {
     99        throw new InvalidOperationException("Concatination is not possible because of different different number of their rows.");
    67100      }
    68101      return new MLDoubleArray(a1, a2);
    69102    }
    70103
    71     public static bool ConcatFeasible(MLDoubleArray a1, MLDoubleArray a2) {
     104    public static MLDoubleArray Concat(IEnumerable<MLDoubleArray> arrays) {
     105      if (arrays.Count() < 1) {
     106        throw new ArgumentException("Given number of MLDoubleArrays is 0");
     107      }
     108      if (!ConcatIsPossible(arrays)) {
     109        throw new InvalidOperationException("Concatenation is not possible because of different different number of their rows.");
     110      }
     111      return new MLDoubleArray(arrays);
     112    }
     113
     114    public static bool ConcatIsPossible(MLDoubleArray a1, MLDoubleArray a2) {
    72115      return a1.Rows == a2.Rows;
     116    }
     117
     118    public static bool ConcatIsPossible(IEnumerable<MLDoubleArray> arrays) {
     119      var first = arrays.First();
     120      foreach (var array in arrays) {
     121        if (!ConcatIsPossible(first, array)) {
     122          return false;
     123        }
     124      }
     125      return true;
    73126    }
    74127
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLTimeseries.cs

    r15919 r15926  
    33using System.Linq;
    44using System.Text;
     5using System.Threading;
    56using System.Threading.Tasks;
    67
     
    2425    /// </summary>
    2526    /// <param name="timeseries"></param>
    26     public MLTimeseries(IEnumerable<MLTimeseries> timeseries) : this() {
     27    public MLTimeseries(IEnumerable<MLTimeseries> timeseries, CancellationToken token) : this() {
    2728      var dataHeaders = new List<string>();
    2829      var times = new SortedSet<double>(); // The time values have to be unique and sorted.
     
    6465
    6566          foreach (var v in vs) {
     67            if (token.IsCancellationRequested) {
     68              return;
     69            }
    6670            values[idx++] = v;
    6771          }
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/RegressionMatlabInstanceProvider.cs

    r15912 r15926  
    6767
    6868
    69     public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable<KeyValuePair<string, MLDatatype>> variableNames) {
     69    public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable<MLVariableInfo> variableNames) {
    7070      var dataset = type.Values;
    7171      var targetVar = type.TargetVariable;
Note: See TracChangeset for help on using the changeset viewer.