jax icon indicating copy to clipboard operation
jax copied to clipboard

Added code examples for ix_ function with better doc string.

Open selamw1 opened this issue 1 year ago • 7 comments

selamw1 avatar Mar 20 '24 00:03 selamw1

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Mar 20 '24 00:03 google-cla[bot]

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

It's already signed.

selamw1 avatar Mar 20 '24 00:03 selamw1

Hi @jakevdp

Is the following modification a doable solution for inserting embedded code examples (customized to JAX) for NumPy and SciPy?

We would use lax_description for inserting examples and leave the implements decorator as it is.

@util.implements(np.ix_, lax_description="""Example:
    >>> import jax.numpy as jnp
    >>> a = jnp.arange(10).reshape(2, 5)
    >>> a
    Array([[0, 1, 2, 3, 4],
           [5, 6, 7, 8, 9]], dtype=int32)
    >>> ixgrid = jnp.ix_(jnp.array([0, 1]), jnp.array([2, 4]))
    >>> ixgrid
    (Array([[0],
           [1]], dtype=int32), Array([[2, 4]], dtype=int32))
    >>> ixgrid[0].shape, ixgrid[1].shape
    ((2, 1), (1, 2))
    >>> a[ixgrid]
    Array([[2, 4],
           [7, 9]], dtype=int32)""")
def ix_(*args: ArrayLike) -> tuple[Array, ...]:
  util.check_arraylike("ix", *args)
Screenshot 2024-04-25 at 2 09 51 PM

If not, I will do it from scratch as you suggested earlier, without referencing the original NumPy docstring.

Since the CLA has failed here, I will close and raise another PR.

selamw1 avatar Apr 25 '24 21:04 selamw1

Typically the example comes below the description of the function parameters. I think in this case it would be best to rewrite the docstring from scratch. I'm doing some similar work in #20941 – you can do something similar for this case.

jakevdp avatar Apr 25 '24 22:04 jakevdp

Oh, and please don't open a new PR if possible – it's useful to keep all the conversation in one place. The failing CLA is probably due to whatever email you have attached to the commits on your branch: you can undo previous commits and redo new commits with the correct email in order to update this branch

jakevdp avatar Apr 25 '24 22:04 jakevdp

It make sene, docstring is rewritten from scratch and tried to squash the commits.

selamw1 avatar Apr 26 '24 01:04 selamw1

Thanks @jakevdp

'See also' section added and indentations corrected. Locally, the formatting appears as follows:

image

selamw1 avatar Apr 27 '24 03:04 selamw1

Thanks @jakevdp

  • Description is adjusted:
    • ndarray is modified to "Tuple of JAX arrays".
    • integer sequences is modified to integer arrays.
  • New example is added.

selamw1 avatar Apr 30 '24 00:04 selamw1

unnecessary texts and example outputs are modified/removed.

selamw1 avatar Apr 30 '24 23:04 selamw1

Looks like there are some lint issues with whitespace. You can fix these locally by running pre-commit: https://jax.readthedocs.io/en/latest/contributing.html#linting-and-type-checking

jakevdp avatar May 01 '24 21:05 jakevdp

Looks like there are some lint issues with whitespace. You can fix these locally by running pre-commit: https://jax.readthedocs.io/en/latest/contributing.html#linting-and-type-checking

It seems the lint failure was below: trim trailing whitespace.................................................Failed

removing whitespace before the following lines: def ix_(*args: ArrayLike) -> tuple[Array, ...]: and util.check_arraylike("ix", *args)

resolve the issue?

running pre-commit locally prints the following error:

      Preparing metadata (pyproject.toml): finished with status 'error'
stderr:
      error: subprocess-exited-with-error
      
      × Preparing metadata (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [39 lines of output]
          INFO:hatch_jupyter_builder.utils:Running jupyter-builder

selamw1 avatar May 01 '24 23:05 selamw1

Yeah, removing all trailing whitespace should fix the error. Regarding the pre-commit installation error, it's probably some kind of version incompatibility. Hard to say without seeing more of the error or logs.

jakevdp avatar May 01 '24 23:05 jakevdp

It seems like some other commits snuck into your PR! It looks like you both rebased and merged, but targeted different snapshots of the main branch. I'd try fixing it like this:

$ git remote -v  # just to make sure it's clear what upstream and origin are here
origin	[email protected]:selamw1/jax.git
upstream	[email protected]:google/jax.git
$ git checkout main
$ git pull upstream main  # this should fast-forward cleanly if you haven't added commits to your local main branch
$ git checkout add_examples
$ git rebase main  # this should rebase cleanly if you haven't done anything strange
$ git log  # make sure you only have a single commit

In most cases this should work, though admittedly I don't really know what you've done to your local branch, so if you've done something really strange (like merged against branches with conflicts) then you may have to do more to fix it.

jakevdp avatar May 03 '24 21:05 jakevdp

It seems like some other commits snuck into your PR! It looks like you both rebased and merged, but targeted different snapshots of the main branch in each operation. I'd try fixing it like this:

$ git remote -v  # just to make sure it's clear what upstream and origin are here
origin	[email protected]:selamw1/jax.git
upstream	[email protected]:google/jax.git
$ git checkout main
$ git pull upstream main  # this should fast-forward cleanly if you haven't added commits to your local main branch
$ git checkout add_examples
$ git rebase main  # this should rebase cleanly if you haven't done anything strange
$ git log  # make sure you only have a single commit
$ git push origin +add_examples

In most cases this should work, though admittedly I don't really know what you've done to your local branch, so if you've done something really strange (like merged against branches with conflicts) then you may have to do more to fix it.

jakevdp avatar May 03 '24 21:05 jakevdp