Cytomine-python-client icon indicating copy to clipboard operation
Cytomine-python-client copied to clipboard

Add ontology and terms inside a software

Open mizjaggy18 opened this issue 3 years ago • 1 comments

Hi @geektortoise,

Is it possible to add new ontology from a software?

Here part of my codes:

    import sys
    import numpy as np
    import os

    from shapely.geometry import shape, box, Polygon,Point
    from shapely import wkt
    from glob import glob
    from tifffile import imread
    from cytomine import Cytomine, models, CytomineJob
    from cytomine.models import Annotation, AnnotationTerm, AnnotationCollection, ImageInstanceCollection, Job
    from cytomine.models.ontology import Ontology, OntologyCollection, Term, RelationTerm, TermCollection

    from PIL import Image
    import argparse
    import json
    import logging

    def main(argv):
      with CytomineJob.from_cli(argv) as conn:
        print(conn.parameters)
        
        conn.job.update(status=Job.RUNNING, progress=0, statusComment="Initialization...")
        base_path = "{}".format(os.getenv("HOME")) # Mandatory for Singularity
        working_path = os.path.join(base_path,str(conn.job.id))

        ontology = Ontology("classPNcells"+str(conn.parameters.cytomine_id_project)).save()

        #Algorithm goes here
        #.....
        #.....

                if cellclass==1:
                    print("Positive (H: ", str(hue), ", V: ", str(val))
                    id_terms=Term("PositiveCell", ontology.id, "#FF0000").save()
                elif cellclass==2:
                    print("Negative (H: ", str(hue), ", V: ", str(val))
                    id_terms=Term("NegativeCell", ontology.id, "#00FF00").save()

                cytomine_annotations = AnnotationCollection()
                cytomine_annotations.append(Annotation(location=roi_geometry,
                                                       id_image=id_image,#conn.parameters.cytomine_id_image,
                                                       id_project=conn.parameters.cytomine_id_project,
                                                       id_terms=[id_terms]))
                print(".",end = '',flush=True)

                #Send Annotation Collection (for this ROI) to Cytomine server in one http request
                ca = cytomine_annotations.save()

                conn.job.update(status=Job.TERMINATED, progress=100, statusComment="Finished.")

    if __name__ == "__main__":
      main(sys.argv[1:])

The input is: python3 classifypncell.py --cytomine_host "http://localhost-core" --cytomine_public_key "xxxxx" --cytomine_private_key "xxxxxx" --cytomine_id_project "44890" --cytomine_id_software "48284" --cytomine_id_images "44902" --cytomine_id_roi_term "1486"

And the log:

