numpy operations with multiple arrays #493
-
Dear community, I'm having a difficult time to perform numpy operations with 2 csp.ts[np.ndarray] involved. I tried 2 versions of clip function. I set dn_limit=csp.stats.quant(x, quant=0.01) to use the 1% quantile of the past data to winsorize. For either version, the error is shown below. What's the recommended way to do such operation?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
I just copy-pasted your code and both solutions seem to work for me fine on 3.11.11. I am on csp 0.8.0 by the way import csp
from typing import Union, Any
import numpy as np
from datetime import datetime, timedelta
# 1)
@csp.node
def clip(x: csp.ts[Union[float, np.ndarray]],
up_limit: Union[float, csp.ts[np.ndarray], Any] = None,
dn_limit: Union[float, csp.ts[np.ndarray], Any] = None) -> csp.ts[np.ndarray]:
return np.clip(x, dn_limit, up_limit)
# 2)
@csp.graph
def clip(x: csp.ts[Union[float, np.ndarray]],
up_limit: Union[float, csp.ts[np.ndarray], Any] = None,
dn_limit: Union[float, csp.ts[np.ndarray], Any] = None) -> csp.ts[np.ndarray]:
return csp.apply(x, lambda u: np.minimum(np.maximum(u, dn_limit), up_limit), np.ndarray)
@csp.graph
def g() -> csp.ts[np.ndarray]:
x = csp.curve(np.ndarray, [(datetime(2020,1,1) + timedelta(seconds=i), np.array([float(i), float(i)])) for i in range(10)])
return clip(x, up_limit=7.0, dn_limit=2.0)
res = csp.run(g, starttime=datetime(2020,1,1), endtime=timedelta(seconds=10))
print(res) gives:
|
Beta Was this translation helpful? Give feedback.
-
Ok thanks for the repro. When I run off However, I see another bug with the
But, if I change the annotation to not be a Union and directly take the array, which really shouldn't change the behaviour at all, it works fine. @csp.node
def clip_v1(x: csp.ts[Union[float, np.ndarray]],
up_limit: csp.ts[np.ndarray] = None,
dn_limit: csp.ts[np.ndarray] = None) -> csp.ts[np.ndarray]:
return np.clip(x, dn_limit, up_limit) Gives:
Also, fyi, What I'd recommend for now is changing the annotation to not use the Union type and using |
Beta Was this translation helpful? Give feedback.
-
Closing this as resolved as we moved the bug report to #495 |
Beta Was this translation helpful? Give feedback.
If you just remove the Union annotation, it works off of the current
0.8.0
release. The bug is with the handling of the type annotation only.