Skip to content

Commit

Permalink
Update SG pickle, simplify its dumping, add strong Group tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baharis committed May 12, 2024
1 parent 5ab853c commit 2168209
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
3 changes: 1 addition & 2 deletions hikari/resources/Hall_symbols.dat
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
n:c H-M entry Hall entry
--------- ------------ --------------
n:c "H-M entry" "Hall entry"
1 P_1 p_1
2 P_-1 -p_1
3:b P_1_2_1 p_2y
Expand Down
10 changes: 8 additions & 2 deletions hikari/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@


def _load_indexed_csv(filename):
return pd.read_csv(io.StringIO(get(__name__, filename).decode('utf-8')),
comment='#', index_col=0)
s = io.StringIO(get(__name__, filename).decode('utf-8'))
return pd.read_csv(s, comment='#', index_col=0)


def _load_indexed_ssv(filename):
s = io.StringIO(get(__name__, filename).decode('utf-8'))
return pd.read_csv(s, comment='#', index_col=0, delim_whitespace=True)


def _load_json(filename):
Expand All @@ -34,3 +39,4 @@ def _save_pickle(data, filename):
characteristic_radiation = _load_json('characteristic_radiation.json')
cif_core_dict = get(__name__, 'cif_core_2.4.5.dic').decode('utf-8')
Xray_atomic_form_factors = _load_indexed_csv('Xray_atomic_form_factors.csv')
hall_symbols_table = _load_indexed_ssv('Hall_symbols.dat')
Binary file modified hikari/resources/space_groups.pickle
Binary file not shown.
12 changes: 11 additions & 1 deletion hikari/symmetry/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"""
from itertools import product as itertools_product
from enum import Enum
from typing import Union
import pickle
from typing import Dict, Union

import numpy as np

Expand All @@ -31,6 +32,15 @@ def _unpack_group_dictionary_from_json(json_dict):
return group_dict


def _dump_group_dictionary_to_pickle(
group_dict: Dict[Union[str, int], 'Group'],
pickle_path: str
):
"""Development function used to make a pickle of space groups"""
with open(pickle_path, 'bw') as pickle_file:
pickle.dump(group_dict, file=pickle_file)


class Group:
"""
Base immutable class containing information about symmetry groups.
Expand Down
9 changes: 7 additions & 2 deletions hikari/symmetry/hall_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ def generators(self) -> List[SymmOp]:
tl4 += self.STATIC_TRANSLATIONS[tl]
generators.append(SymmOp(-np.eye(3), tl4))

if any(elements.group(16, 17, 18)): # TODO
raise self.HallSymbolException('Origin shifts are not implemented')
if any(elements.group(16, 17, 18)):
shift_ints = [int(i) for i in elements.group(16, 17, 18)]
shift_vector = np.array(shift_ints, dtype=float) / 12
shift_op_left = SymmOp(np.eye(3), shift_vector)
shift_op_right = SymmOp(np.eye(3), -shift_vector)
generators = [SymmOp.from_matrix(shift_op_left.matrix @ g.matrix
@ shift_op_right.matrix) for g in generators]

return generators
43 changes: 43 additions & 0 deletions test/test_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest

from hikari.resources import hall_symbols_table, point_groups_json,\
space_groups_json
from hikari.symmetry import Group, PG, SymmOp, SG
from hikari.symmetry.group import _unpack_group_dictionary_from_json # noqa


class TestGroup(unittest.TestCase):
def test_group_initializes(self):
sg230_generator_codes = [
'x,y,z', '-x+1/2,-y,z+1/2', '-x,y+1/2,-z+1/2', 'z,x,y',
'y+3/4,x+1/4,-z+1/4', '-x,-y,-z', 'x+1/2,y+1/2,z+1/2'
]
sg230_generators = [SymmOp.from_code(c) for c in sg230_generator_codes]
_ = Group(*sg230_generators)

def test_point_groups_consistent_with_pickles(self):
json_pg_dict = _unpack_group_dictionary_from_json(point_groups_json)
pickled_pg_dict = PG
all_pg_keys = list({*json_pg_dict.keys(), *pickled_pg_dict.keys()})
for pg_key in all_pg_keys:
json_pg = json_pg_dict[pg_key]
pickled_pg = pickled_pg_dict[pg_key]
self.assertEqual(json_pg, pickled_pg)

def test_space_groups_consistent_with_pickles(self):
json_sg_dict = _unpack_group_dictionary_from_json(space_groups_json)
pickled_sg_dict = SG
all_sg_keys = list({*json_sg_dict.keys(), *pickled_sg_dict.keys()})
for sg_key in all_sg_keys:
json_sg = json_sg_dict[sg_key]
pickled_sg = pickled_sg_dict[sg_key]
self.assertEqual(json_sg, pickled_sg)

def test_space_groups_picked_match_hall(self):
for i in range(1, 231):
sg_index_regex = rf'^{i}(?::[\da-z-]+)?$'
mask = hall_symbols_table.index.str.contains(sg_index_regex)
sg_index_first = list(mask).index(True)
hall_symbol = hall_symbols_table['Hall entry'].iloc[sg_index_first]
print(str(i) + ' ' + hall_symbol)
self.assertEqual(SG[i], Group.from_hall_symbol(hall_symbol))

0 comments on commit 2168209

Please sign in to comment.