Skip to content

Commit

Permalink
Merge pull request #10 from Materials-Data-Science-and-Informatics/mo…
Browse files Browse the repository at this point in the history
…ve_to_class

Move to Annotate Crystal class
  • Loading branch information
NinadBhat authored Apr 29, 2024
2 parents 3df4ea1 + 5673c9d commit d94bbed
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "atomID"
version = "0.2.2"
version = "0.3.2"
description = "Python package to identify and annotate crystal structure data files"
authors = ["Ninad Bhat"]
readme = "README.md"
Expand Down
File renamed without changes.
112 changes: 112 additions & 0 deletions src/atomid/annotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""Annotate crystal class."""

import atomrdf as ardf
from ase.io import read as ase_read

from atomid.crystal.structure_identification import (
find_lattice_parameter,
get_crystal_structure_using_cna,
)
from atomid.point_defect_analysis.wigner_seitz_method import analyze_defects


class AnnotateCrystal:
"""Annotate crystal object."""

def __init__(self) -> None:
self.system = ardf.System()
self.kg = ardf.KnowledgeGraph()

def read_crystal_structure_file(self, data_file: str, format: str) -> None:
"""Read the crystal structure file.
Parameters
----------
data_file : str
The name of the file to read
format : str
The format of the file. If None, the format is guessed from the file extension
"""
crystal_data = ase_read(data_file, format=format)
kg = ardf.KnowledgeGraph()
crystal_structure = ardf.System.read.file(
filename=crystal_data, format="ase", graph=kg
)

self.kg = kg
self.system = crystal_structure

def annotate_crystal_structure(self) -> None:
"""Identify and annotate the crystal structure.
This method identifies the crystal structure using Common Neighbour Analysis
and lattice constant using radial distribution function.
"""
crystal_structure = get_crystal_structure_using_cna(self.system)

if crystal_structure != "others":
lattice_constants = find_lattice_parameter(self.system, crystal_structure)
self.system = ardf.System.read.file(
crystal_structure,
format="ase",
graph=self.kg,
lattice=crystal_structure,
lattice_constant=lattice_constants,
)

def identify_defects(self, reference_data_file: str, ref_format: str) -> dict:
"""Identify defects in the crystal structure using the reference data file.
Parameters
----------
reference_data_file : str
The name of the file to read
ref_format : str
The format of the file. If None, the format is guessed from the file extension
Returns
-------
defects : dict
A dictionary containing the vacancy and interstitial defects.
"""
actual_positions = self.system.atoms.positions
ref_ase = ase_read(reference_data_file, format=ref_format)
ref_positions = ref_ase.positions

defects: dict[str, dict[str, float]] = analyze_defects(
reference_positions_list=ref_positions,
actual_positions_list=actual_positions,
)
return defects

def annotate_defects(self, reference_data_file: str, ref_format: str) -> None:
"""Annotate defects in the crystal structure using the reference data file.
Parameters
----------
reference_data_file : str
The name of the file to read
ref_format : str
The format of the file. If None, the format is guessed from the file extension
"""
defects = self.identify_defects(reference_data_file, ref_format)

vacancies = defects.get("Vacancies", {"count": 0, "fraction": 0})

if vacancies["count"] > 0:
self.system.add_vacancy(
concentration=vacancies["fraction"], number=vacancies["count"]
)

def write_to_file(self, filename: str, format: str = "ttl") -> None:
"""Write the annotated system to a file.
Parameters
----------
filename : str
The name of the file to write
format : str
The format of the file. If None, the format is guessed from the file extension
"""
self.kg.write(filename, format=format)
9 changes: 5 additions & 4 deletions src/atomid.py → src/atomid/atomid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import ase
from ase.io import read as ase_read
from atomrdf import KnowledgeGraph, System
from crystal_analysis.structure_identification import (
from rdflib import Namespace

from atomid.crystal.structure_identification import (
find_lattice_parameter,
get_crystal_structure_using_cna,
)
from point_defect_analysis.wigner_seitz_method import analyze_defects
from rdflib import Namespace
from atomid.point_defect_analysis.wigner_seitz_method import analyze_defects

CMSO = Namespace("http://purls.helmholtz-metadaten.de/cmso/")
PODO = Namespace("http://purls.helmholtz-metadaten.de/podo/")
Expand Down Expand Up @@ -57,7 +58,7 @@ def identify_defects_in_crystal_structure(
ref_positions = ref_ase.positions

defects: dict[str, dict[str, float]] = analyze_defects(
reference_positions=ref_positions, actual_positions=actual_positions
reference_positions_list=ref_positions, actual_positions_list=actual_positions
)

return defects
Expand Down
File renamed without changes.

0 comments on commit d94bbed

Please sign in to comment.