def GetColumnMetadata(reader): '''Add metadata to an image from a column-type metadata file using :func:`readColMetadata` :param reader: a reader object from reading an image ''' if not GSASIIpath.GetConfigValue('Column_Metadata_directory'): return parParms = readColMetadata(reader.readfilename) if not parParms: return # check for read failure specialKeys = ('filename',"polarization", "center", "distance", "pixelSize", "wavelength",) reader.Comments = ['Metadata from {} assigned by {}'.format(parParms['par file'],parParms['lbls file'])] for key in parParms: if key in specialKeys+('par file','lbls file'): continue reader.Comments += ["{} = {}".format(key,parParms[key])] if "polarization" in parParms: reader.Data['PolaVal'][0] = parParms["polarization"] else: reader.Data['PolaVal'][0] = 0.99 if "center" in parParms: reader.Data['center'] = parParms["center"] if "pixelSize" in parParms: reader.Data['pixelSize'] = parParms["pixelSize"] if "wavelength" in parParms: reader.Data['wavelength'] = parParms['wavelength'] else: G2Print('Error: wavelength not defined in {}'.format(parParms['lbls file'])) if "distance" in parParms: reader.Data['distance'] = parParms['distance'] reader.Data['setdist'] = parParms['distance'] else: G2Print('Error: distance not defined in {}'.format(parParms['lbls file']))
def Reader(self, filename, ParentFrame=None, **kwarg): '''Scan file structure using :meth:`visit` and map out locations of image(s) then read one image using :meth:`readDataset`. Save map of file structure in buffer arg, if used. ''' imagenum = kwarg.get('blocknum') if imagenum is None: imagenum = 1 self.buffer = kwarg.get('buffer', {}) try: fp = h5py.File(filename, 'r') if not self.buffer.get('init'): self.buffer['init'] = True self.Comments = self.visit(fp) if imagenum > len(self.buffer['imagemap']): self.errors = 'No valid images found in file' return False self.Data, self.Npix, self.Image = self.readDataset(fp, imagenum) if self.Npix == 0: self.errors = 'No valid images found in file' return False self.LoadImage(ParentFrame, filename, imagenum) self.repeatcount = imagenum self.repeat = imagenum < len(self.buffer['imagemap']) if GSASIIpath.GetConfigValue('debug'): print('Read image #' + str(imagenum) + ' from file ' + filename) return True except IOError: print('cannot open file ' + filename) return False finally: fp.close()
def Reader(self, filename, ParentFrame=None, **unused): 'Read a csv file' x = [] y = [] w = [] fp = open(filename, 'r') for i, S in enumerate(fp): vals = S.replace(',', ' ').replace(';', ' ').split() if len(vals) < 2 and i > 0: print('Line ' + str(i + 1) + ' cannot be read:\n\t' + S) continue try: x.append(float(vals[0])) f = float(vals[1]) if f <= 0.0: y.append(0.0) w.append(0.0) elif len(vals) == 3: y.append(float(vals[1])) w.append(1.0 / float(vals[2])**2) else: y.append(float(vals[1])) w.append(1.0 / float(vals[1])) err = False except ValueError: err = True msg = 'Error parsing number in line ' + str(i + 1) except: err = True msg = 'Error in line ' + str(i + 1) if err and i > 0: if GSASIIpath.GetConfigValue('debug'): print(msg) print(S.strip()) break fp.close() N = len(x) self.powderdata = [ np.array(x), # x-axis values np.array(y), # powder pattern intensities np.array(w), # 1/sig(intensity)^2 values (weights) np.zeros(N), # calc. intensities (zero) np.zeros(N), # calc. background (zero) np.zeros(N), # obs-calc profiles ] self.powderentry[0] = filename #self.powderentry[1] = pos # bank offset (N/A here) #self.powderentry[2] = 1 # xye file only has one bank self.idstring = ospath.basename(filename) # scan comments for temperature Temperature = 300 for S in self.comments: if 'Temp' in S.split('=')[0]: try: Temperature = float(S.split('=')[1]) except: pass self.Sample['Temperature'] = Temperature return True
def RereadImageData(ImageReaderlist,imagefile,ImageTag=None,FormatName=''): '''Read a single image with an image importer. This is called to reread an image after it has already been imported, so it is not necessary to reload metadata. Based on :func:`GetImageData.GetImageData` which this can replace where imageOnly=True :param list ImageReaderlist: list of Reader objects for images :param str imagefile: name of image file :param int/str ImageTag: specifies a particular image to be read from a file. First image is read if None (default). :param str formatName: the image reader formatName :returns: an image as a numpy array ''' # determine which formats are compatible with this file primaryReaders = [] secondaryReaders = [] for rd in ImageReaderlist: flag = rd.ExtensionValidator(imagefile) if flag is None: secondaryReaders.append(rd) elif flag: if not FormatName: primaryReaders.append(rd) elif FormatName == rd.formatName: primaryReaders.append(rd) if len(secondaryReaders) + len(primaryReaders) == 0: G2Print('Error: No matching format for file '+imagefile) raise Exception('No image read') errorReport = '' if not imagefile: return for rd in primaryReaders+secondaryReaders: rd.ReInitialize() # purge anything from a previous read rd.errors = "" # clear out any old errors if not rd.ContentsValidator(imagefile): # rejected on cursory check errorReport += "\n "+rd.formatName + ' validator error' if rd.errors: errorReport += ': '+rd.errors continue flag = rd.Reader(imagefile,None,blocknum=ImageTag) if flag: # this read succeeded if rd.Image is None: raise Exception('No image read. Strange!') if GSASIIpath.GetConfigValue('Transpose'): G2Print ('Warning: Transposing Image!') rd.Image = rd.Image.T #rd.readfilename = imagefile return rd.Image else: G2Print('Error reading file '+imagefile) G2Print('Error messages(s)\n'+errorReport) raise Exception('No image read')
def LookupFromTable(dist, parmList): '''Interpolate image parameters for a supplied distance value :param float dist: distance to use for interpolation :returns: a list with 2 items: * a dict with interpolated parameter values, * the closest imctrl ''' cols, parms, IMfileList, ParmList, nonInterpVars, ControlsTable, MaskTable = parmList x = np.array([float(i) for i in parms[0]]) closest = abs(x - dist).argmin() D = {'setdist': dist} imctfile = IMfileList[closest] for c in range(1, cols - 1): lbl = ParmList[c] if lbl in nonInterpVars: if lbl in [ 'outChannels', ]: D[lbl] = int(float(parms[c][closest])) else: D[lbl] = float(parms[c][closest]) else: y = np.array([float(i) for i in parms[c]]) D[lbl] = np.interp(dist, x, y) # full integration when angular range is 0 D['fullIntegrate'] = (D['LRazimuth_min'] == D['LRazimuth_max']) # conversion for paired values for a, b in ('center_x', 'center_y'), ('LRazimuth_min', 'LRazimuth_max'), ('IOtth_min', 'IOtth_max'): r = a.split('_')[0] D[r] = [D[a], D[b]] if r in [ 'LRazimuth', ]: D[r] = [int(D[a]), int(D[b])] del D[a] del D[b] interpDict, imgctrl = D, imctfile if GSASIIpath.GetConfigValue('debug'): print('DBG_interpolated values: ', interpDict) f = os.path.split(imgctrl)[1] ImageControls = ControlsTable[f] ImageControls.update(interpDict) ImageControls['showLines'] = True ImageControls['ring'] = [] ImageControls['rings'] = [] ImageControls['ellipses'] = [] ImageControls['setDefault'] = False for i in 'range', 'size', 'GonioAngles': if i in ImageControls: del ImageControls[i] ImageMasks = MaskTable.get(f) return ImageControls, ImageMasks
def InitMP(allowMP=True): '''Called to initialize use of Multiprocessing ''' global useMP, ncores if ncores is not None: return useMP, ncores useMP = False if not allowMP: G2fil.G2Print('Multiprocessing disabled') ncores = 0 return useMP, ncores ncores = GSASIIpath.GetConfigValue('Multiprocessing_cores', 0) if ncores < 0: ncores = mp.cpu_count() // 2 if ncores > 1: useMP = True if useMP: G2fil.G2Print('Multiprocessing with {} cores enabled'.format(ncores)) return useMP, ncores
def LoadProject(self, fil): '''Load the Covariance entry from a .GPX file to the tree. see :func:`GSASIIIO.ProjFileOpen` ''' G2frame = self filep = open(fil, 'rb') shortname = os.path.splitext(os.path.split(fil)[1])[0] wx.BeginBusyCursor() Phases = None try: while True: try: data = cPickleLoad(filep) except EOFError: break if not data[0][0].startswith('Covariance'): continue Covar = data[0] #GSASIIpath.IPyBreak_base() #if self.PWDRfilter is not None: # implement filter # if self.PWDRfilter not in data[0][0]: continue Covar[0] = shortname + ' Covariance' Id = G2frame.GPXtree.AppendItem(parent=G2frame.root, text=Covar[0]) G2frame.GPXtree.SetItemPyData(Id, Covar[1]) break else: print("{} does not have refinement results".format(shortname)) except Exception as errmsg: if GSASIIpath.GetConfigValue('debug'): print('\nError reading GPX file:', errmsg) import traceback print(traceback.format_exc()) msg = wx.MessageDialog(G2frame, message="Error reading file " + str(fil) + ". This is not a current GSAS-II .gpx file", caption="Load Error", style=wx.ICON_ERROR | wx.OK | wx.STAY_ON_TOP) msg.ShowModal() finally: filep.close() wx.EndBusyCursor() self.GPXtree.Expand(self.root)
def Reader(self,filename, ParentFrame=None, **unused): 'Read a Topas file' x = [] y = [] w = [] gotCcomment = False begin = True fp = open(filename,'r') for i,S in enumerate(fp): self.errors = 'Error reading line: '+str(i+1) # or a block of comments delimited by /* and */ # or (GSAS style) each line can begin with '#' # or WinPLOTR style, a '!' if begin: if self.Chi or self.Wave: if i < 4: continue else: begin = False else: if gotCcomment and S.find('*/') > -1: self.comments.append(S[:-1]) begin = False continue if S.strip().startswith('/*'): self.comments.append(S[:-1]) gotCcomment = True continue if S[0] in ["'",'#','!']: self.comments.append(S[:-1]) continue #ignore comments, if any elif i == 0 and 'xydata' in S.lower(): continue # fullprof header elif S.startswith('TITLE'): self.comments = [S] continue else: begin = False # valid line to read #vals = S.split() vals = S.replace(',',' ').replace(';',' ').split() if len(vals) < 2: print ('Line '+str(i+1)+' cannot be read:\n\t'+S) continue try: if self.Wave: try: val = min(0.995,self.Wave/(4.*np.pi/float(vals[0]))) #set max at 168deg x.append(2.0*asind(val)) except: msg = 'Error converting '+str(vals[0] ) + 'with wavelength ' + str(self.Wave) break else: x.append(float(vals[0])) f = float(vals[1]) if f <= 0.0: y.append(0.0) w.append(0.0) elif len(vals) == 3: y.append(float(vals[1])) w.append(1.0/float(vals[2])**2) else: y.append(float(vals[1])) w.append(1.0/float(vals[1])) except ValueError: msg = 'Error parsing number in line '+str(i+1) if GSASIIpath.GetConfigValue('debug'): print (msg) print (S.strip()) break except: msg = 'Error in line '+str(i+1) if GSASIIpath.GetConfigValue('debug'): print (msg) print (S.strip()) break N = len(x) self.powderdata = [ np.array(x), # x-axis values np.array(y), # powder pattern intensities np.array(w), # 1/sig(intensity)^2 values (weights) np.zeros(N), # calc. intensities (zero) np.zeros(N), # calc. background (zero) np.zeros(N), # obs-calc profiles ] self.powderentry[0] = filename #self.powderentry[1] = pos # bank offset (N/A here) #self.powderentry[2] = 1 # xye file only has one bank self.idstring = ospath.basename(filename) # scan comments for temperature Temperature = 300 for S in self.comments: if 'temp' in S.lower().split('=')[0]: try: Temperature = float(S.split('=')[1]) except: pass self.Sample['Temperature'] = Temperature fp.close() return True
def GetTifData(filename): '''Read an image in a pseudo-tif format, as produced by a wide variety of software, almost always incorrectly in some way. ''' import struct as st import array as ar import ReadMarCCDFrame as rmf image = None File = open(filename, 'rb') dataType = 5 center = [None, None] wavelength = None distance = None polarization = None samplechangerpos = None try: Meta = open(filename + '.metadata', 'Ur') head = Meta.readlines() for line in head: line = line.strip() try: if '=' not in line: continue keyword = line.split('=')[0].strip() if 'dataType' == keyword: dataType = int(line.split('=')[1]) elif 'wavelength' == keyword.lower(): wavelength = float(line.split('=')[1]) elif 'distance' == keyword.lower(): distance = float(line.split('=')[1]) elif 'polarization' == keyword.lower(): polarization = float(line.split('=')[1]) elif 'samplechangercoordinate' == keyword.lower(): samplechangerpos = float(line.split('=')[1]) except: G2fil.G2Print('error reading metadata: ' + line) Meta.close() except IOError: G2fil.G2Print('no metadata file found - will try to read file anyway') head = [ 'no metadata file found', ] tag = File.read(2) if 'bytes' in str(type(tag)): tag = tag.decode('latin-1') byteOrd = '<' if tag == 'II' and int(st.unpack('<h', File.read(2))[0]) == 42: #little endian IFD = int(st.unpack(byteOrd + 'i', File.read(4))[0]) elif tag == 'MM' and int(st.unpack('>h', File.read(2))[0]) == 42: #big endian byteOrd = '>' IFD = int(st.unpack(byteOrd + 'i', File.read(4))[0]) else: # print (tag) lines = [ 'not a detector tiff file', ] return lines, 0, 0, 0 File.seek(IFD) #get number of directory entries NED = int(st.unpack(byteOrd + 'h', File.read(2))[0]) IFD = {} nSlice = 1 if DEBUG: print('byteorder:', byteOrd) for ied in range(NED): Tag, Type = st.unpack(byteOrd + 'Hh', File.read(4)) nVal = st.unpack(byteOrd + 'i', File.read(4))[0] if DEBUG: print('Try:', Tag, Type, nVal) if Type == 1: Value = st.unpack(byteOrd + nVal * 'b', File.read(nVal)) elif Type == 2: Value = st.unpack(byteOrd + 'i', File.read(4)) elif Type == 3: Value = st.unpack(byteOrd + nVal * 'h', File.read(nVal * 2)) st.unpack(byteOrd + nVal * 'h', File.read(nVal * 2)) elif Type == 4: if Tag in [273, 279]: nSlice = nVal nVal = 1 Value = st.unpack(byteOrd + nVal * 'i', File.read(nVal * 4)) elif Type == 5: Value = st.unpack(byteOrd + nVal * 'i', File.read(nVal * 4)) elif Type == 11: Value = st.unpack(byteOrd + nVal * 'f', File.read(nVal * 4)) IFD[Tag] = [Type, nVal, Value] if DEBUG: print(Tag, IFD[Tag]) sizexy = [IFD[256][2][0], IFD[257][2][0]] [nx, ny] = sizexy Npix = nx * ny time0 = time.time() if 34710 in IFD: G2fil.G2Print('Read MAR CCD tiff file: ' + filename) marFrame = rmf.marFrame(File, byteOrd, IFD) image = np.flipud(np.array(np.asarray(marFrame.image), dtype=np.int32)) tifType = marFrame.filetitle pixy = [marFrame.pixelsizeX / 1000.0, marFrame.pixelsizeY / 1000.0] head = marFrame.outputHead() # extract resonable wavelength from header wavelength = marFrame.sourceWavelength * 1e-5 wavelength = (marFrame.opticsWavelength > 0) and marFrame.opticsWavelength * 1e-5 or wavelength wavelength = (wavelength <= 0) and None or wavelength # extract resonable distance from header distance = (marFrame.startXtalToDetector + marFrame.endXtalToDetector) * 5e-4 distance = (distance <= marFrame.startXtalToDetector * 5e-4) and marFrame.xtalToDetector * 1e-3 or distance distance = (distance <= 0) and None or distance # extract resonable center from header center = [ marFrame.beamX * marFrame.pixelsizeX * 1e-9, marFrame.beamY * marFrame.pixelsizeY * 1e-9 ] center = (center[0] != 0 and center[1] != 0) and center or [None, None] #print head,tifType,pixy elif nSlice > 1: #CheMin multislice tif file! try: import Image as Im except ImportError: try: from PIL import Image as Im except ImportError: G2fil.G2Print( "PIL/pillow Image module not present. This TIF cannot be read without this" ) #raise Exception("PIL/pillow Image module not found") lines = [ 'not a detector tiff file', ] return lines, 0, 0, 0 tifType = 'CheMin' pixy = [40., 40.] image = np.flipud(np.array(Im.open(filename))) * 10. distance = 18.0 center = [pixy[0] * sizexy[0] / 2000, 0] #the CheMin beam stop is here wavelength = 1.78892 elif 272 in IFD: ifd = IFD[272] File.seek(ifd[2][0]) S = File.read(ifd[1]) if b'PILATUS' in S: tifType = 'Pilatus' dataType = 0 pixy = [172., 172.] File.seek(4096) G2fil.G2Print('Read Pilatus tiff file: ' + filename) image = np.array(np.frombuffer(File.read(4 * Npix), dtype=np.int32), dtype=np.int32) else: if IFD[258][2][0] == 16: if sizexy == [3888, 3072] or sizexy == [3072, 3888]: tifType = 'Dexela' pixy = [74.8, 74.8] G2fil.G2Print('Read Dexela detector tiff file: ' + filename) else: tifType = 'GE' pixy = [200., 200.] G2fil.G2Print('Read GE-detector tiff file: ' + filename) File.seek(8) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif IFD[258][2][0] == 32: # includes CHESS & Pilatus files from Area Detector tifType = 'CHESS' pixy = [200., 200.] File.seek(8) G2fil.G2Print('Read as 32-bit unsigned (CHESS) tiff file: ' + filename) image = np.array(ar.array('I', File.read(4 * Npix)), dtype=np.uint32) elif 270 in IFD: File.seek(IFD[270][2][0]) S = File.read(IFD[273][2][0] - IFD[270][2][0]) if b'ImageJ' in S: tifType = 'ImageJ' dataType = 0 pixy = [200., 200.] * IFD[277][2][0] File.seek(IFD[273][2][0]) G2fil.G2Print('Read ImageJ tiff file: ' + filename) if IFD[258][2][0] == 32: image = File.read(4 * Npix) image = np.array(np.frombuffer(image, dtype=byteOrd + 'i4'), dtype=np.int32) elif IFD[258][2][0] == 16: image = File.read(2 * Npix) pixy = [109.92, 109.92] #for LCLS ImageJ tif files image = np.array(np.frombuffer(image, dtype=byteOrd + 'u2'), dtype=np.int32) else: #gain map from 11-ID-C? pixy = [200., 200.] tifType = 'Gain map' image = File.read(4 * Npix) image = np.array(np.frombuffer(image, dtype=byteOrd + 'f4') * 1000, dtype=np.int32) elif 262 in IFD and IFD[262][2][0] > 4: tifType = 'DND' pixy = [158., 158.] File.seek(512) G2fil.G2Print('Read DND SAX/WAX-detector tiff file: ' + filename) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif sizexy == [1536, 1536]: tifType = 'APS Gold' pixy = [150., 150.] File.seek(64) G2fil.G2Print('Read Gold tiff file:' + filename) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif sizexy == [2048, 2048] or sizexy == [1024, 1024 ] or sizexy == [3072, 3072]: if IFD[273][2][0] == 8: if IFD[258][2][0] == 32: tifType = 'PE' pixy = [200., 200.] File.seek(8) G2fil.G2Print('Read APS PE-detector tiff file: ' + filename) if dataType == 5: image = np.array(np.frombuffer(File.read(4 * Npix), dtype=np.float32), dtype=np.int32) #fastest else: image = np.array(np.frombuffer(File.read(4 * Npix), dtype=np.int32), dtype=np.int32) elif IFD[258][2][0] == 16: tifType = 'MedOptics D1' pixy = [46.9, 46.9] File.seek(8) G2fil.G2Print('Read MedOptics D1 tiff file: ' + filename) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif IFD[273][2][0] == 4096: if sizexy[0] == 3072: pixy = [73., 73.] tifType = 'MAR225' else: pixy = [158., 158.] tifType = 'MAR325' File.seek(4096) G2fil.G2Print('Read MAR CCD tiff file: ' + filename) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif IFD[273][2][0] == 512: tifType = '11-ID-C' pixy = [200., 200.] File.seek(512) G2fil.G2Print('Read 11-ID-C tiff file: ' + filename) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif sizexy == [4096, 4096]: if IFD[273][2][0] == 8: if IFD[258][2][0] == 16: tifType = 'scanCCD' pixy = [9., 9.] File.seek(8) G2fil.G2Print('Read APS scanCCD tiff file: ' + filename) image = np.array(ar.array('H', File.read(2 * Npix)), dtype=np.int32) elif IFD[258][2][0] == 32: tifType = 'PE4k' pixy = [100., 100.] File.seek(8) G2fil.G2Print('Read PE 4Kx4K tiff file: ' + filename) image = np.array( np.frombuffer(File.read(4 * Npix), dtype=np.float32) / 2.**4, dtype=np.int32) elif IFD[273][2][0] == 4096: tifType = 'Rayonix' pixy = [73.242, 73.242] File.seek(4096) G2fil.G2Print('Read Rayonix MX300HE tiff file: ' + filename) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.uint16), dtype=np.int32) elif sizexy == [391, 380]: pixy = [109.92, 109.92] File.seek(8) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.int16), dtype=np.int32) elif sizexy == [380, 391]: File.seek(110) pixy = [109.92, 109.92] image = np.array(np.frombuffer(File.read(Npix), dtype=np.uint8), dtype=np.int32) elif sizexy == [825, 830]: pixy = [109.92, 109.92] File.seek(8) image = np.array(np.frombuffer(File.read(Npix), dtype=np.uint8), dtype=np.int32) elif sizexy == [1800, 1800]: pixy = [109.92, 109.92] File.seek(110) image = np.array(np.frombuffer(File.read(Npix), dtype=np.uint8), dtype=np.int32) elif sizexy == [2880, 2880]: pixy = [150., 150.] File.seek(8) dt = np.dtype(np.float32) dt = dt.newbyteorder(byteOrd) image = np.array(np.frombuffer(File.read(Npix * 4), dtype=dt), dtype=np.int32) # elif sizexy == [960,960]: # tiftype = 'PE-BE' # pixy = (200,200) # File.seek(8) # if not imageOnly: # print 'Read Gold tiff file:',filename # image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) if image is None: lines = [ 'not a known detector tiff file', ] return lines, 0, 0, 0 if sizexy[1] * sizexy[0] != image.size: # test is resize is allowed lines = [ 'not a known detector tiff file', ] return lines, 0, 0, 0 if GSASIIpath.GetConfigValue('debug'): G2fil.G2Print('image read time: %.3f' % (time.time() - time0)) image = np.reshape(image, (sizexy[1], sizexy[0])) center = (not center[0]) and [ pixy[0] * sizexy[0] / 2000, pixy[1] * sizexy[1] / 2000 ] or center wavelength = (not wavelength) and 0.10 or wavelength distance = (not distance) and 100.0 or distance polarization = (not polarization) and 0.99 or polarization samplechangerpos = (not samplechangerpos) and 0.0 or samplechangerpos data = { 'pixelSize': pixy, 'wavelength': wavelength, 'distance': distance, 'center': center, 'size': sizexy, 'setdist': distance, 'PolaVal': [polarization, False], 'samplechangerpos': samplechangerpos } File.close() return head, data, Npix, image
LogInfo = {'Logging':False, 'Tree':None, 'LastPaintAction':None} 'Contains values that are needed in the module for past actions & object location' # TODO: # Might want to save the last displayed screen with some objects to make sure # the commands are executed in a sensible order # 1) save tree press under tab press item. # 2) save tree & tab for button press # TODO: # probably need an object for saving calls and arguments to call specific functions/methods. # The items to be called need to register themselves so that they can be found # This needs to be done with a Register(function,'unique-string') call after every def # and then a LogCall('unique-string',pargs,kwargs) call inside the routine debug = GSASIIpath.GetConfigValue('logging_debug') #=========================================================================== # objects for logging variables def _l2s(lst,separator='+'): 'Combine a list of objects into a string, with a separator' s = '' for i in lst: if s != '': s += separator s += '"'+str(i)+'"' return s class LogEntry(object): '''Base class to define logging objects. These store information on events in a manner that can be pickled and saved -- direct references to wx objects is not allowed.
def Reader(self, filename, ParentFrame=None, **unused): 'Read a FullProf file' x = [] y = [] w = [] gotCcomment = False begin = True steps = False Stop = False N = 0 fp = open(filename, 'r') for i, S in enumerate(fp): self.errors = 'Error reading line: ' + str(i + 1) # Allow a block of comments delimited by /* and */ # or (GSAS style) each comment line can begin with '#' or '!' if S.lstrip()[0] in [ "'", '#', '!', ]: self.comments.append(S[:-1]) continue # store comments, if any if begin: if gotCcomment and S.find('*/') > -1: self.comments.append(S[:-1]) begin = False continue if S.strip().startswith('/*'): self.comments.append(S[:-1]) gotCcomment = True continue # look for a line with start, steps etc. in 1st 4 lines if not steps: vals = S.replace(',', ' ').split(None, 4) if 'lambda' in S: self.instdict['wave'] = float(vals[1]) continue elif len(vals) >= 3: try: start = float(vals[0]) step = float(vals[1]) stop = float(vals[2]) steps = True begin = False if len(vals) > 3: self.comments.append(vals[3][:-1]) except: print('Skipping line ', S) continue elif i < 3: print('Skipping header line ', S) continue # should be a valid line to read vals = S.split() #data strings try: for j in range(len(vals)): x.append(start + N * step) f = float(vals[j]) if f <= 0.0: y.append(0.0) w.append(0.0) else: y.append(float(vals[j])) w.append(1.0 / float(vals[j])) if x[-1] >= stop: Stop = True break N += 1 if Stop: break except ValueError: msg = 'Error parsing number in line ' + str(i + 1) if GSASIIpath.GetConfigValue('debug'): print(msg) print(S.strip()) break except: msg = 'Error in line ' + str(i + 1) if GSASIIpath.GetConfigValue('debug'): print(msg) print(S.strip()) break fp.close() N = len(x) if N <= 1: return False self.powderdata = [ np.array(x), # x-axis values np.array(y), # powder pattern intensities np.array(w), # 1/sig(intensity)^2 values (weights) np.zeros(N), # calc. intensities (zero) np.zeros(N), # calc. background (zero) np.zeros(N), # obs-calc profiles ] self.powderentry[0] = filename #self.powderentry[1] = pos # bank offset (N/A here) #self.powderentry[2] = 1 # xye file only has one bank self.idstring = ospath.basename(filename) # scan comments for temperature Temperature = 300 for S in self.comments: if 'Temp' in S.split('=')[0]: try: Temperature = float(S.split('=')[1]) except: pass self.Sample['Temperature'] = Temperature return True
def LoadPhase(self, fil): '''Load Phase entries from a .GPX file to the tree. see :func:`GSASIIIO.ProjFileOpen` ''' G2frame = self filep = open(fil, 'rb') shortname = os.path.splitext(os.path.split(fil)[1])[0] wx.BeginBusyCursor() Phases = None try: while True: try: data = cPickleLoad(filep) except EOFError: break if not data[0][0].startswith('Phase'): continue Phases = data #if self.PWDRfilter is not None: # implement filter # if self.PWDRfilter not in data[0][0]: continue data[0][0] += ' (' if Phases: data[0][0] += shortname data[0][0] += ')' else: data[0][0] += shortname data[0][0] += 'has no phases)' Phases = data break except Exception as errmsg: if GSASIIpath.GetConfigValue('debug'): print('\nError reading GPX file:', errmsg) import traceback print(traceback.format_exc()) msg = wx.MessageDialog(G2frame, message="Error reading file " + str(fil) + ". This is not a current GSAS-II .gpx file", caption="Load Error", style=wx.ICON_ERROR | wx.OK | wx.STAY_ON_TOP) msg.ShowModal() finally: filep.close() wx.EndBusyCursor() datum = None if Phases: datum = data[0] #datum[0] = G2obj.MakeUniqueLabel(datum[0],self.histList) Id = G2frame.GPXtree.AppendItem(parent=G2frame.root, text=datum[0]) G2frame.GPXtree.SetItemPyData(Id, datum[1]) for datus in data[1:]: #datus[0] += ' (' #datus[0] += shortname #datus[0] += ')' sub = G2frame.GPXtree.AppendItem(Id, datus[0]) G2frame.GPXtree.SetItemPyData(sub, datus[1]) if datum: # was anything loaded? self.GPXtree.Expand(Id) print('project load successful for {}'.format(datum[0])) # G2frame.Status.SetStatusText('Mouse RB drag/drop to reorder',0) # G2frame.SetTitleByGPX() self.GPXtree.Expand(self.root)
def Reader(self, filename, ParentFrame=None, **kwarg): '''Read powder data from a CIF. If multiple datasets are requested, use self.repeat and buffer caching. ''' # Define lists of data names used for holding powder diffraction data # entries of a type that are not implemented are commented out in case # we will want them later. xDataItems = ( # "x-axis" data names" ('_pd_meas_2theta_range_min', '_pd_meas_2theta_range_max', '_pd_meas_2theta_range_inc'), ('_pd_proc_2theta_range_min', '_pd_proc_2theta_range_max', '_pd_proc_2theta_range_inc'), '_pd_meas_2theta_scan', '_pd_meas_time_of_flight', '_pd_proc_2theta_corrected', #'_pd_proc_d_spacing', #'_pd_proc_energy_incident', #'_pd_proc_energy_detection', '_pd_proc_recip_len_q', #'_pd_proc_wavelength', ) intDataItems = ( # intensity axis data names '_pd_meas_counts_total', #'_pd_meas_counts_background', #'_pd_meas_counts_container', '_pd_meas_intensity_total', #'_pd_meas_intensity_background', #'_pd_meas_intensity_container', '_pd_proc_intensity_net', '_pd_proc_intensity_total', #'_pd_proc_intensity_bkg_calc', #'_pd_proc_intensity_bkg_fix', '_pd_calc_intensity_net', # allow computed patterns as input data? '_pd_calc_intensity_total', ) ESDDataItems = ( # items that can be used to compute uncertainties for obs data '_pd_proc_ls_weight', '_pd_meas_counts_total') ModDataItems = ( # items that modify the use of the data '_pd_meas_step_count_time', '_pd_meas_counts_monitor', '_pd_meas_intensity_monitor', '_pd_proc_intensity_norm', '_pd_proc_intensity_incident', ) rdbuffer = kwarg.get('buffer') cf = None choicelist = None selections = None # reload previously saved values if self.repeat and rdbuffer is not None: cf = rdbuffer.get('lastcif') choicelist = rdbuffer.get('choicelist') print('debug: Reuse previously parsed CIF') selections = rdbuffer.get('selections') if cf is None: if GSASIIpath.GetConfigValue('debug'): print("Starting parse of {} as CIF".format(filename)) cf = G2obj.ReadCIF(filename) if GSASIIpath.GetConfigValue('debug'): print("CIF file parsed") # scan all blocks for sets of data if choicelist is None: choicelist = [] for blk in cf.keys(): blkkeys = [ k.lower() for k in cf[blk].keys() ] # save a list of the data items, since we will use it often # scan through block for x items xldict = {} for x in xDataItems: if type( x ) is tuple: # check for the presence of all three items that define a range of data if not all([i in blkkeys for i in x]): continue try: items = [float(cf[blk][xi]) for xi in x] l = 1 + int(0.5 + (items[1] - items[0]) / items[2]) except: continue else: if x not in blkkeys: continue l = len(cf[blk][x]) if xldict.get(l) is None: xldict[l] = [x] else: xldict[l].append(x) # now look for matching intensity items yldict = {} suldict = {} for y in intDataItems: if y in blkkeys: l = len(cf[blk][y]) if yldict.get(l) is None: yldict[l] = [y] else: yldict[l].append(y) # now check if the first item has an uncertainty if cif.get_number_with_esd(cf[blk][y][0])[1] is None: continue if suldict.get(l) is None: suldict[l] = [y] else: suldict[l].append(y) for y in ESDDataItems: if y in blkkeys: l = len(cf[blk][y]) if suldict.get(l) is None: suldict[l] = [y] else: suldict[l].append(y) modldict = {} for y in ModDataItems: if y in blkkeys: l = len(cf[blk][y]) if modldict.get(l) is None: modldict[l] = [y] else: modldict[l].append(y) for l in xldict: if yldict.get(l) is None: continue choicelist.append([ blk, l, xldict[l], yldict[l], suldict.get(l, []), modldict.get(l, []) ]) #print blk,l,xldict[l],yldict[l],suldict.get(l,[]),modldict.get(l,[]) print("CIF file scanned for blocks with data") if not choicelist: selblk = None # no block to choose self.errors = "No powder diffraction blocks found" return False elif len(choicelist) == 1: # only one choice selblk = 0 elif self.repeat and selections is not None: # we were called to repeat the read print('debug: repeat #', self.repeatcount, 'selection', selections[self.repeatcount]) selblk = selections[self.repeatcount] self.repeatcount += 1 if self.repeatcount >= len(selections): self.repeat = False else: # choose from options # compile a list of choices for the user choices = [] for blk, l, x, y, su, mod in choicelist: sx = x[0] if len(x) > 1: sx += '...' sy = y[0] if len(y) > 1: sy += '...' choices.append('Block ' + str(blk) + ', ' + str(l) + ' points. X=' + sx + ' & Y=' + sy) selections = G2IO.MultipleBlockSelector( choices, ParentFrame=ParentFrame, title='Select dataset(s) to read from the list below', size=(600, 100), header='Dataset Selector') if len(selections) == 0: self.errors = "Abort: block not selected" return False selblk = selections[0] # select first in list if len(selections) > 1: # prepare to loop through again self.repeat = True self.repeatcount = 1 if rdbuffer is not None: rdbuffer['selections'] = selections rdbuffer[ 'lastcif'] = cf # save the parsed cif for the next loop rdbuffer[ 'choicelist'] = choicelist # save the parsed choices for the future # got a selection, now read it # do we need to ask which fields to read? blk, l, xch, ych, such, modch = choicelist[selblk] xi, yi, sui, modi = 0, 0, 0, 0 if len(xch) > 1 or len(ych) > 1 or len(such) > 1 or len(modch) > 0: choices = [] chlbls = [] chlbls.append('Select the scanned data item') xchlst = [] for x in xch: if type(x) is tuple: xchlst.append(x[0]) else: xchlst.append(x) choices.append(xchlst) chlbls.append('Select the intensity data item') choices.append(ych) chlbls.append('Select the data item to be used for weighting') choices.append(such) chlbls.append('Divide intensities by data item') choices.append(['none'] + modch) res = G2IO.MultipleChoicesSelector(choices, chlbls) if not res: self.errors = "Abort: data items not selected" return False xi, yi, sui, modi = res # now read in the values # x-values self.powderentry[0] = filename #self.powderentry[1] = pos # bank offset (N/A here) #self.powderentry[2] = 1 # xye file only has one bank self.idstring = os.path.basename(filename) + ': ' + blk if cf[blk].get('_diffrn_radiation_probe'): if cf[blk]['_diffrn_radiation_probe'] == 'neutron': self.instdict['type'] = 'PNC' #if cf[blk].get('_pd_meas_time_of_flight'): self.instdict['type'] = 'PNT' # not supported yet else: self.instdict['type'] = 'PXC' if cf[blk].get('_diffrn_radiation_wavelength'): val = cf[blk]['_diffrn_radiation_wavelength'] wl = [] if type(val) is list: for v in val: w, e = cif.get_number_with_esd(v) if w: wl.append(w) else: w, e = cif.get_number_with_esd(val) if w: wl.append(w) if wl: if len(wl) > 1: self.instdict['wave'] = wl else: self.instdict['wave'] = wl[0] if cf[blk].get('_diffrn_ambient_temperature'): val = cf[blk]['_diffrn_ambient_temperature'] w, e = cif.get_number_with_esd(val) if w: self.Sample['Temperature'] = w xcf = xch[xi] if type(xcf) is tuple: vals = [float(cf[blk][ixi]) for ixi in xcf] x = np.array([ (i * vals[2] + vals[0]) for i in range(1 + int(0.5 + (vals[1] - vals[0]) / vals[2])) ]) else: vl = [] for val in cf[blk].get(xcf, '?'): v, e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(np.NaN) else: vl.append(v) x = np.array(vl) if 'recip_len_q' in xcf and 'wave' in self.instdict: wl = self.instdict['wave'] x = 2. * asind(wl * x / (4. * np.pi)) # y-values ycf = ych[yi] vl = [] v2 = [] for val in cf[blk].get(ycf, '?'): v, e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(np.NaN) v2.append(np.NaN) else: vl.append(v) if e is None: v2.append(np.sqrt(v)) else: v2.append(max(e, 1.0)) y = np.array(vl) w = 1. / np.array(v2)**2 # weights if sui == -1: # no weights vl = np.zeros(len(x)) + 1. else: vl = [] sucf = such[sui] if sucf == '_pd_proc_ls_weight': for val in cf[blk].get(sucf, '?'): v, e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(0.) else: vl.append(v) elif sucf == '_pd_proc_intensity_total': for val in cf[blk].get(sucf, '?'): v, e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(0.) elif v <= 0: vl.append(1.) else: vl.append(1. / v) elif sucf == '_pd_meas_counts_total': for val in cf[blk].get(sucf, '?'): v, e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(0.) elif v <= 0: vl.append(1.) else: vl.append(1. / v) else: for val in cf[blk].get(sucf, '?'): v, e = cif.get_number_with_esd(val) if v is None or e is None: # not parsed or no ESD vl.append(np.NaN) elif e <= 0: vl.append(1.) else: vl.append(1. / (e * e)) # w = np.array(vl) # intensity modification factor if modi >= 1: ycf = modch[modi - 1] vl = [] for val in cf[blk].get(ycf, '?'): v, e = cif.get_number_with_esd(val) if v is None: # not parsed vl.append(np.NaN) else: vl.append(v) y /= np.array(vl) w /= np.array(vl) N = len(x) print("CIF file, read from selected block") self.errors = "Error while storing read values" self.powderdata = [ np.array(x), # x-axis values np.array(y), # powder pattern intensities np.array(w), # 1/sig(intensity)^2 values (weights) np.zeros(N), # calc. intensities (zero) np.zeros(N), # calc. background (zero) np.zeros(N), # obs-calc profiles ] return True
def SeqRefine(GPXfile,dlg,refPlotUpdate=None): '''Perform a sequential refinement -- cycles through all selected histgrams, one at a time ''' import GSASIImpsubs as G2mp G2mp.InitMP() import pytexture as ptx ptx.pyqlmninit() #initialize fortran arrays for spherical harmonics printFile = open(ospath.splitext(GPXfile)[0]+'.lst','w') G2fil.G2Print ('Starting Sequential Refinement') G2stIO.ShowBanner(printFile) Controls = G2stIO.GetControls(GPXfile) G2stIO.ShowControls(Controls,printFile,SeqRef=True) restraintDict = G2stIO.GetRestraints(GPXfile) Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile) if not Phases: G2fil.G2Print (' *** ERROR - you have no phases to refine! ***') G2fil.G2Print (' *** Refine aborted ***') return False,'No phases' if not Histograms: G2fil.G2Print (' *** ERROR - you have no data to refine with! ***') G2fil.G2Print (' *** Refine aborted ***') return False,'No data' rigidbodyDict = G2stIO.GetRigidBodies(GPXfile) rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]}) rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,pFile=printFile) G2mv.InitVars() (Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables, maxSSwave) = G2stIO.GetPhaseData(Phases,restraintDict,rbIds, Print=False,pFile=printFile,seqRef=True) for item in phaseVary: if '::A0' in item: G2fil.G2Print ('**** WARNING - lattice parameters should not be refined in a sequential refinement ****') G2fil.G2Print ('**** instead use the Dij parameters for each powder histogram ****') return False,'Lattice parameter refinement error - see console message' if '::C(' in item: G2fil.G2Print ('**** WARNING - phase texture parameters should not be refined in a sequential refinement ****') G2fil.G2Print ('**** instead use the C(L,N) parameters for each powder histogram ****') return False,'Phase texture refinement error - see console message' if 'Seq Data' in Controls: histNames = Controls['Seq Data'] else: # patch from before Controls['Seq Data'] was implemented? histNames = G2stIO.GetHistogramNames(GPXfile,['PWDR',]) if Controls.get('Reverse Seq'): histNames.reverse() SeqResult = G2stIO.GetSeqResult(GPXfile) # SeqResult = {'SeqPseudoVars':{},'SeqParFitEqList':[]} Histo = {} NewparmDict = {} G2stIO.SetupSeqSavePhases(GPXfile) for ihst,histogram in enumerate(histNames): if GSASIIpath.GetConfigValue('Show_timing'): t1 = time.time() G2fil.G2Print('\nRefining with '+str(histogram)) G2mv.InitVars() (Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup, FFtables,BLtables,MFtables,maxSSwave) = G2stIO.GetPhaseData( Phases,restraintDict,rbIds, Print=False,pFile=printFile,seqRef=True) ifPrint = False if dlg: dlg.SetTitle('Residual for histogram '+str(ihst)) calcControls = {} calcControls['atomIndx'] = atomIndx calcControls['Natoms'] = Natoms calcControls['FFtables'] = FFtables calcControls['BLtables'] = BLtables calcControls['MFtables'] = MFtables calcControls['maxSSwave'] = maxSSwave if histogram not in Histograms: G2fil.G2Print("Error: not found!") continue #TODO - implement "Fix FXU" for seq refinement here - done? hId = Histograms[histogram]['hId'] redphaseVary = phaseCheck(phaseVary,Phases,histogram) Histo = {histogram:Histograms[histogram],} hapVary,hapDict,controlDict = G2stIO.GetHistogramPhaseData(Phases,Histo,Print=False) calcControls.update(controlDict) histVary,histDict,controlDict = G2stIO.GetHistogramData(Histo,False) calcControls.update(controlDict) varyList = rbVary+redphaseVary+hapVary+histVary # if not ihst: # save the initial vary list, but without histogram numbers on parameters saveVaryList = varyList[:] for i,item in enumerate(saveVaryList): items = item.split(':') if items[1]: items[1] = '' item = ':'.join(items) saveVaryList[i] = item if not ihst: SeqResult['varyList'] = saveVaryList else: SeqResult['varyList'] = list(set(SeqResult['varyList']+saveVaryList)) parmDict = {} parmDict.update(rbDict) parmDict.update(phaseDict) parmDict.update(hapDict) parmDict.update(histDict) if Controls['Copy2Next']: # update with parms from last histogram #parmDict.update(NewparmDict) # don't use in case extra entries would cause a problem for parm in NewparmDict: if parm in parmDict: parmDict[parm] = NewparmDict[parm] elif histogram in SeqResult: # update phase from last seq ref NewparmDict = SeqResult[histogram].get('parmDict',{}) for parm in NewparmDict: if '::' in parm and parm in parmDict: parmDict[parm] = NewparmDict[parm] G2stIO.GetFprime(calcControls,Histo) # do constraint processing #reload(G2mv) # debug constrDict,fixedList = G2stIO.GetConstraints(GPXfile) varyListStart = tuple(varyList) # save the original varyList before dependent vars are removed msg = G2mv.EvaluateMultipliers(constrDict,parmDict) if msg: return False,'Unable to interpret multiplier(s): '+msg try: groups,parmlist = G2mv.GenerateConstraints(varyList,constrDict,fixedList,parmDict,SeqHist=hId) # if GSASIIpath.GetConfigValue('debug'): print("DBG_"+ # G2mv.VarRemapShow(varyList,True)) # print('DependentVars',G2mv.GetDependentVars()) # print('IndependentVars',G2mv.GetIndependentVars()) constraintInfo = (groups,parmlist,constrDict,fixedList,ihst) except G2mv.ConstraintException: G2fil.G2Print (' *** ERROR - your constraints are internally inconsistent ***') #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList) #print 'Errors',errmsg #if warnmsg: print 'Warnings',warnmsg return False,' Constraint error' #print G2mv.VarRemapShow(varyList) if not ihst: # first histogram to refine against firstVaryList = [] for item in varyList: items = item.split(':') if items[1]: items[1] = '' item = ':'.join(items) firstVaryList.append(item) newVaryList = firstVaryList else: newVaryList = [] for item in varyList: items = item.split(':') if items[1]: items[1] = '' item = ':'.join(items) newVaryList.append(item) if newVaryList != firstVaryList and Controls['Copy2Next']: # variable lists are expected to match between sequential refinements when Copy2Next is on #print '**** ERROR - variable list for this histogram does not match previous' #print ' Copy of variables is not possible' #print '\ncurrent histogram',histogram,'has',len(newVaryList),'variables' combined = list(set(firstVaryList+newVaryList)) c = [var for var in combined if var not in newVaryList] p = [var for var in combined if var not in firstVaryList] G2fil.G2Print('*** Variables change ***') for typ,vars in [('Removed',c),('Added',p)]: line = ' '+typ+': ' if vars: for var in vars: if len(line) > 70: G2fil.G2Print(line) line = ' ' line += var + ', ' else: line += 'none, ' G2fil.G2Print(line[:-2]) firstVaryList = newVaryList ifSeq = True printFile.write('\n Refinement results for histogram: %s\n'%histogram) printFile.write(135*'-'+'\n') try: IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histo,Phases,restraintDict, rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg, refPlotUpdate=refPlotUpdate) G2fil.G2Print (' wR = %7.2f%%, chi**2 = %12.6g, reduced chi**2 = %6.2f, last delta chi = %.4f'%( Rvals['Rwp'],Rvals['chisq'],Rvals['GOF']**2,Rvals['DelChi2'])) # add the uncertainties into the esd dictionary (sigDict) if not IfOK: G2fil.G2Print('***** Sequential refinement failed at histogram '+histogram,mode='warn') break sigDict = dict(zip(varyList,sig)) # the uncertainties for dependent constrained parms into the esd dict sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict)) # a dict with values & esds for dependent (constrained) parameters - avoid extraneous holds depParmDict = {i:(parmDict[i],sigDict[i]) for i in varyListStart if i in sigDict and i not in varyList} newCellDict = copy.deepcopy(G2stMth.GetNewCellParms(parmDict,varyList)) newAtomDict = copy.deepcopy(G2stMth.ApplyXYZshifts(parmDict,varyList)) histRefData = { 'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals, 'varyListStart':varyListStart, 'covMatrix':covMatrix,'title':histogram,'newAtomDict':newAtomDict, 'newCellDict':newCellDict,'depParmDict':depParmDict, 'constraintInfo':constraintInfo, 'parmDict':parmDict} SeqResult[histogram] = histRefData G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True) # G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile) G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histo,None,ifPrint,printFile) G2stIO.SetHistogramData(parmDict,sigDict,Histo,None,ifPrint,printFile) G2stIO.SaveUpdatedHistogramsAndPhases(GPXfile,Histo,Phases,rigidbodyDict,histRefData) NewparmDict = {} # make dict of varied parameters in current histogram, renamed to # next histogram, for use in next refinement. if Controls['Copy2Next'] and ihst < len(histNames)-1: hId = Histo[histogram]['hId'] # current histogram nexthId = Histograms[histNames[ihst+1]]['hId'] for parm in set(list(varyList)+list(varyListStart)): items = parm.split(':') if len(items) < 3: continue if str(hId) in items[1]: items[1] = str(nexthId) newparm = ':'.join(items) NewparmDict[newparm] = parmDict[parm] else: if items[2].startswith('dA'): parm = parm.replace(':dA',':A') NewparmDict[parm] = parmDict[parm] except G2obj.G2RefineCancel as Msg: printFile.close() G2fil.G2Print (' ***** Refinement stopped *****') return False,Msg.msg except G2obj.G2Exception as Msg: # cell metric error, others? printFile.close() G2fil.G2Print (' ***** Refinement error *****') return False,Msg.msg if GSASIIpath.GetConfigValue('Show_timing'): t2 = time.time() G2fil.G2Print("Fit step time {:.2f} sec.".format(t2-t1)) t1 = t2 SeqResult['histNames'] = [itm for itm in G2stIO.GetHistogramNames(GPXfile,['PWDR',]) if itm in SeqResult.keys()] G2stIO.SetSeqResult(GPXfile,Histograms,SeqResult) printFile.close() G2fil.G2Print (' Sequential refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst') G2fil.G2Print (' ***** Sequential refinement successful *****') return True,'Success'
def LoadPwdr(self, fil): '''Load PWDR entries from a .GPX file to the tree. see :func:`GSASIIIO.ProjFileOpen` ''' G2frame = self filep = open(fil, 'rb') shortname = os.path.splitext(os.path.split(fil)[1])[0] wx.BeginBusyCursor() histLoadList = [] try: while True: try: data = cPickleLoad(filep) except EOFError: break if not data[0][0].startswith('PWDR'): continue if self.PWDRfilter is not None: # implement filter if self.PWDRfilter not in data[0][0]: continue data[0][0] += ' (' data[0][0] += shortname data[0][0] += ')' histLoadList.append(data) except Exception as errmsg: if GSASIIpath.GetConfigValue('debug'): print('\nError reading GPX file:', errmsg) import traceback print(traceback.format_exc()) msg = wx.MessageDialog(G2frame, message="Error reading file " + str(fil) + ". This is not a current GSAS-II .gpx file", caption="Load Error", style=wx.ICON_ERROR | wx.OK | wx.STAY_ON_TOP) msg.ShowModal() finally: filep.close() wx.EndBusyCursor() datum = None for i, data in enumerate(histLoadList): datum = data[0] datum[0] = G2obj.MakeUniqueLabel(datum[0], self.histList) Id = G2frame.GPXtree.AppendItem(parent=G2frame.root, text=datum[0]) self.histList.append(datum[0]) # if 'ranId' not in datum[1][0]: # patch: add random Id if not present # datum[1][0]['ranId'] = ran.randint(0,sys.maxsize) G2frame.GPXtree.SetItemPyData( Id, datum[1][:3]) #temp. trim off junk (patch?) for datus in data[1:]: sub = G2frame.GPXtree.AppendItem(Id, datus[0]) #patch if datus[0] == 'Instrument Parameters' and len(datus[1]) == 1: if datum[0].startswith('PWDR'): datus[1] = [ dict( zip(datus[1][3], zip(datus[1][0], datus[1][1], datus[1][2]))), {} ] else: datus[1] = [ dict( zip(datus[1][2], zip(datus[1][0], datus[1][1]))), {} ] for item in datus[1][ 0]: #zip makes tuples - now make lists! datus[1][0][item] = list(datus[1][0][item]) #end patch G2frame.GPXtree.SetItemPyData(sub, datus[1]) if datum: # was anything loaded? print('project load successful for {}'.format(datum[0])) # G2frame.Status.SetStatusText('Mouse RB drag/drop to reorder',0) # G2frame.SetTitleByGPX() self.GPXtree.Expand(self.root)
def GetSFRMData(self, filename): 'Read cbf compressed binarydetector data sfrm file' if GSASIIpath.GetConfigValue('debug'): print('Read cbf compressed binary detector data sfrm file: ' + filename) File = open(filename, 'rb') sizexy = [0, 0] pixSize = [135.3, 135.3] #Pixium4700? cent = [0, 0] wave = 1.54187 #default <CuKa> dist = 250. stream = File.read() if 'bytes' in str(type(stream)): stream = stream.decode('latin-1') starter = 'IMG: ' meanwaves = { 'Cu': 1.54051, 'Ti': 2.74841, 'Cr': 2.28962, 'Fe': 1.93597, 'Co': 1.78892, 'Mo': 0.70926, 'Ag': 0.559363 } imageBeg = stream.find(starter) + 4 head = np.array(list(stream[:imageBeg].split('CFR:')[0])) head = head.reshape(-1, 80) lines = [] for line in head: line = ''.join(line) lines.append(line) fields = line.split(':')[1].split() if 'TARGET' in line: wave = meanwaves[fields[0]] target = fields[0].capitalize() elif 'DISTANC' in line: dist = float(fields[1]) * 10. elif 'ANGLES' in line: twoth = float(fields[0]) elif 'CENTER' in line: cent = [float(fields[0]), float(fields[1])] elif 'NROWS' in line: sizexy[1] = int(fields[0]) elif 'NCOLS' in line: sizexy[0] = int(fields[0]) elif 'FORMAT' in line: frmt = int(fields[0]) elif 'HDRBLKS' in line: imageBeg = 512 * int(fields[0]) elif 'NOVERFL' in line: Nunder = int(fields[0]) N2byte = 2 * int(fields[1]) if N2byte % 16: N2byte = (N2byte // 16 + 1) * 16 N4byte = 4 * int(fields[2]) if N4byte % 16: N4byte = (N4byte // 16 + 1) * 16 if frmt == 86: lines = [ 'FORMAT 86 Bruker files currently not readible by GSAS-II', ] return lines, 0, 0, 0 nxy = sizexy[0] * sizexy[1] cent = [cent[0] * pixSize[0] / 1000., cent[1] * pixSize[1] / 1000.] cent[0] += dist * np.tan(np.pi * twoth / 180.) File.seek(imageBeg) img = File.read(nxy) img2byte = File.read(N2byte) img4byte = File.read(N4byte) time0 = time.time() img = np.array(np.frombuffer(img, dtype='u1'), dtype=np.int32) img2byte = np.array(np.frombuffer(img2byte, dtype='u2'), dtype=np.int32) img4byte = np.array(np.frombuffer(img4byte, dtype='u4'), dtype=np.int32) ins2byte = np.argwhere(img == 255) for j, i in enumerate(list(ins2byte)): img[i] = img2byte[j] ins4byte = np.argwhere(img == 65535) for j, i in enumerate(list(ins4byte)): img[i] = img4byte[j] image = np.reshape(img, (sizexy[1], sizexy[0])) print('import time: %.3f' % (time.time() - time0)) data = { 'pixelSize': pixSize, 'wavelength': wave, 'distance': dist, 'center': cent, 'size': sizexy, 'target': target, 'tilt': -twoth, 'rotation': 90., 'twoth': str(round(twoth, 1)) } data['pixLimit'] = 5 data['calibdmin'] = 1.0 data['cutoff'] = .5 Npix = sizexy[0] * sizexy[1] return lines, data, Npix, image
def GetGEsumData(self, filename, imagenum=1, sum=False): '''Read G.E. detector images from various files as produced at 1-ID and with Detector Pool detector. Also sums multiple image files if desired ''' import struct as st import platform if '2' in platform.python_version_tuple()[0]: import cPickle else: import pickle as cPickle import time more = False time0 = time.time() File = open(filename, 'rb') if filename.split('.')[-1] in ['sum', 'cor32']: head = [ 'GE detector sum/corrected data from APS 1-ID', ] sizexy = [2048, 2048] Npix = sizexy[0] * sizexy[1] image = np.array(np.frombuffer(File.read(4 * Npix), dtype=np.float32), dtype=np.int32) elif filename.split('.')[-1] in ['avg', 'cor']: File.seek(0, 2) last = File.tell() pos = last - 2 * (2048**2) File.seek(pos) head = [ 'GE detector avg or cor data from APS 1-ID', ] sizexy = [2048, 2048] Npix = sizexy[0] * sizexy[1] image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.int16), dtype=np.int32) else: head = [ 'GE detector raw data', ] File.seek(18) size, nframes = st.unpack('<ih', File.read(6)) # number of frames seems to be 3 for single-image files if size != 2048: print('Warning GE image size unexpected: ' + str(size)) print('Assumed 2048x2048') size = 2048 statinfo = os.stat(str(File).split("'")[1]) fsize = statinfo.st_size nframes = (fsize - 8192) / (2 * 2048**2) # return 0,0,0,0,False # probably should quit now if imagenum > nframes: print('Error: attempt to read image #' + str(imagenum) + ' from file with ' + str(nframes) + ' images.') return 0, 0, 0, 0, False elif imagenum < nframes: more = True sizexy = [2048, 2048] Npix = sizexy[0] * sizexy[1] pos = 8192 + (imagenum - 1) * 2 * Npix File.seek(pos) image = np.array(np.frombuffer(File.read(2 * Npix), dtype=np.int16), dtype=np.int32) if len(image) != sizexy[1] * sizexy[0]: print('not enough images while reading GE file: ' + filename + 'image #' + str(imagenum)) return 0, 0, 0, 0, False head += [ 'file: ' + filename + ' image #' + str(imagenum), ] if sum: #will ignore imagenum print('Frames to read %d,' % (nframes), end='') while nframes > 1: #OK, this will sum the frames. try: image += np.array(np.frombuffer(File.read(2 * Npix), dtype=np.int16), dtype=np.int32) except ValueError: break nframes -= 1 print('%d,' % (nframes), end='') print('') more = False filename = os.path.splitext(filename)[0] + '.G2img' File = open(filename, 'wb') Data = { 'pixelSize': [200., 200.], 'wavelength': 0.15, 'distance': 250.0, 'center': [204.8, 204.8], 'size': sizexy } image = np.reshape(image, (sizexy[1], sizexy[0])) cPickle.dump([head, Data, Npix, image], File, 1) File.close() self.sumfile = filename self.formatName = 'GSAS-II image' sum = False image = np.reshape(image, (sizexy[1], sizexy[0])) data = { 'pixelSize': [200., 200.], 'wavelength': 0.15, 'distance': 250.0, 'center': [204.8, 204.8], 'size': sizexy } File.close() if GSASIIpath.GetConfigValue('debug'): print('Image read time %.2fs' % (time.time() - time0)) print('Read GE file: ' + filename + ' image #' + '%04d' % (imagenum)) return head, data, Npix, image, more
def GetMAR345Data(filename, imageOnly=False): 'Read a MAR-345 image plate image' try: import pack_f as pf except: print( '**** ERROR - Unable to load the GSAS MAR image decompression, pack_f' ) return None, None, None, None if not imageOnly: print('Read Mar345 file: ' + filename) File = open(filename, 'rb') head = File.read(4095).decode(encoding='latin-1') lines = head[128:].split('\n') head = [] for line in lines: line = line.strip() if 'PIXEL' in line: values = line.split() pixel = (int(values[2]), int(values[4])) #in microns elif 'WAVELENGTH' in line: wave = float(line.split()[1]) elif 'DISTANCE' in line: distance = float(line.split()[1]) #in mm if not distance: distance = 500. elif 'CENTER' in line: values = line.split() center = [float(values[2]) / 10., float(values[4]) / 10.] #make in mm from pixels if line: head.append(line) data = { 'pixelSize': pixel, 'wavelength': wave, 'distance': distance, 'center': center } for line in head: if 'FORMAT' in line[0:6]: items = line.split() sizex = int(items[1]) Npix = int(items[3]) sizey = int(Npix / sizex) pos = 4096 data['size'] = [sizex, sizey] File.seek(pos) line = File.read(8).decode(encoding='latin-1') while 'CCP4' not in line: #get past overflow list for now line = File.read(8).decode(encoding='latin-1') pos += 8 pos += 37 File.seek(pos) image = np.zeros(shape=(sizex, sizey), dtype=np.int32) time0 = time.time() if '2' in platform.python_version_tuple()[0]: raw = File.read() image = np.flipud( pf.pack_f(len(raw), raw, sizex, sizey, image).T) #transpose to get it right way around & flip else: raw = np.frombuffer(File.read(), dtype=np.uint8) image = np.flipud( pf.pack_f3(len(raw), raw, sizex, sizey, image).T) #transpose to get it right way around & flip if GSASIIpath.GetConfigValue('debug'): print('image read time: %.3f' % (time.time() - time0)) File.close() if imageOnly: return image else: return head, data, Npix, image
def readColMetadata(imagefile): '''Reads image metadata from a column-oriented metadata table (1-ID style .par file). Called by :func:`GetColumnMetadata` The .par file has any number of columns separated by spaces. The directory for the file must be specified in Config variable :data:`config_example.Column_Metadata_directory`. As an index to the .par file a second "label file" must be specified with the same file root name as the .par file but the extension must be .XXX_lbls (where .XXX is the extension of the image) or if that is not present extension .lbls. :param str imagefile: the full name of the image file (with extension, directory optional) :returns: a dict with parameter values. Named parameters will have the type based on the specified Python function, named columns will be character strings The contents of the label file will look like this:: # define keywords filename:lambda x,y: "{}_{:0>6}".format(x,y)|33,34 distance: float | 75 wavelength:lambda keV: 12.398425/float(keV)|9 pixelSize:lambda x: [74.8, 74.8]|0 ISOlikeDate: lambda dow,m,d,t,y:"{}-{}-{}T{} ({})".format(y,m,d,t,dow)|0,1,2,3,4 Temperature: float|53 FreePrm2: int | 34 | Free Parm2 Label # define other variables 0:day 1:month 2:date 3:time 4:year 7:I_ring This file contains three types of lines in any order. * Named parameters are evaluated with user-supplied Python code (see subsequent information). Specific named parameters are used to determine values that are used for image interpretation (see table, below). Any others are copied to the Comments subsection of the Image tree item. * Column labels are defined with a column number (integer) followed by a colon (:) and a label to be assigned to that column. All labeled columns are copied to the Image's Comments subsection. * Comments are any line that does not contain a colon. Note that columns are numbered starting at zero. Any named parameter may be defined provided it is not a valid integer, but the named parameters in the table have special meanings, as descibed. The parameter name is followed by a colon. After the colon, specify Python code that defines or specifies a function that will be called to generate a value for that parameter. Note that several keywords, if defined in the Comments, will be found and placed in the appropriate section of the powder histogram(s)'s Sample Parameters after an integration: ``Temperature``, ``Pressure``, ``Time``, ``FreePrm1``, ``FreePrm2``, ``FreePrm3``, ``Omega``, ``Chi``, and ``Phi``. After the Python code, supply a vertical bar (|) and then a list of one more more columns that will be supplied as arguments to that function. Note that the labels for the three FreePrm items can be changed by including that label as a third item with an additional vertical bar. Labels will be ignored for any other named parameters. The examples above are discussed here: ``filename:lambda x,y: "{}_{:0>6}".format(x,y)|33,34`` Here the function to be used is defined with a lambda statement:: lambda x,y: "{}_{:0>6}".format(x,y) This function will use the format function to create a file name from the contents of columns 33 and 34. The first parameter (x, col. 33) is inserted directly into the file name, followed by a underscore (_), followed by the second parameter (y, col. 34), which will be left-padded with zeros to six characters (format directive ``:0>6``). When there will be more than one image generated per line in the .par file, an alternate way to generate list of file names takes into account the number of images generated:: lambda x,y,z: ["{}_{:0>6}".format(x,int(y)+i) for i in range(int(z))] Here a third parameter is used to specify the number of images generated, where the image number is incremented for each image. ``distance: float | 75`` Here the contents of column 75 will be converted to a floating point number by calling float on it. Note that the spaces here are ignored. ``wavelength:lambda keV: 12.398425/float(keV)|9`` Here we define an algebraic expression to convert an energy in keV to a wavelength and pass the contents of column 9 as that input energy ``pixelSize:lambda x: [74.8, 74.8]|0`` In this case the pixel size is a constant (a list of two numbers). The first column is passed as an argument as at least one argument is required, but that value is not used in the expression. ``ISOlikeDate: lambda dow,m,d,t,y:"{}-{}-{}T{} ({})".format(y,m,d,t,dow)|0,1,2,3,4`` This example defines a parameter that takes items in the first five columns and formats them in a different way. This parameter is not one of the pre-defined parameter names below. Some external code could be used to change the month string (argument ``m``) to a integer from 1 to 12. ``FreePrm2: int | 34 | Free Parm2 Label`` In this example, the contents of column 34 will be converted to an integer and placed as the second free-named parameter in the Sample Parameters after an integration. The label for this parameter will be changed to "Free Parm2 Label". **Pre-defined parameter names** ============= ========= ======== ===================================================== keyword required type Description ============= ========= ======== ===================================================== filename yes str or generates the file name prefix for the matching image list file (MyImage001 for file /tmp/MyImage001.tif) or a list of file names. polarization no float generates the polarization expected based on the monochromator angle, defaults to 0.99. center no list of generates the approximate beam center on the detector 2 floats in mm, such as [204.8, 204.8]. distance yes float generates the distance from the sample to the detector in mm pixelSize no list of generates the size of the pixels in microns such as 2 floats [200.0, 200.0]. wavelength yes float generates the wavelength in Angstroms ============= ========= ======== ===================================================== ''' dir,fil = os.path.split(os.path.abspath(imagefile)) imageName,ext = os.path.splitext(fil) if not GSASIIpath.GetConfigValue('Column_Metadata_directory'): return parfiles = glob.glob(os.path.join(GSASIIpath.GetConfigValue('Column_Metadata_directory'),'*.par')) if len(parfiles) == 0: G2Print('Sorry, No Column metadata (.par) file found in '+ GSASIIpath.GetConfigValue('Column_Metadata_directory')) return {} for parFil in parfiles: # loop over all .par files (hope just 1) in image dir until image is found parRoot = os.path.splitext(parFil)[0] for e in (ext+'_lbls','.lbls'): if os.path.exists(parRoot+e): lblFil = parRoot+e break else: G2Print('Warning: No labels definitions found for '+parFil) continue labels,lbldict,keyCols,keyExp,errors = readColMetadataLabels(lblFil) if errors: print('Errors in labels file '+lblFil) for i in errors: print(' '+i) continue else: G2Print('Read '+lblFil) # scan through each line in this .par file, looking for the matching image rootname fp = open(parFil,'r') for iline,line in enumerate(fp): items = line.strip().split(' ') nameList = keyExp['filename'](*[items[j] for j in keyCols['filename']]) if type(nameList) is str: if nameList != imageName: continue name = nameList else: for name in nameList: if name == imageName: break # got a match else: continue # parse the line and finish metadata = evalColMetadataDicts(items,labels,lbldict,keyCols,keyExp) metadata['par file'] = parFil metadata['lbls file'] = lblFil G2Print("Metadata read from {} line {}".format(parFil,iline+1)) fp.close() return metadata else: G2Print("Image {} not found in {}".format(imageName,parFil)) fp.close() continue fp.close() else: G2Print("Warning: No .par metadata for image {}".format(imageName)) return {}
def GetCbfData(self, filename): 'Read cif binarydetector data cbf file' if GSASIIpath.GetConfigValue('debug'): print('Read cif binary detector data cbf file: ' + filename) File = open(filename, 'rb') sizexy = [0, 0] pixSize = [172, 172] #Pixium4700? cent = [0, 0] wave = 1.54187 #default <CuKa> dist = 1000. byteOrd = '<' stream = File.read() if 'bytes' in str(type(stream)): stream = stream.decode('latin-1') starter = '\x0c\x1a\x04\xd5' imageBeg = stream.find(starter) + 4 head = stream[:imageBeg] term = '\r\n' #CR-LF if term in head: pass else: term = '\n' #LF only head = head.split(term) for line in head: fields = line.split() if 'Wavelength' in line: wave = float(fields[2]) elif 'Detector_distance' in line: dist = float(fields[2]) * 1000. elif 'Pixel_size' in line: pixSize = [float(fields[2]) * 1.e6, float(fields[5]) * 1.e6] elif 'Beam_xy' in line: cent = [ float(fields[2].strip('(').strip(',')), float(fields[3].strip(')')) ] elif 'X-Binary-Size:' in line: compImageSize = int(fields[1]) elif 'BIG_ENDIAN' in line: byteOrd = '>' elif 'Fastest-Dimension' in line: sizexy[0] = int(fields[1]) elif 'Second-Dimension' in line: sizexy[1] = int(fields[1]) elif 'Number-of-Elements' in line: Npix = int(fields[1]) nxy = sizexy[0] * sizexy[1] cent = [cent[0] * pixSize[0] / 1000., cent[1] * pixSize[1] / 1000.] File.seek(0) img = File.read()[imageBeg:imageBeg + compImageSize] nimg = len(img) image = np.zeros(nxy, dtype=np.int32) time0 = time.time() if 'bytes' in str(type(img)): img = np.frombuffer(img, dtype=np.uint8) image = cbf.unpack_cbf3(nimg, img, nxy, image) else: image = cbf.unpack_cbf(nimg, img, nxy, image) image = np.reshape(image, (sizexy[1], sizexy[0])) print('import time: %.3f' % (time.time() - time0)) data = { 'pixelSize': pixSize, 'wavelength': wave, 'distance': dist, 'center': cent, 'size': sizexy } Npix = sizexy[0] * sizexy[1] return head, data, Npix, image
def Refine(GPXfile,dlg=None,makeBack=True,refPlotUpdate=None): 'Global refinement -- refines to minimize against all histograms' import GSASIImpsubs as G2mp G2mp.InitMP() import pytexture as ptx ptx.pyqlmninit() #initialize fortran arrays for spherical harmonics printFile = open(ospath.splitext(GPXfile)[0]+'.lst','w') G2stIO.ShowBanner(printFile) varyList = [] parmDict = {} G2mv.InitVars() Controls = G2stIO.GetControls(GPXfile) G2stIO.ShowControls(Controls,printFile) calcControls = {} calcControls.update(Controls) constrDict,fixedList = G2stIO.GetConstraints(GPXfile) restraintDict = G2stIO.GetRestraints(GPXfile) Histograms,Phases = G2stIO.GetUsedHistogramsAndPhases(GPXfile) if not Phases: G2fil.G2Print (' *** ERROR - you have no phases to refine! ***') G2fil.G2Print (' *** Refine aborted ***') return False,'No phases' if not Histograms: G2fil.G2Print (' *** ERROR - you have no data to refine with! ***') G2fil.G2Print (' *** Refine aborted ***') return False,'No data' rigidbodyDict = G2stIO.GetRigidBodies(GPXfile) rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]}) rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,pFile=printFile) (Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables, maxSSwave) = G2stIO.GetPhaseData(Phases,restraintDict,rbIds,pFile=printFile) calcControls['atomIndx'] = atomIndx calcControls['Natoms'] = Natoms calcControls['FFtables'] = FFtables calcControls['BLtables'] = BLtables calcControls['MFtables'] = MFtables calcControls['maxSSwave'] = maxSSwave hapVary,hapDict,controlDict = G2stIO.GetHistogramPhaseData(Phases,Histograms,pFile=printFile) TwConstr,TwFixed = G2stIO.makeTwinFrConstr(Phases,Histograms,hapVary) constrDict += TwConstr fixedList += TwFixed calcControls.update(controlDict) histVary,histDict,controlDict = G2stIO.GetHistogramData(Histograms,pFile=printFile) calcControls.update(controlDict) varyList = rbVary+phaseVary+hapVary+histVary parmDict.update(rbDict) parmDict.update(phaseDict) parmDict.update(hapDict) parmDict.update(histDict) G2stIO.GetFprime(calcControls,Histograms) # do constraint processing varyListStart = tuple(varyList) # save the original varyList before dependent vars are removed msg = G2mv.EvaluateMultipliers(constrDict,parmDict) if msg: return False,'Unable to interpret multiplier(s): '+msg try: G2mv.GenerateConstraints(varyList,constrDict,fixedList,parmDict) #print(G2mv.VarRemapShow(varyList)) #print('DependentVars',G2mv.GetDependentVars()) #print('IndependentVars',G2mv.GetIndependentVars()) except G2mv.ConstraintException: G2fil.G2Print (' *** ERROR - your constraints are internally inconsistent ***') #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList) #print 'Errors',errmsg #if warnmsg: print 'Warnings',warnmsg return False,' Constraint error' # print G2mv.VarRemapShow(varyList) ifSeq = False printFile.write('\n Refinement results:\n') printFile.write(135*'-'+'\n') try: covData = {} IfOK,Rvals,result,covMatrix,sig = RefineCore(Controls,Histograms,Phases,restraintDict, rigidbodyDict,parmDict,varyList,calcControls,pawleyLookup,ifSeq,printFile,dlg, refPlotUpdate=refPlotUpdate) if IfOK: sigDict = dict(zip(varyList,sig)) newCellDict = G2stMth.GetNewCellParms(parmDict,varyList) newAtomDict = G2stMth.ApplyXYZshifts(parmDict,varyList) covData = {'variables':result[0],'varyList':varyList,'sig':sig,'Rvals':Rvals, 'varyListStart':varyListStart, 'covMatrix':covMatrix,'title':GPXfile,'newAtomDict':newAtomDict, 'newCellDict':newCellDict,'freshCOV':True} # add the uncertainties into the esd dictionary (sigDict) sigDict.update(G2mv.ComputeDepESD(covMatrix,varyList,parmDict)) G2mv.PrintIndependentVars(parmDict,varyList,sigDict,pFile=printFile) G2stMth.ApplyRBModels(parmDict,Phases,rigidbodyDict,True) G2stIO.SetRigidBodyModels(parmDict,sigDict,rigidbodyDict,printFile) G2stIO.SetPhaseData(parmDict,sigDict,Phases,rbIds,covData,restraintDict,printFile) G2stIO.SetHistogramPhaseData(parmDict,sigDict,Phases,Histograms,calcControls['FFtables'],pFile=printFile) G2stIO.SetHistogramData(parmDict,sigDict,Histograms,calcControls['FFtables'],pFile=printFile) G2stIO.SetUsedHistogramsAndPhases(GPXfile,Histograms,Phases,rigidbodyDict,covData,makeBack) printFile.close() G2fil.G2Print (' Refinement results are in file: '+ospath.splitext(GPXfile)[0]+'.lst') G2fil.G2Print (' ***** Refinement successful *****') else: G2fil.G2Print ('****ERROR - Refinement failed') raise G2obj.G2Exception('****ERROR - Refinement failed') except G2obj.G2RefineCancel as Msg: printFile.close() G2fil.G2Print (' ***** Refinement stopped *****') return False,Msg.msg except G2obj.G2Exception as Msg: # cell metric error, others? printFile.close() G2fil.G2Print (' ***** Refinement error *****') return False,Msg.msg #for testing purposes, create a file for testderiv if GSASIIpath.GetConfigValue('debug'): # and IfOK: #needs: values,HistoPhases,parmDict,varylist,calcControls,pawleyLookup fl = open(ospath.splitext(GPXfile)[0]+'.testDeriv','wb') cPickle.dump(result[0],fl,1) cPickle.dump([Histograms,Phases,restraintDict,rigidbodyDict],fl,1) cPickle.dump([constrDict,fixedList,G2mv.GetDependentVars()],fl,1) cPickle.dump(parmDict,fl,1) cPickle.dump(varyList,fl,1) cPickle.dump(calcControls,fl,1) cPickle.dump(pawleyLookup,fl,1) fl.close() if dlg: return True,Rvals
def OnTimerLoop(self, event): '''A method that is called every :meth:`PollTime` seconds that is used to check for new files and process them. Integrates new images. Also optionally sets up and computes PDF. This is called only after the "Start" button is pressed (then its label reads "Pause"). ''' if GSASIIpath.GetConfigValue('debug'): import datetime print("DBG_Timer tick at {:%d %b %Y %H:%M:%S}\n".format( datetime.datetime.now())) if self.PreventTimerReEntry: return self.PreventTimerReEntry = True self.ShowMatchingFiles(None) if not self.currImageList: self.PreventTimerReEntry = False return updateList = False # get input for integration imgprms = mskprms = None if not self.params['TableMode']: # read in image controls/masks, used below in loop. In Table mode # we will get this image-by image. gpxinp = G2sc.G2Project(self.gpxin[3]) print('reading template project', gpxinp.filename) img = gpxinp.image(self.imprm[1].GetStringSelection()) imgprms = img.getControls(True) if self.maskfl[1].GetStringSelection().strip(): img = gpxinp.image(self.maskfl[1].GetStringSelection()) mskprms = img.getMasks() # setup shared input for PDF computation (for now will not be table mode) xydata = {} if self.params['ComputePDF']: pdfEntry = self.pdfSel.GetStringSelection() try: PDFobj = gpxinp.pdf(pdfEntry) except KeyError: print("PDF entry not found: {}".format(pdfEntry)) # update with GUI input for i, lbl in enumerate( ('Sample Bkg.', 'Container', 'Container Bkg.')): name = self.pbkg[i][1].GetStringSelection() try: xydata[lbl] = gpxinp.histogram(name).data['data'] except AttributeError: pass PDFobj.data['PDF Controls'][lbl]['Mult'] = self.pbkg[i][6] PDFobj.data['PDF Controls'][lbl]['Name'] = name else: PDFobj = None if self.MPpool: self.MPpool.imap_unordered( ProcessImageMP, self.ArgGen(PDFobj, imgprms, mskprms, xydata)) else: for intArgs in self.ArgGen(PDFobj, imgprms, mskprms, xydata): newImage = intArgs[0] print('processing ', newImage) ProcessImage(*intArgs) updateList = True for newImage in self.currImageList: self.ProcessedList.append(newImage) if updateList: self.ShowMatchingFiles(None) self.PreventTimerReEntry = False self.Raise()
def __init__(self, parent): size = wx.Size(700, 450) wx.Frame.__init__(self, name='dComp', parent=parent, size=size, style=wx.DEFAULT_FRAME_STYLE, title='GSAS-II data/model comparison') # misc vars self.VcovColor = 'RdYlGn' # plot window try: size = GSASIIpath.GetConfigValue('Plot_Size') if type(size) is tuple: pass elif type(size) is str: size = eval(size) else: raise Exception except: size = wx.Size(700, 600) self.plotFrame = wx.Frame(None, -1, 'dComp Plots', size=size, style=wx.DEFAULT_FRAME_STYLE ^ wx.CLOSE_BOX) self.G2plotNB = G2plt.G2PlotNoteBook(self.plotFrame, G2frame=self) #self.plotFrame.Show() self.plotFrame.Show( False) # until we have some graphics, hide the plot window # menus Frame = self.GetTopLevelParent() # same as self self.MenuBar = wx.MenuBar() File = wx.Menu(title='') self.MenuBar.Append(menu=File, title='&File') item = File.Append(wx.ID_ANY, '&Import project...\tCtrl+O', 'Open a GSAS-II project file (*.gpx)') self.Bind(wx.EVT_MENU, self.onLoadGPX, id=item.GetId()) # item = File.Append(wx.ID_ANY,'&Import selected...','Open a GSAS-II project file (*.gpx)') # self.Bind(wx.EVT_MENU, self.onLoadSel, id=item.GetId()) self.Mode = wx.Menu(title='') self.MenuBar.Append(menu=self.Mode, title='&Mode') self.wxID_Mode = {} for m in "Histogram", "Phase", "Project": i = self.wxID_Mode[m] = wx.NewId() item = self.Mode.AppendRadioItem(i, m, 'Display {}s'.format(m)) self.Bind(wx.EVT_MENU, self.onRefresh, id=item.GetId()) item = self.Mode.Append(wx.ID_ANY, 'Set histogram filter', 'Set a filter for histograms to display') self.Bind(wx.EVT_MENU, self.onHistFilter, id=item.GetId()) modeMenu = wx.Menu(title='') self.MenuBar.Append(menu=modeMenu, title='TBD') self.modeMenuPos = self.MenuBar.FindMenu('TBD') #item = modeMenu.Append(wx.ID_ANY,'test') # menu can't be empty Frame.SetMenuBar(self.MenuBar) # status bar self.Status = self.CreateStatusBar() self.Status.SetFieldsCount(2) # split the frame and add the tree self.mainPanel = wx.SplitterWindow(self, wx.ID_ANY, style=wx.SP_LIVE_UPDATE | wx.SP_3D) self.mainPanel.SetMinimumPaneSize(100) self.treePanel = wx.Panel(self.mainPanel, wx.ID_ANY, style=wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER) # self.dataWindow = G2DataWindow(self.mainPanel) self.dataWindow = wx.ScrolledWindow(self.mainPanel) dataSizer = wx.BoxSizer(wx.VERTICAL) self.dataWindow.SetSizer(dataSizer) self.mainPanel.SplitVertically(self.treePanel, self.dataWindow, 200) self.Status.SetStatusWidths([200, -1]) # make these match? treeSizer = wx.BoxSizer(wx.VERTICAL) self.treePanel.SetSizer(treeSizer) self.GPXtree = G2G.G2TreeCtrl(id=wx.ID_ANY, parent=self.treePanel, size=self.treePanel.GetClientSize(), style=wx.TR_DEFAULT_STYLE) TreeId = self.GPXtree.Id treeSizer.Add(self.GPXtree, 1, wx.EXPAND | wx.ALL, 0) #self.GPXtree.Bind(wx.EVT_TREE_SEL_CHANGED,self.OnDataTreeSelChanged) self.GPXtree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnDataTreeSelChanged) # self.GPXtree.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK,self.OnDataTreeSelChanged) # self.GPXtree.Bind(wx.EVT_TREE_ITEM_COLLAPSED, # self.OnGPXtreeItemCollapsed, id=TreeId) #self.GPXtree.Bind(wx.EVT_TREE_ITEM_EXPANDED, # self.OnGPXtreeItemExpanded, id=TreeId) # self.GPXtree.Bind(wx.EVT_TREE_DELETE_ITEM, # self.OnGPXtreeItemDelete, id=TreeId) # self.GPXtree.Bind(wx.EVT_TREE_KEY_DOWN, # self.OnGPXtreeKeyDown, id=TreeId) # self.GPXtree.Bind(wx.EVT_TREE_BEGIN_RDRAG, # self.OnGPXtreeBeginRDrag, id=TreeId) # self.GPXtree.Bind(wx.EVT_TREE_END_DRAG, # self.OnGPXtreeEndDrag, id=TreeId) self.root = self.GPXtree.root self.Bind(wx.EVT_CLOSE, lambda event: sys.exit()) self.fileList = [] # list of files read for use in Reload self.histList = [] # list of histograms loaded for unique naming self.PWDRfilter = None for win, var in ((self.plotFrame, 'Plot_Pos'), ): #for win,var in ((self,'Main_Pos'),(self.plotFrame,'Plot_Pos')): try: pos = GSASIIpath.GetConfigValue(var) if type(pos) is str: pos = eval(pos) win.SetPosition(pos) if GetDisplay(pos) is None: win.Center() except: if GSASIIpath.GetConfigValue(var): print('Value for config {} {} is invalid'.format( var, GSASIIpath.GetConfigValue(var))) win.Center() self.SetModeMenu()