Skip to content

Instantly share code, notes, and snippets.

@hanjinliu
Last active June 23, 2022 05:07
Show Gist options
  • Save hanjinliu/849109604915e89bd7735ecbe3b1e4f1 to your computer and use it in GitHub Desktop.
Save hanjinliu/849109604915e89bd7735ecbe3b1e4f1 to your computer and use it in GitHub Desktop.
Make a microtubule in napari, for illustration.
import napari
import numpy as np
class MtModel:
def __init__(
self,
npf: int = 13,
start: int = 3,
space: float = 4.0,
radius: float = 9.8,
):
self.npf = npf
self.start = start
self.space = space
self.radius = radius
def _make_a_ring(self, offset: int):
theta = np.linspace(0, 2*np.pi, self.npf, endpoint=False)
coords = [
np.linspace(0, self.space*self.start, self.npf, endpoint=False) + self.space*offset,
self.radius * np.cos(theta),
self.radius * np.sin(theta),
]
return np.stack(coords, axis=1)
def polymerize(self, edges: tuple[int, int]) -> np.ndarray:
start, stop = edges
tub_a = np.concatenate([self._make_a_ring(offset=2*i) for i in range(start, stop)], axis=0)
tub_b = np.concatenate([self._make_a_ring(offset=2*i + 1) for i in range(start, stop)], axis=0)
mt = np.concatenate([tub_a, tub_b], axis=0)
return mt
def add_mt(
self,
viewer,
edges: tuple[int, int],
colors=("magenta", "#f693ff"),
name: str = None,
):
mt = self.polymerize(edges)
n = mt.shape[0] // 2
face_color = [colors[0]] * n + [colors[1]] * n
return viewer.add_points(
mt, face_color=face_color, edge_width=0.1, size=5, name=name
)
viewer = napari.Viewer()
model = MtModel()
model.add_mt(viewer, (0, 8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment