Skip to content

fix(inputs,algorithms,connect): deprecation aliases, regression fits, CSR guard, dtype#853

Merged
chaoming0625 merged 1 commit into
masterfrom
fix/audit-20260619-small-modules
Jun 18, 2026
Merged

fix(inputs,algorithms,connect): deprecation aliases, regression fits, CSR guard, dtype#853
chaoming0625 merged 1 commit into
masterfrom
fix/audit-20260619-small-modules

Conversation

@chaoming0625

@chaoming0625 chaoming0625 commented Jun 18, 2026

Copy link
Copy Markdown
Member

Fresh review of brainpy/{connect,initialize,encoding,inputs,algorithms,tools}.

  • Criticalspike_current/ramp_current deprecation aliases forwarded to constant_input with wrong kwargs (crash); now forward to spike_input/ramp_input.
  • CriticalLogisticRegression.call crashed (flattened targets then read .shape[1]); now inits a 1-D parameter vector.
  • HighElasticNetRegression trained with add_bias=True while predict used self.add_bias → feature-mismatch crash; now consistent.
  • HighCSRConn.build_csr guard pre_num != pre_num was a tautology; now validates against inptr.size-1.
  • Mediumconnect.coo2csr scattered int counts into a uint32 buffer (int32→uint32 warning / future JAX error); now uses get_idx_type().
  • Mediumpolynomial_features allocated one extra always-zero column (off-by-one).

Prior-audit C-23/C-24/H-46/H-47 verified already-fixed. In-scope: 509 passed. Findings: docs/issues-found-20260619-small-modules.md.

Summary by Sourcery

Fix multiple correctness issues in offline regression algorithms, sparse connectivity utilities, and deprecated current input aliases, and document the findings from the small-modules audit.

Bug Fixes:

  • Ensure LogisticRegression initializes a 1-D parameter vector so fitting no longer crashes on flattened targets.
  • Make ElasticNetRegression use consistent polynomial feature construction between training and prediction, respecting the add_bias flag.
  • Correct CSRConn.build_csr to validate the declared pre_size against the CSR indptr length and raise on inconsistencies.
  • Update coo2csr to use the connection index dtype for count buffers to avoid int-to-uint scatter warnings and future JAX errors.
  • Adjust polynomial_features to allocate the exact required number of feature columns, removing an always-zero extra column.
  • Fix deprecated spike_current and ramp_current aliases to delegate to spike_input and ramp_input respectively instead of constant_input.

Documentation:

  • Add an issues-found report documenting the results of the small-modules audit and the defects fixed in this change.

Tests:

  • Expand and update unit tests for LogisticRegression and ElasticNetRegression to verify successful training, prediction, and numerical sanity.
  • Add tests for CSRConn consistency checking and coo2csr warning-free behavior.
  • Add tests to ensure deprecated spike_current and ramp_current aliases both warn and behave identically to the corresponding non-deprecated inputs.

… CSR guard, dtype/off-by-one

- spike_current/ramp_current deprecation aliases forwarded to constant_input
  with wrong kwargs (crash); forward to spike_input/ramp_input (Critical)
- LogisticRegression.call crashed (flattened targets then read .shape[1]);
  init a 1-D parameter vector (Critical)
- ElasticNetRegression trained with default add_bias=True while predict used
  self.add_bias -> feature mismatch crash; pass add_bias=self.add_bias (High)
- CSRConn.build_csr guard "pre_num != pre_num" was a tautology; compare against
  inptr.size-1 and raise on inconsistent pre_size (High)
- connect.coo2csr scattered int counts into a uint32 buffer (int32->uint32
  warning, future JAX error); use get_idx_type() (Medium)
- polynomial_features allocated one extra always-zero column (off-by-one) (Medium)

Findings recorded in docs/issues-found-20260619-small-modules.md
@chaoming0625 chaoming0625 merged commit 3e29c8e into master Jun 18, 2026
1 of 4 checks passed
@sourcery-ai

sourcery-ai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fixes several correctness issues in small BrainPy modules: deprecation aliases for spike/ramp currents now call the correct implementations, logistic and elastic-net regressions now construct parameters/features consistently, CSR connections validate pre_size and avoid JAX dtype warnings, and polynomial feature expansion no longer allocates an extra dead column; tests and an issues-found doc were added/updated accordingly.

