本文整理汇总了Python中prov.model.ProvDocument.serialize方法的典型用法代码示例。如果您正苦于以下问题:Python ProvDocument.serialize方法的具体用法?Python ProvDocument.serialize怎么用?Python ProvDocument.serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prov.model.ProvDocument
的用法示例。
在下文中一共展示了ProvDocument.serialize方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from prov.model import ProvDocument [as 别名]
# 或者: from prov.model.ProvDocument import serialize [as 别名]
class ProjectProvenance:
def __init__(self, database_helper, full_provenance=False):
"""
Initializes the provenance for the mjclawar_rarshad project
Parameters
----------
database_helper: DatabaseHelper
full_provenance: bool
Returns
-------
"""
assert isinstance(database_helper, DatabaseHelper)
self.database_helper = database_helper
if full_provenance:
self.prov_doc = ProvDocument.deserialize(dir_info.plan_json)
else:
self.prov_doc = ProvDocument()
self.prov_doc.add_namespace(mcras.BDP_NAMESPACE.name, mcras.BDP_NAMESPACE.link)
self.prov_doc.add_namespace(mcras.ALG_NAMESPACE.name, mcras.ALG_NAMESPACE.link)
self.prov_doc.add_namespace(mcras.DAT_NAMESPACE.name, mcras.DAT_NAMESPACE.link)
self.prov_doc.add_namespace(mcras.LOG_NAMESPACE.name, mcras.LOG_NAMESPACE.link)
self.prov_doc.add_namespace(mcras.ONT_NAMESPACE.name, mcras.ONT_NAMESPACE.link)
def write_provenance_json(self):
self.prov_doc.serialize(dir_info.plan_json)
示例2: example
# 需要导入模块: from prov.model import ProvDocument [as 别名]
# 或者: from prov.model.ProvDocument import serialize [as 别名]
#.........这里部分代码省略.........
g.actedOnBehalfOf(walter, utexas)
g.actedOnBehalfOf(me, utexas)
# Include the ADAMA platform as an Agent and set attribution
# dcterms:title and dcterms:description are hardcoded
# dcterms:language is hard-coded
# dcterms:source is the URI of the public git source repository for ADAMA
# "dcterms:updated": "2015-04-17T09:44:56" - this would actually be the date ADAMA was updated
adama_platform = g.agent(ap['adama_platform'], {'dcterms:title': "ADAMA", 'dcterms:description': "Araport Data and Microservices API", 'dcterms:language':"en-US", 'dcterms:identifier':"https://api.araport.org/community/v0.3/", 'dcterms:updated': "2015-04-17T09:44:56" })
g.wasGeneratedBy(adama_platform, walter)
# Include the ADAMA microservice as an Agent and set attribution+delegation
# dcterms:title and dcterms:description are inherited from the service's metadata
# dcterms:language is hard-coded
# dcterms:identifier is the deployment URI for the service
# dcterms:source is the URI of the public git source repository. The URL in this example is just a dummy
#
# The name for each microservice should be unique. We've decided to
# use the combination of namespace, service name, and version
microservice_name = 'mwvaughn/bar_annotation_v1.0.0'
adama_microservice = g.agent(ap[microservice_name], {'dcterms:title': "BAR Annotation Service", 'dcterms:description': "Returns annotation from locus ID", 'dcterms:language':"en-US", 'dcterms:identifier':"https://api.araport.org/community/v0.3/mwvaughn/bar_annotation_v1.0.0", 'dcterms:source':"https://github.com/Arabidopsis-Information-Portal/prov-enabled-api-sample" })
# the microservice was generated by me on date X (don't use now, use when the service was updated)
g.wasGeneratedBy(adama_microservice, me, datetime.datetime.now())
# The microservice used the platform now
g.used(adama_microservice, adama_platform, datetime.datetime.now())
# Sources
#
# Define BAR
# Agents
nick = g.agent(ap['nicholas_provart'], {
'prov:type': PROV["Person"], 'foaf:givenName': "Nicholas Provart", 'foaf:mbox': "[email protected]"
})
utoronto = g.agent(ap['university_of_toronto'], {
'prov:type': PROV["Organization"], 'foaf:givenName': "University of Toronto", 'dcterms:identifier':"http://www.utoronto.ca/"
})
g.actedOnBehalfOf(nick, utoronto)
# Entity
# All fields derived from Sources.yml
# dcterms:title and dcterms:description come straight from the YAML
# dcterms:identifier - URI pointing to the source's canonical URI representation
# optional - dcterms:language: Recommended best practice is to use a controlled vocabulary such as RFC 4646
# optional - dcterms:updated: date the source was published or last updated
# optional - dcterms:license: Simple string or URI to license. Validate URI if provided?
datasource1 = g.entity(ap['datasource1'], {'dcterms:title': "BAR Arabidopsis AGI -> Annotation", 'dcterms:description': "Most recent annotation for given AGI", 'dcterms:language':"en-US", 'dcterms:identifier':"http://bar.utoronto.ca/webservices/agiToAnnot.php", 'dcterms:updated':"2015-04-17T09:44:56", 'dcterms:license':"Creative Commons 3.0" })
# Set up attribution to Nick
g.wasAttributedTo(datasource1, nick)
# Define TAIR
# Agents
# dcterms:language: Recommended best practice is to use a controlled vocabulary such as RFC 4646
eva = g.agent(ap['eva_huala'], {
'prov:type': PROV["Person"], 'foaf:givenName': "Eva Huala"
})
phoenix = g.agent(ap['phoenix_bioinformatics'], {
'prov:type': PROV["Organization"], 'foaf:givenName': "Phoenix Bioinformatics"
})
g.actedOnBehalfOf(eva, phoenix)
# Entity
# All fields derived from Sources.yml
# optional - dcterms:citation: Plain text bibliographic citation. If only provided as doi, should we try to validate it?
datasource2 = g.entity(ap['datasource2'], {'dcterms:title': "TAIR", 'dcterms:description': "The Arabidopsis Information Resource", 'dcterms:language':"en-US", 'dcterms:identifier':"https://www.arabidopsis.org/", 'dcterms:citation':"The Arabidopsis Information Resource (TAIR): improved gene annotation and new tools. Nucleic Acids Research 2011 doi: 10.1093/nar/gkr1090"})
g.wasAttributedTo(datasource2, eva)
# In Sources.yml, these two sources are nested. Define that relationship here
# There are other types of relationships but we will just use derived from for simplicity in this prototype
g.wasDerivedFrom(ap['datasource1'], ap['datasource2'])
# Depending on which ADAMA microservice type we are using, define an activity
# Eventually, break these into more atomic actions in a chain
action1 = g.activity(ap['do_query'], datetime.datetime.now())
# action1 = g.activity(ap['do_map'], datetime.datetime.now())
# action1 = g.activity(ap['do_generic'], datetime.datetime.now())
# action1 = g.activity(ap['do_passthrough'], datetime.datetime.now())
# Future... Support for ADAMA-native microservices
# action1 = g.activity(ap['generate'], datetime.datetime.now())
# Define current ADAMA response as an Entity
# This is what's being returned to the user and is thus the subject of the PROV record
# May be able to add more attributes to it but this is the minimum
response = g.entity(ap['adama_response'])
# Response is generated by the process_query action
# Time-stamp it!
g.wasGeneratedBy(response, ap['do_query'], datetime.datetime.now())
# The process_query used the microservice
g.used(ap['do_query'], adama_microservice, datetime.datetime.now())
# The microservice used datasource1
g.used(adama_microservice, datasource1, datetime.datetime.now())
# Print prov_n
print(g.get_provn())
# Print prov-json
print(g.serialize())
# Write out as a pretty picture
graph = prov.dot.prov_to_dot(g)
graph.write_png('Sources.png')
示例3: NIDMExporter
# 需要导入模块: from prov.model import ProvDocument [as 别名]
# 或者: from prov.model.ProvDocument import serialize [as 别名]
#.........这里部分代码省略.........
def _get_model_parameters_estimations(self, error_model):
"""
Infer model estimation method from the 'error_model'. Return an object
of type ModelParametersEstimation.
"""
if error_model.dependance == NIDM_INDEPEDENT_ERROR:
if error_model.variance_homo:
estimation_method = STATO_OLS
else:
estimation_method = STATO_WLS
else:
estimation_method = STATO_GLS
mpe = ModelParametersEstimation(estimation_method, self.software.id)
return mpe
def use_prefixes(self, ttl):
prefix_file = os.path.join(os.path.dirname(__file__), 'prefixes.csv')
context = dict()
with open(prefix_file, encoding="ascii") as csvfile:
reader = csv.reader(csvfile)
next(reader, None) # skip the headers
for alphanum_id, prefix, uri in reader:
if alphanum_id in ttl:
context[prefix] = uri
ttl = "@prefix " + prefix + ": <" + uri + "> .\n" + ttl
ttl = ttl.replace(alphanum_id, prefix + ":")
if uri in ttl:
ttl = ttl.replace(alphanum_id, prefix + ":")
elif uri in ttl:
context[prefix] = uri
ttl = "@prefix " + prefix + ": <" + uri + "> .\n" + ttl
ttl = ttl.replace(alphanum_id, prefix + ":")
return (ttl, context)
def save_prov_to_files(self, showattributes=False):
"""
Write-out provn serialisation to nidm.provn.
"""
self.doc.add_bundle(self.bundle)
# provn_file = os.path.join(self.export_dir, 'nidm.provn')
# provn_fid = open(provn_file, 'w')
# # FIXME None
# # provn_fid.write(self.doc.get_provn(4).replace("None", "-"))
# provn_fid.close()
ttl_file = os.path.join(self.export_dir, 'nidm.ttl')
ttl_txt = self.doc.serialize(format='rdf', rdf_format='turtle')
ttl_txt, json_context = self.use_prefixes(ttl_txt)
# Add namespaces to json-ld context
for namespace in self.doc._namespaces.get_registered_namespaces():
json_context[namespace._prefix] = namespace._uri
for namespace in \
list(self.doc._namespaces._default_namespaces.values()):
json_context[namespace._prefix] = namespace._uri
json_context["xsd"] = "http://www.w3.org/2000/01/rdf-schema#"
# Work-around to issue with INF value in rdflib (reported in
# https://github.com/RDFLib/rdflib/pull/655)
ttl_txt = ttl_txt.replace(' inf ', ' "INF"^^xsd:float ')
with open(ttl_file, 'w') as ttl_fid:
ttl_fid.write(ttl_txt)
# print(json_context)
jsonld_file = os.path.join(self.export_dir, 'nidm.json')
jsonld_txt = self.doc.serialize(format='rdf', rdf_format='json-ld',
context=json_context)
with open(jsonld_file, 'w') as jsonld_fid:
jsonld_fid.write(jsonld_txt)
# provjsonld_file = os.path.join(self.export_dir, 'nidm.provjsonld')
# provjsonld_txt = self.doc.serialize(format='jsonld')
# with open(provjsonld_file, 'w') as provjsonld_fid:
# provjsonld_fid.write(provjsonld_txt)
# provn_file = os.path.join(self.export_dir, 'nidm.provn')
# provn_txt = self.doc.serialize(format='provn')
# with open(provn_file, 'w') as provn_fid:
# provn_fid.write(provn_txt)
# Post-processing
if not self.zipped:
# Just rename temp directory to output_path
os.rename(self.export_dir, self.out_dir)
else:
# Create a zip file that contains the content of the temp directory
os.chdir(self.export_dir)
zf = zipfile.ZipFile(os.path.join("..", self.out_dir), mode='w')
try:
for root, dirnames, filenames in os.walk("."):
for filename in filenames:
zf.write(os.path.join(filename))
finally:
zf.close()
# Need to move up before deleting the folder
os.chdir("..")
shutil.rmtree(os.path.join("..", self.export_dir))
示例4: write_targets_prov
# 需要导入模块: from prov.model import ProvDocument [as 别名]
# 或者: from prov.model.ProvDocument import serialize [as 别名]
def write_targets_prov(self, tlist, C, bundle_id):
#Initialisation
# cs = b.agent('CrowdScanner')
if self.document_id == -1:
d = ProvDocument()
d.add_namespace(AO)
d.set_default_namespace(self.defaultns % self.game_id)
if uploadprov:
provstore_document = self.api.document.create(d, name="Operation%s CrowdScanner" % self.game_id, public=True)
document_uri = provstore_document.url
logging.info("prov doc URI: " + str(document_uri))
self.provfilelist.append(provstore_document.id)
self.savelocalrecord()
self.document_id = provstore_document.id
b = ProvDocument() # Create a new document for this update
b.add_namespace(AO)
b.set_default_namespace(self.defaultns % self.game_id)
# cs to be used with all targets
cs = b.agent('agent/CrowdScanner', (('prov:type', AO['IBCCAlgo']), ('prov:type', PROV['SoftwareAgent'])))
timestamp = time.time() # Record the timestamp at each update to generate unique identifiers
startTime = datetime.datetime.fromtimestamp(timestamp)
endTime = startTime
activity = b.activity('activity/cs/update_report_%s' % timestamp, startTime, endTime)
activity.wasAssociatedWith(cs)
#Add target and report entities
for i, tdata in enumerate(tlist):
if self.changedtargets[i]==0:
continue
#Target entity for target i
tid = int(tdata[0])
x = tdata[1]
y = tdata[2]
# targettype = tdata[3] #don't record here, it will be revealed and recorded by UAVs
v = int(tdata[4])
agentids = tdata[7]
targetattributes = {'ao:longitude': x, 'ao:latitude': y, }
#'ao:asset_type':str(targettype)}
target_v0 = b.entity('cs/target/'+str(tid)+'.'+str(v), targetattributes)
#Post the root report if this is the first version
if v==0:
self.targets[tid] = b.entity('cs/target/'+str(tid))
else:
try:
target_v0.wasDerivedFrom(self.targetversions[tid])
except KeyError:
logging.error("Got a key error for key " + str(tid) + ', which is supposed to be version' + str(v))
self.targetversions[tid] = target_v0
target_v0.specializationOf(self.targets[tid])
target_v0.wasAttributedTo(cs)
#Report entities for origins of target i
for j, r in enumerate(self.target_rep_ids[i]):
if r not in self.postedreports:
Crow = C[r,:]
x = Crow[1]
y = Crow[2]
reptext = tdata[5][j].decode('utf8')
# Try to replace unusual characters
reptext = reptext.encode('ascii', 'replace')
agentid = agentids[j]
reporter_name = 'agent/crowdreporter%s' % agentid
b.agent(reporter_name, (('prov:type', AO['CrowdReporter']), ('prov:type', PROV['Person'])))
reportattributes = {'ao:longitude': x, 'ao:latitude': y, 'ao:report': reptext}
self.postedreports[r] = b.entity('cs/report/'+str(r), reportattributes)
self.postedreports[r].wasAttributedTo(reporter_name)
activity.used(self.postedreports[r])
target_v0.wasDerivedFrom(self.postedreports[r])
if uploadprov:
#Invalidate old targets no longer in use
for i,tid in enumerate(self.targets_to_invalidate):
target_v = self.targetversions[tid]
b.wasInvalidatedBy(target_v, activity)
#Post the document to the server
#bundle = b.bundle('crowd_scanner')
bundle_id = 'bundle/csupdate/%s' % timestamp
self.api.add_bundle(self.document_id, b.serialize(), bundle_id)