### Install KeplerMapper from source Source: https://github.com/scikit-tda/kepler-mapper/blob/master/README.md This snippet details the process of installing KeplerMapper directly from its GitHub repository. It involves cloning the repository, navigating into the directory, and then performing an editable installation. ```bash git clone https://github.com/MLWave/kepler-mapper cd kepler-mapper pip install -e . ``` -------------------------------- ### Install KeplerMapper using pip Source: https://github.com/scikit-tda/kepler-mapper/wiki/Documentation Provides instructions for installing the KeplerMapper library using pip, the standard Python package installer. It also includes commands for installing from source. ```Bash pip install kmapper ``` ```Bash git clone https://github.com/MLWave/kepler-mapper cd kepler-mapper pip install -e . ``` -------------------------------- ### Initialize Kepler Mapper and Import Utilities Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/KeplerMapper-usage-in-Jupyter-Notebook.ipynb Imports the necessary KeplerMapper library and the jupyter utility for enhancing the notebook display. This is the initial setup for using KeplerMapper. ```python import kmapper as km from kmapper import jupyter # Creates custom CSS full-size Jupyter screen ``` -------------------------------- ### Install KeplerMapper using pip Source: https://github.com/scikit-tda/kepler-mapper/blob/master/README.md This snippet shows how to install the KeplerMapper library using pip, the standard package installer for Python. It also mentions optional dependencies for enhanced visualization capabilities. ```bash pip install kmapper ``` -------------------------------- ### Install NetworkX Dependency Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/Adapters.ipynb Command to install the required networkx library for graph manipulation. ```bash pip install networkx ``` -------------------------------- ### Initialize and Start Graph Simulation (JavaScript) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/breast-cancer-multiple-color-functions-and-multiple-node-color-functions.html Sets up and restarts the D3.js force simulation for graph rendering. It initializes nodes and links, draws them, and applies forces. Handles shallow copying of data to allow simulation restarts. ```javascript function start() { nodes = graph.nodes.map(n => Object.assign({}, n)); links = graph.links.map(l => Object.assign({}, l)); link = link .data(links) .join("line") .attr("class", "link") .style("stroke-width", function(d) { return d.w * nominal_stroke; }) .style("stroke-width", function(d) { return d.w * nominal_stroke; }); node = node .data(nodes, d => d.name) .join(enter => enter.append("g") .attr("class", "node") .attr("id", function(d){ return "node-" + d.name }) .append("path") .attr("d", draw_circle_size) .attr("class", "circle") .on("mouseover.focus", node_mouseover) .on("mouseout.focus", node_mouseout) .on('mousedown.focus', node_mousedown) .on("dblclick.freeze", (e, d) => unfreeze_node(d)) .on('click.zoom', node_click) .on('center_viewport', center_on_node) .call(drag)); simulation.nodes(nodes); simulation.force('link').links(links); simulation.alpha(1).restart() reset_color_functions() } init(); start(); ``` -------------------------------- ### Import Kepler-Mapper and Plotly Visualization Modules Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/digits/digits.ipynb Imports the core Kepler-Mapper library along with specific visualization tools from `kmapper.plotlyviz`. This setup is necessary for creating and displaying topological maps. ```python import kmapper as km from kmapper.plotlyviz import plotlyviz from kmapper.plotlyviz import * import plotly.graph_objs as go import ipywidgets as ipw ``` -------------------------------- ### Initialize SVG and Force Simulation Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/digits_ylabel_tooltips.html Performs one-time setup for the visualization, including creating the SVG canvas, appending a group element, initializing D3 selections for links and nodes, and setting up the force simulation with various forces (charge, center, link, x, y). It also defines drag behavior for nodes. ```javascript function init() { zoom = d3.zoom() .scaleExtent([min_zoom, max_zoom]) .on('zoom', zoomed) // We draw the graph in SVG svg = d3.select("#canvas svg") .attr("width", width) .attr("height", height) .style("cursor","move") .call(zoom) .on('dblclick.zoom', null); // prevent default zoom-in on dblclick svg.on('click.focus', function(e){ set_focus_via_click(null); }); g = svg.append("g") link = g.selectAll(".link") node = g.selectAll(".node") text = g.selectAll(".text") simulation = d3.forceSimulation() .force('charge', d3.forceManyBody().strength(-1200)) .force('center', d3.forceCenter(width / 2, height / 2)) .force('link', d3.forceLink().distance(5)) .force('x', d3.forceX()) // not sure what this does... .force('y', d3.forceY()) .on('tick', ticked) drag = d3.drag() .on("start", function(e, d){ svg.style('cursor','grabbing'); if (!e.active) { simulation.alphaTarget(0.3).restart() } d.fx = d.x d.fy = d.y dragging = true; }) .on('drag', function(e, d){ d.fx = e.x d.fy = e.y }) .on('end', function(e, d){ if (!e.active) { simulation.alphaTarget(0) } dragging = false; }); resize(); d3.select(window).on("resize", resize); d3.select(window).on("mouseup.focus", function(e){ if (focus_node == null) { set_cursor('move'); } else { set_cursor('pointer'); } }); } ``` -------------------------------- ### Import Kepler-Mapper and Plotly Libraries Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/makecircles/make-circles-with-plotly.ipynb Imports necessary libraries for Kepler-Mapper, scikit-learn for data generation, and Plotly for visualization. This setup is crucial for subsequent graph creation and display. ```python import kmapper as km import sklearn from sklearn import datasets #for Plotly visualization import: from kmapper.plotlyviz import plotlyviz from kmapper.plotlyviz import * import plotly.graph_objs as go ``` -------------------------------- ### Initialize and Start Graph Simulation (JavaScript) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/breast-cancer-multiple-node-color-functions.html Initializes and restarts the force-directed graph simulation. It sets up nodes and links, draws them using D3.js, and configures event listeners for interactions like mouseovers, clicks, and dragging. This function is crucial for rendering the initial graph and for resetting it. ```javascript function start() { /* * Force-related things */ // shallow copy to enable restarting, // because otherwise, starting the force simulation mutates the links (replaces indeces with refs) nodes = graph.nodes.map(n => Object.assign({}, n)); links = graph.links.map(l => Object.assign({}, l)); // draw links first so that they appear behind nodes link = link .data(links) .join("line") .attr("class", "link") .style("stroke-width", function(d) { return d.w * nominal_stroke; }) .style("stroke-width", function(d) { return d.w * nominal_stroke; }); node = node .data(nodes, d => d.name) .join(enter => enter.append("g") .attr("class", "node") .attr("id", function(d){ return "node-" + d.name }) // append circles... .append("path") .attr("d", draw_circle_size ) .attr("class", "circle") .on("mouseover.focus", node_mouseover) .on("mouseout.focus", node_mouseout) .on('mousedown.focus', node_mousedown) .on("dblclick.freeze", (e, d) => unfreeze_node(d) ) .on('click.zoom', node_click) .on('center_viewport', center_on_node) .call(drag)); simulation.nodes(nodes); simulation.force('link').links(links); simulation.alpha(1).restart() reset_color_functions() } init(); start(); ``` -------------------------------- ### JavaScript Data Loading and Visualization Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/kmapper/templates/base.html This snippet demonstrates how to load and prepare data for KeplerMapper visualizations using JavaScript. It assumes the data (graph, colorscale, summary, histogram) is provided in JSON format and utilizes d3.js for rendering. The code initializes variables to hold the mapped data, color scale, and summary statistics. ```javascript const graph = {{ mapper_data|tojson|safe }}; const colorscale = {{ colorscale|tojson|safe }}; const summary = {{ summary_json|tojson|safe }}; const summary_histogram = {{ histogram|tojson|safe }}; // Requires JavaScript (d3.js) for visualizations ``` -------------------------------- ### Initialize and Start D3 Force Simulation Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/breast-cancer.html Initializes the force simulation with nodes and links, sets up the SVG rendering for nodes and links, and attaches event listeners for interaction. It ensures the simulation is restarted upon initialization or restart calls. ```javascript function start() { nodes = graph.nodes.map(n => Object.assign({}, n)); links = graph.links.map(l => Object.assign({}, l)); link = link.data(links).join("line").attr("class", "link").style("stroke-width", d => d.w * nominal_stroke); node = node.data(nodes, d => d.name).join(enter => enter.append("g").attr("class", "node").append("path").attr("d", draw_circle_size).on("mouseover.focus", node_mouseover).call(drag)); simulation.nodes(nodes); simulation.force('link').links(links); simulation.alpha(1).restart(); } ``` -------------------------------- ### JavaScript: UI and Canvas Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/output/make_circles_keplermapper_output.html Initializes canvas dimensions, UI element heights, and global variables for graph visualization. It sets up D3.js scales for node sizing and defines default colors and sizes for graph elements. ```javascript var page_height = window.innerHeight - 5; var header_height = document.getElementById('header').offsetHeight; var canvas_height = page_height - header_height; document.getElementById("canvas").style.height = canvas_height + "px"; var width = document.getElementById("canvas").offsetWidth; var height = document.getElementById("canvas").offsetHeight; var w = width; var h = height; var padding = 40; var focus_node_id = null; var focus_node = null; var text_center = false; var outline = false; var size = d3.scalePow().exponent(1) .domain([1,100]) .range([8,24]); var default_node_color = "#ccc"; var default_node_color = "rgba(160,160,160, 0.5)"; var default_link_color = "rgba(160,160,160, 0.5)"; var nominal_base_node_size = 8; var nominal_text_size = 15; var max_text_size = 24; var nominal_stroke = 1.0; var max_stroke = 4.5; var max_base_node_size = 36; var min_zoom = 0.1; var max_zoom = 7; var zoom; var svg, g; var simulation; var link, node; var drag; var dragging = false; var circle; var text; var focus_via_click = false; var nodes = []; var links = []; var tocolor = "fill"; var towhite = "stroke"; if (outline) { tocolor = "stroke"; towhite = "fill"; } ``` -------------------------------- ### Initialize Force-Directed Graph Layout Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/lion/lion_keplermapper_output.html Parses graph data (nodes and links) from the DOM and initializes a D3.js force-directed layout. It configures physics properties like link distance, gravity, and charge, then starts the simulation. ```javascript var graph = JSON.parse(document.getElementById("json_graph").dataset.graph); var force = d3.layout.force() .linkDistance(5) .gravity(0.2) .charge(-1200) .size([w,h]); force .nodes(graph.nodes) .links(graph.links) .start(); var link = g.selectAll(".link") .data(graph.links) .enter().append("line") .attr("class", "link") .style("stroke-width", function(d) { return d.w * nominal_stroke; }) .style("stroke-width", function(d) { return d.w * nominal_stroke; }); var node = g.selectAll(".node") .data(graph.nodes) .enter().append("g") .attr("class", "node") .call(force.drag); node.on("dblclick.zoom", function(d) { d3.event.stopPropagation(); var dcx = (window.innerWidth/2-d.x*zoom.scale()); var dcy = (window.innerHeight ``` -------------------------------- ### JavaScript Graph Visualization Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/breast-cancer/breast-cancer.html Initializes D3.js force-directed graph layout, including canvas dimensions, zoom behavior, color scales, and node/link data loading. It sets up SVG elements for rendering the graph and defines drag behavior for nodes. ```javascript var canvas_height = window.innerHeight - 5; document.getElementById("canvas").style.height = canvas_height + "px"; var width = document.getElementById("canvas").offsetWidth; var height = document.getElementById("canvas").offsetHeight; var w = width; var h = height; var padding = 40; var focus_node = null, highlight_node = null; var text_center = false; var outline = false; var size = d3.scale.pow().exponent(1) .domain([1,100]) .range([8,24]); var palette = [ '#0500ff', '#0300ff', '#0100ff', '#0002ff', '#0022ff', '#0044ff', '#0064ff', '#0084ff', '#00a4ff', '#00a4ff', '#00c4ff', '#00e4ff', '#00ffd0', '#00ff83', '#00ff36', '#17ff00', '#65ff00', '#b0ff00', '#fdff00', '#FFf000', '#FFdc00', '#FFc800', '#FFb400', '#FFa000', '#FF8c00', '#FF7800', '#FF6400', '#FF5000', '#FF3c00', '#FF2800', '#FF1400', '#FF0000' ]; var highlight_color = "blue"; var highlight_trans = 0.1; var default_node_color = "#ccc"; var default_node_color = "rgba(160,160,160, 0.5)"; var default_link_color = "rgba(160,160,160, 0.5)"; var nominal_base_node_size = 8; var nominal_text_size = 15; var max_text_size = 24; var nominal_stroke = 1.0; var max_stroke = 4.5; var max_base_node_size = 36; var min_zoom = 0.1; var max_zoom = 7; var zoom = d3.behavior.zoom().scaleExtent([min_zoom,max_zoom]); var tocolor = "fill"; var towhite = "stroke"; if (outline) { tocolor = "stroke"; towhite = "fill"; } var svg = d3.select("#canvas").append("svg") .attr("width", width) .attr("height", height); svg.style("cursor","move"); var g = svg.append("g"); var graph = JSON.parse(document.getElementById("json_graph").dataset.graph); var force = d3.layout.force() .linkDistance(5) .gravity(0.2) .charge(-1200) .size([w,h]); force .nodes(graph.nodes) .links(graph.links) .start(); var link = g.selectAll(".link") .data(graph.links) .enter().append("line") .attr("class", "link") .style("stroke-width", function(d) { return d.w * nominal_stroke; }); var node = g.selectAll(".node") .data(graph.nodes) .enter().append("g") .attr("class", "node") .call(force.drag); node.on("dblclick.zoom", function(d) { d3.event.stopPropagation(); var dcx = (window.innerWidth/2-d. ``` -------------------------------- ### Data Generation Example (Python) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/makecircles/keplermapper-makecircles-xaxis.html This code snippet demonstrates how to generate a sample dataset using scikit-learn's make_circles function, which is often used as input for topological data analysis tools like Kepler Mapper. It specifies the number of samples, noise level, and a factor to control the separation of circles. ```python from sklearn.datasets import make_circles X, y = make_circles(n_samples=5000, noise=0.05, factor=0.3) ``` -------------------------------- ### JavaScript UI and Visualization Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/breast-cancer.html Initializes D3.js visualization settings, including canvas dimensions, zoom behavior, force simulation, and drag functionality. It also sets up event listeners for UI controls like pane toggling and color function selection. ```javascript var page_height = window.innerHeight - 5; var header_height = document.getElementById('header').offsetHeight; var canvas_height = page_height - header_height; document.getElementById("canvas").style.height = canvas_height + "px"; var width = document.getElementById("canvas").offsetWidth; var height = document.getElementById("canvas").offsetHeight; var w = width; var h = height; var padding = 40; var focus_node_id = null; var focus_node = null; var text_center = false; var outline = false; // Size for zooming var size = d3.scalePow().exponent(1) .domain([1,100]) .range([8,24]); // Variety of variable inits var default_node_color = "#ccc"; var default_node_color = "rgba(160,160,160, 0.5)"; var default_link_color = "rgba(160,160,160, 0.5)"; var nominal_base_node_size = 8; var nominal_text_size = 15; var max_text_size = 24; var nominal_stroke = 1.0; var max_stroke = 4.5; var max_base_node_size = 36; var min_zoom = 0.1; var max_zoom = 7; var zoom; var svg, g; var simulation; var link, node; var drag; var dragging = false; var circle; var text; var focus_via_click = false; var nodes = []; var links = []; var tocolor = "fill"; var towhite = "stroke"; if (outline) { tocolor = "stroke"; towhite = "fill"; } /** * Side panes */ // Show/Hide Functionality function toggle_pane(content, content_id, tag) { var active = content.active ? false : true; if (active) { content_id.style("display", "block"); tag.node().textContent = "[-"; } else { content_id.style("display", "none"); tag.node().textContent = "[+"; } // TODO: This is probably not the best way to find the correct height. var h = canvas_height - content.offsetTop - padding; content_id.style("height", h + "px") content.active = active; } d3.select("#tooltip_control").on("click", function(e) { toggle_pane(tooltip_content, d3.select("#tooltip_content"), d3.select("#tooltip_tag")) }); d3.select("#meta_control").on("click", function(e) { toggle_pane(meta_content, d3.select("#meta_content"), d3.select("#meta_tag")) }); d3.select("#help_control").on("click", function(e) { toggle_pane(helptip_content, d3.select("#helptip_content"), d3.select("#helptip_tag")) }); d3.select('#select-color-function').on('input', function(e){ color_function_index = parseInt(e.target.value); update_color_functions() }) d3.select('#select-node-color-function').on('input', function(e){ node_color_function_index = parseInt(e.target.value); update_color_functions() }) /** * * Set up color scale * */ // var colorscale defined in base.html var domain = colorscale.map((x)=>x[0]) var palette = colorscale.map((x)=>x[1]) var color = d3.scaleLinear() .domain(domain) .range(palette); /* * one-time setups, like SVG and force init */ function init() { zoom = d3.zoom() .scaleExtent([min_zoom, max_zoom]) .on('zoom', zoomed) // We draw the graph in SVG svg = d3.select("#canvas svg") .attr("width", width) .attr("height", height) .style("cursor","move") .call(zoom) .on('dblclick.zoom', null); // prevent default zoom-in on dblclick svg.on('click.focus', function(e){ set_focus_via_click(null); }); g = svg.append("g") link = g.selectAll(".link") node = g.selectAll(".node") text = g.selectAll(".text") simulation = d3.forceSimulation() .force('charge', d3.forceManyBody().strength(-1200)) .force('center', d3.forceCenter(width / 2, height / 2)) .force('link', d3.forceLink().distance(5)) .force('x', d3.forceX()) // not sure what this does... .force('y', d3.forceY()) .on('tick', ticked) drag = d3.drag() .on("start", function(e, d){ svg.style('cursor','grabbing'); if (!e.active) { simulation.alphaTarget(0.3).restart() } d.fx = d.x d.fy = d.y dragging = true; }) .on('drag', function(e, d){ d.fx = e.x d.fy = e.y }) .on('end', function(e, d){ if (!e.active) { simulation.alphaTarget(0) } dragging = false; }); resize(); d3.select(window).on("resize", resize); d3.select(window).on("mouseup.focus", function(e){ if (focus_node == null) { set_cursor('move'); } else { set_cursor('pointer'); } }); } function set_histogram(selection, data){ selection.selectAll('.bin') .data(data) .join( enter => enter.append('div') .attr('class', 'bin') .call(enter => enter.append('div') .text(d => d.perc + '%')) , update => update .call(update => update.select('div') .text(d => d.perc + '%')) ) .style('height', (d) => (d.height || 1) + 'px') .style('background', (d) => d.color); } var color_function_index = 0; var node_color_function_index = 0; function reset_color_functions(){ color_function_index = 0; node_color_function_index = 0; update_color_functions() } function update_color_functions(){ // update_meta_content_histogram set_histogram(d3.select('#meta_content .histogram'), summary_histogram[node_color_function_index][color_function_index]) // update node colors node.style(tocolor, function(d) { return color(d.color[node_color_function_in ``` -------------------------------- ### Initialize and Start D3 Force Simulation Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/digits_ylabel_tooltips.html Initializes the D3 force simulation, binds node and link data, and sets up event listeners for user interactions like hover, click, and drag. It ensures the simulation is properly restarted when the graph structure changes. ```javascript function start() { nodes = graph.nodes.map(n => Object.assign({}, n)); links = graph.links.map(l => Object.assign({}, l)); link = link.data(links).join("line").attr("class", "link"); node = node.data(nodes, d => d.name).join(enter => enter.append("g").attr("class", "node").append("path").attr("d", draw_circle_size)); simulation.nodes(nodes); simulation.force('link').links(links); simulation.alpha(1).restart(); } ``` -------------------------------- ### Controlling Edge Creation with GraphNerve Source: https://context7.com/scikit-tda/kepler-mapper/llms.txt This example demonstrates how to control the creation of edges between nodes in a Kepler-Mapper graph using the `GraphNerve` class. It shows how to set a minimum number of shared members required for an edge to be formed, comparing the results of default and stricter settings. ```python import kmapper as km from sklearn import datasets data, _ = datasets.make_moons(n_samples=1000, noise=0.1) mapper = km.KeplerMapper(verbose=1) lens = mapper.fit_transform(data) # Default nerve (edges when any shared members) nerve_default = km.GraphNerve(min_intersection=1) # Stricter nerve (edges only when 3+ shared members) nerve_strict = km.GraphNerve(min_intersection=3) # Compare graph structures graph_default = mapper.map(lens, data, nerve=nerve_default) graph_strict = mapper.map(lens, data, nerve=nerve_strict) print(f"Default edges: {sum(len(v) for v in graph_default['links'].values())}") print(f"Strict edges: {sum(len(v) for v in graph_strict['links'].values())}") ``` -------------------------------- ### JavaScript D3.js Graph Visualization Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/horse/horse_keplermapper_output.html Initializes D3.js for graph visualization, setting up canvas dimensions, zoom behavior, and defining various visual parameters like node size, text size, and link stroke width. It prepares the SVG canvas and group element for drawing the graph. ```javascript var canvas_height = window.innerHeight - 5; document.getElementById("canvas").style.height = canvas_height + "px"; var width = document.getElementById("canvas").offsetWidth; var height = document.getElementById("canvas").offsetHeight; var w = width; var h = height; var padding = 40; var focus_node = null, highlight_node = null; var text_center = false; var outline = false; var size = d3.scale.pow().exponent(1) .domain([1,100]) .range([8,24]); var highlight_color = "blue"; var highlight_trans = 0.1; var default_node_color = "#ccc"; var default_node_color = "rgba(160,160,160, 0.5)"; var default_link_color = "rgba(160,160,160, 0.5)"; var nominal_base_node_size = 8; var nominal_text_size = 15; var max_text_size = 24; var nominal_stroke = 1.0; var max_stroke = 4.5; var max_base_node_size = 36; var min_zoom = 0.1; var max_zoom = 7; var zoom = d3.behavior.zoom().scaleExtent([min_zoom,max_zoom]); var tocolor = "fill"; var towhite = "stroke"; if (outline) { tocolor = "stroke"; towhite = "fill"; } var svg = d3.select("#canvas").append("svg") .attr("width", width) .attr("height", height); svg.style("cursor","move"); var g = svg.append("g"); ``` -------------------------------- ### Initialize Visualization Settings and Variables Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/digits_ylabel_tooltips.html Sets up initial configurations for the visualization, including canvas dimensions, node and link styling, zoom behavior, and D3 force simulation parameters. It also initializes D3 selections for SVG elements and drag behavior. ```javascript var page_height = window.innerHeight - 5; var header_height = document.getElementById('header').offsetHeight; var canvas_height = page_height - header_height; document.getElementById("canvas").style.height = canvas_height + "px"; var width = document.getElementById("canvas").offsetWidth; var height = document.getElementById("canvas").offsetHeight; var w = width; var h = height; var padding = 40; var focus_node_id = null; var focus_node = null; var text_center = false; var outline = false; // Size for zooming var size = d3.scalePow().exponent(1) .domain([1,100]) .range([8,24]); // Variety of variable inits var default_node_color = "#ccc"; var default_node_color = "rgba(160,160,160, 0.5)"; var default_link_color = "rgba(160,160,160, 0.5)"; var nominal_base_node_size = 8; var nominal_text_size = 15; var max_text_size = 24; var nominal_stroke = 1.0; var max_stroke = 4.5; var max_base_node_size = 36; var min_zoom = 0.1; var max_zoom = 7; var zoom; var svg, g; var simulation; var link, node; var drag; var dragging = false; var circle; var text; var focus_via_click = false; var nodes = []; var links = []; var tocolor = "fill"; var towhite = "stroke"; if (outline) { tocolor = "stroke"; towhite = "fill"; } ``` -------------------------------- ### Initialize KeplerMapper and Generate Graph Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/Adapters.ipynb Imports necessary libraries, generates synthetic circle data, and creates a Mapper graph using KeplerMapper. ```python import kmapper from sklearn import datasets import networkx as nx data = datasets.make_circles(n_samples=1000)[0] km = kmapper.KeplerMapper() lens = km.project(data) graph = km.map(X=data, lens=lens) ``` -------------------------------- ### Generate Data, Initialize, Project, Map, and Visualize with Kepler Mapper Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/KeplerMapper-usage-in-Jupyter-Notebook.ipynb This snippet covers the core functionality: generating sample data using scikit-learn, initializing KeplerMapper, projecting the data onto a 2D plane, mapping the data to create a graph, and finally visualizing the graph as an HTML file. It also shows how to display the visualization within a Jupyter Notebook. ```python # Some sample data from sklearn import datasets data, labels = datasets.make_circles(n_samples=5000, noise=0.03, factor=0.3) # Initialize mapper = km.KeplerMapper(verbose=1) # Fit to and transform the data projected_data = mapper.fit_transform(data, projection=[0,1]) # X-Y axis # Create a cover with 10 elements cover = km.Cover(n_cubes=10) # Create dictionary called 'graph' with nodes, edges and meta-information graph = mapper.map(projected_data, data, cover=cover) # Visualize it _ = mapper.visualize(graph, path_html="output/make_circles_keplermapper_output.html", title="make_circles(n_samples=5000, noise=0.03, factor=0.3)") ## Uncomment the below to view the above-generated visualization # # jupyter.display(path_html="output/make_circles_keplermapper_output.html") ## Alternatively, use an IFrame to display a vis with a set width and height # # from IPython.display import IFrame # IFrame(src="http://mlwave.github.io/tda/word2vec-gender-bias.html", width=800, height=600) ``` -------------------------------- ### Restart Graph Simulation (JavaScript) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/breast-cancer-multiple-color-functions-and-multiple-node-color-functions.html Resets and restarts the graph simulation. It clears existing nodes and links (commented out) and calls the main 'start' function to re-initialize the graph. ```javascript function restart() { // nodes = [] // links = [] // node.remove() // link.remove() focus_via_click = false; start() } ``` -------------------------------- ### Basic KeplerMapper Workflow in Python Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/started.rst Demonstrates the fundamental workflow of KeplerMapper: initializing the mapper, projecting and transforming sample data, defining a cover, mapping the data to a simplicial complex, and visualizing the resulting graph. It uses scikit-learn's make_circles dataset for demonstration. ```python # Import the class import kmapper as km # Some sample data from sklearn import datasets data, labels = datasets.make_circles(n_samples=5000, noise=0.03, factor=0.3) # Initialize mapper = km.KeplerMapper(verbose=1) # Fit to and transform the data projected_data = mapper.fit_transform(data, projection=[0,1]) # X-Y axis # Create a cover with 10 elements cover = km.Cover(n_cubes=10) # Create dictionary called 'graph' with nodes, edges and meta-information graph = mapper.map(projected_data, data, cover=cover) # Visualize it mapper.visualize(graph, path_html="make_circles_keplermapper_output.html", title="make_circles(n_samples=5000, noise=0.03, factor=0.3)") ``` -------------------------------- ### Importing KeplerMapper and Scikit-Learn Dependencies Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/KeplerMapper-Newsgroup20-Pipeline.ipynb Initializes the environment by importing KeplerMapper, Scikit-Learn datasets, and preprocessing/decomposition modules required for the NLP pipeline. ```python import kmapper as km from kmapper import Cover, jupyter import numpy as np from sklearn.datasets import fetch_20newsgroups from sklearn.cluster import AgglomerativeClustering from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.decomposition import TruncatedSVD from sklearn.manifold import Isomap from sklearn.preprocessing import MinMaxScaler ``` -------------------------------- ### Display Kepler-Mapper TDA Visualization (Python) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/TOR-XGB-TDA.ipynb This snippet shows how to import and use the Kepler-Mapper library in Python to display a generated TDA visualization, typically an HTML file. It requires the kmapper library to be installed. ```python from kmapper import jupyter #jupyter.display("output/tor-tda.html") ``` -------------------------------- ### Initialize Kepler Mapper and Plotly Environment Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/cat/cat.ipynb Imports necessary libraries including Kepler Mapper, Plotly, and ipywidgets to set up the environment for topological data analysis and visualization. ```python import numpy as np import sklearn import kmapper as km from kmapper.plotlyviz import plotlyviz import plotly.graph_objs as go import ipywidgets as ipw ``` -------------------------------- ### JavaScript D3.js Color Scale Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/horse/horse_keplermapper_output.html Configures a D3.js linear color scale based on JSON data provided in the 'json_colorscale' element. It maps domain values to a color palette for coloring graph nodes. ```javascript var colorscale = JSON.parse(document.getElementById("json_colorscale").dataset.colorscale); var domain = colorscale.map((x)=>x[0]) var palette = colorscale.map((x)=>x[1]) var color = d3.scale.linear() .domain(domain) .range(palette); ``` -------------------------------- ### Import Libraries and Handle UMAP Dependency Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/digits/digits.ipynb Imports necessary libraries including NumPy, scikit-learn, and UMAP. It includes a check for the UMAP library and provides installation instructions if it's not found, as UMAP is required for data projection. ```python import numpy as np import sklearn from sklearn import datasets try: import umap except ImportError: print("This example requires the UMAP library. You can install it with the command `!pip install umap-learn`") import warnings warnings.filterwarnings("ignore") ``` -------------------------------- ### Data Preparation and Projection with Kepler-Mapper (Python) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/TOR-XGB-TDA.ipynb This snippet demonstrates loading and preparing data using pandas and numpy, followed by feature extraction. It then applies an Isolation Forest for projection and uses Kepler-Mapper with k-NN distance for a second projection. The two projections are combined. ```python import kmapper as km import pandas as pd import numpy as np from sklearn import ensemble, cluster df = load_and_prep_data() features = [c for c in df.columns if c not in ['Source IP', ' Source Port', ' Destination IP', ' Destination Port', ' Protocol', 'label']] X = np.array(df[features]) y = np.array(df.label) projector = ensemble.IsolationForest(random_state=0, n_jobs=-1) projector.fit(X) lens1 = projector.decision_function(X) mapper = km.KeplerMapper(verbose=3) lens2 = mapper.fit_transform(X, projection="knn_distance_5") lens = np.c_[lens1, lens2] ``` -------------------------------- ### Setup D3 Color Scale Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/digits_ylabel_tooltips.html Configures a D3 color scale based on provided 'colorscale' data, which is expected to be defined elsewhere (e.g., in 'base.html'). It extracts domain and range values to create a linear color scale. ```javascript // var colorscale defined in base.html var domain = colorscale.map((x)=>x[0]) var palette = colorscale.map((x)=>x[1]) var color = d3.scaleLinear() .domain(domain) .range(palette); ``` -------------------------------- ### Initialize KeplerMapper Class Source: https://context7.com/scikit-tda/kepler-mapper/llms.txt Demonstrates the initialization of the KeplerMapper object. The verbose parameter controls the logging level during execution. ```python import kmapper as km import numpy as np from sklearn import datasets # Initialize KeplerMapper with logging enabled mapper = km.KeplerMapper(verbose=1) # Output: KeplerMapper(verbose=1) ``` -------------------------------- ### Graph Initialization and Update (JavaScript) Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/output/make_circles_keplermapper_output.html Initializes and restarts the D3.js force-directed graph simulation. It handles node and link data binding, rendering, and simulation setup. This function is crucial for drawing the initial graph and for refreshing it after changes. ```javascript function start() { /* * Force-related things */ // shallow copy to enable restarting, // because otherwise, starting the force simulation mutates the links (replaces indeces with refs) nodes = graph.nodes.map(n => Object.assign({}, n)); links = graph.links.map(l => Object.assign({}, l)); // draw links first so that they appear behind nodes link = link .data(links) .join("line") .attr("class", "link") .style("stroke-width", function(d) { return d.w * nominal_stroke; }) .style("stroke-width", function(d) { return d.w * nominal_stroke; }); node = node .data(nodes, d => d.name) .join(enter => enter.append("g") .attr("class", "node") .attr("id", function(d){ return "node-" + d.name }) // append circles... .append("path") .attr("d", draw_circle_size ) .attr("class", "circle") .on("mouseover.focus", node_mouseover) .on("mouseout.focus", node_mouseout) .on('mousedown.focus', node_mousedown) .on("dblclick.freeze", (e, d) => unfreeze_node(d) ) .on('click.zoom', node_click) .on('center_viewport', center_on_node) .call(drag)); simulation.nodes(nodes); simulation.force('link').links(links); simulation.alpha(1).restart() reset_color_functions() } init(); start(); ``` -------------------------------- ### Initialize D3.js Force-Directed Graph Visualization Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/output/horse.html Sets up the SVG canvas, defines zoom behaviors, and initializes the force layout simulation for node-link diagrams. It includes logic for handling node dragging and double-click centering. ```javascript var width = document.getElementById("canvas").offsetWidth; var height = document.getElementById("canvas").offsetHeight; var svg = d3.select("#canvas").append("svg").attr("width", width).attr("height", height); var force = d3.layout.force().linkDistance(5).gravity(0.2).charge(-1200).size([width, height]); force.nodes(graph.nodes).links(graph.links).start(); ``` -------------------------------- ### UMAP Initialization for Kepler-Mapper Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/self-guessing.rst This snippet demonstrates how to initialize a UMAP model, which can be used as a component within the Kepler-Mapper framework. It emphasizes the use of pre-written implementations for portability and re-use. ```python import umap model = umap.UMAP() ``` -------------------------------- ### Construct Plotly Scales from Matplotlib Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/Plotly-Demo.ipynb This snippet demonstrates how to convert Matplotlib colormaps into Plotly-compatible colorscales using the `mpl_to_plotly` function from `kmapper.plotlyviz`. It shows examples using `cm.RdYlBu` and `cmocean.cm.delta`. These custom colorscales can then be applied to Kepler-Mapper visualizations. ```python import numpy as np import matplotlib.cm as cm import cmocean # https://matplotlib.org/cmocean/ from kmapper.plotlyviz import mpl_to_plotly plotly_RdYlBu = mpl_to_plotly(cm.RdYlBu, 11) plotly_delta = mpl_to_plotly(cmocean.cm.delta, 11) ``` -------------------------------- ### JavaScript D3.js Color Scale Setup Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/breast-cancer/breast-cancer.html Defines an ordinal color scale using a predefined palette for mapping data values (0-30) to colors. This scale is used for coloring graph elements based on their associated scores or values. ```javascript var color = d3.scale.ordinal() .domain(["0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13","14","15","16","17","18","19","20", "21","22","23","24","25","26","27","28","29","30"]) .range(palette); ``` -------------------------------- ### Initialize Kepler Mapper and Generate Simplicial Complex Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/Plotly-Demo.ipynb Sets up the Kepler Mapper environment, processes input data using make_circles, and generates a simplicial complex. This complex serves as the foundation for all subsequent visualization tasks. ```python import sklearn from sklearn import datasets import kmapper as km data, labels = datasets.make_circles(n_samples=5000, noise=0.05, factor=0.3) mapper = km.KeplerMapper(verbose=0) lens = mapper.fit_transform(data, projection=[0]) simplicial_complex = mapper.map(lens, X=data, cover=km.Cover(n_cubes=20, perc_overlap=0.1)) ``` -------------------------------- ### Import Libraries for Kepler-Mapper and Plotly Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/Cancer-demo.ipynb Imports necessary libraries including pandas for data manipulation, scikit-learn for machine learning models, kmapper for topological data analysis, and Plotly for visualization. Includes error handling for missing pandas installation. ```python import sys try: import pandas as pd except ImportError as e: print("pandas is required for this example. Please install with conda or pip and then try again.") sys.exit() import numpy as np import sklearn from sklearn import ensemble import kmapper as km from kmapper.plotlyviz import * from sklearn.decomposition import PCA import matplotlib.pyplot as plt import warnings warnings.filterwarnings("ignore") ``` ```python import plotly.graph_objs as go from ipywidgets import ( HBox, VBox ) ``` -------------------------------- ### KeplerMapper Initialization Source: https://context7.com/scikit-tda/kepler-mapper/llms.txt Initialize the KeplerMapper class. You can set a verbosity level for logging. ```APIDOC ## KeplerMapper Class Initialization ### Description The main class for building topological networks from data. Initialize with a verbosity level for logging. ### Method `__init__` ### Parameters - **verbose** (int) - Optional - Verbosity level for logging. ### Request Example ```python import kmapper as km # Initialize KeplerMapper with logging enabled mapper = km.KeplerMapper(verbose=1) ``` ### Response - **mapper** (KeplerMapper) - An instance of the KeplerMapper class. ``` -------------------------------- ### JavaScript D3.js Force-Directed Graph Layout Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/horse/horse_keplermapper_output.html Sets up and starts a D3.js force-directed graph layout using 'json_graph' data. It configures force simulation parameters like link distance, gravity, and charge, then binds nodes and links to SVG elements. ```javascript var graph = JSON.parse(document.getElementById("json_graph").dataset.graph); var force = d3.layout.force() .linkDistance(5) .gravity(0.2) .charge(-1200) .size([w,h]); force .nodes(graph.nodes) .links(graph.links) .start(); var link = g.selectAll(".link") .data(graph.links) .enter().append("line") .attr("class", "link") .style("stroke-width", function(d) { return d.w * nominal_stroke; }) .style("stroke-width", function(d) { return d.w * nominal_stroke; }); var node = g.selectAll(".node") .data(graph.nodes) .enter().append("g") .attr("class", "node") .call(force.drag); ``` -------------------------------- ### Initialize D3.js Force-Directed Graph Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/makecircles/keplermapper-makecircles-distmean.html Sets up the SVG canvas, defines zoom behaviors, and initializes the force layout with nodes and links. It handles dynamic resizing based on window dimensions and configures node interaction. ```javascript var canvas_height = window.innerHeight - 5; var svg = d3.select("#canvas").append("svg").attr("width", width).attr("height", height); var force = d3.layout.force().linkDistance(5).gravity(0.2).charge(-1200).size([w,h]); force.nodes(graph.nodes).links(graph.links).start(); ``` -------------------------------- ### Initialize Kepler-Mapper and Perform Projection and Mapping Source: https://github.com/scikit-tda/kepler-mapper/blob/master/examples/digits/digits.ipynb Initializes the KeplerMapper with a verbosity setting. It then projects the input data using UMAP for dimensionality reduction and subsequently maps the projected data to a simplicial complex using DBSCAN for clustering and a custom cover. ```python mapper = km.KeplerMapper(verbose=0) projected_data = mapper.fit_transform(data, projection=umap.UMAP(n_neighbors=8, min_dist=0.65, n_components=2, metric='euclidean', random_state=3571)) # Get the simplicial complex scomplex = mapper.map(projected_data, clusterer=sklearn.cluster.DBSCAN(eps=0.3, min_samples=15), cover=km.Cover(35, 0.9)) ``` -------------------------------- ### KeplerMapper Pipeline Execution Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/KeplerMapper-usage-in-Jupyter-Notebook.ipynb This endpoint-like documentation covers the core workflow of the KeplerMapper library, including initialization, data projection, mapping, and visualization generation. ```APIDOC ## POST /mapper/process ### Description Initializes the KeplerMapper object, projects data onto a lower-dimensional space, maps the data into a graph structure, and exports the visualization to an HTML file. ### Method POST ### Endpoint /mapper/process ### Parameters #### Request Body - **data** (array) - Required - The input dataset to be analyzed. - **projection** (list) - Required - The indices of the dimensions to project onto. - **n_cubes** (int) - Optional - Number of intervals/cubes for the cover. ### Request Example { "data": [[0.1, 0.2], [0.3, 0.4]], "projection": [0, 1], "n_cubes": 10 } ### Response #### Success Response (200) - **path_html** (string) - The file path where the generated visualization is saved. - **graph_stats** (object) - Metadata regarding the number of nodes and edges created. #### Response Example { "path_html": "output/visualization.html", "graph_stats": { "nodes": 92, "edges": 242 } } ``` -------------------------------- ### Loading and Inspecting Newsgroups20 Dataset Source: https://github.com/scikit-tda/kepler-mapper/blob/master/docs/notebooks/KeplerMapper-Newsgroup20-Pipeline.ipynb Fetches the training subset of the Newsgroups20 dataset and prints sample data, shape, and target labels to verify successful loading. ```python newsgroups = fetch_20newsgroups(subset='train') X, y, target_names = np.array(newsgroups.data), np.array(newsgroups.target), np.array(newsgroups.target_names) print("SAMPLE",X[0]) print("SHAPE",X.shape) print("TARGET",target_names[y[0]]) ```