Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pip install -e .

Note: Depending on your platform, a local build may be required.

Optional features (extras)
- Core install includes only `numpy`.
- Visualization utils require `graphviz` and the Graphviz system binary: `uv pip install -e .[viz]`.
- Dataset download progress uses `tqdm`: `uv pip install -e .[datasets]`.
- Install everything optional: `uv pip install -e .[all]`.

## Quickstart

Minimal example training MNIST with mini‑batches. Tensor slicing is not available yet, so batching uses NumPy views. See `examples/mnist.py` for a complete script.
Expand Down
20 changes: 18 additions & 2 deletions cranberry/features/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@
import tempfile
from typing import Optional, Union
import urllib.request
import sys

import numpy as np
from psutil import OSX
from tqdm import tqdm
from cranberry import Tensor

# Platform detection without psutil
OSX = sys.platform == "darwin"

# Optional tqdm progress bar
try:
from tqdm import tqdm as _tqdm # type: ignore
tqdm = _tqdm # noqa: N802 - keep name compatibility
except Exception: # pragma: no cover - fallback path
class _NoopTqdm:
def __init__(self, *a, **kw):
pass
def update(self, n: int):
pass

# Keep the same callable interface as tqdm
tqdm = _NoopTqdm # noqa: N802 - keep name compatibility

_cache_dir: str = getenv("XDG_CACHE_HOME", os.path.expanduser("~/Library/Caches" if OSX else "~/.cache"))


Expand Down
14 changes: 13 additions & 1 deletion cranberry/features/visualize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from graphviz import Digraph
from cranberry import Tensor

# Defer graphviz requirement until used, with a helpful error
try:
from graphviz import Digraph # type: ignore
except ImportError as _gv_err: # pragma: no cover - informative path
Digraph = None # type: ignore
_GRAPHVIZ_IMPORT_ERROR = _gv_err


def trace(root: Tensor):
nodes, edges = set(), set()
Expand All @@ -27,6 +33,12 @@ def plot_graph(root: Tensor, fmt="svg", rankdir="LR"):
rankdir: TB (top to bottom graph) | LR (left to right)
"""
assert rankdir in ["LR", "TB"]
if Digraph is None:
raise ImportError(
"graphviz is required for visualization. Install the extra with '\n"
"pip install 'cranberry[viz]'\n"
"and ensure the Graphviz system binary is installed (brew/apt/choco)."
) from _GRAPHVIZ_IMPORT_ERROR
nodes, edges = trace(root)
dot = Digraph(format=fmt, graph_attr={"rankdir": rankdir})

Expand Down
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ classifiers = [
]
dependencies = [
"numpy~=2.0",
]

[project.optional-dependencies]
viz = [
"graphviz>=0.20.3,<0.21",
]
datasets = [
"tqdm>=4.66.4,<5",
]
all = [
"graphviz>=0.20.3,<0.21",
"psutil>=6.0.0,<7",
"tqdm>=4.66.4,<5",
]

Expand Down
36 changes: 16 additions & 20 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.