<commit_before>"""This defines classes for working with geometry file formats"""

########################################################################
#                                                                      #
# This script was written by <NAME> in 2017.                    #
#        <EMAIL>     <EMAIL>                    #
#                                                                      #
# Copyright 2017 <NAME>                                   #
#                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");      #
# you may not use this file except in compliance with the License.     #
# You may obtain a copy of the License at                              #
#                                                                      #
#    http://www.apache.org/licenses/LICENSE-2.0                        #
#                                                                      #
# Unless required by applicable law or agreed to in writing, software  #
# distributed under the License is distributed on an "AS IS" BASIS,    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      #
# implied.                                                             #
# See the License for the specific language governing permissions and  #
# limitations under the License.                                       #
#                                                                      #
########################################################################

from __future__ import absolute_import

import re
import numpy as np
from numpy.linalg import norm
from .exceptions import UnknownEnergyError, InputError
# TODO add tests for these


def rotation_matrix(axis, theta):
    """
    Return the rotation matrix associated with counterclockwise rotation about
    the given axis by theta radians.

    copied from
    https://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector
    """
    axis = np.asarray(axis)
    axis = axis/np.sqrt(np.dot(axis, axis))
    a = np.cos(theta/2.0)
    b, c, d = -axis * np.sin(theta/2.0)
    aa, bb, cc, dd = a*a, b*b, c*c, d*d
    bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
    return np.array([[aa+bb-cc-dd, 2*(bc+ad), 2*(bd-ac)],
                     [2*(bc-ad), aa+cc-bb-dd, 2*(cd+ab)],
                     [2*(bd+ac), 2*(cd-ab), aa+dd-bb-cc]])


class Vector(np.ndarray):

    def __new__(cls, *xyz):
        if len(xyz) != 3:
            try:
                xyz = xyz[0]
            except IndexError:
                raise InputError(xyz, '3 values are required to make a vector')
        if len(xyz) != 3:
            raise InputError(xyz, 'Length of vector must be 3.')
        obj = super(Vector, cls).__new__(cls, shape=(3,),
                                         buffer=np.array(xyz, dtype=float))
        return obj

    def cross(self, vec):
        return np.cross(self, vec)

    def diff_angle(self, vec):
        return np.arccos(self.dot(vec) /
                         (norm(self) * norm(vec)))

    def rotate(self, axis, angle):
        r_mat = rotation_matrix(axis, angle)
        return np.dot(r_mat, self)

    @property
    def x(self):
        return self[0]

    @property
    def y(self):
        return self[1]

    @property
    def z(self):
        return self[2]


class XYZ(object):
    def __init__(self, f_name):
        self.file = f_name
        with open(f_name, 'r') as f_file:
            f_lines = f_file.readlines()
        self._header = f_lines[0:2]
        if 'Energy' in self._header[1]:
            energy_match = re.search('(?:Energy:\s+)(-\d+\.\d+)',
                                     self._header[1])
            self._energy = float(energy_match.group(1))
        else:
            self._energy = None
        self._original_energy = self._energy
        data = [line.split() for line in f_lines[2:]]
        self.atoms = [atom[0] for atom in data]
        self.coords = [Vector([float(coord) for coord in atom[1:4]]) for
                       atom in data]<commit_msg>Add magnitude property (mag) to Vector class<commit_after>"""This defines classes for working with geometry file formats"""

########################################################################
#                                                                      #
# This script was written by <NAME> in 2017.                    #
#        <EMAIL>     <EMAIL>                    #
#                                                                      #
# Copyright 2017 <NAME>                                   #
#                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");      #
# you may not use this file except in compliance with the License.     #
# You may obtain a copy of the License at                              #
#                                                                      #
#    http://www.apache.org/licenses/LICENSE-2.0                        #
#                                                                      #
# Unless required by applicable law or agreed to in writing, software  #
# distributed under the License is distributed on an "AS IS" BASIS,    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      #
# implied.                                                             #
# See the License for the specific language governing permissions and  #
# limitations under the License.                                       #
#                                                                      #
########################################################################

from __future__ import absolute_import

import re
import numpy as np
from numpy.linalg import norm
from .exceptions import UnknownEnergyError, InputError
# TODO add tests for these


def rotation_matrix(axis, theta):
    """
    Return the rotation matrix associated with counterclockwise rotation about
    the given axis by theta radians.

    copied from
    https://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector
    """
    axis = np.asarray(axis)
    axis = axis/np.sqrt(np.dot(axis, axis))
    a = np.cos(theta/2.0)
    b, c, d = -axis * np.sin(theta/2.0)
    aa, bb, cc, dd = a*a, b*b, c*c, d*d
    bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
    return np.array([[aa+bb-cc-dd, 2*(bc+ad), 2*(bd-ac)],
                     [2*(bc-ad), aa+cc-bb-dd, 2*(cd+ab)],
                     [2*(bd+ac), 2*(cd-ab), aa+dd-bb-cc]])


class Vector(np.ndarray):

    def __new__(cls, *xyz):
        if len(xyz) != 3:
            try:
                xyz = xyz[0]
            except IndexError:
                raise InputError(xyz, '3 values are required to make a vector')
        if len(xyz) != 3:
            raise InputError(xyz, 'Length of vector must be 3.')
        obj = super(Vector, cls).__new__(cls, shape=(3,),
                                         buffer=np.array(xyz, dtype=float))
        return obj

    def cross(self, vec):
        return np.cross(self, vec)

    def diff_angle(self, vec):
        return np.arccos(self.dot(vec) /
                         (norm(self) * norm(vec)))

    def rotate(self, axis, angle):
        r_mat = rotation_matrix(axis, angle)
        return np.dot(r_mat, self)

    @property
    def x(self):
        return self[0]

    @property
    def y(self):
        return self[1]

    @property
    def z(self):
        return self[2]

    @property
    def mag(self):
        return norm(self)


class XYZ(object):
    def __init__(self, f_name):
        self.file = f_name
        with open(f_name, 'r') as f_file:
            f_lines = f_file.readlines()
        self._header = f_lines[0:2]
        if 'Energy' in self._header[1]:
            energy_match = re.search('(?:Energy:\s+)(-\d+\.\d+)',
                                     self._header[1])
            self._energy = float(energy_match.group(1))
        else:
            self._energy = None
        self._original_energy = self._energy
        data = [line.split() for line in f_lines[2:]]
        self.atoms = [atom[0] for atom in data]
        self.coords = [Vector([float(coord) for coord in atom[1:4]]) for
                       atom in data]
{'fraction_non_alphanumeric': 0.13750752558699578, 'fraction_numerical': 0.011559301625526791, 'mean_word_length': 1.4407875404055246, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2812450', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2141, 'n_words': 648}