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

Add possibility to change the display mode (atom <-> structure) #351

Merged
merged 43 commits into from
Aug 5, 2024

Conversation

sofiia-chorna
Copy link
Collaborator

@sofiia-chorna sofiia-chorna commented Jul 8, 2024

Where to play around with modes:

npm i
npm run start

Open the Chemical Shieldings example

tox -e docs
python3 -m http.server

Open examples/2-structure_map.html

TODO:

  • change in EnvironmentInfo
  • change in map
  • change in structure
  • move mode out of indexer
  • add loader
  • change UI of toggle
  • testing (played with the examples from chemiscope.org & in doc/examples)

After review:

  • UI: add titles to buttons; substruct height of buttons in the map; reset color range
  • Code: document, delete usage of settings while changing the mode; fully recreate mapOptions
  • Display errors as errors instread of warning --> throw error, change logging in separate PR
  • Add mode to the settings

Note for exporting/importing of the dataset:

  • If mode is changed and z-axes is not selected, we export NaN to JSON as z.min and z.max; hence it breaks once someone wants to import this dataset to the chemiscope. To do the import, it is necessary to delete the z-axes from the json settings (or set 0).
  • This bug to be fixed in the separate PR

@sofiia-chorna sofiia-chorna marked this pull request as ready for review July 15, 2024 13:32
@sofiia-chorna sofiia-chorna linked an issue Jul 15, 2024 that may be closed by this pull request
Copy link
Contributor

@Luthaf Luthaf left a comment

Choose a reason for hiding this comment

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

This is working very well! The one small bug I found was that the color bar range is not reset when switching mode.

I've updated the example dataset for CSD to include a read structure PCA (the previous data only contained a single PCA dimension, with the other two being 0 everywhere); you can get the updated version by removing app/example and starting the dev server again.

