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

Easy definition of sanitized substitutions #312

Merged
merged 15 commits into from
Dec 2, 2015
Merged

Conversation

jlstevens
Copy link
Contributor

This PR aims to address issue #264.

@jlstevens jlstevens changed the title Dimension.format_string now replaced with dimension.title_format Easy definition of sanitized substitutions Dec 1, 2015
@jlstevens
Copy link
Contributor Author

I think this PR is ready to merge. Here is an example of what it allows you to do:

layout = (hv.Image(np.random.rand(10,10), 
         group=('Spectrum', 'Frequency spectrum'), 
         label=('Glucose', '$C_6H_{12}O_6$'))
+ hv.Image(np.random.rand(10,10), 
         group=('Spectrum', 'Frequency spectrum'), 
         label=('Water', '$H_2O$')))

image

Now you can select the two parts of the layout conveniently with layout.Spectrum.Glucose or layout.Spectrum.Water while showing the chemical compound names in the title (before you had to use some awkward, auto-generated sanitized string).

To make this less awkward, you can do this using the new Aliases helper function:

al = hv.util.Aliases(Spectrum='Frequency spectrum', 
                     Water='$H_2O$', 
                     Glucose='$C_6H_{12}O_6$')

(hv.Image(np.random.rand(10,10), group=al.Spectrum,  label=al.Glucose)
+ hv.Image(np.random.rand(10,10), group=al.Spectrum, label=al.Water))

Lastly, the same principles apply setting dimension names using tuples:

im =hv.Image(np.random.rand(10,10), 
             kdims=[('Lambda', '$\Lambda$'), ('Joules', 'Energy ($J$)')])

Which now makes methods such as select easier:

im.select(Lambda=(-0.2, 0.2), Joules=(-0.3, 0.3))

Of course you can use the Aliases helper here too! You can define as many of these Aliases as you desire if you wish to have 'namespaces' keeping your aliases separate (i.e one for each of groups, labels and dimension names).

After I add some tests, I think this is ready to merge and I definitely think it will help scientists (and anyone using LaTeX and special symbols really!) use HoloViews productively.

@jlstevens
Copy link
Contributor Author

I've now added unit tests (based on the examples above) and once they pass, I think it is ready to merge (unless you have any comments!).

It is worth mentioning one limitation of this new system, the group/label/dimension name stays a string (the long string, not the short alias) which means your alias definitions won't be pickled together with the object.

Aliases are defined as dictionaries (the aliases parameter) on the three global functions: hv.util.label_sanitizer, hv.util.group_sanitizer, hv.util.dimension_sanitizer. Restoring your aliases comes down to restoring these dictionaries on these three objects appropriately.

@philippjfr
Copy link
Member

Looks good, very happy to finally have a nice way to specify aliases. Since the tests are passing and I have no specific comments I'll go ahead and merge.

philippjfr added a commit that referenced this pull request Dec 2, 2015
Easy definition of sanitized substitutions
@philippjfr philippjfr merged commit af9c101 into master Dec 2, 2015
@jbednar
Copy link
Member

jbednar commented Dec 2, 2015

Excellent! Looks great.

@jlstevens jlstevens deleted the sanitized_aliases branch December 7, 2015 15:36
@jlstevens jlstevens mentioned this pull request Jan 23, 2017
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