def setUpClass(cls): cls.nx = 10 # do not change this here unless you fix also the tests cases cls.ny = 19 cls.nz = 10 cls.xmin = 1 cls.xmax = 10 cls.x = numpy.linspace(cls.xmin, cls.xmax, num=cls.nx) cls.y = cls.x.copy() cls.z = cls.x.copy() cls.data = numpy.random.rand(cls.nx) cls.gridder = xu.Gridder3D(cls.nx, cls.ny, cls.nz) cls.gridder(cls.x, cls.y, cls.z, cls.data)
def gridmap(specfile, scannr, experiment, nx, ny, nz, **kwargs): """ read ccd frames and grid them in reciprocal space angular coordinates are taken from the spec file **kwargs are passed to the rawmap function """ qx, qy, qz, intensity = rawmap(specfile, scannr, experiment, **kwargs) # convert data to rectangular grid in reciprocal space gridder = xu.Gridder3D(nx, ny, nz) gridder(qx, qy, qz, intensity) return gridder.xaxis, gridder.yaxis, gridder.zaxis, gridder.data, gridder
def gridmap(specfile, scan_nb, mydetector, region=None, myflatfield=None, myhotpixels=None, reload=0, previous_data=None, previous_mask=None, mysetup='ID01', myrocking_angle="outofplane", follow_bragg=0, myenergy=None, myoffsets=(0, 0, 0, 0, 0)): """ Load the data, check for saturated pixels, interpolate on an orthogonal grid :param specfile: :param scan_nb: :param mydetector: "Eiger4M" or "Eiger2M" or "Maxipix" :param region: roi on the detector :param myflatfield: 2D array, flatfield correction for the detector :param myhotpixels: 2D array, detector hotpixels to be masked :param reload: 1 when you reload the data :param previous_data: when you reload the data :param previous_mask: when you reload the data :param mysetup: 'ID01' or 'SIXS' or 'CRISTAL', different data loading method :param myrocking_angle: name of the motor which is tilted during the rocking curve :param follow_bragg: for energy_scan, set to 1 if the detector is scanned to follow the Bragg peak (delta @ ID01) :param myenergy: energy in eV of the experiment, in case it is not in the spec file :param myoffsets: sample and detector offsets for xrayutilities :return: """ global sixs_beta, nb_pixel_x, nb_pixel_y if region is None: if mydetector == "Eiger2M": region = [0, nb_pixel_y, 0, nb_pixel_x] elif mydetector == "Maxipix": region = [0, nb_pixel_y, 0, nb_pixel_x] elif mydetector == "Eiger4M": region = [0, nb_pixel_y, 0, nb_pixel_x] else: region = [0, nb_pixel_y, 0, nb_pixel_x] if mysetup == 'ID01': motor_names = specfile[str(scan_nb) + '.1'].motor_names # positioners motor_positions = specfile[str(scan_nb) + '.1'].motor_positions # positioners labels = specfile[str(scan_nb) + '.1'].labels # motor scanned labels_data = specfile[str(scan_nb) + '.1'].data # motor scanned chi = 0 delta = motor_positions[motor_names.index('del')] nu = motor_positions[motor_names.index('nu')] if myrocking_angle == "outofplane": eta = labels_data[labels.index('eta'), :] phi = motor_positions[motor_names.index('phi')] myenergy = motor_positions[motor_names.index('nrj')] # in kev elif myrocking_angle == "inplane": phi = labels_data[labels.index('phi'), :] eta = motor_positions[motor_names.index('eta')] myenergy = motor_positions[motor_names.index('nrj')] # in kev elif myrocking_angle == "energy": myenergy = labels_data[labels.index('energy'), :] # in kev if follow_bragg == 1: delta = labels_data[labels.index('del'), :] # TODO: understand why Qx is positive phi = motor_positions[motor_names.index('phi')] eta = motor_positions[motor_names.index('eta')] else: print("Error in rocking angle definition") sys.exit() myenergy = myenergy * 1000.0 # switch to eV if isinstance(myenergy, float) and myenergy < 0: print("Energy not correctly defined in spec file, default to 9keV") myenergy = 9000.0 elif mysetup == 'SIXS': mydataset = nxsReady.DataSet(datadir + ccdfiletmp % scan_nb, ccdfiletmp % scan_nb, scan="SBS") img = mydataset.mfilm[1:, :, :] # first frame is duplicated delta = mydataset.delta[1:].mean() # not scanned gamma = mydataset.gamma[1:].mean() # not scanned mu = mydataset.mu[1:] elif mysetup == 'CRISTAL': omega = specfile['a']['scan_data']['actuator_1_1'][:] / 1e6 delta = specfile['a/CRISTAL/I06-C-C07__EX__DIF-DELTA__#1/raw_value'][:] nu = specfile['a/CRISTAL/I06-C-C07__EX__DIF-GAMMA__#1/raw_value'][:] maxpix_img = specfile['a']['scan_data']['data_04'][:] elif mysetup == 'P10': # TODO: find motors in specfile mu = [] for index in range(25): # header specfile.readline() for index, myline in enumerate(specfile, 0): myline = myline.strip() mycolumns = myline.split() if mycolumns[0] == '!': break mu.append(mycolumns[0]) mu = np.asarray(mu, dtype=float) nb_img = len(mu) specfile.close() if reload == 0: if mydetector == "Eiger2M": counter = 'ei2minr' mymask = np.zeros((nb_pixel_y, nb_pixel_x)) elif mydetector == "Maxipix": counter = 'mpx4inr' # counter = 'roi7' mymask = np.zeros((nb_pixel_y, nb_pixel_x)) elif mydetector == "Eiger4M": mymask = np.zeros((nb_pixel_y, nb_pixel_x)) else: counter = 'mpx4inr' mymask = np.zeros((nb_pixel_y, nb_pixel_x)) if myflatfield is None: myflatfield = np.ones(mymask.shape) if myhotpixels is None: myhotpixels = np.zeros(mymask.shape) if mysetup == 'ID01': ccdn = labels_data[labels.index(counter), :] rawdata = np.zeros((len(ccdn), region[1] - region[0], region[3] - region[2])) for index in range(len(ccdn)): i = int(ccdn[index]) e = fabio.open(ccdfiletmp % i) ccdraw = e.data ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask) if mydetector == "Eiger2M": ccdraw, mymask = mask_eiger(ccdraw, mymask) elif mydetector == "Maxipix": ccdraw, mymask = mask_maxipix(ccdraw, mymask) ccdraw = myflatfield * ccdraw ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region) rawdata[int(i - ccdn[0]), :, :] = ccd elif mysetup == 'SIXS': nb_img = img.shape[0] rawdata = np.zeros((nb_img, region[1] - region[0], region[3] - region[2])) for index in range(nb_img): ccdraw = img[index, :, :] # first image is duplicated ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask) if mydetector == "Eiger2M": ccdraw, mymask = mask_eiger(ccdraw, mymask) elif mydetector == "Maxipix": ccdraw, mymask = mask_maxipix(ccdraw, mymask) ccdraw = myflatfield * ccdraw ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region) rawdata[index, :, :] = ccd elif mysetup == 'CRISTAL': nb_img = omega.shape[0] rawdata = np.zeros((nb_img, region[1] - region[0], region[3] - region[2])) for index in range(nb_img): ccdraw = maxpix_img[index, :, :] ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask) ccdraw, mymask = mask_maxipix(ccdraw, mymask) ccdraw = myflatfield * ccdraw ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region) rawdata[index, :, :] = ccd elif mysetup == 'P10': rawdata = np.zeros((nb_img, region[1] - region[0], region[3] - region[2])) for index in range(nb_img): h5file = h5py.File(ccdfiletmp % (index + 1), 'r') try: ccdraw = h5file['entry']['data']['data'][:].sum(axis=0) except OSError: print('hdf5plugin is not installed') sys.exit() ccdraw, mymask = remove_hotpixels(ccdraw, myhotpixels, mymask) ccdraw, mymask = mask_eiger4m(ccdraw, mymask) ccdraw = myflatfield * ccdraw ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region) rawdata[index, :, :] = ccd mymask = mymask[region[0]:region[1], region[2]:region[3]] rawdata, mymask = check_pixels(rawdata, mymask, var_threshold=5, debugging=0) # additional check for hotpixels numz, numy, numx = rawdata.shape rawmask3d = np.repeat(mymask[np.newaxis, :, :], numz, axis=0) rawmask3d[np.isnan(rawdata)] = 1 rawdata[np.isnan(rawdata)] = 0 else: rawmask3d = previous_mask rawdata = previous_data numz, numy, numx = rawdata.shape # transform scan angles to reciprocal space coordinates for all detector pixels if mysetup == 'ID01': myqx, myqy, myqz = hxrd.Ang2Q.area(eta, chi, phi, nu, delta, en=myenergy, delta=myoffsets) mygridder = xu.Gridder3D(numz, numy, numx) # convert mask to rectangular grid in reciprocal space mygridder(myqx, myqz, myqy, rawmask3d) mymask3d = np.copy(mygridder.data) # convert data to rectangular grid in reciprocal space mygridder(myqx, myqz, myqy, rawdata) return mygridder.xaxis, mygridder.yaxis, mygridder.zaxis, rawdata, mygridder.data, rawmask3d, mymask3d elif mysetup == 'SIXS': if myenergy is None: print('Defaulting energy to 8.5keV') myenergy = 8500 myqx, myqy, myqz = hxrd.Ang2Q.area(sixs_beta, mu, sixs_beta, gamma, delta, en=myenergy, delta=myoffsets) mygridder = xu.Gridder3D(numz, numy, numx) # convert mask to rectangular grid in reciprocal space mygridder(myqx, myqz, myqy, rawmask3d) mymask3d = np.copy(mygridder.data) # convert data to rectangular grid in reciprocal space mygridder(myqx, myqz, myqy, rawdata) return mygridder.xaxis, mygridder.yaxis, mygridder.zaxis, rawdata, mygridder.data, rawmask3d, mymask3d elif mysetup == 'CRISTAL': # TODO: implement this for CRISTAL print('Gridder not yet implemented for CRISTAL setup') return 0, 0, 0, rawdata, 0, rawmask3d, 0 elif mysetup == 'P10': # TODO: implement this for P10 print('Gridder not yet implemented for P10 setup') return 0, 0, 0, rawdata, 0, rawmask3d, 0 else: print('Wrong setup') sys.exit()
def processMap(self, **kwargs): """ read ad frames and grid them in reciprocal space angular coordinates are taken from the spec file **kwargs are passed to the rawmap function """ maxImageMem = self.appConfig.getMaxImageMemory() gridder = xu.Gridder3D(self.nx, self.ny, self.nz) gridder.KeepData(True) rangeBounds = self.dataSource.getRangeBounds() try: # repository version or xrayutilities > 1.0.6 gridder.dataRange(rangeBounds[0], rangeBounds[1], rangeBounds[2], rangeBounds[3], rangeBounds[4], rangeBounds[5], True) except: # xrayutilities 1.0.6 and below gridder.dataRange((rangeBounds[0], rangeBounds[1]), (rangeBounds[2], rangeBounds[3]), (rangeBounds[4], rangeBounds[5]), True) imageToBeUsed = self.dataSource.getImageToBeUsed() progress = 0 for scan in self.dataSource.getAvailableScans(): if True in imageToBeUsed[scan]: imageSize = self.dataSource.getDetectorDimensions()[0] * \ self.dataSource.getDetectorDimensions()[1] numImages = len(imageToBeUsed[scan]) if imageSize * 4 * numImages <= maxImageMem: kwargs['mask'] = imageToBeUsed[scan] qx, qy, qz, intensity = self.dataSource.rawmap((scan, ), **kwargs) # convert data to rectangular grid in reciprocal space gridder(qx, qy, qz, intensity) progress += 100 if self.progressUpdater is not None: self.progressUpdater(progress) else: nPasses = int(imageSize * 4 * numImages / maxImageMem + 1) for thisPass in range(nPasses): imageToBeUsedInPass = np.array(imageToBeUsed[scan]) imageToBeUsedInPass[:int(thisPass * numImages / nPasses)] = False imageToBeUsedInPass[int((thisPass + 1) * numImages / nPasses):] = False if True in imageToBeUsedInPass: kwargs['mask'] = imageToBeUsedInPass qx, qy, qz, intensity = \ self.dataSource.rawmap((scan,), **kwargs) # convert data to rectangular grid in reciprocal space try: gridder(qx, qy, qz, intensity) progress += 1.0 / nPasses * 100.0 if self.progressUpdater is not None: self.progressUpdater(progress) except InputError as ex: print("Wrong Input to gridder") print("qx Size: " + str(qx.shape)) print("qy Size: " + str(qy.shape)) print("qz Size: " + str(qz.shape)) print("intensity Size: " + str(intensity.shape)) raise InputError(ex) else: progress += 1.0 / nPasses * 100.0 if self.progressUpdater is not None: self.progressUpdater(progress) self.progressUpdater(100.0) return gridder.xaxis, gridder.yaxis, gridder.zaxis, gridder.data, gridder
def gridmap(specfile, scan_nb, mydetector, region=None, myflatfield=None, myhotpixels=""): global offset, offset_chi, offset_eta, offset_phi, filtered_data, datadir if region is None: if mydetector == "Eiger2M": region = [0, 2164, 0, 1030] elif mydetector == "Maxipix": region = [0, 516, 0, 516] if mydetector == "Eiger2M": counter = 'ei2minr' mymask = np.zeros((2164, 1030)) elif mydetector == "Maxipix": counter = 'mpx4inr' mymask = np.zeros((516, 516)) else: sys.exit("Incorrect value for 'mydetector' parameter") if myhotpixels != "": print("Loading hotpixels array") hotpix_array = np.load(myhotpixels) npz_key = hotpix_array.keys() hotpix_array = hotpix_array[npz_key[0]] if len(hotpix_array.shape) == 3: # 3D array hotpix_array = hotpix_array.sum(axis=0) hotpix_array[hotpix_array != 0] = -1 if myflatfield is None: myflatfield = np.ones(mymask.shape) motor_names = specfile[str(scan_nb) + '.1'].motor_names # positioners motor_positions = specfile[str(scan_nb) + '.1'].motor_positions # positioners labels = specfile[str(scan_nb) + '.1'].labels # motor scanned labels_data = specfile[str(scan_nb) + '.1'].data # motor scanned chi = offset_chi phi = offset_phi delta = motor_positions[motor_names.index('del')] nu = motor_positions[motor_names.index('nu')] eta = labels_data[labels.index('eta'), :] + offset_eta ccdn = labels_data[labels.index(counter), :] if filtered_data == 0: rawdata = np.zeros( (len(ccdn), region[1] - region[0], region[3] - region[2])) for index in range(len(ccdn)): i = int(ccdn[index]) e = fabio.open(ccdfiletmp % i) ccdraw = e.data if myhotpixels != "": ccdraw, mymask = remove_hotpixels(ccdraw, hotpix_array, mymask) if mydetector == "Eiger2M": ccdraw, mymask = mask_eiger(ccdraw, mymask) elif mydetector == "Maxipix": ccdraw, mymask = mask_maxipix(ccdraw, mymask) ccdraw = myflatfield * ccdraw ccd = xu.blockAverage2D(ccdraw, nav[0], nav[1], roi=region) rawdata[int(i - ccdn[0]), :, :] = ccd else: print('Loading filtered data') myfile_path = filedialog.askopenfilename(initialdir=specdir, title="Select 3D data", filetypes=[("NPZ", "*.npz")]) rawdata = np.load(myfile_path)['data'] rawdata = rawdata[region[0]:region[1], region[2]:region[3]] mymask = mymask[region[0]:region[1], region[2]:region[3]] numz, numy, numx = rawdata.shape if numz != len(ccdn): print('Filtered data has not the same shape as raw data') sys.exit() rawmask3d = np.zeros((numz, region[1] - region[0], region[3] - region[2])) for index in range(numz): rawmask3d[index, :, :] = mymask # transform scan angles to reciprocal space coordinates for all detector pixels myqx, myqy, myqz = hxrd.Ang2Q.area(eta, chi, phi, nu, delta, delta=(0, 0, 0, offset, 0)) mygridder = xu.Gridder3D(numz, numy, numx) # convert mask to rectangular grid in reciprocal space mygridder(myqx, myqz, myqy, rawmask3d) mymask3d = np.copy(mygridder.data) # convert data to rectangular grid in reciprocal space mygridder(myqx, myqz, myqy, rawdata) return mygridder.xaxis, mygridder.yaxis, mygridder.zaxis, rawdata, mygridder.data, rawmask3d, mymask3d