@@ -270,153 +270,3 @@ def post_proc_cellpose(
270270 return inst_map , hsv_flows
271271
272272 return inst_map
273-
274-
275- # def get_masks_cellpose(
276- # p: np.ndarray,
277- # mask: np.ndarray,
278- # rpad: int = 20,
279- # ) -> np.ndarray:
280- # """Create masks using pixel convergence after running dynamics.
281-
282- # Makes a histogram of final pixel locations p, initializes masks
283- # at peaks of histogram and extends the masks from the peaks so that
284- # they include all pixels with more than 2 final pixels p. Discards
285- # masks with flow errors greater than the threshold.
286-
287- # Parameters
288- # ----------
289- # p : np.ndarray
290- # Final locations of each pixel after dynamics. Shape (2, H, W).
291- # mask : np.ndarray
292- # The binary mask of the cells. Shape (H, W).
293- # rpad : int, default=20
294- # Histogram edge padding.
295-
296- # Returns
297- # -------
298- # np.ndarray:
299- # Instance labelled mask. Shape (H, W).
300- # """
301- # shape0 = p.shape[1:]
302- # dims = len(p)
303-
304- # inds = np.meshgrid(np.arange(shape0[0]), np.arange(shape0[1]), indexing="ij")
305-
306- # for i in range(dims):
307- # p[i, ~mask] = inds[i][~mask]
308-
309- # pflows = []
310- # edges = []
311- # for i in range(dims):
312- # pflows.append(p[i].flatten().astype("int32"))
313- # edges.append(np.arange(-0.5 - rpad, shape0[i] + 0.5 + rpad, 1))
314-
315- # h, _ = np.histogramdd(tuple(pflows), bins=edges)
316- # hmax = h.copy()
317- # for i in range(dims):
318- # hmax = maximum_filter1d(hmax, 5, axis=i)
319-
320- # seeds = np.nonzero(np.logical_and(h - hmax > -1e-6, h > 10))
321- # Nmax = h[seeds]
322- # isort = np.argsort(Nmax)[::-1]
323- # for s in seeds:
324- # s = s[isort]
325-
326- # pix = list(np.array(seeds).T)
327- # shape = h.shape
328- # expand = np.nonzero(np.ones((3, 3)))
329- # for e in expand:
330- # e = np.expand_dims(e, 1)
331-
332- # for iter in range(5):
333- # for k in range(len(pix)):
334- # if iter == 0:
335- # pix[k] = list(pix[k])
336- # newpix = []
337- # iin = []
338- # for i, e in enumerate(expand):
339- # epix = e[:, None] + np.expand_dims(pix[k][i], 0) - 1
340- # epix = epix.flatten()
341- # iin.append(np.logical_and(epix >= 0, epix < shape[i]))
342- # newpix.append(epix)
343- # iin = np.all(tuple(iin), axis=0)
344- # for p in newpix:
345- # p = p[iin]
346- # newpix = tuple(newpix)
347- # igood = h[newpix] > 2
348- # for i in range(dims):
349- # pix[k][i] = newpix[i][igood]
350- # if iter == 4:
351- # pix[k] = tuple(pix[k])
352-
353- # M = np.zeros(h.shape, np.int32)
354- # for k in range(len(pix)):
355- # M[pix[k]] = 1 + k
356-
357- # for i in range(dims):
358- # pflows[i] = pflows[i] + rpad
359-
360- # # remove big masks
361- # M0 = M[tuple(pflows)]
362- # _, counts = np.unique(M0, return_counts=True)
363- # big = np.prod(shape0) * 1.0
364- # for i in np.nonzero(counts > big)[0]:
365- # M0[M0 == i] = 0
366-
367- # _, M0 = np.unique(M0, return_inverse=True)
368- # M0 = np.reshape(M0, shape0)
369-
370- # return M0
371-
372-
373- # def post_proc_cellpose(
374- # inst_map: np.ndarray,
375- # flow_map: np.ndarray,
376- # dist_map: np.ndarray = None,
377- # return_flows: bool = False,
378- # min_size: int = 30,
379- # **kwargs
380- # ) -> np.ndarray:
381- # """Run the cellpose post-processing pipeline.
382-
383- # https://www.nature.com/articles/s41592-020-01018-x
384-
385- # Parameters
386- # ----------
387- # inst_map : np.ndarray
388- # Instance labelled or binary mask. Shape (H, W).
389- # flow_map : np.ndarray
390- # Y- and x-flows. Shape: (2, H, W)
391- # dist_map : np.ndarray, default=None
392- # Regressed distance transform. Shape: (H, W).
393- # return_flows : bool, default=False
394- # If True, returns the HSV converted flows. They are just not
395- # needed for anything relevant.
396- # min_size : int
397- # The minimum size for the objects that will not be removed.
398-
399- # Returns
400- # -------
401- # np.ndarray:
402- # The instance labelled segmentation mask. Shape (H, W)
403- # """
404- # # convert channels to CHW
405- # if dist_map is not None:
406- # binary_mask = apply_hysteresis_threshold(dist_map, 0.5, 0.5)
407- # else:
408- # binary_mask = binarize(inst_map).astype(bool)
409-
410- # dP = flow_map * binary_mask # /5.
411- # # dP = normalize_field(dP)
412-
413- # pixel_loc, _ = follow_flows(dP, niter=200, mask=binary_mask, suppress_euler=False)
414-
415- # mask = get_masks_cellpose(p=pixel_loc, mask=binary_mask)
416- # inst_map = fill_holes_and_remove_small_masks(mask, min_size=min_size).astype("i4")
417-
418- # if return_flows:
419- # hsv_flows = gen_flows(dP)
420- # return inst_map, hsv_flows
421-
422- # return inst_map
0 commit comments