# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import pandas as pd
from sklearn import preprocessing
# from sklearn.manifold import TSNE
# import matplotlib as plt
from matplotlib import pyplot
import seaborn as sns

from alignment import TrivialAlignment, CCA, ManifoldLinear#, manifold_nonlinear
from correspondence import Correspondence
from distance import SquaredL2
from neighborhood import neighbor_graph
from util import pairwise_error, Timer
# from viz import show_alignment
from warping import manifold_warping_linear

from rpy2.robjects import r
import rpy2.robjects.pandas2ri as pandas2ri

# +
file = "../../data/expr.RData"
rf = r['load'](file)

dayExpr = pandas2ri.ri2py_dataframe(r['dayExpr'])
nightExpr = pandas2ri.ri2py_dataframe(r['nightExpr'])

X = dayExpr.as_matrix()
Y = nightExpr.as_matrix()
n = 17695
d = 3

# +
X_normalized = preprocessing.normalize(X, norm='l2').T
Y_normalized = preprocessing.normalize(Y, norm='l2')[0:13, :].T

corr = Correspondence(matrix=np.eye(n))
Wx = neighbor_graph(X_normalized, k=5)
Wy = neighbor_graph(Y_normalized, k=5)
# -

lin_aligners = (
    ('no alignment', lambda: TrivialAlignment(X_normalized, Y_normalized, d)),
    ('cca', lambda: CCA(X_normalized, Y_normalized, corr, d)),
    ('linear manifold', lambda: ManifoldLinear(X_normalized, Y_normalized, corr, d, Wx, Wy)),
#     ('manifold warping', lambda: manifold_warping_linear(X_normalized, Y_normalized, d, Wx, Wy)[1]),
)

disMat = np.empty((0,313113025), float)
metric = SquaredL2

for name, aln in lin_aligners:
    with Timer(name):
        Xnew, Ynew = aln().project(X_normalized, Y_normalized)
    print (' sum sq. error =', pairwise_error(Xnew, Ynew, metric=SquaredL2))
    disMat = np.vstack((disMat, metric.between(Xnew/Xnew.max(), Ynew/Ynew.max()).flatten()))

sns.distplot(disMat[0], hist=False, kde_kws=dict(cumulative=True))
sns.distplot(disMat[1], hist=False, kde_kws=dict(cumulative=True))
sns.distplot(disMat[2], hist=False, kde_kws=dict(cumulative=True))

sns_plot = sns.distplot(disMat[0], hist=False, kde_kws=dict(cumulative=True))
sns_plot = sns.distplot(disMat[1], hist=False, kde_kws=dict(cumulative=True))
sns_plot.savefig("output.png")

sns.distplot(disMat[0], kde_kws=dict(cumulative=True))

from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('../../figs/cdf.pdf')
pyplot.ion()

sns.distplot(disMat[0], kde_kws=dict(cumulative=True))
pyplot.draw()
pp.savefig()

for name, aln in lin_aligners:
    pyplot.figure()
    with Timer(name):
        Xnew, Ynew = aln().project(X_normalized, Y_normalized)
    print (' sum sq. error =', pairwise_error(Xnew, Ynew, metric=SquaredL2))
    show_alignment(Xnew, Ynew, 'day', 'night', name)
    pyplot.draw()

X_embedded = TSNE(n_components=3, perplexity=100).fit_transform(X_normalized)
Y_embedded = TSNE(n_components=3, perplexity=100).fit_transform(Y_normalized)
show_alignment(X_embedded, Y_embedded, 'day', 'night', name)

X_embedded_ = TSNE(n_components=3, perplexity=100).fit_transform(Xnew)
Y_embedded_ = TSNE(n_components=3, perplexity=100).fit_transform(Ynew)
show_alignment(X_embedded_, Y_embedded_, 'day', 'night', name)

disMat = np.empty((0,313113025), float)

disMat = np.vstack((disMat, metric.between(Xnew/Xnew.max(), Ynew/Ynew.max()).flatten()))

disMat3 = disMat
# %store disMat3
del disMat3

x = np.random.randn(200)
y = np.random.randn(100)
sns.distplot(x,
             hist=False,
             kde_kws=dict(cumulative=True))
sns.distplot(y,
             hist=False,
             kde_kws=dict(cumulative=True))

disMat.shape

from sklearn.cross_decomposition import CCA
cca = CCA(n_components=3)
cca.fit(X_normalized, Y_normalized)
X_c, Y_c = cca.transform(X_normalized, Y_normalized)

pairwise_error(X_c, Y_c, metric=SquaredL2)

from alignment import manifold_nonlinear
from warping import manifold_warping_nonlinear, manifold_warping_twostep

other_aligners = (
    ('nonlinear manifold aln', lambda: manifold_nonlinear(X_normalized, Y_normalized, corr, d, Wx, Wy)),
    ('nonlinear manifold warp', lambda: manifold_warping_nonlinear(X_normalized, Y_normalized, d, Wx, Wy)[1:]),
    ('manifold warping two-step', lambda: manifold_warping_twostep(X_normalized, Y_normalized, d, Wx, Wy)[1:]),
)

for name, aln in other_aligners:
    pyplot.figure()
    with Timer(name):
        Xnew, Ynew = aln()
    print (' sum sq. error =', pairwise_error(Xnew, Ynew, metric=SquaredL2))
    show_alignment(Xnew, Ynew, 'day', 'night', name)

Xnew



StarcoderdataJupyterScriptsDedupFiltered