def update(self,update_propagator=True): """ Update the internal pixel sizes, giving precedence to the sample pixel size (psize_sam) if self.psize_is_fixed is True. """ # 4 cases if not self.p.psize_sam_is_fix and not self.p.psize_det_is_fix: # this is a rare case logger.debug('No pixel size is marked as constant. Setting detector pixel size as fix') self.psize_det_is_fix = True self.update() return elif not self.p.psize_sam_is_fix and self.p.psize_det_is_fix: if self.p.prop_type=='farfield': self.p.psize_sam[:] = self.lz / self.p.psize_det / self.p.N else: self.p.psize_sam[:] = self.p.psize_det elif self.p.psize_sam_is_fix and not self.p.psize_det_is_fix: if self.p.prop_type=='farfield': self.p.psize_det[:] = self.lz / self.p.psize_sam / self.p.N else: self.p.psize_det[:] = self.p.psize else: # both psizes are fix if self.p.prop_type=='farfield': # frame misfit that would make it work self.p.misfit[:] = self.lz / self.p.psize_sam / self.p.psize_det - self.p.N else: self.p.misfit[:] = self.p.psize_sam - self.p.psize_det # Update the propagator too (optionally pass the dictionary, # but Geometry & Propagator should share a dict if update_propagator: self.propagator.update(self.p)
def get_path(self, path): try: d = dict(self.runtime.iter_info[-1]) self.p.update(d) except: logger.debug("Receiving runtime info for dumping/saving failed") path = os.path.abspath(os.path.expanduser(path % self.p)) return path
def get_path(self, path): try: d = dict(self.runtime.iter_info[-1]) self.p.update(d) except: logger.debug('Receiving runtime info for dumping/saving failed') path = os.path.abspath(os.path.expanduser(path % self.p)) return path
def _initialize(self,pars=None,**kwargs): # Starting parameters p=u.Param(DEFAULT) if pars is not None: p.update(pars) for k,v in kwargs.iteritems(): if p.has_key(k): p[k] = v self.p = p self.interact = False # set distance if self.p.z is None or self.p.z==0: raise ValueError('Distance (geometry.z) must not be None or 0') # set frame shape if self.p.N is None or (np.array(self.p.N)==0).any(): raise ValueError('Frame size (geometry.N) must not be None or 0') else: self.p.N = u.expect2(p.N) # Set energy and wavelength if p.energy is None: if p.lam is None: raise ValueError('Wavelength (geometry.lam) and energy (geometry.energy)\n must not both be None') else: self.lam = p.lam # also sets energy else: if p.lam is not None: logger.debug('Energy and wavelength both specified. Energy takes precedence over wavelength') self.energy = p.energy # set initial geometrical misfit to 0 self.p.misfit = u.expect2(0.) # Pixel size self.p.psize_det_is_fix = p.psize_det is not None self.p.psize_sam_is_fix = p.psize_sam is not None if not self.p.psize_det_is_fix and not self.p.psize_sam_is_fix: raise ValueError('Pixel size in sample plane (geometry.psize_sam) and detector plane \n(geometry.psize_det) must not both be None') # fill pixel sizes self.p.psize_sam = u.expect2(p.psize_sam) if self.p.psize_sam_is_fix else u.expect2(1.0) self.p.psize_det = u.expect2(p.psize_det) if self.p.psize_det_is_fix else u.expect2(1.0) # update other values self.update(False) # attach propagator self._propagator = self._get_propagator() self.interact=True
def update(self, update_propagator=True): """ Update the internal pixel sizes, giving precedence to the sample pixel size (psize_sam) if self.psize_is_fixed is True. """ # 4 cases if not self.p.psize_sam_is_fix and not self.p.psize_det_is_fix: # this is a rare case logger.debug( 'No pixel size is marked as constant. Setting detector pixel size as fix' ) self.psize_det_is_fix = True self.update() return elif not self.p.psize_sam_is_fix and self.p.psize_det_is_fix: if self.p.prop_type == 'farfield': self.p.psize_sam[:] = self.lz / self.p.psize_det / self.p.N else: self.p.psize_sam[:] = self.p.psize_det elif self.p.psize_sam_is_fix and not self.p.psize_det_is_fix: if self.p.prop_type == 'farfield': self.p.psize_det[:] = self.lz / self.p.psize_sam / self.p.N else: self.p.psize_det[:] = self.p.psize else: # both psizes are fix if self.p.prop_type == 'farfield': # frame misfit that would make it work self.p.misfit[:] = self.lz / self.p.psize_sam / self.p.psize_det - self.p.N else: self.p.misfit[:] = self.p.psize_sam - self.p.psize_det # Update the propagator too (optionally pass the dictionary, # but Geometry & Propagator should share a dict if update_propagator: self.propagator.update(self.p)
def _initialize(self, pars=None, **kwargs): # Starting parameters p = u.Param(DEFAULT) if pars is not None: p.update(pars) for k, v in kwargs.iteritems(): if p.has_key(k): p[k] = v self.p = p self.interact = False # set distance if self.p.z is None or self.p.z == 0: raise ValueError('Distance (geometry.z) must not be None or 0') # set frame shape if self.p.N is None or (np.array(self.p.N) == 0).any(): raise ValueError('Frame size (geometry.N) must not be None or 0') else: self.p.N = u.expect2(p.N) # Set energy and wavelength if p.energy is None: if p.lam is None: raise ValueError( 'Wavelength (geometry.lam) and energy (geometry.energy)\n must not both be None' ) else: self.lam = p.lam # also sets energy else: if p.lam is not None: logger.debug( 'Energy and wavelength both specified. Energy takes precedence over wavelength' ) self.energy = p.energy # set initial geometrical misfit to 0 self.p.misfit = u.expect2(0.) # Pixel size self.p.psize_det_is_fix = p.psize_det is not None self.p.psize_sam_is_fix = p.psize_sam is not None if not self.p.psize_det_is_fix and not self.p.psize_sam_is_fix: raise ValueError( 'Pixel size in sample plane (geometry.psize_sam) and detector plane \n(geometry.psize_det) must not both be None' ) # fill pixel sizes self.p.psize_sam = u.expect2( p.psize_sam) if self.p.psize_sam_is_fix else u.expect2(1.0) self.p.psize_det = u.expect2( p.psize_det) if self.p.psize_det_is_fix else u.expect2(1.0) # update other values self.update(False) # attach propagator self._propagator = self._get_propagator() self.interact = True
def prepare(self, scan=None, filename=None, dtype=np.uint32, **kwargs): self.p.update( kwargs) #rebin = rebin if rebin is not None else self.p.rebin scan = scan if scan is not None else self.p.scan self.read(scan, **kwargs) DS = u.Param() dark = self.dark data = self.data if dark is not None: if dark.ndim == 3: dark = dark.mean(0) dark = np.resize(dark, data.shape) flat = self.flat if flat is not None: if flat.ndim == 3: flat = flat.mean(0) flat = np.resize(flat, self.data.shape) #plt.ion(); #plt.figure();plt.imshow(dark[0]);plt.colorbar() #plt.figure();plt.imshow((flat-dark)[0]);plt.colorbar() #plt.figure();plt.imshow(data[0]);plt.colorbar() if flat is not None and dark is not None: data = (data - dark) / (flat - dark) elif dark is not None: data = data - dark else: data = data # remove negative values data[data < 0] = 0 # #plt.figure();plt.imshow(DS.data[0],vmin=0);plt.colorbar() # if self.mask is None: mask = np.ones_like(data, dtype=np.bool) # #DS.flat = self.flat #DS.dark = self.dark DS.scan_info = u.Param() s = DS.scan_info p = self.p #s.scan_number = p.scan_number s.scan_label = 'S' + self.label #'S%05d' % p.scan_number s.data_filename = self.scan #scandict['data_filename'] s.wavelength = u.keV2m(p.energy) s.energy = p.energy rebin = self.p.rebin s.detector_pixel_size = p.detector_pixel_size * rebin if p.detector_pixel_size is not None else None p.dpsize = p.dpsize / rebin s.detector_distance = p.detector_distance s.initial_ctr = self.ctr / rebin if rebin != 1: sh = data.shape data = u.rebin(data, sh[0], sh[1] / rebin, sh[2] / rebin) mask = u.rebin(mask.astype(int), sh[0], sh[1] / rebin, sh[2] / rebin).astype(bool) data = flip(data, self.p.flip) mask = flip(mask, self.p.flip) DS.data = data.astype(dtype) DS.mask = mask DS.data[np.invert(DS.mask)] = 0 #s.date_collected = scandict['date'] s.date_processed = time.asctime() s.exposure_time = self.exp #if meta is not None: s.raw_filenames = meta['filename'] s.preparation_basepath = self.base_path s.preparation_other = {} s.shape = DS.data.shape s.positions_theory = None s.scan_command = self.command motors = p.motors if self.motors is not None: Nmotors = len(motors) logger.debug('Motors are : %s' % str(p.motors)) mmult = u.expect2(p.motors_multiplier) pos_list = [ mmult[i] * np.array(self.motors[motors[i]]) for i in range(Nmotors) ] s.positions = np.array(pos_list).T else: s.positions = None self.p.scan_label = s.scan_label if filename is None: p.write_path = WRITE_PATH_PATTERN.format(**p) filename = SAVE_FILENAME_PATTERN.format(**p) s.data_filename = u.clean_path(filename) io.h5write(filename, DS) return DS
def read(self, scan=None, **kwargs): """\ Read in the data TODO: (maybe?) MPI to avoid loading all data in a single process for large scans. """ scan = scan if scan is not None else self.p.scan logger.info('Processing scan number %s' % str(scan)) self.scan = self.get_nexus_file(scan) logger.debug('Data will be read from path: %s' % self.scan) self.exp = load(self.scan, self.nxs.frame) try: self.motors = load(self.scan, self.nxs.motors) except: self.motors = None self.command = load(self.scan, self.nxs.command) self.data = load(self.scan, self.nxs.frame).astype(float) self.label = load(self.scan, self.nxs.label)[0] if self.p.experimentID is None: try: experimentID = load(self.scan, self.nxs.experiment)[0] except: logger.debug( 'Could not find experiment ID from nexus file %s.' % self.scan) experimentID = os.path.split(base_path[:-1])[1] logger.debug('experimentID: "%s" (automatically set).' % experimentID) else: logger.debug('experimentID: "%s".' % self.p.experimentID) self.experimentID = self.p.experimentID dpsize = self.p.dpsize ctr = self.p.ctr sh = self.data.shape[-2:] fullframe = False if dpsize is None: dpsize = sh logger.debug( 'Full frames (%d x %d) will be saved (so no recentering).' % (sh)) fullframe = True self.p.dpsize = expect2(dpsize) #data_filename = self.get_save_filename(scan_number, dpsize) #logger.info( 'Data will be saved to %s' % data_filename) f = self.data if not fullframe: # Compute center of mass if self.mask is None: ctr_auto = mass_center(f.sum(0)) else: ctr_auto = mass_center(f.sum(0) * self.mask) print ctr_auto # Check for center position if ctr is None: ctr = ctr_auto logger.debug('Using center: (%d, %d)' % (ctr[0], ctr[1])) #elif ctr == 'inter': #import matplotlib as mpl #fig = mpl.pyplot.figure() #ax = fig.add_subplot(1,1,1) #ax.imshow(np.log(f)) #ax.set_title('Select center point (hit return to finish)') #s = u.Multiclicks(ax, True, mode='replace') #mpl.pyplot.show() #s.wait_until_closed() #ctr = np.round(np.array(s.pts[0][::-1])); #logger.debug( 'Using center: (%d, %d) - I would have guessed it is (%d, %d)' % (ctr[0], ctr[1], ctr_auto[0], ctr_auto[1])) else: logger.debug( 'Using center: (%d, %d) - I would have guessed it is (%d, %d)' % (ctr[0], ctr[1], ctr_auto[0], ctr_auto[1])) self.dpsize = dpsize self.ctr = np.array(ctr) lim_inf = -np.ceil(ctr - dpsize / 2.).astype(int) lim_sup = np.ceil(ctr + dpsize / 2.).astype(int) - np.array(sh) hplane_list = [(lim_inf[0], lim_sup[0]), (lim_inf[1], lim_sup[1])] logger.debug('Going from %s to %s (hplane_list = %s)' % (str(sh), str(dpsize), str(hplane_list))) if self.mask is not None: self.mask = u.crop_pad(self.mask, hplane_list).astype(bool) if self.flat is not None: self.flat = u.crop_pad(self.flat, hplane_list, fillpar=1.) if self.dark is not None: self.dark = u.crop_pad(self.dark, hplane_list) if self.data is not None: self.data = u.crop_pad(self.data, hplane_list)
def __init__( self, pars=None ): #base_path, experimentID=None, mask=None, flat=None, dark=None): """\ I13 DLS data reading class. The only mendatory argument for this constructor are 'base_path' and 'user'. Mask, flat or dark should be either None, scan numbers (interger) or file paths """ # Semi-smart base path detection: we are looking for a "raw" directory p = u.Param(DEFAULT) if pars is not None: p.update(pars) try: verbose_level = p.verbose_level verbose.set_level(verbose_level) except: pass self.p = p base_path = p.base_path if p.base_path.endswith( '/') else p.base_path + '/' experimentID = p.experimentID if base_path is None: d = os.getcwd() while True: if 'raw' in os.listdir(d): base_path = d break d, rest = os.path.split(d) if not rest: break if base_path is None: raise RuntimeError('Could not figure out base_path') logger.debug('base_path: "%s" (automatically set).' % base_path) else: logger.debug('base_path: "%s".' % base_path) self.base_path = base_path self.nxs = u.Param() self.nxs.frame = FRAME_IN_NEXUS_FILE.format(**p) self.nxs.exp = EXPOSURE_IN_NEXUS_FILE.format(**p) self.nxs.motors = MOTORS_IN_NEXUS_FILE.format(**p) self.nxs.command = COMMAND_IN_NEXUS_FILE.format(**p) self.nxs.experiment = EXPERIMENT_IN_NEXUS_FILE.format(**p) self.nxs.label = LABEL_IN_NEXUS_FILE.format(**p) # Load mask, flat and dark try: self.mask = load(self.get_nexus_file(p.mask), self.nxs.frame) except: self.mask = p.mask #assert self.mask.shape[-2:] == sh try: self.dark = load(self.get_nexus_file(p.dark), self.nxs.frame) except: self.dark = p.dark #assert self.dark.shape[-2:] == sh try: self.flat = load(self.get_nexus_file(p.flat), self.nxs.frame) except: self.flat = p.flat
def __init__(self, pars=None):#base_path, experimentID=None, mask=None, flat=None, dark=None): """\ I13 DLS data reading class. The only mendatory argument for this constructor are 'base_path' and 'user'. Mask, flat or dark should be either None, scan numbers (interger) or file paths """ # Semi-smart base path detection: we are looking for a "raw" directory p = u.Param(DEFAULT) if pars is not None: p.update(pars) try: verbose_level = p.verbose_level verbose.set_level(verbose_level) except: pass self.p=p base_path = p.base_path if p.base_path.endswith('/') else p.base_path + '/' experimentID = p.experimentID if base_path is None: d = os.getcwd() while True: if 'raw' in os.listdir(d): base_path = d break d,rest = os.path.split(d) if not rest: break if base_path is None: raise RuntimeError('Could not figure out base_path') logger.debug( 'base_path: "%s" (automatically set).' % base_path) else: logger.debug( 'base_path: "%s".' % base_path) self.base_path = base_path self.nxs = u.Param() self.nxs.frame = FRAME_IN_NEXUS_FILE.format(**p) self.nxs.exp = EXPOSURE_IN_NEXUS_FILE.format(**p) self.nxs.motors = MOTORS_IN_NEXUS_FILE.format(**p) self.nxs.command = COMMAND_IN_NEXUS_FILE.format(**p) self.nxs.experiment = EXPERIMENT_IN_NEXUS_FILE.format(**p) self.nxs.label = LABEL_IN_NEXUS_FILE.format(**p) # Load mask, flat and dark try: self.mask = load(self.get_nexus_file(p.mask),self.nxs.frame) except: self.mask = p.mask #assert self.mask.shape[-2:] == sh try: self.dark = load(self.get_nexus_file(p.dark),self.nxs.frame) except: self.dark = p.dark #assert self.dark.shape[-2:] == sh try: self.flat = load(self.get_nexus_file(p.flat),self.nxs.frame) except: self.flat = p.flat
def prepare(self,scan=None,filename=None,dtype=np.uint32,**kwargs): self.p.update(kwargs) #rebin = rebin if rebin is not None else self.p.rebin scan = scan if scan is not None else self.p.scan self.read( scan, **kwargs) DS = u.Param() dark = self.dark data = self.data if dark is not None: if dark.ndim == 3: dark = dark.mean(0) dark = np.resize(dark,data.shape) flat = self.flat if flat is not None: if flat.ndim == 3: flat = flat.mean(0) flat = np.resize(flat,self.data.shape) #plt.ion(); #plt.figure();plt.imshow(dark[0]);plt.colorbar() #plt.figure();plt.imshow((flat-dark)[0]);plt.colorbar() #plt.figure();plt.imshow(data[0]);plt.colorbar() if flat is not None and dark is not None: data = (data-dark )/(flat-dark) elif dark is not None: data = data - dark else: data = data # remove negative values data[data<0]=0 # #plt.figure();plt.imshow(DS.data[0],vmin=0);plt.colorbar() # if self.mask is None: mask = np.ones_like(data,dtype=np.bool) # #DS.flat = self.flat #DS.dark = self.dark DS.scan_info = u.Param() s = DS.scan_info p = self.p #s.scan_number = p.scan_number s.scan_label = 'S'+self.label #'S%05d' % p.scan_number s.data_filename = self.scan #scandict['data_filename'] s.wavelength = u.keV2m( p.energy ) s.energy = p.energy rebin = self.p.rebin s.detector_pixel_size = p.detector_pixel_size * rebin if p.detector_pixel_size is not None else None p.dpsize = p.dpsize / rebin s.detector_distance = p.detector_distance s.initial_ctr = self.ctr / rebin if rebin!=1: sh = data.shape data = u.rebin(data,sh[0],sh[1]/rebin,sh[2]/rebin) mask = u.rebin(mask.astype(int),sh[0],sh[1]/rebin,sh[2]/rebin).astype(bool) data = flip(data,self.p.flip) mask = flip(mask,self.p.flip) DS.data = data.astype(dtype) DS.mask = mask DS.data[np.invert(DS.mask)]=0 #s.date_collected = scandict['date'] s.date_processed = time.asctime() s.exposure_time = self.exp #if meta is not None: s.raw_filenames = meta['filename'] s.preparation_basepath = self.base_path s.preparation_other = {} s.shape = DS.data.shape s.positions_theory = None s.scan_command = self.command motors = p.motors if self.motors is not None: Nmotors = len(motors) logger.debug( 'Motors are : %s' % str(p.motors)) mmult = u.expect2(p.motors_multiplier) pos_list = [mmult[i]*np.array(self.motors[motors[i]]) for i in range(Nmotors)] s.positions = np.array(pos_list).T else: s.positions = None self.p.scan_label = s.scan_label if filename is None: p.write_path = WRITE_PATH_PATTERN.format(**p) filename = SAVE_FILENAME_PATTERN.format(**p) s.data_filename = u.clean_path(filename) io.h5write(filename,DS) return DS
def read(self, scan=None, **kwargs): """\ Read in the data TODO: (maybe?) MPI to avoid loading all data in a single process for large scans. """ scan=scan if scan is not None else self.p.scan logger.info( 'Processing scan number %s' % str(scan)) self.scan = self.get_nexus_file(scan) logger.debug( 'Data will be read from path: %s' % self.scan) self.exp = load(self.scan,self.nxs.frame) try: self.motors = load(self.scan,self.nxs.motors) except: self.motors=None self.command = load(self.scan,self.nxs.command) self.data = load(self.scan, self.nxs.frame).astype(float) self.label = load(self.scan, self.nxs.label)[0] if self.p.experimentID is None: try: experimentID = load(self.scan, self.nxs.experiment)[0] except: logger.debug('Could not find experiment ID from nexus file %s.' % self.scan) experimentID = os.path.split(base_path[:-1])[1] logger.debug( 'experimentID: "%s" (automatically set).' % experimentID) else: logger.debug( 'experimentID: "%s".' % self.p.experimentID) self.experimentID = self.p.experimentID dpsize = self.p.dpsize ctr = self.p.ctr sh = self.data.shape[-2:] fullframe = False if dpsize is None: dpsize = sh logger.debug( 'Full frames (%d x %d) will be saved (so no recentering).' % (sh)) fullframe = True self.p.dpsize = expect2(dpsize) #data_filename = self.get_save_filename(scan_number, dpsize) #logger.info( 'Data will be saved to %s' % data_filename) f = self.data if not fullframe: # Compute center of mass if self.mask is None: ctr_auto = mass_center(f.sum(0)) else: ctr_auto = mass_center(f.sum(0)*self.mask) print ctr_auto # Check for center position if ctr is None: ctr = ctr_auto logger.debug( 'Using center: (%d, %d)' % (ctr[0],ctr[1])) #elif ctr == 'inter': #import matplotlib as mpl #fig = mpl.pyplot.figure() #ax = fig.add_subplot(1,1,1) #ax.imshow(np.log(f)) #ax.set_title('Select center point (hit return to finish)') #s = u.Multiclicks(ax, True, mode='replace') #mpl.pyplot.show() #s.wait_until_closed() #ctr = np.round(np.array(s.pts[0][::-1])); #logger.debug( 'Using center: (%d, %d) - I would have guessed it is (%d, %d)' % (ctr[0], ctr[1], ctr_auto[0], ctr_auto[1])) else: logger.debug( 'Using center: (%d, %d) - I would have guessed it is (%d, %d)' % (ctr[0], ctr[1], ctr_auto[0], ctr_auto[1])) self.dpsize = dpsize self.ctr = np.array(ctr) lim_inf = -np.ceil(ctr - dpsize/2.).astype(int) lim_sup = np.ceil(ctr + dpsize/2.).astype(int) - np.array(sh) hplane_list = [(lim_inf[0], lim_sup[0]), (lim_inf[1], lim_sup[1])] logger.debug( 'Going from %s to %s (hplane_list = %s)' % (str(sh), str(dpsize), str(hplane_list))) if self.mask is not None: self.mask = u.crop_pad(self.mask, hplane_list).astype(bool) if self.flat is not None: self.flat = u.crop_pad(self.flat, hplane_list,fillpar=1.) if self.dark is not None: self.dark = u.crop_pad(self.dark, hplane_list) if self.data is not None: self.data = u.crop_pad(self.data, hplane_list)