def testSynchronizeObject(self): filename = _tmpDirectory + "/data.h5" h5 = h5py.File(filename) model = hdf5.Hdf5TreeModel() model.insertH5pyObject(h5) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) index = model.index(0, 0, qt.QModelIndex()) node1 = model.nodeFromIndex(index) model.synchronizeH5pyObject(h5) self.waitForPendingOperations(model) # Now h5 was loaded from it's filename # Another ref is owned by the model h5.close() index = model.index(0, 0, qt.QModelIndex()) node2 = model.nodeFromIndex(index) self.assertIsNot(node1, node2) # after sync time.sleep(0.1) self.qapp.processEvents() time.sleep(0.1) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) self.assertIsNotNone(h5File) h5File = None # delete the model ref = weakref.ref(model) model = None self.qWaitForDestroy(ref)
def testGroupData(self): h5 = commonh5.File("/foo/bar/1.mock", "w") d = h5.create_group("foo") d.attrs["desc"] = "fooo" model = hdf5.Hdf5TreeModel() model.insertH5pyObject(d) displayed = self.getRowDataAsDict(model, row=0) self.assertEquals( displayed[hdf5.Hdf5TreeModel.NAME_COLUMN, qt.Qt.DisplayRole], "1.mock::foo") self.assertIsInstance( displayed[hdf5.Hdf5TreeModel.NAME_COLUMN, qt.Qt.DecorationRole], qt.QIcon) self.assertEquals( displayed[hdf5.Hdf5TreeModel.TYPE_COLUMN, qt.Qt.DisplayRole], "") self.assertEquals( displayed[hdf5.Hdf5TreeModel.SHAPE_COLUMN, qt.Qt.DisplayRole], "") self.assertEquals( displayed[hdf5.Hdf5TreeModel.VALUE_COLUMN, qt.Qt.DisplayRole], "") self.assertEquals( displayed[hdf5.Hdf5TreeModel.DESCRIPTION_COLUMN, qt.Qt.DisplayRole], "fooo") self.assertEquals( displayed[hdf5.Hdf5TreeModel.NODE_COLUMN, qt.Qt.DisplayRole], "Group")
def testDatasetData(self): h5 = commonh5.File("/foo/bar/1.mock", "w") value = numpy.array([1, 2, 3]) d = h5.create_dataset("foo", data=value) model = hdf5.Hdf5TreeModel() model.insertH5pyObject(d) displayed = self.getRowDataAsDict(model, row=0) self.assertEquals( displayed[hdf5.Hdf5TreeModel.NAME_COLUMN, qt.Qt.DisplayRole], "1.mock::foo") self.assertIsInstance( displayed[hdf5.Hdf5TreeModel.NAME_COLUMN, qt.Qt.DecorationRole], qt.QIcon) self.assertEquals( displayed[hdf5.Hdf5TreeModel.TYPE_COLUMN, qt.Qt.DisplayRole], value.dtype.name) self.assertEquals( displayed[hdf5.Hdf5TreeModel.SHAPE_COLUMN, qt.Qt.DisplayRole], "3") self.assertEquals( displayed[hdf5.Hdf5TreeModel.VALUE_COLUMN, qt.Qt.DisplayRole], "[1 2 3]") self.assertEquals( displayed[hdf5.Hdf5TreeModel.DESCRIPTION_COLUMN, qt.Qt.DisplayRole], "") self.assertEquals( displayed[hdf5.Hdf5TreeModel.NODE_COLUMN, qt.Qt.DisplayRole], "Dataset")
def testRootParent(self): model = hdf5.Hdf5TreeModel() h5_1 = commonh5.File("/foo/bar/1.mock", "w") model.insertH5pyObject(h5_1) index = model.index(0, 0, qt.QModelIndex()) index = model.parent(index) self.assertEquals(index, qt.QModelIndex())
def testRemoveObject(self): h5 = commonh5.File("/foo/bar/1.mock", "w") model = hdf5.Hdf5TreeModel() self.assertEquals(model.rowCount(qt.QModelIndex()), 0) model.insertH5pyObject(h5) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) model.removeH5pyObject(h5) self.assertEquals(model.rowCount(qt.QModelIndex()), 0)
def testAppendFilename(self): model = hdf5.Hdf5TreeModel() self.assertEqual(model.rowCount(qt.QModelIndex()), 0) model.appendFile(self.filename) self.assertEqual(model.rowCount(qt.QModelIndex()), 1) # clean up ref = weakref.ref(model) model = None self.qWaitForDestroy(ref)
def testAppendFilename(self): with self.h5TempFile() as filename: model = hdf5.Hdf5TreeModel() self.assertEquals(model.rowCount(qt.QModelIndex()), 0) model.appendFile(filename) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) h5File.close()
def testCloseFile(self): """A file inserted as a filename is open and closed internally.""" model = hdf5.Hdf5TreeModel() self.assertEqual(model.rowCount(qt.QModelIndex()), 0) model.insertFile(self.filename) self.assertEqual(model.rowCount(qt.QModelIndex()), 1) index = model.index(0, 0) h5File = model.data(index, role=hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) model.removeIndex(index) self.assertEqual(model.rowCount(qt.QModelIndex()), 0) self.assertFalse(bool(h5File.id.valid), "The HDF5 file was not closed")
def setUp(self): TestCaseQt.setUp(self) self.model = hdf5.Hdf5TreeModel() filename = _tmpDirectory + "/data.h5" self.h5 = h5py.File(filename) self.model.insertH5pyObject(self.h5) self.listener = SignalListener() self.model.sigH5pyObjectLoaded.connect(self.listener.partial(signal="loaded")) self.model.sigH5pyObjectRemoved.connect(self.listener.partial(signal="removed")) self.model.sigH5pyObjectSynchronized.connect(self.listener.partial(signal="synchronized"))
def testFileData(self): h5 = commonh5.File("/foo/bar/1.mock", "w") model = hdf5.Hdf5TreeModel() model.insertH5pyObject(h5) displayed = self.getRowDataAsDict(model, row=0) self.assertEqual(displayed[hdf5.Hdf5TreeModel.NAME_COLUMN, qt.Qt.DisplayRole], "1.mock") self.assertIsInstance(displayed[hdf5.Hdf5TreeModel.NAME_COLUMN, qt.Qt.DecorationRole], qt.QIcon) self.assertEqual(displayed[hdf5.Hdf5TreeModel.TYPE_COLUMN, qt.Qt.DisplayRole], "") self.assertEqual(displayed[hdf5.Hdf5TreeModel.SHAPE_COLUMN, qt.Qt.DisplayRole], "") self.assertEqual(displayed[hdf5.Hdf5TreeModel.VALUE_COLUMN, qt.Qt.DisplayRole], "") self.assertEqual(displayed[hdf5.Hdf5TreeModel.DESCRIPTION_COLUMN, qt.Qt.DisplayRole], "") self.assertEqual(displayed[hdf5.Hdf5TreeModel.NODE_COLUMN, qt.Qt.DisplayRole], "File")
def testSelection_SelectNone(self): tree = commonh5.File("/foo/bar/1.mock", "w") model = hdf5.Hdf5TreeModel() model.insertH5pyObject(tree) view = hdf5.Hdf5TreeView() view.setModel(model) view.setSelectedH5Node(tree) view.setSelectedH5Node(None) selection = list(view.selectedH5Nodes()) self.assertEqual(len(selection), 0)
def testAppendFilename(self): filename = _tmpDirectory + "/data.h5" model = hdf5.Hdf5TreeModel() self.assertEquals(model.rowCount(qt.QModelIndex()), 0) model.appendFile(filename) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) ref = weakref.ref(model) model = None self.qWaitForDestroy(ref)
def testUnconsistantTypes(self): model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") h5.create_group("aaa100") h5.create_group("100aaa") model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() proxy.setSourceModel(model) proxy.sort(0, qt.Qt.AscendingOrder) names = self.getChildNames(proxy, proxy.index(0, 0, qt.QModelIndex())) self.assertListEqual(names, ["100aaa", "aaa100"])
def testSelection_RootFromSubTree(self): tree = commonh5.File("/foo/bar/1.mock", "w") group = tree.create_group("a1") group.create_group("b/c/d") model = hdf5.Hdf5TreeModel() model.insertH5pyObject(group) view = hdf5.Hdf5TreeView() view.setModel(model) view.setSelectedH5Node(group) selected = list(view.selectedH5Nodes())[0] self.assertIs(group, selected.h5py_object)
def testSelection_Simple(self): tree = commonh5.File("/foo/bar/1.mock", "w") item = tree.create_group("a/b/c/d") item.create_group("e").create_group("f") model = hdf5.Hdf5TreeModel() model.insertH5pyObject(tree) view = hdf5.Hdf5TreeView() view.setModel(model) view.setSelectedH5Node(item) selected = list(view.selectedH5Nodes())[0] self.assertIs(item, selected.h5py_object)
def testDropFirstAsLast(self): model = hdf5.Hdf5TreeModel() h5_1 = commonh5.File("/foo/bar/1.mock", "w") h5_2 = commonh5.File("/foo/bar/2.mock", "w") model.insertH5pyObject(h5_1) model.insertH5pyObject(h5_2) self.assertEquals(self.getItemName(model, 0), "1.mock") self.assertEquals(self.getItemName(model, 1), "2.mock") index = model.index(0, 0, qt.QModelIndex()) mimeData = model.mimeData([index]) model.dropMimeData(mimeData, qt.Qt.MoveAction, 2, 0, qt.QModelIndex()) self.assertEquals(self.getItemName(model, 0), "2.mock") self.assertEquals(self.getItemName(model, 1), "1.mock")
def testNumber(self): model = hdf5.Hdf5TreeModel() h5 = _mock.File("/foo/bar/1.mock") h5.create_group("a1") h5.create_group("a20") h5.create_group("a3") model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() proxy.setSourceModel(model) proxy.sort(0, qt.Qt.AscendingOrder) names = self.getChildNames(proxy, proxy.index(0, 0, qt.QModelIndex())) self.assertListEqual(names, ["a1", "a3", "a20"])
def testEndTimeInArray(self): model = hdf5.Hdf5TreeModel() h5 = _mock.File("/foo/bar/1.mock") h5.create_group("a").create_dataset("end_time", numpy.array([numpy.string_("2015")])) h5.create_group("b").create_dataset("end_time", numpy.array([numpy.string_("2013")])) h5.create_group("c").create_dataset("end_time", numpy.array([numpy.string_("2014")])) model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() proxy.setSourceModel(model) proxy.sort(0, qt.Qt.DescendingOrder) names = self.getChildNames(proxy, proxy.index(0, 0, qt.QModelIndex())) self.assertListEqual(names, ["a", "c", "b"])
def testSelection_FileFromSubTree(self): tree = commonh5.File("/foo/bar/1.mock", "w") group = tree.create_group("a1") group.create_group("b").create_group("b").create_group("d") model = hdf5.Hdf5TreeModel() model.insertH5pyObject(group) view = hdf5.Hdf5TreeView() view.setModel(model) view.setSelectedH5Node(tree) selection = list(view.selectedH5Nodes()) self.assertEquals(len(selection), 0)
def testSelection_NotFound(self): tree2 = commonh5.File("/foo/bar/2.mock", "w") tree = commonh5.File("/foo/bar/1.mock", "w") item = tree.create_group("a/b/c/d") item.create_group("e").create_group("f") model = hdf5.Hdf5TreeModel() model.insertH5pyObject(tree) view = hdf5.Hdf5TreeView() view.setModel(model) view.setSelectedH5Node(tree2) selection = list(view.selectedH5Nodes()) self.assertEqual(len(selection), 0)
def testNXentryName(self): """Test NXentry without start_time or end_time""" model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") create_NXentry(h5, "a") create_NXentry(h5, "c") create_NXentry(h5, "b") model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() proxy.setSourceModel(model) proxy.sort(0, qt.Qt.AscendingOrder) names = self.getChildNames(proxy, proxy.index(0, 0, qt.QModelIndex())) self.assertListEqual(names, ["a", "b", "c"])
def testInsertFilename(self): try: model = hdf5.Hdf5TreeModel() self.assertEqual(model.rowCount(qt.QModelIndex()), 0) model.insertFile(self.filename) self.assertEqual(model.rowCount(qt.QModelIndex()), 1) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) self.assertIsNotNone(h5File) finally: ref = weakref.ref(model) model = None self.qWaitForDestroy(ref)
def testStartTime(self): """If it is not NXentry, start_time is not used""" model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") h5.create_group("a").create_dataset("start_time", data=numpy.string_("2015")) h5.create_group("b").create_dataset("start_time", data=numpy.string_("2013")) h5.create_group("c").create_dataset("start_time", data=numpy.string_("2014")) model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() proxy.setSourceModel(model) proxy.sort(0, qt.Qt.AscendingOrder) names = self.getChildNames(proxy, proxy.index(0, 0, qt.QModelIndex())) self.assertListEqual(names, ["a", "b", "c"])
def testNXentryEndTimeInArray(self): """Test NXentry with end_time""" model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") create_NXentry(h5, "a").create_dataset("end_time", data=numpy.array([numpy.string_("2015")])) create_NXentry(h5, "b").create_dataset("end_time", data=numpy.array([numpy.string_("2013")])) create_NXentry(h5, "c").create_dataset("end_time", data=numpy.array([numpy.string_("2014")])) model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() proxy.setSourceModel(model) proxy.sort(0, qt.Qt.DescendingOrder) names = self.getChildNames(proxy, proxy.index(0, 0, qt.QModelIndex())) self.assertListEqual(names, ["a", "c", "b"])
def testSupportedDrop(self): model = hdf5.Hdf5TreeModel() self.assertNotEquals(model.supportedDropActions(), 0) model.setFileMoveEnabled(False) model.setFileDropEnabled(False) self.assertEquals(model.supportedDropActions(), 0) model.setFileMoveEnabled(False) model.setFileDropEnabled(True) self.assertNotEquals(model.supportedDropActions(), 0) model.setFileMoveEnabled(True) model.setFileDropEnabled(False) self.assertNotEquals(model.supportedDropActions(), 0)
def testInsertFilenameAsync(self): filename = _tmpDirectory + "/data.h5" try: model = hdf5.Hdf5TreeModel() self.assertEqual(model.rowCount(qt.QModelIndex()), 0) model.insertFileAsync(filename) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5LoadingItem.Hdf5LoadingItem) self.waitForPendingOperations(model) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) finally: ref = weakref.ref(model) model = None self.qWaitForDestroy(ref)
def testDropExternalFile(self): with self.h5TempFile() as filename: model = hdf5.Hdf5TreeModel() mimeData = qt.QMimeData() mimeData.setUrls([qt.QUrl.fromLocalFile(filename)]) model.dropMimeData(mimeData, qt.Qt.CopyAction, 0, 0, qt.QModelIndex()) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) # after sync time.sleep(0.1) self.qapp.processEvents() time.sleep(0.1) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, role=hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) h5File.close()
def testInsertFilenameAsync(self): with self.h5TempFile() as filename: model = hdf5.Hdf5TreeModel() self.assertEquals(model.rowCount(qt.QModelIndex()), 0) model.insertFileAsync(filename) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5LoadingItem.Hdf5LoadingItem) time.sleep(0.1) self.qapp.processEvents() time.sleep(0.1) index = model.index(0, 0, qt.QModelIndex()) self.assertIsInstance(model.nodeFromIndex(index), hdf5.Hdf5Item.Hdf5Item) self.assertEquals(model.rowCount(qt.QModelIndex()), 1) # clean up index = model.index(0, 0, qt.QModelIndex()) h5File = model.data(index, hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) h5File.close()
def testNotCloseFile(self): """A file inserted as an h5py object is not open (then not closed) internally.""" filename = _tmpDirectory + "/data.h5" try: h5File = h5py.File(filename) model = hdf5.Hdf5TreeModel() self.assertEqual(model.rowCount(qt.QModelIndex()), 0) model.insertH5pyObject(h5File) self.assertEqual(model.rowCount(qt.QModelIndex()), 1) index = model.index(0, 0) h5File = model.data(index, role=hdf5.Hdf5TreeModel.H5PY_OBJECT_ROLE) model.removeIndex(index) self.assertEqual(model.rowCount(qt.QModelIndex()), 0) self.assertTrue(bool(h5File.id.valid), "The HDF5 file was unexpetedly closed") finally: h5File.close()
def testSelection_Tree(self): tree1 = commonh5.File("/foo/bar/1.mock", "w") tree2 = commonh5.File("/foo/bar/2.mock", "w") tree3 = commonh5.File("/foo/bar/3.mock", "w") tree1.create_group("a/b/c") tree2.create_group("a/b/c") tree3.create_group("a/b/c") item = tree2 model = hdf5.Hdf5TreeModel() model.insertH5pyObject(tree1) model.insertH5pyObject(tree2) model.insertH5pyObject(tree3) view = hdf5.Hdf5TreeView() view.setModel(model) view.setSelectedH5Node(item) selected = list(view.selectedH5Nodes())[0] self.assertIs(item, selected.h5py_object)