Flow diagram for CSR connectivity validation and dtype-safe COO→CSR conversion

flowchart TD
    A[CSRConn.build_csr] --> B{pre_num == inptr.size - 1?}
    B -- no --> C[raise ConnectorError]
    B -- yes --> D[proceed to build CSR]

    E[coo2csr] --> F[get_idx_type]
    F --> G[allocate final_pre_count with get_idx_type]
    G --> H[scatter pre_count into final_pre_count without dtype mismatch]
Loading

File-Level Changes

Change Details Files
Fix LogisticRegression parameter initialization so call no longer crashes and returns a 1-D weight vector.
  • Flattened targets are now matched with a 1-D parameter vector instead of indexing targets.shape[1].
  • LogisticRegression.init_weights is called with output dimension 1 and the result flattened.
  • Offline tests updated to assert separator learning, finite weights, and reasonable accuracy on a separable dataset.
brainpy/algorithms/offline.py
brainpy/algorithms/offline_test.py
Make ElasticNetRegression training and prediction use consistent polynomial feature construction with respect to add_bias.
  • ElasticNetRegression.call now passes add_bias=self.add_bias into polynomial_features before normalization.
  • Existing elastic net tests were rewritten to assert end-to-end train/predict consistency for default and add_bias=True configurations, checking output shapes and finiteness.
brainpy/algorithms/offline.py
brainpy/algorithms/offline_test.py
Correct polynomial_features design matrix sizing to avoid an extra always-zero feature column.
  • Changed the allocation of X_new so its width is n_features (+bias if requested) plus the number of interaction terms, without an additional leading +1.
  • Updated utils tests to assert exact output shapes for degree=2 with and without bias and to verify no all-zero columns remain.
brainpy/algorithms/utils.py
brainpy/algorithms/utils_test.py
Fix deprecated spike_current and ramp_current wrappers to delegate to the correct spike_input and ramp_input functions while preserving deprecation warnings.
  • spike_current now returns spike_input(*args, **kwargs) instead of constant_input.
  • ramp_current now returns ramp_input(*args, **kwargs) instead of constant_input.
  • Current tests and coverage tests updated so deprecation wrappers both warn and accept the documented spike/ramp-style arguments, and so the alias outputs equal the direct functions.
brainpy/inputs/currents.py
brainpy/inputs/currents_test.py
brainpy/inputs/currents_coverage_test.py
Harden CSR connectivity utilities: validate CSRConn pre_size against indptr length and align coo2csr count dtypes with index dtypes to avoid JAX scatter warnings.
  • CSRConn.build_csr now checks self.pre_num != self.inptr.size - 1 and raises ConnectorError on inconsistency instead of using a tautological self-comparison.
  • coo2csr now allocates the pre-count buffer using get_idx_type() instead of uint32 in both numpy and JAX branches to avoid int->uint scatter mismatches.
  • New tests cover CSRConn success and failure paths and assert that coo2csr runs without raising FutureWarning about dtype casts.
brainpy/connect/custom_conn.py
brainpy/connect/custom_conn_test.py
brainpy/connect/base.py
Document the results of the small-modules audit in a new issues-found markdown file.
  • Added docs/issues-found-20260619-small-modules.md describing the discovered Critical/High/Medium/Low issues, their locations, rationale, repros, fixes, and status.
  • Cross-referenced prior-audit items and recorded still-open low-priority findings without code changes.
docs/issues-found-20260619-small-modules.md

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In LogisticRegression.call, the new logic always initializes a 1-D parameter vector for a single output; if multi-class or multi-output use is intended, consider handling 2D targets explicitly instead of flattening to 1D so the API doesn’t silently restrict to binary problems.
  • In the JAX branch of coo2csr, indptr is still converted to a NumPy array via onp.insert; consider using a JAX-native operation (e.g., jnp.concatenate) to keep both indices and indptr on the same device and avoid implicit host transfers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `LogisticRegression.call`, the new logic always initializes a 1-D parameter vector for a single output; if multi-class or multi-output use is intended, consider handling 2D targets explicitly instead of flattening to 1D so the API doesn’t silently restrict to binary problems.
- In the JAX branch of `coo2csr`, `indptr` is still converted to a NumPy array via `onp.insert`; consider using a JAX-native operation (e.g., `jnp.concatenate`) to keep both `indices` and `indptr` on the same device and avoid implicit host transfers.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant