gh-77188: Name-mangled private methods don't roundtrip when pickling by ZackerySpytz · Pull Request #21480 · python/cpython
newville pushed a commit to lmfit/lmfit-py that referenced this pull request
Dec 5, 2024* Updated the Minimizer.least_squares method to wrap the Jacobian function - The Jacobian function provided by the user is now handled similarly to how it is handled in, e.g., Minimizer.leastsq. - If the Jacobian function is called 'Dfun' in the keyword arguments, then it is also accepted (Minimizer.scalar_minimize already does this). * Updated the Minimizer.__jacobian method signature to fix exception - The signature now matches the signature of Minimizer.__residual. This was changed because the following exception was being raised whenever the user provided a function for calculating the Jacobian: "TypeError: Minimizer.__jacobian() got an unexpected keyword argument 'apply_bounds_transformation'". * Updated the Minimizer.__jacobian method to use the new parameter - The updating of the parameter values is now similar to how it is done in Minimizer.__residual. * Updated test_least_squares_jacobian_types to handle the wrapped Jacobian function - The params argument of the jac_array function is now an actual Parameters object like it is in the f function, which means that the parameters can be accessed via string keys. * Updated the coerce_float64 function - Added explicit support for sparse matrices (e.g., Block Sparse Row) and LinearOperator objects. This change is needed because the updated test_least_squares_jacobian_types would otherwise fail. Similar to how these types are handled when calculating the Hessian matrix in Minimizer.least_squares. * Renamed the Minimizer.__jacobian method to avoid a bug related to pickling - Attempting to make use of multiprocessing.Pool while providing, e.g., Minimizer.least_squares a function for calculating the Jacobian did not work prior to the renaming of the method due to an issue related to pickling and unpickling of private methods. See the (currently) open issue (python/cpython#77188) and pull request (python/cpython#21480) for more information. * Added a test for when the "least_squares" method is provided a function for the Jacobian - Verifies that a) the fitted parameters have the expected values, b) the jac function is actually called, and c) that there are indeed fewer function evaluations as a result. * Updated the docstring of Minimizer._jacobian - The docstring is updated to include the parameters and return values. * Added tests for (un)pickling Minimizer when a Jacobian function is provided * Updated timeout value in test_jacobian_with_forkingpickler - Increased the timeout value so that slow runners can actually succeed instead of timing out. If no timeout was specified, then a failing test could end up hanging instead. * Added an example that demonstrates the benefits of providing a Jacobian function * Updated the list of contributors