Skip to content

Commit

Permalink
adapted shape_map script to new function in cosymlib
Browse files Browse the repository at this point in the history
general cleaning
  • Loading branch information
abelcarreras committed Jul 7, 2020
1 parent 197049a commit 0359daf
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 74 deletions.
23 changes: 7 additions & 16 deletions cosymlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,13 @@ def get_molecule_generalized_coord(self, shape_label1, shape_label2, central_ato
def get_path_parameters(self, shape_label1, shape_label2, central_atom=0):

if type(shape_label1) is Geometry:
label1 = shape_label1.get_positions()
label1 = shape_label1
label1_name = shape_label1.name
else:
label1 = shape_label1
label1_name = shape_label1
if type(shape_label2) is Geometry:
label2 = shape_label2.get_positions()
label2 = shape_label2
label2_name = shape_label2.name
else:
label2 = shape_label2
Expand All @@ -540,12 +540,7 @@ def get_path_parameters(self, shape_label1, shape_label2, central_atom=0):
label2_name: self.get_shape_measure(label2, 'measure', central_atom)}
devpath = self.get_molecule_path_deviation(label1, label2, central_atom)
generalized_coord = self.get_molecule_generalized_coord(label1, label2, central_atom)
#criteria = devpath
#devpath = filter_results(devpath, criteria, maxdev, mindev)
#generalized_coord = filter_results(generalized_coord, criteria, maxdev, mindev)
#criteria = generalized_coord
#devpath = filter_results(devpath, criteria, maxgco, mingco)
#generalized_coord = filter_results(generalized_coord, criteria, maxgco, mingco)

return csm, devpath, generalized_coord

