from k1lib.imports import *
/home/kelvin/anaconda3/envs/ray2/lib/python3.9/site-packages/scipy/__init__.py:155: UserWarning: A NumPy version >=1.18.5 and <1.25.0 is required for this version of SciPy (detected version 1.25.0 warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
2023-12-30 04:39:29,574 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: 192.168.1.19:6379...
2023-12-30 04:39:29,583 INFO worker.py:1633 -- Connected to Ray cluster. View the dashboard at 127.0.0.1:8265
Matplotlib plot renderer for sketch() cli.
# serve
default = [[["plot",[-2,-1.9,-1.8],[-0.4,-0.3,-0.2]],["title","abc"]],[["plot",[-2,-1.9,-1.8],[-0.4,-0.3,-0.2]],["title","def"]]] | aS(json.dumps)
lock = threading.Lock()
def endpoint(ctx:str=default) -> PIL.Image.Image: # returns base64 image
ctx = json.loads(ctx)
n = len(ctx); ncols = math.ceil(n**0.5); nrows = math.ceil(n/ncols)
fig, axes = plt.subplots(nrows, ncols, figsize=(ncols*5, nrows*4))
if nrows*ncols == 1: axes = [axes]
if axes | cli.shape() | cli.shape(0) > 1: axes = axes.flatten()
for i, [ax, es] in enumerate(zip(axes, ctx)):
plt.sca(ax)
for e in es:
e0 = e[0] # big switch
if e0 == "plot": ax.plot(e[1], e[2])
elif e0 == "title": ax.set_title(e[1])
elif e0 == "xscale": ax.set_xscale(e[1])
elif e0 == "yscale": ax.set_xscale(e[1])
elif e0 == "xlabel": ax.set_xlabel(e[1])
elif e0 == "ylabel": ax.set_ylabel(e[1])
elif e0 == "grid": ax.grid(e[1])
elif e0 == "legend":
kws = {}
if len(e) >= 2: kws["framealpha"] = e[2]
if e[1]: ax.legend(e[1], **kws)
else: ax.legend(**kws)
axes[i+1:] | cli.op().remove().all() | cli.deref(); plt.tight_layout()
with lock: im = fig | cli.toImg()
return im; #return im | cli.toBytes() | aS(base64.b64encode) | op().decode()
# doc = kapi.embed.__init__.__doc__.replace("\n", "<br>\n"); endpoint.__doc__ = f"<pre>{doc}</pre>"
#assert endpoint(dill.dumps(["123", "456", "789"])) | aS(dill.loads) | shape(0) == 3
endpoint().encode() | aS(base64.b64decode) | toImg()
x = np.linspace(-2, 2); y = x**2
jsF = [[x, y]] | (toJsFunc() | (sketch() | ~aS(plt.plot))); jsF.interface()
jsF
const _jsD_786_9_1703929171 = [[[-2.0, -1.9183673469387754, -1.836734693877551, -1.7551020408163265, -1.6734693877551021, -1.5918367346938775, -1.510204081632653, -1.4285714285714286, -1.3469387755102042, -1.2653061224489797, -1.183673469387755, -1.1020408163265307, -1.0204081632653061, -0.9387755102040818, -0.8571428571428572, -0.7755102040816328, -0.6938775510204083, -0.6122448979591837, -0.5306122448979593, -0.44897959183673475, -0.3673469387755104, -0.2857142857142858, -0.20408163265306145, -0.12244897959183687, -0.04081632653061229, 0.04081632653061229, 0.12244897959183643, 0.204081632653061, 0.2857142857142856, 0.36734693877551017, 0.4489795918367343, 0.5306122448979589, 0.6122448979591835, 0.693877551020408, 0.7755102040816326, 0.8571428571428568, 0.9387755102040813, 1.020408163265306, 1.1020408163265305, 1.1836734693877546, 1.2653061224489792, 1.3469387755102038, 1.4285714285714284, 1.510204081632653, 1.591836734693877, 1.6734693877551017, 1.7551020408163263, 1.8367346938775508, 1.9183673469387754, 2.0], [4.0, 3.680133277800916, 3.3735943356934612, 3.080383173677634, 2.8004997917534364, 2.533944189920866, 2.2807163681799247, 2.0408163265306123, 1.8142440649729283, 1.6009995835068722, 1.4010828821324448, 1.2144939608496463, 1.0412328196584757, 0.8812994585589341, 0.7346938775510206, 0.6014160766347358, 0.4814660558100793, 0.3748438150770512, 0.281549354435652, 0.20158267388588094, 0.13494377342773858, 0.08163265306122454, 0.04164931278633912, 0.014993752603082082, 0.0016659725114535648, 0.0016659725114535648, 0.014993752603081974, 0.04164931278633894, 0.08163265306122441, 0.13494377342773842, 0.20158267388588053, 0.2815493544356515, 0.37484381507705095, 0.48146605581007895, 0.6014160766347355, 0.7346938775510198, 0.8812994585589332, 1.0412328196584753, 1.2144939608496457, 1.4010828821324437, 1.600999583506871, 1.8142440649729272, 2.040816326530612, 2.2807163681799247, 2.533944189920865, 2.8004997917534347, 3.0803831736776335, 3.3735943356934603, 3.680133277800916, 4.0]]];
async function _jsF_574_5_1703929171() {
let _jsD_786_3 = null;
const _jsF_574_4 = (_jsD_786_7, _jsD_786_8=null) => {
if (!_jsD_786_8) { // handle only xIdx is available case
_jsD_786_8 = _jsD_786_7; _jsD_786_7 = [...Array(_jsD_786_8.length).keys()];
}
_jsD_786_3.push(["plot", _jsD_786_7, _jsD_786_8]);
}
const _jsF_574_3 = (_jsD_786_5) => { return _jsD_786_5.aSInv(_jsF_574_4); }
const _jsF_574_2 = (_jsD_786_4) => { return _jsF_574_3(_jsD_786_4); };
const _jsF_574_1 = async (_jsD_786_2) => { // dataIdx should have
const ctx = []; // this is the object that will be sent to the rendering server!
const titles = null ?? Array(_jsD_786_2.length);
for (const i of [...Array(_jsD_786_2.length).keys()]) {
_jsD_786_3 = [];
// actually executing function and plotting function downstream
_jsF_574_2(_jsD_786_2[i]);
if (titles[i]) _jsD_786_3.push(["title", titles[i]]);
// inject all transforms here
;
ctx.push(_jsD_786_3); _jsD_786_3 = null;
}
// console.log(ctx);
// console.log(JSON.stringify(ctx));
const res = await (await fetch("https://local.mlexps.com/routeServer/kapi_9-mpl", {
method: "POST",
body: JSON.stringify({ "ctx": JSON.stringify(ctx) }),
headers: { "Content-Type": "application/json" }
})).json()
if (res.success) {
const base64 = res.data;
console.log("mpl fetched");
return `<img src="data:image/jpg;base64, ${base64}" />`
} else { throw new Error(res.reason); }
// return ctx;
}
const _jsF_574_0 = async (_jsD_786_1) => { return await _jsF_574_1(_jsD_786_1); };
return await _jsF_574_0(_jsD_786_9_1703929171);
}
x = np.linspace(-2, 2); y = x**2
jsF = [[x, y]] | (toJsFunc() | (sketch([op().grid(True), op().title("abc")]) | ~aS(plt.plot))); jsF.interface()
jsF
const _jsD_786_21_1703929171 = [[[-2.0, -1.9183673469387754, -1.836734693877551, -1.7551020408163265, -1.6734693877551021, -1.5918367346938775, -1.510204081632653, -1.4285714285714286, -1.3469387755102042, -1.2653061224489797, -1.183673469387755, -1.1020408163265307, -1.0204081632653061, -0.9387755102040818, -0.8571428571428572, -0.7755102040816328, -0.6938775510204083, -0.6122448979591837, -0.5306122448979593, -0.44897959183673475, -0.3673469387755104, -0.2857142857142858, -0.20408163265306145, -0.12244897959183687, -0.04081632653061229, 0.04081632653061229, 0.12244897959183643, 0.204081632653061, 0.2857142857142856, 0.36734693877551017, 0.4489795918367343, 0.5306122448979589, 0.6122448979591835, 0.693877551020408, 0.7755102040816326, 0.8571428571428568, 0.9387755102040813, 1.020408163265306, 1.1020408163265305, 1.1836734693877546, 1.2653061224489792, 1.3469387755102038, 1.4285714285714284, 1.510204081632653, 1.591836734693877, 1.6734693877551017, 1.7551020408163263, 1.8367346938775508, 1.9183673469387754, 2.0], [4.0, 3.680133277800916, 3.3735943356934612, 3.080383173677634, 2.8004997917534364, 2.533944189920866, 2.2807163681799247, 2.0408163265306123, 1.8142440649729283, 1.6009995835068722, 1.4010828821324448, 1.2144939608496463, 1.0412328196584757, 0.8812994585589341, 0.7346938775510206, 0.6014160766347358, 0.4814660558100793, 0.3748438150770512, 0.281549354435652, 0.20158267388588094, 0.13494377342773858, 0.08163265306122454, 0.04164931278633912, 0.014993752603082082, 0.0016659725114535648, 0.0016659725114535648, 0.014993752603081974, 0.04164931278633894, 0.08163265306122441, 0.13494377342773842, 0.20158267388588053, 0.2815493544356515, 0.37484381507705095, 0.48146605581007895, 0.6014160766347355, 0.7346938775510198, 0.8812994585589332, 1.0412328196584753, 1.2144939608496457, 1.4010828821324437, 1.600999583506871, 1.8142440649729272, 2.040816326530612, 2.2807163681799247, 2.533944189920865, 2.8004997917534347, 3.0803831736776335, 3.3735943356934603, 3.680133277800916, 4.0]]];
async function _jsF_574_12_1703929171() {
let _jsD_786_14 = null;
const _jsF_574_11 = (_jsD_786_18, _jsD_786_19=null) => {
if (!_jsD_786_19) { // handle only xIdx is available case
_jsD_786_19 = _jsD_786_18; _jsD_786_18 = [...Array(_jsD_786_19.length).keys()];
}
_jsD_786_14.push(["plot", _jsD_786_18, _jsD_786_19]);
}
const _jsF_574_10 = (_jsD_786_16) => { return _jsD_786_16.aSInv(_jsF_574_11); }
const _jsF_574_9 = (_jsD_786_15) => { return _jsF_574_10(_jsD_786_15); };
const _jsF_574_8 = async (_jsD_786_13) => { // dataIdx should have
const ctx = []; // this is the object that will be sent to the rendering server!
const titles = null ?? Array(_jsD_786_13.length);
for (const i of [...Array(_jsD_786_13.length).keys()]) {
_jsD_786_14 = [];
// actually executing function and plotting function downstream
_jsF_574_9(_jsD_786_13[i]);
if (titles[i]) _jsD_786_14.push(["title", titles[i]]);
// inject all transforms here
_jsD_786_14.push(["grid", true]); _jsD_786_14.push(["title", `abc`]);
ctx.push(_jsD_786_14); _jsD_786_14 = null;
}
// console.log(ctx);
// console.log(JSON.stringify(ctx));
const res = await (await fetch("https://local.mlexps.com/routeServer/kapi_9-mpl", {
method: "POST",
body: JSON.stringify({ "ctx": JSON.stringify(ctx) }),
headers: { "Content-Type": "application/json" }
})).json()
if (res.success) {
const base64 = res.data;
console.log("mpl fetched");
return `<img src="data:image/jpg;base64, ${base64}" />`
} else { throw new Error(res.reason); }
// return ctx;
}
const _jsF_574_7 = async (_jsD_786_12) => { return await _jsF_574_8(_jsD_786_12); };
return await _jsF_574_7(_jsD_786_21_1703929171);
}
x = np.linspace(-2, 2); y = x**2
jsF = [[[x, y], True, "abc"]] | (toJsFunc() | (sketch() | ~aS(plt.plot) + aS(plt.grid) + aS(plt.title))); jsF.interface()
jsF
const _jsD_786_37_1703929171 = [[[[-2.0, -1.9183673469387754, -1.836734693877551, -1.7551020408163265, -1.6734693877551021, -1.5918367346938775, -1.510204081632653, -1.4285714285714286, -1.3469387755102042, -1.2653061224489797, -1.183673469387755, -1.1020408163265307, -1.0204081632653061, -0.9387755102040818, -0.8571428571428572, -0.7755102040816328, -0.6938775510204083, -0.6122448979591837, -0.5306122448979593, -0.44897959183673475, -0.3673469387755104, -0.2857142857142858, -0.20408163265306145, -0.12244897959183687, -0.04081632653061229, 0.04081632653061229, 0.12244897959183643, 0.204081632653061, 0.2857142857142856, 0.36734693877551017, 0.4489795918367343, 0.5306122448979589, 0.6122448979591835, 0.693877551020408, 0.7755102040816326, 0.8571428571428568, 0.9387755102040813, 1.020408163265306, 1.1020408163265305, 1.1836734693877546, 1.2653061224489792, 1.3469387755102038, 1.4285714285714284, 1.510204081632653, 1.591836734693877, 1.6734693877551017, 1.7551020408163263, 1.8367346938775508, 1.9183673469387754, 2.0], [4.0, 3.680133277800916, 3.3735943356934612, 3.080383173677634, 2.8004997917534364, 2.533944189920866, 2.2807163681799247, 2.0408163265306123, 1.8142440649729283, 1.6009995835068722, 1.4010828821324448, 1.2144939608496463, 1.0412328196584757, 0.8812994585589341, 0.7346938775510206, 0.6014160766347358, 0.4814660558100793, 0.3748438150770512, 0.281549354435652, 0.20158267388588094, 0.13494377342773858, 0.08163265306122454, 0.04164931278633912, 0.014993752603082082, 0.0016659725114535648, 0.0016659725114535648, 0.014993752603081974, 0.04164931278633894, 0.08163265306122441, 0.13494377342773842, 0.20158267388588053, 0.2815493544356515, 0.37484381507705095, 0.48146605581007895, 0.6014160766347355, 0.7346938775510198, 0.8812994585589332, 1.0412328196584753, 1.2144939608496457, 1.4010828821324437, 1.600999583506871, 1.8142440649729272, 2.040816326530612, 2.2807163681799247, 2.533944189920865, 2.8004997917534347, 3.0803831736776335, 3.3735943356934603, 3.680133277800916, 4.0]], true, "abc"]];
async function _jsF_574_24_1703929171() {
let _jsD_786_24 = null;
const _jsF_574_19 = (_jsD_786_29, _jsD_786_30=null) => {
if (!_jsD_786_30) { // handle only xIdx is available case
_jsD_786_30 = _jsD_786_29; _jsD_786_29 = [...Array(_jsD_786_30.length).keys()];
}
_jsD_786_24.push(["plot", _jsD_786_29, _jsD_786_30]);
}
const _jsF_574_18 = (_jsD_786_27) => { return _jsD_786_27.aSInv(_jsF_574_19); }
const _jsF_574_21 = (_jsD_786_33) => { _jsD_786_24.push(["grid", _jsD_786_33]); }
const _jsF_574_23 = (_jsD_786_36) => { _jsD_786_24.push(["title", _jsD_786_36]); }
const _jsF_574_17 = (_jsD_786_26) => [_jsF_574_18(_jsD_786_26[0]), _jsF_574_21(_jsD_786_26[1]), _jsF_574_23(_jsD_786_26[2])];
const _jsF_574_16 = (_jsD_786_25) => { return _jsF_574_17(_jsD_786_25); };
const _jsF_574_15 = async (_jsD_786_23) => { // dataIdx should have
const ctx = []; // this is the object that will be sent to the rendering server!
const titles = null ?? Array(_jsD_786_23.length);
for (const i of [...Array(_jsD_786_23.length).keys()]) {
_jsD_786_24 = [];
// actually executing function and plotting function downstream
_jsF_574_16(_jsD_786_23[i]);
if (titles[i]) _jsD_786_24.push(["title", titles[i]]);
// inject all transforms here
;
ctx.push(_jsD_786_24); _jsD_786_24 = null;
}
// console.log(ctx);
// console.log(JSON.stringify(ctx));
const res = await (await fetch("https://local.mlexps.com/routeServer/kapi_9-mpl", {
method: "POST",
body: JSON.stringify({ "ctx": JSON.stringify(ctx) }),
headers: { "Content-Type": "application/json" }
})).json()
if (res.success) {
const base64 = res.data;
console.log("mpl fetched");
return `<img src="data:image/jpg;base64, ${base64}" />`
} else { throw new Error(res.reason); }
// return ctx;
}
const _jsF_574_14 = async (_jsD_786_22) => { return await _jsF_574_15(_jsD_786_22); };
return await _jsF_574_14(_jsD_786_37_1703929171);
}
x = np.linspace(-2, 2); y = x**2
jsF = [2, 3, 4, 5] | apply(lambda e: [x, x**e]) | wrapList() | (toJsFunc() | (sketch() | ~apply(plt.plot))); jsF.interface()
a = np.linspace(1.2, 5, 20) | batched(4) | T(); a
array([[1.2, 2. , 2.8, 3.6, 4.4], [1.4, 2.2, 3. , 3.8, 4.6], [1.6, 2.4, 3.2, 4. , 4.8], [1.8, 2.6, 3.4, 4.2, 5. ]])
# thumbnail
x = np.linspace(0.1, 2)
a | aS(lambda e: [e, [x, x**e]]).all(2) | (toJsFunc(("grid", bool, True), ("tail", range(50), 50)) | (sketch([op().grid("grid")]) | (cut(1) | tail("tail").all(2) | ~apply(plt.plot)) & (cut(0) | apply('f"x^{round(x, 2)}"') | deref() | aS(plt.legend)))) | op().interface()