Skip to content

Commit d26ea1e

Browse files
committed
Implement Black formatting
1 parent 00060a5 commit d26ea1e

File tree

10 files changed

+253
-144
lines changed

10 files changed

+253
-144
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ repos:
2626
rev: 6.1.0
2727
hooks:
2828
- id: flake8
29+
args:
30+
- --ignore=E501,W503,F841,E203
31+
# E501: line too long
32+
# W503: line break before binary operator
33+
# F841: local variable is assigned to but never used
34+
# E203: whitespace before ':'
2935
- repo: https://github.com/pre-commit/pre-commit-hooks
3036
rev: v4.4.0
3137
hooks:

graphreadability/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
from .core.metricssuite import MetricsSuite
33
from .core.readabilitygraph import ReadabilityGraph
44

5-
from .utils.helpers import *
6-
from .utils.crosses_promotion import *
5+
import utils.helpers as helpers
6+
import utils.crosses_promotion as crosses_promotion
77

88
# Import tests
9-
from .tests import *
9+
import tests
10+
11+
__all__ = ["MetricsSuite", "ReadabilityGraph", "helpers", "crosses_promotion", "tests"]

graphreadability/core/metricssuite.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import math
22
import time
33
from typing import Optional, Union, Sequence
4-
from collections import defaultdict
54
import networkx as nx
65
from ..metrics import metrics
76

87
# Get all the functions in the metrics module
9-
_metric_functions = [func for func in dir(metrics) if callable(getattr(metrics, func)) and not func.startswith("__")]
8+
_metric_functions = [
9+
func
10+
for func in dir(metrics)
11+
if callable(getattr(metrics, func)) and not func.startswith("__")
12+
]
1013

1114
# Generate the DEFAULT_WEIGHTS dictionary
1215
DEFAULT_WEIGHTS = {func: 1 for func in _metric_functions}
1316

1417
# Generate the METRICS dictionary
1518
METRICS = {func: {"func": getattr(metrics, func)} for func in _metric_functions}
1619

