# Interactively visualize data

**URL:** <https://renku.discourse.group/t/interactively-visualize-data/56>\
**Category:** RenkuLab\
**Created:** [11 March 2020 14:02 UTC](https://renku.discourse.group/t/interactively-visualize-data/56 "2020-03-11T14:02:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![adriente](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@adriente](https://renku.discourse.group/u/adriente)\
**Post date:** [11 March 2020 14:02 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/1 "2020-03-11T14:02:24Z")

</div>

**Issue**

I am working in electron microscopy and it is important for me (and other microscopist) to inspect data and pick up areas of the dataset interactively. To explore the dataset I usually use the PyQt5 backend of matplotlib.  
For exemple : I want to be able to pick up and display an area of an image. Then If I find this area suitable for processing I would like to be able to use it as an input for a python script.

I installed everything so that PyQt5 should be working but when I try to use the Ipython magic command line : `%matplotlib qt` an ImportError shows up (see **code output** section).

Searching on StackExchange I found this question : [StackExchange issue](https://stackoverflow.com/questions/56129786/cannot-load-backend-qt5agg-which-requires-the-qt5-interactive-framework-as). If I understood well it is normal to get such errors in the renkulab environment.

Can something be done to solve this issue regardless?  
More generally about interactivity: Would it be possible in renku to have “hybrid” workflows were the user could have manual input during the processing of the data ?

**Docker files**

requirements.txt  
`cython==0.29.15`  
`hyperspy==1.5.2`  
`hyperspy_gui_ipywidgets==1.2`  
`hyperspy_gui_traitsui==1.2`  
`lmfit==1.0.0`  
`PyQt5==5.14.1`

DockerFile  
`FROM renku/singleuser:0.4.3-renku0.8.2`

`#Uncomment and adapt if code is to be included in the image`  
`#COPY src /code/src`

`#Uncomment and adapt if your R or python packages require extra linux (ubuntu) software`  
`#e.g. the following installs apt-utils and vim; each pkg on its own line, all lines`  
`#except for the last end with backslash '\' to continue the RUN line`

`USER root`  
`RUN apt-get update && \`  
` apt-get install -y --no-install-recommends \`  
` libgl1-mesa-glx`  
`USER ${NB_USER}`

`#install the python dependencies`  
`COPY requirements.txt environment.yml /tmp/`  
`RUN conda env update -q -f /tmp/environment.yml && \`  
` /opt/conda/bin/pip install -r /tmp/requirements.txt && \`  
` conda clean -y --all && \`  
`conda env export -n "root"`

**code ouput**

`import matplotlib`  
`print(matplotlib.get_backend())`  
`--> Qt5Agg`

`%matplotlib qt`  
`---------------------------------------------------------------------------`  
`ImportError Traceback (most recent call last)`  
`<ipython-input-7-325c2f98aa22> in <module>`  
`----> 1 get_ipython().run_line_magic('matplotlib', 'qt')`

`/opt/conda/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)`  
` 2312 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals`  
` 2313 with self.builtin_trap:`  
`-> 2314 result = fn(*args, **kwargs)`  
` 2315 return result`  
` 2316`

`</opt/conda/lib/python3.7/site-packages/decorator.py:decorator-gen-108> in matplotlib(self, line)`

`/opt/conda/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)`  
` 185 # but it's overkill for just that one bit of state.`  
` 186 def magic_deco(arg):`  
`--> 187 call = lambda f, *a, **k: f(*a,**k)`  
` 188`  
` 189 if callable(arg):`

`/opt/conda/lib/python3.7/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)`  
` 97 print("Available matplotlib backends: %s" % backends_list)`  
` 98 else:`  
`---> 99 gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)`  
` 100 self._show_matplotlib_backend(args.gui, backend)`  
` 101`

`/opt/conda/lib/python3.7/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)`  
` 3412 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)`  
` 3413`  
`-> 3414 pt.activate_matplotlib(backend)`  
` 3415 pt.configure_inline_support(self, backend)`  
` 3416`

`/opt/conda/lib/python3.7/site-packages/IPython/core/pylabtools.py in activate_matplotlib(backend)`  
` 312`  
` 313 import matplotlib.pyplot`  
`--> 314 matplotlib.pyplot.switch_backend(backend)`  
` 315`  
` 316 # This must be imported last in the matplotlib series, after`

