BCDA-APS / epics-bluesky-vm

Simulate a beam line using EPICS IOCs, GUIs, Bluesky and related components.
Other
1 stars 0 forks source link

lesson3 part A show data LivePlot/ipython issue #36

Open lcgallington opened 4 months ago

lcgallington commented 4 months ago

When I try to run the jupyter notebook code for lesson 3 ( https://bcda-aps.github.io/bluesky_training/tutor/_lesson3a.html ) on the 2024_1 Bluesky training VM, I get an error that iPython is not defined. If I replace %matplotlib notebook with %matplotlib widget, the error goes away, but the plots are not updated real-time like the LiveTable, but after the scans are complete. Relevant code snippet below. I assume this is a case of a missing package rather than an issue with the code itself, so I posted here instead of bluesky training.


There is a callback routine that will plot the data as it is acquired. When starting graphics, it is necessary to first initialize the graphics manager of the display. The setup is specific to the graphics manager. For command line or python program use, see https://blueskyproject.io/bluesky/callbacks.html#aside-making-plots-update-live .

For jupyter notebooks:

%matplotlib notebook 
## %matplotlib widget instead of %matplotlib notebook, which gives an IPython error
from bluesky.utils import install_nb_kicker
install_nb_kicker() 

We’ll import the LivePlot callback from the Bluesky library:

from bluesky.callbacks import LivePlot

Count the scaler 5 times. We’ll just plot the scint signal.

RE(bp.count([scaler],num=5), LivePlot("scint"))
Javascript Error: IPython is not defined
prjemian commented 4 months ago

Thanks for posting the issue. I believe this is more about the best choice for the %matplotlib magic function to enable dyanmic updates of the plot. Different options supplied will produce or not produce the error.

Why this is more appropriate for the bluesky_training repository comes down to a couple bits of code in the notebook:

P = "sky:"

Most of the training notebooks have been homogenized to use gp: instead of various other IOC prefixes, including sky:. A consistency problem in the bluesky_training repo due to evolution.

from bluesky.utils import install_nb_kicker
install_nb_kicker()

These two lines are no longer needed by the BestEffortCallback. This indicates the notebook has not been revised in a few years.

Probably best to switch notebooks to this one: https://bcda-aps.github.io/bluesky_training/tutor/_basic_c.html

Still remaining is the question of which %matplotlib OPTION to pick. I'm still looking into it... Here's my concise test notebook:

%matplotlib inline
# %matplotlib ipympl
# %matplotlib nbagg
# %matplotlib notebook
# %matplotlib widget
from bluesky import plans as bp
from bluesky import RunEngine
from bluesky.callbacks import LiveTable, LivePlot
from ophyd import EpicsMotor
from ophyd.scaler import ScalerCH

RE = RunEngine({})
P = "gp:"
m1 = EpicsMotor(f"{P}m1", name="m1")
scaler = ScalerCH(f"{P}scaler1", name="scaler")
scaler.wait_for_connection()
scaler.channels.chan01.chname.put("clock")
scaler.channels.chan02.chname.put("I0")
scaler.channels.chan03.chname.put("scint")
scaler.preset_time.put(0.4)
scaler.select_channels()

m1.move(1)

print("Starting the scan ...")
RE(bp.scan([scaler], m1, 1, 5, 11), LivePlot("scint", "m1"))
prjemian commented 4 months ago

I'll keep the issue in this repo for now.

prjemian commented 4 months ago

What option do the bluesky tutorials use?

widget seems to be more common, then inline

prjemian commented 4 months ago

With notebook, I get the error but not with inline:

Javascript Error: IPython is not defined
lcgallington commented 4 months ago

I went back and forth about which repo to use for this issue, so I won't be mortally offended if this gets moved. It looks like basic_c uses inline instead of widget so maybe I should stick to that. Are updates in progress for lessons 5 and onward? I'm planning to tackle those next.

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Pete R Jemian @.> Sent: Wednesday, February 21, 2024 11:06:00 AM To: BCDA-APS/epics-bluesky-vm @.> Cc: Gallington, Leighanne C. @.>; Author @.> Subject: Re: [BCDA-APS/epics-bluesky-vm] lesson3 part A show data LivePlot/ipython issue (Issue #36)

With notebook, I get the error but not with inline: Javascript Error: IPython is not defined — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread. Message ID: BCDA-APS/epics-bluesky-vm/issues/36/1957326375@ github. com ZjQcmQRYFpfptBannerStart This Message Is From an External Sender This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

With notebook, I get the error but not with inline:

Javascript Error: IPython is not defined

— Reply to this email directly, view it on GitHubhttps://urldefense.us/v3/__https://github.com/BCDA-APS/epics-bluesky-vm/issues/36*issuecomment-1957326375__;Iw!!G_uCfscf7eWS!ced1JBGFzrFk0g0Ymy8zvpkQDg3-oyqUjVi8Kp0qJtLl_Z4nfh140w-1JYSthP6jq1Hk420NnfBYZDhXJhZY05A2tQ$, or unsubscribehttps://urldefense.us/v3/__https://github.com/notifications/unsubscribe-auth/AO3WXSMXDIXWJISDRQOQCXDYUYSNXAVCNFSM6AAAAABDSAJBRSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJXGMZDMMZXGU__;!!G_uCfscf7eWS!ced1JBGFzrFk0g0Ymy8zvpkQDg3-oyqUjVi8Kp0qJtLl_Z4nfh140w-1JYSthP6jq1Hk420NnfBYZDhXJhZVJDm-ng$. You are receiving this because you authored the thread.Message ID: @.***>

prjemian commented 4 months ago

No updates are in progress for lesson 5 and onwards at this time. The only active issue is to revise taxi/fly example so that it works with servo motors (such as the USAXS analyzer crystal $\theta$ rotation, called ar).

prjemian commented 4 months ago

I asked this question on Bluesky's Mattermost discussion channel: https://mattermost.hzdr.de/bluesky/pl/4y1qzo1aqp8fikwnprgiq6ppar

prjemian commented 4 months ago

I want my LivePlot to update dynamically, while the scan() plan is running. Instead, it appears only when the RE returns. How to?

and pasted my notebook example code with the %matplotlib widget line as the uncommented choice.

lcgallington commented 4 months ago

So it looks like all the matplotlib options for embedded figures in jupyter notebooks are using the same backend module, ipympl.backend_nbagg , and plt.ion() does not make the notebook plots interactive. If I run %matplotlib with no arguments, it defaults to the qtagg backend, and plt.ion() results in live updating plots in external windows.


%matplotlib notebook
#%matplotlib widget
#%matplotlib ipympl

print(matplotlib.get_backend())
plt.ion()
from bluesky.utils import install_nb_kicker
install_nb_kicker()

module://ipympl.backend_nbagg


%matplotlib
print(matplotlib.get_backend())
plt.ion()
from bluesky.utils import install_nb_kicker
install_nb_kicker()

Using matplotlib backend: <object object at 0x7f0c7841ec00> QtAgg