Commit 4c2139c3 authored by myron's avatar myron
Browse files

this modification should make sure that lists of keys made of strings are...

this modification should make sure that lists of keys made of strings are always properly sorted according to their numerical part
parent 3d107ee8
......@@ -151,7 +151,7 @@ class Plot2D(MyMplCanvas):
def compute_initial_figure(self):
file_list = glob.glob("scan_*.txt")
file_list.sort()
file_list.sort( key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )
img=[]
error = []
......
......@@ -302,7 +302,7 @@ class imageset:
def loadkimberlite(self,matfilename):
data_dict = io.loadmat(matfilename)
sorted_keys = sorted(data_dict.keys())
sorted_keys = sorted(data_dict.keys(), key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )
sy = data_dict['sy'][0]
allsx = []
for key in sorted_keys[3:12]:
......
......@@ -313,7 +313,7 @@ class offDiagonal:
ds_group = h5group[ "offDiaDataSets" ]
keys = [nm for nm in ds_group]
keys.sort()
keys.sort(key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )
for k in keys:
......
......@@ -884,7 +884,7 @@ class mainwindow(Qt.QMainWindow):
print( x,y, cx, cy)
newspots.append((y,x,i, int((cy*4+cx)+1) ) )
else:
spots.sort()
spots.sort(key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )
newspots = []
for k,(y,x,i) in enumerate( spots):
newspots.append((y,x,i,k+1) )
......
......@@ -767,7 +767,7 @@ class roi_finder:
counter = 0
new_rois = {}
for data, key in zip(pw_data, sorted(roi_obj.red_rois)): # go through each matrix (one per ROI)
for data, key in zip(pw_data, sorted(roi_obj.red_rois , key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )): # go through each matrix (one per ROI)
# decompose data, choose method
if method == 'nnma': # non negative matrix factorisation
......@@ -883,7 +883,7 @@ class roi_finder:
counter = 0
new_rois = {}
for data, key in zip(pw_data, sorted(self.roi_obj.red_rois)): # go through each matrix (one per ROI)
for data, key in zip(pw_data, sorted(self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )): # go through each matrix (one per ROI)
# decompose data, choose method
if method == 'nnma': # non negative matrix factorisation
......@@ -1123,7 +1123,7 @@ class roi_finder:
plt.ioff()
counter = 0
for data, key in zip(cw_data, sorted(self.roi_obj.red_rois)):
for data, key in zip(cw_data, sorted(self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )):
for ii in range(data.shape[1]):
if save_dataset:
the_shelve = shelve.open(save_dataset)
......@@ -1254,7 +1254,7 @@ class roi_finder:
plt.cla()
counter = 0
for data, key in zip(cw_data, sorted(self.roi_obj.red_rois)):
for data, key in zip(cw_data, sorted(self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )):
print('Processing: ', key)
for ii in range(data.shape[1]):
print('>>>>>>>>> ', ii)
......@@ -1375,11 +1375,11 @@ class roi_finder:
cw_data[key] = np.sum( data_dict_norm[key], axis=1 )
# fetch all red_roi keys
roi_keys = [key for key in sorted(self.roi_obj.red_rois)]
roi_keys = [key for key in sorted(self.roi_obj.red_rois , key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )]
# deep copy all ROIs, set all to zero
red_rois_copy = {}
for key in sorted( self.roi_obj.red_rois ):
for key in sorted( self.roi_obj.red_rois , key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
red_rois_copy[key] = copy.deepcopy( self.roi_obj.red_rois[key] )
red_rois_copy[key][1] = np.zeros_like( self.roi_obj.red_rois[key][1] )
......@@ -1492,7 +1492,7 @@ class roi_finder:
self.column_ind -= 0
def finish( self, event ):
for key in sorted(red_rois_copy):
for key in sorted(red_rois_copy, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
red_rois_copy[key][1] = self.red_rois[key][1]
plt.close()
......@@ -1602,7 +1602,7 @@ class roi_finder:
plt.ioff()
counter = 0
for data, key in zip(cw_data, sorted(self.roi_obj.red_rois)):
for data, key in zip(cw_data, sorted(self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )):
print('Processing: ', key)
for ii in range(data.shape[1]):
plt.cla()
......@@ -1955,11 +1955,11 @@ class roi_finder:
rw_data[key] = np.sum( data_dict_norm[key], axis=2 )
# fetch all red_roi keys
roi_keys = [key for key in sorted(self.roi_obj.red_rois)]
roi_keys = [key for key in sorted(self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )]
# deep copy all ROIs, set all to zero
red_rois_copy = {}
for key in sorted( self.roi_obj.red_rois ):
for key in sorted( self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
red_rois_copy[key] = copy.deepcopy( self.roi_obj.red_rois[key] )
red_rois_copy[key][1] = np.zeros_like( self.roi_obj.red_rois[key][1] )
......@@ -2073,7 +2073,7 @@ class roi_finder:
self.row_ind -= 0
def finish( self, event ):
for key in sorted(red_rois_copy):
for key in sorted(red_rois_copy, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
red_rois_copy[key][1] = self.red_rois[key][1]
plt.close()
......@@ -2247,11 +2247,11 @@ class roi_finder:
data_dict_norm[key] = scans[0].raw_signals[key]
# fetch all red_roi keys
roi_keys = [key for key in sorted(self.roi_obj.red_rois)]
roi_keys = [key for key in sorted(self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )]
# deep copy all ROIs, set all to zero
red_rois_copy = {}
for key in sorted( self.roi_obj.red_rois ):
for key in sorted( self.roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
red_rois_copy[key] = copy.deepcopy( self.roi_obj.red_rois[key] )
red_rois_copy[key][1] = np.zeros_like( self.roi_obj.red_rois[key][1] )
......@@ -2267,7 +2267,7 @@ class roi_finder:
ax0 = plt.subplot2grid( (16, 16), (0, 0), colspan=16, rowspan=16 )
new_rois = {}
for counter, key in enumerate(sorted(pw_data)): # go through each matrix (one per ROI)
for counter, key in enumerate(sorted(pw_data, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ) ): # go through each matrix (one per ROI)
# decompose data, choose method
if method == 'nnma': # non negative matrix factorisation
......
......@@ -181,7 +181,7 @@ class imageset:
def loadkimberlite(self,matfilename):
data_dict = io.loadmat(matfilename)
sorted_keys = sorted(data_dict.keys())
sorted_keys = sorted(data_dict.keys(), key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )
sy = data_dict['sy'][0]
allsx = []
for key in sorted_keys[3:12]:
......
......@@ -83,7 +83,7 @@ def optimize_analyzer_focus(path, SPECfname, EDFprefix, EDFname, EDFpostfix, roi
scan72.get_raw_signals( roi_obj, method='pixel')
# measure the FWHM for each ROI
for ii,key in enumerate(sorted(scan72.raw_signals)):
for ii,key in enumerate(sorted(scan72.raw_signals , key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )):
motor_scale = scan72.counters[scan72_motornames[ii]]
points = []
heights = [] # index 1
......
......@@ -551,7 +551,8 @@ class imageset:
def loadkimberlite(self,matfilename):
data_dict = io.loadmat(matfilename)
sorted_keys = sorted(data_dict.keys())
sorted_keys = sorted(data_dict.keys() , key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )
sy = data_dict['sy'][0]
allsx = []
for key in sorted_keys[3:12]:
......
......@@ -514,7 +514,7 @@ class Hydra:
self.errors = np.zeros((len(self.energy),len(self.cenom_dict)))
master_eloss = (self.energy - np.median([self.cenom_dict[key] for key in self.cenom_dict]))*1.0e3
self.E0 = np.median([self.cenom_dict[key] for key in self.cenom_dict])
for key,ii in zip(sorted(self.cenom_dict), range(len(self.cenom_dict))):
for key,ii in zip(sorted(self.cenom_dict, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.cenom_dict))):
# signals
x = ( self.energy - self.cenom_dict[key] )*1.0e3
y = self.raw_signals[key][:]
......@@ -539,7 +539,7 @@ class Hydra:
self.errors = np.zeros((len(self.energy),len(self.cenom_dict)))
master_eloss = ( self.energy - np.median(self.cenom_dict[first_key][self.cenom_dict[first_key] > 0.0]) )*1.0e3
self.E0 = np.median(self.cenom_dict[first_key][self.cenom_dict[first_key] > 0.0])
for key,ii in zip(sorted(self.cenom_dict), range(len(self.cenom_dict))):
for key,ii in zip(sorted(self.cenom_dict, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.cenom_dict))):
print ('Pixel-by-pixel compensation for ' + key +'.')
signal = np.zeros(len(master_eloss))
error = np.zeros(len(master_eloss))
......@@ -570,7 +570,7 @@ class Hydra:
self.errors = np.zeros((len(self.energy),len(self.roi_obj.red_rois)))
energy = self.energy * 1e3 # energy in eV
for key,ii in zip(sorted(self.raw_signals), range(len(self.raw_signals))):
for key,ii in zip(sorted(self.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.raw_signals))):
y = self.raw_signals[key]
meanii = len(range(y.shape[1]))/2
yc = np.zeros_like(y)
......@@ -1074,7 +1074,7 @@ class Hydra:
# normalize data
pw_matrices_norm = []
for key in sorted(raw_signals):
for key in sorted(raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
if method == 'pixel':
unrav_mat = np.zeros((raw_signals[key].shape[0], raw_signals[key].shape[1]*raw_signals[key].shape[2]))
elif method in [ 'column' , 'row']:
......@@ -1098,7 +1098,7 @@ class Hydra:
# normalize data (first scan)
pw_matrices_norm = []
for key in sorted(raw_signals):
for key in sorted(raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
if method == 'pixel':
unrav_mat = np.zeros((raw_signals[key].shape[0], raw_signals[key].shape[1]*raw_signals[key].shape[2]))
elif method in [ 'column' , 'row' ]:
......@@ -1119,7 +1119,7 @@ class Hydra:
raw_signals = self.scans[scanname].raw_signals # dict with raw_signals
monitor = self.scans[scanname].monitor
for key,jj in zip(sorted(raw_signals), range(len(raw_signals))):
for key,jj in zip(sorted(raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(raw_signals))):
if method == 'pixel':
unrav_mat = np.zeros((raw_signals[key].shape[0], raw_signals[key].shape[1]*raw_signals[key].shape[2]))
elif method in [ 'column' , 'row']:
......@@ -1173,7 +1173,7 @@ class Hydra:
self.scans[scan_name].get_raw_signals( self.roi_obj, method='sum' )
# find CENOM of each ROI
for key in sorted(self.scans[scan_name].raw_signals):
for key in sorted(self.scans[scan_name].raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
cofm = xrs_utilities.find_center_of_mass(self.scans[scan_name].energy,self.scans[scan_name].raw_signals[key])
self.cenom_dict[key] = cofm
......@@ -1192,7 +1192,7 @@ class Hydra:
self.scans[scan_name].get_raw_signals( self.roi_obj, method='pixel' )
# find CENOM for each pixel of each ROI
for key in sorted(self.scans[scan_name].raw_signals):
for key in sorted(self.scans[scan_name].raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
self.cenom_dict[key] = np.zeros_like(self.roi_obj.red_rois[key][1],dtype='float')
for ii in range(self.cenom_dict[key].shape[0]):
for jj in range(self.cenom_dict[key].shape[1]):
......@@ -1393,7 +1393,7 @@ class Hydra_imaging(Hydra):
# parse energy scale
energy = np.array([])
for scan_key in sorted(self.scans):
for scan_key in sorted(self.scans, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
energy = np.append(energy, self.scans[scan_key].motors['energy'])
energy = np.array(list(set(energy)))
......@@ -1402,7 +1402,7 @@ class Hydra_imaging(Hydra):
for roi_key, (pos,M) in sorted(self.roi_obj.red_rois.items()):
signals[roi_key] = np.zeros( (len(energy), ) )
for en,estep in zip(energy, range(len(energy))):
for scan_key in sorted(self.scans):
for scan_key in sorted(self.scans, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
if self.scans[scan_key].motors['energy'] == en:
signals[roi_key][estep] += np.sum(self.scans[scan_key].raw_signals[roi_key])
......@@ -1430,7 +1430,7 @@ class Hydra_imaging(Hydra):
"""
# define master energy-loss scale
energy = np.array([])
for scan_key in sorted(self.scans):
for scan_key in sorted(self.scans, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
energy = np.append(energy, self.scans[scan_key].motors['energy'])
energy = np.sort(np.array(list(set(energy))))
......@@ -1451,7 +1451,7 @@ class Hydra_imaging(Hydra):
for roi_key, (pos,M) in sorted(self.roi_obj.red_rois.items()):
self.raw_signals[roi_key] = np.zeros( (len(energy), len(scan_scale_1), M.shape[1], len(scan_scale_2)) )
for en,estep in zip(energy, range(len(energy))):
for scan_key in sorted(self.scans):
for scan_key in sorted(self.scans, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
if self.scans[scan_key].motors['energy'] == en:
for zstep in range(len(scan_scale_2)):
if self.scans[scan_key].motors[step_motor] == scan_scale_2[zstep]:
......@@ -1459,7 +1459,7 @@ class Hydra_imaging(Hydra):
# interpolate everything onto master energy-loss scale
self.raw_signals_int = {}
for roi_key in sorted(self.raw_signals):
for roi_key in sorted(self.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
x = (energy - self.cenom_dict[roi_key])*1.0e3
y = self.raw_signals[roi_key]
f = interp1d(x, y, kind='linear', axis=0, bounds_error=False, fill_value=0.0)
......@@ -1722,7 +1722,7 @@ class Fourc:
elastic_scan.get_raw_signals( self.roi_obj, method='pixel' )
# find CENOM for each pixel of each ROI
for key in sorted(elastic_scan.raw_signals):
for key in sorted(elastic_scan.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
self.cenom_dict[key] = np.zeros_like(self.roi_obj.red_rois[key][1])
for ii in range(self.cenom_dict[key].shape[0]):
for jj in range(self.cenom_dict[key].shape[1]):
......@@ -2087,7 +2087,7 @@ class Fourc:
# normalize data
pw_matrices_norm = []
for key in sorted(raw_signals):
for key in sorted(raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
if method == 'pixel':
unrav_mat = np.zeros((raw_signals[key].shape[0], raw_signals[key].shape[1]*raw_signals[key].shape[2]))
elif method in [ 'column' , 'row']:
......@@ -2111,7 +2111,7 @@ class Fourc:
# normalize data (first scan)
pw_matrices_norm = []
for key in sorted(raw_signals):
for key in sorted(raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ):
if method == 'pixel':
unrav_mat = np.zeros((raw_signals[key].shape[0], raw_signals[key].shape[1]*raw_signals[key].shape[2]))
elif method in [ 'column' , 'row']:
......@@ -2132,7 +2132,7 @@ class Fourc:
raw_signals = self.scans[scanname].raw_signals # dict with raw_signals
monitor = self.scans[scanname].monitor
for key,jj in zip(sorted(raw_signals), range(len(raw_signals))):
for key,jj in zip(sorted(raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(raw_signals))):
if method == 'pixel':
unrav_mat = np.zeros((raw_signals[key].shape[0], raw_signals[key].shape[1]*raw_signals[key].shape[2]))
elif method in [ 'column' ,'row'] :
......
......@@ -202,7 +202,7 @@ class roi_object:
# append the other ROIs
self_len = len( new_obj.red_rois )
for ii,key in enumerate( sorted( roi_obj.red_rois ) ):
for ii,key in enumerate( sorted( roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ) ):
new_key = 'ROI%02d'%( ii+self_len )
if not new_key in list( new_obj.red_rois.keys() ):
new_obj.red_rois[new_key] = roi_obj.red_rois[key]
......@@ -328,7 +328,7 @@ class roi_object:
orig_length = len( self.red_rois )
for ii,key in enumerate(sorted(roi_object.red_rois)):
for ii,key in enumerate(sorted(roi_object.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )):
new_key = 'ROI%02d'%(ii+orig_length)
self.red_rois[new_key] = roi_object.red_rois[key]
self.red_rois[new_key][1][ self.red_rois[new_key][1]>0 ] += orig_length
......@@ -457,7 +457,7 @@ class roi_object:
# delete last ROI if no key is specified
if not roi_key:
roi_key = sorted(list( self.red_rois.keys()))[-1]
roi_key = sorted(list( self.red_rois.keys()) , key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) )[-1]
# make sure the ROI exists
assert(roi_key in list(self.red_rois.keys()) )
......@@ -521,7 +521,7 @@ class roi_object:
labelbottom=True )
# draw ROIs and labels
for ii, key in enumerate( sorted(self.red_rois) ):
for ii, key in enumerate( sorted(self.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ) ):
# plot the ROI as frame
corner = self.red_rois[key][0]
inset = self.red_rois[key][1]
......
......@@ -376,7 +376,7 @@ class Scan:
errors = {} # sqrt of the sum of counts
rot_angles_dict = {} # put possible rotation angles into dict
counter = 0
for key, (pos, M) in sorted(roi_obj.red_rois.items()):
for key, (pos, M) in sorted(roi_obj.red_rois.items() ):
signals[key] = np.zeros((len(self.energy), M.shape[0]))
errors[key] = np.zeros((len(self.energy), M.shape[0]))
if rot_angles is not None:
......@@ -461,7 +461,7 @@ class Scan:
self.__signals_normalized__ = True
# assign
for key,ii in zip(sorted(roi_obj.red_rois), range(len(roi_obj.red_rois))):
for key,ii in zip(sorted(roi_obj.red_rois, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(roi_obj.red_rois))):
if np.any(scaling):
self.raw_signals[key] = signals[key] * scaling[ii]
self.raw_errors[key] = errors[key] * scaling[ii]
......@@ -494,7 +494,7 @@ class Scan:
if method == 'sum':
self.signals = np.zeros(( len(self.energy), len(self.raw_signals) ))
self.errors = np.zeros(( len(self.energy), len(self.raw_signals) ))
for key,ii in zip(sorted(self.raw_signals), range(len(self.raw_signals))):
for key,ii in zip(sorted(self.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.raw_signals))):
if not self.__signals_normalized__:
self.signals[:,ii] = self.raw_signals[key]/self.monitor
self.errors[:,ii] = self.raw_errors[key]/self.monitor
......@@ -513,7 +513,7 @@ class Scan:
return
self.signals = np.zeros(( len(self.energy), len(self.raw_signals) ))
self.errors = np.zeros(( len(self.energy), len(self.raw_signals) ))
for key,ii in zip(sorted(self.raw_signals), range(len(self.raw_signals))):
for key,ii in zip(sorted(self.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.raw_signals))):
S = cenom_dict[key].shape
master_cenom = cenom_dict[key][int(S[0]/2.),int(S[1]/2.)]
for dim1 in range(self.raw_signals[key].shape[1]):
......@@ -545,7 +545,7 @@ class Scan:
self.errors = np.zeros(( len(self.energy), len(self.raw_signals) ))
energy = self.energy #* 1e3 # energy in eV
## meanmon = np.mean(self.monitor)
for key,ii in zip(sorted(self.raw_signals), range(len(self.raw_signals))):
for key,ii in zip(sorted(self.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.raw_signals))):
S = cenom_dict[key].shape
#the_hist = np.histogram(cenom_dict[key][cenom_dict[key]>0.0 ], bins=10)
#master_cenom = np.average(the_hist[1][1:], weights= the_hist[0])#cenom_dict[key][int(S[0]/2.),int(S[1]/2.)]
......@@ -583,7 +583,7 @@ class Scan:
self.errors = np.zeros(( len(self.energy), len(self.raw_signals) ))
energy = self.energy * 1e3 # energy in eV
### meanmon = np.mean(self.monitor)
for key,ii in zip(sorted(self.raw_signals), range(len(self.raw_signals))):
for key,ii in zip(sorted(self.raw_signals, key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ), range(len(self.raw_signals))):
y = self.raw_signals[key]
yn = (y.T/self.monitor).T #### * meanmon CHECK THIS REMOVAL
meanii = len(range(yn.shape[1]))//2
......@@ -1569,7 +1569,7 @@ def findgroups(scans):
for scan in scans:
print( scan )
allscannames.append(scan)
allscannames.sort() #
allscannames.sort( key = lambda x: int(''.join(filter(str.isdigit, str(x) ))) ) #
allscans = []
for scan in allscannames:
allscans.append(scans[scan])
......
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