Skip to content

Commit 13caeeb

Browse files
committed
add from graph methods
1 parent 346522c commit 13caeeb

File tree

3 files changed

+93
-10
lines changed

3 files changed

+93
-10
lines changed

atomrdf/datamodels/workflow/potential.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def to_graph(self, graph, main_id):
4848
return potential
4949

5050
@classmethod
51-
def from_graph(cls, graph, uri):
52-
uri = graph.value(uri, CMSO.hasReference)
53-
label = graph.value(uri, RDFS.label)
54-
pot_type = graph.value(uri, RDF.type)
51+
def from_graph(cls, graph, potential_id):
52+
uri = graph.value(potential_id, CMSO.hasReference)
53+
label = graph.value(potential_id, RDFS.label)
54+
pot_type = graph.value(potential_id, RDF.type)
5555
if pot_type is not None:
5656
pot_type = pot_type.toPython().split("/")[-1]
5757
return cls(uri=uri, label=label, potential_type=pot_type)

atomrdf/datamodels/workflow/workflow.py

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from atomrdf.datamodels.workflow.software import *
3131
from atomrdf.datamodels.workflow.method import *
3232
from atomrdf.datamodels.workflow.xcfunctional import *
33+
from atomrdf.utils import get_simulation
3334

3435

3536
class Simulation(BaseModel, TemplateMixin):
@@ -191,7 +192,7 @@ def _validate_workflow_manager(cls, v):
191192
return SoftwareAgent(**v)
192193
return v
193194

194-
def _add_md_details(self, graph, simulation):
195+
def _to_graph_md_details(self, graph, simulation):
195196
# add ensemble
196197
if self.thermodynamic_ensemble:
197198
ensemble = self.thermodynamic_ensemble.to_graph(graph, simulation)
@@ -202,18 +203,94 @@ def _add_md_details(self, graph, simulation):
202203
potential = self.interatomic_potential.to_graph(graph, simulation)
203204
graph.add((simulation, ASMO.hasInteratomicPotential, potential))
204205

205-
def _add_dft_details(self, graph, simulation):
206+
@classmethod
207+
def _from_graph_md_details(cls, graph, sim_id):
208+
sim = get_simulation(graph, sim_id)
209+
ensemble = graph.value(sim, ASMO.hasStatisticalEnsemble)
210+
if ensemble:
211+
cls.thermodynamic_ensemble = ensemble.toPython().split("/")[-1]
212+
213+
potential = graph.value(sim, ASMO.hasInteratomicPotential)
214+
if potential:
215+
pot_type = potential.toPython().split("/")[-1]
216+
if pot_type == "ModifiedEmbeddedAtomModel":
217+
cls.interatomic_potential = ModifiedEmbeddedAtomModel.from_graph(
218+
graph, potential
219+
)
220+
elif pot_type == "EmbeddedAtomModel":
221+
cls.interatomic_potential = EmbeddedAtomModel.from_graph(
222+
graph, potential
223+
)
224+
elif pot_type == "LennardJonesPotential":
225+
cls.interatomic_potential = LennardJonesPotential.from_graph(
226+
graph, potential
227+
)
228+
elif pot_type == "MachineLearningPotential":
229+
cls.interatomic_potential = MachineLearningPotential.from_graph(
230+
graph, potential
231+
)
232+
else:
233+
cls.interatomic_potential = InteratomicPotential.from_graph(
234+
graph, potential
235+
)
236+
return cls
237+
238+
def _to_graph_dft_details(self, graph, simulation):
206239
# add XC functional
207240
if self.xc_functional:
208241
xc_functional = self.xc_functional.to_graph()
209242
graph.add((simulation, MDO.hasXCFunctional, xc_functional))
210243

211-
def _add_dof(self, graph, simulation):
244+
@classmethod
245+
def _from_graph_dft_details(cls, graph, sim_id):
246+
sim = get_simulation(graph, sim_id)
247+
xc_functional = graph.value(sim, MDO.hasXCFunctional)
248+
if xc_functional:
249+
cls.xc_functional = xc_functional.toPython().split("/")[-1]
250+
return cls
251+
252+
def _to_graph_dof(self, graph, simulation):
212253
if self.degrees_of_freedom:
213254
for dof in self.degrees_of_freedom:
214255
graph.add((simulation, ASMO.hasRelaxationDOF, dof.to_graph()))
215256

216-
def _add_software(self, graph, simulation):
257+
@classmethod
258+
def _from_graph_dof(cls, graph, sim_id):
259+
sim = get_simulation(graph, sim_id)
260+
dofs = [x[2] for x in graph.triples((sim, ASMO.hasRelaxationDOF, None))]
261+
doflist = []
262+
for dof in dofs:
263+
doflist.append(dof.toPython().split("/")[-1])
264+
265+
if doflist:
266+
cls.degrees_of_freedom = doflist
267+
return cls
268+
269+
def _to_graph_software(self, graph, simulation):
270+
workflow_manager = None
271+
if self.workflow_manager:
272+
workflow_manager = self.workflow_manager.to_graph(
273+
graph,
274+
)
275+
graph.add((simulation, PROV.wasAssociatedWith, workflow_manager))
276+
217277
if self.software:
218-
software = self.software.to_graph()
219-
graph.add((simulation, ASMO.hasSoftware, software))
278+
for software in self.software:
279+
softobj = software.to_graph(
280+
graph,
281+
)
282+
graph.add((simulation, PROV.wasAssociatedWith, softobj))
283+
if workflow_manager:
284+
graph.add((workflow_manager, PROV.actedOnBehalfOf, softobj))
285+
286+
@classmethod
287+
def _from_graph_software(cls, graph, sim_id):
288+
sim = get_simulation(graph, sim_id)
289+
workflow_manager = graph.value(sim, PROV.wasAssociatedWith)
290+
if workflow_manager:
291+
cls.workflow_manager = SoftwareAgent.from_graph(graph, workflow_manager)
292+
293+
software = [x[2] for x in graph.triples((sim, PROV.wasAssociatedWith, None))]
294+
if software:
295+
cls.software = [SoftwareAgent.from_graph(graph, s) for s in software]
296+
return cls

atomrdf/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
import numpy as np
66

77

8+
def get_simulation(kg, simulation_id):
9+
if isinstance(simulation_id, str):
10+
simulation_id = URIRef(simulation_id)
11+
return simulation_id
12+
13+
814
def get_material(kg, sample_id):
915
if isinstance(sample_id, str):
1016
sample_id = URIRef(sample_id)

0 commit comments

Comments
 (0)