Skip to content

Commit

Permalink
GroupCatalogue to make group dicts, hikari.typing, a->e
Browse files Browse the repository at this point in the history
  • Loading branch information
Baharis committed May 16, 2024
1 parent 7e37464 commit 491b1cb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
14 changes: 7 additions & 7 deletions hikari/resources/Hall_symbols.dat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
n:c "H-M entry" "Hall entry"
n:c HM Hall
1 P_1 p_1
2 P_-1 -p_1
3:b P_1_2_1 p_2y
Expand Down Expand Up @@ -189,7 +189,7 @@ n:c "H-M entry" "Hall entry"
38:-cba C_2_m_m c_-2_2
38:bca C_m_2_m c_-2_-2
38:a-cb A_m_2_m a_-2_-2
39 A_b_m_2 a_2_-2b
39 A_e_m_2 a_2_-2b
39:ba-c B_m_a_2 b_2_-2a
39:cab B_2_c_m b_-2a_2
39:-cba C_2_m_b c_-2a_2
Expand All @@ -201,7 +201,7 @@ n:c "H-M entry" "Hall entry"
40:-cba C_2_c_m c_-2c_2
40:bca C_c_2_m c_-2c_-2c
40:a-cb A_m_2_a a_-2a_-2a
41 A_b_a_2 a_2_-2ab
41 A_e_a_2 a_2_-2ab
41:ba-c B_b_a_2 b_2_-2ab
41:cab B_2_c_b b_-2ab_2
41:-cba C_2_c_b c_-2ac_2
Expand Down Expand Up @@ -302,7 +302,7 @@ n:c "H-M entry" "Hall entry"
63:-cba A_m_a_m -a_2_2a
63:bca B_b_m_m -b_2_2b
63:a-cb B_m_m_b -b_2b_2
64 C_m_c_a -c_2ac_2
64 C_m_c_e -c_2ac_2
64:ba-c C_c_m_b -c_2ac_2ac
64:cab A_b_m_a -a_2ab_2ab
64:-cba A_c_a_m -a_2_2ab
Expand All @@ -314,14 +314,14 @@ n:c "H-M entry" "Hall entry"
66 C_c_c_m -c_2_2c
66:cab A_m_a_a -a_2a_2
66:bca B_b_m_b -b_2b_2b
67 C_m_m_a -c_2a_2
67 C_m_m_e -c_2a_2
67:ba-c C_m_m_b -c_2a_2a
67:cab A_b_m_m -a_2b_2b
67:-cba A_c_m_m -a_2_2b
67:bca B_m_c_m -b_2_2a
67:a-cb B_m_a_m -b_2a_2
68:1 C_c_c_a:1 c_2_2_-1ac
68:2 C_c_c_a:2 -c_2a_2ac
68:1 C_c_c_e:1 c_2_2_-1ac
68:2 C_c_c_e:2 -c_2a_2ac
68:1ba-c C_c_c_b:1 c_2_2_-1ac
68:2ba-c C_c_c_b:2 -c_2a_2c
68:1cab A_b_a_a:1 a_2_2_-1ab
Expand Down
85 changes: 85 additions & 0 deletions hikari/symmetry/catalogue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from copy import deepcopy
import pickle
import re

import numpy as np
import pandas as pd

from hikari.utility.typing import PathLike
from hikari.symmetry.group import Group


class GroupCatalogue:
"""Manage generating and mappings of point and space groups"""

REST_COL_FORMAT = {'n:c': '7.7s', 'HM-short': '9.9s',
#'Schoenflies': '7.7s',
'Hall': '14.14s'}

def __init__(self, table: pd.DataFrame) -> 'GroupCatalogue':
has_colon = table['n:c'].str.contains(':')
table['n:c0'] = table['n:c']
table.loc[~has_colon, 'n:c0'] = table.loc[~has_colon, 'n:c'] + ':0'
table[['number', 'setting']] = table['n:c'].str.split(':', expand=True)
table['number'] = table['number'].astype(int)
if 'group' not in table.columns:
table['group'] = [Group.from_hall_symbol(h) for h in table['Hall']]
if 'HM-short' not in table.columns:
table['HM-short'] = table['HM'].str.replace('_', '')
if 'HM-simple' not in table.columns:
colon = table['HM'].str.contains(':')
gsm = Group.System.monoclinic
m = np.array([g.system == gsm for g in table['group']])
table['HM-simple'] = table['HM-short']
table.loc[colon, 'HM-simple'] = \
table.loc[colon, 'HM-simple'].str.replace(r'\:.', '', regex=True)
table.loc[m, 'HM-simple'] = table.loc[m, 'HM'].str.replace('_1', '')
table['HM-simple'] = table['HM-simple'].str.replace('_', '')
if 'standard' not in table.columns:
table['standard'] = table['number'].rolling(2).var() != 0
self.table: pd.DataFrame = table

@classmethod
def _from_ssv(cls, ssv_path: PathLike) -> 'GroupCatalogue':
"""Generate a `GroupCatalogue` from a .ssv file"""
with open(ssv_path, 'r') as ssv_file:
table = pd.read_csv(ssv_file, comment='#', delim_whitespace=True)
return cls(table)

@classmethod
def _from_pickle(cls, pickle_path: PathLike) -> 'GroupCatalogue':
"""Load a `GroupCatalogue` from a .pickle file"""
with open(pickle_path, 'br') as pickle_file:
new = pickle.load(pickle_file)
if isinstance(new, cls):
return new
else:
raise TypeError(f'Loaded pickle is not instance of {cls}')

def _as_rest_table(self, txt_path: PathLike) -> None:
"""Write an instant of `GroupCatalogue` to a .txt file as ReST table"""
r = self.REST_COL_FORMAT
field_len_re = re.compile('^(\d*)(?:\.\d+)?[a-z]*$')
field_lens = [int(field_len_re.match(v).group(1)) for v in r.values()]
sep = '+-' + '-+-'.join(['-' * f for f in field_lens]) + '-+'
fmt = '| ' + ' | '.join([f'{{:{f}.{f}s}}' for f in field_lens]) + ' |'
lines = [fmt.format(*r.keys())]
for t in self.table[list(r.keys())].itertuples(index=False):
lines.append(fmt.format(*t))
rest = sep + '\n' + ('\n' + sep + '\n').join(lines) + '\n' + sep
with open(txt_path, 'w') as txt_file:
txt_file.write(rest)

def _to_pickle(self, pickle_path: PathLike) -> None:
"""Dump an instance of self to a .pickle file"""
with open(pickle_path, 'bw') as pickle_file:
pickle.dump(self, file=pickle_file)

@property
def standard(self) -> 'GroupCatalogue':
standard = deepcopy(self.table[self.table['standard']]).reset_index()
return self.__class__(standard)

def export_dict(self, col: str) -> dict:
exported = self.table[[col, 'group']]
return {r[0]: r[1] for r in exported.itertuples(index=False)}
2 changes: 1 addition & 1 deletion hikari/symmetry/point_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
can be accessed using their short Hermann-Maugin notation, as presented below.
+-------+---------------+----------------+---------------+-----------------+
| No. | CRYSTAL | Hermann-Maugin | Schoenflies | Can be accessed |
| No. | CRYSTAL | Hermann-Mauguin| Schoenflies | Can be accessed |
| | SYSTEM | notation | notation | using |
+-------+---------------+----------------+---------------+-----------------+
| 1 | triclinic | 1 | C1 | `PG['1']` |
Expand Down
3 changes: 0 additions & 3 deletions hikari/symmetry/space_groups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@



"""
Dictionary containing all known space groups written as :class:`Group`
along with alternative axis settings. The point groups in this dictionary
Expand Down
5 changes: 5 additions & 0 deletions hikari/utility/typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
import typing


PathLike = typing.Union[str, bytes, os.PathLike]

0 comments on commit 491b1cb

Please sign in to comment.