Skip to content

Commit

Permalink
change printing format of gsym and cchir outputs
Browse files Browse the repository at this point in the history
add printing precision to Cosymlib class
  • Loading branch information
abelcarreras committed Jul 23, 2022
1 parent 78f7de5 commit 831ee9a
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 42 deletions.
117 changes: 97 additions & 20 deletions cosymlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _get_symmetry_arguments(locals):
return kwargs


def _get_table_format(labels, molecules_names, data):
def _get_table_format_measures(labels, molecules_names, data, precision=3):

txt = 'Structure'
max_len_name = 12
Expand All @@ -61,14 +61,14 @@ def _get_table_format(labels, molecules_names, data):
txt += '{:{width}}'.format(name + ',', width=max_len_name)
n = 11
for idn, label in enumerate(labels):
txt += '{:>{width}.{prec}f},'.format(data[idn][idx], width=n, prec=3)
txt += '{:>{width}.{prec}f},'.format(data[idn][idx], width=n, prec=precision)
n = 10
txt += '\n'
txt += '\n'
return txt


def _get_table_format_chir(labels, molecules_names, data, axis):
def _get_table_format_axes(labels, molecules_names, axis, precision=3):

txt = 'Structure'
max_len_name = 12
Expand All @@ -90,7 +90,7 @@ def _get_table_format_chir(labels, molecules_names, data, axis):
txt += '{:{width}}'.format(name + ',', width=max_len_name)
n = 11
for idn, label in enumerate(labels):
txt += '{:>{width}.{prec}f},'.format(data[idn][idx], width=n, prec=3)
#txt += '{:>{width}.{prec}f},'.format(data[idn][idx], width=n, prec=precision)
n = 10
txt += '\n'

Expand All @@ -100,7 +100,7 @@ def _get_table_format_chir(labels, molecules_names, data, axis):
txt += ' ' * (max_len_name-1) + c_lab + ' '

for c in np.array(axis).T[idn][0]:
txt += '{:>{width}.{prec}f},'.format(c, width=n, prec=3)
txt += '{:>{width}.{prec}f},'.format(c, width=n, prec=precision)
n = 10
txt += '\n'

Expand All @@ -109,6 +109,62 @@ def _get_table_format_chir(labels, molecules_names, data, axis):
return txt


def _get_table_format_permutation(labels, molecules_names, permutation):

txt = 'Structure'
max_len_name = 12
names = []
for name in molecules_names:
if len(name) > max_len_name:
names.append(name[:(max_len_name - 1)])
elif len(name) > 0:
names.append(name)

n = max_len_name - 3
for label in labels:
n += len(label)
txt += '{}'.format(label.rjust(n))
n = 11 - len(label)
txt += '\n\n'

for idx, name in enumerate(names):
txt += '{:{width}}'.format(name + ',', width=max_len_name)
n = 11
for idn, label in enumerate(labels):
# txt += '{:>{width}.{prec}f},'.format(data[idn][idx], width=n, prec=precision)
n = 10
txt += '\n\n'

for idn in range(len(np.array(permutation)[idn][0])):
txt += ' ' * (max_len_name + 1)
for c in np.array(permutation).T[idn][0]:
txt += '{:^{width}}'.format(c, width=n+1 )
n = 10
txt += '\n'
txt += '\n'
txt += '\n'
return txt


def _get_table_format_gsym(molecules_names, csm_list, permutation_list, axis_list, precision=3):

txt = ' CSM '+ (2*precision)*' '+ 'Symmetry axis (x,y,z) '+ ' '*(1*precision) + 'permutation\n\n'
for idx, name in enumerate(molecules_names):

max_name = len(max(name, key=len))
txt += '{} '.format(name)
if max_name < 9:
n = 18 - len(name)
else:
n = 9 + max_name - len(name)

txt += '{:{width}.{prec}f} '.format(csm_list[idx], width=n, prec=precision) + \
' {:.{prec}f} {:.{prec}f} {:.{prec}f}'.format(*axis_list[idx], width=n, prec=precision) + \
' {}\n'.format(permutation_list[idx])

return txt


def _get_axis_info(molecule, group, axis, axis2, center):

