PageRenderTime 27ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/eLMM/CodePlex/MBF/matrix/Matrix.UnitTest.cs

#
C# | 295 lines | 174 code | 39 blank | 82 comment | 20 complexity | 776506ce55e58acc0839cc55bb1e6003 MD5 | raw file
  1. //*********************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. //
  6. //
  7. //
  8. //
  9. //
  10. //*********************************************************
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using Bio.Util;
  16. using System.Threading.Tasks;
  17. using System.IO;
  18. namespace Bio.Matrix
  19. {
  20. /// <summary>
  21. /// Unit tests on many kinds of matrices.
  22. /// </summary>
  23. public class MatrixUnitTest
  24. {
  25. /// <summary>
  26. /// Preforms unit tests related to setting and reading value on all the built-in matrix types.
  27. /// </summary>
  28. /// <param name="doOutOfRangeTest">If true performs a test should throw a caught exception.</param>
  29. /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param>
  30. static public void MainTest(bool doOutOfRangeTest, ParallelOptions parallelOptions)
  31. {
  32. //densematrix
  33. TestByKeysAndIndexes(() => CreateModelMatrix().ToDenseMatrix(), doOutOfRangeTest);
  34. //sparsematrix
  35. TestByKeysAndIndexes(() => CreateModelMatrix().ToSparseMatrix(), doOutOfRangeTest);
  36. //TransposeView
  37. TestByKeysAndIndexes(() => CreateModelMatrix().TransposeView().ToDenseMatrix().TransposeView(), doOutOfRangeTest);
  38. //ConvertValueView
  39. TestByKeysAndIndexes(() => CreateModelMatrix().ConvertValueView(ValueConverter.DoubleToInt, int.MaxValue).ToDenseMatrix().ConvertValueView(ValueConverter.IntToDouble, double.NaN), doOutOfRangeTest);
  40. //SelectRowsAndColsView
  41. TestByKeysAndIndexes(() => CreateModelMatrix().SelectRowsAndColsView(new int[] { 2, 1, 0 }, new int[] { 2, 1, 0 }).ToDenseMatrix().SelectRowsAndColsView(new int[] { 2, 1, 0 }, new int[] { 2, 1, 0 }), doOutOfRangeTest);
  42. //RenameColsView
  43. TestByKeysAndIndexes(() => CreateModelMatrix().RenameColsView(new Dictionary<string, string> { { "x", "X" }, { "y", "Y" }, { "z", "Z" } }).ToDenseMatrix().RenameColsView(new Dictionary<string, string> { { "X", "x" }, { "Y", "y" }, { "Z", "z" } }), doOutOfRangeTest);
  44. //PermuteColValuesForEachRowView
  45. TestByKeysAndIndexes(() => CreateModelMatrix().PermuteColValuesForEachRowView(new int[] { 2, 1, 0 }).ToDenseMatrix().PermuteColValuesForEachRowView(new int[] { 2, 1, 0 }), doOutOfRangeTest);
  46. //MergeColsView
  47. TestByKeysAndIndexes(() => CreateModelMatrix().SelectColsView(0).ToDenseMatrix().MergeColsView(/*rowsMustMatch*/true, CreateModelMatrix().SelectColsView(1, 2).ToDenseMatrix()), doOutOfRangeTest);
  48. //MergeRowsView
  49. TestByKeysAndIndexes(() => CreateModelMatrix().SelectRowsView(0).ToDenseMatrix().MergeRowsView(/*colsMustMatch*/true, CreateModelMatrix().SelectRowsView(1, 2).ToDenseMatrix()), doOutOfRangeTest);
  50. //rowkeyspaddeddouble
  51. TestByKeysAndIndexes(() => CreateModelMatrix().ToPaddedDouble(parallelOptions), doOutOfRangeTest);
  52. //rowkeysansi
  53. TestByKeysAndIndexes(() => CreateModelMatrix().ConvertValueView(ValueConverter.DoubleToChar, '?').ToDenseAnsi(parallelOptions).ConvertValueView(ValueConverter.CharToDouble, double.NaN), doOutOfRangeTest);
  54. //densepairansi
  55. ValueConverter<double, UOPair<char>> doubleToUOPairConvert = new ValueConverter<double, UOPair<char>>(
  56. r => new UOPair<char>(((int)r).ToString()[0], ((int)r).ToString()[0]),
  57. pair => double.Parse(pair.First.ToString()));
  58. TestByKeysAndIndexes(() => CreateModelMatrix().ConvertValueView(doubleToUOPairConvert, DensePairAnsi.StaticMissingValue).ToDensePairAnsi(parallelOptions).ConvertValueView(doubleToUOPairConvert.Inverted, double.NaN), doOutOfRangeTest);
  59. //RowKeysPaddedDouble
  60. string paddedDoubleFile = Path.GetTempFileName();
  61. CreateModelMatrix().WritePaddedDouble(paddedDoubleFile, parallelOptions);
  62. using (RowKeysPaddedDouble rowKeysPaddedDouble = RowKeysPaddedDouble.GetInstanceFromPaddedDouble(paddedDoubleFile, parallelOptions, FileAccess.ReadWrite, FileShare.ReadWrite))
  63. {
  64. TestByKeys(rowKeysPaddedDouble, doOutOfRangeTest);
  65. }
  66. CreateModelMatrix().WritePaddedDouble(paddedDoubleFile, parallelOptions);
  67. using (RowKeysPaddedDouble rowKeysPaddedDouble = RowKeysPaddedDouble.GetInstanceFromPaddedDouble(paddedDoubleFile, parallelOptions, FileAccess.ReadWrite, FileShare.ReadWrite))
  68. {
  69. TestByIndexes(rowKeysPaddedDouble, doOutOfRangeTest);
  70. }
  71. File.Delete(paddedDoubleFile);
  72. //RowKeysRowKeysAnsi
  73. string rowKeysAnsiFile = Path.GetTempFileName();
  74. CreateModelMatrix().WriteDenseAnsi(rowKeysAnsiFile, parallelOptions);
  75. using (RowKeysAnsi rowKeysRowKeysAnsi = RowKeysAnsi.GetInstanceFromDenseAnsi(rowKeysAnsiFile, parallelOptions, FileAccess.ReadWrite, FileShare.ReadWrite))
  76. {
  77. TestByKeys(rowKeysRowKeysAnsi.ConvertValueView(ValueConverter.CharToDouble, double.NaN), doOutOfRangeTest);
  78. }
  79. CreateModelMatrix().WriteDenseAnsi(rowKeysAnsiFile, parallelOptions);
  80. using (RowKeysAnsi rowKeysRowKeysAnsi = RowKeysAnsi.GetInstanceFromDenseAnsi(rowKeysAnsiFile, parallelOptions, FileAccess.ReadWrite, FileShare.ReadWrite))
  81. {
  82. TestByIndexes(rowKeysRowKeysAnsi.ConvertValueView(ValueConverter.CharToDouble, double.NaN), doOutOfRangeTest);
  83. }
  84. File.Delete(rowKeysAnsiFile);
  85. //RowKeysRowKeysPairAnsi
  86. string rowKeysPairAnsiFile = Path.GetTempFileName();
  87. CreateModelMatrix().ConvertValueView(doubleToUOPairConvert, DensePairAnsi.StaticMissingValue).WriteDensePairAnsi(rowKeysPairAnsiFile, parallelOptions);
  88. using (RowKeysPairAnsi rowKeysAnsiPair = RowKeysPairAnsi.GetInstanceFromPairAnsi(rowKeysPairAnsiFile, parallelOptions, FileAccess.ReadWrite, FileShare.ReadWrite))
  89. {
  90. TestByKeys(rowKeysAnsiPair.ConvertValueView(doubleToUOPairConvert.Inverted, double.NaN), doOutOfRangeTest);
  91. }
  92. CreateModelMatrix().ConvertValueView(doubleToUOPairConvert, DensePairAnsi.StaticMissingValue).WriteDensePairAnsi(rowKeysPairAnsiFile, parallelOptions);
  93. using (RowKeysPairAnsi rowKeysRowKeysAnsi = RowKeysPairAnsi.GetInstanceFromPairAnsi(rowKeysPairAnsiFile, parallelOptions, FileAccess.ReadWrite, FileShare.ReadWrite))
  94. {
  95. TestByIndexes(rowKeysRowKeysAnsi.ConvertValueView(doubleToUOPairConvert.Inverted, double.NaN), doOutOfRangeTest);
  96. }
  97. File.Delete(rowKeysPairAnsiFile);
  98. }
  99. /// <summary>
  100. /// Create a new instance of the matrix that the tests expect for input.
  101. /// </summary>
  102. /// <returns>A new instance of the test matrix.</returns>
  103. static public Matrix<string, string, double> CreateModelMatrix()
  104. {
  105. return new DenseMatrix<string, string, double>(new double[,] { { 1, 2, 3 }, { 4, double.NaN, 5 }, { double.NaN, double.NaN, double.NaN } }, new string[] { "A", "B", "C" }, new string[] { "X", "Y", "Z" }, double.NaN);
  106. }
  107. /// <summary>
  108. /// Test the matrix created by the matrixCreator.
  109. /// </summary>
  110. /// <param name="matrixCreator">A function that creates a new test matrix.</param>
  111. /// <param name="doOutOfRangeTest">If true, does a test that throws and catches an exception related to being out of bounds.</param>
  112. public static void TestByKeysAndIndexes(Func<Matrix<string, string, double>> matrixCreator, bool doOutOfRangeTest)
  113. {
  114. TestByKeys(matrixCreator(), doOutOfRangeTest);
  115. TestByIndexes(matrixCreator(), doOutOfRangeTest);
  116. }
  117. /// <summary>
  118. /// Do a series of unit tests in which the matrix is access by rowKey and colKey
  119. /// </summary>
  120. /// <param name="matrix">The matrix to test. It should have the values of the matrix from CreateModelMatrix()</param>
  121. /// <param name="doOutOfRangeTest">If true, does a test that throws and catches an exception related to being out of bounds.</param>
  122. public static void TestByKeys(Matrix<string, string, double> matrix, bool doOutOfRangeTest)
  123. {
  124. //Loop for index and keys
  125. //Get the missing value
  126. double missingValue = matrix.MissingValue;
  127. //TryGetValue - true
  128. double valueAX;
  129. Helper.CheckCondition(matrix.TryGetValue("A", "X", out valueAX) && valueAX == 1);
  130. //TryGetValue - false
  131. double valueCZ;
  132. Helper.CheckCondition(!matrix.TryGetValue("C", "Z", out valueCZ) && matrix.IsMissing(valueCZ));
  133. //TryGetValue that is not in range should throw some exception
  134. if (doOutOfRangeTest)
  135. {
  136. try
  137. {
  138. double valueDW;
  139. matrix.TryGetValue("D", "W", out valueDW);
  140. Helper.CheckCondition(false);
  141. }
  142. catch (Exception)
  143. {
  144. }
  145. }
  146. //SetValueOrMissing - value -> value
  147. matrix.SetValueOrMissing("A", "X", 6);
  148. Helper.CheckCondition(matrix.TryGetValue("A", "X", out valueAX) && valueAX == 6);
  149. //SetValueOrMissing - value -> missing
  150. matrix.SetValueOrMissing("A", "X", double.NaN);
  151. Helper.CheckCondition(!matrix.TryGetValue("A", "X", out valueAX));
  152. //SetValueOrMissing - missing -> value
  153. matrix.SetValueOrMissing("A", "X", 7);
  154. Helper.CheckCondition(matrix.TryGetValue("A", "X", out valueAX) && valueAX == 7);
  155. //SetValueOrMissing - missing -> missing
  156. matrix.SetValueOrMissing("C", "Z", double.NaN);
  157. Helper.CheckCondition(!matrix.TryGetValue("C", "Z", out valueCZ));
  158. //Remove - true
  159. Helper.CheckCondition(matrix.Remove("A", "X"));
  160. //Remove - false
  161. Helper.CheckCondition(!matrix.Remove("A", "X"));
  162. //this get value
  163. Helper.CheckCondition(2 == matrix["A", "Y"]);
  164. //this set to value
  165. matrix["A", "Y"] = 8;
  166. Helper.CheckCondition(8 == matrix["A", "Y"]);
  167. //this get missing - expect error
  168. try
  169. {
  170. double valueCX = matrix["C", "X"];
  171. Helper.CheckCondition(false);
  172. }
  173. catch (Exception)
  174. {
  175. }
  176. //this set to missing - expect error
  177. try
  178. {
  179. matrix["A", "X"] = double.NaN;
  180. Helper.CheckCondition(false);
  181. }
  182. catch (Exception)
  183. {
  184. }
  185. }
  186. /// <summary>
  187. /// Do a series of unit tests in which the matrix is access by rowIndex and colIndex.
  188. /// </summary>
  189. /// <param name="matrix">The matrix to test. It should have the values of the matrix from CreateModelMatrix()</param>
  190. /// <param name="doOutOfRangeTest">If true, does a test that throws and catches an exception related to being out of bounds.</param>
  191. public static void TestByIndexes(Matrix<string, string, double> matrix, bool doOutOfRangeTest)
  192. {
  193. //Loop for index and keys
  194. //Get the missing value
  195. double missingValue = matrix.MissingValue;
  196. //TryGetValue - true
  197. double valueAX;
  198. Helper.CheckCondition(matrix.TryGetValue(0, 0, out valueAX) && valueAX == 1);
  199. //TryGetValue - false
  200. double valueCZ;
  201. Helper.CheckCondition(!matrix.TryGetValue(2, 2, out valueCZ) && matrix.IsMissing(valueCZ));
  202. //TryGetValue that is not in range should throw some exception
  203. if (doOutOfRangeTest)
  204. {
  205. try
  206. {
  207. double valueDW;
  208. matrix.TryGetValue(3, 3, out valueDW);
  209. Helper.CheckCondition(false);
  210. }
  211. catch (Exception)
  212. {
  213. }
  214. }
  215. //SetValueOrMissing - value -> value
  216. matrix.SetValueOrMissing(0, 0, 6);
  217. Helper.CheckCondition(matrix.TryGetValue(0, 0, out valueAX) && valueAX == 6);
  218. //SetValueOrMissing - value -> missing
  219. matrix.SetValueOrMissing(0, 0, double.NaN);
  220. Helper.CheckCondition(!matrix.TryGetValue(0, 0, out valueAX));
  221. //SetValueOrMissing - missing -> value
  222. matrix.SetValueOrMissing(0, 0, 7);
  223. Helper.CheckCondition(matrix.TryGetValue(0, 0, out valueAX) && valueAX == 7);
  224. //SetValueOrMissing - missing -> missing
  225. matrix.SetValueOrMissing(2, 2, double.NaN);
  226. Helper.CheckCondition(!matrix.TryGetValue(2, 2, out valueCZ));
  227. //Remove - true
  228. Helper.CheckCondition(matrix.Remove(0, 0));
  229. //Remove - false
  230. Helper.CheckCondition(!matrix.Remove(0, 0));
  231. //this get value
  232. Helper.CheckCondition(2 == matrix[0, 1]);
  233. //this set to value
  234. matrix[0, 1] = 8;
  235. Helper.CheckCondition(8 == matrix[0, 1]);
  236. //this get missing - expect error
  237. try
  238. {
  239. double valueCX = matrix[2, 0];
  240. Helper.CheckCondition(false);
  241. }
  242. catch (Exception)
  243. {
  244. }
  245. //this set to missing - expect error
  246. try
  247. {
  248. matrix[0, 0] = double.NaN;
  249. Helper.CheckCondition(false);
  250. }
  251. catch (Exception)
  252. {
  253. }
  254. }
  255. }
  256. }