-
Notifications
You must be signed in to change notification settings - Fork 795
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
serialization of expr in parameter #2573
Comments
Hi @mattijn, in the lines you identified in the other PR, if we add an if self.param.expr is Undefined:
return {"expr": self.name}
elif isinstance(self.param.expr, str):
return {"expr": self.param.expr}
else:
return {"expr": repr(self.param.expr)} that seems to fix the issue. If I understand correctly, the main issue was This change produces "thetaOffset": {
"expr": "-PI/length(data('source_wind'))"
} in the serialization. Are you confident that the following would be preferred? "thetaOffset": {
"expr": "parameter027"
} |
I think my question about which "thetaOffset": {
"expr": "parameter027"
} then the |
I think I prefer: theta_shift_as_par = alt.parameter(expr=(-PI/length(data('source_wind'))))
theta_shift_as_expr = alt.expr(-PI/length(data('source_wind'))) # not yet possible
chart = alt.Chart(source_wind).mark_arc(
tooltip=True,
thetaOffset=theta_shift_as_par # serialize to parameter name
thetaOffset=theta_shift_as_expr # serialize directly to expr
).encode(
theta='winddirection:N',
color='label:N'
).add_parameter(theta_shift_as_par)
chart#.to_dict() |
The |
Could you post an example of how you make the above parameter using |
import altair as alt
from altair.expr import PI, length, data
data_wind = [
{'winddirection': 0, 'label': 'North'},
{'winddirection': 90, 'label': 'East'},
{'winddirection': 180, 'label': 'South'},
{'winddirection': 270, 'label': 'West'},
]
source_wind = alt.DataSource(alt.InlineData(values=data_wind, name='source_wind'))
theta_shift_as_par = alt.parameter(expr=-PI/length(data('source_wind')))
#theta_shift_as_expr = alt.expr(-PI/length(data('source_wind')))
chart = alt.Chart(source_wind).mark_arc(
tooltip=True,
thetaOffset=theta_shift_as_par # should serialize to parameter name?
#thetaOffset=theta_shift_as_expr # should serialize directly to expr?
).encode(
theta='winddirection:N',
color='label:N'
)#.add_parameter(theta_shift_as_par) # when wanted as parameter name
chart.to_dict()
https://colab.research.google.com/drive/1-jFoG2BOYVUnMzAhKhaLQLTU6dFpeDWI?usp=sharing |
@jakevdp do you agree with @mattijn's suggestion, that import altair as alt
from altair.expr import PI, length, data
theta_shift_as_par = alt.parameter(expr=(-PI/length(data('source_wind'))))
theta_shift_as_expr = alt.expr(-PI/length(data('source_wind'))) # not yet possible
chart = alt.Chart(source_wind).mark_arc(
tooltip=True,
thetaOffset=theta_shift_as_par # serialize to parameter name
thetaOffset=theta_shift_as_expr # serialize directly to expr
).encode(
theta='winddirection:N',
color='label:N'
).add_parameter(theta_shift_as_par)
chart#.to_dict() |
Maybe @domoritz can shed a light on this? What is the proper way to adopt for altair? Will |
I'm not following this whole conversation but here is my take (let me know if you want to to elaborate on anything). We plan to support and keep supporting
{
"data": {"url": "data/cars.json"},
"vconcat": [{
"params": [{"name": "brush", "select": "interval"}],
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Miles_per_Gallon", "type": "quantitative"}
}
}, {
"transform": [
{"filter": {"param": "brush"}}
],
"mark": "point",
"encoding": {
"x": {
"field": "Acceleration", "type": "quantitative",
"scale": {"domain": [0,25]}
},
"y": {
"field": "Displacement", "type": "quantitative",
"scale": {"domain": [0, 500]}
}
}
}]
} |
I'm a little stuck with how to proceed @mattijn @jakevdp @joelostblom Say we define a parameter like @mattijn did above theta_shift = alt.parameter(expr=(-PI/length(data('source_wind')))) There is currently no real difference between that object in Altair and an object like The good thing about the current setup is that Should we make it so that I am also tempted by the two-line temporary solution that I mentioned above #2573 (comment) which would get @mattijn's original chart working (and doesn't seem to break anything that currently works). I'm happy for any suggestions! |
Thanks @domoritz, its a bit of a semantic discussion. The parts you mentioned are clear. The issue here is about the
I think if 1 is the case it always should be referred by the parameter name (not happening at the moment in altair). For 2, if |
I mentioned before that this was not possible: expr_ref_type = alt.expr(-PI/length(data('source_wind'))) But I just found out that within VL this is called an expr_ref_type = alt.ExprRef(-PI/length(data('source_wind')))
expr_ref_type
So one can currently use an expression in-line, without defining an expression within a parameter as such: import altair as alt
from altair.expr import PI, length, data
data_wind = [
{'winddirection': 0, 'label': 'North'},
{'winddirection': 90, 'label': 'East'},
{'winddirection': 180, 'label': 'South'},
{'winddirection': 270, 'label': 'West'},
]
source_wind = alt.DataSource(alt.InlineData(values=data_wind, name='source_wind'))
expr_ref_str = alt.ExprRef("-PI/length(data('source_wind'))")
expr_ref_type = alt.ExprRef(-PI/length(data('source_wind')))
chart = alt.Chart(source_wind).mark_arc(
tooltip=True,
thetaOffset=expr_ref_type # <--
).encode(
theta='winddirection:N',
color='label:N'
)
chart.to_dict()
But Not sure if we can or should introduce a shortcut to create an |
As is mentioned here: #2528 (comment). I've the feeling when an
expr
is defined within aparameter
it is serialized into a debatable VL-spec.Current behavior also leads to not needing the
add_parameter
, when anexpr
is defined, as mentioned here: #2528 (comment).BTW, in VL one can write:
"thetaOffset": {"expr": "-PI/length(data('MY_DATA'))"}
, but in Altair one can not writealt.expr(-PI/length(data('source_wind')))
.Open the Chart in the Vega Editor
The text was updated successfully, but these errors were encountered: