Skip to content

Commit

Permalink
Merge pull request #145 from pyscal/add_purge
Browse files Browse the repository at this point in the history
Add purge
  • Loading branch information
srmnitc authored Jul 18, 2024
2 parents 3ee4941 + b39b4dd commit def08c7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.9.6
current_version = 0.9.7
commit = True
tag = False

Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ url: 'https://atomrdf.pyscal.org'
license: "MIT"
repository-code: https://github.com/pyscal/atomRDF
type: software
version: 0.9.6
version: 0.9.7
48 changes: 46 additions & 2 deletions atomrdf/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from atomrdf.network.ontology import read_ontology
from atomrdf.structure import System
import atomrdf.properties as prp
from atomrdf.stores import create_store
from atomrdf.stores import create_store, purge
import atomrdf.json_io as json_io
from atomrdf.workflow.workflow import Workflow
from atomrdf.sample import Sample
Expand Down Expand Up @@ -195,7 +195,6 @@ def __init__(
enable_log : bool, optional
Whether to enable logging. Default is False.
"""

create_store(
self,
store,
Expand All @@ -204,6 +203,10 @@ def __init__(
structure_store=structure_store,
)

self._store = store
self._identifier = identifier
self._store_file = store_file

# enable logging
if enable_log:
logger = _prepare_log(os.path.join(os.getcwd(), "atomrdf.log"))
Expand All @@ -229,6 +232,37 @@ def __init__(
self._initialize_graph()
self.workflow = Workflow(self)

def purge(self, force=False):
"""
Remove all information from the KnowledgeGraph.
Parameters
----------
force : bool, optional
Whether to proceed with purging the graph. Default is False.
Returns
-------
None
Notes
-----
This method removes all information from the KnowledgeGraph. If the `force` parameter is set to False, a warning is issued before proceeding with the purging.
"""
if not force:
warnings.warn('This will remove all information from the KnowledgeGraph. Call with force=True to proceed.')
return
else:
#clean up files
sample_files = self.sample_files
for file in sample_files:
if os.path.exists(file):
os.remove(file)

graph = purge(self._store, self._identifier, self._store_file)
self.graph = graph
self._n_triples = 0

def add_structure(self, structure):
"""
Add a structure to the knowledge graph.
Expand Down Expand Up @@ -1085,6 +1119,16 @@ def sample_ids(self):

return [x[0] for x in self.triples((None, RDF.type, CMSO.AtomicScaleSample))]

@property
def sample_files(self):
files = []
for sample_id in self.sample_ids:
filepath = self.value(
URIRef(f"{sample_id}_Position"), CMSO.hasPath
).toPython()
files.append(filepath)
return files

@property
def sample_names(self):
"""
Expand Down
24 changes: 23 additions & 1 deletion atomrdf/stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from atomrdf.namespace import Literal

import os

import shutil

def create_store(kg, store, identifier, store_file=None, structure_store=None):
"""
Expand Down Expand Up @@ -130,3 +130,25 @@ def _setup_structure_store(structure_store=None):
if not os.path.exists(structure_store):
os.mkdir(structure_store)
return structure_store

def purge(store, identifier, store_file):
if store in ["Memory", "memory"]:
return _purge_memory(identifier, store_file)

elif store in ["SQLAlchemy", "db", "database", "sqlalchemy"]:
return _purge_alchemy(identifier, store_file)

else:
raise ValueError("Unknown store found!")


def _purge_memory(identifier, store_file):
graph = Graph(store="Memory", identifier=identifier)
return graph

def _purge_alchemy(identifier, store_file):
os.remove(store_file)
graph = Graph(store="SQLAlchemy", identifier=identifier)
uri = Literal(f"sqlite:///{store_file}")
graph.open(uri, create=True)
return graph
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='atomrdf',
version='0.9.6',
version='0.9.7',
author='Abril Azocar Guzman, Sarath Menon',
author_email='[email protected]',
description='Ontology based structural manipulation and quering',
Expand Down
10 changes: 9 additions & 1 deletion tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,13 @@ def test_extract_sample():
# status, _ = s._check_domain_if_ontoterm((CMSO.Material, CMSO.hasDefect, PLDO.AntiphaseBoundary))
# assert status == True

def test_purge():
s = KnowledgeGraph()
sys = System.create.element.Fe(graph=s)
s.purge(force=True)
assert s.n_samples == 0


s = KnowledgeGraph(store='db', store_file=f'testr.db')
sys = System.create.element.Fe(graph=s)
s.purge(force=True)
assert s.n_samples == 0
2 changes: 2 additions & 0 deletions tests/test_structuregraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ def test_structuregraph():
assert(sys.sample != None)




0 comments on commit def08c7

Please sign in to comment.