• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MilesCranmer / PySR / 5439183423

pending completion
5439183423

push

github

web-flow
style: Format code with black

949 of 1065 relevant lines covered (89.11%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/pysr/export_numpy.py
1
"""Code for exporting discovered expressions to numpy"""
2
import numpy as np
1✔
3
import pandas as pd
1✔
4
from sympy import lambdify
1✔
5
import warnings
1✔
6

7

8
class CallableEquation:
1✔
9
    """Simple wrapper for numpy lambda functions built with sympy"""
10

11
    def __init__(self, sympy_symbols, eqn, selection=None, variable_names=None):
1✔
12
        self._sympy = eqn
1✔
13
        self._sympy_symbols = sympy_symbols
1✔
14
        self._selection = selection
1✔
15
        self._variable_names = variable_names
1✔
16

17
    def __repr__(self):
1✔
18
        return f"PySRFunction(X=>{self._sympy})"
1✔
19

20
    def __call__(self, X):
1✔
21
        expected_shape = (X.shape[0],)
1✔
22
        if isinstance(X, pd.DataFrame):
1✔
23
            # Lambda function takes as argument:
24
            return self._lambda(
1✔
25
                **{k: X[k].values for k in self._variable_names}
26
            ) * np.ones(expected_shape)
27
        if self._selection is not None:
1✔
28
            if X.shape[1] != len(self._selection):
1✔
29
                warnings.warn(
×
30
                    "`X` should be of shape (n_samples, len(self._selection)). "
31
                    "Automatically filtering `X` to selection. "
32
                    "Note: Filtered `X` column order may not match column order in fit "
33
                    "this may lead to incorrect predictions and other errors."
34
                )
35
                X = X[:, self._selection]
×
36
        return self._lambda(*X.T) * np.ones(expected_shape)
1✔
37

38
    @property
1✔
39
    def _lambda(self):
1✔
40
        return lambdify(self._sympy_symbols, self._sympy)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc