Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_projection_without_pipeline(self):
# accomodate scaling, values are in (0,1), but will be scaled slightly
atol = 0.1
mapper = KeplerMapper(verbose=1)
data = np.random.rand(100, 5)
lens = mapper.project(data, projection=[0, 1])
np.testing.assert_allclose(lens, data[:, :2], atol=atol)
lens = mapper.project(data, projection=[0])
np.testing.assert_allclose(lens, data[:, :1], atol=atol)
def test_lens_size(self):
mapper = KeplerMapper()
data = np.random.rand(100, 10)
lens = mapper.fit_transform(data)
assert lens.shape[0] == data.shape[0]
def test_project_sklearn_class(self):
mapper = KeplerMapper()
data = np.random.rand(100, 5)
lens = mapper.project(data, projection=PCA(n_components=1), scaler=None)
pca = PCA(n_components=1)
lens_confirm = pca.fit_transform(data)
assert lens.shape == (100, 1)
np.testing.assert_array_equal(lens, lens_confirm)
def mapper():
mapper = km.KeplerMapper(verbose=0)
data = np.random.rand(100, 2)
graph = mapper.map(data)
return graph
def test_members_from_id(self):
mapper = KeplerMapper(verbose=1)
data = np.random.rand(100, 2)
ids = np.random.choice(10, 100)
data[ids] = 2
graph = mapper.map(data)
graph["nodes"]["new node"] = ids
mems = mapper.data_from_cluster_id("new node", graph, data)
np.testing.assert_array_equal(data[ids], mems)
def profile():
num_sets = 100
blob_size = 1000
nr_cubes = 10
overlap = 0.2
blob_list = []
for i in range(num_sets):
data, _ = datasets.make_blobs(blob_size)
blob_list.append(data)
mapper = KeplerMapper(verbose=0)
pr = cProfile.Profile()
pr.enable()
for data in blob_list:
lens = mapper.fit_transform(data)
graph = mapper.map(lens, data, nr_cubes=nr_cubes, overlap_perc=overlap)
pr.disable()
s = io.StringIO()
sortby = "cumulative"
ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats(sortby)
ps.print_stats("kmapper")
print(
"Ran {} blobs of size {} with params (nr_cubes:{}\toverlap:{})".format(
num_sets, blob_size, nr_cubes, overlap
def test_distance_matrix(self):
# todo, test other distance_matrix functions
mapper = KeplerMapper(verbose=4)
X = np.random.rand(100, 10)
lens = mapper.fit_transform(X, distance_matrix="euclidean")
X_pdist = distance.squareform(distance.pdist(X, metric="euclidean"))
lens2 = mapper.fit_transform(X_pdist)
np.testing.assert_array_equal(lens, lens2)
from dyneusr import DyNeuGraph
from dyneusr.datasets import make_trefoil
from kmapper import KeplerMapper
# Generate synthetic dataset
dataset = make_trefoil(size=100)
X = dataset.data
y = dataset.target
# Generate shape graph using KeplerMapper
mapper = KeplerMapper(verbose=1)
lens = mapper.fit_transform(X, projection=[0])
graph = mapper.map(lens, X, nr_cubes=6, overlap_perc=0.2)
# Visualize the shape graph using DyNeuSR's DyNeuGraph
dG = DyNeuGraph(G=graph, y=y)
dG.visualize('dyneusr4D_trefoil_knot.html', template='4D', static=True, show=True)
# Create images for a custom tooltip array
tooltip_s = []
for image_data in data:
output = io.BytesIO()
img = toimage(image_data.reshape((8, 8))) # Data was a flat row of 64 "pixels".
img.save(output, format="PNG")
contents = output.getvalue()
img_encoded = base64.b64encode(contents)
img_tag = """<img src="data:image/png;base64,{}">""".format(img_encoded.decode('utf-8'))
tooltip_s.append(img_tag)
output.close()
tooltip_s = np.array(tooltip_s) # need to make sure to feed it as a NumPy array, not a list
# Initialize to use t-SNE with 2 components (reduces data to 2 dimensions). Also note high overlap_percentage.
mapper = km.KeplerMapper(verbose=2)
# Fit and transform data
projected_data = mapper.fit_transform(data,
projection=sklearn.manifold.TSNE())
# Create the graph (we cluster on the projected data and suffer projection loss)
graph = mapper.map(projected_data,
clusterer=sklearn.cluster.DBSCAN(eps=0.3, min_samples=15),
cover=km.Cover(35, 0.4))
# Create the visualizations (increased the graph_gravity for a tighter graph-look.)
print("Output graph examples to html" )
# Tooltips with image data for every cluster member
mapper.visualize(graph,
title="Handwritten digits Mapper",
path_html="output/digits_custom_tooltips.html",
from dyneusr import DyNeuGraph
from dyneusr.datasets import make_trefoil
from kmapper import KeplerMapper
from sklearn.decomposition import PCA
# Generate synthetic dataset
import tadasets
X = tadasets.sphere(n=500, r=1)
# Sort by first column
inds = np.argsort(X[:, 0])
X = X[inds].copy()
y = np.arange(X.shape[0])
# Generate shape graph using KeplerMapper
mapper = KeplerMapper(verbose=1)
lens = mapper.fit_transform(X, projection=PCA(2))
graph = mapper.map(lens, X, nr_cubes=6, overlap_perc=0.5)
# Visualize the shape graph using DyNeuSR's DyNeuGraph
dG = DyNeuGraph(G=graph, y=y)
dG.visualize('dyneusr3D_sphere.html', template='3D', static=True, show=True)