Skip to content

Commit

Permalink
2.4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
erdogant committed Aug 21, 2023
1 parent 2796bbd commit 336096a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
2 changes: 1 addition & 1 deletion d3graph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

__author__ = 'Erdogan Tasksen'
__email__ = '[email protected]'
__version__ = '2.4.8'
__version__ = '2.4.9'


# module level doc-string
Expand Down
65 changes: 32 additions & 33 deletions d3graph/d3graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Make interactive network in D3 javascript."""
# ---------------------------------
# Author : E.Taskesen
# Name : d3graph.py
# Licence : See licences
# ---------------------------------
Expand All @@ -12,7 +13,6 @@
from pathlib import Path
from sys import platform
from tempfile import TemporaryDirectory
from typing import List, Union, Tuple
from unicodedata import normalize

from community import community_louvain
Expand Down Expand Up @@ -72,7 +72,7 @@ class d3graph:
def __init__(self,
collision: float = 0.5,
charge: int = 450,
slider: List[int] = None,
slider: list[int] = None,
support='text',
verbose: int = 20) -> None:
"""Initialize d3graph."""
Expand Down Expand Up @@ -103,7 +103,7 @@ def _clean(self, clean_config: bool = True) -> None:
if clean_config and hasattr(self, 'config'): del self.config

def show(self,
figsize: Tuple[int, int] = (1500, 800),
figsize: tuple[int, int] = (1500, 800),
title: str = 'd3graph',
filepath: str = 'd3graph.html',
showfig: bool = True,
Expand Down Expand Up @@ -203,8 +203,8 @@ def set_edge_properties(self,
label: str = None,
label_color = '#808080',
label_fontsize: int = 8,
minmax: List[float] = [0.5, 15],
minmax_distance: List[float] = [50, 100],
minmax: list[float] = [0.5, 15],
minmax_distance: list[float] = [50, 100],
) -> dict:
"""Edge properties.
Expand Down Expand Up @@ -296,15 +296,15 @@ def set_edge_properties(self,
logger.debug('Number of edges: %.0d', len(self.edge_properties.keys()))

def set_node_properties(self,
label: List[str] = None,
tooltip: List[str] = None,
color: Union[str, List[str]] = 'cluster',
opacity: Union[float, List[float]] = 'degree',
size: Union[int, List[int]] = 15,
edge_color: Union[str, List[str]] = '#000000',
edge_size: Union[int, List[int]] = 1,
fontcolor: Union[str, List[str]] = 'node_color',
fontsize: Union[int, List[int]] = 12,
label: list[str] = None,
tooltip: list[str] = None,
color: tuple[str, list[str]] = 'cluster',
opacity: tuple[float, list[float]] = 'degree',
size: tuple[int, list[int]] = 15,
edge_color: tuple[str, list[str]] = '#000000',
edge_size: tuple[int, list[int]] = 1,
fontcolor: tuple[str, list[str]] = 'node_color',
fontsize: tuple[int, list[int]] = 12,
cmap: str = 'Set1',
scaler: str = 'zscore',
minmax = [8, 13]):
Expand Down Expand Up @@ -503,17 +503,17 @@ def set_node_properties(self,
############# Store in dict #############
self.node_properties = {}
for i, node in enumerate(node_names):
self.node_properties[node] = {'name' : node,
'label' : label[i],
'tooltip' : tooltip[i],
'color' : color[i].astype(str),
'opacity' : opacity[i].astype(str),
'fontcolor' : fontcolor[i].astype(str),
'fontsize' : fontsize[i].astype(int),
'size' : size[i],
'edge_size' : edge_size[i],
'edge_color' : edge_color[i],
'group' : group[i]}
self.node_properties[node] = {'name': node,
'label': label[i],
'tooltip': tooltip[i],
'color': color[i].astype(str),
'opacity': opacity[i].astype(str),
'fontcolor': fontcolor[i].astype(str),
'fontsize': fontsize[i].astype(int),
'size': size[i],
'edge_size': edge_size[i],
'edge_color': edge_color[i],
'group': group[i]}

logger.info('Number of unique nodes: %.0d', len(self.node_properties.keys()))

Expand Down Expand Up @@ -780,18 +780,15 @@ def import_example(self, data='energy', url=None, sep=','):
* https://github.com/erdogant/datazets
"""

if data == 'small':
source = ['node A', 'node F', 'node B', 'node B', 'node B', 'node A', 'node C', 'node Z']
target = ['node F', 'node B', 'node J', 'node F', 'node F', 'node M', 'node M', 'node A']
weight = [5.56, 0.5, 0.64, 0.23, 0.9, 3.28, 0.5, 0.45]
adjmat = vec2adjmat(source, target, weight=weight)
return adjmat, None
elif data == 'bigbang':
source = ['Penny', 'Penny', 'Amy', 'Bernadette', 'Bernadette', 'Sheldon', 'Sheldon', 'Sheldon', 'Rajesh']
target = ['Leonard', 'Amy', 'Bernadette', 'Rajesh', 'Howard', 'Howard', 'Leonard', 'Amy', 'Penny']
weight = [5, 3, 2, 2, 5, 2, 3, 5, 10]
adjmat = vec2adjmat(source, target, weight=weight)
df = dz.get(data=data)
adjmat = vec2adjmat(df['source'], df['target'], weight=df['weight'])
return adjmat
elif data == 'karate':
import scipy
Expand All @@ -817,7 +814,6 @@ def import_example(self, data='energy', url=None, sep=','):
return dz.get(data=data, url=url, sep=sep)



# %%
def set_logger(verbose: int = 20) -> None:
"""Set the logger for verbosity messages."""
Expand Down Expand Up @@ -1117,7 +1113,10 @@ def make_graph(node_properties: dict, edge_properties: dict) -> dict:


# %% Normalize.
def _normalize_size(getsizes, minscale: Union[int, float] = 0.5, maxscale: Union[int, float] = 4, scaler: str = 'zscore'):
def _normalize_size(getsizes,
minscale: tuple[int, float] = 0.5,
maxscale: tuple[int, float] = 4,
scaler: str = 'zscore'):
# Instead of Min-Max scaling, that shrinks any distribution in the [0, 1] interval, scaling the variables to
# Z-scores is better. Min-Max Scaling is too sensitive to outlier observations and generates unseen problems,

Expand Down Expand Up @@ -1227,7 +1226,7 @@ def remove_special_chars(adjmat):


# %% Convert adjacency matrix to vector
def vec2adjmat(source: list, target: list, weight: List[int] = None, symmetric: bool = True, aggfunc='sum') -> pd.DataFrame:
def vec2adjmat(source: list, target: list, weight: list[int] = None, symmetric: bool = True, aggfunc='sum') -> pd.DataFrame:
"""Convert source and target into adjacency matrix.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion d3graph/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from d3graph import d3graph, adjmat2vec, vec2adjmat

# %% opacity
from d3graph import d3graph
from d3graph import d3graph, adjmat2vec
# intialize to load example dataset
d3 = d3graph()
#
Expand Down

0 comments on commit 336096a

Please sign in to comment.