def datasetcontext(graph, ds, set_label=True): ieuris = {} iod = None uid = getsinglevalue(ds, 0x00080018, 'UI') # SOP Instance UID if uid is not None: subject = uritools.urifromuid(uid) label = str(uid) else: subject, label = generateuri() if set_label: graph.add((subject, RDFS.label, rdflib.Literal(label))) sopuid = getsinglevalue(ds, 0x00080016, 'UI') # SOP Class UID if sopuid is not None: uidstring = uritools.uidasstring(sopuid) if uidstring in sopclasses: iod = sopclasses[uidstring] for ie in iods[iod].keys(): ieuris[ie] = None else: print >> sys.stderr, "SOP Class", uidstring, "not found" sopuiduri = uritools.urifromuid(sopuid) graph.add((subject, RDF.type, sopuiduri)) return subject, ieuris, iod
def datasetcontext(graph,ds,set_label=True): ieuris={} iod=None uid=getsinglevalue(ds,0x00080018,'UI') # SOP Instance UID if uid is not None: subject=uritools.urifromuid(uid) label=str(uid) else: subject,label=generateuri() if set_label: graph.add((subject,RDFS.label,rdflib.Literal(label))) sopuid=getsinglevalue(ds,0x00080016,'UI') # SOP Class UID if sopuid is not None: uidstring=uritools.uidasstring(sopuid) if uidstring in sopclasses: iod=sopclasses[uidstring] for ie in iods[iod].keys(): ieuris[ie]=None else: print >> sys.stderr, "SOP Class",uidstring,"not found" sopuiduri=uritools.urifromuid(sopuid) graph.add((subject,RDF.type,sopuiduri)) return subject,ieuris,iod
def getieuri(graph,subject,ds,ie): uidtag=None if ie=='Study': uidtag=0x0020000D elif ie=='Series': uidtag=0x0020000E elif ie=='Frame of Reference': uidtag=0x00200052 uri=None if uidtag: uid=getsinglevalue(ds,uidtag,'UI') if uid is not None: uri=uritools.urifromuid(uid) label=str(uid) if not uri: uri,label=generateuri() graph.add((uri,RDF.type,uritools.getieclass(ie))) graph.add((uri,RDFS.label,rdflib.Literal(label))) graph.add((subject,DCTERMS.subject,uri)) return uri
def getieuri(graph, subject, ds, ie): uidtag = None if ie == 'Study': uidtag = 0x0020000D elif ie == 'Series': uidtag = 0x0020000E elif ie == 'Frame of Reference': uidtag = 0x00200052 uri = None if uidtag: uid = getsinglevalue(ds, uidtag, 'UI') if uid is not None: uri = uritools.urifromuid(uid) label = str(uid) if not uri: uri, label = generateuri() graph.add((uri, RDF.type, uritools.getieclass(ie))) graph.add((uri, RDFS.label, rdflib.Literal(label))) graph.add((subject, DCTERMS.subject, uri)) return uri
def tripleobject(vr,value): if vr in ('AE','CS','LO','SH'): value=unicode(value) value=value.strip(' ') # leading and trailing spaces insignificant value=cleantext(value) if value=='': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr in ('LT','ST','UT'): value=unicode(value) value=value.rstrip(' ') # trailing spaces insignificant value=cleantext(value) if value=='': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr=='PN': value=unicode(value) value=value.strip(' ') # leading and trailing spaces insignificant value=cleantext(value) if value=='': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr=='UI': return uritools.urifromuid(value) elif vr=='AS': value=unicode(value) value=cleantext(value) if value=='': # will almost always mean unknown -> drop return None return parseduration(value) elif vr=='IS': value=str(value) if value.strip()=='': return None value=int(value) assert type(value)==int return rdflib.Literal(long(value)) elif vr=='DS': value=str(value) if value.strip()=='': return None return rdflib.Literal(float(value),datatype=XSD.double) elif vr in ('FL','OF'): assert type(value)==float return rdflib.Literal(value,datatype=XSD.double) elif vr=='FD': assert type(value)==float return rdflib.Literal(value,datatype=XSD.double) elif vr in ('SL','SS','UL','US','US or SS'): # UL requires xsd:long! assert type(value) in (int,long),type(value) return rdflib.Literal(long(value)) elif vr=='AT': return rdflib.Literal(long(value)) elif vr=='DA': value=str(value) if not value: return None return rdflib.Literal(parsedate(value)) elif vr=='TM': value=str(value) if not value: return None return rdflib.Literal(parsetime(value)) # rdf representation for missing seconds, hours ? elif vr=='DT': value=str(value) if not value: return None return rdflib.Literal(parsedatetime(value)) # rdf representation for things missing after year ? elif vr=='UN': # represent UN as plain literal if possible try: value=unicode(value) value=value.strip(' ') # leading and trailing spaces insignificant value=cleantext(value) except Exception: return None if value=='': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr in ('OB','OW','OB or OW','OW or OB'): return None else: assert False,vr
def tripleobject(vr, value): if vr in ('AE', 'CS', 'LO', 'SH'): value = unicode(value) value = value.strip(' ') # leading and trailing spaces insignificant value = cleantext(value) if value == '': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr in ('LT', 'ST', 'UT'): value = unicode(value) value = value.rstrip(' ') # trailing spaces insignificant value = cleantext(value) if value == '': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr == 'PN': value = unicode(value) value = value.strip(' ') # leading and trailing spaces insignificant value = cleantext(value) if value == '': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr == 'UI': return uritools.urifromuid(value) elif vr == 'AS': value = unicode(value) value = cleantext(value) if value == '': # will almost always mean unknown -> drop return None return parseduration(value) elif vr == 'IS': value = str(value) if value.strip() == '': return None value = int(value) assert type(value) == int return rdflib.Literal(long(value)) elif vr == 'DS': value = str(value) if value.strip() == '': return None return rdflib.Literal(float(value), datatype=XSD.double) elif vr in ('FL', 'OF'): assert type(value) == float return rdflib.Literal(value, datatype=XSD.double) elif vr == 'FD': assert type(value) == float return rdflib.Literal(value, datatype=XSD.double) elif vr in ('SL', 'SS', 'UL', 'US', 'US or SS'): # UL requires xsd:long! assert type(value) in (int, long), type(value) return rdflib.Literal(long(value)) elif vr == 'AT': return rdflib.Literal(long(value)) elif vr == 'DA': value = str(value) if not value: return None return rdflib.Literal(parsedate(value)) elif vr == 'TM': value = str(value) if not value: return None return rdflib.Literal(parsetime(value)) # rdf representation for missing seconds, hours ? elif vr == 'DT': value = str(value) if not value: return None return rdflib.Literal(parsedatetime(value)) # rdf representation for things missing after year ? elif vr == 'UN': # represent UN as plain literal if possible try: value = unicode(value) value = value.strip( ' ') # leading and trailing spaces insignificant value = cleantext(value) except Exception: return None if value == '': # will almost always mean unknown -> drop return None return rdflib.Literal(value) elif vr in ('OB', 'OW', 'OB or OW', 'OW or OB'): return None else: assert False, vr
#!/usr/bin/python import sys import dicom, rdflib sys.path.append('..') import uritools from sopclasses import * from namespaces import * graph = uritools.newgraph() for uid, value in dicom._UID_dict.UID_dictionary.items(): name = value[0] if not uid or not name: continue uid = uid.strip() subject = uritools.urifromuid(uid) graph.add((subject, RDFS.label, rdflib.Literal(name))) if uid in sopclasses: graph.add((subject, RDF.type, OWL.Class)) graph = graph.serialize(sys.stdout, format="pretty-xml")
#!/usr/bin/python import sys import dicom,rdflib sys.path.append('..') import uritools from sopclasses import * from namespaces import * graph=uritools.newgraph() for uid,value in dicom._UID_dict.UID_dictionary.items(): name=value[0] if not uid or not name: continue uid=uid.strip() subject=uritools.urifromuid(uid) graph.add((subject,RDFS.label,rdflib.Literal(name))) if uid in sopclasses: graph.add((subject,RDF.type,OWL.Class)) graph=graph.serialize(sys.stdout,format="pretty-xml")