Calling napari from KNIME

Hi,

I have a script (pasted below) to display images in napari. The script uses path information provided by KNIME and I select labels based on some logic in KNIME (thresholds, ML classification, etc.) to display in napari the images the masks of the objects and the selected objects. The code executes perfectly until a napari veier is called. I can call a viewer from the environment so the installation is correct. Any idea what might go wrong?
Thanks

Here is the script:
import knime.scripting.io as knio

Copy input to output

import napari
from tifffile import imread
from skimage import transform, img_as_float, img_as_uint
import numpy as np

input_table_1 = knio.input_tables[0].to_pandas()
input_table_2 = knio.input_tables[1].to_pandas()
image_paths = sorted(input_table_1[‘Path’])
#nuclei_label_paths = sorted(input_table_1[‘NucleiSeg’])
#organoids_label_paths = sorted(input_table_1[‘OrganoidSeg’])
img_405 = np.asarray([imread(img) for img in image_paths if r’Z01C01.tif’ in img])
img_488 = np.asarray([imread(img) for img in image_paths if r’Z01C02.tif’ in img])
img_564 = np.asarray([imread(img) for img in image_paths if r’Z01C03.tif’ in img])
label_nuclei = np.asarray([imread(img) for img in image_paths if ‘nuclei_Seg.tif’ in img]).astype(int)
label_organoids = np.asarray([imread(img) for img in image_paths if ‘organoid_Seg.tif’ in img]).astype(int)
grouped = input_table_2.groupby(‘site’)
live_nuclei_labels = np.zeros(label_nuclei.shape)
for name, group in grouped:
field = int(name[1:])-1
a= np.array(group[‘label’].values).astype(int)
#print(group[‘label’])
mask = np.isin(label_nuclei[field], a)
live_nuclei_labels[field] = label_nuclei[field]*mask
#print(group[‘label’].values)
label_nuclei[label_nuclei>0] = 1

viewer = napari.Viewer()
viewer.add_image(
img_405,
#contrast_limits=[200, 5200],
name=‘Nuclei’,
colormap=‘gray’,
blending=‘additive’
)
viewer.add_image(
img_488,
#contrast_limits=[200, 5200],
name=‘Yap Ab’,
colormap=‘green’,
blending=‘additive’
)
viewer.add_image(
img_564,
#contrast_limits=[200, 5200],
name=‘mCherry’,
colormap=‘red’,
blending=‘additive’
)
viewer.add_labels(
label_nuclei,
name=‘nuclei_labels’,
blending=‘additive’,
opacity=0.6,
)
viewer.add_labels(
live_nuclei_labels.astype(int),
name=‘live nuclei’,
blending=‘additive’,
opacity=0.6,
)
viewer.add_labels(
label_organoids.astype(int),
name=‘Organoids_labels’,
blending=‘additive’,
opacity=0.6,
)

napari.run()

knio.output_tables[0] = knio.Table.from_pandas(input_table_1.copy())