`/opt/conda/lib/python3.7/site-packages/matplotlib/pyplot.py in switch_backend(newbackend)`  
` 235 "Cannot load backend {!r} which requires the {!r} interactive "`  
` 236 "framework, as {!r} is currently running".format(`  
`--> 237 newbackend, required_framework, current_framework))`  
` 238`  
` 239 rcParams['backend'] = rcParamsDefault['backend'] = newbackend`

```
ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework, as 'headless' is currently running
```

---

<div class="post-metadata">

**Author:** ![rrrrrok](https://yyz2.discourse-cdn.com/free1/user_avatar/renku.discourse.group/rrrrrok/32/117_2.png) [@rrrrrok](https://renku.discourse.group/u/rrrrrok)\
**Post date:** [11 March 2020 16:22 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/2 "2020-03-11T16:22:40Z")

</div>

Hi @adriente thanks for raising this question! I think the qt backend is trying to open a window on your machine and because it’s running in a remote container it’s not possible. I’m not sure if there is some inline qt mode for jupyter?

Regarding your second question, can you be a bit more specific about how you imagine it working?

---

<div class="post-metadata">

**Author:** ![adriente](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@adriente](https://renku.discourse.group/u/adriente)\
**Post date:** [11 March 2020 18:36 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/3 "2020-03-11T18:36:17Z")

</div>

Hi @rrrrrok, thanks for this quick reply.

I forgot to mention that I tried `%matplotlib nbagg` with the configuration described above.

`import matplotlib.pyplot as plt`  
`plt.plot([1,2,3])`  
`Javascript Error: IPython is not defined`  
`[<matplotlib.lines.Line2D at 0x7fb054c575c0>]`

I think that the notebook interface is particularly adapted for data exploration. I mainly use the hyperspy library to interactively explore microscopy dataset ( [https://hyperspy.org/](https://hyperspy.org/) ). With this library it is possible to create Region of Interest widgets notably to manullay pick up data.

How I would imagine it would work :  
–\> I pick up a rectangular area of my data and then my notebook would gather (top,left,bottom,right) of the selected areas (maybe in a file???).  
–\> Still in my notebook I might also want to perform additional processing (such as denoising or background removal) and visualize the result to check it was correctly done.  
–\> Then my data treatment script would use the outputs of the notebook and process the data originating from the selected regions.  
–\> Renku would then help to keep track of which regions were used for a given dataset and which data processing was used.

I am not exactly sure how it would be possible in Renku to properly keep track of everything while still having a rather handy process. Maybe I am asking for too much ?

---

<div class="post-metadata">

**Author:** ![rrrrrok](https://yyz2.discourse-cdn.com/free1/user_avatar/renku.discourse.group/rrrrrok/32/117_2.png) [@rrrrrok](https://renku.discourse.group/u/rrrrrok)\
**Post date:** [16 March 2020 13:16 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/4 "2020-03-16T13:16:07Z")

</div>

Hi @adriente sorry I let this drop a bit - the use-case you describe is very close to what @hluetck has been doing for another imaging project. Maybe you can share the experience @hluetck? I’m not an expert on widgets in notebooks, but the notebooks I saw were pretty convincing 😄

I’m not sure how flexible hyperspy is in the choice of a backend? Does it need `qt` specifically?

---

<div class="post-metadata">

**Author:** ![hluetck](https://yyz2.discourse-cdn.com/free1/user_avatar/renku.discourse.group/hluetck/32/45_2.png) [@hluetck](https://renku.discourse.group/u/hluetck)\
**Post date:** [19 March 2020 12:28 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/5 "2020-03-19T12:28:23Z")

</div>

Hi @adriente sorry for the delayed reply, we are a bit ‘under water’ at the moment. I am using a combination of ipywidgets and matplotlib widgets to implement the interactive part of an image processing pipeline. This works well as long as I use the ‘classic’ Jupyter notebook interface (not JupyterLab). For example, to select a rectangular ROI:

```auto
from matplotlib import pyplot as plt
from matplotlib.widgets import RectangleSelector

```

```auto
%matplotlib notebook

```

```auto
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.imshow(img, cmap='gray', interpolation='none')
ax1.set_title('Please select ROI for analysis\n(Click & drag)')
rect = RectangleSelector(ax1, ui.line_select_callback, drawtype='box', useblit=False, 
                         button=[1, 3], # don't use middle button
                         minspanx=5, minspany=5, spancoords='pixels', interactive=True)
plt.show()

```

```auto
roi_coordinates = [int(round(x)) for x in rect.extents]
print('Selected ROI coordinates:')
print(*roi_coordinates)

```

Hope this helps. Let me know if you need more information.  
Cheers, Henry

---

<div class="post-metadata">

**Author:** ![adriente](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@adriente](https://renku.discourse.group/u/adriente)\
**Post date:** [19 March 2020 13:00 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/6 "2020-03-19T13:00:05Z")

</div>

Hi,

Thanks for the answers. I have not been really quick either.  
The code you sent doesn’t work and I still get `Javascript Error: IPython is not defined`.

Could you send your “requirements.txt” file and DockerFile because it seems that I am missing something here.

@rrrrrok hyperspy does not need qt specifically. It should work fine with the nbagg backend.

Best,

Adrien

---

<div class="post-metadata">

**Author:** ![hluetck](https://yyz2.discourse-cdn.com/free1/user_avatar/renku.discourse.group/hluetck/32/45_2.png) [@hluetck](https://renku.discourse.group/u/hluetck)\
**Post date:** [19 March 2020 13:19 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/7 "2020-03-19T13:19:54Z")

</div>

Here is my `environment.yml`:

```auto
name: "base"
channels:
  - conda-forge
dependencies:
  - python=3.7
  - dask=2.11.0
  - dask-labextension>=1.0.3
  - distributed=2.11.0
  - graphviz=2.42.3
  - h5py=2.10.0
  - ipykernel=5.1.3
  - ipywidgets>=7.5
  - matplotlib=3.1.2
  - nodejs=13.9.0
  - numpy=1.17.3
  - papermill=2.0.0
  - pip=19.3.1
  - python-graphviz=0.13.2
  - scikit-image=0.16.2
  - scipy=1.4.1
  - toolz=0.10.0
  - pip:
    - joblib==0.14.1
    - opencv-python==4.1.2.30
    - pystackreg==0.2.2
    - tqdm==4.40.2
    - wget==3.2
prefix: "/opt/conda"

```

and the `Dockerfile`:

```auto
FROM renku/singleuser:latest

# install the python dependencies
COPY requirements.txt environment.yml /tmp/
RUN conda env update -q -f /tmp/environment.yml && \
    /opt/conda/bin/pip install -r /tmp/requirements.txt && \
    conda clean -y --all && \
    conda env export -n "root"

```

---

<div class="post-metadata">

**Author:** ![adriente](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@adriente](https://renku.discourse.group/u/adriente)\
**Post date:** [19 March 2020 15:06 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/8 "2020-03-19T15:06:56Z")

</div>

@hluetck I don’t know what I am doing wrong but I tried to use the same `DockerFile` and `environment.yml` and I still get `Javascript Error: IPython is not defined`.

I will try to work without interactivity for the moment then.

---

<div class="post-metadata">

**Author:** ![hluetck](https://yyz2.discourse-cdn.com/free1/user_avatar/renku.discourse.group/hluetck/32/45_2.png) [@hluetck](https://renku.discourse.group/u/hluetck)\
**Post date:** [19 March 2020 15:12 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/9 "2020-03-19T15:12:23Z")

</div>

are you using JupyterLab or Jupyter notebook? When you start your environment, does the URL end with `/lab` or `/tree`? You can start the ‘classic’ Jupyter Notebook interface by changing the `/lab` part to `/tree`.

---

<div class="post-metadata">

**Author:** ![adriente](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@adriente](https://renku.discourse.group/u/adriente)\
**Post date:** [19 March 2020 15:35 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/10 "2020-03-19T15:35:27Z")

</div>

I tried to change from `\lab` to `\tree` and it actually works I can use the `nbagg` backend without any problem.

I guess there is no way to use the `qt` or `nbagg` environment in jupyter lab. That should not be a big deal for me now that i know it.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![hluetck](https://yyz2.discourse-cdn.com/free1/user_avatar/renku.discourse.group/hluetck/32/45_2.png) [@hluetck](https://renku.discourse.group/u/hluetck)\
**Post date:** [20 March 2020 08:23 UTC](https://renku.discourse.group/t/interactively-visualize-data/56/11 "2020-03-20T08:23:48Z")

</div>

Great that it works. You can set `default_url = /tree` in your `renku.ini` file (in the `.renku` folder) and it will start your environment in Jupyter notebook mode automatically.
