Skip to content

fix(docs): correct stale unit examples in array_creation notebook#128

Merged
chaoming0625 merged 1 commit into
mainfrom
worktree-fix-doc-notebooks
Jun 14, 2026
Merged

fix(docs): correct stale unit examples in array_creation notebook#128
chaoming0625 merged 1 commit into
mainfrom
worktree-fix-doc-notebooks

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Executed all 31 source notebooks under docs/ (excluding docs/_build/ build artifacts). Two cells in docs/unit_operations/array_creation.ipynb raised UnitMismatchError because they used examples inconsistent with saiunit's documented unit semantics. This fixes both so every notebook runs cleanly.

Fixes

  • asarray([1, 2, 3], unit=second) → a raw dimensionless list cannot be converted to seconds. asarray with an explicit unit= performs strict conversion — consistent with its docstring, the audit comment in _fun_array_creation.py, and the notebook's own markdown. The cell now converts a same-dimension quantity across magnitudes (mss), matching the surrounding prose ("same dimension but different magnitude → converted to the provided unit").
  • logspace(0*s, 10*s, 5)logspace computes base ** x, which is intrinsically dimensionless and rejects unitful start/stop (per its docstring). The cell now demonstrates that constraint via try/except, matching the notebook's existing idiom for showing expected errors.

No library code changed — both were stale documentation examples. Only docs/unit_operations/array_creation.ipynb is modified (2 source cells + re-executed outputs).

Verification

==== SUMMARY ====
31 passed, 0 failed

🤖 Generated with Claude Code

Summary by Sourcery

Update unit operations array creation documentation examples to align with saiunit’s unit semantics and ensure the notebook executes cleanly.

Bug Fixes:

  • Correct asarray example to demonstrate conversion between quantities of the same dimension and magnitude scaling instead of converting a raw list to a unitful quantity.
  • Adjust logspace example to illustrate that it only accepts dimensionless inputs by showing the expected error when units are provided.

Documentation:

  • Refresh docs/unit_operations/array_creation.ipynb with corrected code cells and re-executed outputs so that all documented examples run successfully.

Two cells in docs/unit_operations/array_creation.ipynb used examples that
violated saiunit's documented unit semantics and raised UnitMismatchError,
breaking notebook execution:

