TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization

TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: gpio on Oct. 26, 2024, 1:49 p.m.

I’m encountering an issue while running my Python code for a project involving 3D point annotations.
annotations_wsd = to_wsd(annotations)

The code is supposed to create Point objects with specific attributes, including index, label, and coordinates. However, I’m getting the following error: TypeError: __new__() got an unexpected keyword argument 'index'

After checking the structures.py in the utils folder, I suspect it may be related to compatibility issues with specific library versions, especially shapely, wholeslidedata, or creationism.

My Environment: Python version: 3.8 Shapely version: 2.0.5

Any guidance on resolving this new argument compatibility issue or suggestions for alternative approaches would be greatly appreciated! Thank you in advance!

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: LindaSt on Oct. 28, 2024, 2:25 p.m.

Hi

Could you please share a bit more information? It would be helpful to see your whole script and the whole error traceback.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: vj2212 on Oct. 28, 2024, 4:29 p.m.

Hi also getting this when running 3_inference.ipynb notebook.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[12], line 13
      6 # Recreate your model with the updated code
      7 model = Detectron2DetectionPredictor(
      8     output_dir=output_path,
      9     threshold= 0.1,
     10     nms_threshold=0.3,
     11     weight_root = "./outputs/model_final.pth"
     12 )
---> 13 inference(
     14     iterator=iterator,
     15     predictor=model,
     16     spacing=spacings[0],
     17     image_path=image_path,
     18     output_path=output_path,
     19     json_filename=json_filename
     20 )
     22 iterator.stop()

Cell In[11], line 59
     56 print("saving predictions...")
     58 # saving xml file
---> 59 annotations_wsd = to_wsd(annotations)
     60 xml_filename = 'points_results.xml'
     61 output_path_xml = os.path.join(output_path,xml_filename)

Cell In[10], line 8
      6 new_points = []
      7 for i, point in enumerate(points):
----> 8     p = Point(
      9         index=i,
     10         label=Label("lymphocyte", 1, color="blue"),
     11         coordinates=[point],
     12     )
     13     new_points.append(p)
     14 return new_points

TypeError: Point.__new__() got an unexpected keyword argument 'index'
 Last edited by: vj2212 on Oct. 28, 2024, 4:29 p.m., edited 1 time in total.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: LindaSt on Oct. 29, 2024, 11:39 a.m.

Can you show me your imports?

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: gpio on Oct. 30, 2024, 2:27 a.m.

Here's my import:

import os
import json
import torch
import creationism
from tqdm import tqdm
from wholeslidedata.interoperability.asap.annotationwriter import write_point_set
from wholeslidedata.image.wholeslideimage import WholeSlideImage
from wholeslidedata.iterators import create_patch_iterator, PatchConfiguration
from wholeslidedata.annotation.labels import Label
from utils.wsdetectron2 import Detectron2DetectionPredictor
from utils.structures import Point
import numpy as np

image_path = r'/monkey-dataset/images/pas-cpg/A_P000001_PAS_CPG.tif'
mask_path = r'/monkey-dataset/images/tissue-masks/A_P000001_mask.tif'
output_path = r"./outputs/results"

I also printed the predicted points, they seem to be correct.

Index: 0, Coordinates: (10370.0, 86372.0)
Index: 1, Coordinates: (10418.0, 86370.0)
Index: 2, Coordinates: (10394.0, 86444.0)
...

The error happens when running the function to_wsd

        p = Point(
            index=i,
            label=Label("lymphocyte", 1, color="blue"),
            coordinates=[point],
        )
 Last edited by: gpio on Oct. 30, 2024, 2:30 a.m., edited 1 time in total.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: LindaSt on Oct. 30, 2024, 2:34 p.m.

Did you make any changes to structures.py? At the moment two issues seem the most logical to me, either you are not using the Point() class defined in utils/structures.py and that's why there it does not recognize the argument, or you have changed something in the Point() class that removed the index argument.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: FazaelAyatollahi on Nov. 1, 2024, 10:19 a.m.

Hi

Which backend do you use? (ASAP?)

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: vj2212 on Nov. 1, 2024, 12:41 p.m.

I am running into this error when running the last cell in the 3_inference.ipynb notebook from the docker container built using the base docker file with the entry point command commented out and an extra RUN /venv/bin/python3.8 -m pip install ipykernel jupyterto install supporting packages for the running of notebooks.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: FazaelAyatollahi on Nov. 1, 2024, 1:52 p.m.

The Docker created for running a .py file so you can run the inference.py (inference) but for notebook file (.ipynb) you should install the dependencies on your own docker as you said "ipykernel"

Would you please try something; just remove (comment) this part of code and check if you are able to create the json file as the output

    # saving xml file
    annotations_wsd = to_wsd(annotations)
    xml_filename = 'points_results.xml'
    output_path_xml = os.path.join(output_path,xml_filename)
    write_point_set(
        annotations_wsd,
        output_path_xml,
        label_color="blue",
    )

I am trying to figure out, which part has the error.

 Last edited by: FazaelAyatollahi on Nov. 1, 2024, 1:52 p.m., edited 1 time in total.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: gpio on Nov. 3, 2024, 12:15 a.m.

I use ASAP as the backend.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: vj2212 on Nov. 5, 2024, 12:34 p.m.

Yes commenting out the xml file creation lines in the inference notebook lets the final cell run without errors, the json files are created properly.

The issue for me seems to be the conversion thats happening in to_wsd which takes in the list of coordinates and converts it to a list of Point objects that seems to be necessary for creating the output xml files.

Is this a necessary output for the submission of an algorithm?

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: FilipPilarek on Nov. 11, 2024, 11:25 p.m.

I have encountered this error aswell.

As you stated you used Shapely 2.0.5, and at the top of the utils/structures.py file there is a warning:

warnings.filterwarnings( "ignore", "Setting custom attributes on geometry objects is deprecated, and will raise an AttributeError in Shapely 2.0", )

I have simply downgraded the shapely version to 1.8.1 by:

pip install --user shapely==1.8.1

and now it works :)

Predicted 4053 points saving predictions... [(10368.0 ... finished!

 Last edited by: FilipPilarek on Nov. 11, 2024, 11:27 p.m., edited 1 time in total.

Re: TypeError: __new__() got an unexpected keyword argument 'index' in Point Initialization  

  By: gpio on Nov. 13, 2024, 1:23 a.m.

Thank you so much FilipPilarek!

It did work when changing "shapely" from 2.0.5 to 1.8.1.