Skip to content

Add docstrings to randomstart.py - #20

Open
nataliemes wants to merge 5 commits into
gambitproject:mainfrom
nataliemes:docs/randomstart-docstrings
Open

Add docstrings to randomstart.py#20
nataliemes wants to merge 5 commits into
gambitproject:mainfrom
nataliemes:docs/randomstart-docstrings

Conversation

@nataliemes

Copy link
Copy Markdown
Collaborator

Changes

  • Added simple docstrings to internal functions and to main() (for click --help text).
  • Extracted the plotting logic out of the click-decorated main() into a standalone plot_simplex() function, which library users can call directly in their code (it will do the same thing as the CLI randomstart command).
  • Added a NumPy style docstring to plot_simplex().
  • Added a range check for higherdim inside plot_simplex() - invalid values raise ValueError.

Copilot AI 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.

Pull request overview

This PR improves lemke.randomstart usability by documenting existing helper functions and extracting the CLI plotting logic into a library-callable function.

Changes:

  • Added docstrings to internal helpers and main() (to improve click --help output).
  • Extracted plotting logic into a new plot_simplex() helper with a NumPy-style docstring.
  • Added a higherdim range check in plot_simplex() that raises ValueError for invalid values.
Suppressed comments (1)

src/lemke/randomstart.py:110

  • plot_simplex() is described as a library-callable helper, but it currently prints to stdout and recomputes segmentstart inside the sampling loop. Printing is an unexpected side effect for a library API, and recomputing a constant each iteration is unnecessary. Consider removing the prints and precomputing segmentstart once, using it in the loop. Also, fig1 is unused; prefix it with _ to avoid unused-variable warnings.
    if not 3 <= higherdim <= 10:
        raise ValueError("higherdim must be between 3 and 10")
    print(
        f"numpoints={numpoints} accuracy={accuracy} higherdim={higherdim} naiveplot={naiveplot}"
    )

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/lemke/randomstart.py Outdated
Comment on lines +134 to +135
plt.scatter([x], [y], s=10000 // accuracy, facecolors="none",
edgecolors="r")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

good suggestion by copilot

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I used 20 instead of 1, since with size 1 the red circles were still too small to actually be visible on the plot.

Comment thread src/lemke/randomstart.py Outdated
Comment on lines +79 to +107
def plot_simplex(numpoints=200, accuracy=20, higherdim=3, naiveplot=False):
"""Generate a simplex sampling plot.

Samples `numpoints` random points from the simplex of dimension `higherdim`
and projects them onto a 2D triangle (if `higherdim` is greater than 3,
only the middle 3 components of each point are used, renormalized to sum to 1).
Plots the raw sampled points in green and their rounded approximations in red.

Parameters
----------
numpoints : int
Number of points to plot. Default is 200.
accuracy : int
Denominator x; each coordinate is rounded to the nearest multiple of 1/x.
Default is 20. Must be between 1 and 10,000,000.
higherdim : int
Dimension from which the middle 3 components will be sampled.
Default is 3. Must be between 3 and 10.
naiveplot : bool
Sample naively by normalizing random uniforms (biased toward center).
Default is False.

Raises
------
ValueError
If `accuracy` or `higherdim` is out of range.
"""
if not 3 <= higherdim <= 10:
raise ValueError("higherdim must be between 3 and 10")
@rahulsavani
rahulsavani requested a review from stengel September 3, 2026 12:11
Comment thread src/lemke/randomstart.py

@stengel stengel Sep 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

after line 36 add:
The probabilities are multiplied by accuracy, then rounded down to their integer parts,
which will be the numerators, augmented by 1 in order of decreasing size of the remainders
(which are less than 1) until they sum to accuracy.
Example: accuracy=10, x = [0.18, .35, .47] becomes [2/10,3/10,5/10].

Comment thread src/lemke/randomstart.py
Round each entry of an array of probabilities `x`
to the nearest multiple of 1 / `accuracy`.
"""
if not 1 <= accuracy <= MAX_ACCURACY:

@stengel stengel Sep 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we also check that accuracy is an integer? Essential for this to work.

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.

3 participants