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

Color the 3D structure by a property #303

Merged
merged 15 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions python/examples/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Atom property coloring in chemiscope
====================================

This example demonstrates how to color atoms based on scalar
properties.

Note that the same parameters can be used with `chemiscope.show`
to visualize an interactive widget in a Jupyter notebook.
"""

import ase.io
import chemiscope
import numpy as np

# loads a dataset of structures
frames = ase.io.read("data/alpha-mu.xyz", ":")

# converts the arrays from the format they are stored in to an array
# format that can be processed by the ASE utilities
for a in frames:
a.arrays["polarizability"] = np.array(
[
(axx + ayy + azz) / 3
for (axx, ayy, azz) in zip(
a.arrays["axx"], a.arrays["ayy"], a.arrays["azz"]
)
]
)

# one possible measure of anisotropy...
a.arrays["alpha_eigenvalues"] = np.array(
[
np.linalg.eigvalsh([[axx, axy, axz], [axy, ayy, ayz], [axz, ayz, azz]])
for (axx, ayy, azz, axy, axz, ayz) in zip(
a.arrays["axx"],
a.arrays["ayy"],
a.arrays["azz"],
a.arrays["axy"],
a.arrays["axz"],
a.arrays["ayz"],
)
]
)

a.arrays["anisotropy"] = (
a.arrays["alpha_eigenvalues"][:, 2] - a.arrays["alpha_eigenvalues"][:, 0]
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this in the "atomic coloring" example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what you mean by that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, I thought it was better to avoid adding new data files, but we need scalar properties to display.
I'll add a comment so it's clear it's not like one needs to do this in order to use the coloring.


chemiscope.write_input(
"colors-example.json.gz",
frames=frames,
properties=chemiscope.extract_properties(
frames, only=["polarizability", "anisotropy", "alpha_eigenvalues"]
),
settings={ # the write_input function also allows defining the default visualization settings
"map": {
"x": {"property": "alpha_eigenvalues[1]"},
"y": {"property": "alpha_eigenvalues[2]"},
"z": {"property": "alpha_eigenvalues[3]"},
"palette": "seismic",
"color": {"property": "anisotropy"},
},
"structure": [
{
"spaceFilling": False,
"atomLabels": False,
"atoms": True,
"axes": "off",
"keepOrientation": False,
"playbackDelay": 700,
"environments": {
"activated": True,
"bgColor": "CPK",
"bgStyle": "licorice",
"center": False,
"cutoff": 4,
},
"color": {"property": "anisotropy", "min": 1, "max": 15},
}
],
},
environments=chemiscope.all_atomic_environments(frames),
)
6 changes: 3 additions & 3 deletions src/structure/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class StructureOptions extends OptionsGroup {
min: new HTMLOption('number', 0),
max: new HTMLOption('number', 0),
mode: new HTMLOption('string', 'linear'),
palette: new HTMLOption('string', 'Rwb'),
palette: new HTMLOption('string', 'bwr'),
};

// Handling undefined propertiesName:
Expand All @@ -131,7 +131,7 @@ export class StructureOptions extends OptionsGroup {
);
}
this.color.mode.validate = optionValidator(['linear', 'log', 'sqrt', 'inverse'], 'mode');
this.color.palette.validate = optionValidator(['Rwb', 'Roygb', 'Sinebow'], 'palette');
this.color.palette.validate = optionValidator(['bwr', 'Roygb', 'Sinebow'], 'palette');
Luthaf marked this conversation as resolved.
Show resolved Hide resolved

this.environments.bgColor.validate = optionValidator(
['grey', 'CPK', 'prop'],
Expand Down Expand Up @@ -299,7 +299,7 @@ export class StructureOptions extends OptionsGroup {
// ======= color palette
const selectPalette = this.getModalElement<HTMLSelectElement>('atom-color-palette');
selectPalette.options.length = 0;
for (const key of ['Rwb', 'Roygb', 'Sinebow']) {
for (const key of ['bwr', 'Roygb', 'Sinebow']) {
selectPalette.options.add(new Option(key, key));
}
this.color.palette.bind(selectPalette, 'value');
Expand Down
2 changes: 1 addition & 1 deletion src/structure/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ export class MoleculeViewer {

let grad: $3Dmol.Gradient = new $3Dmol.Gradient.RWB(max, min);

if (this._options.color.palette.value === 'Rwb') {
if (this._options.color.palette.value === 'bwr') {
// min and max are swapped to ensure red is used for high values, blue for low values
grad = new $3Dmol.Gradient.RWB(max, min);
} else if (this._options.color.palette.value === 'Roygb') {
Expand Down