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

Sympy integration via ufunc dispatch, expanded numpy integration #60

Merged
merged 8 commits into from
Feb 21, 2024

Conversation

timkpaine
Copy link
Member

@timkpaine timkpaine commented Feb 10, 2024

Just getting started on this but opening for visibility

fixes #59 eventually

fixes #63 as a side-effect

TODO after

  • update docs on edge methods
  • update docs on separation of some stuff into csp.math
  • add example

Proof of concept:

import csp
from csp import DelayedEdge, ts
from datetime import timedelta
from sympy.utilities.lambdify import lambdify
from sympy.parsing.sympy_parser import (
    parse_expr,
    standard_transformations as _st,
    implicit_multiplication_application as _ima,
)

# Parse sympy expression
expr = parse_expr("x**2 + y**2", transformations=(_st + (_ima,)))

# wrap to csp
syms = list(expr.free_symbols)
names = [s.name for s in syms]
modules = ["scipy", "numpy"]
func = lambdify(syms, expr, modules=modules)

@csp.graph
def graph():
  edges = {n: DelayedEdge(ts[float], True) for n in names}
  value_ts = func(*edges.values())

  # now bind to curves
  x_ts = csp.curve(float, [(timedelta(seconds=0.1 * n), 1.0 + (1 * n)) for n in range(10)])
  y_ts = csp.curve(float, [(timedelta(seconds=0.1 * n), 2.0 + (1 * n)) for n in range(10)])
  edges["x"].bind(x_ts)
  edges["y"].bind(y_ts)
  # print the result
  csp.print("out", value_ts)

csp.show_graph(graph, graph_filename="tmp.png")
csp.run(graph, realtime=True)
2024-02-10 20:48:13.115249 out:5.0
2024-02-10 20:48:13.215249 out:13.0
2024-02-10 20:48:13.315249 out:25.0
2024-02-10 20:48:13.415249 out:41.0
2024-02-10 20:48:13.515249 out:61.0
2024-02-10 20:48:13.615249 out:85.0
2024-02-10 20:48:13.715249 out:113.0
2024-02-10 20:48:13.815249 out:145.0
2024-02-10 20:48:13.915249 out:181.0
2024-02-10 20:48:14.015249 out:221.0

tmp

Here's the generated graph for e.g. black scholes, the left print is printing the spot price, the right is printing the calculated vanilla european call option price
tmp

@timkpaine timkpaine added type: feature Issues and PRs related to new features tag: wip PRs that are a work in progress - converted to drafts labels Feb 10, 2024
@timkpaine timkpaine changed the title WIP sympy integration via ufunc dispatch [WIP] sympy integration via ufunc dispatch Feb 10, 2024
csp/impl/wiring/edge.py Outdated Show resolved Hide resolved
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Allow for only a single argument to be a numpy type, with required
modifications to max and min to support this.

Add numpy specializations - due to numpy's ufunc dispatch already
deferring to numpy functions when applicable, this does not have
a huge effect.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
csp/tests/test_baselib.py Outdated Show resolved Hide resolved
cpp/csp/cppnodes/CMakeLists.txt Outdated Show resolved Hide resolved
csp/baselib.py Outdated Show resolved Hide resolved
csp/tests/test_engine.py Show resolved Hide resolved
csp/baselib.py Show resolved Hide resolved
csp/baselib.py Outdated Show resolved Hide resolved
csp/baselib.py Outdated Show resolved Hide resolved
csp/impl/wiring/edge.py Outdated Show resolved Hide resolved
…se big dict

Signed-off-by: Tim Paine <timothy.paine@cubistsystematic.com>
…_, and __abs__ to pandas tsarray

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@timkpaine timkpaine changed the title [WIP] sympy integration via ufunc dispatch Sympy integration via ufunc dispatch, expanded numpy integration Feb 20, 2024
@timkpaine timkpaine added type: enhancement Issues and PRs related to improvements to existing features and removed tag: wip PRs that are a work in progress - converted to drafts labels Feb 20, 2024
@timkpaine timkpaine marked this pull request as ready for review February 20, 2024 21:26
Copy link
Collaborator

@AdamGlustein AdamGlustein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the comment on Edge modulus (it seems incorrectly implemented), other than that it lgtm.

csp/impl/wiring/edge.py Show resolved Hide resolved
… methods

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@timkpaine timkpaine merged commit 2cc848e into main Feb 21, 2024
14 checks passed
@timkpaine timkpaine deleted the tkp/sympy branch February 21, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Issues and PRs related to improvements to existing features type: feature Issues and PRs related to new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Accidental recursion in csp.abs Implement numpy ufunc dispatch for compatibility with sympy lambdify
2 participants