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

Set initial slider position in Hyperslicer #276

Open
EvdWee opened this issue May 1, 2023 · 3 comments
Open

Set initial slider position in Hyperslicer #276

EvdWee opened this issue May 1, 2023 · 3 comments
Labels

Comments

@EvdWee
Copy link

EvdWee commented May 1, 2023

Hello,

I am using hyperslicer to visualize an image stack (3D array) and navigate through the stack. This works nicely, but I would like to set the initial position of the slider to a specific value, not just the first image in the stack.

How to do this with the hyperslicer() function? My code looks something like this:

fig = plt.figure(figsize=(9,9))

control1 = hyperslicer(array,vmin=np.min(array), vmax=np.max(array))

plt.show()
@EvdWee EvdWee added the question label May 1, 2023
@EvdWee
Copy link
Author

EvdWee commented May 1, 2023

I have found the dictionary with the slider position, and can modify it, but it does not update the slider position:

MicrosoftTeams-image (5)

@ianhi
Copy link
Collaborator

ianhi commented May 1, 2023

Hi @EvdWee I'm glad youre finding it useful!

Unfortunately the inability to set initial positions is one of the longstanding issues with this library that turns out to be surprisingly complicated to fix. See more details here: #183

That said you can hopefully use the below set_param_indices function to accomplish what you want:

%matplotlib ipympl

import matplotlib.pyplot as plt
import numpy as np

from mpl_interactions import hyperslicer


def set_param_indices(controls, setters: dict[str, int] = None, **kwargs):
    """
    Set initial values of sliders assuming that you
    are creating ipywidgets sliders, not Matpotlib sliders.

    Parameters
    ----------
    controls : controls object
    setters : dict
        A dictionary of names mapping to indices. Necessary in case one of the slider
        names is invalid as a kwarg to function (e.g. if it has a space in it).
    ** kwargs:
        pairs of param names and index to set the slider to.
        NOTE: the values must be integer indices, not necessarily the
        value that you expect the slider to display.
    """

    if setters is not None:
        kwargs.update(setters)
    for name, idx in kwargs.items():
        controls.controls[name].children[0].value = idx


fig = plt.figure()

array = np.random.rand(5, 2, 512, 512)
control1 = hyperslicer(
    array, vmin=np.min(array), vmax=np.max(array), names=("Z Position", "channel")
)

# can set either using setters or kwargs
set_param_indices(control1, setters={"Z Position": 3}, channel=1)
plt.show()

note: This is setting the index, not the value. So in the case in hyperslicer yo uhave where the z indices map to values you need to set the index that would correspond to 0.9 rather than setting 0.9.

I'm sorry this is confusing, it's all a consequence of design choices I made several years ago that made sense at the time but have made things difficult now :(

@EvdWee
Copy link
Author

EvdWee commented May 1, 2023

Thanks for the quick response. This function seems to be a bit more trustworthy than my fix I just found:
control1.vbox.children[0].children[0].value=3

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

No branches or pull requests

2 participants