From 795d738a9b8a7efe534098c28d08d9cc12f6cdba Mon Sep 17 00:00:00 2001 From: AlbertDominguez Date: Wed, 18 Dec 2024 23:43:11 +0100 Subject: [PATCH] added annotation script --- extra/annotation_ui.py | 164 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 extra/annotation_ui.py diff --git a/extra/annotation_ui.py b/extra/annotation_ui.py new file mode 100644 index 0000000..d03881a --- /dev/null +++ b/extra/annotation_ui.py @@ -0,0 +1,164 @@ +""" +Starts a napari widget for annotating points on an image starting from LoG candidates or existing results stored in a CSV. + +The widget allows to: +- toggle points visibility +- save points to a csv file +- detect points using LoG detector +- increase/decrease LoG threshold with w/e keys and a slider + +Usage: + python annotation_ui.py [options] +""" +import numpy as np +import napari +from pathlib import Path +from tifffile import imread +from qtpy.QtWidgets import QMessageBox +from csbdeep.utils import normalize +from skimage.feature import blob_log +from magicgui import magicgui +import pandas as pd +import argparse +from spotiflow.utils import read_coords_csv +from spotiflow.utils.fitting import estimate_params + + +KEY_SHORTCUTS = { + 'toggle': ('q', 'toggle points'), + 'save': ('s' , 'save csv'), + 'thr_dec': ('w', 'decrease thr'), + 'thr_inc': ('e', 'increase thr'), + 'detect': ('d', 'detect') + } + +def load_points(path): + path = Path(path) + if path.suffix==".npy": + return np.load(args.points) + elif path.suffix==".csv": + return read_coords_csv(path) + else: + raise ValueError(f'not supported extension {path.suffix}') + + +def save_points(path, arr): + path = Path(path) + print(path) + if path.suffix==".npy": + np.save(path, arr) + elif path.suffix==".csv": + pd.DataFrame(arr, columns=['y','x']).to_csv(path, index=False) + else: + raise ValueError(f'not supported extension {path.suffix}') + +def filter_points_bbox(points, bbox): + """ bbox = ((y1,x1), (y2, x2)) """ + inds = np.bitwise_and( + np.bitwise_and(points[:,0]>=bbox[0,0],points[:,0]=bbox[0,1],points[:,1]