[2021-05-19 21:43:48,738][INFO] [GET] [currentuser] CURRENT USER - 61 : admin | 200 OK [2021-05-19 21:43:48,906][INFO] [GET] [project] 44890 : Test_classifyPNcells | 200 OK [2021-05-19 21:43:48,961][INFO] [GET] [software] 48284 : classify_PN_cells_test | 200 OK [2021-05-19 21:43:49,082][INFO] [GET] [software_parameter collection] 7 objects | 200 OK [2021-05-19 21:43:49,449][INFO] [POST] [job] 50694 : classify_PN_cells_test : 33 | 200 OK [2021-05-19 21:43:49,492][INFO] [GET] [user] 50700 : JOB[admin ], 2021-05-19 13:43:49:381 | 200 OK [2021-05-19 21:43:49,535][INFO] [GET] [currentuser] CURRENT USER - 50700 : JOB[admin ], 2021-05-19 13:43:49:381 | 200 OK [2021-05-19 21:43:49,536][INFO] Job (id:50694) status update: "None" (status: RUNNING, progress: 0%) [2021-05-19 21:43:49,634][INFO] [PUT] [job] 50694 : classify_PN_cells_test : 33 | 200 OK [2021-05-19 21:43:49,743][INFO] [POST] [jobparameter] 50707 : cytomine_id_images | 200 OK [2021-05-19 21:43:49,813][INFO] [POST] [jobparameter] 50713 : cytomine_id_roi_term | 200 OK [2021-05-19 21:43:49,814][INFO] Job (id:50694) status update: "Initialization..." (status: RUNNING, progress: 0%) [2021-05-19 21:43:49,898][INFO] [PUT] [job] 50694 : classify_PN_cells_test : 33 | 200 OK [2021-05-19 21:43:50,471][INFO] [POST] [ontology] None : classPNcells44890 | 500 Internal Server Error [2021-05-19 21:43:50,654][INFO] [GET] [ontology collection] 0 objects | 200 OK [ontology collection] 0 objects [2021-05-19 21:43:50,748][INFO] [GET] [term collection] 3 objects | 200 OK [term collection] 3 objects [2021-05-19 21:43:51,088][INFO] [GET] [imageinstance collection] 1 objects | 200 OK [44902] [2021-05-19 21:43:51,088][INFO] Job (id:50694) status update: "Running PN classification on image (1/1)." (status: RUNNING, progress: 0%) [2021-05-19 21:43:51,159][INFO] [PUT] [job] 50694 : classify_PN_cells_test : 33 | 200 OK [2021-05-19 21:43:51,370][INFO] [GET] [annotation collection] 1 objects | 200 OK [annotation collection] 1 objects ----------------------------Cells------------------------------ ROI Geometry from Shapely: POLYGON ((5 505, 621 505, 621 0, 5 0, 5 505)) ROI Bounds (5.0, 0.0, 621.0, 505.0) roi_png_filename: /home/wshmunirah/50694/44890/44902/44918/44918.png [2021-05-19 21:43:54,455][INFO] File downloaded successfully from http://localhost-core/api/userannotation/44918/alphamask.png with parameters {'bits': 8, 'complete': True} hue: 199.8789470500792 val: 200.5944437181143 Positive (H: 199.8789470500792 , V: 200.5944437181143 [2021-05-19 21:43:54,800][INFO] Job (id:50694) status update: "'bool' object has no attribute 'id'" (status: FAILED, progress: 0%) [2021-05-19 21:43:54,900][INFO] [PUT] [job] 50694 : classify_PN_cells_test : 33 | 200 OK Traceback (most recent call last): File "classifypncell.py", line 226, in main(sys.argv[1:]) File "classifypncell.py", line 204, in main id_terms=Term("PositiveCell", ontology.id, "#FF0000").save()

It seems like the error is caused by 500 Internal Server Error when adding the new Ontology, hence ontology.id is not defined. I tried using the example add_ontology.py and it worked like charm. Is there anything I missed here? I have updated to the latest Cytomine release 3.0.3.

Thank you.

mizjaggy18 avatar May 19 '21 14:05 mizjaggy18

Hi @geektortoise and Cytomine developers,

Using the same code above, but now trying to add properties to the annotation using the lines below:

prop1 = Property(Annotation().fetch(id_image), key="Hue", value=str(hue)).save() prop2 = Property(Annotation().fetch(id_image), key="Val", value=str(val)).save()

Giving this error:

[2021-05-25 01:01:54,521][INFO] Job (id:81602) status update: "'bool' object has no attribute 'is_new'" (status: FAILED, progress: 0%) [2021-05-25 01:01:54,652][INFO] [PUT] [job] 81602 : classify_PN_cells_test : 92 | 200 OK Traceback (most recent call last): File "classifypncell.py", line 283, in main(sys.argv[1:]) File "classifypncell.py", line 250, in main prop1 = Property(Annotation().fetch(id_image), key="Hue", value=str(hue)).save() File "/usr/local/lib/python3.6/dist-packages/Cytomine_Python_Client-2.2.1-py3.6.egg/cytomine/models/property.py", line 36, in init File "/usr/local/lib/python3.6/dist-packages/Cytomine_Python_Client-2.2.1-py3.6.egg/cytomine/models/model.py", line 118, in init AttributeError: 'bool' object has no attribute 'is_new'

Appreciate your kind assistance. Thanks.

mizjaggy18 avatar May 24 '21 17:05 mizjaggy18