Changeset 15926 for branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab
- Timestamp:
- 05/02/18 13:14:16 (7 years ago)
- 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 9 9 namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api { 10 10 public interface IMatlabConnector : IDisposable { 11 event Action<string> ErrorOccuredEvent; 12 11 13 /// <summary> 12 14 /// Executes a given matlab script. … … 25 27 /// </summary> 26 28 /// <returns></returns> 27 I Dictionary<string, MLDatatype> GetVariablesFromScript(string script);29 IEnumerable<MLVariableInfo> GetVariablesFromScript(string script); 28 30 29 31 /// <summary> … … 47 49 /// <returns></returns> 48 50 MLDouble GetDouble(string varName); 51 52 /// <summary> 53 /// 54 /// </summary> 55 /// <param name="varName"></param> 56 /// <returns></returns> 57 MLDoubleArray GetDoubleArray(string varName); 49 58 } 50 59 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/MatlabConnector.cs
r15919 r15926 31 31 private MLApp.MLApp matLabApi; 32 32 private string workspace; 33 private object locker = new object(); 34 35 public event Action<string> ErrorOccuredEvent; 33 36 34 37 public MatlabConnector(bool showcommandWindow = false, string workspace = "base") { … … 68 71 69 72 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 } 73 82 74 83 Execute("variables=whos();nrOfVariables=size(variables)"); … … 77 86 for (int i = 1; i <= len; i++) { 78 87 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[,]; 80 89 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); 85 92 } 86 93 87 94 return variables; 88 95 } 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 } 90 126 91 127 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); 96 136 } 97 98 return new MLTimeseries(varName, times, data);99 137 } 100 138 101 139 public MLDouble GetDouble(string varName) { 102 140 return new MLDouble(varName, GetVariable(varName)); 141 } 142 143 144 public MLDoubleArray GetDoubleArray(string varName) { 145 return new MLDoubleArray(varName, GetVariable(varName)); 103 146 } 104 147 … … 142 185 } 143 186 } 144 145 187 } 146 188 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDatatype.cs
r15912 r15926 9 9 Double, 10 10 DoubleArray, 11 Timeseries 11 Timeseries, 12 Struct, 13 Undefinded 12 14 } 13 15 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDoubleArray.cs
r15919 r15926 27 27 Name = string.Format("{0};{1}", a1.Name, a2.Name); 28 28 29 if (!Concat Feasible(a1, a2)) {29 if (!ConcatIsPossible(a1, a2)) { 30 30 throw new ArgumentException(string.Format("Cannot concat {0} and {1}. Different number of rows.", a1.Name, a2.Name)); 31 31 } 32 32 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++) { 36 36 if (i < a1.Columns) { 37 37 _dataHeaders[i] = a1.DataHeader[i]; … … 41 41 } 42 42 43 Data = new double[a1.Rows, numberOfColumns];43 Data = new double[a1.Rows, columns]; 44 44 for (int i = 0; i < a1.Rows; i++) { 45 for (int j = 0; j < numberOfColumns; j++) {45 for (int j = 0; j < columns; j++) { 46 46 if (j < a1.Columns) { 47 47 Data[i, j] = a1.Data[i, j]; 48 48 } else { 49 49 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++; 50 83 } 51 84 } … … 57 90 /// <summary> 58 91 /// Concats two given MLDoubleArrays if their number of rows equals. 59 /// If th ey can't be concated, this method returns null.92 /// If throws an InvalidOperationException if they can't be concatenated 60 93 /// </summary> 61 94 /// <param name="a1"></param> … … 63 96 /// <returns></returns> 64 97 public static MLDoubleArray Concat(MLDoubleArray a1, MLDoubleArray a2) { 65 if (Concat Feasible(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."); 67 100 } 68 101 return new MLDoubleArray(a1, a2); 69 102 } 70 103 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) { 72 115 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; 73 126 } 74 127 -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLTimeseries.cs
r15919 r15926 3 3 using System.Linq; 4 4 using System.Text; 5 using System.Threading; 5 6 using System.Threading.Tasks; 6 7 … … 24 25 /// </summary> 25 26 /// <param name="timeseries"></param> 26 public MLTimeseries(IEnumerable<MLTimeseries> timeseries ) : this() {27 public MLTimeseries(IEnumerable<MLTimeseries> timeseries, CancellationToken token) : this() { 27 28 var dataHeaders = new List<string>(); 28 29 var times = new SortedSet<double>(); // The time values have to be unique and sorted. … … 64 65 65 66 foreach (var v in vs) { 67 if (token.IsCancellationRequested) { 68 return; 69 } 66 70 values[idx++] = v; 67 71 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/RegressionMatlabInstanceProvider.cs
r15912 r15926 67 67 68 68 69 public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable< KeyValuePair<string, MLDatatype>> variableNames) {69 public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable<MLVariableInfo> variableNames) { 70 70 var dataset = type.Values; 71 71 var targetVar = type.TargetVariable;
Note: See TracChangeset
for help on using the changeset viewer.