Skip to content

Commit c6391ca

Browse files
committed
Demonstrate use of np.where #128
1 parent b416875 commit c6391ca

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

test/Numpy.UnitTest/NumpyTest.cs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ public void ndarray_masking1()
351351
" [[15, 16, 17, 18, 19],\n" +
352352
" [20, 21, 22, 23, 24],\n" +
353353
" [25, 26, 27, 28, 29]]])",
354-
x.repr);
354+
x.repr);
355355
var b = np.array(new[,] { { true, true, false }, { false, true, true } });
356356
Assert.AreEqual(
357357
"array([[ 0, 1, 2, 3, 4],\n" +
358358
" [ 5, 6, 7, 8, 9],\n" +
359359
" [20, 21, 22, 23, 24],\n" +
360360
" [25, 26, 27, 28, 29]])",
361-
x[b].repr);
361+
x[b].repr);
362362
}
363363

364364
[TestMethod]
@@ -641,12 +641,11 @@ public void ComplexNumbers()
641641
public void IssueByXlient()
642642
{
643643
var points = new Point[] { new Point(0, 0), new Point(17, 4), new Point(2, 22), new Point(10, 7), };
644-
int[,] Pts = new int[,]
645-
{
646-
{points[0].X, points[0].Y },
647-
{points[1].X, points[1].Y },
648-
{points[2].X, points[2].Y } ,
649-
{points[3].X, points[3].Y }
644+
int[,] Pts = new int[,] {
645+
{ points[0].X, points[0].Y },
646+
{ points[1].X, points[1].Y },
647+
{ points[2].X, points[2].Y },
648+
{ points[3].X, points[3].Y }
650649
};
651650

652651
// exception here / deadlock
@@ -674,7 +673,7 @@ public void IssueByVolgaone()
674673
Assert.AreEqual("1,2,3", string.Join(",", row0Data));
675674
var col1 = n[":,1"]; //extract 1st column - NDarray is [2 5 8] as expected
676675
Assert.AreEqual("array([2., 5., 8.], dtype=float32)", col1.repr);
677-
var col1Data = col1.GetData();//this is wrong - {2,3,4}
676+
var col1Data = col1.GetData(); //this is wrong - {2,3,4}
678677
Assert.AreEqual("2,5,8", string.Join(",", col1Data));
679678
}
680679

@@ -951,8 +950,8 @@ public async Task IssueByMrCOrrupted()
951950
np.savez_compressed(filename, null, arrays);
952951
var archive = np.load(filename);
953952
Console.WriteLine(archive.repr);
954-
var a = new NDarray( archive.PyObject["a"]);
955-
var b = new NDarray( archive.PyObject["b"]);
953+
var a = new NDarray(archive.PyObject["a"]);
954+
var b = new NDarray(archive.PyObject["b"]);
956955
Console.WriteLine(a.repr);
957956
Console.WriteLine(b.repr);
958957
Assert.AreEqual("array([[0, 1, 2],\n [3, 4, 5]])", a.repr);
@@ -962,8 +961,8 @@ public async Task IssueByMrCOrrupted()
962961
[TestMethod]
963962
public async Task AsscalarRemovedInNumpyV1_23()
964963
{
965-
Assert.AreEqual(143, new NDarray<int>(new int[]{143}).asscalar<int>());
966-
Assert.AreEqual(143d, new NDarray<double>(new [] { 143d }).asscalar<double>());
964+
Assert.AreEqual(143, new NDarray<int>(new int[] { 143 }).asscalar<int>());
965+
Assert.AreEqual(143d, new NDarray<double>(new[] { 143d }).asscalar<double>());
967966
Assert.AreEqual(143d, new NDarray<double>(new[] { 143d }).item());
968967
}
969968

@@ -972,7 +971,7 @@ public async Task IssueByMaLichtenegger()
972971
{
973972
// byte array als uint32 array
974973
var bytes = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0 };
975-
var uints =np.zeros(new Shape(3), np.uint32);
974+
var uints = np.zeros(new Shape(3), np.uint32);
976975
Console.WriteLine(uints.repr);
977976
var ctypes = uints.PyObject.ctypes;
978977
long ptr = ctypes.data;
@@ -1030,15 +1029,15 @@ public async Task F16Workaround()
10301029

10311030
// these bytes in binary notation correspond to f16 numbers 1, -1 and 65504 (float16 max value)
10321031
// note, the bytes are in reversed order to the bits shown above
1033-
var bytes = new byte[] {
1032+
var bytes = new byte[] {
10341033
0b00000000, 0b00111100, // 1
10351034
0b00000000, 0b10111100, // -1
10361035
0b11111111, 0b01111011, // 65504
10371036
};
10381037
var floats = np.zeros(new Shape(3), np.float16);
10391038
Console.WriteLine(floats.repr);
10401039
// note, the using prevents a mem-leak with ctypes
1041-
using (var ctypes = floats.PyObject.ctypes) {
1040+
using (var ctypes = floats.PyObject.ctypes) {
10421041
long ptr = ctypes.data;
10431042
Marshal.Copy(bytes, 0, new IntPtr(ptr), bytes.Length);
10441043
}
@@ -1093,8 +1092,31 @@ public async Task IssueByTimiil()
10931092
[ -4.5-2.59807621j, 117. +0.j , -4.5+2.59807621j],
10941093
[ 0. +0.j , -13.5+7.79422863j, 0. +0.j ]]])".Replace("\r", ""), fft_img.repr);
10951094
}
1096-
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#structural-indexing-tools
1097-
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#assigning-values-to-indexed-arrays
1098-
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#dealing-with-variable-numbers-of-indices-within-programs
1095+
1096+
[TestMethod]
1097+
public void IssueByPandath()
1098+
{
1099+
//>>> grid = np.array([[1, 2, 3, 0, 0, 4], [5, 6, 7, 0, 0, 8]])
1100+
//>>> xs, ys = np.where(grid == 0)
1101+
//>>> xs
1102+
//array([0, 0, 1, 1], dtype = int64)
1103+
//>>> ys
1104+
//array([3, 4, 3, 4], dtype = int64)
1105+
1106+
var grid = np.array(new[,] {{ 1, 2, 3, 0, 0, 4 }, { 5, 6, 7, 0, 0, 8 } });
1107+
var result = np.where(grid.equals(0));
1108+
Console.WriteLine(result[0].repr);
1109+
Console.WriteLine(result[1].repr);
1110+
Assert.AreEqual("array([0, 0, 1, 1], dtype=int64)", result[0].repr);
1111+
Assert.AreEqual("array([3, 4, 3, 4], dtype=int64)", result[1].repr);
1112+
}
10991113
}
1114+
1115+
1116+
1117+
1118+
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#structural-indexing-tools
1119+
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#assigning-values-to-indexed-arrays
1120+
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#dealing-with-variable-numbers-of-indices-within-programs
1121+
11001122
}

0 commit comments

Comments
 (0)