# Path: telluride_decoding/scaled_lda.py
# class LinearDiscriminantAnalysis(object):
# class ScaledLinearDiscriminantAnalysis(LinearDiscriminantAnalysis):
#   def __init__(self):
#   def mean_vectors(self):
#   def coef_array(self):
#   def labels(self):
#   def model_parameters(self):
#   def model_parameters(self, values):
#   def _set_parameters(self, values):
#   def from_fitted_data(cls, x, y):
#   def expand_dims(self, data):
#   def _compute_in_class_scatter_matrix(self, x, y):
#   def _compute_between_class_scatter_matrix(self, x, y):
#   def fit(self, x, y):
#   def transform(self, x):
#   def fit_transform(self, x, y):
#   def explained_variance_ratio(self):
#   def __init__(self):
#   def model_parameters(self):
#   def model_parameters(self, values):
#   def _set_parameters(self, values):
#   def fit(self, x, y, y0=0, y1=1):
#   def fit_two_classes(self, class0, class1):
#   def transform(self, x):


import os
import matplotlib.pyplot as plt
import numpy as np
from absl.testing import absltest
from telluride_decoding import scaled_lda
    with self.assertRaisesRegex(
        ValueError, 'Must fit the model before transforming.'):
      lda.explained_variance_ratio()

    x_lda = lda.fit_transform(x, y)

    labels = lda.labels
    self.assertLen(labels, 2)

    # Plot the transformed data.
    plt.subplot(2, 1, 2)
    plt.plot(x_lda[y == labels[0], 0], x_lda[y == labels[0], 1], 'rx')
    plt.plot(x_lda[y == labels[1], 0], x_lda[y == labels[1], 1], 'bo')
    plt.title('Transfomed Data')

    # Make sure the transformed centers are symmetric on the first (x) axis.
    mean_vectors = [np.reshape(v, (1, -1)) for v in lda.mean_vectors]
    centers = lda.transform(np.concatenate(mean_vectors, axis=0))
    print('Transformed centers are:', centers)
    self.assertAlmostEqual(centers[0, 0], -centers[1, 0], delta=0.1)
    np.testing.assert_allclose(centers[:, 1], [0., 0.], atol=0.1)

    plt.savefig(os.path.join(os.environ.get('TMPDIR') or '/tmp',
                             'scaled_lda.png'))

    with self.assertRaisesRegex(
        TypeError, 'Inconsistent training and transform sizes'):
      lda.transform(d1[:, 0:1])

    # Now test model from saved parameters

# Path: telluride_decoding/scaled_lda.py
# class LinearDiscriminantAnalysis(object):
# class ScaledLinearDiscriminantAnalysis(LinearDiscriminantAnalysis):
#   def __init__(self):
#   def mean_vectors(self):
#   def coef_array(self):
#   def labels(self):
#   def model_parameters(self):
#   def model_parameters(self, values):
#   def _set_parameters(self, values):
#   def from_fitted_data(cls, x, y):
#   def expand_dims(self, data):
#   def _compute_in_class_scatter_matrix(self, x, y):
#   def _compute_between_class_scatter_matrix(self, x, y):
#   def fit(self, x, y):
#   def transform(self, x):
#   def fit_transform(self, x, y):
#   def explained_variance_ratio(self):
#   def __init__(self):
#   def model_parameters(self):
#   def model_parameters(self, values):
#   def _set_parameters(self, values):
#   def fit(self, x, y, y0=0, y1=1):
#   def fit_two_classes(self, class0, class1):
#   def transform(self, x):


# Path: test/scaled_lda_test.py
import os
import matplotlib.pyplot as plt
import numpy as np
from absl.testing import absltest
from telluride_decoding import scaled_lda

    with self.assertRaisesRegex(
        ValueError, 'Must fit the model before transforming.'):
      lda.explained_variance_ratio()

    x_lda = lda.fit_transform(x, y)

    labels = lda.labels
    self.assertLen(labels, 2)

    # Plot the transformed data.
    plt.subplot(2, 1, 2)
    plt.plot(x_lda[y == labels[0], 0], x_lda[y == labels[0], 1], 'rx')
    plt.plot(x_lda[y == labels[1], 0], x_lda[y == labels[1], 1], 'bo')
    plt.title('Transfomed Data')

    # Make sure the transformed centers are symmetric on the first (x) axis.
    mean_vectors = [np.reshape(v, (1, -1)) for v in lda.mean_vectors]
    centers = lda.transform(np.concatenate(mean_vectors, axis=0))
    print('Transformed centers are:', centers)
    self.assertAlmostEqual(centers[0, 0], -centers[1, 0], delta=0.1)
    np.testing.assert_allclose(centers[:, 1], [0., 0.], atol=0.1)

    plt.savefig(os.path.join(os.environ.get('TMPDIR') or '/tmp',
                             'scaled_lda.png'))

    with self.assertRaisesRegex(
        TypeError, 'Inconsistent training and transform sizes'):
      lda.transform(d1[:, 0:1])

    # Now test model from saved parameters

    nlda = scaled_lda.LinearDiscriminantAnalysis()