Skip to content

Commit f8f3287

Browse files
vector_contours
1 parent b33cdef commit f8f3287

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/.DS_Store

0 Bytes
Binary file not shown.

src/omnipose/dependencies.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'scipy',
55
'edt',
66
'scikit-image',
7-
'ncolor>=1.2.1',
7+
'ncolor>=1.4.2',
88
'scikit-learn',
99
'torch>=1.10',
1010
'torchvision', # redundant from torchvf
@@ -23,7 +23,8 @@
2323
'fastremap', # not sure how I missed this one
2424
'cmap',
2525
'dbscan', # almost 2x faster than sklearn dbscan!
26-
'pyinstrument'
26+
'pyinstrument',
27+
'imagecodecs'
2728
]
2829

2930
# notes: Numpy 2 is close, networkit might be the last dependency needed to upgrade

src/omnipose/plot.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ def setup():
6464
'axes.edgecolor': 'gray',
6565
# Legend defaults – place legend outside axes on the right, no frame
6666
'legend.loc': 'center left',
67-
# 'legend.bbox_to_anchor': (1.02, 0.5), # Removed per instructions
6867
'legend.frameon': False,
6968
'legend.framealpha': 0,
7069
'legend.borderaxespad': 0.0,
70+
'lines.scale_dashes': False
7171
}
7272

7373
# Update rcParams
@@ -1455,10 +1455,23 @@ def get_aspect(ax):
14551455
from scipy.interpolate import splprep, splev
14561456
from matplotlib.collections import PatchCollection
14571457

1458-
def vector_contours(fig,ax,mask, smooth_factor=5, color = 'r', linewidth=1):
1459-
pad = 1
1460-
msk = np.pad(mask,pad,mode='constant')
1458+
def vector_contours(fig,ax,mask, crop=None, smooth_factor=5, color = 'r', linewidth=1,
1459+
y_offset=0, x_offset=0,
1460+
pad=2,
1461+
mode='constant'
1462+
):
14611463

1464+
msk = np.pad(mask,pad,mode='edge')
1465+
1466+
# msk = np.pad(mask,pad,mode=mode)
1467+
1468+
if crop is not None:
1469+
# Crop the mask to the specified region
1470+
# msk = msk[crop]
1471+
y_offset += crop[0].start
1472+
x_offset += crop[1].start
1473+
1474+
msk = np.pad(msk,1,mode='constant', constant_values=0)
14621475

14631476
# set up dimensions
14641477
dim = msk.ndim
@@ -1479,19 +1492,32 @@ def vector_contours(fig,ax,mask, smooth_factor=5, color = 'r', linewidth=1):
14791492
affinity_graph,
14801493
coords,
14811494
neighbors,
1482-
cardinal_only=1)
1495+
cardinal_only=True)
14831496

14841497
# List to hold patches
14851498
patches = []
14861499
for contour in contour_list:
14871500
if len(contour) > 1:
1488-
pts = np.stack([c[contour] for c in coords]).T
1501+
pts = np.stack([c[contour] for c in coords]).T[:, ::-1] # YX to XY
1502+
pts+= np.array([x_offset,y_offset]) # Apply offsets
14891503
tck, u = splprep(pts.T, u=None, s=len(pts)/smooth_factor, per=1)
14901504
u_new = np.linspace(u.min(), u.max(), len(pts))
14911505
x_new, y_new = splev(u_new, tck, der=0)
1506+
14921507

14931508
# Define the points of the polygon
1494-
points = np.column_stack([y_new-pad, x_new-pad])
1509+
# points = np.column_stack([y_new-pad+y_offset, x_new-pad+x_offset])
1510+
# points = np.column_stack([ x_new-pad+x_offset,y_new-pad+y_offset])
1511+
# points = np.column_stack([ x_new-2*pad+x_offset,y_new-2*pad+y_offset])
1512+
# points = np.column_stack([x_new-pad,y_new-pad])
1513+
if isinstance(pad,tuple):
1514+
# If pad is a tuple, apply it to x and y separately
1515+
points = np.column_stack([x_new-(pad[0][0]+1), y_new-(pad[1][0]+1)])
1516+
else:
1517+
points = np.column_stack([x_new-(pad+1),y_new-(pad+1)])
1518+
1519+
1520+
14951521

14961522
# Create a Path from the points
14971523
path = mpath.Path(points, closed=True)
@@ -1511,8 +1537,8 @@ def vector_contours(fig,ax,mask, smooth_factor=5, color = 'r', linewidth=1):
15111537
# Add the PatchCollection to the axis/axes
15121538
if isinstance(ax,list):
15131539
for a in ax:
1514-
patch_collection = PatchCollection(patches, match_original=True)
1540+
patch_collection = PatchCollection(patches, match_original=True, snap=False)
15151541
a.add_collection(patch_collection)
15161542
else:
1517-
patch_collection = PatchCollection(patches, match_original=True)
1543+
patch_collection = PatchCollection(patches, match_original=True, snap=False)
15181544
ax.add_collection(patch_collection)

0 commit comments

Comments
 (0)