Skip to content

Shapely 1.8.5 lead to AttributeError: 'Polygon' object has no attribute 'uid' #2

@lydiayliu

Description

@lydiayliu

Using cellvit-inference with version 1.8.5 of Shapely resulted in the following error:

Traceback (most recent call last):
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/bin/cellvit-inference", line 8, in <module>
    sys.exit(main())
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/lib/python3.10/site-packages/cellvit/detect_cells.py", line 50, in main
    celldetector.process_wsi(
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/lib/python3.10/site-packages/cellvit/inference/inference.py", line 864, in process_wsi
    keep_idx = self._post_process_edge_cells(cell_list=cell_dict_wsi)
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/lib/python3.10/site-packages/cellvit/inference/inference.py", line 507, in _post_process_edge_cells
    cleaned_cells = cell_cleaner.clean_detected_cells()
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/lib/python3.10/site-packages/cellvit/inference/overlap_cell_cleaner.py", line 76, in clean_detected_cells
    cleaned_edge_cells = self._remove_overlap(cleaned_edge_cells)
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/lib/python3.10/site-packages/cellvit/inference/overlap_cell_cleaner.py", line 141, in _remove_overlap
    merged_cells = self._remove_overlap_shapely_1_8(
  File "/nemo/lab/swantonc/working/liul/.conda/envs/cellvit_env/lib/python3.10/site-packages/cellvit/inference/overlap_cell_cleaner.py", line 191, in _remove_overlap_shapely_1_8
    if query_poly.uid not in iterated_cells:
AttributeError: 'Polygon' object has no attribute 'uid'

This is my version of Shapely confirmed:

$pip show shapely
Name: Shapely
Version: 1.8.5.post1

This was the command run:

cellvit-inference \
    --model SAM \
    --nuclei_taxonomy pannuke \
    --batch_size 8 \
    --cpu_count 32 \
    --ray_worker 6 \
    --ray_remote_cpus 4 \
    --outdir ./test/JP2K-33003-2/ \
    process_wsi \
    --wsi_path ./test/JP2K-33003-2/JP2K-33003-2.svs \
    --wsi_mpp 0.25 \
    --wsi_magnification 40

At a glance, this seems to be because the uid attribute has been commented out from the function?

    def _remove_overlap_shapely_1_8(
        self, cleaned_edge_cells: pd.DataFrame
    ) -> pd.DataFrame:
        """Remove overlapping cells from provided DataFrame

        Args:
            cleaned_edge_cells (pd.DataFrame): DataFrame that should be cleaned

        Returns:
            pd.DataFrame: Cleaned DataFrame
        """
        merged_cells = cleaned_edge_cells

        for iteration in range(20):
            poly_list = []
            for idx, cell_info in merged_cells.iterrows():
                poly = Polygon(cell_info["contour"])
                if not poly.is_valid:
                    self.logger.debug("Found invalid polygon - Fixing with buffer 0")
                    multi = poly.buffer(0)
                    if isinstance(multi, MultiPolygon):
                        if len(multi) > 1:
                            poly_idx = np.argmax([p.area for p in multi])
                            poly = multi[poly_idx]
                            poly = Polygon(poly)
                        else:
                            poly = multi[0]
                            poly = Polygon(poly)
                    else:
                        poly = Polygon(multi)
                # poly.uid = idx
                poly_list.append(poly)

            # use an strtree for fast querying
            tree = strtree.STRtree(poly_list)

            merged_idx = deque()
            iterated_cells = set()
            overlaps = 0

            for query_poly in poly_list:
                if query_poly.uid not in iterated_cells:

Uncommenting # poly.uid = idx solved the issue

Does the developer recommend upgrading to Shapely version >= 2.0.0?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions