Coder Social home page Coder Social logo

Comments (10)

argman avatar argman commented on July 17, 2024

you can assign views to views, like

xt::view(channels, 0) = xt::view(....)

from xtensor.

hassanromhuda avatar hassanromhuda commented on July 17, 2024

you can assign views to views, like

xt::view(channels, 0) = xt::view(....)

I can't get something like this to work. See #2748. Any ideas?

from xtensor.

JohanMabille avatar JohanMabille commented on July 17, 2024

channels needs to be an evaluated expression for having this work.

from xtensor.

OleksiiMatiash avatar OleksiiMatiash commented on July 17, 2024

@argman

you can assign views to views, like

xt::view(channels, 0) = xt::view(....)

If I get this correctly, it compiles, but crashes on assignment:

auto channels = xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 });
auto ch0 = xt::view(activeAreaImageR1, xt::range(0, 2));
auto ch01d = xt::reshape_view(ch0, { activeAreaImageHeight * activeAreaImageWidth / 4 });
xt::view(channels, 0) = ch0;

image

@JohanMabille

channels needs to be an evaluated expression for having this work.

Sorry, I probably didn't get the idea, because this does not work:

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
channels[0] = xt::eval(xt::view(activeAreaImageR1, xt::range(0, 2)));

With any combinations of xt::eval it tells me this:
image

from xtensor.

JohanMabille avatar JohanMabille commented on July 17, 2024

The first snippet should not build since you still try to assign to a view on an unevaluated expression.

Regading the second one, be carefull that operator[] in xtensor is different from operator[] in numpy, see https://github.com/compiler-research/xeus-cpp/actions/runs/6892192995/job/18748906995?pr=19 for the different ways to access elements in xensor expressions. The following should work:

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
xt::view(channels, 0) = xt::eval(xt::view(activeAreaImageR1, xt::range(0, 2)));

(assuming the shapes are compatible).

from xtensor.

OleksiiMatiash avatar OleksiiMatiash commented on July 17, 2024

The first snippet should not build since you still try to assign to a view on an unevaluated expression.

First variant compiles, but crashes on execution of last line.

see https://github.com/compiler-research/xeus-cpp/actions/runs/6892192995/job/18748906995?pr=19

Wrong link, probably?

This compiles (added reshape), but crashes on execution

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
xt::view(channels, 0) = xt::eval(xt::reshape_view(xt::view(activeAreaImageR1, xt::range(0, 2)), { activeAreaImageHeight * activeAreaImageWidth / 4 }));

image

from xtensor.

JohanMabille avatar JohanMabille commented on July 17, 2024

Wrong link, probably?

Indeed, sorry for that, here is the correct one: https://xtensor.readthedocs.io/en/latest/expression.html#element-access

This compiles (added reshape), but crashes on execution

Can you print the shapes of both expressions in the last statement? No need for xt::eval on the right hand side.

from xtensor.

OleksiiMatiash avatar OleksiiMatiash commented on July 17, 2024

Can you print the shapes of both expressions in the last statement? No need for xt::eval on the right hand side.

If I got you correctly (sorry if no), this

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
auto reshapedView = xt::reshape_view(xt::view(activeAreaImageR1, xt::range(0, 2)), { activeAreaImageHeight * activeAreaImageWidth / 4 });
auto shape1 = reshapedView.shape();
auto shape2 = xt::view(channels, 0).shape();

gives this:
image

from xtensor.

JohanMabille avatar JohanMabille commented on July 17, 2024

OK, so the shpaes are compatible, I would try the following:

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
auto image_view = xt::eval(xt::view(activeAreaImageR1, xt::range(0, 2)));
auto reshapedView = xt::reshape_view(image_view, { activeAreaImageHeight * activeAreaImageWidth / 4 });
xt::view(channels, 0)  = reshaped_view;

and the following:

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
auto image_view = xt::eval(xt::view(activeAreaImageR1, xt::range(0, 2)));
auto reshapedView = xt::eval(xt::reshape_view(image_view, { activeAreaImageHeight * activeAreaImageWidth / 4 }));
xt::view(channels, 0)  = reshaped_view;

to see where it crashes. You should also check how activeAreaImageR1 is initialized, and its shape.

from xtensor.

OleksiiMatiash avatar OleksiiMatiash commented on July 17, 2024

This

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
auto image_view = xt::eval(xt::view(activeAreaImageR1, xt::range(0, 2)));
auto reshapedView = xt::reshape_view(image_view, { activeAreaImageHeight * activeAreaImageWidth / 4 });
xt::view(channels, 0)  = reshaped_view;

crashes on the last line:
image

And this:

auto channels = xt::eval(xt::empty<uint16_t>({ 4, activeAreaImageHeight * activeAreaImageWidth / 4 }));
auto image_view = xt::eval(xt::view(activeAreaImageR1, xt::range(0, 2)));
auto reshapedView = xt::eval(xt::reshape_view(image_view, { activeAreaImageHeight * activeAreaImageWidth / 4 }));
xt::view(channels, 0)  = reshaped_view;

on this line:

auto reshapedView = xt::eval(xt::reshape_view(image_view, { activeAreaImageHeight * activeAreaImageWidth / 4 }));

image

You should also check how activeAreaImageR1 is initialized, and its shape.

Full code:

uint16_t* sourceBuffer = new uint16_t[sourceFileInfo->metadata->dataSize / sizeof(uint16_t)];

FILE* fileptr;
fileptr = fopen(filePath.toStdString().c_str(), "rb");
if (fileptr == nullptr) return;

fseek(fileptr, sourceFileInfo->metadata->dataOffset, SEEK_SET);
fread(sourceBuffer, sizeof(uint16_t), sourceFileInfo->metadata->dataSize / sizeof(uint16_t), fileptr);
fclose(fileptr);

std::vector shape = { sourceFileInfo->metadata->dataSize / sizeof(uint16_t) };
QList<int> activeArea = sourceFileInfo->metadata->activeArea;
int activeAreaImageHeight = activeArea[2] - activeArea[0];
int activeAreaImageWidth = activeArea[3] - activeArea[1];

auto source = xt::adapt(sourceBuffer, sourceFileInfo->metadata->dataSize / sizeof(uint16_t), xt::no_ownership(), shape);

source.reshape({ sourceFileInfo->metadata->imageHeight, sourceFileInfo->metadata->imageWidth });
auto activeAreaImage = xt::view(source, xt::range(activeArea[0], activeArea[2]), xt::range(activeArea[1], activeArea[3]));

auto activeAreaImageR1 = xt::view(activeAreaImage, xt::all(), 2);
xt::reshape_view(activeAreaImageR1, { -1 });

xt::xarray<uint16_t> activeAreaImageR2 = xt::view(activeAreaImage, xt::range(1, 2));
xt::reshape_view(activeAreaImageR2, { -1 });

and then the code with problems from original question

from xtensor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.