|
| 1 | +using Dynamo; |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Numerics; |
| 7 | +using CoreNodeModels.Input; |
| 8 | +using Dynamo; |
| 9 | +using NUnit.Framework; |
| 10 | +using static DSPythonTests.PythonEvalTests; |
| 11 | +using Directory = CoreNodeModels.Input.Directory; |
| 12 | + |
| 13 | + |
| 14 | +namespace DynamoPythonTests |
| 15 | +{ |
| 16 | + internal class PythonModulesTests : DynamoModelTestBase |
| 17 | + { |
| 18 | + protected override void GetLibrariesToPreload(List<string> libraries) |
| 19 | + { |
| 20 | + libraries.Add("FFITarget.dll"); |
| 21 | + libraries.Add("DSCoreNodes.dll"); |
| 22 | + } |
| 23 | + |
| 24 | + public IEnumerable<PythonEvaluatorDelegate> Evaluators = new List<PythonEvaluatorDelegate> { |
| 25 | + DSCPython.CPythonEvaluator.EvaluatePythonScript |
| 26 | + }; |
| 27 | + |
| 28 | + #region Python Modules |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void TestNumpyPythonModuleEncoding() |
| 32 | + { |
| 33 | + string code = @" |
| 34 | +import sys |
| 35 | +import clr |
| 36 | +
|
| 37 | +import numpy as np |
| 38 | +a = np.arange(15).reshape(3, 5) |
| 39 | +result = [] |
| 40 | +for list in a: |
| 41 | + sub_result = [] |
| 42 | + for b in list: |
| 43 | + # Cast to int, otherwise getting DynamoCPythonHandle |
| 44 | + sub_result.append(int(b)) |
| 45 | + result.append(sub_result) |
| 46 | +OUT = result |
| 47 | +"; |
| 48 | + var empty = new ArrayList(); |
| 49 | + var expected = new ArrayList { new ArrayList { 0, 1, 2, 3, 4 }, new ArrayList { 5, 6, 7, 8, 9 }, new ArrayList { 10, 11, 12, 13, 14 } }; |
| 50 | + |
| 51 | + foreach (var pythonEvaluator in Evaluators) |
| 52 | + { |
| 53 | + var result = pythonEvaluator(code, empty, empty); |
| 54 | + Assert.IsTrue(result is IEnumerable); |
| 55 | + CollectionAssert.AreEqual(expected, result as IEnumerable); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + [Test] |
| 61 | + public void TestPandasPythonModuleEncoding() |
| 62 | + { |
| 63 | + string code = @" |
| 64 | +import sys |
| 65 | +import clr |
| 66 | +
|
| 67 | +import numpy as np |
| 68 | +import pandas as pd |
| 69 | +dates = pd.date_range(""20130101"", periods=6) |
| 70 | +a = [str(i) for i in dates] |
| 71 | +OUT = a |
| 72 | +"; |
| 73 | + var empty = new ArrayList(); |
| 74 | + var expected = new ArrayList |
| 75 | + { |
| 76 | + "2013-01-01 00:00:00", |
| 77 | + "2013-01-02 00:00:00", |
| 78 | + "2013-01-03 00:00:00", |
| 79 | + "2013-01-04 00:00:00", |
| 80 | + "2013-01-05 00:00:00", |
| 81 | + "2013-01-06 00:00:00", |
| 82 | + }; |
| 83 | + |
| 84 | + foreach (var pythonEvaluator in Evaluators) |
| 85 | + { |
| 86 | + var result = pythonEvaluator(code, empty, empty); |
| 87 | + Assert.IsTrue(result is IEnumerable); |
| 88 | + CollectionAssert.AreEqual(expected, result as IEnumerable); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Test] |
| 93 | + public void TestScypiPythonModuleEncoding() |
| 94 | + { |
| 95 | + string code = @" |
| 96 | +import sys |
| 97 | +import clr |
| 98 | +
|
| 99 | +import numpy as np |
| 100 | +from scipy import linalg |
| 101 | +A = np.array([[1,2],[3,4]]) |
| 102 | +B = linalg.inv(A) |
| 103 | +C = [[round(i) for i in b] for b in B] |
| 104 | +OUT = C |
| 105 | +"; |
| 106 | + var empty = new ArrayList(); |
| 107 | + var expected = new ArrayList { new ArrayList { -2, 1 }, new ArrayList { 1, 0 } }; |
| 108 | + |
| 109 | + foreach (var pythonEvaluator in Evaluators) |
| 110 | + { |
| 111 | + var result = pythonEvaluator(code, empty, empty); |
| 112 | + Assert.IsTrue(result is IEnumerable); |
| 113 | + CollectionAssert.AreEqual(expected, result as IEnumerable); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + [Test] |
| 118 | + public void TestOpenpyxlPythonModuleEncoding() |
| 119 | + { |
| 120 | + string code = @" |
| 121 | +import sys |
| 122 | +import clr |
| 123 | +
|
| 124 | +from openpyxl import Workbook |
| 125 | +
|
| 126 | +wb = Workbook() |
| 127 | +ws = wb.active |
| 128 | +
|
| 129 | +ws1 = wb.create_sheet(""Mysheet"") # insert at the end (default) |
| 130 | +ws.title = ""New Title"" |
| 131 | +ws.sheet_properties.tabColor = ""1072BA"" |
| 132 | +
|
| 133 | +result = wb.sheetnames |
| 134 | +OUT = result |
| 135 | +"; |
| 136 | + var empty = new ArrayList(); |
| 137 | + var expected = new ArrayList { "New Title", "Mysheet" }; |
| 138 | + |
| 139 | + foreach (var pythonEvaluator in Evaluators) |
| 140 | + { |
| 141 | + var result = pythonEvaluator(code, empty, empty); |
| 142 | + Assert.IsTrue(result is IEnumerable); |
| 143 | + CollectionAssert.AreEqual(expected, result as IEnumerable); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + [Test] |
| 149 | + public void TestPillowPythonModuleEncoding() |
| 150 | + { |
| 151 | + var examplePath = Path.Combine(TestDirectory, @"core\python\python_modules", "test.png"); |
| 152 | + Assert.NotNull(examplePath); |
| 153 | + Assert.IsTrue(File.Exists(examplePath)); |
| 154 | + examplePath = examplePath.Replace("\\", "/"); // Convert to python file path |
| 155 | + |
| 156 | + string code = string.Format(@" |
| 157 | +import sys |
| 158 | +import clr |
| 159 | +
|
| 160 | +from PIL import Image |
| 161 | +
|
| 162 | +im = Image.open('{0}') |
| 163 | +OUT = im.format, im.size, im.mode |
| 164 | +", examplePath); |
| 165 | + |
| 166 | + |
| 167 | + var empty = new ArrayList(); |
| 168 | + var expected = new ArrayList{ "PNG", new ArrayList { 640, 480 }, "RGBA" }; |
| 169 | + |
| 170 | + foreach (var pythonEvaluator in Evaluators) |
| 171 | + { |
| 172 | + var result = pythonEvaluator(code, empty, empty); |
| 173 | + Assert.IsTrue(result is IEnumerable); |
| 174 | + CollectionAssert.AreEqual(expected, result as IEnumerable); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + [Test] |
| 180 | + public void TestMatplotlibPythonModuleEncoding() |
| 181 | + { |
| 182 | + var examplePath = Path.Combine(TestDirectory, @"core\python\python_modules", "test_export.png"); |
| 183 | + Assert.NotNull(examplePath); |
| 184 | + Assert.IsFalse(File.Exists(examplePath)); // Make sure the file is not there when we start |
| 185 | + examplePath = examplePath.Replace("\\", "/"); // Convert to python file path |
| 186 | + |
| 187 | + string code = string.Format(@" |
| 188 | +import sys |
| 189 | +import clr |
| 190 | +
|
| 191 | +import matplotlib.pyplot as plt |
| 192 | +import numpy as np |
| 193 | +
|
| 194 | +x = np.linspace(0, 2 * np.pi, 200) |
| 195 | +y = np.sin(x) |
| 196 | +
|
| 197 | +fig, ax = plt.subplots() |
| 198 | +ax.plot(x, y) |
| 199 | +plt.show() |
| 200 | +image = plt.savefig('{0}') |
| 201 | +
|
| 202 | +OUT = '' |
| 203 | +", examplePath); |
| 204 | + |
| 205 | + |
| 206 | + var empty = new ArrayList(); |
| 207 | + |
| 208 | + foreach (var pythonEvaluator in Evaluators) |
| 209 | + { |
| 210 | + var result = pythonEvaluator(code, empty, empty); |
| 211 | + Assert.IsTrue(File.Exists(examplePath)); // We should have exported an image now |
| 212 | + File.Delete(examplePath); |
| 213 | + Assert.IsFalse(File.Exists(examplePath)); // Make sure the file is not there when we finish |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + #endregion |
| 218 | + } |
| 219 | +} |
0 commit comments