Commit fc8d8415 authored by eric pellegrini's avatar eric pellegrini
Browse files

reorganized the files

parent edfa3165
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib ipympl"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import h5py\n",
"from HDF5Parser import HDF5Parser\n",
"from PathSelector import PathSelector"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "18008505c76449e5a98d3b681b2e06bd",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Accordion(children=(SelectMultiple(options=(('[..]', '..'), ('[bin]', 'bin'), ('[Data]', 'Data'), ('[Desktop]'…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"path = PathSelector(extensions=[\".hdf\",\".h5\",\".nxs\"])\n",
"path.widget"
]
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib ipympl"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "ImportError",
"evalue": "No module named 'hdfviewer.HDFViewer'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-4e5ea23e6fd6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mh5py\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mhdfviewer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mHDFViewer\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mHDFViewer\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mhdfviewer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPathSelector\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mPathSelector\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mImportError\u001b[0m: No module named 'hdfviewer.HDFViewer'"
]
}
],
"source": [
"import h5py\n",
"\n",
"from hdfviewer.widgets.HDFViewer import HDFViewer\n",
"from hdfviewer.widgets.PathSelector import PathSelector"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'PathSelector' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-21cbad8e706b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpath\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPathSelector\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mextensions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\".hdf\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\".h5\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\".nxs\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwidget\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'PathSelector' is not defined"
]
}
],
"source": [
"path = PathSelector(extensions=[\".hdf\",\".h5\",\".nxs\"])\n",
"path.widget"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
%% Cell type:code id: tags:
``` python
%matplotlib ipympl
```
%% Cell type:code id: tags:
``` python
import h5py
from hdfviewer.widgets.HDFViewer import HDFViewer
from hdfviewer.widgets.PathSelector import PathSelector
```
%%%% Output: error
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-4e5ea23e6fd6> in <module>()
1 import h5py
2
----> 3 from hdfviewer.HDFViewer import HDFViewer
4 from hdfviewer.PathSelector import PathSelector
ImportError: No module named 'hdfviewer.HDFViewer'
%% Cell type:code id: tags:
``` python
path = PathSelector(extensions=[".hdf",".h5",".nxs"])
path.widget
```
%%%% Output: error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-21cbad8e706b> in <module>()
----> 1 path = PathSelector(extensions=[".hdf",".h5",".nxs"])
2 path.widget
NameError: name 'PathSelector' is not defined
%% Cell type:code id: tags:
``` python
if path.file:
hdf5 = h5py.File(path.file,"r")
display(HDFViewer(hdf5))
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......@@ -4,9 +4,9 @@ MatPlotLib based viewer for 1D, 2D and 3D NumPy data.
import numpy as np
from .MplDataViewer1D import _MplDataViewer1D
from .MplDataViewer2D import _MplDataViewer2D
from .MplDataViewer3D import _MplDataViewer3D
from hdfviewer.viewers.MplDataViewer1D import _MplDataViewer1D
from hdfviewer.viewers.MplDataViewer2D import _MplDataViewer2D
from hdfviewer.viewers.MplDataViewer3D import _MplDataViewer3D
_viewers = {1 : _MplDataViewer1D, 2 : _MplDataViewer2D, 3 : _MplDataViewer3D}
......@@ -15,7 +15,7 @@ class MplDataViewerError(Exception):
class MplDataViewer(object):
def __init__(self,dataset):
def __init__(self,dataset,standAlone=True):
# Remove axis with that has dimension 1 (e.g. (20,1,30) --> (20,30))
self._dataset = np.squeeze(dataset)
......@@ -24,7 +24,7 @@ class MplDataViewer(object):
if ndim not in _viewers:
raise MplDataViewerError("The dataset dimension ({ndim:d}) is not supported by the viewer".format(ndim=ndim))
self._viewer = _viewers[ndim](self._dataset)
self._viewer = _viewers[ndim](self._dataset,standAlone)
@property
def viewer(self):
......
......@@ -14,7 +14,7 @@ import matplotlib.pyplot as plt
class _MplDataViewer1D(object):
def __init__(self,dataset):
def __init__(self,dataset,**kwargs):
self._figure = plt.figure()
......
......@@ -9,36 +9,36 @@ import matplotlib.pyplot as plt
import ipywidgets as widgets
from .MplDataViewer import MplDataViewer, MplDataViewerError
from hdfviewer.viewers.MplDataViewer import MplDataViewer, MplDataViewerError
from .MplOutputWidget import MplOutputWidget
from hdfviewer.widgets.MplOutput import MplOutput
class HDF5Parser(widgets.Accordion):
class HDFViewer(widgets.Accordion):
def __init__(self, hdf5, startPath=None):
def __init__(self, hdf, startPath=None):
widgets.Accordion.__init__(self)
self._hdf5 = hdf5
self._hdf = hdf
if startPath is None:
self._startPath = "/"
self.children = [HDF5Parser(hdf5,self._startPath)]
self.children = [HDFViewer(hdf,self._startPath)]
self.set_title(0,self._startPath)
else:
self._startPath = startPath
attributesAccordion = widgets.Accordion()
for idx,(key,value) in enumerate(self._hdf5[self._startPath].attrs.items()):
for idx,(key,value) in enumerate(self._hdf[self._startPath].attrs.items()):
attributesAccordion.children = list(attributesAccordion.children) + [widgets.HTML(value)]
attributesAccordion.set_title(idx,key)
# Setup the groups and datasets accordion
groupsAccordion = widgets.Accordion()
datasetsAccordion = widgets.Accordion()
for value in list(self._hdf5[self._startPath].values()):
for value in list(self._hdf[self._startPath].values()):
if isinstance(value,h5py.Group):
groupsAccordion.children = list(groupsAccordion.children) + [HDF5Parser(hdf5,value.name)]
groupsAccordion.children = list(groupsAccordion.children) + [HDFViewer(hdf,value.name)]
groupsAccordion.set_title(len(groupsAccordion.children)-1,value.name)
elif isinstance(value,h5py.Dataset):
datasetInfo = []
......@@ -49,7 +49,7 @@ class HDF5Parser(widgets.Accordion):
datasetInfo.append("<i>Type: %s</i>" % value.dtype.name)
datasetInfo = "<br>".join(datasetInfo)
vbox = widgets.VBox()
vbox.children = [widgets.HTML(datasetInfo),MplOutputWidget()]
vbox.children = [widgets.HTML(datasetInfo),MplOutput()]
datasetsAccordion.children = list(datasetsAccordion.children) + [vbox]
datasetsAccordion.set_title(len(datasetsAccordion.children)-1,value.name)
datasetsAccordion.observe(self._onSelectDataset,names="selected_index")
......@@ -85,14 +85,13 @@ class HDF5Parser(widgets.Accordion):
with output:
try:
a = np.random.uniform(0,1,(300,400,50))
self._viewer = MplDataViewer(a,standAlone=False)
self._viewer = MplDataViewer(self._hdf[path],standAlone=False)
except MplDataViewerError as e:
label = widgets.Label(value=str(e))
display(label)
else:
# Bind the DataViewer figure to the MplOutput widget for allowing a "clean" output clearing (i.e. release the figure from plt)
output.figure = self._viewer.figure
output.figure = self._viewer.viewer.figure
hbox = widgets.HBox()
......
"""
Code taken and slightly modified from https://stackoverflow.com/questions/48056345/jupyter-lab-browsing-the-remote-file-system-inside-a-notebook
"""
import sys
import os
import ipywidgets as widgets
from IPython.display import display
class PathSelector(object):
def __init__(self,start_dir=None,select_file=True,extensions=None):
self.file = None
self.select_file = select_file
self.cwd = start_dir if start_dir else os.environ["HOME"]
self.select = widgets.SelectMultiple(options=['init'],value=(),rows=10,description='')
self.widget = widgets.Accordion(children=[self.select])
self.extensions = extensions if extensions else []
self.widget.selected_index = None # Start closed (showing path only)
self.refresh(self.cwd)
self.select.observe(self.on_update,'value')
def on_update(self,change):
if len(change['new']) > 0:
self.refresh(change['new'][0])
def refresh(self,item):
path = os.path.abspath(os.path.join(self.cwd,item))
if os.path.isfile(path):
if self.select_file:
self.widget.set_title(0,path)
self.file = path
self.widget.selected_index = None
else:
self.select.value = ()
else: # os.path.isdir(path)
self.file = None
self.cwd = path
# Build list of files and dirs
keys = ['[..]'];
for item in os.listdir(path):
if item[0] == '.':
continue
elif os.path.isdir(os.path.join(path,item)):
keys.append('['+item+']');
else:
if self.extensions:
ext = os.path.splitext(item)[-1]
if ext in self.extensions:
keys.append(item);
else:
keys.append(item);
# Sort and create list of output values
keys.sort(key=str.lower)
vals = []
for k in keys:
if k[0] == '[':
vals.append(k[1:-1]) # strip off brackets
else:
vals.append(k)
# Update widget
self.widget.set_title(0,path)
self.select.options = list(zip(keys,vals))
with self.select.hold_trait_notifications():
self.select.value = ()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment