spatialproteomics 0.7.10__tar.gz → 0.8.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {spatialproteomics-0.7.10/spatialproteomics.egg-info → spatialproteomics-0.8.0}/PKG-INFO +1 -1
  2. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/pyproject.toml +1 -1
  3. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pl/plot.py +4 -0
  4. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pl/utils.py +3 -0
  5. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pp/preprocessing.py +38 -20
  6. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0/spatialproteomics.egg-info}/PKG-INFO +1 -1
  7. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/LICENSE +0 -0
  8. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/README.md +0 -0
  9. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/setup.cfg +0 -0
  10. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/__init__.py +0 -0
  11. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/base_logger.py +0 -0
  12. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/constants.py +0 -0
  13. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/container.py +0 -0
  14. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/image_container/ImageContainer.py +0 -0
  15. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/image_container/__init__.py +0 -0
  16. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/la/__init__.py +0 -0
  17. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/la/label.py +0 -0
  18. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/la/utils.py +0 -0
  19. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/nh/__init__.py +0 -0
  20. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/nh/neighborhood.py +0 -0
  21. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/nh/utils.py +0 -0
  22. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pl/__init__.py +0 -0
  23. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pp/__init__.py +0 -0
  24. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pp/intensity.py +0 -0
  25. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/pp/utils.py +0 -0
  26. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/sd/__init__.py +0 -0
  27. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/sd/utils.py +0 -0
  28. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/tl/__init__.py +0 -0
  29. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/tl/tool.py +0 -0
  30. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics/tl/utils.py +0 -0
  31. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics.egg-info/SOURCES.txt +0 -0
  32. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics.egg-info/dependency_links.txt +0 -0
  33. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics.egg-info/requires.txt +0 -0
  34. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/spatialproteomics.egg-info/top_level.txt +0 -0
  35. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/tests/test_container.py +0 -0
  36. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/tests/test_integration.py +0 -0
  37. {spatialproteomics-0.7.10 → spatialproteomics-0.8.0}/tests/test_to_zarr.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: spatialproteomics
3
- Version: 0.7.10
3
+ Version: 0.8.0
4
4
  Summary: Spatialproteomics is an interoperable toolbox for analyzing highly multiplexed fluorescence image data
5
5
  Author: Matthias Meyer-Bender, Harald Vohringer
6
6
  Requires-Python: >=3.10
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "spatialproteomics"
3
- version = "0.7.10"
3
+ version = "0.8.0"
4
4
  description = "Spatialproteomics is an interoperable toolbox for analyzing highly multiplexed fluorescence image data"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -245,6 +245,10 @@ class PlotAccessor:
245
245
  min_val = obs_colors["min"]
246
246
  max_val = obs_colors["max"]
247
247
 
248
+ # handling the special case where all values in the segmentation are the same,
249
+ # which would lead to a division by zero in the normalization step
250
+ max_val = max_val if max_val > min_val else min_val + 1
251
+
248
252
  # Create the colorbar with dynamic positioning and size
249
253
  sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=min_val, vmax=max_val))
250
254
  sm.set_array([])
@@ -219,6 +219,9 @@ def _render_obs(
219
219
  vmin: float = None,
220
220
  vmax: float = None,
221
221
  ) -> np.ndarray:
222
+ # handling the special case where all values in the segmentation are the same,
223
+ # which would lead to a division by zero in the normalization step
224
+ vmax = vmax if vmax > vmin else vmin + 1
222
225
  # normalize the segmentation to be in the range [0, 1]
223
226
  segmentation = (segmentation - vmin) / (vmax - vmin)
224
227
 
@@ -313,6 +313,7 @@ def filter_by_obs(
313
313
  func: Callable,
314
314
  segmentation_key: str = SDLayers.SEGMENTATION,
315
315
  table_key: str = SDLayers.TABLE,
316
+ reindex: bool = False,
316
317
  copy: bool = False,
317
318
  ):
318
319
  """
@@ -324,6 +325,7 @@ def filter_by_obs(
324
325
  func (Callable): A filtering function that takes in the values of the feature and returns a boolean array.
325
326
  segmentation_key (str): The key of the segmentation mask in the object. Default is SDLayers.SEGMENTATION.
326
327
  table_key (str): The key of the table in the object. Default is SDLayers.TABLE.
328
+ reindex (bool): Whether to reindex the obs after filtering. Default is False.
327
329
  copy (bool): If True, a copy of the object is returned. Default is False.
328
330
  """
329
331
  import spatialdata
