Napari Plugin to display csv as image

Hello Napari people,

Thank you for the new plugin feature of Napari, since I had an immediate use case for it I thought it would be a good idea to just ask for help here.

I have a timelapse image and a csv file containing T X Y information (attached).

In a short example program Example program I have the image and the csv file in the jupyter notebook and now I want to open the CSV file in the image and make dots at the location (T X Y ) read from the csv file.

It is possible right to do this in this notebook itself, right?
This is the minimal output from the program (screenshot) and I just want to put dots at the proper TXY:
example.csv (10.8 KB)

1 Like

The current napari builtin csv reader will look for columns named axis-0, axis-1, … etc and use those for the data. We’ll be working on something more flexible promptly.

If you’re in the notebook you could always use something like read_csv from pandas to read the csv and then directly select the columns you want and do to_numpy on the data frame before passing to napari.

Let me know what works best for you or if you’d like a code snippet example for any of the above.

So exciting that you’re giving this a try!!

2 Likes

Wooh! that worked:

Add CSV files to Image as layers in 3D/TimeLapse

This shortens one full chunk of my workflow where the usual way would have been to take csv file to create image and then add it via add channels tool in Fiji, now I can bypass all this and just use the csv files generated in the program directly to display on the loaded image! Just one little thing more you would have to add here, saving image with layers as tiff file (RGB, Float, Integer). But this is super cool addition to jupyter notebook based workflow! Many thanks :slight_smile:

4 Likes

can you expand on that? You can save image layers as tiff with the “Save Layers” dialog… and with code. But are you looking for slightly different behavior?

Gorgeous, @kapoorlab!

One more thing: I notice you are using two points layers, presumably points with different properties — you can actually set the property as a different column in the CSV and colour or size them by property. See this example:

With thanks to @kevinyamauchi!

1 Like

Ah You can save them as RGB? because I was going over the tutorial and in the part about saving images it said :
Napari Tutorial

It is also currently not possible to save images or other layer types directly from the viewer
But I guess that this must be an old tutorial then

I will actually need 5 point layers in the end because I am generating 5 csv files from the program and would like to view them with different colors on the base image and then save the final image with all the layers as a single RGB tiff or individual layers as float or binary tiffs.

You could combine them into a single one by adding an extra column (‘point_type’=[0, 1, 2, 3, 4]), then setting colour to ‘point_type’. But it might be extra work, yes. =)

Do you mean you want to save a “flattened” image that contains the points overlaid on the data? For that, I think you would have to use the viewer.screenshot() method in a loop. e.g.:

images = []
for i in range(Raw.shape[0]):
    viewer.dims.set_point(axis=0, value=i)
    images.append(viewer.screenshot())
tifffile.imsave(np.stack(images))
1 Like

Perfect, just after I replied I thought about merging csv files into one n using only 1 layer.

Yup, I think this does it. In the end a flattened RGB or individual binary layer tiffs is something that would reduce one full step in my workflow. Thanks this saves a lot of time spent on analysis. :+1:

It might be off-topic here on this #napari topic, but: no matter if in Fiji or anywhere else, this sounds like a very suboptimal workflow that you had before. In Fiji/ImageJ, I would read the csv, create a multi-point ROI, and add it as an overlay to the current image. No need to create an image from the points. But of course, glad that you found a good solution using napari :slightly_smiling_face:

Ah actually I should mention that it is a totally python based workflow that generates the csv files, only switching for the last part to Fiji, so keeping it all in Python where users get the final RGB image of dots with different properties n csv file with it makes it easier for them to avoid the step of taking the csv file from python tool in Fiji to generate overlays/dot drawn images . It is just for this specific workflow that makes it easier for the end users :slight_smile: .

1 Like

But I think what @imagejan is getting at is, what’s the purpose of the tiffs? If it’s to look at the data, why not just look at it in napari/Fiji in the same way that you are looking at it now?

The only purpose is to slim the final workflow for the users by reducing the steps they have to take to look at the data.

Currently my python workflow just gives them csv files which then they have to view in Fiji but if I put Napari viewer in the same notebook they have one less step before they can see the data overlaid on their image, save the tiff files they want for records.

Putting this viewer n saving files like they want in the same notebook that is generating the csv files saves the overall workload on them, end result is the same but now 1 step less than before for them. I should also point out that the workflow I am mentioning here does not use the notebook I made here, which was just a minimalist example to show what I want to do by making napari a part of my original workflow.

1 Like

Just to add to the code to save images as flattened tiffs, this worked:

        images = []
        for i in range(Raw.shape[0]):
           viewer.dims.set_point(axis=0, value=i)
           images.append(viewer.screenshot())
        tifffile.imsave(save_dir + 'Event' + '.tif', np.asarray(images))