<commit_before>__author__ = 'ratcave'

import numpy as np
from sklearn.decomposition import PCA

def rotate_to_var(markers):
    """Returns degrees to rotate about y axis so greatest marker variance points in +X direction"""

    # Mean-Center
    markers -= np.mean(markers, axis=0)

    # Vector in direction of greatest variance
    pca = PCA(n_components=1).fit(markers[:, [0, 2]])
    coeff_vec = pca.components_[0]


    # Flip coeff_vec in direction of max variance along the vector.
    marker_var = markers[markers[:,2].argsort(), 2]  # Check variance along component to determine whether to flip.
    winlen = int(len(marker_var)/2+1)  # Window length for moving mean (two steps, with slight overlap)
    var_means = np.array([marker_var[:winlen], marker_var[-winlen:]]).mean(axis=1)
    coeff_vec = coeff_vec * -1 if np.diff(var_means)[0] < 0 else coeff_vec

    # Rotation amount, in radians
    base_vec = np.array([1, 0])  # Vector in +X direction
    msin, mcos = np.cross(coeff_vec, base_vec), np.dot(coeff_vec, base_vec)
    angle = np.degrees(np.arctan2(msin, mcos))
    print("Angle within function: {}".format(angle))
    return angle<commit_msg>utils.rotate_var() now rotates in correct direction depending on the variance of the second pca<commit_after>__author__ = 'ratcave'

import numpy as np
from sklearn.decomposition import PCA

def rotate_to_var(markers):
    """Returns degrees to rotate about y axis so greatest marker variance points in +X direction"""

    # Mean-Center
    markers -= np.mean(markers, axis=0)

    # Vector in direction of greatest variance
    pca = PCA(n_components=2).fit(markers[:, [0, 2]])
    coeff_vec = pca.components_[0]

    # Flip coeff_vec in direction of max variance along the vector.
    markers_rotated = pca.fit_transform(markers)  # Rotate data to PCA axes.
    markers_reordered = markers_rotated[markers_rotated[:,0].argsort(), :]  # Reorder Markers to go along first component
    winlen = int(markers_reordered.shape[0]/2+1)  # Window length for moving mean (two steps, with slight overlap)
    var_means = [np.var(markers_reordered[:winlen, 1]), np.var(markers_reordered[-winlen:, 1])] # Compute variance for each half
    coeff_vec = coeff_vec * -1 if np.diff(var_means)[0] < 0 else coeff_vec  # Flip or not depending on which half if bigger.

    # Rotation amount, in radians
    base_vec = np.array([1, 0])  # Vector in +X direction
    msin, mcos = np.cross(coeff_vec, base_vec), np.dot(coeff_vec, base_vec)
    angle = np.degrees(np.arctan2(msin, mcos))
    print("Angle within function: {}".format(angle))
    return angle
{'fraction_non_alphanumeric': 0.1114099116404149, 'fraction_numerical': 0.012677679600461006, 'mean_word_length': 4.0859375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4302425', 'n_tokens_mistral': 874, 'n_tokens_neox': 850, 'n_words': 260}