@@ -347,13 +349,15 @@ def filter_by_obs(
347
349
  # setting all cells that are not in cells to 0
348
350
  segmentation = _remove_unlabeled_cells(segmentation, cells_sel)
349
351
  # relabeling cells in the segmentation mask so the IDs go from 1 to n again
350
- segmentation, relabel_dict = _relabel_cells(segmentation)
352
+ if reindex:
353
+ segmentation, relabel_dict = _relabel_cells(segmentation)
351
354
 
352
355
  # removing the cells which are not in cells_sel
353
356
  adata = adata[adata.obs[SDFeatures.ID].isin(cells_sel), :].copy()
354
357
  # updating the cell coords of the object
355
- adata.obs[SDFeatures.ID] = [relabel_dict[cell] for cell in adata.obs[SDFeatures.ID].values]
356
- adata.obs.index = [f"Cell_{x}" for x in adata.obs[SDFeatures.ID].values]
358
+ if reindex:
359
+ adata.obs[SDFeatures.ID] = [relabel_dict[cell] for cell in adata.obs[SDFeatures.ID].values]
360
+ adata.obs.index = [f"Cell_{x}" for x in adata.obs[SDFeatures.ID].values]
357
361
 
358
362
  # overwriting the segmentation mask in the object
359
363
  # get transformations
@@ -822,7 +826,7 @@ class PreprocessingAccessor:
822
826
  def add_segmentation(
823
827
  self,
824
828
  segmentation: Union[str, np.ndarray] = None,
825
- reindex: bool = True,
829
+ reindex: bool = False,
826
830
  keep_labels: bool = True,
827
831
  add_obs: bool = True,
828
832
  ) -> xr.Dataset:
@@ -1593,7 +1597,9 @@ class PreprocessingAccessor:
1593
1597
 
1594
1598
  return xr.merge([obj, new_img, new_seg], join="outer", compat="no_conflicts")
1595
1599
 
1596
- def filter_by_obs(self, col: str, func: Callable, segmentation_key: str = Layers.SEGMENTATION):
1600
+ def filter_by_obs(
1601
+ self, col: str, func: Callable, segmentation_key: str = Layers.SEGMENTATION, reindex: bool = False
1602
+ ):
1597
1603
  """
1598
1604
  Filter the object by observations based on a given feature and filtering function.
1599
1605
 
@@ -1601,6 +1607,7 @@ class PreprocessingAccessor:
1601
1607
  col (str): The name of the feature to filter by.
1602
1608
  func (Callable): A filtering function that takes in the values of the feature and returns a boolean array.
1603
1609
  segmentation_key (str): The key of the segmentation mask in the object. Default is Layers.SEGMENTATION.
1610
+ reindex (bool): Whether to reindex the cell IDs after filtering. Default is False.
1604
1611
 
1605
1612
  Returns:
1606
1613
  xr.Dataset: The filtered object with the selected cells and updated segmentation mask.
@@ -1610,7 +1617,6 @@ class PreprocessingAccessor:
1610
1617
 
1611
1618
  Notes:
1612
1619
  - This method filters the object by selecting only the cells that satisfy the filtering condition.
1613
- - It also updates the segmentation mask to remove cells that are not selected and relabels the remaining cells.
1614
1620
 
1615
1621
  Example:
1616
1622
  To filter the object by the feature "area" and keep only the cells with an area greater than 70px:
@@ -1639,10 +1645,11 @@ class PreprocessingAccessor:
1639
1645
  segmentation = obj[segmentation_key].values
1640
1646
  # setting all cells that are not in cells to 0
1641
1647
  segmentation = _remove_unlabeled_cells(segmentation, cells_sel)
1642
- # relabeling cells in the segmentation mask so the IDs go from 1 to n again
1643
- segmentation, relabel_dict = _relabel_cells(segmentation)
1644
- # updating the cell coords of the object
1645
- obj.coords[Dims.CELLS] = [relabel_dict[cell] for cell in obj.coords[Dims.CELLS].values]
1648
+ if reindex:
1649
+ # relabeling cells in the segmentation mask so the IDs go from 1 to n again
1650
+ segmentation, relabel_dict = _relabel_cells(segmentation)
1651
+ # updating the cell coords of the object
1652
+ obj.coords[Dims.CELLS] = [relabel_dict[cell] for cell in obj.coords[Dims.CELLS].values]
1646
1653
 
1647
1654
  # creating a data array with the segmentation mask, so that we can merge it to the original
1648
1655
  da = xr.DataArray(
@@ -1659,7 +1666,11 @@ class PreprocessingAccessor:
1659
1666
  return xr.merge([obj, da], join="outer", compat="no_conflicts")
1660
1667
 
1661
1668
  def remove_outlying_cells(
1662
- self, dilation_size: int = 25, threshold: int = 5, segmentation_key: str = Layers.SEGMENTATION
1669
+ self,
1670
+ dilation_size: int = 25,
1671
+ threshold: int = 5,
1672
+ segmentation_key: str = Layers.SEGMENTATION,
1673
+ reindex: bool = False,
1663
1674
  ):
1664
1675
  """
1665
1676
  Removes outlying cells from the image container. It does so by dilating the segmentation mask and removing cells that belong to a connected component with less than 'threshold' cells.
@@ -1672,6 +1683,8 @@ class PreprocessingAccessor:
1672
1683
  The minimum number of cells in a connected component required for the cells to be kept. Default is 5.
1673
1684
  segmentation_key : str
1674
1685
  The key of the segmentation mask in the object. Default is '_segmentation'.
1686
+ reindex : bool
1687
+ Whether to reindex the cell IDs after filtering. Default is False.
1675
1688
 
1676
1689
  Returns
1677
1690
  -------
@@ -1703,10 +1716,11 @@ class PreprocessingAccessor:
1703
1716
  obj = self._obj.sel(query)
1704
1717
  # setting all cells that are not in cells to 0
1705
1718
  segmentation = _remove_unlabeled_cells(segmentation, cells_sel)
1706
- # relabeling cells in the segmentation mask so the IDs go from 1 to n again
1707
- segmentation, relabel_dict = _relabel_cells(segmentation)
1708
- # updating the cell coords of the object
1709
- obj.coords[Dims.CELLS] = [relabel_dict[cell] for cell in obj.coords[Dims.CELLS].values]
1719
+ if reindex:
1720
+ # relabeling cells in the segmentation mask so the IDs go from 1 to n again
1721
+ segmentation, relabel_dict = _relabel_cells(segmentation)
1722
+ # updating the cell coords of the object
1723
+ obj.coords[Dims.CELLS] = [relabel_dict[cell] for cell in obj.coords[Dims.CELLS].values]
1710
1724
 
1711
1725
  # creating a data array with the segmentation mask, so that we can merge it to the original
1712
1726
  da = xr.DataArray(
@@ -2082,13 +2096,16 @@ class PreprocessingAccessor:
2082
2096
 
2083
2097
  return xr.merge([obj, da], join="outer", compat="no_conflicts")
2084
2098
 
2085
- def mask_cells(self, mask_key: str = Layers.MASK, segmentation_key=Layers.SEGMENTATION) -> xr.Dataset:
2099
+ def mask_cells(
2100
+ self, mask_key: str = Layers.MASK, segmentation_key=Layers.SEGMENTATION, reindex: bool = False
2101
+ ) -> xr.Dataset:
2086
2102
  """
2087
2103
  Mask cells in the segmentation mask.
2088
2104
 
2089
2105
  Parameters:
2090
2106
  mask_key (str): The key of the mask to use for masking.
2091
2107
  segmentation_key (str): The key of the segmentation mask in the object. Default is Layers.SEGMENTATION.
2108
+ reindex (bool): Whether to reindex the cell IDs after filtering. Default is False.
2092
2109
 
2093
2110
  Returns:
2094
2111
  xr.Dataset: The object with the masked cells in the segmentation mask.
@@ -2117,10 +2134,11 @@ class PreprocessingAccessor:
2117
2134
  segmentation = obj[segmentation_key].values
2118
2135
  # setting all cells that are not in cells to 0
2119
2136
  segmentation = _remove_unlabeled_cells(segmentation, cells_sel)
2120
- # relabeling cells in the segmentation mask so the IDs go from 1 to n again
2121
- segmentation, relabel_dict = _relabel_cells(segmentation)
2122
- # updating the cell coords of the object
2123
- obj.coords[Dims.CELLS] = [relabel_dict[cell] for cell in obj.coords["cells"].values]
2137
+ if reindex:
2138
+ # relabeling cells in the segmentation mask so the IDs go from 1 to n again
2139
+ segmentation, relabel_dict = _relabel_cells(segmentation)
2140
+ # updating the cell coords of the object
2141
+ obj.coords[Dims.CELLS] = [relabel_dict[cell] for cell in obj.coords["cells"].values]
2124
2142
 
2125
2143
  # creating a data array with the segmentation mask, so that we can merge it to the original
2126
2144
  da = xr.DataArray(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: spatialproteomics
3
- Version: 0.7.10
3
+ Version: 0.8.0
4
4
  Summary: Spatialproteomics is an interoperable toolbox for analyzing highly multiplexed fluorescence image data
5
5
  Author: Matthias Meyer-Bender, Harald Vohringer
6
6
  Requires-Python: >=3.10