Skip to content

Commit

Permalink
This it's related with #14
Browse files Browse the repository at this point in the history
  • Loading branch information
devmessias committed Jul 13, 2020
1 parent f287f30 commit 5761dd5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.tests/*.png
.vscode
# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
4 changes: 2 additions & 2 deletions requirements_cpu.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
numpy==1.19.0
scipy==1.5.0
numpy
scipy
tensorflow==1.15.3
6 changes: 3 additions & 3 deletions requirements_gpu.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
numpy==1.19.0
scipy==1.5.0
tensorflow-gpu==1.15.2
numpy
scipy
tensorflow-gpu==1.15.3
6 changes: 6 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
numpy
scipy
tensorflow-gpu==1.15.3
networkx
sklearn
matplotlib
4 changes: 4 additions & 0 deletions tests/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import emate
61 changes: 61 additions & 0 deletions tests/test_kpm.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5761dd5

Please sign in to comment.