FunctionRandomAccessible issue

Hi @ctrueden @axtimwalde @hanslovsky @NicoKiaru @imagejan,

I have trouble getting the FunctionRandomAccessible to work.

Below is my code and a screenshot showing always some strange pattern as if some pixel values are not computed properly:

		final FloatType floatType = new FloatType();

		BiConsumer< RealLocalizable, FloatType > biConsumer = ( l, t ) -> {
			t.setReal( l.getFloatPosition( 0 ) );
		};

		final Supplier< FloatType > typeSupplier = () -> floatType;

		final FunctionRealRandomAccessible< FloatType > randomAccessible =
				new FunctionRealRandomAccessible( 2, biConsumer, typeSupplier );

		BdvFunctions.show(
				randomAccessible,
				FinalInterval.createMinMax( 0, 0, 0, 1000, 1000, 0),
				"image", BdvOptions.options().is2D() ).setDisplayRange( 0, 1000 );

Does someone have an idea what I am doing wrong?

Thanks you very much!

1 Like

Are you supplying the same instance of FloatType for every position here?

6 Likes

Good question!

I tried to copy and paste from here:

Do you think I am doing something different? Or maybe it also is wrong there?

Anyway that indeed fixes it :slight_smile:

final Supplier< FloatType > typeSupplier = () -> new FloatType();

But you probably don’t want to do this right?
Because it means you are creating a new FloatType at every pixel. Or?

But now it works, and I find it kind of logical, because the data needs to be stored somewhere, isn’t it? So I guess we need actual instances of objects that can hold the values for each pixel.
I think the reason we saw anything sensible in the first place was due to BDV caching…

But I guess we need someone with the authority of @axtimwalde to come to a final conclusion :wink:

I wonder however whether I can be “clever”, because in my use-case (different from the mock example above) there are only very few values occurring, as the image has a step-like appearance. So I could have an Map< Float, FloatType > and return always the same FloatType for the same value to save some memory. Does that make sense?

It’s created for each thread, no ? And one thread performs a lot of pixels. Or is it one per pixel ?

Yes, as far as I know, this is called whenever a RandomAccess is created, should be once per thread.
I also recommend using function reference instead of lambda, i.e.

FloatType::new
4 Likes

To figure out this sort of thing, you could always add a print statement to your lambda. Then you’d know how often it happens. :grin:

3 Likes