def print_minimum_distortion_path_shape(self, shape_label1, shape_label2, central_atom=0,
Expand Down Expand Up @@ -587,22 +582,18 @@ def print_minimum_distortion_path_shape(self, shape_label1, shape_label2, centra
txt_params += 'skipped {} structure/s\n\n'.format(filter_mask.count(False))
output3.write(txt_params)

if type(shape_label1) is Geometry:
label1 = shape_label1.get_positions()
if isinstance(shape_label1, Geometry):
label1_name = shape_label1.name
else:
label1 = shape_label1
label1_name = shape_label1
if type(shape_label2) is Geometry:
label2 = shape_label2.get_positions()
if isinstance(shape_label2, Geometry):
label2_name = shape_label2.name
else:
label2 = shape_label2
label2_name = shape_label2

path = get_shape_path(label1, label2, num_points)
path = get_shape_path(shape_label1, shape_label2, num_points)
txt_path = 'Minimum distortion path\n'
txt_path += ' {:^6} {:^6}\n'.format(shape_label1, shape_label2)
txt_path += ' {:^6} {:^6}\n'.format(label1_name, label2_name)
for idx, value in enumerate(path[0]):
txt_path += '{:6.3f} {:6.3f}'.format(path[0][idx], path[1][idx])
txt_path += '\n'
Expand Down
13 changes: 9 additions & 4 deletions cosymlib/shape/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ def _get_key(central_atom, reference, fix_permutation=False, reference_2=''):
label_key = reference.lower()
else:
label_key = np.array2string(reference.get_positions(), precision=10)
label2_key = reference_2.lower()

if isinstance(reference_2, str):
label2_key = reference_2.lower()
else:
label2_key = np.array2string(reference_2.get_positions(), precision=10)

central_atom_key = int(central_atom)
fix_permutation_key = str(bool(fix_permutation))
Expand Down Expand Up @@ -81,13 +85,13 @@ def structure(self, reference, central_atom=0, fix_permutation=False):
return self._structures[key]

def path_deviation(self, shape_label1, shape_label2, central_atom=0):

from cosymlib.molecule.geometry import Geometry
key = _get_key(central_atom, shape_label1, reference_2=shape_label2)
if key not in self._path_deviation:
Sx = self.measure(shape_label1, central_atom)
Sy = self.measure(shape_label2, central_atom)
new_theta = np.arcsin(np.sqrt(Sx) / 10) + np.arcsin(np.sqrt(Sy) / 10)
if isinstance(shape_label1, np.ndarray):
if isinstance(shape_label1, Geometry):
structure_a = shape_label1
else:
structure_a = tools.get_test_structure(shape_label1, central_atom=central_atom)
Expand All @@ -97,11 +101,12 @@ def path_deviation(self, shape_label1, shape_label2, central_atom=0):
return self._path_deviation[key]

def generalized_coordinate(self, shape_label1, shape_label2, central_atom=0):
from cosymlib.molecule.geometry import Geometry

key = _get_key(central_atom, shape_label1, reference_2=shape_label2)
if key not in self._gen_coord:
Sq = self.measure(shape_label1, central_atom)
if isinstance(shape_label1, np.ndarray):
if isinstance(shape_label1, Geometry):
structure_a = shape_label1
else:
structure_a = tools.get_test_structure(shape_label1, central_atom=central_atom)
Expand Down
6 changes: 3 additions & 3 deletions cosymlib/shape/maps.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from cosymlib.shape import tools, Shape
import numpy as np

from cosymlib.molecule.geometry import Geometry

def get_shape_map(shape_label1, shape_label2, num_points=20):

path_structures = []
if isinstance(shape_label1, np.ndarray):
coordinates_a = shape_label1
if isinstance(shape_label1, Geometry):
coordinates_a = shape_label1.get_positions()
else:
coordinates_a = tools.get_test_structure(shape_label1, central_atom=1).get_positions()
coordinates_b = Shape(coordinates_a).structure(shape_label2, central_atom=len(coordinates_a))
Expand Down
10 changes: 5 additions & 5 deletions scripts/shape
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ parser.add_argument('-m', '--measure',
metavar='SH',
default=None,
help='compute the SH measure of the input structures (use "custom" to use custom structure')
parser.add_argument('-l', '--labels', action='store_true',
dest='labels',
default=False,
help='show the reference shape labels')
parser.add_argument('-s', '--structure',
dest='structure',
action='store_true',
Expand All @@ -50,13 +46,17 @@ parser.add_argument('-r', '--references',
action='store_true',
default=False,
help='store the coordinates of the reference polyhedra in a file')

parser.add_argument('-cref', '--custom_ref',
dest='custom_ref',
metavar='filename',
default=None,
help='define filename containing the structure/s to be used as reference (requires -m custom)')

# Extra options
parser.add_argument('-l', '--labels', action='store_true',
dest='labels',
default=False,
help='show the reference shape labels')
parser.add_argument('--info',
action='store_true',
default=False,
Expand Down
157 changes: 111 additions & 46 deletions scripts/shape_map
Original file line number Diff line number Diff line change
@@ -1,61 +1,126 @@
#!/usr/bin/env python
import argparse
import sys
import yaml
from cosymlib import file_io
from cosymlib import Cosymlib

from cosymlib.file_io.tools import add_extra_keywords, print_header, print_footer, print_input_info
from cosymlib.file_io import read_generic_structure_file
from cosymlib.shape import tools
import argparse
import sys

parser = argparse.ArgumentParser(description='Shape_map ')
parser.add_argument(type=str, dest='input_file', help='input file name(+extension)')
parser.add_argument('-o', '--output_name', dest='output_name', default=None, help='save in file name')
parser.add_argument(type=str, dest="yaml_input", nargs='?', default=None,
help='Perform the calculations with the command file')
parser.add_argument('-c', '--central_atom', action='store', dest='central_atom',
type=int, default=0, help='position of the central atom if exist')
parser.add_argument('-custom_ref', action='store', dest='custom_ref', default=None,
help='take a given structure from the file and use it as reference')
parser.add_argument('-fix_perm', '--fix_permutation', dest='fix_permutation', action='store_true', default=False,
help='use the given permutation to perform a calculation')

# Shape input flags
group_shape = parser.add_argument_group('Shape')
group_shape.add_argument('-x', dest='x', action='store', default=False,
help='x axis label')
group_shape.add_argument('-y', dest='y', action='store', default=False,
help='y axis label')
group_shape.add_argument('-p', dest='p', action='store_true', default=False,
help='Print all the information')


args = parser.parse_args(sys.argv[1:])

# positional arguments
parser.add_argument(type=str,
dest='input_file',
help='input file with structures')
parser.add_argument(type=str,
dest="yaml_input",
nargs='?',
default=None,
help='Input file with keywords')

# Main options
parser.add_argument('-m', '--map',
dest='map',
metavar='SH',
default=None,
nargs=2,
help='get map using shape 2 labels')
parser.add_argument('-cref', '--custom_ref',
dest='custom_ref',
metavar='filename',
default=None,
help='get map using custom 2 structures from a file')
parser.add_argument('-o', '--output_name',
dest='output_name',
metavar='filename',
default=None,
help='store the output into a file')
parser.add_argument('-c', '--central_atom',
dest='central_atom',
metavar='N',
type=int,
default=0,
help='define central atom as the atom in position N in the input structure')

# Extra options
parser.add_argument('-l', '--labels', action='store_true',
dest='labels',
default=False,
help='show the reference shape labels')
parser.add_argument('--info',
action='store_true',
default=False,
help='print information about the input structures')

# Modifiers
parser.add_argument('--fix_permutation',
dest='fix_permutation',
action='store_true',
help='do not permute atoms')
parser.add_argument('--min_dev',
dest='min_dev',
default=0.0,
help='minimum deviation')
parser.add_argument('--max_dev',
dest='max_dev',
default=100.0,
help='maximum deviation')
parser.add_argument('--min_gco',
dest='min_gco',
default=0.0,
help='minimum coordinates gradient')
parser.add_argument('--max_gco',
dest='max_gco',
action='store_true',
default=100.0,
help='maximum coordinates gradient')
parser.add_argument('--n_points',
dest='n_points',
default=20,
type=int,
help='number of path structures to calculate')

args = parser.parse_args()

if args.yaml_input:
with open(args.yaml_input, 'r') as stream:
input_parameters = yaml.load(stream)
add_extra_keywords(args, args.yaml_input)

for key, value in input_parameters.items():
if key.lower() in args:
setattr(args, key.lower(), value)
else:
raise KeyError("Key %s is not valid" % key)
common_output = open(args.output_name, 'w') if args.output_name is not None else sys.stdout
print_header(common_output)

reference_polyhedron = []
if args.input_file is not None:
structures = file_io.read_generic_structure_file(args.input_file, read_multiple=True)
symobj = Cosymlib(structures)
structures = read_generic_structure_file(args.input_file, read_multiple=True)
structure_set = Cosymlib(structures)
n_atoms = structure_set.get_n_atoms()

if args.custom_ref is not None:
if args.info:
print_input_info(structure_set.get_geometries(), output=common_output)
exit()

# Shape's commands
if args.labels:
common_output.write(tools.get_shape_label_info(n_atoms, with_central_atom=args.central_atom))
exit()

reference_polyhedron = []
if args.custom_ref:
reference_polyhedron = file_io.get_geometry_from_file_xyz(args.custom_ref, read_multiple=True)
[x.set_positions(args.central_atom - 1) for x in reference_polyhedron]

references = [geom for geom in reference_polyhedron]
args.map = 'custom'
else:
references = args.map

if not args.x:
args.x = reference_polyhedron[0]
elif not args.y:
args.y = reference_polyhedron[0]
if args.map:
structure_set.print_minimum_distortion_path_shape(references[0],
references[1],
central_atom=args.central_atom,
min_dev=args.min_dev,
max_dev=args.max_dev,
min_gco=args.min_gco,
max_gco=args.max_gco,
num_points=args.n_points,
output_name=args.output_name)

if args.x:
symobj.print_minimum_distortion_path_shape(args.x,
args.y,
central_atom=args.central_atom)
print_footer(common_output)

0 comments on commit 0359daf

Please sign in to comment.