20+
1721
class MetricsSuite:
1822
"""A suite for calculating several metrics for graph drawing aesthetics, as well as methods for combining these into a single cost function.
1923
Takes as an argument a path to a GML or GraphML file, or a NetworkX Graph object. Also takes as an argument a dictionary of metric:weight key/values.
@@ -38,7 +42,7 @@ def __init__(
3842
# Dictionary mapping metric names to their functions, values, and weights
3943
self.metrics = METRICS.copy()
4044
for k in self.metrics.keys():
41-
self.metrics[k].update({"weight":0, "value": None, "is_calculated": False})
45+
self.metrics[k].update({"weight": 0, "value": None, "is_calculated": False})
4246

4347
# Check all metrics given are valid and assign weights
4448
self.initial_weights = self.set_weights(metric_weights)
@@ -62,19 +66,21 @@ def __init__(
6266
raise TypeError(
6367
f"'graph' must be a string representing a path to a GML or GraphML file, or a NetworkX Graph object, not {type(graph)}"
6468
)
65-
69+
6670
if sym_tolerance < 0:
67-
raise ValueError(f"sym_tolerance must be positive.")
71+
raise ValueError("sym_tolerance must be positive.")
6872

6973
self.sym_tolerance = sym_tolerance
7074

7175
if sym_threshold < 0:
72-
raise ValueError(f"sym_threshold must be positive.")
76+
raise ValueError("sym_threshold must be positive.")
7377

7478
self.sym_threshold = sym_threshold
7579

7680
def set_weights(self, metric_weights: Sequence[float]):
77-
metrics_to_remove = [metric for metric, weight in metric_weights.items() if weight <= 0]
81+
metrics_to_remove = [
82+
metric for metric, weight in metric_weights.items() if weight <= 0
83+
]
7884

7985
if any(metric_weights[metric] < 0 for metric in metric_weights):
8086
raise ValueError("Metric weights must be positive.")
@@ -85,8 +91,10 @@ def set_weights(self, metric_weights: Sequence[float]):
8591
for metric in metric_weights:
8692
self.metrics[metric]["weight"] = metric_weights[metric]
8793

88-
return {metric: weight for metric, weight in metric_weights.items() if weight > 0}
89-
94+
return {
95+
metric: weight for metric, weight in metric_weights.items() if weight > 0
96+
}
97+
9098
def weighted_prod(self):
9199
"""Returns the weighted product of all metrics. Should NOT be used as a cost function - may be useful for comparing graphs."""
92100
return math.prod(
@@ -114,17 +122,19 @@ def load_graph_test(self, nxg=nx.sedgewick_maze_graph):
114122

115123
nx.set_node_attributes(G, pos)
116124
return G
117-
125+
118126
def reset_metrics(self):
119127
for metric in self.metrics:
120128
self.metrics[metric]["value"] = None
121129
self.metrics[metric]["is_calculated"] = False
122-
130+
123131
def calculate_metric(self, metric: str = None):
124132
"""Calculate the value of the given metric by calling the associated function."""
125133
if metric is None:
126-
raise ValueError("No metric provided. Did you mean to call calculate_metrics()?")
127-
134+
raise ValueError(
135+
"No metric provided. Did you mean to call calculate_metrics()?"
136+
)
137+
128138
if not self.metrics[metric]["is_calculated"]:
129139
self.metrics[metric]["value"] = self.metrics[metric]["func"](self._graph)
130140
self.metrics[metric]["is_calculated"] = True
@@ -141,7 +151,9 @@ def calculate_metrics(self):
141151
self.calculate_metric(metric)
142152
n_metrics += 1
143153
end_time = time.perf_counter()
144-
print(f"Calculated {n_metrics} metrics in {end_time - start_time:0.3f} seconds.")
154+
print(
155+
f"Calculated {n_metrics} metrics in {end_time - start_time:0.3f} seconds."
156+
)
145157

146158
def combine_metrics(self):
147159
"""Combine several metrics based on the given multiple criteria decision analysis technique."""
@@ -172,4 +184,4 @@ def metric_table(self):
172184
for k, v in self.metrics.items():
173185
metrics[k] = v["value"]
174186
metrics["Combined"] = combined
175-
return metrics
187+
return metrics

graphreadability/core/readabilitygraph.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
"""
22
This module is based on:
33
4-
C. Dunne, S. I. Ross, B. Shneiderman, and M. Martino, “Readability metric feedback for aiding
5-
node-link visualization designers,” IBM Journal of Research and Development, vol. 59, no. 2/3,
4+
C. Dunne, S. I. Ross, B. Shneiderman, and M. Martino, “Readability metric feedback for aiding
5+
node-link visualization designers,” IBM Journal of Research and Development, vol. 59, no. 2/3,
66
p. 14:1-14:16, Mar. 2015, doi: 10.1147/JRD.2015.2411412.
77
"""
88

99
import networkx as nx
1010
import numpy as np
11-
from graphreadability.utils.helpers import divide_or_zero, lines_intersect, calculate_angle_between_vectors
11+
from graphreadability.utils.helpers import (
12+
divide_or_zero,
13+
lines_intersect,
14+
calculate_angle_between_vectors,
15+
)
16+
1217

1318
class ReadabilityGraph(nx.Graph):
1419
def __init__(self, data=None, **attr):
@@ -21,12 +26,12 @@ def __init__(self, data=None, **attr):
2126
None # Store computed node overlaps to avoid recomputation
2227
)
2328

24-
### HELPER FUNCTIONS ###
29+
# HELPER FUNCTIONS #
2530
def edge_vector(self, edge):
2631
"""Calculate the vector of an edge given its nodes' positions."""
2732
pos1, pos2 = self.nodes[edge[0]]["pos"], self.nodes[edge[1]]["pos"]
2833
return np.array(pos2) - np.array(pos1)
29-
34+
3035
def calculate_edge_crossings(self):
3136
positions = nx.get_node_attributes(
3237
self, "pos"
@@ -98,7 +103,7 @@ def calculate_node_node_overlap(self):
98103

99104
return overlaps
100105

101-
### METRICS ###
106+
# METRICS #
102107
def node_overlap_global(self):
103108
# Implement computation for global overlap metric
104109
pass

0 commit comments

Comments
 (0)