diff --git a/atomrdf/graph.py b/atomrdf/graph.py index 0a4a6d3..6be1992 100644 --- a/atomrdf/graph.py +++ b/atomrdf/graph.py @@ -1161,7 +1161,7 @@ def activity_ids(self): return [x[0] for x in self.triples((None, RDF.type, PROV.Activity))] - def iterate_graph(self, item, create_new_graph=False): + def iterate_graph(self, item, create_new_graph=False, create_new_list=False): """ Iterate through the graph starting from the given item. @@ -1177,16 +1177,40 @@ def iterate_graph(self, item, create_new_graph=False): ------- None """ - if isinstance(item, str): - item = URIRef(item) + #active = False + + if not type(item).__name__ == 'URIRef': + return + + #if create_new_graph: + # self.sgraph = KnowledgeGraph() + + if create_new_list: + self.slist = [] - if create_new_graph: - self.sgraph = KnowledgeGraph() triples = list(self.triples((item, None, None))) + #print(triples) + for triple in triples: - self.sgraph.graph.add(triple) + #active = True + #if create_new_graph: + # self.sgraph.graph.add(triple) + self.slist.append(triple) self.iterate_graph(triple[2]) - + + def iterate_and_reapply_triples(self, item): + self.iterate_graph(item, create_new_list=True) + triples = copy.deepcopy(self.slist) + #now we have to edit this triples, and reapply them + #for that we make a dict of all URIRef values in this graph + uri_dict = {} + for triple in triples: + if isinstance(triple[0], URIRef): + print(triple[0]) + if triple[0].toPython() not in uri_dict.keys(): + uri_dict[triple[0].toPython()] = None + return uri_dict + def get_sample(self, sample, no_atoms=False): """ Get the Sample as a KnowledgeGraph @@ -1210,7 +1234,7 @@ def get_sample(self, sample, no_atoms=False): if isinstance(sample, str): sample = URIRef(sample) - self.iterate_graph(sample, create_new_graph=True) + _ = self.iterate_graph(sample, create_new_graph=True) if no_atoms: na = self.sgraph.value(sample, CMSO.hasNumberOfAtoms).toPython() return self.sgraph, na diff --git a/atomrdf/structure.py b/atomrdf/structure.py index d9ae5d0..1c34c7d 100644 --- a/atomrdf/structure.py +++ b/atomrdf/structure.py @@ -2553,3 +2553,18 @@ def add_shear_triples(self, translation_vector, plane, distance, ): self.graph.add((plane_vector, CMSO.hasComponent_y, Literal(plane[1], datatype=XSD.float),)) self.graph.add((plane_vector, CMSO.hasComponent_z, Literal(plane[2], datatype=XSD.float),)) self.graph.add((activity, UNSAFECMSO.hasDistance, Literal(distance, datatype=XSD.float))) + + def copy_defects(self, parent_sample): + if self.sample is None: + return + if parent_sample is None: + return + + parent_material = list([k[2] for k in self.kg.triples((parent_sample, CMSO.hasMaterial, None))])[0] + parent_defects = list([x[2] for x in self.kg.triples((parent_material, CMSO.hasDefect, None))]) + + material = list([k[2] for k in self.kg.triples((self.sample, CMSO.hasMaterial, None))])[0] + + for defect in parent_defects: + new_defect = URIRef(defect.toPython()) + \ No newline at end of file diff --git a/examples/02_grain_boundaries.ipynb b/examples/02_grain_boundaries.ipynb index 4d110a5..86a9917 100644 --- a/examples/02_grain_boundaries.ipynb +++ b/examples/02_grain_boundaries.ipynb @@ -60,6 +60,78 @@ " graph=kg)" ] }, + { + "cell_type": "code", + "execution_count": 4, + "id": "dc65fb07", + "metadata": {}, + "outputs": [], + "source": [ + "from atomrdf.namespace import PROV, CMSO\n", + "parent_material = list(\n", + " [\n", + " k[2]\n", + " for k in kg.triples((struct_gb_1.sample, CMSO.hasMaterial, None))\n", + " ]\n", + ")[0]\n", + "parent_defects = list(\n", + " [x[2] for x in kg.triples((parent_material, CMSO.hasDefect, None))]\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2f07ae38", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[rdflib.term.URIRef('sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary')]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "parent_defects" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6f2b8588", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary\n", + "sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary\n", + "sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary\n", + "sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary\n", + "sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary\n" + ] + }, + { + "data": { + "text/plain": [ + "{'sample:ddb98ea8-c0dd-4b86-b6a1-97bddf93b94a_SymmetricalTiltGrainBoundary': None}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kg.iterate_and_reapply_triples(parent_defects[0])" + ] + }, { "cell_type": "markdown", "id": "717626e5-3672-470e-a9d9-2878310268ca",