def testSelectImageFromEdf(self): dialog = self.createDialog() dialog.show() self.qWaitForWindowExposed(dialog) # init state filename = _tmpDirectory + "/singleimage.edf" url = silx.io.url.DataUrl(scheme="silx", file_path=filename, data_path="/scan_0/instrument/detector_0/data") dialog.selectUrl(url.path()) self.assertEqual(dialog._selectedData().shape, (100, 100)) self.assertSamePath(dialog.selectedFile(), filename) self.assertSamePath(dialog.selectedUrl(), url.path())
def testSelectImageFromEdf(self): dialog = self.createDialog() dialog.show() self.qWaitForWindowExposed(dialog) # init state filename = _tmpDirectory + "/singleimage.edf" url = silx.io.url.DataUrl(scheme="silx", file_path=filename, data_path="/scan_0/instrument/detector_0/data") dialog.selectUrl(url.path()) self.assertTrue(dialog._selectedData().shape, (100, 100)) self.assertSamePath(dialog.selectedFile(), filename) self.assertSamePath(dialog.selectedUrl(), url.path())
def test_silx_scheme(self): url = silx.io.url.DataUrl(scheme="silx", file_path=self.h5_filename, data_path="/") with utils.open(url.path()) as f: self.assertIsNotNone(f) self.assertTrue(silx.io.utils.is_file(f))
def test_fabio_scheme(self): if h5py is None: self.skipTest("H5py is missing") if fabio is None: self.skipTest("Fabio is missing") url = silx.io.url.DataUrl(scheme="fabio", file_path=self.edf_filename) self.assertRaises(IOError, utils.open, url.path())
def test_silx_scheme(self): if h5py is None: self.skipTest("H5py is missing") url = silx.io.url.DataUrl(scheme="silx", file_path=self.h5_filename, data_path="/") with utils.open(url.path()) as f: self.assertIsNotNone(f) self.assertTrue(silx.io.utils.is_file(f))
def testBadSubpath(self): dialog = self.createDialog() self.qWaitForPendingActions(dialog) browser = testutils.findChildren(dialog, qt.QWidget, name="browser")[0] filename = _tmpDirectory + "/data.h5" url = silx.io.url.DataUrl(scheme="silx", file_path=filename, data_path="/group/foobar") dialog.selectUrl(url.path()) self.qWaitForPendingActions(dialog) self.assertIsNotNone(dialog._selectedData()) # an existing node is browsed, but the wrong path is selected index = browser.rootIndex() obj = index.model().data(index, role=Hdf5TreeModel.H5PY_OBJECT_ROLE) self.assertEqual(obj.name, "/group") url = silx.io.url.DataUrl(dialog.selectedUrl()) self.assertEqual(url.data_path(), "/group")
def get_data(url): """Returns a numpy data from an URL. Examples: >>> # 1st frame from an EDF using silx.io.open >>> data = silx.io.get_data("silx:/users/foo/image.edf::/scan_0/instrument/detector_0/data[0]") >>> # 1st frame from an EDF using fabio >>> data = silx.io.get_data("fabio:/users/foo/image.edf::[0]") Yet 2 schemes are supported by the function. - If `silx` scheme is used, the file is opened using :meth:`silx.io.open` and the data is reach using usually NeXus paths. - If `fabio` scheme is used, the file is opened using :meth:`fabio.open` from the FabIO library. No data path have to be specified, but each frames can be accessed using the data slicing. This shortcut of :meth:`silx.io.open` allow to have a faster access to the data. .. seealso:: :class:`silx.io.url.DataUrl` :param Union[str,silx.io.url.DataUrl]: A data URL :rtype: Union[numpy.ndarray, numpy.generic] :raises ImportError: If the mandatory library to read the file is not available. :raises ValueError: If the URL is not valid or do not match the data :raises IOError: If the file is not found or in case of internal error of :meth:`fabio.open` or :meth:`silx.io.open`. In this last case more informations are displayed in debug mode. """ if not isinstance(url, silx.io.url.DataUrl): url = silx.io.url.DataUrl(url) if not url.is_valid(): raise ValueError("URL '%s' is not valid" % url.path()) if not os.path.exists(url.file_path()): raise IOError("File '%s' not found" % url.file_path()) if url.scheme() == "silx": data_path = url.data_path() data_slice = url.data_slice() with open(url.file_path()) as h5: if data_path not in h5: raise ValueError("Data path from URL '%s' not found" % url.path()) data = h5[data_path] if not silx.io.is_dataset(data): raise ValueError("Data path from URL '%s' is not a dataset" % url.path()) if data_slice is not None: data = h5py_read_dataset(data, index=data_slice) else: # works for scalar and array data = h5py_read_dataset(data) elif url.scheme() == "fabio": import fabio data_slice = url.data_slice() if data_slice is None: data_slice = (0, ) if data_slice is None or len(data_slice) != 1: raise ValueError( "Fabio slice expect a single frame, but %s found" % data_slice) index = data_slice[0] if not isinstance(index, int): raise ValueError( "Fabio slice expect a single integer, but %s found" % data_slice) try: fabio_file = fabio.open(url.file_path()) except Exception: logger.debug("Error while opening %s with fabio", url.file_path(), exc_info=True) raise IOError( "Error while opening %s with fabio (use debug for more information)" % url.path()) if fabio_file.nframes == 1: if index != 0: raise ValueError( "Only a single frame available. Slice %s out of range" % index) data = fabio_file.data else: data = fabio_file.getframe(index).data # There is no explicit close fabio_file = None else: raise ValueError("Scheme '%s' not supported" % url.scheme()) return data
def test_sliced_url(self): url = silx.io.url.DataUrl(file_path=self.h5_filename, data_slice=(5, )) self.assertRaises(IOError, utils.open, url.path())
def test_bad_url(self): url = silx.io.url.DataUrl(scheme="sil", file_path=self.h5_filename) self.assertRaises(IOError, utils.open, url.path())
def test_fabio_scheme(self): url = silx.io.url.DataUrl(scheme="fabio", file_path=self.edf_filename) self.assertRaises(IOError, utils.open, url.path())
def test_sliced_url(self): url = silx.io.url.DataUrl(file_path=self.h5_filename, data_slice=(5,)) self.assertRaises(IOError, utils.open, url.path())
def get_data(url): """Returns a numpy data from an URL. Examples: >>> # 1st frame from an EDF using silx.io.open >>> data = silx.io.get_data("silx:/users/foo/image.edf::/scan_0/instrument/detector_0/data[0]") >>> # 1st frame from an EDF using fabio >>> data = silx.io.get_data("fabio:/users/foo/image.edf::[0]") Yet 2 schemes are supported by the function. - If `silx` scheme is used, the file is opened using :meth:`silx.io.open` and the data is reach using usually NeXus paths. - If `fabio` scheme is used, the file is opened using :meth:`fabio.open` from the FabIO library. No data path have to be specified, but each frames can be accessed using the data slicing. This shortcut of :meth:`silx.io.open` allow to have a faster access to the data. .. seealso:: :class:`silx.io.url.DataUrl` :param Union[str,silx.io.url.DataUrl]: A data URL :rtype: Union[numpy.ndarray, numpy.generic] :raises ImportError: If the mandatory library to read the file is not available. :raises ValueError: If the URL is not valid or do not match the data :raises IOError: If the file is not found or in case of internal error of :meth:`fabio.open` or :meth:`silx.io.open`. In this last case more informations are displayed in debug mode. """ if not isinstance(url, silx.io.url.DataUrl): url = silx.io.url.DataUrl(url) if not url.is_valid(): raise ValueError("URL '%s' is not valid" % url.path()) if not os.path.exists(url.file_path()): raise IOError("File '%s' not found" % url.file_path()) if url.scheme() == "silx": data_path = url.data_path() data_slice = url.data_slice() with open(url.file_path()) as h5: if data_path not in h5: raise ValueError("Data path from URL '%s' not found" % url.path()) data = h5[data_path] if not silx.io.is_dataset(data): raise ValueError("Data path from URL '%s' is not a dataset" % url.path()) if data_slice is not None: data = data[data_slice] else: # works for scalar and array data = data[()] elif url.scheme() == "fabio": import fabio data_slice = url.data_slice() if data_slice is None: data_slice = (0, ) if data_slice is None or len(data_slice) != 1: raise ValueError("Fabio slice expect a single frame, but %s found" % data_slice) index = data_slice[0] if not isinstance(index, int): raise ValueError("Fabio slice expect a single integer, but %s found" % data_slice) try: fabio_file = fabio.open(url.file_path()) except Exception: logger.debug("Error while opening %s with fabio", url.file_path(), exc_info=True) raise IOError("Error while opening %s with fabio (use debug for more information)" % url.path()) if fabio_file.nframes == 1: if index != 0: raise ValueError("Only a single frame availalbe. Slice %s out of range" % index) data = fabio_file.data else: data = fabio_file.getframe(index).data # There is no explicit close fabio_file = None else: raise ValueError("Scheme '%s' not supported" % url.scheme()) return data