Skip to content

Instantly share code, notes, and snippets.

View hanjinliu's full-sized avatar

Hanjin Liu hanjinliu

View GitHub Profile
@hanjinliu
hanjinliu / ipython-startup.py
Created September 21, 2022 04:58
Import modules into the IPython shell without overhead
class LazyImporter:
def __init__(self, name: str, alias: str = None):
self._name = name
self._alias = alias or name
def __repr__(self):
return repr(self._mod)
def __str__(self):
return str(self._mod)
@hanjinliu
hanjinliu / magicclass_layerlist_like.py
Created April 28, 2022 09:38
Create a layerlist-like widget for a matplotlib canvas
from magicclass import magicclass, field
from magicclass.widgets import Figure
from magicgui.widgets import CheckBox, Container
@magicclass
class A:
plt = field(Figure)
cbs = field(Container)
def add_plot(self):
line, *_ = self.plt.ax.plot(np.random.random(100))
@hanjinliu
hanjinliu / microtubule_in_napari.py
Last active June 23, 2022 05:07
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,
@hanjinliu
hanjinliu / napari_explorer.py
Created September 9, 2021 15:05
Explorer widget
from __future__ import annotations
from qtpy.QtWidgets import QWidget, QFileSystemModel, QTreeView, QGridLayout, QPushButton, QFileDialog
from qtpy.QtCore import Qt, QModelIndex
import os
from skimage import io
import napari
class Explorer(QWidget):
def __init__(self, viewer:"napari.Viewer", path:str=""):
@hanjinliu
hanjinliu / classgui.py
Created September 5, 2021 14:34
Convert a class into a QWidget and the methods into push buttons.
from __future__ import annotations
import inspect
from magicgui import magicgui
import napari
from qtpy.QtWidgets import QPushButton, QWidget, QVBoxLayout
# Usage:
# >>> viewer = napari.Viewer()
# >>> cgui = ClassGui(viewer)
# >>> @cgui.from_class
@hanjinliu
hanjinliu / bind_protocol.py
Last active August 22, 2021 14:21
Make image analysis protocols in napari from a function
from __future__ import annotations
import inspect
from typing import Callable
import napari
from magicgui.widgets import Label, Table, create_widget, Container
import numpy as np
from skimage.measure import regionprops_table
# GUI class can generate image analysis protocols using decorator @gui.bind_protocol