From 5761dd5d3e3c06c79f460c4cd37499ab0544c729 Mon Sep 17 00:00:00 2001 From: devmessias Date: Mon, 13 Jul 2020 08:46:55 -0300 Subject: [PATCH] This it's related with #14 --- .gitignore | 1 + requirements_cpu.txt | 4 +-- requirements_gpu.txt | 6 ++--- requirements_test.txt | 6 +++++ tests/context.py | 4 +++ tests/test_kpm.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 requirements_test.txt create mode 100644 tests/context.py create mode 100644 tests/test_kpm.py diff --git a/.gitignore b/.gitignore index a186522..1f8369f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.tests/*.png .vscode # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/requirements_cpu.txt b/requirements_cpu.txt index 0f5540f..3e36fc6 100644 --- a/requirements_cpu.txt +++ b/requirements_cpu.txt @@ -1,3 +1,3 @@ -numpy==1.19.0 -scipy==1.5.0 +numpy +scipy tensorflow==1.15.3 diff --git a/requirements_gpu.txt b/requirements_gpu.txt index 070ca98..8f704c0 100644 --- a/requirements_gpu.txt +++ b/requirements_gpu.txt @@ -1,3 +1,3 @@ -numpy==1.19.0 -scipy==1.5.0 -tensorflow-gpu==1.15.2 +numpy +scipy +tensorflow-gpu==1.15.3 diff --git a/requirements_test.txt b/requirements_test.txt new file mode 100644 index 0000000..53d2993 --- /dev/null +++ b/requirements_test.txt @@ -0,0 +1,6 @@ +numpy +scipy +tensorflow-gpu==1.15.3 +networkx +sklearn +matplotlib \ No newline at end of file diff --git a/tests/context.py b/tests/context.py new file mode 100644 index 0000000..0e1b323 --- /dev/null +++ b/tests/context.py @@ -0,0 +1,4 @@ +import os +import sys +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +import emate \ No newline at end of file diff --git a/tests/test_kpm.py b/tests/test_kpm.py new file mode 100644 index 0000000..0e6ca7d --- /dev/null +++ b/tests/test_kpm.py @@ -0,0 +1,61 @@ +from sklearn.neighbors import KernelDensity +from scipy import integrate +import networkx as nx +import numpy as np +import matplotlib.pyplot as plt + +from .context import emate + +try: + import cupy as cp + textCupyImplementation = True +except: + textCupyImplementation = False + + +def calc_cupykpm(W, vals, kde): + num_moments = 140 + num_vecs = 140 + extra_points = 25 + ek, rho = emate.hermitian.cupykpm( + W.tocsr().astype("complex64"), num_moments, num_vecs, extra_points) + + print("Saving the cupyKPM plot..") + plt.hist(vals, density=True, bins=100, alpha=.9, color="steelblue") + plt.scatter(ek.get(), rho.get(), c="tomato", zorder=999, alpha=0.9, marker="d") + plt.savefig("tests/test_kpm_cupy.png", filetype="png") + + log_dens = kde.score_samples(ek.get()[:, np.newaxis]) + + return integrate.simps(ek.get(), np.abs(rho.get()-np.exp(log_dens))) < 0.01 + +def calc_tfkpm(W, vals, kde): + num_moments = 40 + num_vecs = 40 + extra_points = 25 + ek, rho = emate.hermitian.cupykpm( + W.tocsr().astype("complex64"), num_moments, num_vecs, extra_points, device="cpu:0") + + print("Saving the tfKPM plot..") + plt.hist(vals, density=True, bins=100, alpha=.9, color="steelblue") + plt.scatter(ek, rho, c="tomato", zorder=999, alpha=0.9, marker="d") + plt.savefig("tests/test_kpm_tf.png", filetype="png") + + log_dens = kde.score_samples(ek[:, np.newaxis]) + + return integrate.simps(ek, np.abs(rho-np.exp(log_dens))) < 0.01 + +def test_kpm(): + n = 1000 + g = nx.erdos_renyi_graph(n , 3/n) + W = nx.adjacency_matrix(g) + + vals = np.linalg.eigvals(W.todense()).real + + kde = KernelDensity(kernel='gaussian', bandwidth=0.1).fit(vals[:, np.newaxis]) + + assert calc_tfkpm(W, vals, kde) + + if textCupyImplementation: + assert calc_cupykpm(W, vals, kde) + \ No newline at end of file