Spaces:
Sleeping
Sleeping
provide "context" with a shared param
Browse files- core.py +14 -3
- interactivity.py +4 -1
core.py
CHANGED
|
@@ -10,7 +10,12 @@ from interactive_pipe import Curve, SingleCurve
|
|
| 10 |
def gen_color(
|
| 11 |
frequency: int = 0, # discrete slider (int)
|
| 12 |
isotropy: float = 0., # continuous slider (float)
|
|
|
|
| 13 |
) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
lin_coord = np.linspace(0, 1., 256)
|
| 15 |
X, Y = np.meshgrid(lin_coord, isotropy*lin_coord)
|
| 16 |
radius = 0.5+0.5*np.cos(frequency*np.sqrt(X**2 + Y**2))
|
|
@@ -47,14 +52,20 @@ def compare_by_splitting(
|
|
| 47 |
return out
|
| 48 |
|
| 49 |
|
| 50 |
-
def extract_profile(img: np.ndarray,) -> Curve:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
luma = img.mean(axis=-1)
|
| 52 |
-
h_profile = SingleCurve(
|
|
|
|
| 53 |
v_profile = SingleCurve(y=luma[:, 0], label="V profile")
|
| 54 |
return Curve(
|
| 55 |
[h_profile, v_profile],
|
| 56 |
xlabel="Position", ylabel="Luminance",
|
| 57 |
-
grid=True
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
# -------------------
|
|
|
|
| 10 |
def gen_color(
|
| 11 |
frequency: int = 0, # discrete slider (int)
|
| 12 |
isotropy: float = 0., # continuous slider (float)
|
| 13 |
+
context={} # global parameters (dict)
|
| 14 |
) -> np.ndarray:
|
| 15 |
+
# context allows sharing information between processing blocks.
|
| 16 |
+
# Let's share the frequency value for further use.
|
| 17 |
+
context["freq"] = frequency
|
| 18 |
+
print(context.get("initialized_param", "Wrongly initialized context!"))
|
| 19 |
lin_coord = np.linspace(0, 1., 256)
|
| 20 |
X, Y = np.meshgrid(lin_coord, isotropy*lin_coord)
|
| 21 |
radius = 0.5+0.5*np.cos(frequency*np.sqrt(X**2 + Y**2))
|
|
|
|
| 52 |
return out
|
| 53 |
|
| 54 |
|
| 55 |
+
def extract_profile(img: np.ndarray, context={}) -> Curve:
|
| 56 |
+
# context allows sharing information between processing blocks
|
| 57 |
+
frequency = context.get('freq', 0)
|
| 58 |
+
init_shared_param = context.get(
|
| 59 |
+
'initialized_param', "Wrongly initialized context!")
|
| 60 |
luma = img.mean(axis=-1)
|
| 61 |
+
h_profile = SingleCurve(
|
| 62 |
+
y=luma[0, :], label=f"H profile f={frequency}")
|
| 63 |
v_profile = SingleCurve(y=luma[:, 0], label="V profile")
|
| 64 |
return Curve(
|
| 65 |
[h_profile, v_profile],
|
| 66 |
xlabel="Position", ylabel="Luminance",
|
| 67 |
+
grid=True,
|
| 68 |
+
title=f"Luminance profiles\n(global context parameter {init_shared_param})"
|
| 69 |
)
|
| 70 |
|
| 71 |
# -------------------
|
interactivity.py
CHANGED
|
@@ -31,7 +31,10 @@ def run_interactive_pipeline(
|
|
| 31 |
playable_tutorial_pipeline = interactive_pipeline(
|
| 32 |
gui=backend,
|
| 33 |
cache=True,
|
| 34 |
-
markdown_description=markdown_description
|
|
|
|
|
|
|
|
|
|
| 35 |
)(tutorial_pipeline)
|
| 36 |
playable_tutorial_pipeline()
|
| 37 |
|
|
|
|
| 31 |
playable_tutorial_pipeline = interactive_pipeline(
|
| 32 |
gui=backend,
|
| 33 |
cache=True,
|
| 34 |
+
markdown_description=markdown_description,
|
| 35 |
+
context={"initialized_param": 42}
|
| 36 |
+
# context is shared between processing blocks.
|
| 37 |
+
# Initialization allow to pre-load a neural network for example.
|
| 38 |
)(tutorial_pipeline)
|
| 39 |
playable_tutorial_pipeline()
|
| 40 |
|