Skip to content

Commit

Permalink
clean extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
srmnitc committed Aug 15, 2024
1 parent 8eb8265 commit 22de6a7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 8 deletions.
40 changes: 32 additions & 8 deletions atomrdf/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions atomrdf/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

72 changes: 72 additions & 0 deletions examples/02_grain_boundaries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 22de6a7

Please sign in to comment.