txt = ''
Expand Down Expand Up @@ -158,7 +214,8 @@ def __init__(self,
connectivity=None,
connectivity_thresh=None,
charge_eh=0,
mode=0):
mode=0,
precision=3):

def get_electronic_structure(structure):
if mode == 0:
Expand Down Expand Up @@ -189,6 +246,7 @@ def get_electronic_structure(structure):
if ignore_connectivity:
molecule.geometry.set_connectivity(None)

self._precision = precision

def get_n_atoms(self):
"""
Expand Down Expand Up @@ -261,7 +319,7 @@ def print_shape_measure(self, shape_reference, central_atom=0, fix_permutation=F
else:
references_names.append(reference)

txt_shape = _get_table_format(references_names, molecules_names, measure_list)
txt_shape = _get_table_format_measures(references_names, molecules_names, measure_list, self._precision)
output.write(txt_shape)

def print_shape_structure(self, shape_reference, central_atom=0, fix_permutation=False, output=sys.stdout):
Expand Down Expand Up @@ -379,18 +437,20 @@ def print_geometric_symmetry_measure(self, label, central_atom=0, center=None, p

txt = 'Evaluating symmetry operation : {}\n \n'.format(label)

txt += ' CSM permutation\n'
molecules_names = []
csm_list = []
permutation_list = []
axis_list = []

for idx, molecule in enumerate(self._molecules):
csm = molecule.geometry.get_symmetry_measure(**kwargs)
permutation = molecule.geometry.get_symmetry_permutation(**kwargs)
csm_list.append(molecule.geometry.get_symmetry_measure(**kwargs))
permutation_list.append(molecule.geometry.get_symmetry_permutation(**kwargs))
molecules_names.append(molecule.name)
axis_list.append(molecule.geometry.get_symmetry_optimum_axis(label, central_atom=central_atom,
center=center, permutation=permutation))

max_name = len(max(molecule.name, key=len))
txt += '{} '.format(molecule.name)
if max_name < 9:
n = 18 - len(molecule.name)
else:
n = 9 + max_name - len(molecule.name)
txt += '{:{width}.{prec}f}'.format(csm, width=n, prec=3) + ' {}\n'.format(permutation)

txt += _get_table_format_gsym(molecules_names, csm_list, permutation_list, axis_list, precision=self._precision)

output.write(txt)

Expand Down Expand Up @@ -449,6 +509,7 @@ def print_chirality_measure(self, order=1, central_atom=0, center=None, permutat

csm_list = []
axis_list = []
permutation_list = []
for label in reference:
csm_list.append([geometry.get_symmetry_measure(label, central_atom=central_atom,
center=center, permutation=permutation)
Expand All @@ -457,9 +518,25 @@ def print_chirality_measure(self, order=1, central_atom=0, center=None, permutat
center=center, permutation=permutation)
for geometry in self.get_geometries()])

permutation_list.append([geometry.get_symmetry_permutation(label, central_atom=central_atom,
center=center, permutation=permutation)
for geometry in self.get_geometries()])

molecules_names = [molecule.name for molecule in self._molecules]
txt = 'Chirality measure and symmetry axis\n\n'
txt += _get_table_format_chir(reference, molecules_names, csm_list, axis_list)

txt = 'Chirality measures\n\n'
txt += _get_table_format_measures(reference, molecules_names, csm_list, precision=self._precision)

txt += '\nChirality plane axes\n\n'

txt += _get_table_format_axes(reference, molecules_names, axis_list, precision=self._precision)


if permutation is not None:
txt += 'Custom permutation: {}\n'.format(permutation)
else:
txt += 'Atoms permutation \n\n'
txt += _get_table_format_permutation(reference, molecules_names, permutation_list)

output.write(txt)

Expand Down Expand Up @@ -487,7 +564,7 @@ def _print_electronic_symmetry_measure(self, group, axis=None, axis2=None, cente
txt += '\n'
txt += sep_line

txt += '{:<9} '.format(molecule.name)[:9] + ' '.join(['{:7.3f}'.format(s) for s in wf_measure['csm']])
txt += '{:<9} '.format(molecule.name)[:9] + ' '.join(['{:7.3f}'.format(s) for s in wf_measure['csm']])
txt += '\n'
first = False

Expand Down
2 changes: 1 addition & 1 deletion cosymlib/file_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def get_permutation_from_file(file_name):
with open(file_name, 'r') as f:
for line in f:
try:
permutation += np.array(line.strip().split(), dtype=int).tolist()
permutation.append(np.array(line.strip().split(), dtype=int).tolist())
except ValueError:
raise Exception('Error reading permutation file')

Expand Down
26 changes: 18 additions & 8 deletions scripts/cchir
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ parser.add_argument('--permutation_file',
metavar='filename',
type=str,
default=None,
help='define the atoms permutation to be used in the calculations')
help='use atoms permutation stored in file')
parser.add_argument('--print_precision',
dest='print_precision', metavar='I',
type=int,
default=3,
help='number of decimal places to print (default: 3)')


args = parser.parse_args()

Expand All @@ -110,7 +116,7 @@ else:
if args.permutation_file:
permutation = get_permutation_from_file(args.permutation_file)
else:
permutation = None
permutation = [None]

if args.version:
print('Cosymlib version = {}'.format(__version__))
Expand All @@ -128,7 +134,8 @@ structure_set = Cosymlib(structures,
ignore_atoms_labels=args.ignore_atoms_labels,
ignore_connectivity=args.ignore_connectivity,
connectivity=connectivity,
connectivity_thresh=args.connectivity_thresh)
connectivity_thresh=args.connectivity_thresh,
precision=args.print_precision)

if args.info:
print_input_info(structure_set.get_geometries(), output=common_output)
Expand All @@ -137,10 +144,13 @@ if args.info:

# Chirality commands
if args.measure:
structure_set.print_chirality_measure(args.order,
central_atom=args.central_atom,
center=args.center,
permutation=permutation,
output=common_output)
for i, p in enumerate(permutation):
structure_set.print_chirality_measure(args.order,
central_atom=args.central_atom,
center=args.center,
permutation=p,
output=common_output)
if i < len(permutation)-1:
print('.'*70 + '\n')

print_footer(common_output)
39 changes: 26 additions & 13 deletions scripts/gsym
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ parser.add_argument('--permutation_file',
dest='permutation_file',
metavar='filename',
default=None,
help='define the atoms permutation to be used in the calculations')
help='use atoms permutation stored in file')
parser.add_argument('--print_precision',
dest='print_precision', metavar='I',
type=int,
default=3,
help='number of decimal places to print (default: 3)')


args = parser.parse_args()
Expand Down Expand Up @@ -134,7 +139,7 @@ else:
if args.permutation_file:
permutation = get_permutation_from_file(args.permutation_file)
else:
permutation = None
permutation = [None]

print_header(common_output)

Expand All @@ -143,7 +148,8 @@ structure_set = Cosymlib(structures,
ignore_atoms_labels=args.ignore_atoms_labels,
ignore_connectivity=args.ignore_connectivity,
connectivity=connectivity,
connectivity_thresh=args.connectivity_thresh)
connectivity_thresh=args.connectivity_thresh,
precision=args.print_precision)

if args.labels:
print_symmetry_labels()
Expand All @@ -155,11 +161,14 @@ if args.info:

# Symgroup commands
if args.measure:
structure_set.print_geometric_symmetry_measure(args.measure,
central_atom=args.central_atom,
center=args.center,
permutation=permutation,
output=common_output)
for i, p in enumerate(permutation):
structure_set.print_geometric_symmetry_measure(args.measure,
central_atom=args.central_atom,
center=args.center,
permutation=p,
output=common_output)
if i < len(permutation)-1:
print('.'*70 + '\n')

if args.structure:
if args.output_name is None:
Expand All @@ -168,11 +177,15 @@ if args.structure:
else:
structure_output = open(args.output_name + '_sym.xyz', 'w')

structure_set.print_symmetry_nearest_structure(args.measure,
central_atom=args.central_atom,
center=args.center,
permutation=permutation,
output=structure_output)
for i, p in enumerate(permutation):
structure_set.print_symmetry_nearest_structure(args.measure,
central_atom=args.central_atom,
center=args.center,
permutation=permutation,
output=structure_output)

if i < len(permutation)-1:
print('.'*70 + '\n')

# point group
if args.determine_pg:
Expand Down

0 comments on commit 831ee9a

Please sign in to comment.