Here is the knime.log:
2024-01-11 11:04:00,272 : ERROR : KNIME-Worker-21-Python Script 3:543 : : Node : Python Script : 3:543 : Error while sending a command.
org.knime.core.node.KNIMEException: Error while sending a command.
at org.knime.python3.scripting.nodes2.PythonScriptNodeModel.execute(PythonScriptNodeModel.java:194)
at org.knime.core.node.NodeModel.executeModel(NodeModel.java:588)
at org.knime.core.node.Node.invokeFullyNodeModelExecute(Node.java:1297)
at org.knime.core.node.Node.execute(Node.java:1059)
at org.knime.core.node.workflow.NativeNodeContainer.performExecuteNode(NativeNodeContainer.java:595)
at org.knime.core.node.exec.LocalNodeExecutionJob.mainExecute(LocalNodeExecutionJob.java:98)
at org.knime.core.node.workflow.NodeExecutionJob.internalRun(NodeExecutionJob.java:201)
at org.knime.core.node.workflow.NodeExecutionJob.run(NodeExecutionJob.java:117)
at org.knime.core.util.ThreadUtils$RunnableWithContextImpl.runWithContext(ThreadUtils.java:367)
at org.knime.core.util.ThreadUtils$RunnableWithContext.run(ThreadUtils.java:221)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at org.knime.core.util.ThreadPool$MyFuture.run(ThreadPool.java:123)
at org.knime.core.util.ThreadPool$Worker.run(ThreadPool.java:246)
Caused by: py4j.Py4JException: Error while sending a command.
at py4j.CallbackClient.sendCommand(CallbackClient.java:397)
at py4j.CallbackClient.sendCommand(CallbackClient.java:356)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:106)
at jdk.proxy17/jdk.proxy17.$Proxy71.execute(Unknown Source)
at org.knime.python3.scripting.nodes2.PythonScriptingSession.execute(PythonScriptingSession.java:255)
at org.knime.python3.scripting.nodes2.PythonScriptNodeModel.lambda$2(PythonScriptNodeModel.java:205)
at org.knime.core.util.ThreadUtils$CallableWithContextImpl.callWithContext(ThreadUtils.java:383)
at org.knime.core.util.ThreadUtils$CallableWithContext.call(ThreadUtils.java:269)
… 3 more
Caused by: py4j.Py4JNetworkException: Error while sending a command: c
t
execute
simport knime.scripting.io as knio\n# Copy input to output\nimport napari\nfrom tifffile import imread\nfrom skimage import transform, img_as_float, img_as_uint\nimport numpy as np\n\ninput_table_1 = knio.input_tables[0].to_pandas()\ninput_table_2 = knio.input_tables[1].to_pandas()\nimage_paths = sorted(input_table_1[‘Path’])\n#nuclei_label_paths = sorted(input_table_1[‘NucleiSeg’])\n#organoids_label_paths = sorted(input_table_1[‘OrganoidSeg’])\nimg_405 = np.asarray([imread(img) for img in image_paths if r’Z01C01.tif’ in img])\nimg_488 = np.asarray([imread(img) for img in image_paths if r’Z01C02.tif’ in img])\nimg_564 = np.asarray([imread(img) for img in image_paths if r’Z01C03.tif’ in img])\nlabel_nuclei = np.asarray([imread(img) for img in image_paths if ‘nuclei_Seg.tif’ in img]).astype(int)\nlabel_organoids = np.asarray([imread(img) for img in image_paths if ‘organoid_Seg.tif’ in img]).astype(int)\ngrouped = input_table_2.groupby(‘site’)\nlive_nuclei_labels = np.zeros(label_nuclei.shape)\nfor name, group in grouped:\n field = int(name[1:])-1\n a= np.array(group[‘label’].values).astype(int)\n #print(group[‘label’])\n mask = np.isin(label_nuclei[field], a)\n live_nuclei_labels[field] = label_nuclei[field]*mask\n#print(group[‘label’].values) \nlabel_nuclei[label_nuclei>0] = 1\n \nviewer = napari.Viewer()\nviewer.add_image(\n img_405,\n #contrast_limits=[200, 5200],\n name=‘Nuclei’,\n colormap=‘gray’,\n blending=‘additive’\n )\nviewer.add_image(\n img_488,\n #contrast_limits=[200, 5200],\n name=‘Yap Ab’,\n colormap=‘green’,\n blending=‘additive’\n )\nviewer.add_image(\n img_564,\n #contrast_limits=[200, 5200],\n name=‘mCherry’,\n colormap=‘red’,\n blending=‘additive’\n )\nviewer.add_labels(\n label_nuclei,\n name=‘nuclei_labels’,\n blending=‘additive’,\n opacity=0.6,\n )\nviewer.add_labels(\n live_nuclei_labels.astype(int),\n name=‘live nuclei’,\n blending=‘additive’,\n opacity=0.6,\n )\nviewer.add_labels(\n label_organoids.astype(int),\n name=‘Organoids_labels’,\n blending=‘additive’,\n opacity=0.6,\n )\n\nnapari.run()\n\n\nknio.output_tables[0] = knio.Table.from_pandas(input_table_1.copy())\n\n
btrue
e

at py4j.ClientServerConnection.sendCommand(ClientServerConnection.java:253)
at py4j.CallbackClient.sendCommand(CallbackClient.java:384)
... 10 more

Caused by: py4j.Py4JException: Received empty command
at py4j.ClientServerConnection.sendCommand(ClientServerConnection.java:236)
… 11 more

@bickle it is difficult to read the code and the log. Maybe you try to attach this in a readable way and also tell us what the code should do.

I see two tables being exported but what about an image?

Maybe you can provide a complete workflow

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.