Skip to content

Instantly share code, notes, and snippets.

@oeway
Created September 23, 2021 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oeway/6903917a157d6621ff4121bae1ecc663 to your computer and use it in GitHub Desktop.
Save oeway/6903917a157d6621ff4121bae1ecc663 to your computer and use it in GitHub Desktop.
<docs lang="markdown">
[TODO: write documentation for this plugin.]
</docs>
<config lang="json">
{
"name": "HPASingleCellTriton",
"type": "web-python",
"version": "0.1.0",
"description": "[TODO: describe this plugin with one sentence.]",
"tags": [],
"ui": "",
"cover": "",
"inputs": null,
"outputs": null,
"flags": [],
"icon": "extension",
"api_version": "0.1.8",
"env": "",
"permissions": [],
"requirements": ["imageio", "pillow", "pyotritonclient"],
"dependencies": []
}
</config>
<script lang="python">
from imjoy import api
from js import fetch
import io
import numpy as np
import imageio
from PIL import Image
from pyotritonclient import get_config, execute_model
LABELS = {
0: 'Nucleoplasm',
1: 'Nuclear membrane',
2: 'Nucleoli',
3: 'Nucleoli fibrillar center',
4: 'Nuclear speckles',
5: 'Nuclear bodies',
6: 'Endoplasmic reticulum',
7: 'Golgi apparatus',
8: 'Intermediate filaments',
9: 'Actin filaments',
10: 'Microtubules',
11: 'Mitotic spindle',
12: 'Centrosome',
13: 'Plasma membrane',
14: 'Mitochondria',
15: 'Aggresome',
16: 'Cytosol',
17: 'Vesicles and punctate cytosolic patterns',
18: 'Negative',
}
COLORS = ["red", "green", "blue", "yellow"]
async def get_image(image_id):
crops = []
for color in COLORS:
response = await fetch(f'https://images.proteinatlas.org/{image_id}_{color}.jpg')
bytes = await response.arrayBuffer()
bytes = bytes.to_py()
buffer = io.BytesIO(bytes)
buffer.name = '672_E2_1_blue_red_green.jpg'
img = imageio.imread(buffer)
image = Image.fromarray(img)
image = image.resize(size=(340, 340)).convert('L')
image = np.array(image)
crop = image[60:188, 120:248]
crops.append(crop)
image = np.stack(crops, axis=0)
assert image.shape == (4, 128, 128)
return image
async def predict_hpa_image(image_id, threshold=0.5):
image = await get_image('115/672_E2_1')
config = await get_config('https://triton.imjoy.io', 'bestfitting-inceptionv3-single-cell')
results = await execute_model([image.astype('float32')/255], config=config)
classes = results['classes']
pred = [(LABELS[i], prob) for i, prob in enumerate(classes.tolist()) if prob>threshold]
return image, pred
class ImJoyPlugin():
def setup(self):
api.log('initialized')
async def run(self, ctx):
image_id = await api.prompt('Type HPA image id', '115/672_E2_1')
image, pred = await predict_hpa_image(image_id)
await api.alert(f'Prediction: {pred}')
api.export(ImJoyPlugin())
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment