Skip to content

Commit

Permalink
add option to color based on clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
erdogant committed Nov 17, 2022
1 parent 5cb019b commit 7b27049
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions d3heatmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

__author__ = 'Erdogan Tasksen'
__email__ = '[email protected]'
__version__ = '0.2.1'
__version__ = '0.2.3'

# module level doc-string
__doc__ = """
Expand All @@ -22,10 +22,10 @@
>>> # Example 1:
>>> df = d3.import_example()
>>> # Create heatmap
>>> paths = results = d3.heatmap(df)
>>> paths = d3.heatmap(df, vmax=1)
>>>
>>> # Example 2:
>>> df = d3.import_example(size=(6,20))
>>> df = d3.import_example(size=(12,20))
>>> # Create heatmap
>>> paths = d3.matrix(df)
>>>
Expand Down
19 changes: 11 additions & 8 deletions d3heatmap/d3heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@


# %%
def heatmap(df, clust=None, path=None, title='d3heatmap', description=None, vmax=None, width=720, height=720, showfig=True, stroke='red', verbose=3):
"""Heatmap in d3 javascript.
def heatmap(df, color='cluster', path=None, title='d3heatmap', description=None, vmax=None, width=720, height=720, showfig=True, stroke='red', verbose=3):
"""Heatmap in d3js.
Parameters
----------
df : pd.DataFrame()
Input data. The index and column names are used for the row/column naming.
clust : Numpy array
Cluster label data. Should be in the same order as the columns and of the input dataframe
color : Numpy array
Should be in the same order as the columns and of the input dataframe
None or 'cluster': a clustering approach is used for coloring.
path : String, (Default: user temp directory)
Directory path to save the output, such as 'c://temp/index.html'
title : String, (default: 'd3 Heatmap!')
Expand Down Expand Up @@ -66,7 +67,7 @@ def heatmap(df, clust=None, path=None, title='d3heatmap', description=None, vmax
>>> # Import example
>>> df = d3.import_example()
>>> # Create heatmap
>>> paths = results = d3.heatmap(df)
>>> results = d3.heatmap(df, vmax=1)
Returns
-------
Expand All @@ -80,6 +81,8 @@ def heatmap(df, clust=None, path=None, title='d3heatmap', description=None, vmax
if verbose>=2: print('[d3heatmap] >Warning: Input data should contain unique index names otherwise d3js randomly removes the non-unique ones.')
if description is None:
description = "This heatmap is created in d3js using https://github.com/erdogant/d3heatmap.\n\nA network can be represented by an adjacency matrix, where each cell ij represents an edge from vertex i to vertex j.\n\nGiven this two-dimensional representation of a graph, a natural visualization is to show the matrix! However, the effectiveness of a matrix diagram is heavily dependent on the order of rows and columns: if related nodes are placed closed to each other, it is easier to identify clusters and bridges.\nWhile path-following is harder in a matrix view than in a node-link diagram, matrices have other advantages. As networks get large and highly connected, node-link diagrams often devolve into giant hairballs of line crossings. Line crossings are impossible with matrix views. Matrix cells can also be encoded to show additional data; here color depicts clusters computed by a community-detection algorithm."
if isinstance(color, str) and color=='cluster':
color=None

# Rescale data
if vmax is not None:
Expand Down Expand Up @@ -117,10 +120,10 @@ def heatmap(df, clust=None, path=None, title='d3heatmap', description=None, vmax
# dfvec.to_csv(PATHNAME_TO_CSV, index=False)

# Cluster the nodes
if clust is None:
if color is None:
ce = clusteval()
results = ce.fit(df.values)
clust = results['labx']
color = results['labx']

# Embed the Data in the HTML. Note that the embedding is an important stap te prevent security issues by the browsers.
# Most (if not all) browser do not accept to read a file using d3.csv or so. It then requires security-by-passes, but thats not the way to go.
Expand All @@ -129,7 +132,7 @@ def heatmap(df, clust=None, path=None, title='d3heatmap', description=None, vmax

NODE_STR = '\n{\n"nodes":\n[\n'
for i in range(0, len(nodes)):
NODE_STR = NODE_STR + '{"name":' + '"' + nodes[i] + '"' + ',' + '"cluster":' + str(clust[i]) + "},"
NODE_STR = NODE_STR + '{"name":' + '"' + nodes[i] + '"' + ',' + '"cluster":' + str(color[i]) + "},"
NODE_STR = NODE_STR + '\n'
NODE_STR = NODE_STR + '],\n'

Expand Down
2 changes: 1 addition & 1 deletion d3heatmap/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
df = d3.import_example()

# Create interactive heatmap
results = d3.heatmap(df, vmax=1, path='c:/temp/d3_matrix.html')
results = d3.heatmap(df, vmax=1, path='c:/temp/d3_matrix.html', color=None)

# Create heatmap with some user-defined settings
# results = d3.heatmap(df, vmax=1, width=800, height=800, path='c:/temp/example/d3_heatmap.html', title='Created in d3heatmap', description='d3 heatmap is created using https://github.com/erdogant/d3heatmap. This heatmap is a stand-alone application!')
Expand Down

0 comments on commit 7b27049

Please sign in to comment.