def delete_user(self, id_user, admin=False): if admin: return Cytomine.get_instance().delete( "project/{}/user/{}/admin.json".format(self.id, id_user)) else: return Cytomine.get_instance().delete( "project/{}/user/{}.json".format(self.id, id_user))
def __init__(self, host, public_key, private_key, base_path, working_path, image_file, modes, directory, roi_term, positive_term, roi_zoom=None): # Public attributes self.start_time = time.time() self.n_images = None self.modes = modes self.project_id = None # Private attributes self.__term_id_positive = positive_term self.__term_id_roi = roi_term self.__host = host self.__public_key = public_key self.__private_key = private_key self.__conn = Cytomine(host, public_key, private_key, base_path=base_path, working_path=working_path, verbose=False) dir_name = image_file.split(".")[0] self.path = os.path.join(directory, str(dir_name)) if not os.path.exists(self.path): os.makedirs(self.path) self.project_name = "" self.project_id = -1 self.__filename_txt = "{}/log.txt".format(self.path) self.__txt = open(self.__filename_txt, 'w') if 1 in modes: self.__image_info_filename_csv = "{}/image_info.csv".format( self.path) self.__image_info_csv = open(self.__image_info_filename_csv, 'w') if 2 in modes: self.__area_filename_csv = "{}/area.csv".format(self.path) self.__area_csv = open(self.__area_filename_csv, 'w') if 3 in modes: self.__color_filename_csv = "{}/color.csv".format(self.path) self.__color_csv = open(self.__color_filename_csv, 'w') self.__roi_zoom = roi_zoom self.__images = [] self.image_job_dict = {} self.get_images_from_file(image_file)
def attach_swc_file_to_image_group(folder_path, id_project, host, public_key, private_key): file_list = [ f for f in listdir(folder_path) if isfile(join(folder_path, f)) ] with Cytomine(host=host, public_key=public_key, private_key=private_key, verbose=logging.INFO) as cyto_connection: print(cyto_connection.current_user) # ... Assume we are connected # Get the project from server. # The fetch() method makes the appropriate HTTP GET request. # See more on the github repository to see what is accessible: https://github.com/cytomine/Cytomine-python-client/tree/master/client/cytomine/models image_groups = ImageGroupCollection().fetch_with_filter( "project", id_project) for image_group in image_groups: # For this group, the images have all been saved into .ome.tif so we remove this file extension base_image_group_name = image_group.name[:-8] # Attach SWC file only to the RAW image, so we skip the images having a _lbl suffix. if base_image_group_name[-4:] != '_lbl': print(base_image_group_name) # We loop through the directory containing the SWC file for file_path in file_list: # If one SWC file basename match a .ome.tif file base name, we attach the SWC file to the image group if path.basename(file_path)[:-4] == base_image_group_name: print('Matching ' + path.join(folder_path, file_path)) AttachedFile(image_group, filename=path.join( folder_path, file_path)).upload()
def download_images(best_img): """ Download images Usage: download_images() Download all the images listed in the file CSV_IMGS in utils.paths """ print("-- Download images") with Cytomine(CYTO_HOST, CYTO_PUB_KEY, CYTO_PRV_KEY, verbose=logging.INFO) as cytomine: for id, name in best_img: print("\n- Image" + str(name)) # Paths # Directory path # Creates the directory print("Creating directory") os.makedirs(RAW_IMG_DIR, exist_ok=True) # Get instance of image with given id image_id = int(id) image_instances = ImageInstanceCollection().fetch_with_filter( "project", PROJECT_ID) image_instance = [ im for im in image_instances if im.id == image_id ] image_instance = image_instance[0] # Download image print("Downloading image") image_instance.download(RAW_IMG_DIR + name)
def get_core_connection(): if 'cytomine' not in g: g.cytomine = Cytomine.connect( current_app.config['CYTOMINE_HOST'], current_app.config['CYTOMINE_PUBLIC_KEY'], current_app.config['CYTOMINE_PRIVATE_KEY']) return g.cytomine
def env_parser(): parser = CTParser() parser.add_argument("--save_path", dest="save_path") parser.add_argument("--data_path", "--data_path", dest="data_path") parser.add_argument("--device", dest="device", default="cuda:0") parser.add_argument("--n_jobs", dest="n_jobs", default=1, type=int) _ = Cytomine._add_cytomine_cli_args(parser.parser) return parser
def download_tile_image(self): slide = self.base_image col_tile = self.abs_offset_x // 256 row_tile = self.abs_offset_y // 256 _slice = slide.image_instance response = Cytomine.get_instance().get('imaging_server.json', None) imageServerUrl = response['collection'][0]['url'] return Cytomine.get_instance().download_file( imageServerUrl + "/image/tile", self.cache_filepath, False, payload={ "zoomify": _slice.fullPath, "mimeType": _slice.mime, "x": col_tile, "y": row_tile, "z": slide.api_zoom_level })
def connect_as(user=None, open_admin_session=False): public_key = None private_key = None if hasattr(user, "publicKey") and user.publicKey: public_key = user.publicKey if hasattr(user, "privateKey") and user.privateKey: private_key = user.privateKey if not public_key or not private_key: keys = user.keys() public_key, private_key = keys["publicKey"], keys["privateKey"] Cytomine.get_instance().set_credentials(public_key, private_key) if open_admin_session: Cytomine.get_instance().open_admin_session() return Cytomine.get_instance().current_user
def run_import_with_cytomine(cytomine_auth, filepath, name, cytomine_listener, prefer_copy): with Cytomine(*cytomine_auth, configure_logging=False) as c: if not c.current_user: raise AuthenticationException("PIMS authentication to Cytomine failed.") run_import_( filepath, name, extra_listeners=[cytomine_listener], prefer_copy=prefer_copy )
def main(argv): # parsing arguments # print("In Parser") parser = ArgumentParser(prog="get_and_move.py", description="workflow with simple elastix") parser.add_argument('--cytomine_host', dest="cytomine_host", default='demo.cytomine.be') parser.add_argument('--cytomine_public_key', dest="cytomine_public_key") parser.add_argument('--cytomine_private_key', dest="cytomine_private_key") parser.add_argument('--cytomine_id_software', dest="cytomine_id_software") parser.add_argument("--cytomine_id_project", dest="cytomine_id_project") parser.add_argument('--fix_image_id', dest="id_fix_image") parser.add_argument("--mov_image_id,", dest="id_mov_image") parser.add_argument('--nb_iterations', dest="nb_iterations") parser.add_argument("--nb_spatialsampels", dest="nb_spatialsampels") parser.add_argument("--cytomine_storage_id", dest="storage_id") parser.add_argument("--annotation_fix_id", dest="annotation_fix_id") parser.add_argument("--annotation_moving_id", dest="annotation_moving_id") parser.add_argument("--cytomine_working_path", dest="working_path") parser.add_argument("--cytomine_upload", dest="cytomine_upload") parser.add_argument("--export_overlay_images", dest="export_overlay_images") parser.add_argument("--number_of_resolution", dest="number_of_resolution") parser.add_argument("--result_file_name", dest="result_file_name") arguments, others = parser.parse_known_args(argv) # connection to demo # print("Connect to demo") print("Argument host : " + arguments.cytomine_host) print("Pk = " + arguments.cytomine_public_key) print("Prk = " + arguments.cytomine_private_key) cytomine = Cytomine( arguments.cytomine_host, arguments.cytomine_public_key, arguments.cytomine_private_key, working_path=arguments. working_path # = software_router/algo/simple_elastix # ) # instance SimpleElastixJob object and run the logic of the algorithm # print("In with") print arguments.__dict__ with SimpleElastixJob( cytomine, arguments.cytomine_id_software, arguments.cytomine_id_project, arguments.__dict__, arguments.id_fix_image, arguments.id_mov_image, arguments.nb_spatialsampels, arguments.nb_iterations, arguments.storage_id, arguments.annotation_fix_id, arguments.annotation_moving_id, arguments.working_path, arguments.cytomine_host, arguments.cytomine_upload, arguments.cytomine_public_key, arguments.cytomine_private_key, arguments.export_overlay_images, arguments.number_of_resolution, arguments.result_file_name) as context: context.run()
def _parallel_download_wsi(identifiers, path, argv): with Cytomine.connect_from_cli(argv): instances = list() for _id in identifiers: instance = ImageInstance().fetch(_id) filepath = os.path.join(path, instance.originalFilename) print("Download", filepath) instance.download(dest_pattern=filepath, override=False) instance.download_path = filepath instances.append(instance) return instances
def run(self): from cytomine import Cytomine while not self.terminate_event.is_set(): try: url, box = self.queue.get_nowait() except queue.Empty: continue response = requests.get(url) if response.status_code in [ 200, 304 ] and response.headers['Content-Type'] == 'image/jpeg': try: tile = Image.open(BytesIO(response.content)) self.out_queue.put((tile, box)) Cytomine.get_instance().logger.info( "Reader fetched {}".format(url)) except IOError as e: Cytomine.get_instance().logger.error(e) print(e) else: Cytomine.get_instance().logger.error( "Bad request: {}".format(url)) self.queue.task_done()
def run(): """ Deletes all the annotations from an image, but those created by a software Example: python main.py --cytomine_host 'localhost-core' --cytomine_public_key 'b6ebb23c-00ff-427b-be24-87b2a82490df' --cytomine_private_key '6812f09b-3f33-4938-82ca-b23032d377fd' --cytomine_id_image_instance 347 --cytomine_id_user 61 --cytomine_id_project 154 python main.py --cytomine_host 'localhost-core' --cytomine_public_key 'b6ebb23c-00ff-427b-be24-87b2a82490df' --cytomine_private_key '6812f09b-3f33-4938-82ca-b23032d377fd' --cytomine_id_image_instance 3643 --cytomine_id_user 61 --cytomine_id_project 154 python main.py --cytomine_host 'localhost-core' --cytomine_public_key 'd2be8bd7-2b0b-40c3-9e81-5ad5765568f3' --cytomine_private_key '6dfe27d7-2ad1-4ca2-8ee9-6321ec3f1318' --cytomine_id_image_instance 2140 --cytomine_id_user 58 --cytomine_id_project 197 """ parser = ArgumentParser(prog="Cytomine Python client example") # Cytomine parser.add_argument('--cytomine_host', dest='host', default='demo.cytomine.be', help="The Cytomine host") parser.add_argument('--cytomine_public_key', dest='public_key', help="The Cytomine public key") parser.add_argument('--cytomine_private_key', dest='private_key', help="The Cytomine private key") parser.add_argument('--cytomine_id_image_instance', dest='id_image_instance', help="The image with annotations to delete") parser.add_argument('--cytomine_id_user', dest='id_user', help="The user with annotations to delete") parser.add_argument('--cytomine_id_project', dest='id_project', help="The project with annotations to delete") params, _ = parser.parse_known_args(sys.argv[1:]) with Cytomine(host=params.host, public_key=params.public_key, private_key=params.private_key, verbose=logging.INFO) as cytomine: # Get the list of annotations annotations = AnnotationCollection() annotations.image = params.id_image_instance # NOTE: use userjob id to retrieve annotations from the job. However, they # cannot be deleted. annotations.user = params.id_user annotations.project = params.id_project annotations.fetch() print(annotations) for annotation in annotations: annotation.delete()
def download_tile_image(self): slide = self.base_image iip_topology = TileTopology(slide, None, max_width=256, max_height=256, overlap=0) col_tile = self.abs_offset_x // 256 row_tile = self.abs_offset_y // 256 iip_tile_index = col_tile + row_tile * iip_topology.tile_horizontal_count _slice = slide.slice_instance return Cytomine.get_instance().download_file(_slice.imageServerUrl + "/slice/tile", self.cache_filepath, False, payload={ "fif": _slice.path, "mimeType": _slice.mime, "tileIndex": iip_tile_index, "z": slide.api_zoom_level })
def download_attach_files_from_image_group(id_project, host, public_key, private_key, folder_path): with Cytomine(host=host, public_key=public_key, private_key=private_key, verbose=logging.INFO) as cyto_connection: print(cyto_connection.current_user) # ... Assume we are connected image_groups = ImageGroupCollection().fetch_with_filter( "project", id_project) for image_group in image_groups: attached_collection = AttachedFileCollection(image_group).fetch() if len(attached_collection) > 0: print(image_group.name) for attached_file in attached_collection: print(attached_file.filename) attached_file.download( path.join(folder_path, attached_file.filename), True)
def main(argv): """ IMAGES VALID: * 005-TS_13C08351_2-2014-02-12 12.22.44.ndpi | id : 77150767 * 024-12C07162_2A-2012-08-14-17.21.05.jp2 | id : 77150761 * 019-CP_12C04234_2-2012-08-10-12.49.26.jp2 | id : 77150809 IMAGES TEST: * 004-PF_08C11886_1-2012-08-09-19.05.53.jp2 | id : 77150623 * 011-TS_13C10153_3-2014-02-13 15.22.21.ndpi | id : 77150611 * 018-PF_07C18435_1-2012-08-17-00.55.09.jp2 | id : 77150755 """ with Cytomine.connect_from_cli(argv): path = os.path.join(str(Path.home()), "data", "thyroid", "wsi") os.makedirs(path, exist_ok=True) to_fetch = ImageInstanceCollection().fetch_with_filter("project", 77150529) for instance in to_fetch: instance.download(dest_pattern=os.path.join(path, "{originalFilename}"), override=False)
def main(argv): parser = ArgumentParser(prog="Demo_SLDC_Workflow_With_Pyxit", description="Demo software for SLDC Workflow on Cytomine") parser.add_argument('--cytomine_host', dest="cytomine_host", default='demo.cytomine.be') parser.add_argument('--cytomine_public_key', dest="cytomine_public_key") parser.add_argument('--cytomine_private_key', dest="cytomine_private_key") parser.add_argument('--cytomine_base_path', dest="cytomine_base_path", default='/api/') default_working_path = os.path.join(tempfile.gettempdir(), "cytomine") parser.add_argument('--cytomine_working_path', dest="cytomine_working_path", default=default_working_path) parser.add_argument('--cytomine_id_software', dest="cytomine_id_software", type=int) parser.add_argument("--cytomine_id_project", dest="cytomine_id_project", type=int) parser.add_argument("--cytomine_id_image", dest="cytomine_id_image", type=int) parser.add_argument("--sldc_tile_overlap", dest="sldc_tile_overlap", type=int, default=10) parser.add_argument("--sldc_tile_width", dest="sldc_tile_width", type=int, default=768) parser.add_argument("--sldc_tile_height", dest="sldc_tile_height", type=int, default=768) parser.add_argument("--pyxit_model_path", dest="pyxit_model_path") parser.add_argument("--n_jobs", dest="n_jobs", type=int, default=1) parser.add_argument("--min_area", dest="min_area", type=int, default=500) parser.add_argument("--threshold", dest="threshold", type=int, default=215) parser.add_argument("--rseed", dest="rseed", type=int, default=0) default_workflow_wpath = os.path.join(tempfile.gettempdir(), "sldc") parser.add_argument("--working_path", dest="working_path", default=default_workflow_wpath) params, other = parser.parse_known_args(argv) # Initialize cytomine client cytomine = Cytomine( params.cytomine_host, params.cytomine_public_key, params.cytomine_private_key, working_path=params.cytomine_working_path, base_path=params.cytomine_base_path ) if not os.path.exists(params.working_path): os.makedirs(params.working_path) if not os.path.exists(params.cytomine_working_path): os.makedirs(params.cytomine_working_path) with DemoJob(cytomine, params.cytomine_id_software, params.cytomine_id_project, params.__dict__, params.sldc_tile_overlap, params.sldc_tile_width, params.sldc_tile_height, params.n_jobs, params.threshold, params.min_area, params.pyxit_model_path, params.rseed, params.working_path) as job: slide = CytomineSlide(cytomine, params.cytomine_id_image) job.run(slide)
def get_image_map(params): with Cytomine(host=params.host, public_key=params.public_key, private_key=params.private_key) as cytomine: image_instances = ImageInstanceCollection().fetch_with_filter( "project", params.id_project) res = dict() for image in image_instances: filename = image.filename if filename.find("/") != -1: filename = filename.split('/')[-1] res[image.id] = filename if params.download_path: # To download the original files that have been uploaded to Cytomine path = os.path.join(params.download_path, filename) print(path) if not os.path.exists(path): image.download(path) return res
def read_window(self): from cytomine import Cytomine window = copy.copy(self.window_position) window.width = window.width * pow(2, self.zoom) window.height = window.height * pow(2, self.zoom) window.x = window.x * pow(2, self.zoom) window.y = window.y * pow(2, self.zoom) url = "imageinstance/{}/window-{}-{}-{}-{}.png".format( self.whole_slide.image.id, window.x, window.y, window.width, window.height) response = Cytomine.get_instance()._get(url, {"zoom": self.zoom}) if response.status_code in [ 200, 304 ] and response.headers['Content-Type'] == 'image/jpeg': image = Image.open(BytesIO(response.content)) return transform_rgb_to_bgr(image) if self.rgb2bgr else image else: return False
def get_images_with_annotations(host, public_key, private_key, project_id=None, download=True, annotation_ids=None): """ Find and download (if not present) annotation information and images :param annotation_ids: List of annotations to fetch :param download: Whether or not to download missing files. Otherwise just fetches information. :param private_key: Private key as string :param public_key: Public key as string :param host: Hostname :param project_id: Restrict :return: List of dictionaries with 'image' and 'annotations' """ output = [] with Cytomine(host=host, public_key=public_key, private_key=private_key) as cytomine: annotations = AnnotationCollection() if project_id is not None: annotations.project = project_id annotations.fetch() print(f'{annotations}') image_regions = dd(list) for annotation in annotations: print(annotation) if annotation_ids is not None and annotation.id not in annotation_ids: continue print(f'Found annotation {annotation.id}') annotation: Annotation path = Path('/tmp') / 'cytomine' / 'p{project}' / 'i{image}' / 'masked{id}.png' formatted = str(path).format(id=annotation.id, image=annotation.image, project=annotation.project) print(f'Checking whether or not to download to {formatted}') if download and not Path(formatted).is_file(): print(f'Dumping annotation to {formatted}') annotation.dump(str(path), override=True, mask=True, alpha=True) assert Path(formatted).is_file(), "Annotation image not found after download!" image_regions[annotation.image]\ .append(formatted) print(image_regions) return image_regions
def __init__(self, working_path, id_project, without_image_download=False, without_image_groups=False, without_user_annotations=False, without_metadata=False, without_annotation_metadata=False, anonymize=False): self.project = Project().fetch(id_project) if not self.project: raise ValueError("Project not found") items = [Cytomine.get_instance().host, self.project.id, self.project.name, datetime.now()] self.project_directory = "{}-{}-{}-{}".format(*[str(item).replace(" ", "-") for item in items]) self.working_path = working_path self.project_path = os.path.join(working_path, self.project_directory) self.attached_file_path = None self.with_image_download = not without_image_download self.with_image_groups = not without_image_groups self.with_user_annotations = not without_user_annotations self.with_annotation_metadata = not without_annotation_metadata self.with_metadata = not without_metadata self.anonymize = anonymize self.users = UserCollection()
def get_terms_list(params): with Cytomine(host=params.cytomine_host, public_key=params.cytomine_public_key, private_key=params.cytomine_private_key) as cytomine: res = {} annotations = AnnotationCollection() annotations.project = params.cytomine_id_project annotations.showWKT = True annotations.showMeta = True annotations.showGIS = True annotations.showTerm = True annotations.showImage = True annotations.fetch() print(annotations) for annotation in annotations: print("ID: {} | Img: {} | Pjct: {} | Term: {} ".format( annotation.id, annotation.image, annotation.project, annotation.term)) if len(annotation.term) == 1: if (annotation.term[0], annotation.image) not in res.keys(): res[(annotation.term[0], annotation.image)] = [] res[(annotation.term[0], annotation.image)].append(loads(annotation.location)) last = res[(annotation.term[0], annotation.image)][-1] print(last.bounds, last.to_wkt().count(",")) ks = res.keys() print(ks) kl = [x for x in ks] stuff = np.array(kl)[:, 0] stuff = stuff.tolist() stuff = set(stuff) print(stuff) terms = [x for x in stuff] terms.remove(params.slice_term) print(terms) return terms
def main(argv): # parsing arguments parser = ArgumentParser(prog="TestParser", description="Catch arguments form exec cmd") parser.add_argument('--cytomine_host', dest="cytomine_host", default='demo.cytomine.be') parser.add_argument('--cytomine_public_key', dest="cytomine_public_key") parser.add_argument('--cytomine_private_key', dest="cytomine_private_key") parser.add_argument('--cytomine_id_software', dest="cytomine_id_software", type=int) parser.add_argument("--cytomine_id_project", dest="cytomine_id_project", type=int) parser.add_argument('--id_fix_image', dest="id_fix_image", type=int) parser.add_argument("--id_mov_image", dest="id_mov_image", type=int) parser.add_argument('--nb_iterations', dest="nb_iterations", type=int) parser.add_argument("--nb_spatialsampels", dest="nb_spatialsampels", type=int) arguments, others = parser.parse_known_args(argv) # set var fixImageId = arguments.id_fix_image movImageId = arguments.id_mov_image projectId = arguments.cytomine_id_project # connection conn = Cytomine(urlCore, Pk, Prk, working_path=workingPath) # dump images in folder working path + images, for reach iamges : working path + images + id project -> u are in the right folder annotationFix = conn.get_annotations(projectId, id_image=fixImageId) fixData = annotationFix.data() excludedTerms = [] IdTerm = 0 for a in fixData: i = 0 term = conn.get_term(int(a.term[i])) if term.name != 'ROI': excludedTerms[i] = a.term[i] else: IdTerm = a.term[i] i += 1 conn.dump_annotations(annotations=annotationFix, get_image_url_func=Annotation.get_annotation_crop_url, dest_path=workingPath + "/imagefix/", excluded_terms=excludedTerms, desired_zoom=0 ) annotationMov = conn.get_annotations(projectId, id_image=movImageId) movData = annotationMov.data() excludedTerms = [] for b in movData: i = 0 term = conn.get_term(int(a.term[i])) if term.name != 'ROI': excludedTerms[i] = a.term[i] else: IdTerm = a.term[i] i += 1 dump_annotations = conn.dump_annotations(annotations=annotationMov, get_image_url_func=Annotation.get_annotation_crop_url, dest_path=workingPath + "/imagemov/", desired_zoom=excludedTerms, excluded_terms=excludedTerms ) listFix= os.listdir(workingPath + "imagefix" + "/" + str(IdTerm) + "/") listMov = os.listdir(workingPath + "imagemov" + "/" + str(IdTerm) + "/") fixImage = sitk.ReadImage(workingPath + "imagefix" + "/" + str(IdTerm) + "/" + listFix[0],sitk.sitkFloat32) movImage = sitk.ReadImage(workingPath + "imagemov" + "/" + str(IdTerm) + "/" + listMov[0],sitk.sitkFloat32) simpleElastix = sitk.SimpleElastix() simpleElastix.SetFixedImage(fixImage) simpleElastix.SetMovingImage(movImage) parameterMapTranslation = sitk.GetDefaultParameterMap("translation") parameterMapAffine = sitk.GetDefaultParameterMap("affine") simpleElastix.SetParameterMap(parameterMapTranslation) simpleElastix.AddParameterMap(parameterMapAffine) simpleElastix.SetParameter("MaximumNumberOfIterations", str(arguments.nb_iterations)) simpleElastix.SetParameter("NumberOfSpatialSamples",str(arguments.nb_spatialsampels)) simpleElastix.Execute() img = simpleElastix.GetResultImage() np_img = sitk.GetArrayFromImage(img) img_to_save = np.zeros((np_img.shape[0], np_img.shape[1], 3)) img_to_save[:, :, 1] = np_img img_to_save[:, :, 2] = sitk.GetArrayFromImage(fixImage) misc.imsave("/home/tvessiere/data/Project/TestProcessing/images/result_translationaffine.png", img_to_save) misc.imsave("/home/tvessiere/data/Project/TestProcessing/images/movimage.png",np_img) storageId = 19676833 demoUpload = Cytomine(urlUploadDemo, Pk, Prk, verbose=True) sync = False storage = conn.get_storage(storageId) assert storage.id == storageId demoUpload.upload_image("/home/tvessiere/data/Project/TestProcessing/images/result_translationaffine.png",projectId, storageId, "http://demo.cytomine.be") os.system("rm -rf " + workingPath + "imagefix" + "/" + str(IdTerm) + "/") os.system("rm -rf " + workingPath + "imagemov" + "/" + str(IdTerm) + "/")
# Cytomine parser.add_argument('--cytomine_host', dest='host', default='demo.cytomine.be', help="The Cytomine host") parser.add_argument('--cytomine_public_key', dest='public_key', help="The Cytomine public key") parser.add_argument('--cytomine_private_key', dest='private_key', help="The Cytomine private key") params, other = parser.parse_known_args(sys.argv[1:]) with Cytomine(host=params.host, public_key=params.public_key, private_key=params.private_key, verbose=logging.INFO) as cytomine: """ We will create a new ontology with the following structure: _MY_ONTOLOGY_NAME_ == TERM1 == CATEGORY1 ==== TERM2 ==== TERM3 """ # First we create the required resources ontology = Ontology("_MY_ONTOLOGY_NAME_").save() term1 = Term("TERM1", ontology.id, "#00FF00").save() cat1 = Term("CATEGORY1", ontology.id, "#000000").save() term2 = Term("TERM2", ontology.id, "#FF0000").save()
__copyright__ = "Copyright 2010-2015 University of Liège, Belgium, http://www.cytomine.be/" from cytomine import Cytomine from cytomine.models import * from shapely.geometry.polygon import Polygon from shapely.geometry import Point, box #Cytomine connection parameters cytomine_host = "XXX" cytomine_public_key = "XXX" cytomine_private_key = "XXX" #Connection to Cytomine Core conn = Cytomine(cytomine_host, cytomine_public_key, cytomine_private_key, base_path='/api/', working_path='/tmp/', verbose=True) #Replace XXX by your values id_image = XXX id_term = XXX #Create one geometrical object locally (using shapely) #circle = Point(100,100).buffer(1000) #annotation.location=circle.wkt #rectangle = box(100, 100, 200, 200, ccw=True) #annotation.location=rectangle.wkt point = Point( 1000, 1000) #point at position (1000,1000) where (0,0) is bottom left corner
__contributors__ = [ "Marée Raphaël <*****@*****.**>", "Rollus Loïc <*****@*****.**" ] __copyright__ = "Copyright 2010-2015 University of Liège, Belgium, http://www.cytomine.be/" from cytomine import Cytomine #Replace connection XXX parameters cytomine_host = "XXX" cytomine_public_key = "XXX" cytomine_private_key = "XXX" #Connection to Cytomine Core conn = Cytomine(cytomine_host, cytomine_public_key, cytomine_private_key, base_path='/api/', working_path='/tmp/', verbose=True) #Replace XXX with Identifiers of image, project, annotation annotation_id = XXX image_id = XXX project_id = XXX #Methods to add properties to an existing annotation #add annotation_property = conn.add_annotation_property(annotation_id, "key_prop", "value_prop") assert annotation_property != None #get one property annotation_property2 = conn.get_annotation_property(annotation_id,
def run(self): print "Start Running..." path_job = os.path.join(self._working_path, str(self.job.id)) print "path job = " + path_job os.mkdir(path_job) if self._id_annotation_fix == "" and self._id_annotation_fix == "": # dump images # # fix # fix_image_instance = self._cytomine.get_image_instance( long(self._fix_image_id)) fix_collection = models.ImageInstanceCollection() fix_collection.data().append(fix_image_instance) self._cytomine.dump_project_images( image_instances=fix_collection, id_project=long(self._project_id), dest_path=os.path.join(str(self.job.id), "images/"), override=False, max_size=5000) # moving # moving_image_instance = self._cytomine.get_image_instance( int(self._moving_image_id)) moving_collection = models.ImageInstanceCollection() moving_collection.data().append(moving_image_instance) self._cytomine.dump_project_images( image_instances=moving_collection, id_project=long(self._project_id), dest_path=os.path.join(str(self.job.id), "images/"), override=False, max_size=5000) # format paths # path_to_fix_image = os.path.join(self._working_path, str(self.job.id), "images", str(self._project_id), str(self._fix_image_id) + ".jpg") path_to_moving_image = os.path.join( self._working_path, str(self.job.id), "images", str(self._project_id), str(self._moving_image_id) + ".jpg") # debug # print "path_to_fix_image : " + str(path_to_fix_image) print "path_to_moving_image : " + str(path_to_moving_image) else: # dump annotations# annotation_fix = self._cytomine.get_annotation( long(self._id_annotation_fix)) collection_fix = models.AnnotationCollection() collection_fix.data().append(annotation_fix) #os.makedirs(os.path.join(self._working_path,str(self.job.id),"images", "annotation_fix/")) print len(collection_fix) self._cytomine.dump_annotations \ ( annotations=collection_fix, get_image_url_func=models.Annotation.get_annotation_crop_url, dest_path=os.path.join(self._working_path,str(self.job.id),"images", "annotation_fix/"), desired_zoom=0 ) print "dest path dump " + os.path.join(str(self.job.id), "images", "annotation_fix") annotation_moving = self._cytomine.get_annotation( long(self._id_annotation_moving)) collection_moving = models.AnnotationCollection() collection_moving.data().append(annotation_moving) #os.makedirs(os.path.join(self._working_path,str(self.job.id), "images", "annotation_moving/")) print len(collection_moving) self._cytomine.dump_annotations \ ( annotations=collection_moving, get_image_url_func=models.Annotation.get_annotation_crop_url, dest_path=os.path.join(self._working_path,str(self.job.id),"images", "annotation_moving/"), desired_zoom=0 ) print "dest path dump " + os.path.join(str(self.job.id), "images", "annotation_moving") # get id_term for path # id_term = annotation_fix.term[0] # because the name of the file is vague, just list the file and get the elem at 0 # list_fix = os.listdir( os.path.join(self._working_path, str(self.job.id), "images", "annotation_fix", str(id_term))) list_moving = os.listdir( os.path.join(self._working_path, str(self.job.id), "images", "annotation_moving", str(id_term))) # format paths # path_to_fix_image = os.path.join(self._working_path, str(self.job.id), "images", "annotation_fix", str(id_term), str(list_fix[0])) path_to_moving_image = os.path.join(self._working_path, str(self.job.id), "images", "annotation_moving", str(id_term), str(list_moving[0])) # debug # print "path_to_fix_image : " + str(path_to_fix_image) print "path_to_moving_image : " + str(path_to_moving_image) # load images # fix_image_grey = sitk.ReadImage(path_to_fix_image, sitk.sitkFloat32) moving_image_grey = sitk.ReadImage(path_to_moving_image, sitk.sitkFloat32) # open img color with CV # cv_moving_image = cv2.imread(path_to_moving_image) cv_fix_image = cv2.imread(path_to_fix_image) # start processing algorithm # # got all the channel for keep orignal color # itk_mov_image_color_0 = sitk.GetImageFromArray(cv_moving_image[:, :, 0]) itk_mov_image_color_1 = sitk.GetImageFromArray(cv_moving_image[:, :, 1]) itk_mov_image_color_2 = sitk.GetImageFromArray(cv_moving_image[:, :, 2]) # set ParamtersMap to sitk for compute transformation # simple_elastix = sitk.SimpleElastix() simple_elastix.SetFixedImage(fix_image_grey) simple_elastix.SetMovingImage(moving_image_grey) parameter_map_translation = sitk.GetDefaultParameterMap("translation") parameter_map_affine = sitk.GetDefaultParameterMap("affine") # translation & affine # simple_elastix.SetParameterMap(parameter_map_translation) simple_elastix.AddParameterMap(parameter_map_affine) # params set by user # simple_elastix.SetParameter("MaximumNumberOfIterations", str(self._nb_iterations)) simple_elastix.SetParameter("NumberOfSpatialSamples", str(self._nb_spatial_sample)) simple_elastix.SetParameter("NumberOfResolutions", str(self._number_of_resolutions)) # start computing # simple_elastix.Execute() # get parameters of the transform for apply it on 3 channels # transform_map = simple_elastix.GetTransformParameterMap() # for set shape of images # np_img = sitk.GetArrayFromImage(simple_elastix.GetResultImage()) # set parameterMap & complete properties_map # properties_map = {} transform_x = sitk.SimpleTransformix() transform_x.SetTransformParameterMap(transform_map) # translation map # # IMPORTANT FOR UNDERSTAND # # those dicts are specific here, i added a tuple with a boolean for know if i need to get one or all element # in the tuple returned by sitk, after that i give to the function "MakeUpProperties" the dict, mode(number # of parameter map (e.g : 0 || 1 etc)) the properties map to link at the image and the transform_x object. # Basically if the boolean is True, got all the parameters of the tuple # and format it for got a single Key/value pair like direction_0 = x, direction_1 = y. If the boolean is # False, only get the first element # dict_translation = \ { "DefaultPixelValue": ("sitk_translation_default_pixel_value", False), # int # "Direction": ("sitk_translation_direction", True), # tuple with 4 ints # "FinalBSplineInterpolationOrder": ("sitk_translation_final_bspline_interpolation_order", False), # int # "FixedImageDimension": ("sitk_translation_fixed_image_dimension", False), # int # "FixedInternalImagePixelType": ("sitk_translation_fixed_internal_image_pixel_type", False), # string # "HowToCombineTransforms": ("sitk_translation_how_combine_transforms", False), # string # "Index": ("sitk_translation_index", True), # tuple with 2 int # "InitialTransformParametersFileName": ( "sitk_translation_initial_transform_parameters_file_name", False), "MovingImageDimension": ("sitk_translation_moving_image_dimension", False), "MovingInternalImagePixelType": ("sitk_translation_moving_internal_image_pixel_type", False), "NumberOfParameters": ("sitk_translation_number_of_parameters", False), "Origin": ("sitk_translation_origin", True), "ResampleInterpolator": ("sitk_translation_resample_interpolator", False), "Resampler": ("sitk_translation_resampler", False), "Spacing": ("sitk_translation_spacing", True), "Transform": ("sitk_translation_transform", False), "TransformParameters": ("sitk_translation_transform_parameters", True), "UseDirectionCosines": ("sitk_translation_use_directions_Cosines", False), } MakeUpProperties(dict_translation, 0, properties_map, transform_x) dict_affine = \ { "CenterOfRotationPoint": ("sitk_affine_centrer_of_rotation", True), "DefaultPixelValue": ("sitk_affine_default_pixel_value", False), "Direction": ("sitk_affine_direction", True), "FinalBSplineInterpolationOrder": ("sitk_affine_final_bspline_interpolation_order", False), "FixedImageDimension": ("sitk_affine_fixed_image_dimension", False), "FixedInternalImagePixelType": ("sitk_affine_fixed_internal_image_pixel_type", False), "HowToCombineTransforms": ("sitk_affine_how_combine_transforms", False), "Index": ("sitk_affine_index", True), "InitialTransformParametersFileName": ("sitk_affine_initial_transform_parameters_file_name", False), "MovingImageDimension": ("sitk_affine_moving_image_dimension", False), "MovingInternalImagePixelType": ("sitk_affine_moving_internal_image_pixel_type", False), "NumberOfParameters": ("sitk_affine_number_of_parameters", False), "Origin": ("sitk_affine_origin", True), "ResampleInterpolator": ("sitk_affine_resample_interpolator", False), "Resampler": ("sitk_affine_resampler", False), "Spacing": ("sitk_affine_spacing", True), "Transform": ("sitk_affine_transform", False), "TransformParameters": ("sitk_affine_transform_parameters", True), "UseDirectionCosines": ("sitk_affine_use_directions_Cosines", False), } MakeUpProperties(dict_affine, 1, properties_map, transform_x) # apply transforms on all channels # transform_x.SetMovingImage(itk_mov_image_color_0) img_to_save_0 = transform_x.Execute() transform_x.SetMovingImage(itk_mov_image_color_1) img_to_save_1 = transform_x.Execute() transform_x.SetMovingImage(itk_mov_image_color_2) img_to_save_2 = transform_x.Execute() # format image color # img_color_final = np.zeros((np_img.shape[0], np_img.shape[1], 3)) img_color_final[:, :, 0] = sitk.GetArrayFromImage(img_to_save_0) img_color_final[:, :, 1] = sitk.GetArrayFromImage(img_to_save_1) img_color_final[:, :, 2] = sitk.GetArrayFromImage(img_to_save_2) # save images # img_transform_to_save_path = os.path.join(self._working_path, str(self.job.id), "images", str(self._result_file_name)) if (self._overlayed_images == "true"): img_overlay_to_save_path = os.path.join(self._working_path, str(self.job.id), "images", "overlayed_images.png") cv2.imwrite(img_transform_to_save_path, img_color_final) cv2.imwrite(img_overlay_to_save_path, img_color_final + (0.80 * cv_fix_image)) # connection to demo-upload & upload # demo_upload = Cytomine(self._cytomine_upload, self._pk, self._prk, verbose=True) demo_upload.upload_image(img_transform_to_save_path, self._project_id, self._storage_id, str(self._cytomine_host), properties=properties_map) demo_upload.upload_image(img_overlay_to_save_path, self._project_id, self._storage_id, str(self._cytomine_host), properties=None) else: cv2.imwrite(img_transform_to_save_path, img_color_final) # connection to demo-upload & upload # demo_upload = Cytomine(self._cytomine_upload, self._pk, self._prk, verbose=True) demo_upload.upload_image(img_transform_to_save_path, self._project_id, self._storage_id, str(self._cytomine_host), properties=properties_map) # remove the directory of the current job # shutil.rmtree(os.path.join(self._working_path, str(self.job.id)), ignore_errors=True) self.done(True)
def main(argv): parser = ArgumentParser() parser.add_argument(*_cytomine_parameter_name_synonyms("project_id"), dest="project_id", type=int, help="The Cytomine project id.", required=True) parser.add_argument( "-i", "--ignore-existing", action="store_true", dest="ignore_existing", help= "Ignore existing ground truth annotation associated with the project. If not specified," " current annotations will be deleted before uploading the new ones.") parser.set_defaults(ignore_existing=False) options, _ = parser.parse_known_args(argv) with Cytomine.connect_from_cli(argv) as cytomine: project = Project().fetch(options.project_id) print("Project '{}' (#{}): discipline '{}'".format( project.name, project.id, project.disciplineShortName)) if not options.ignore_existing: annotations = AnnotationCollection() annotations.project = project.id annotations.user = cytomine.current_user.id annotations.fetch() delete_collection(annotations, "annotation") tracks = TrackCollection() tracks.project = project.id tracks.user = cytomine.current_user.id tracks.fetch_with_filter("project", project.id) tracks._data = [ t for t in tracks.data() if t.name.startswith("gt-") ] delete_collection(tracks, "track") fake_job = FakeJob(project) home = Path.home() in_path = os.path.join(home, "data", "in") gt_path = os.path.join(home, "data", "gt") os.makedirs(in_path) os.makedirs(gt_path) in_images, gt_images = download_images(fake_job, in_path, gt_path, gt_suffix="_lbl") if project.disciplineShortName == "TreTrc": # ground truth is contained in swc files so need to # convert them into masks beforehand print("TreTrc problem: start converting SWC to masks") download_attached(in_images, gt_path, do_download=True) alternate_gt_path = os.path.join(home, "data", "altgt") os.makedirs(alternate_gt_path) for in_image in in_images: swc_filepath = in_image.attached[0].filepath im_size = imageio.volread( in_image.filepath).shape # Size is Depth,Height,Width im_size = im_size[:: -1] # Invert the size order to Width,Height,Depth swc_to_tiff_stack(input_path=swc_filepath, output_path=os.path.join( alternate_gt_path, in_image.filename), im_size=im_size) gt_path = alternate_gt_path is_2d = guess_dims(gt_path) print("Image detected as {}".format("2d" if is_2d else ">2d")) upload_data(problemclass=project.disciplineShortName, nj=fake_job, inputs=in_images, out_path=gt_path, is_2d=is_2d, projection=-1)
def delete_user(self, id_user, admin=False): if admin: return Cytomine.get_instance().delete("project/{}/user/{}/admin.json".format(self.id, id_user)) else: return Cytomine.get_instance().delete("project/{}/user/{}.json".format(self.id, id_user))
import shapely.wkt from cytomine import Cytomine from cytomine.models import AnnotationCollection from patches import Patch from utils.path import * host = "http://cytomine.icube.unistra.fr" public_key = "8da00e26-3bcb-4229-b31d-a2b5937c4e5e" # check your own keys from your account page in the web interface private_key = "c0018f6a-8aa1-4791-957b-ab72dce4238d" term = {''} if __name__ == '__main__': with Cytomine(host=CYTO_HOST, public_key=CYTO_PUB_KEY, private_key=CYTO_PRV_KEY, verbose=logging.INFO) as cytomine: annotations = AnnotationCollection() annotations.project = "1345" annotations.showWKT = True annotations.showMeta = True annotations.showTerm = True annotations.showGIS = True annotations.fetch() print(annotations) f = open("./anno.csv", "w+") f.write("ID;Image;Project;Term;User;Area;Perimeter;WKT;TRACK \n") for annotation in annotations: f.write("{};{};{};{};{};{};{};{}\n".format( annotation.id, annotation.image, annotation.project,
__author__ = "Marée Raphaël <*****@*****.**>" __copyright__ = "Copyright 2010-2015 University of Liège, Belgium" from cytomine import Cytomine import sys, time import os from array import * #Connection parameters to Cytomine cytomine_host = "XXX" cytomine_public_key = "XXX" cytomine_private_key = "XXX" #Connection to Cytomine Core conn = Cytomine(cytomine_host, cytomine_public_key, cytomine_private_key, base_path='/api/', working_path='/tmp/', verbose=True) #Replace XXX values by the project identifier, term id of the ROI where you want to count object (of type id_object_term) created by user id_user id_project = XXX id_roi_term = XXX id_object_term = XXX id_user = XXX #Replace XXX by identifiers of images where you want to compute statistics images = array('i', [XXX, XXX]) for id_image in images: #Retrieve image instance image_instance = conn.get_image_instance(id_image)