Skip to content

Commit a9a20e8

Browse files
committed
Ruff fix and version change
1 parent e736ffd commit a9a20e8

11 files changed

Lines changed: 32 additions & 62 deletions

File tree

examples/einsum_demo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ def parse_args() -> argparse.Namespace:
204204
choices=("auto", "manual"),
205205
default="auto",
206206
help=(
207-
"Trace construction mode: auto uses tv.einsum, "
208-
"manual uses pair_tensor + torch.einsum."
207+
"Trace construction mode: auto uses tv.einsum, manual uses pair_tensor + torch.einsum."
209208
),
210209
)
211210
parser.add_argument(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "tensor-network-visualization"
7-
version = "1.3.0"
7+
version = "1.3.3"
88
description = "Minimal Matplotlib visualizations for TensorKrowch, TensorNetwork, Quimb, and TeNPy tensor networks."
99
authors = [{ name = "Alejandro Mata Ali" }]
1010
readme = "README.md"

scripts/clean.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ def main() -> int:
2727
root = Path(__file__).resolve().parent.parent
2828
venv = root / ".venv"
2929

30-
dir_names = frozenset({
31-
"__pycache__",
32-
".pytest_cache",
33-
".ruff_cache",
34-
".tmp",
35-
".pip_tmp",
36-
"build",
37-
"dist",
38-
})
30+
dir_names = frozenset(
31+
{
32+
"__pycache__",
33+
".pytest_cache",
34+
".ruff_cache",
35+
".tmp",
36+
".pip_tmp",
37+
"build",
38+
"dist",
39+
}
40+
)
3941
file_suffixes = (".pyc", ".pyo", ".pyd")
4042

4143
print("Cleaning project caches and temporary files...")
@@ -47,8 +49,10 @@ def main() -> int:
4749
continue
4850
if _should_skip(path, venv):
4951
continue
50-
if path.name in dir_names or path.name.endswith(".egg-info") or path.name.startswith(
51-
"pytest-cache-files-"
52+
if (
53+
path.name in dir_names
54+
or path.name.endswith(".egg-info")
55+
or path.name.startswith("pytest-cache-files-")
5256
):
5357
dirs_to_remove.append(path)
5458

src/tensor_network_viz/_core/_draw_common.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ def _draw_edges(
225225
_perpendicular_3d(direction) if dimensions == 3 else _perpendicular_2d(direction)
226226
)
227227
perpendicular = perpendicular / np.linalg.norm(perpendicular)
228-
if (dimensions == 2 and perpendicular[1] < 0) or (
229-
dimensions == 3 and perpendicular[2] < 0
230-
):
228+
if (dimensions == 2 and perpendicular[1] < 0) or (dimensions == 3 and perpendicular[2] < 0):
231229
perpendicular = -perpendicular
232230
plotter.plot_text(
233231
midpoint + perpendicular * p.label_offset,
@@ -321,9 +319,7 @@ class _DrawScaleParams:
321319
def _draw_scale_params(config: PlotConfig, scale: float, *, is_3d: bool) -> _DrawScaleParams:
322320
"""Compute scale-dependent drawing parameters from config."""
323321
r = (
324-
config.node_radius
325-
if config.node_radius is not None
326-
else PlotConfig.DEFAULT_NODE_RADIUS
322+
config.node_radius if config.node_radius is not None else PlotConfig.DEFAULT_NODE_RADIUS
327323
) * scale
328324
stub = (
329325
config.stub_length if config.stub_length is not None else PlotConfig.DEFAULT_STUB_LENGTH

src/tensor_network_viz/_core/_nodes_edges_common.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ def _build_graph_from_nodes_edges(
8585
for node in node_refs:
8686
name = _stringify(_require_attr(node, "name", "node"))
8787
node_edges = tuple(_iterable_attr(node, "edges", "node"))
88-
axes_names = tuple(
89-
_stringify(item) for item in _iterable_attr(node, axis_attr, "node")
90-
)
88+
axes_names = tuple(_stringify(item) for item in _iterable_attr(node, axis_attr, "node"))
9189
if len(node_edges) != len(axes_names):
9290
raise TypeError(
9391
f"Node {name!r} has {len(node_edges)} edges but {len(axes_names)} {axis_attr}."

src/tensor_network_viz/_core/curves.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ def _ellipse_points_3d(
5454
) -> Vector:
5555
theta = np.linspace(0.0, 2.0 * math.pi, samples)
5656
return (
57-
center
58-
+ np.outer(np.cos(theta), axis_a) * width
59-
+ np.outer(np.sin(theta), axis_b) * height
57+
center + np.outer(np.cos(theta), axis_a) * width + np.outer(np.sin(theta), axis_b) * height
6058
)
6159

6260

src/tensor_network_viz/_core/graph.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ def _make_dangling_edge(
6464
label: str | None | object = _DEFAULT_LABEL,
6565
) -> _EdgeData:
6666
resolved_label = endpoint.axis_name or name
67-
edge_label = (
68-
resolved_label if label is _DEFAULT_LABEL else cast(str | None, label)
69-
)
67+
edge_label = resolved_label if label is _DEFAULT_LABEL else cast(str | None, label)
7068
return _EdgeData(
7169
name=name,
7270
kind="dangling",
@@ -91,9 +89,7 @@ def _make_contraction_edge(
9189
)
9290
endpoints = (left_endpoint, right_endpoint)
9391
resolved_label = _build_edge_label(kind=kind, endpoints=endpoints, edge_name=name)
94-
edge_label = (
95-
resolved_label if label is _DEFAULT_LABEL else cast(str | None, label)
96-
)
92+
edge_label = resolved_label if label is _DEFAULT_LABEL else cast(str | None, label)
9793
return _EdgeData(
9894
name=name,
9995
kind=kind,

src/tensor_network_viz/_core/graph_utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ def _extract_unique_items(
3737
raw_items = source
3838
should_sort = _is_unordered_collection(raw_items)
3939
else:
40-
extra = (
41-
f", or an object with '{attr_sources[0]}' attribute."
42-
if attr_sources
43-
else "."
44-
)
40+
extra = f", or an object with '{attr_sources[0]}' attribute." if attr_sources else "."
4541
raise TypeError(f"Input must be an iterable of {backend_name} {type_name}{extra}.")
4642

4743
iterable = raw_items.values() if isinstance(raw_items, dict) else raw_items

src/tensor_network_viz/_core/layout.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,7 @@ def _best_attachment_position_2d(
424424
) -> np.ndarray:
425425
leaf_node_ids = {node_id for node_id, _ in component.trimmed_leaf_parents}
426426
direction_options = (
427-
candidates
428-
if component.structure_kind == "chain"
429-
else (*candidates, axis, -axis)
427+
candidates if component.structure_kind == "chain" else (*candidates, axis, -axis)
430428
)
431429
used_dirs = []
432430
for neighbor_id in component.contraction_graph.neighbors(parent_id):
@@ -458,13 +456,10 @@ def _best_attachment_position_2d(
458456
candidate = origin + direction * distance
459457
score = 0.8 * float(np.dot(direction[:2], outward_dir))
460458
score -= 2.5 * sum(
461-
max(0.0, float(np.dot(direction[:2], used_dir)))
462-
for used_dir in used_dirs
459+
max(0.0, float(np.dot(direction[:2], used_dir))) for used_dir in used_dirs
463460
)
464461
score -= 2.0 * sum(
465-
1.0
466-
for target in assigned_targets
467-
if np.linalg.norm(candidate[:2] - target[:2]) < 0.3
462+
1.0 for target in assigned_targets if np.linalg.norm(candidate[:2] - target[:2]) < 0.3
468463
)
469464
score -= 4.0 * sum(
470465
1.0
@@ -722,9 +717,7 @@ def _compute_free_directions_3d(
722717
components = _analyze_layout_components(graph)
723718
assigned_segments: list[tuple[np.ndarray, np.ndarray]] = []
724719
component_by_node = {
725-
node_id: component
726-
for component in components
727-
for node_id in component.node_ids
720+
node_id: component for component in components for node_id in component.node_ids
728721
}
729722

730723
for node_id, node in graph.nodes.items():
@@ -772,9 +765,7 @@ def _compute_free_directions_3d(
772765
break
773766
else:
774767
fallback = (
775-
named_direction
776-
if named_direction is not None
777-
else _orthogonal_unit(axis)
768+
named_direction if named_direction is not None else _orthogonal_unit(axis)
778769
)
779770
if not _direction_has_space(fallback, used_dirs):
780771
fallback = -fallback
@@ -825,8 +816,7 @@ def _used_axis_directions(
825816

826817
def _direction_has_space(direction: np.ndarray, used_dirs: list[np.ndarray]) -> bool:
827818
overlap = sum(
828-
max(0.0, float(np.dot(direction, used_direction)))
829-
for used_direction in used_dirs
819+
max(0.0, float(np.dot(direction, used_direction))) for used_direction in used_dirs
830820
)
831821
return overlap < _FREE_DIR_OVERLAP_THRESHOLD
832822

src/tensor_network_viz/_core/layout_structure.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,7 @@ def _detect_grid(nx_graph: nx.Graph) -> dict[int, tuple[int, int]] | None:
290290
mapping = nx.vf2pp_isomorphism(nx_graph, grid_graph)
291291
if mapping is None:
292292
continue
293-
return {
294-
node_id: (grid_col, grid_row)
295-
for node_id, (grid_row, grid_col) in mapping.items()
296-
}
293+
return {node_id: (grid_col, grid_row) for node_id, (grid_row, grid_col) in mapping.items()}
297294
return None
298295

299296

0 commit comments

Comments
 (0)