Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Prefer using NumPy's random number generator #83

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jhlegarreta
Copy link
Contributor

@jhlegarreta jhlegarreta commented Feb 26, 2025

  • ENH: Prefer using NumPy's random number generator
  • STYLE: Use the GradientTable class for type hinting
  • STYLE: Remove ClassVar[dict] type hinting from GPR param constraints
  • ENH: Ignore scikit-learn Interval and StrOptions type checking
  • MAINT: Require nitransforms 22.x

@jhlegarreta jhlegarreta requested a review from oesteban February 26, 2025 00:28
@jhlegarreta
Copy link
Contributor Author

X-ref nipreps/nireports#174 (comment).

Copy link

codecov bot commented Feb 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.15%. Comparing base (237a9cb) to head (9263143).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #83   +/-   ##
=======================================
  Coverage   69.15%   69.15%           
=======================================
  Files          20       20           
  Lines         963      963           
  Branches      119      119           
=======================================
  Hits          666      666           
  Misses        254      254           
  Partials       43       43           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhlegarreta
Copy link
Contributor Author

mypy is now using 1.15.0
https://github.com/nipreps/nifreeze/actions/runs/13533765224/job/37821488405?pr=83#step:8:31

vs 1.14.1 in the latest successful build:
https://github.com/nipreps/nifreeze/actions/runs/13011897806/job/36291371050#step:8:31

Honestly, I cannot see how to workaround the issue without adding an exception to the checks on the affected lines.

@jhlegarreta jhlegarreta force-pushed the UseNumpyRng branch 6 times, most recently from 8cc572e to 3c25992 Compare March 23, 2025 00:15
@jhlegarreta
Copy link
Contributor Author

Re #83 (comment) @effigies I'd be grateful if you could review commit 3c25992. I have not been able to fix the issue without preventing mypy from checking those lines.

Prefer using NumPy's random number generator and the relevant random
number sampling functions introduced in NumPy 1.17.0 over the legacy
versions.

Add a fixture for tests to set the random number generator consistently
across tests.

Documentation:
https://numpy.org/doc/2.2/reference/random/new-or-different.html#what-s-new-or-different
Use the `dipy.core.gradientsGradientTable` class for type hinting
instead of `dipy.core.gradients.gradient_table`: the latter is a
function name and hence does not provide type information.

Fixes:
```
src/nifreeze/testing/simulations.py:151: error:
 Function "dipy.core.gradients.gradient_table" is not valid as a type  [valid-type]
src/nifreeze/testing/simulations.py:151: note:
 Perhaps you need "Callable[...]" or a callback protocol?
src/nifreeze/testing/simulations.py:185: error:
 Function "dipy.core.gradients.gradient_table" is not valid as a type  [valid-type]
src/nifreeze/testing/simulations.py:185: note:
 Perhaps you need "Callable[...]" or a callback protocol?
src/nifreeze/testing/simulations.py:207: error:
 gradient_table? has no attribute "b0s_mask"  [attr-defined]
src/nifreeze/testing/simulations.py:208: error:
 gradient_table? has no attribute "bvecs"  [attr-defined]
```
Remove `ClassVar[dict]` type hinting from GPR parameter constraints
variable and define it as a normal dictionary.

Fixes:
```
Cannot override instance variable '_parameter_constraints'
 (previously declared on base class 'GaussianProcessRegressor') with class variable
```

As `_param_constraints` is an instance variable in `scikit-learn`:
https://github.com/scikit-learn/scikit-learn/blob/1.6.X/sklearn/gaussian_process/_gpr.py#L189
Ignore `scikit-learn` `Interval` and `StrOptions` type checking errors
to prevent `mypy` erroring with the argument that they are abstract
classes with the `__str__` abstract attribute.

Fixes:
```
src/nifreeze/model/gpr.py:161: error:
 Cannot instantiate abstract class "Interval" with abstract attribute "__str__"  [abstract]
src/nifreeze/model/gpr.py:162: error:
 Cannot instantiate abstract class "StrOptions" with abstract attribute "__str__"  [abstract]
src/nifreeze/model/gpr.py:163: error:
 Cannot instantiate abstract class "Interval" with abstract attribute "__str__"  [abstract]
src/nifreeze/model/gpr.py:166: error:
 Cannot instantiate abstract class "Interval" with abstract attribute "__str__"  [abstract]
```
Require `nitransforms` 22.x.

Fixes:
```
.tox/py310/lib/python3.10/site-packages/nitransforms/io/base.py:4: in <module>
    from scipy.io.matlab.miobase import get_matfile_version
E   ImportError: cannot import name 'get_matfile_version' from 'scipy.io.matlab.miobase'
 (/home/runner/work/nifreeze/nifreeze/.tox/py310/lib/python3.10/site-packages/scipy/io/matlab/miobase.py)
```

raised for example in:
https://github.com/nipreps/nifreeze/actions/runs/14013636690/job/39236852012?pr=83#step:11:58

Importing `scipy.io.matlabt.miobase.get_matfile_version` was removed in:
https://github.com/nipy/nitransforms/pull/151/files

included in release 22.0.0:
https://github.com/nipy/nitransforms/releases/tag/22.0.0
@jhlegarreta
Copy link
Contributor Author

@oesteban I'd dare to say that any new PR will fail in absence of the 4 relevant commits in this PR. After having checked #83 (comment), I'd say that the changes in this PR should be uncontroversial so as to merge it and have the CIs passing.

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