Skip to content

Commit

Permalink
allow count spots in cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanguage committed Apr 22, 2024
1 parent adf6abc commit beff518
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
57 changes: 52 additions & 5 deletions public/plugins/chatbot-extension.imjoy.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script lang="javascript">
const chatbot_url = "https://bioimage.io/chat/"
const ufish_url = "https://ufish-team.github.io/"
const utils_url = "http://localhost:5173/plugins/chatbot-utils.imjoy.html"
const utils_url = ufish_url + "/plugins/chatbot-utils.imjoy.html"
const py_console_url = "https://nanguage.github.io/web-python-console/"

class ImJoyPlugin {
Expand Down Expand Up @@ -170,18 +170,18 @@
The extension allow chatbot execute Python code in Pyodide environment.
Code reference:
+ The `api` is the ImJoy API object, it's a global variable in the Pyodide environment.
+ You can get the spots (numpy array) using this code:
+ The \`api\` is the ImJoy API object, it's a global variable in the Pyodide environment.
+ Get the spots (numpy array) of U-FISH result:
\`\`\`
ufish = await api.getWindow("ufish")
spots = (await ufish.getOutput())['spots']
\`\`\`
+ You can get the input image (numpy array) using this code:
+ Get the input image (numpy array) of U-FISH:
\`\`\`
ufish = await api.getWindow("ufish")
input_img = await ufish.getInputImage()
\`\`\`
+ You can get the enhanced image (numpy array) using this code:
+ Get the enhanced image (numpy array) of U-FISH:
\`\`\`
ufish = await api.getWindow("ufish")
enhanced_img = (await ufish.getOutput())['enhanced']
Expand Down Expand Up @@ -215,6 +215,53 @@
},
}))

// Extension allow count spots in cells
await register({
_rintf: true,
name: "countSpotsInCells",
description: "The extension allow chatbot count spots in cells",
get_schema: async () => {
return {
type: "object",
title: "countSpotsInCells",
description: "Count spots in cells",
properties: {
"use_selected": {
type: "boolean",
description: "Use selected cells",
default: false,
},
},
};
},
execute: async (config) => {
const spots = this.ufish_spots
if (!spots) {
return "Please run U-FISH first"
}
const anno_layer = this.annotation_layer
if (!anno_layer) {
return "Please run cellpose first"
}
const utils = await this.getChatbotUtils()
const client = await this.getUfishClient()
const img = await client.getInputImage()
if (!img) {
return "Please open an image first"
}
const img_shape = img._rshape
let cell_features;
if (config.use_selected) {
cell_features = await anno_layer.get_selected_features()
} else {
cell_features = await anno_layer.get_features()
}
const spots_in_cells = await utils.count_spots_in_cells(
spots, img_shape, cell_features)
return "Number of spots in cells: " + spots_in_cells
},
})

api.log('initialized')
}

Expand Down
19 changes: 19 additions & 0 deletions public/plugins/chatbot-utils.imjoy.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from imjoy_rpc.hypha import connect_to_server
from imjoy import api
from skimage.measure import find_contours, approximate_polygon
from skimage.draw import polygon2mask

SERVER_URL = "https://ai.imjoy.io"

Expand Down Expand Up @@ -92,6 +93,24 @@
async def get_coutours(self):
return self.coutours

async def count_spots_in_cells(self, spots, img_shape, features):
features = features['features']
counts = []
if len(img_shape) == 3:
img_shape = img_shape[:2]
print(img_shape)
for feature in features:
coords = feature['geometry']['coordinates'][0]
polygon = np.array(coords, dtype=np.int32)
# swap x and y
polygon = np.c_[polygon[:, 1], polygon[:, 0]]
print(polygon)
mask = polygon2mask(img_shape, polygon)
mask = mask.astype(np.uint8)
count = int(mask[spots[:, 0], spots[:, 1]].sum())
counts.append(count)
return counts

async def run(self, ctx):
pass

Expand Down

0 comments on commit beff518

Please sign in to comment.