Changeset 3216


Ignore:
Timestamp:
Jan 7, 2018 9:06:19 PM (6 years ago)
Author:
toby
Message:

add data & parameter access in scripting

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • Binaries/win_32_p2.7_n1.13/BuildNotes.txt

    r3214 r3216  
    2020(files libgcc_s_dw2-1.dll, libgmp-10.dll, libquadmath-0.dll,
    2121libgfortran-3.dll, libgmpxx-4.dll, libwinpthread-1.dll)
     22
     23Note: see https://anaconda.org/msys2/m2w64-ntldd-git for a utility
     24that can find dependencies.
  • Binaries/win_32_p3.6_n1.13/BuildNotes.txt

    r3214 r3216  
    2020(files  libgcc_s_dw2-1.dll, libgmp-10.dll, libquadmath-0.dll,
    2121libgfortran-3.dll, libgmpxx-4.dll  & libwinpthread-1.dll)
     22
     23Note: see https://anaconda.org/msys2/m2w64-ntldd-git for a utility
     24that can find dependencies.
  • trunk/GSASIIIO.py

    r3196 r3216  
    12641264    file without invoking any GUI objects.
    12651265    '''
     1266    # TODO: review exporters producing exceptions where .Writer can't be used where G2frame is None (see CIF)
     1267    # TODO: review conflicting uses of .Writer with mode (SeqRef) & elsewhere
     1268    # TODO: move this class to G2fil
    12661269    def __init__(self,G2frame,formatName,extension,longFormatName=None,):
    12671270        self.G2frame = G2frame
     
    17951798            self.sigDict[i] = sig
    17961799        #GSASIIpath.IPyBreak()
    1797                
     1800
     1801    def SetFromArray(self,hist,histname):
     1802        '''Load a histogram into the exporter in preparation for use of the .Writer
     1803        rather than the main tree. This is used in GSASIIscriptable when wx
     1804        is not present.
     1805        '''
     1806        self.Histograms[histname] =  {}
     1807        self.Histograms[histname]['Data'] = hist['data'][1]
     1808        self.Histograms[histname]['Instrument Parameters'] = hist['Instrument Parameters']
     1809        self.Histograms[histname]['Sample Parameters'] = hist['Sample Parameters']
     1810
    17981811    # Tools to pull information out of the data arrays
    17991812    def GetCell(self,phasenam):
  • trunk/GSASIIdataGUI.py

    r3212 r3216  
    24072407        # find all the exporter files
    24082408        if not self.exporterlist: # this only needs to be done once
    2409             pathlist = sys.path
    2410             filelist = []
    2411             for path in pathlist:
    2412                 for filename in glob.iglob(os.path.join(path,"G2export*.py")):
    2413                     filelist.append(filename)   
    2414             filelist = sorted(list(set(filelist))) # remove duplicates
    2415             # go through the routines and import them, saving objects that
    2416             # have export routines (method Exporter)
    2417             for filename in filelist:
    2418                 path,rootname = os.path.split(filename)
    2419                 pkg = os.path.splitext(rootname)[0]
    2420 #                try:
    2421                 fp = None
    2422                 fp, fppath,desc = imp.find_module(pkg,[path,])
    2423                 pkg = imp.load_module(pkg,fp,fppath,desc)
    2424                 for clss in inspect.getmembers(pkg): # find classes defined in package
    2425                     if clss[0].startswith('_'): continue
    2426                     if inspect.isclass(clss[1]):
    2427                         # check if we have the required methods
    2428                         for m in 'Exporter','loadParmDict':
    2429                             if not hasattr(clss[1],m): break
    2430                             if not callable(getattr(clss[1],m)): break
    2431                         else:
    2432                             exporter = clss[1](self) # create an export instance
    2433                             self.exporterlist.append(exporter)
    2434 #                except AttributeError:
    2435 #                    print 'Import_'+errprefix+': Attribute Error'+str(filename)
    2436 #                    pass
    2437 #                except ImportError:
    2438 #                    print 'Import_'+errprefix+': Error importing file'+str(filename)
    2439 #                    pass
    2440                 if fp: fp.close()
     2409            self.exporterlist = G2fil.LoadExportRoutines(self)
     2410               
    24412411        # Add submenu item(s) for each Exporter by its self-declared type (can be more than one)
    24422412        for obj in self.exporterlist:
  • trunk/GSASIIfiles.py

    r3207 r3216  
    369369    return readerlist
    370370
    371 def LoadExportRoutines():
    372     '''Placeholder that will someday retrieve the exporters
     371def LoadExportRoutines(parent, traceback=False):
     372    '''Routine to locate GSASII exporters
    373373    '''
    374     pass
     374    exporterlist = []
     375    pathlist = sys.path
     376    filelist = []
     377    for path in pathlist:
     378        for filename in glob.iglob(os.path.join(path,"G2export*.py")):
     379                    filelist.append(filename)   
     380    filelist = sorted(list(set(filelist))) # remove duplicates
     381    # go through the routines and import them, saving objects that
     382    # have export routines (method Exporter)
     383    for filename in filelist:
     384        path,rootname = os.path.split(filename)
     385        pkg = os.path.splitext(rootname)[0]
     386        try:
     387            fp = None
     388            fp, fppath,desc = imp.find_module(pkg,[path,])
     389            pkg = imp.load_module(pkg,fp,fppath,desc)
     390            for clss in inspect.getmembers(pkg): # find classes defined in package
     391                if clss[0].startswith('_'): continue
     392                if not inspect.isclass(clss[1]): continue
     393                # check if we have the required methods
     394                if not hasattr(clss[1],'Exporter'): continue
     395                if not callable(getattr(clss[1],'Exporter')): continue
     396                if parent is None:
     397                    if not hasattr(clss[1],'Writer'): continue
     398                else:
     399                    if not hasattr(clss[1],'loadParmDict'): continue
     400                    if not callable(getattr(clss[1],'loadParmDict')): continue
     401                try:
     402                    exporter = clss[1](parent) # create an export instance
     403                except AttributeError:
     404                    pass
     405                except Exception as exc:
     406                    print ('\nExport init: Error substantiating class ' + clss[0])
     407                    print (u'Error message: {}\n'.format(exc))
     408                    if traceback:
     409                        traceback.print_exc(file=sys.stdout)
     410                    continue
     411                exporterlist.append(exporter)
     412        except AttributeError:
     413            print ('Export Attribute Error ' + filename)
     414            if traceback:
     415                traceback.print_exc(file=sys.stdout)
     416        except Exception as exc:
     417            print ('\nExport init: Error importing file ' + filename)
     418            print (u'Error message: {}\n'.format(exc))
     419            if traceback:
     420                traceback.print_exc(file=sys.stdout)
     421        finally:
     422            if fp:
     423                fp.close()
     424    return exporterlist
  • trunk/GSASIIscriptable.py

    r3213 r3216  
    99########### SVN repository information ###################
    1010#
    11 # TODO: Export patterns
    1211# TODO: integrate 2d data based on a model
    13 # TODO: export entire project as a JSON file?
    1412#
    1513"""
     
    463461PwdrDataReaders = []
    464462PhaseReaders = []
     463exportersByExtension = {}
     464'''Specifies the list of extensions that are supported for Powder data export'''
    465465
    466466def LoadG2fil():
    467467    """Delay importing this module, it is slow"""
    468468    global G2fil
    469     global PwdrDataReaders
    470     global PhaseReaders
    471469    if G2fil is None:
    472470        import GSASIIfiles
    473471        G2fil = GSASIIfiles
     472        global PwdrDataReaders
     473        global PhaseReaders
    474474        PwdrDataReaders = G2fil.LoadImportRoutines("pwd", "Powder_Data")
    475475        PhaseReaders = G2fil.LoadImportRoutines("phase", "Phase")
    476 
     476        AllExporters = G2fil.LoadExportRoutines(None)
     477        global exportersByExtension
     478        exportersByExtension = {}
     479        for obj in AllExporters:
     480            try:
     481                obj.Writer
     482            except AttributeError:
     483                continue
     484            for typ in obj.exporttype:
     485                if typ not in exportersByExtension:
     486                    exportersByExtension[typ] = {obj.extension:obj}
     487                else:
     488                    exportersByExtension[typ][obj.extension] = obj
    477489
    478490def LoadDictFromProjFile(ProjFile):
     
    19051917    @property
    19061918    def residuals(self):
     1919        '''Provides a dictionary with with the R-factors for this histogram.
     1920        Includes the weighted and unweighted profile terms (R, Rb, wR, wRb, wRmin)
     1921        as well as the Bragg R-values for each phase (ph:H:Rf and ph:H:Rf^2).
     1922        '''
    19071923        data = self.data['data'][0]
    1908         return {key: data[key]
    1909                 for key in ['R', 'Rb', 'wR', 'wRb', 'wRmin']}
    1910 
     1924        return {key: data[key] for key in data
     1925                if key in ['R', 'Rb', 'wR', 'wRb', 'wRmin']
     1926                   or ':' in key}
     1927
     1928    @property
     1929    def InstrumentParameters(self):
     1930        '''Provides a dictionary with with the Instrument Parameters
     1931        for this histogram.
     1932        '''
     1933        return self.data['Instrument Parameters'][0]
     1934
     1935    @property
     1936    def SampleParameters(self):
     1937        '''Provides a dictionary with with the Sample Parameters
     1938        for this histogram.
     1939        '''
     1940        return self.data['Sample Parameters']
     1941   
    19111942    @property
    19121943    def id(self):
     
    19882019        self.proj.save()
    19892020
     2021    def getdata(self,datatype):
     2022        '''Provides access to the histogram data of the selected data type
     2023
     2024        :param str datatype: must be one of the following values (case is ignored)
     2025       
     2026           * 'X': the 2theta or TOF values for the pattern
     2027           * 'Yobs': the observed intensity values
     2028           * 'Yweight': the weights for each data point (1/sigma**2)
     2029           * 'Ycalc': the computed intensity values
     2030           * 'Background': the computed background values
     2031           * 'Residual': the difference between Yobs and Ycalc (obs-calc)
     2032
     2033        :returns: an numpy MaskedArray with data values of the requested type
     2034       
     2035        '''
     2036        enums = ['x', 'yobs', 'yweight', 'ycalc', 'background', 'residual']
     2037        if datatype.lower() not in enums:
     2038            raise G2ScriptException("Invalid datatype = "+datatype+" must be one of "+enums)
     2039        return self.data['data'][1][enums.index(datatype.lower())]
     2040       
    19902041    def y_calc(self):
    19912042        return self.data['data'][1][3]
    19922043
     2044    def Export(self,fileroot,extension):
     2045        '''Write the histogram into a file. The path is specified by fileroot and
     2046        extension.
     2047       
     2048        :param str fileroot: name of the file, optionally with a path (extension is
     2049           ignored)
     2050        :param str extension: includes '.', must match an extension in global
     2051           exportersByExtension['powder'] or a Exception is raised.
     2052        :returns: name of file that was written
     2053        '''
     2054        if extension not in exportersByExtension.get('powder',[]):
     2055            raise G2ScriptException('No Writer for file type = "'+extension+'"')
     2056        fil = os.path.abspath(os.path.splitext(fileroot)[0]+extension)
     2057        obj = exportersByExtension['powder'][extension]
     2058        obj.SetFromArray(hist=self.data,histname=self.name)
     2059        obj.Writer(self.name,fil)
     2060           
    19932061    def plot(self, Yobs=True, Ycalc=True, Background=True, Residual=True):
    19942062        try:
     
    25222590                    print(u'Unknown HAP key: '+key)
    25232591
     2592    def getHAPvalues(self, histname):
     2593        """Returns a dict with HAP values for the selected histogram
     2594
     2595        :param histogram: is a histogram object (:class:`G2PwdrData`) or
     2596            a histogram name or the index number of the histogram
     2597
     2598        :returns: HAP value dict
     2599        """
     2600        if isinstance(histname, G2PwdrData):
     2601            histname = histname.name
     2602        elif histname in self.data['Histograms']:
     2603            pass
     2604        elif type(histname) is int:
     2605            histname = self.proj.histograms()[histname].name
     2606        else:
     2607            raise G2ScriptException("Invalid histogram reference: "+str(histname))
     2608        return self.data['Histograms'][histname]
     2609                   
    25242610    def clear_HAP_refinements(self, refs, histograms='all'):
    25252611        """Clears the given HAP refinement parameters between this phase and
  • trunk/exports/G2export_CIF.py

    r3200 r3216  
    19621962            #        +'\n'+FormatPhaseProfile(phasenam))
    19631963            if hist.startswith("PWDR"):
    1964                 histblk = self.Histograms[hist]["Sample Parameters"]
    19651964                WritePowderData(hist)
    19661965            elif hist.startswith("HKLF"):
     
    23822381            longFormatName = 'Export data as CIF'
    23832382            )
     2383        if G2frame is None: raise AttributeError('CIF export requires data tree') # prevent use from Scriptable
    23842384        self.exporttype = ['powder']
    23852385        # CIF-specific items
Note: See TracChangeset for help on using the changeset viewer.