| 1 |  |  | #!/usr/bin/env python3 | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | # -*- coding: utf-8 -*- | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | """ | 
            
                                                        
            
                                    
            
            
                | 4 |  |  | Created on Mon May  7 15:49:04 2018 | 
            
                                                        
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | @author: Paolo Cozzi <[email protected]> | 
            
                                                        
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 8 |  |  | Functions adapted from Jun Fan misc.py and use_zooma.py python scripts | 
            
                                                        
            
                                    
            
            
                | 9 |  |  | """ | 
            
                                                        
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 11 |  |  | import logging | 
            
                                                        
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 13 |  |  | from image_validation.use_ontology import use_zooma | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 15 |  |  | from common.constants import CONFIDENCES | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 18 |  |  | # Get an instance of a logger | 
            
                                                        
            
                                    
            
            
                | 19 |  |  | logger = logging.getLogger(__name__) | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 22 |  |  | def annotate_generic(model, zooma_type): | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |     """Annotate a generic DictTable | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |     Args: | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |         model (:py:class:`image_app.models.DictBase`): A DictBase istance | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         zooma_type (str): the type of zooma annotation (country, species, ...) | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     """ | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |     logger.debug("Processing %s" % (model)) | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |     result = use_zooma(model.label, zooma_type) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |     # update object (if possible) | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |     if result: | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |         url = result['ontologyTerms'] | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         # https://stackoverflow.com/a/7253830 | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         term = url.rsplit('/', 1)[-1] | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |         # The ontology seems correct. Annotate! | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         logger.info("Updating %s with %s" % (model, result)) | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         url = result['ontologyTerms'] | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         model.term = term | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         # get an int object for such confidence | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         confidence = CONFIDENCES.get_value( | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             result["confidence"].lower()) | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         model.confidence = confidence | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         model.save() | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 |  |  | def annotate_country(country_obj): | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     """Annotate a country object using Zooma""" | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     annotate_generic(country_obj, "country") | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  | def annotate_breed(breed_obj): | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |     """Annotate a breed object using Zooma""" | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |     logger.debug("Processing %s" % (breed_obj)) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     result = use_zooma( | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         breed_obj.supplied_breed, "breed") | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |     # update object (if possible) | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |     if result: | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         url = result['ontologyTerms'] | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         # https://stackoverflow.com/a/7253830 | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         term = url.rsplit('/', 1)[-1] | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         # The ontology seems correct. Annotate! | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         logger.info("Updating %s with %s" % (breed_obj, result)) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         url = result['ontologyTerms'] | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         # this is slight different from annotate_generic | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         breed_obj.mapped_breed_term = term | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         breed_obj.mapped_breed = result['text'] | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         # get an int object for such confidence | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         confidence = CONFIDENCES.get_value( | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |             result["confidence"].lower()) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         breed_obj.confidence = confidence | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         breed_obj.save() | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | def annotate_specie(specie_obj): | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     """Annotate a specie object using Zooma""" | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |     annotate_generic(specie_obj, "species") | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 96 |  |  | def annotate_uberon(uberon_obj): | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |     """Annotate an organism part object using Zooma""" | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |     annotate_generic(uberon_obj, "organism part") | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |