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

Serialization of expr #2591

Closed
wants to merge 2 commits into from
Closed

Serialization of expr #2591

wants to merge 2 commits into from

Conversation

ChristopherDavisUCI
Copy link
Contributor

This is an attempt to resolve the issues @mattijn raised in #2573. Now an expression like theta_shift+5 does not yield a Parameter but instead a new class ParameterExpression. If we have something defined like x = theta_shift+5, we can't use something like add_parameter(x), because x is not a Parameter, but if we have used add_parameter(theta_shift), then we can use x within the chart construction.

Please let me know any comments!

Defines a ParameterExpression class so that an expression like `theta_shift+5` no longer yields a Parameter.  `theta_shift+5` can still be used in a chart, provided that `theta_shift` has been added to the chart.
@mattijn
Copy link
Contributor

mattijn commented Apr 16, 2022

Yes this works logically. I test a parameter expression as string and typed (through altair.expr.*):

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'))

par_expr_str = alt.parameter(expr="-PI/length(data('source_wind'))")
par_expr_type = alt.parameter(expr=-PI/length(data('source_wind')))

chart = alt.Chart(source_wind).mark_arc(
    tooltip=True,
    thetaOffset=par_expr_type + par_expr_str
).encode(
    theta='winddirection:N',
    color='label:N'
).add_parameter(par_expr_str, par_expr_type)
chart.to_dict()
{'$schema': 'https://vega.github.io/schema/vega-lite/v5.2.0.json',
 'config': {'view': {'continuousHeight': 300, 'continuousWidth': 400}},
 'data': {'name': 'source_wind',
  'values': [{'label': 'North', 'winddirection': 0},
   {'label': 'East', 'winddirection': 90},
   {'label': 'South', 'winddirection': 180},
   {'label': 'West', 'winddirection': 270}]},
 'encoding': {'color': {'field': 'label', 'type': 'nominal'},
  'theta': {'field': 'winddirection', 'type': 'nominal'}},
 'mark': {'thetaOffset': {'expr': '(parameter002 + parameter001)'},
  'tooltip': True,
  'type': 'arc'},
 'params': [{'expr': "-PI/length(data('source_wind'))",
   'name': 'parameter001'},
  {'expr': "((-PI) / length(data('source_wind')))", 'name': 'parameter002'}]}

Both are added as an unique parameter within the params and are added together by parameter name within the ExprRef. Nice!

@ChristopherDavisUCI
Copy link
Contributor Author

Thank you for looking this over @mattijn!

@jakevdp I think this portion is finished, but I'm always happy for any suggestions.

@joelostblom
Copy link
Contributor

@ChristopherDavisUCI Could you remind me what was the reason for closing this PR? Was it superseded by one of the others you opened? I just want to make sure it wasn't closed accidentally.

@ChristopherDavisUCI
Copy link
Contributor Author

Hi @joelostblom, hmm, I honestly don't remember. Is there any easy way to tell what date it was closed? It might help to know what I was working on at the time. (Was it definitely closed by me?) I checked @mattijn's example from #2591 (comment) and in my current version it is not working the way @mattijn reported above. In Mattijn's version we have {'thetaOffset': {'expr': '(parameter002 + parameter001)'} and in my current version we have {'expr': "(((-PI) / length(data('source_wind'))) + -PI/length(data('source_wind')))"}.

I will try to think some more.

@joelostblom
Copy link
Contributor

It looks like you might have deleted the repository that the PR was opened from. When I hover over the repo name above that is what it tells me:

image

Because it was deleted that way, I don't think there is a way to know when it happened. But it does seem like the changes are still there in the files tab so maybe you can create a new branch on your current repo, copy in the changes and open a new PR?

@ChristopherDavisUCI
Copy link
Contributor Author

Thanks @joelostblom! I think I didn't realize I was closing this PR when I deleted that repository. I will try to re-create these changes.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants