Skip to content

Commit 0e7324b

Browse files
authored
Merge pull request #1110 from vasole/1109-pymca-main-window-track-order-is-not-preserved
It looks good. Thanks.
2 parents 8cb4bf0 + 4ffbf62 commit 0e7324b

5 files changed

Lines changed: 206 additions & 208 deletions

File tree

src/PyMca5/PyMcaCore/NexusDataSource.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
# but do not crash just because of it
4141
pass
4242
import h5py
43-
from operator import itemgetter
4443
import re
45-
import posixpath
4644
import logging
4745
phynx = h5py
4846

@@ -86,55 +84,6 @@ def h5open(filename):
8684
# give back the original error
8785
return h5py.File(filename, "r")
8886

89-
#sorting method
90-
def h5py_sorting(object_list):
91-
sorting_list = ['start_time', 'end_time', 'name']
92-
n = len(object_list)
93-
if n < 2:
94-
return object_list
95-
96-
# This implementation only sorts entries
97-
if posixpath.dirname(object_list[0].name) != "/":
98-
return object_list
99-
100-
names = list(object_list[0].keys())
101-
102-
sorting_key = None
103-
for key in sorting_list:
104-
if key in names:
105-
sorting_key = key
106-
break
107-
108-
if sorting_key is None:
109-
if 'name' in sorting_list:
110-
sorting_key = 'name'
111-
else:
112-
return object_list
113-
114-
try:
115-
if sorting_key != 'name':
116-
sorting_list = [(o[sorting_key][()], o)
117-
for o in object_list]
118-
sorted_list = sorted(sorting_list, key=itemgetter(0))
119-
return [x[1] for x in sorted_list]
120-
121-
if sorting_key == 'name':
122-
sorting_list = [(_get_number_list(o.name),o)
123-
for o in object_list]
124-
sorting_list.sort()
125-
return [x[1] for x in sorting_list]
126-
except Exception:
127-
#The only way to reach this point is to have different
128-
#structures among the different entries. In that case
129-
#defaults to the unfiltered case
130-
_logger.warning("Default ordering")
131-
_logger.warning("Probably all entries do not have the key %s", sorting_key)
132-
return object_list
133-
134-
def _get_number_list(txt):
135-
rexpr = '[/a-zA-Z:-]'
136-
nbs= [float(w) for w in re.split(rexpr, txt) if w not in ['',' ']]
137-
return nbs
13887

13988
def get_family_pattern(filelist):
14089
name1 = filelist[0]

src/PyMca5/PyMcaCore/NexusTools.py

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
3333
import sys
3434
import os
35-
from operator import itemgetter
36-
import re
3735
import posixpath
3836
try:
3937
# try to import hdf5plugin
@@ -53,6 +51,8 @@ def is_dataset(something):
5351
def is_group(something):
5452
return False
5553

54+
from PyMca5.PyMcaIO.HDF5Utils import sort_h5items
55+
5656
import logging
5757
_logger = logging.getLogger(__name__)
5858

@@ -74,67 +74,6 @@ def isDataset(item):
7474
else:
7575
return False
7676

77-
#sorting method
78-
def h5py_sorting(object_list):
79-
sorting_list = ['start_time', 'end_time', 'name']
80-
n = len(object_list)
81-
if n < 2:
82-
return object_list
83-
84-
# we have received items, not values
85-
# perform a first sort based on received names
86-
# this solves a problem with Eiger data where all the
87-
# external data have the same posixName. Without this sorting
88-
# they arrive "unsorted"
89-
object_list.sort()
90-
try:
91-
posixNames = [item[1].name for item in object_list]
92-
except AttributeError:
93-
# Typical of broken external links
94-
_logger.debug("HDF5Widget: Cannot get posixNames")
95-
return object_list
96-
97-
# This implementation only sorts entries
98-
if posixpath.dirname(posixNames[0]) != "/":
99-
return object_list
100-
101-
sorting_key = None
102-
if hasattr(object_list[0][1], "items"):
103-
for key in sorting_list:
104-
if key in [x[0] for x in object_list[0][1].items()]:
105-
sorting_key = key
106-
break
107-
108-
if sorting_key is None:
109-
if 'name' in sorting_list:
110-
sorting_key = 'name'
111-
else:
112-
return object_list
113-
114-
try:
115-
if sorting_key != 'name':
116-
sorting_list = [(o[1][sorting_key][()], o)
117-
for o in object_list]
118-
sorted_list = sorted(sorting_list, key=itemgetter(0))
119-
return [x[1] for x in sorted_list]
120-
121-
if sorting_key == 'name':
122-
sorting_list = [(_get_number_list(o[1].name),o)
123-
for o in object_list]
124-
sorting_list.sort()
125-
return [x[1] for x in sorting_list]
126-
except Exception:
127-
#The only way to reach this point is to have different
128-
#structures among the different entries. In that case
129-
#defaults to the unfiltered case
130-
_logger.warning("Default ordering. "
131-
"Probably all entries do not have the key %s", sorting_key)
132-
return object_list
133-
134-
def _get_number_list(txt):
135-
rexpr = '[/a-zA-Z:-]'
136-
nbs= [float(w) for w in re.split(rexpr, txt) if w not in ['',' ']]
137-
return nbs
13877

13978
def getEntryName(path, h5file=None):
14079
"""
@@ -462,7 +401,7 @@ def getNXClassGroups(h5file, path, classes, single=False):
462401
groups = []
463402
items_list = list(h5file[path].items())
464403
if ("NXentry" in classes) or (b"NXentry" in classes):
465-
items_list = h5py_sorting(items_list)
404+
items_list = sort_h5items(items_list)
466405

467406
for key, group in items_list:
468407
if not isGroup(group):

src/PyMca5/PyMcaGui/io/hdf5/HDF5Widget.py

Lines changed: 3 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@
3131
import sys
3232
import os
3333
import posixpath
34-
import time
3534
import gc
36-
import re
37-
from operator import itemgetter
3835
import logging
3936
_logger = logging.getLogger(__name__)
4037

@@ -67,6 +64,8 @@ def h5open(filename):
6764
return h5py.File(filename, "r", libver='latest', swmr=True)
6865

6966
from PyMca5.PyMcaGui import PyMcaQt as qt
67+
from PyMca5.PyMcaIO.HDF5Utils import sort_h5items
68+
7069
safe_str = qt.safe_str
7170

7271
if hasattr(qt, 'QStringList'):
@@ -78,95 +77,6 @@ def MyQVariant(x=None):
7877
QVERSION = qt.qVersion()
7978

8079

81-
#sorting method
82-
def h5py_sorting(object_list, sorting_list=None):
83-
if sorting_list is None:
84-
sorting_list = ['start_time', 'end_time', 'name']
85-
n = len(object_list)
86-
if n < 2:
87-
return object_list
88-
89-
# we have received items, not values
90-
# perform a first sort based on received names
91-
# this solves a problem with Eiger data where all the
92-
# external data have the same posixName. Without this sorting
93-
# they arrive "unsorted"
94-
object_list.sort()
95-
try:
96-
posixNames = [item[1].name for item in object_list]
97-
except AttributeError:
98-
# Typical of broken external links
99-
_logger.debug("HDF5Widget: Cannot get posixNames")
100-
return object_list
101-
102-
# This implementation only sorts entries
103-
if posixpath.dirname(posixNames[0]) != "/":
104-
return object_list
105-
106-
sorting_key = None
107-
if hasattr(object_list[0][1], "items"):
108-
for key in sorting_list:
109-
if key in [x[0] for x in object_list[0][1].items()]:
110-
sorting_key = key
111-
break
112-
113-
if sorting_key is None:
114-
if 'name' in sorting_list:
115-
sorting_key = 'name'
116-
else:
117-
return object_list
118-
119-
try:
120-
if sorting_key == 'title':
121-
def getTitle(x):
122-
try:
123-
title = x["title"][()]
124-
except Exception:
125-
# allow the title to be missing
126-
title = ""
127-
if hasattr(title, "dtype"):
128-
if hasattr(title, "__len__"):
129-
if len(title) == 1:
130-
title = title[0]
131-
if hasattr(title, "decode"):
132-
title = title.decode("utf-8")
133-
return title
134-
# sort first by the traditional keys in order to be sorted
135-
# by title and respecting actquisition order for equal title
136-
try:
137-
ordered_list = h5py_sorting(object_list)
138-
except Exception:
139-
ordered_list = object_list
140-
sorting_list = [(getTitle(o[1]), o) for o in ordered_list]
141-
sorted_list = sorted(sorting_list, key=itemgetter(0))
142-
return [x[1] for x in sorted_list]
143-
144-
if sorting_key != 'name':
145-
sorting_list = [(o[1][sorting_key][()], o)
146-
for o in object_list]
147-
sorted_list = sorted(sorting_list, key=itemgetter(0))
148-
return [x[1] for x in sorted_list]
149-
150-
if sorting_key == 'name':
151-
sorting_list = [(_get_number_list(o[1].name),o)
152-
for o in object_list]
153-
sorting_list.sort()
154-
return [x[1] for x in sorting_list]
155-
except Exception:
156-
#The only way to reach this point is to have different
157-
#structures among the different entries. In that case
158-
#defaults to the unfiltered case
159-
_logger.warning("WARNING: Default ordering")
160-
_logger.warning("Probably all entries do not have the key %s" % sorting_key)
161-
return object_list
162-
163-
164-
def _get_number_list(txt):
165-
rexpr = '[/a-zA-Z:_-]'
166-
nbs= [float(w) for w in re.split(rexpr, txt) if w not in ['',' ']]
167-
return nbs
168-
169-
17080
class BrokenLink(object):
17181
pass
17282

@@ -275,7 +185,7 @@ def children(self):
275185
items = list(self.getNode(self.name).items())
276186
try:
277187
# better handling of external links
278-
finalList = h5py_sorting(items, sorting_list=self.__sorting_list)
188+
finalList = sort_h5items(items, sorting_list=self.__sorting_list)
279189
for i in range(len(finalList)):
280190
# avoid an error at silx level with the linechecking "if finalList[i][1] and "
281191
finalListIsTrue = True

0 commit comments

Comments
 (0)