Problem with owlready2

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

Problem with owlready2

leocunha
Hello , Folks!
My name is Leonardo and I'm new here in this forum.
Thanks for this oportunity!

I'm trying capture the pair concept|concept using this code below.
My pc works until the file reaches 7mb.
Have you been through this kind of problem?
Thank you in advance for your help.
Att
Leonardo



CODE

-----------------------------------------------------------------------------------------
from owlready2 import *
import p07_allconcepts as f

default_world.set_backend(filename="pymedtermino.sqlite3")
PYM = get_ontology("http://PYM/").load()
SNOMEDCT_US = PYM["SNOMEDCT_US"]
CUI = PYM["CUI"]

for d in SNOMEDCT_US.descendant_concepts():
    for e in d.children:
        f.conceito(d,e)

print('************************')
print('end')
print('************************')

-----------------------------------------------------------------------------------------
from owlready2 import *

def conceito(d,e):


    l = (str(d.unifieds) + '|' + str(e.unifieds))
    a = str(d.unifieds)
    b = str(e.unifieds)

    a = a.split('#')
    b = b.split('#')

    a = a[0].replace("[CUI[", "")
    a = a.replace("]", "")
    a = a.replace('"', "")
    a = a.replace(' ', "")

    b = b[0].replace("[CUI[", "")
    b = b.replace("]", "")
    b = b.replace('"', "")
    b = b.replace(' ', "")

    # print(d)
    # print(e)

    linha = str(a) + '|' + str(b) + '\n'
    # print(linha)

    text_file = open("Output.txt", "a")
    text_file.write(linha)
    text_file.close()










Reply | Threaded
Open this post in threaded view
|

Re: Problem with owlready2

Jiba
Administrator
Hi,

I've successfully ran your code; it produced a 9.7 Mb file.

You can make you program faster by :

 * opening the file only once (not at each write)

 * create the line in a shorter way, as follows:



for d in SNOMEDCT_US.descendant_concepts():
  for e in d.children:
    linha = d.unifieds[0].name + '|' + e.unifieds[0].name + '\n'
    text_file.write(linha)
text_file.close()

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Problem with owlready2

leocunha
Hello, Jiba!
Thanks a lot!
It works well!

Another simple question:
I need select the CUI of all relations on Snomed, like is_a, has_finding_site, etc.
How can do it?

Thanks for your help!
att
leo
Reply | Threaded
Open this post in threaded view
|

Re: Problem with owlready2

Jiba
Administrator
Hi,

I think you can use a set for that:

cuis = set()

for x in sno.is_a:
    for x_cui in x.unifieds:
        cuis.add(x_cui)

for x in sno.has_finding_site:
    for x_cui in x.unifieds:
        cuis.add(x_cui)

etc..., where sno is the SNOMED CT concept.

You may also use sno.get_class_properties() to obtain the list of properties available for that concept:

for prop in sno.get_class_properties():
    for x in prop[sno]:
        if not isinstance(x, EntityClass): continue
        for x_cui in x.unifieds:
            cuis.add(x_cui)

However, note that .get_class_properties() does not include is_a, and it may contain non desired properties (e.g. unifieds itself).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Problem with owlready2

leocunha
Hi, Jiba!
How are you?
I was talking with my professor and we have another question...
What's the difference between the .is_a function and .children function?
Thanks for your help.
leo
Reply | Threaded
Open this post in threaded view
|

Re: Problem with owlready2

Jiba
Administrator
Hi,

.is_a contains the superclass of a class, or the class(es) of an instance.

.children (only available for terminologies) contains the child classes of a term. It is very similar to .subclasses(), but the child classes are ordered by terminology codes.

Jiba