How to import instances and their respective data properties' values from a CSV file to the ontology?

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

How to import instances and their respective data properties' values from a CSV file to the ontology?

rahul_raj
Hello Jiba!
Thanks for 'Owlready2'. It's helping a lot in doing my project that involves playing around OWL ontology with Python. But I have encountered a problem while trying to import the instances with their respective data property values from a csv file. Basically, I was trying to recreate the same kind of facility which is available on Protege by going to 'Tools' -> 'Create axioms from Excel Workbook'. It lets you do this importing. So, I tried to the same with Owlready2, and was able to create instances by iteration. I have read the CSV file and have it as a dataframe. Suposse the class name is 'Offer', and x is a one dimensional dataframe (single column) which contains the name of all 10 individuals.

for i in range(0,10):
    my_offer = Offer(x[i])

It gives me 10 new instances created under class 'Offer' having names as it's in dataframe 'x'. Now, I created a property 'has_conversion_ratio' like this

with onto:
    class has_conversion_ratio(DataProperty, FunctionalProperty):
        domain = [Offer]
        range = [float]

I wish to now assert these data property values to these instances i.e., allot values of has_conversion_ratio for each instances. This code work perfectly fine for a single instance, one at a time.

onto.offerA.has_conversion_ratio = 0.89

But I wish to assign the data property values to all instances at one go (using loop) since in real cases, I would be having thousands of such instances to deal with. I have another one dimensional dataframe of datatype float 'y' which has the respective values of conversion ratios for each of the these 10 instances.

for i in Offer.instances():
    j=0
    i.has_conversion_ratio = y[j]
    j = j+1

This returned me error i.e., ValueError: Cannot store literal '0.8403944006464152'!  
My y[0] is equal to 0.8403944006464152
I am clueless about what to do allot the data property values to instances. Kindly help on how to do this!
Also, is there any way to access these instances the way we access array or list elements.
Reply | Threaded
Open this post in threaded view
|

Re: How to import instances and their respective data properties' values from a CSV file to the ontology?

Jiba
Administrator
Hello,

> for i in Offer.instances():
>     j=0
>     i.has_conversion_ratio = y[j]
>     j = j+1
>
> This returned me error i.e., ValueError: Cannot store literal
> '0.8403944006464152'!  
> My y[0] is equal to 0.8403944006464152
> I am clueless about what to do allot the data property values to instances.

You used Numpy or Pandas for loading the data, aren't you? I think your float value is not a Python float (but something like numpy.float64). If so, you need to transform them to Python float because Owlready is not designed to work with Numpy:

        my_offer.has_conversion_ratio = float(y[j])

> Also, is there any way to access these instances the way we access array or
> list elements.

You should be cautious when using Offer.instances(), because the order of the instances may not be preserved (it actually depends on the internal database). A better solution is probably to work by row in the data:

for i in range(0,10):
        my_offer = Offer(x[i])
  my_offer.has_conversion_ratio = float(y[j])

Or, if not possible, to create a list of the offers and to fill it as you create them.

Best regards,
Jiba
Reply | Threaded
Open this post in threaded view
|

Re: How to import instances and their respective data properties' values from a CSV file to the ontology?

rahul_raj
In reply to this post by rahul_raj
Thanks a lot, Jiba!
Yes, I had used Pandas for loading the data and yes, it was not the Python float. That's what caused the error that I was talking about. As suggested by you, I change the data type to Python float and it worked! Also, as said by you, it's indeed better to work by row in the data. Finally, this code worked for me.
Here a[i] contains customer IDs. b[i], c[i] and d[i] contains respective values of data properties for each customer.

for i in range(n):
        my_customer = onto.ClassA(a[i])
        my_customer.propertyA = float(b[i])
        my_customer.propertyB = float(c[i])
        my_customer.propertyC = float(d[i])

It has been thrilling working on Ontology with Owlready2. Thanks again!
Reply | Threaded
Open this post in threaded view
|

Re: How to import instances and their respective data properties' values from a CSV file to the ontology?

jui guram
In reply to this post by Jiba
Hi ,

I have been using OWLready to map database to OWL.Its documentation is great and this mailing list is very helpfull.

I  am also trying to do similar thing except I am getting my data from MySql database.I have already created the ontology schema ie the classes,data properties and object properties.
Now I am trying to create instances by accessing the data from database.

Here is my code

with onto:
        for i in range(len(list_of_tuples_CustNo)):
                        my_customer = onto.customers('customer_'+str(i))
                        my_customer.customerNumber = list_of_tuples_CustNo[i][0]

Here list_of_tuples_CustNo[i][0] is all the customernumbers i am trying to attach to dataproperty customerNumber.

The program runs without any error ie it creates instances "customer_1" ,"customer_2"... from this statement my_customer = onto.customers('customer_'+str(i))

But Data property assertion doesn't happen. customersnumber doesnt get attached to these instances.


Any help would really be appreciated.

Regards,
Jui

                       
Reply | Threaded
Open this post in threaded view
|

Re: How to import instances and their respective data properties' values from a CSV file to the ontology?

kloud
In reply to this post by rahul_raj
can you help me please, I have a csv file and I want to populate my ontology with it's content
the line on the file is like this: name, age, num_p

PS: I'm beginner with ontologies, I want to work with olwready