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

MilesCranmer / PySR / 8440251713

26 Mar 2024 05:22PM UTC coverage: 93.575%. First build
8440251713

Pull #587

github

web-flow
Merge de0ce3198 into 5df1590c0
Pull Request #587: Revert GitHub-based registry for backend

1034 of 1105 relevant lines covered (93.57%)

2.55 hits per line

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

92.0
/pysr/export_numpy.py
1
"""Code for exporting discovered expressions to numpy"""
2
import warnings
3✔
3

4
import numpy as np
3✔
5
import pandas as pd
3✔
6
from sympy import lambdify
3✔
7

8

9
def sympy2numpy(eqn, sympy_symbols, *, selection=None):
3✔
10
    return CallableEquation(eqn, sympy_symbols, selection=selection)
3✔
11

12

13
class CallableEquation:
3✔
14
    """Simple wrapper for numpy lambda functions built with sympy"""
15

16
    def __init__(self, eqn, sympy_symbols, selection=None):
3✔
17
        self._sympy = eqn
3✔
18
        self._sympy_symbols = sympy_symbols
3✔
19
        self._selection = selection
3✔
20

21
    def __repr__(self):
3✔
22
        return f"PySRFunction(X=>{self._sympy})"
3✔
23

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

42
    @property
3✔
43
    def _lambda(self):
2✔
44
        return lambdify(self._sympy_symbols, self._sympy)
3✔
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