def fromYaml(cls, in_path): in_path = aug.RepoPath(in_path).resolve() with open(in_path, 'rt') as f: blob = yaml.safe_load(f) if 'include' in blob: orgs = [ OntResGit(path=aug.RepoPath(subblob['path']), ref=subblob['ref']) for subblob in blob['include'] ] else: orgs = [ OntResGit(path=aug.RepoPath(subblob['path']), ref=subblob['ref']) for subblob in blob['paths'] ] if not orgs: raise ValueError(f'orgs is epty for {in_path}') referenceIndex = blob['referenceIndex'] namespaces = blob['namespaces'] if isinstance(namespaces, str): namespaces = namespaces.split(' ') snchf = cls(orgs=orgs, namespaces=namespaces, referenceIndex=referenceIndex) return cls(graph=snchf.populate(OntGraph()))
def methods_sneech(): # TODO the sneech files ? org_m = OntResGit(methods) org_h = OntResGit(helper) org_c = OntResGit(core) with org.repo.getRef('methods'): pass
def methods_mapping(): from pyontutils.namespaces import TEMP, ilxtr, tech from nifstd_tools import methods as m import rdflib # TODO add this to ibnode tests _before = OntResGit(methods, ref='8c30706cbc7ccc7443685a34dec026e8afbbedd1') after = OntResGit( methods, ref='860c88a574d17e0d427c1101fa1947b730b613a9') # renaming commit before = OntResGit(after.path, ref=after.ref + '~1') assert before.graph.identity() == _before.graph.identity() bg = before.graph ag = after.graph _, local = next(ag[:ilxtr.indexNamespace:]) local = rdflib.Namespace(str(local)) T0 = TEMP['0'] l0 = local['0'] b = OntGraph().populate_from_triples(bg.subjectGraph(T0)) a = OntGraph().populate_from_triples(ag.subjectGraph(l0)) b.debug() a.debug() it = b.subjectIdentity(T0) il = a.subjectIdentity(l0) assert it == il assert it in (il, ) assert il in (it, ) # (it,) ni il # SIGH assert it in {il: None} assert il in {it: None} r = b.subjectsRenamed(a) renamed = before.graph.subjectsRenamed(after.graph) assert (len(renamed) - 1 == max([ int(v.rsplit('/', 1)[-1]) for v in renamed.values() if v != ilxtr.Something ])) (OntGraph( namespace_manager=after.graph.namespace_manager).populate_from_triples( before.graph.subjectsRenamedTriples(after.graph)).debug()) return renamed index_graph = OntGraph(path=olr / 'ttl/generated/index-methods.ttl') # FIXME either use or record for posterity the commits where these # transformations were run rather than pointing to the branch name with org.repo.getRef('methods'): input_graph = org.graph output_graph = input_graph.mapTempToIndex(index_graph, TEMP, m.local) a, r, c = output_graph.subjectsChanged(input_graph) index_graph.write()
def test(): snchf = SnchFile.fromYaml('../test/sneech-file.yaml') snchf.writeTtl(aug.RepoPath('../test/rando-sneech-ttl.ttl').resolve()) rp = aug.RepoPath(auth.get_path('ontology-local-repo')) wrangler = SneechWrangler(aug.RepoPath('~/git/sneechenator').expanduser()) dir_snchn = wrangler.dir_process / 'test-sneechening' if not dir_snchn.exists(): # FIXME bad workflow dir_snchn.mkdir() path_index = wrangler.path_index(snchf.index) if not path_index.exists(): path_index = wrangler.new_index( snchf.index) # FIXME move inside Sneechenator? or no path_index.commit_from_working_tree(f'new index {snchf.index}') org_index = OntResGit(path_index) expanded = snchf.write(dir_snchn) # TODO commit expanded.commit_from_working_tree(f'expanded snch file') sncher = Sneechenator(org_index, snchf.namespaces, snchf.orgs) #sncher.preSneechUpon(dir_snchn) rg, maybe_sneeches = sncher.sneechReviewGraph() # commit here I think ? # consider using ilxtr.maybeHasIlxId ? # TODO modified maybe_sneeches file + maybe list -> update list breakpoint()
def _parse_graph(self, graph): # there should be only one of each of these graph.debug() gen = graph[:rdf.type:snchn.SneechFile] s = next(gen) try: next(gen) raise ValueError('MORE THAN ONE SNEECH FILE IN A SINGLE FILE!') except StopIteration: pass self.identity_metadata = graph.subjectIdentity( s) # FIXME align naming with idlib when we get there self.referenceIndex = next(graph[s:snchn.referenceIndex:]) self.namespaces = list(graph[s:snchn.namespaces:]) _orgs = [] orgs = [] for bn in graph[s:snchn['include']:]: try: path = aug.RepoPath(next(graph[bn:snchn.path:])) except StopIteration: iri = next(graph[bn:snchn.iri:]) for string_path in graph[iri:snchn.hasLocalPath:]: path = aug.RepoPath(string_path).resolve() if path.exists(): break else: # TODO OntResIri failover ??? probalby not raise ValueError(f'could not find local path for {iri}') ref = next(graph[bn:snchn.ref:]) # TODO if there is a bound name use that and move the raw paths # to annotations for local sourcing of that file that can be # edited manually if needs be org = OntResGit(path, ref) dorg = org.asDeRef() _orgs.append(org) orgs.append(dorg) self._orgs = _orgs self.orgs = orgs
def npokb_mapping(): index_graph = OntGraph(path=auth.get_path('ontology-local-repo') / 'ttl/generated/neurons/npokb-index.ttl') if index_graph.path.exists(): index_graph.parse() # testing index_graph.bind('npokb', npokb) #[index_graph.add((npokb[str(i)], rdf.type, owl.Class)) for i in range(1, 11)] #[index_graph.add((npokb[str(i)], ilxtr.hasTemporaryId, TEMP[str(i)])) for i in range(1, 11)] ios = [] for eff in ( 'common-usage-types', 'huang-2017', 'markram-2015', 'allen-cell-types', ): # FIXME if the index id is already being used it is still added as a temp id incorrectly path = auth.get_path( 'ontology-local-repo') / f'ttl/generated/neurons/{eff}.ttl' org = OntResGit( path, ref='HEAD' ) # HEAD is default but just for clarity set it explicitly here prev_graph = org.graph input_graph = OntGraph(path=path) input_graph.parse() mapped_graph = input_graph.mapStableIdentifiers( prev_graph, ilxtr.origLabel) output_graph = mapped_graph.mapTempToIndex(index_graph, npokb, TEMP) ios.append((mapped_graph, output_graph)) mapped_graph, output_graph = ios[0] a, r, c = output_graph.subjectsChanged(mapped_graph) index_graph.write() [o.write() for i, o, in ios] # when ready #from sparcur.paths import Path #Path(index_graph.path).xopen() breakpoint()