Skip to content

[Bug] plqERM_Ridge.fit() crashes in IPython but works in plain Python #39

@statmlben

Description

@statmlben

Bug

import numpy as np
from rehline import plq_Ridge_Regressor
np.random.seed(42)
n = 2000
x = np.random.randn(n)
noise = np.random.randn(n) * (0.3 + 0.5 * np.abs(x))
y = 2 * x + noise
X = x.reshape(-1, 1)
clf = plq_Ridge_Regressor(
    loss={'name': 'check_eps', 'qt': 0.9, 'epsilon': 0.1},
    C=10.0 / n
)
clf.fit(X=X, y=y)
  • ✅ python script.py — works fine
  • ❌ IPython — crashes

Cause

IPython auto-displays the return value of clf.fit(...) (which is self). This triggers sklearn's __repr__ / _repr_mimebundle_, which introspects all estimator attributes.

The problem is in plqERM_Ridge.fit() (_class.py):

self.opt_result_ = result          # raw pybind11 C++ object stored as attribute
self.coef_ = result.beta           # numpy view into C++ Eigen memory, no .copy()
self._Lambda = result.Lambda       # same
self._Gamma = result.Gamma         # same
self._xi = result.xi               # same

result is a pybind11-wrapped rehline_result C++ struct. When IPython's repr machinery touches this object (which has no __repr__ defined), it can crash. Additionally, self.coef_ etc. are not copies — they are numpy views into Eigen memory owned by the C++ object, creating dangling-reference risks.

Suggested fix

In _class.py, plqERM_Ridge.fit() (and similar in ReHLine.fit(), CQR_Ridge.fit(), plqERM_ElasticNet.fit()):

self.coef_ = result.beta.copy()
self._Lambda = result.Lambda.copy()
self._Gamma = result.Gamma.copy()
self._xi = result.xi.copy()
self.n_iter_ = result.niter
self.dual_obj_ = list(result.dual_objfns)
self.primal_obj_ = list(result.primal_objfns)
# Remove: self.opt_result_ = result

Workaround

clf.fit(X=X, y=y); # semicolon suppresses IPython display

Environment

  • Python 3.13, macOS
  • rehline 0.1.2.dev
  • IPython (terminal)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions