Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Add ontology and terms inside a software #29

@mizjaggy18

Description

@mizjaggy18

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.

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