src/display-toggle.ts Outdated Show resolved Hide resolved
src/display-toggle.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/info/info.ts Outdated
@@ -57,8 +57,11 @@ export class EnvironmentInfo {
private _shadow: ShadowRoot;
private _root: HTMLElement;
private _atom?: Info;
private _structure: Info;
private _structure!: Info;
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this needed? It it better to not use ! as much as possible, since it can hide errors that TS would have caught.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I used ! it as _structure is initialised not directly in the constructor but in the function called in constructor -> TS complains that _structure might be undefined

src/map/map.ts Outdated Show resolved Hide resolved
src/map/map.ts Outdated Show resolved Hide resolved
src/map/options.ts Outdated Show resolved Hide resolved
src/map/options.ts Outdated Show resolved Hide resolved
src/structure/viewer.ts Outdated Show resolved Hide resolved
@ceriottm
Copy link
Contributor

had a quick look (will try to break it some more) - I know you've been talking about this already, but I noticed that it looks like settings for the structure viewers are preserved when you switch mode (e.g. if you set atoms to space filling, they remain space filling in both struc and env mode) while the map is reset. I find this very confusing.
Other quick comment: I think Environment is better than Atoms to indicate the local view mode.

@sofiia-chorna
Copy link
Collaborator Author

had a quick look (will try to break it some more) - I know you've been talking about this already, but I noticed that it looks like settings for the structure viewers are preserved when you switch mode (e.g. if you set atoms to space filling, they remain space filling in both struc and env mode) while the map is reset. I find this very confusing. Other quick comment: I think Environment is better than Atoms to indicate the local view mode.

@ceriottm
Thanks a lot for the feedback! Indeed, I could reset all settings of structure viewer too, if now it looks confusing, and rename Atoms to Environments

Copy link
Contributor

@Luthaf Luthaf left a comment

Choose a reason for hiding this comment

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

The code looks good! I like that you moved a bunch of functionalities to async, this will be nicer long term.

I'll try to break it a bit, and when @ceriottm is also done with trying to break the code we can merge this.

* @param element HTML element or HTML id of the DOM element where the toggle will be attached
* @param isPerAtom flag indicating if the atom mode should be checked
*/
constructor(element: string | HTMLElement, isPerAtom: boolean = true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of isPerAtom, why not pass the mode around here? (same in the function below)

@ceriottm
Copy link
Contributor

I tweaked the style a bit, and removed the title that seemed heavy and out of style with the rest of the UI.
I noticed a strange issue - if you fire up the website, then load the CS example and then switch back to say arginine, the space occupied by the toggler is not claimed back by the map, even though the div is removed.

Also, once we're done with the polish and the stress-testing, I think it'd be worth adding a sentence or two to the docs somewhere mentioning one can add both types of data to a json, and switch between the two.

@sofiia-chorna
Copy link
Collaborator Author

I tweaked the style a bit, and removed the title that seemed heavy and out of style with the rest of the UI. I noticed a strange issue - if you fire up the website, then load the CS example and then switch back to say arginine, the space occupied by the toggler is not claimed back by the map, even though the div is removed.

Also, once we're done with the polish and the stress-testing, I think it'd be worth adding a sentence or two to the docs somewhere mentioning one can add both types of data to a json, and switch between the two.

@ceriottm Thanks for checking! I pushed the fix for this issue you described with having the correct height back in map.

@ceriottm
Copy link
Contributor

I've one small and one big problem:

  • small problem: the fact that this is called "mode" switch is an issue because in python we use "mode" to refer to the type of viewer layout (map/structure/default). this is true in the source and in the settings (that btw still uses "atom" rather than "environment". I suggest we rename mode->target throughout
  • bigger problem: I can't seem to be able to create (or interact with) two-mode chemiscopes from jupyter notebooks. it doesn't show the toggle (which is maybe OK, we don't want to clutter too much the jupyter viewers), it doesn't respond to changing the settings "mode" field (which is confusing), and when I export what I believe should be a two-mode chemiscope json, I can't open it.

@sofiia-chorna
Copy link
Collaborator Author

  • small problem: the fact that this is called "mode" switch is an issue because in python we use "mode" to refer to the type of viewer layout (map/structure/default). this is true in the source and in the settings (that btw still uses "atom" rather than "environment". I suggest we rename mode->target throughout

@ceriottm Thanks for taking a look!

  • small issue:
    I agree that renaming mode to target would help avoid confusion.
    Regarding atom or environment, I am wondering about the consistency between this new target property (which refers to the display mode) and the target property used within properties, which can be either atom or structure.
    If we adopt settings.target with values like environment or structure, should we align the values in settings.properties as well? Specifically, should settings.properties use environment instead of atom to maintain consistency?

  • big issue: Thanks for checking it, let me take a look on the fix

@ceriottm
Copy link
Contributor

  • small issue:
    I agree that renaming mode to target would help avoid confusion.
    Regarding atom or environment, I am wondering about the consistency between this new target property (which refers to the display mode) and the target property used within properties, which can be either atom or structure.
    If we adopt settings.target with values like environment or structure, should we align the values in settings.properties as well? Specifically, should settings.properties use environment instead of atom to maintain consistency?

Ouch. I didn't realize we were using "atom". This is annoying because I'd say we don't want to lose backward compatibility over this. @Luthaf how would you see using and documenting target: "environment" but accepting also target: "atom"?

@Luthaf
Copy link
Contributor

Luthaf commented Jul 30, 2024

I'm not sure about target: environment. For most people, the atom/structure distinction is a lot clearer. The "environment" name was initially introduced for displaying atom-centered environments in the structure viewer. Unless there is a good reason to change it, I would keep the "atom" name for target everywhere.

I agree on renaming "mode" to "target", since this matches the name we already use for the properties.

@ceriottm
Copy link
Contributor

ceriottm commented Jul 30, 2024 via email

@sofiia-chorna
Copy link
Collaborator Author

@ceriottm @Luthaf
I pushed a small fix to make sure the provided settings correspond to the settings' target.
Besides, here is how I re-install the chemiscope to have the version which is in development in the jupyter notebook:

pip wheel . --no-deps --no-build-isolation --check-build-dependencies --wheel-dir {envtmpdir}/dist
pip install {envtmpdir}/dist/chemiscope-0.7.1-py3-none-any.whl --force-reinstall
jupyter notebook (or restart kernel)

(Let me know if there is a better way, please)

After this re-install, I tested with the example 2-structure_map, seems OK (the display is changing with settings.target property in jupyter).

Please let me know if we need to see the buttons in the jupyter widget.

@ceriottm
Copy link
Contributor

ceriottm commented Aug 1, 2024

After this re-install, I tested with the example 2-structure_map, seems OK (the display is changing with settings.target property in jupyter).

Please let me know if we need to see the buttons in the jupyter widget.

Do you mean it should change if one changes the settings traitlet, or the it'll display the right thing if one .shows chemiscope with the right target setting? The latter works, the traitlet seems not to

@sofiia-chorna
Copy link
Collaborator Author

Do you mean it should change if one changes the settings traitlet, or the it'll display the right thing if one .shows chemiscope with the right target setting? The latter works, the traitlet seems not to

@ceriottm
Yes I tested by setting the target directly in the settings of .show. I might not fully understand how to change the target using the traitlet then.... Could you please provide an example of how you attempted to change it?

@ceriottm
Copy link
Contributor

ceriottm commented Aug 2, 2024

chemiscope.show returns an object (which you can show with display(cs))
If you set cs.settings to a valid setting dictionary, it should (and does) automatically update stuff like map axes, coloring, style...
If you do cs.settings={"target":"atom"} it does nothing

@sofiia-chorna
Copy link
Collaborator Author

chemiscope.show returns an object (which you can show with display(cs)) If you set cs.settings to a valid setting dictionary, it should (and does) automatically update stuff like map axes, coloring, style... If you do cs.settings={"target":"atom"} it does nothing

Thanks for explaining!
It does not work for the settings.target but it seems for other settings properties neither. I tried the following:

cs = chemiscope.show(
    frames, properties, environments, settings={
   "map":{
      "x":{
         "property":"atom PCA[1]",
         "scale":"linear"
      },
   },
})

cs
==> plot with x.property = PCA[1] and scale = linear

cs.settings = {
   "map":{
      "x":{
         "property":"atom PCA[2]",
         "scale":"log"
      },
   },
}

cs
==> plot was not changed

Please let me know if I do it correctly and if it does work on your side

@ceriottm
Copy link
Contributor

ceriottm commented Aug 4, 2024

So, most likely the string was invalid, or the range giving nans (a PCA with a log scale is likely to give problems).
I think we should trigger errors in these cases, but this is probably for another PR. Let's finish this without the traitlet integration and maybe look at better traitlets support in a subsequent PR.

@ceriottm ceriottm self-requested a review August 4, 2024 10:06
Copy link
Contributor

@ceriottm ceriottm left a comment

Choose a reason for hiding this comment

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

We already had a lot of back-and-forth, and I did my own round of polish. This is good to go IMO

@ceriottm
Copy link
Contributor

ceriottm commented Aug 5, 2024

Just to say I'll merge given that it looks like this works well, and that further tweaks of the logging and/or the traitlet interface will better wait for separate PRs. #1 is fixed!

@ceriottm ceriottm merged commit 49b1182 into main Aug 5, 2024
6 checks passed
@ceriottm ceriottm deleted the change-display-mode branch August 5, 2024 21:35
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.

Switch between 'atom' and 'structure' display mode
3 participants