Quansight-Labs / numpy.net

A port of NumPy to .Net
BSD 3-Clause "New" or "Revised" License
131 stars 14 forks source link

how to port this python code to c# ? #7

Closed BackT0TheFuture closed 3 years ago

BackT0TheFuture commented 3 years ago
img = np.fromfile('test.dat',sep=',')
img = img.reshape(10,10,3)
# print(img)
output = img.copy()
valid = np.all(img == [ 255, 255, 240], axis = -1)
rs, cs = valid.nonzero()
output[rs, cs, :] = [255, 255, 255]
print(output)

currently I can not get correct result via c# thanks for your help!

test.zip

KevinBaselinesw commented 3 years ago

Here is a unit test that produces the same result that I get on python. Please note: The very last where you assign 255, 255, 255 will not work with the latest code on nuget. I am working on a new release that fixes this problem.

    [TestMethod]
    public void goodtogoodTest()
    {

        var img = np.fromfile("test.dat", sep : ",");
        img = img.reshape(10, 10, 3);
        print(img);
        var output = img.Copy();

        ndarray equalFlags = np.equal(img, np.array(new float[] { 255, 255, 240 }));
        ndarray valid = np.all(equalFlags, axis : -1);
        ndarray[] rscs = valid.NonZero();

--> wait for the next release for this to work correctly. //output[rscs[0], rscs[1], ":"] = np.array(new float[] { 255, 255, 255 }); // works OK output[rscs[0], rscs[1], ":"] = new float[] { 255, 255, 255 }; // works OK --> wait for the next release for this to work correctly.
print(output); return;

    }
KevinBaselinesw commented 3 years ago
  public void goodgoodTest()
    {
        var img = np.fromfile("test.dat", sep : ",");
        img = img.reshape(10, 10, 3);
        print(img);
        var output = img.Copy();

        ndarray equalFlags = np.equal(img, np.array(new float[] { 255, 255, 240 }));
        ndarray valid = np.all(equalFlags, axis : -1);
        ndarray[] rscs = valid.NonZero();

        //output[rscs[0], rscs[1], ":"] = np.array(new float[] { 255, 255, 255 }); // works OK
        output[rscs[0], rscs[1], ":"] = new float[] { 255, 255, 255 };             // works OK

        print(output);
        return;

    }
KevinBaselinesw commented 3 years ago

I just pushed up 0.9.61. It has a bug fix that makes the sample code work. Please get it from nuget.org It also has huge performance improvements.

BackT0TheFuture commented 3 years ago

@KevinBaselinesw

It works correctly now , thank you so much!

BTW: same name but different case , Is it designed ?

        public object copy(NPY_ORDER order = NPY_ORDER.NPY_CORDER) {
            return ArrayReturn(Copy(order));
        }

        public ndarray Copy(NPY_ORDER order = NPY_ORDER.NPY_CORDER) {
            return NpyCoreApi.NewCopy(this, order);
        }
KevinBaselinesw commented 3 years ago

This project was started by a different team many years ago and then abandoned. They left it in open source where I picked it up and finished it. There is a fair amount of legacy code floating around that I did not build. I stripped out a huge amount of it. I agree that copy vs Copy seems weird. I should probably make another cleanup pass at some point.

BackT0TheFuture commented 3 years ago

got it . thanks for your great work !