- `asarray([1, 2, 3], unit=second)`: a raw dimensionless list cannot be
  converted to seconds. `asarray` with an explicit `unit=` performs strict
  conversion (per its docstring and the notebook's own markdown), so the
  example now converts a same-dimension quantity across magnitudes (ms -> s),
  which is what the surrounding prose describes.
- `logspace(0*s, 10*s, 5)`: `logspace` computes `base ** x`, which is
  intrinsically dimensionless and rejects unitful start/stop. The cell now
  demonstrates that constraint via try/except, matching the notebook's
  existing idiom for showing expected errors.

All 31 source notebooks under docs/ now execute cleanly.
@sourcery-ai

sourcery-ai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Updates the array_creation documentation notebook so that all examples respect saiunit’s unit semantics, fixing two stale code cells and re-executing the notebook to refresh outputs and execution metadata.

File-Level Changes

Change Details Files
Align asarray unit-conversion example with documented behavior for unitful inputs.
  • Replace example that attempted to convert a raw dimensionless list to seconds via unit= with an example converting millisecond Quantities to seconds.
  • Update expected output to show a Quantity with seconds, matching the new input.
  • Keep surrounding asarray examples (plain array, same-unit Quantity, mismatched-unit failure) but refresh their execution metadata and outputs after re-running the cell block.
docs/unit_operations/array_creation.ipynb
Clarify that logspace requires dimensionless inputs and demonstrate the resulting error for unitful arguments.
  • Replace the unitful logspace example that previously returned a Quantity with a try/except snippet that calls logspace with second-valued arguments and prints the raised error message.
  • Update the accompanying comment to explain that logspace is intrinsically dimensionless (base ** x) and therefore rejects units.
  • Re-execute the notebook so the new error output and execution metadata are captured.
docs/unit_operations/array_creation.ipynb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chaoming0625 chaoming0625 merged commit 43a3139 into main Jun 14, 2026
8 checks passed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Most of the diff is notebook execution metadata (timestamps, iopub events, Python version, etc.); consider stripping or normalizing these fields so future documentation changes have smaller, more focused diffs.
  • The tri example now captures a JAX dtype UserWarning in stderr; if this isn’t intentional instructional content, it would be better to adjust the example or environment so this warning doesn’t appear in the rendered docs.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Most of the diff is notebook execution metadata (timestamps, iopub events, Python version, etc.); consider stripping or normalizing these fields so future documentation changes have smaller, more focused diffs.
- The `tri` example now captures a JAX dtype UserWarning in stderr; if this isn’t intentional instructional content, it would be better to adjust the example or environment so this warning doesn’t appear in the rendered docs.

## Individual Comments

### Comment 1
<location path="docs/unit_operations/array_creation.ipynb" line_range="13-22" />
<code_context>
   {
    "cell_type": "markdown",
    "metadata": {},
</code_context>
<issue_to_address>
**suggestion:** Consider avoiding stderr warnings in documentation output for `u.math.tri`.

The `u.math.tri(3)` example currently emits a JAX `UserWarning` about truncating `float64` to `float32`, which will appear in the rendered docs and distract from the intended output. Consider either changing the example to avoid triggering the warning (e.g., by enabling `jax_enable_x64` or using a non-upcasted dtype) or explicitly suppressing this warning in the notebook cell so the visible output only shows the function result.

Suggested implementation:

```
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {

```

```
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "source": [
    "import warnings\n",
    "\n",
    "with warnings.catch_warnings():\n",
    "    warnings.filterwarnings(\n",
    "        \"ignore\",\n",
    "        message=\".*truncating.*float64.*to float32.*\",\n",
    "        category=UserWarning,\n",
    "    )\n",
    "    u.math.tri(3)\n"
   ]
  },

```

I assumed:
1. There is a code cell in `docs/unit_operations/array_creation.ipynb` whose sole (or final) source line is exactly `u.math.tri(3)`.
2. The JSON structure around that cell matches the pattern shown in the snippet.

If the actual notebook cell differs (e.g., multiple expressions or different execution counts/metadata), you'll need to adjust the `SEARCH` block to match the exact JSON for that cell. The key idea is to replace the bare `u.math.tri(3)` source with the `warnings`-wrapped version shown in the `REPLACE` block.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 13 to +22
{
"cell_type": "markdown",
"metadata": {},
"source": "The functions listed below are designed to create `array` or `Quantity` with specific properties, such as filled with a certain value, identity matrices, or arrays with ones on the diagonal. These functions are part of the `saiunit.math` module and are tailored to handle both numerical `array` and `Quantity` with units."
"source": [
"The functions listed below are designed to create `array` or `Quantity` with specific properties, such as filled with a certain value, identity matrices, or arrays with ones on the diagonal. These functions are part of the `saiunit.math` module and are tailored to handle both numerical `array` and `Quantity` with units."
]
},
{
"cell_type": "code",
"execution_count": 1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider avoiding stderr warnings in documentation output for u.math.tri.

The u.math.tri(3) example currently emits a JAX UserWarning about truncating float64 to float32, which will appear in the rendered docs and distract from the intended output. Consider either changing the example to avoid triggering the warning (e.g., by enabling jax_enable_x64 or using a non-upcasted dtype) or explicitly suppressing this warning in the notebook cell so the visible output only shows the function result.

Suggested implementation:

   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {

   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "source": [
    "import warnings\n",
    "\n",
    "with warnings.catch_warnings():\n",
    "    warnings.filterwarnings(\n",
    "        \"ignore\",\n",
    "        message=\".*truncating.*float64.*to float32.*\",\n",
    "        category=UserWarning,\n",
    "    )\n",
    "    u.math.tri(3)\n"
   ]
  },

I assumed:

  1. There is a code cell in docs/unit_operations/array_creation.ipynb whose sole (or final) source line is exactly u.math.tri(3).
  2. The JSON structure around that cell matches the pattern shown in the snippet.

If the actual notebook cell differs (e.g., multiple expressions or different execution counts/metadata), you'll need to adjust the SEARCH block to match the exact JSON for that cell. The key idea is to replace the bare u.math.tri(3) source with the warnings-wrapped version shown in the REPLACE block.

@chaoming0625 chaoming0625 deleted the worktree-fix-doc-notebooks branch June 14, 2026 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant