UnboundLocalError: local variable 'entity' referenced before assignment

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

UnboundLocalError: local variable 'entity' referenced before assignment

ishitapadhiar
Hi all,

I am getting an error while trying to load in an ontology that I am currently working on. The ontology loads in fine on Protege, but give me a "UnboundLocalError: local variable 'entity' referenced before assignment" error message. I think it might have to do with transitivity properties in the ontology. Any ideas on how to fix it? I can attach copy of the ontology if that might help.

Thanks,
Ishita
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

Jiba
Administrator
Hi,

Yes, please attach or send me the ontology so I can test it.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

ishitapadhiar
Below is the link to the git repo with all the ontologies and attached is the rdf version of the ontology that I was directly trying to load (feo.ttl): feordf.rdf.

Ultimately my goal is to run the pellet reasoner on this and create a new ontology with all the inferred axioms.

https://github.com/tetherless-world/food-explanation-ontology/tree/main/ontologies
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

ishitapadhiar
In reply to this post by Jiba
Hi Jiba,

Hope all is well, just wondering if you were able to make any progress with the bug. Thanks for all your help!

Best,
Ishita
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

Jiba
Administrator
In reply to this post by ishitapadhiar
Hi,

The problem is caused by the fact that the ontology modifies a property (isAllergicTo) that is imported from another ontology. Owlready loads the ontology and then loads all properties from it, then loads the imported ontology.

I fixed the problem by loading imported ontologies BEFORE loading properties, in the development version of Owlready on BitBucket. With this modification, I've been able to load your ontology and to classify it with Pellet (it takes a few minutes though).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

ishitapadhiar
Hi Jiba,

Thanks for your help! A small clarification- are you saying that you fixed the problem by loading imported ontologies before loading properties in the developmental version of OwlReady2, or did you do it using a flag when importing/loading the ontology.

Would you mind sharing the code that got the ontology to load?

Best,
Ishita
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

Jiba
Administrator
Hi,

The fix is in the Owlready sources.

I load the ontology as follows (I put the ontology file in /tmp):


from owlready2 import *
import sys, os, time, types

onto_path.append("/tmp")

onto = get_ontology('/tmp/feordf.rdf').load()

sync_reasoner_pellet()


Jiba
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

ishitapadhiar
Hi Jiba,

Thanks for all your help so far. I think I have found another potential bug, in the sync_reasoner_pellet function. I call the function in my code with the property and data flags both set to true, as my queries require inference over those fields. However when I do so, I found that superclasses do not get inferred into individuals. For instance, I have an individual called "Autumn" in my ontology, which is a feo:Season. After you run the reasoner, it gets characterized as a rdf:type of feo:SeasonCharacteristic (owlready does this properly). However, feo:SeasonCharateristic is a subclass of feo:SystemCharacteristic which is a subclass of feo:Charateristic. According to logical inference this means that feo:Season should also be a rdf:type of feo:SystemCharacteristic and feo:Characteristic, which is not happening with owlready. Running pellet on Protege does have this show these relationships. Reading the documentation, I thought that this was a feature supported by owlready. Please correct me if I am wrong or reach out if you have any questions. This is my code:


onto = World()
print("Loading Ontology")
onto.get_ontology(path).load()
print("Ontology Loaded")

print("Running Pellet")
sync_reasoner_pellet(onto, infer_property_values=True, infer_data_property_values=True)

print("Inferred File")
onto.save(file = "./infered_triples_1.rdf", format = "rdfxml")
Reply | Threaded
Open this post in threaded view
|

Re: UnboundLocalError: local variable 'entity' referenced before assignment

Jiba
Administrator
Hi,

If feo:SeasonCharateristic is a subclass of feo:SystemCharacteristic, which is itself a subclass of feo:Charateristic, any individual classified as feo:SeasonCharateristic will not be classified as feo:Charateristic and feo:SeasonCharateristic, in order to limit multiple instantiations, and because these inferences are rather trivial, and already performed by Python, e.g. when you use the isinstance() function.

If you really need them, you can use the .INDIRECT_is_a attribute to get them (even before calling the reasoner if you only need trivial inferences):

ancestors = individual.INDIRECT_is_a

Jiba