Changeset 5285


Ignore:
Timestamp:
May 20, 2022 12:35:20 PM (4 months ago)
Author:
toby
Message:

multiple PDFfit2 changes; seems to work well on Windows w/3.7, 3.8 & 3.9

Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIctrlGUI.py

    r5278 r5285  
    64846484                     maximum=101, parent=None, trialMode=False,
    64856485                     seqLen=0, seqShow=3,
    6486                      style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER):
     6486                     style=None):
    64876487
    64886488        self.trialRw = trialMode # used for Levenberg-Marquardt fitting
     
    64946494        self.rows = 4
    64956495        if self.trialRw: self.rows = 5
     6496        if style is None: style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER
    64966497
    64976498        super(self.__class__,self).__init__(parent, wx.ID_ANY, title,
  • trunk/GSASIIpath.py

    r5264 r5285  
    12521252'''.format(script)
    12531253    subprocess.Popen(["osascript","-e",osascript])
    1254    
     1254
     1255#======================================================================
     1256# conda/pip routines
    12551257def findConda():
    12561258    '''Determines if GSAS-II has been installed as g2conda or gsas2full
     
    13241326        if sys.platform != "win32": proc.wait()
    13251327        sys.exit()
     1328       
     1329def condaTest():
     1330    '''Returns True if it appears that Python is being run under Anaconda
     1331    Python with conda present. Tests for conda environment vars and that
     1332    the conda package is installed in the current environment.
     1333
     1334    :returns: True, if running under Conda
     1335    '''   
     1336    if not all([(i in os.environ) for i in ('CONDA_DEFAULT_ENV','CONDA_EXE', 'CONDA_PREFIX', 'CONDA_PYTHON_EXE')]): return False
     1337    # is the conda package available?
     1338    try:
     1339        import conda.cli.python_api
     1340    except:
     1341        print('You do not have the conda package installed in this environment',
     1342                  '\nConsider using the "conda install conda" command')
     1343        return False
     1344
     1345    # There is no foolproof way to check if someone activates conda
     1346    # but then calls a different Python using its path...
     1347    # ...If we are in the base environment then the conda Python
     1348    # should be the same path as the one currently being run:
     1349    if os.environ['CONDA_DEFAULT_ENV'] == 'base':
     1350        try:
     1351            if os.path.samefile(os.environ['CONDA_PYTHON_EXE'],
     1352                                sys.executable): return True
     1353        except:
     1354            return False
     1355
     1356    # ...If not in the base environment, what we can do is check if the
     1357    # python we are running in shares the beginning part of its path with
     1358    # the one in the base installation:
     1359    return commonPath(os.environ['CONDA_PYTHON_EXE'],sys.executable)
     1360
     1361def condaInstall(packageList):
     1362    '''Installs one or more packages using the anaconda conda package
     1363    manager. Can be used to install multiple packages and optionally
     1364    use channels.
     1365
     1366    :param list packageList: a list of strings with name(s) of packages
     1367      and optionally conda options.
     1368      Examples::
     1369
     1370       packageList=['gsl']
     1371       packageList=['-c','conda-forge','wxpython']
     1372       packageList=['numpy','scipy','matplotlib']
     1373
     1374    :returns: None if the the command ran normally, or an error message
     1375      if it did not.
     1376    '''
     1377    import conda.cli.python_api
     1378    try:
     1379        (out, err, rc) = conda.cli.python_api.run_command(
     1380            conda.cli.python_api.Commands.INSTALL,packageList
     1381#    use_exception_handler=True#, stdout=sys.stdout, stderr=sys.stderr)
     1382            )
     1383        #print('rc=',rc)
     1384        print('Ran conda. output follows...')
     1385        print(70*'='+'\n'+out+'\n'+70*'=')
     1386        #print('err=',err)
     1387        if rc != 0: return str(out)
     1388    except Exception as msg:
     1389        print("Error occurred, see below\n",msg) 
     1390        return "error occurred"
     1391    return None
     1392   
     1393def fullsplit(fil,prev=None):
     1394    '''recursive routine to split all levels of directory names
     1395    '''
     1396    if prev is None: # first call: normalize and drop file name
     1397        fil = os.path.normcase(os.path.abspath(os.path.dirname(fil)))
     1398        prev = []
     1399    i,j = os.path.split(fil)
     1400    if j:
     1401        prev.insert(0,j)
     1402        out = fullsplit(i,prev)
     1403    else:
     1404        return [i]+prev
     1405    return out
     1406
     1407def commonPath(file1,file2):
     1408    '''Check if two files share the same path. Note that paths
     1409    are considered the same if either file is in a subdirectory
     1410    of the other, but not if they are in different subdirectories
     1411    /a/b/c/x.x and  /a/b/c/y.y share a path, as does  /a/b/c/d/y.y
     1412    but  /a/b/c/d/x.x and  /a/b/c/e/x.x do not.
     1413
     1414    :returns: True if the paths are common
     1415    '''
     1416
     1417    for i,j in zip(fullsplit(os.environ['CONDA_PYTHON_EXE']),
     1418               fullsplit(sys.executable)):
     1419        if i != j: return False
     1420    return True
     1421
     1422def pipInstall(packageList):
     1423    '''Installs one or more packages using the pip package installer.
     1424    Use of this should be avoided if conda can be used (see :func:`condaTest`
     1425    to test for conda). Can be used to install multiple packages together.
     1426    One can use pip options, but this is probably not needed.
     1427
     1428    :param list packageList: a list of strings with name(s) of packages
     1429      Examples::
     1430
     1431       packageList=['gsl']
     1432       packageList=['wxpython','matplotlib','scipy']
     1433       packageList=[r'\\Mac\Home\Scratch\wheels\pygsl-2.3.3-py3-none-any.whl']
     1434       packageList=['z:/Scratch/wheels/pygsl-2.3.3-py3-none-any.whl']
     1435
     1436    :returns: None if the the command ran normally, or an error message
     1437      if it did not.
     1438    '''
     1439    try:
     1440        subprocess.check_call([sys.executable, '-m', 'pip', 'install']+packageList)
     1441    except Exception as msg:
     1442        return msg
     1443    return None
    13261444   
    13271445if __name__ == '__main__':
  • trunk/GSASIIphsGUI.py

    r5278 r5285  
    103103    Angstr = chr(0x00c5)   
    104104
     105RMCmisc = {}
    105106#### phase class definitions ################################################################################
    106107class SymOpDialog(wx.Dialog):
     
    49414942            def OnSeqReverse(event):
    49424943                RMCPdict['SeqReverse'] = not RMCPdict['SeqReverse']
    4943 
     4944               
     4945            # --- FileSizer starts here
    49444946            Indx = {}
    49454947            mainSizer = wx.BoxSizer(wx.VERTICAL)
     
    62416243        RMCsel.Bind(wx.EVT_RADIOBOX, OnRMCselect)
    62426244        mainSizer.Add(RMCsel,0)
     6245        RMCmisc['RMCnote'] = wx.StaticText(G2frame.FRMC)
     6246        mainSizer.Add(RMCmisc['RMCnote'])
    62436247        G2G.HorizontalLine(mainSizer,G2frame.FRMC)
    62446248        mainSizer.Add(wx.StaticText(G2frame.FRMC,
     
    62596263        bigSizer.Add(G2G.HelpButton(G2frame.FRMC,helpIndex=G2frame.dataWindow.helpKey))
    62606264        SetPhaseWindow(G2frame.FRMC,bigSizer)
    6261         if G2frame.RMCchoice == 'fullrmc' and G2pwd.findfullrmc() is None:
     6265        if G2frame.RMCchoice == 'PDFfit' and not checkPDFfit(G2frame):
     6266            RMCmisc['RMCnote'].SetLabel('PDFfit may not be installed or operational')
     6267        elif G2frame.RMCchoice == 'fullrmc' and G2pwd.findfullrmc() is None:
    62626268            dlg = wx.MessageDialog(G2frame,
    62636269                    'The fullrmc code is not installed or could not be'
     
    63856391        if sys.platform.lower().startswith('win'):
    63866392            batch = open('pdffit2.bat','w')
     6393            # TODO: should probably include an activate command here
    63876394            batch.write(PDFfit_exec+' '+rname+'\n')
    63886395            # batch.write('pause')
     
    63936400            batch = open('pdffit2.sh','w')
    63946401            batch.write('#!/bin/bash\n')
     6402            # TODO: should probably include an activate command here
    63956403            batch.write('cd ' + os.path.split(os.path.abspath(rname))[0] + '\n')
    63966404            batch.write(PDFfit_exec + ' ' + os.path.abspath(rname) + '\n')
     
    1456514573            return
    1456614574    ChangePage(0)
     14575
     14576def checkPDFfit(G2frame):
     14577    '''Checks to see if PDFfit2 is available and can be imported. PDFfit2 can be installed
     14578    in a separate Python interpreter (saved in the pdffit2_exec config variable). If this is
     14579    defined, no attempt is made to check that it actually runs.
     14580    Otherwise, if diffpy.PDFfit has been installed with conda/pip, it is checked if the
     14581    install command. The fallback is to check if a .so/.pyd file has been supplied with
     14582    GSAS-II. This requires that GSL (GNU Scientific Library) be installed. If the current
     14583    Python is being run from conda, this will be loaded.
     14584
     14585    :returns: False if PDFfit2 cannot be run/accessed. True if it appears it can be run.
     14586    '''
     14587    # if a separate Python interpreter has been specified, just use it, no checking
     14588    if GSASIIpath.GetConfigValue('pdffit2_exec') is not None and is_exe(
     14589            GSASIIpath.GetConfigValue('pdffit2_exec')):
     14590        return True
     14591
     14592    # see if diffpy has been installed directly
     14593    try:
     14594        from diffpy.pdffit2 import PdfFit
     14595        return True
     14596    except:
     14597        pass
     14598
     14599    # See if if we can import the GSAS-II supplied PDFfit
     14600    pdffitloc = os.path.join(GSASIIpath.path2GSAS2,'PDFfit2')
     14601    if pdffitloc not in sys.path: sys.path.append(pdffitloc)
     14602    try:
     14603        from diffpy.pdffit2 import PdfFit
     14604        #pf = PdfFit()
     14605        return True
     14606    except Exception as err:
     14607        pass
     14608        #print('Failed to import PDFfit2 with error\n:'+str(err))
     14609   
     14610    if not os.path.exists(pdffitloc):
     14611        print('PDFfit2 not found in GSAS-II \n\t(expected in '+pdffitloc+')')
     14612        return False
     14613    import glob
     14614    if not glob.glob(os.path.join(GSASIIpath.binaryPath,'pdffit*')):
     14615        msg = 'GSAS-II does not supply PDFfit2 for the version of Python that you are using'
     14616        G2G.G2MessageBox(G2frame,msg,'Need PDFfit2')
     14617        return False
     14618
     14619    # see if we can fix things so the GSAS-II version can be used
     14620    if not GSASIIpath.condaTest():
     14621        msg = ('PDFfit2 is not running with this Python installation. '+
     14622        'Since Python was not installed under conda you need to install this yourself. '+
     14623        'pip install diffpy.pdffit2" may do this for you. You will likely need to '+
     14624        'install the GSL (GNU Software Library).')
     14625        G2G.G2MessageBox(G2frame,msg,'No conda')
     14626        return False
     14627    # can we access conda?
     14628    try:
     14629        import conda.cli.python_api
     14630    except:
     14631        msg = ('You do not have the conda package installed in this '+
     14632                'environment so nothing can be installed.',
     14633                '\n\nConsider using the "conda install conda" command')
     14634        G2G.G2MessageBox(G2frame,msg,'conda import error')
     14635        return False
     14636   
     14637    if 'gsl' not in conda.cli.python_api.run_command(conda.cli.python_api.Commands.LIST,'gsl')[0].lower():
     14638        msg = ('The gsl (GNU Software Library), needed by PDFfit2, '+
     14639                   ' is not installed in this Python. Do you want to have this installed?')
     14640        dlg = wx.MessageDialog(G2frame,msg,caption='Install?',
     14641                                   style=wx.YES_NO|wx.ICON_QUESTION)
     14642        if dlg.ShowModal() != wx.ID_YES:
     14643            dlg.Destroy()
     14644            return False
     14645        dlg.Destroy()
     14646        wx.BeginBusyCursor()
     14647        print('Preparing to install gsl. This may take a few minutes...')
     14648        res = GSASIIpath.condaInstall(['gsl'])
     14649        wx.EndBusyCursor()
     14650        if res:
     14651            msg = 'Installation of the GSL package failed with error:\n' + str(res)
     14652            G2G.G2MessageBox(G2frame,msg,'Install GSL Error')
     14653           
     14654    # GSAS-II supplied version for PDFfit now runs?
     14655    if pdffitloc not in sys.path: sys.path.append(pdffitloc)
     14656    try:
     14657        from diffpy.pdffit2 import PdfFit
     14658        #pf = PdfFit()
     14659    except Exception as err:
     14660        msg = 'Failed to import PDFfit2 with error\n:'+str(err)
     14661        G2G.G2MessageBox(G2frame,msg,'PDFfit2 import error')
     14662        return False
     14663    return True
  • trunk/GSASIIpwd.py

    r5281 r5285  
    33243324    rundata = '''#!/usr/bin/env python
    33253325# -*- coding: utf-8 -*-
    3326 import sys
    3327 '''
     3326import sys,os
     3327datadir = r'{:}'
     3328pathWrap = lambda f: os.path.join(datadir,f)
     3329'''.format(os.path.abspath(os.getcwd()))
    33283330    PDFfit_exe,PDFfit_path = findPDFfit()  # returns python loc and path(s) for pdffit
    33293331    if not PDFfit_exe:
     
    33523354                Nd += 1
    33533355                dType = 'Xdata'
    3354             filNam = os.path.abspath(filNam)
    3355             rundata += "pf.read_data(r'%s', '%s', 30.0, %.4f)\n"%(filNam,dType[0],RMCPdict[dType]['qdamp'][0])
     3356            rundata += "pf.read_data(pathWrap(r'%s'), '%s', 30.0, %.4f)\n"%(filNam,dType[0],RMCPdict[dType]['qdamp'][0])
    33563357            rundata += 'pf.setdata(%d)\n'%Nd
    33573358            rundata += 'pf.pdfrange(%d, %6.2f, %6.2f)\n'%(Nd,RMCPdict[dType]['Fitrange'][0],RMCPdict[dType]['Fitrange'][1])
     
    33673368        fName = 'Sequential_PDFfit.stru'
    33683369    Np = 9
    3369     rundata += "pf.read_struct(r'{:}')\n".format(os.path.abspath(fName))
     3370    rundata += "pf.read_struct(pathWrap(r'{:}'))\n".format(fName)
    33703371    for item in ['delta1','delta2','sratio']:
    33713372        if RMCPdict[item][1]:
     
    34373438        fName = 'Sequential_PDFfit'
    34383439        rfile = open('Seq_PDFfit_template.py','w')
    3439         rundata += 'pf.save_pdf(1, "%s")\n'%(fName+'.fgr')
     3440        rundata += 'pf.save_pdf(1, pathWrap("%s"))\n'%(fName+'.fgr')
    34403441    else:
    34413442        fName = General['Name'].replace(' ','_')+'-PDFfit'
     
    34463447                continue
    34473448            Nd += 1
    3448             rundata += 'pf.save_pdf(%d, "%s")\n'%(Nd,fName+file[0]+'.fgr')
     3449            rundata += 'pf.save_pdf(%d, pathWrap("%s"))\n'%(Nd,fName+file[0]+'.fgr')
    34493450       
    3450     rundata += 'pf.save_struct(1, "%s")\n'%(fName+'.rstr')
    3451     rundata += 'pf.save_res("%s")\n'%(fName+'.res')
     3451    rundata += 'pf.save_struct(1, pathWrap("%s"))\n'%(fName+'.rstr')
     3452    rundata += 'pf.save_res(pathWrap("%s"))\n'%(fName+'.res')
    34523453 
    34533454    rfile.writelines(rundata)
  • trunk/docs/source/packages.rst

    r5149 r5285  
    5959  svn (subversion) conda package. This is not actually part of Python
    6060  and can be installed directly into your system's configuration. It is used by
    61   GSAS-II to download updates to our code.
     61  GSAS-II to download updates to our code. This can be skipped if svn
     62  is installed directly (easy Linux, but a bit harder on MacOS and
     63  Windows).
     64* conda: the conda package allows access to conda features from
     65  inside Python. It will be used inceasingly by GSAS-II to
     66  self-install software. The conda package is installed by default in
     67  miniconda and anaconda but if you create an environment for GSAS-II
     68  (`conda create -n <env> package-list...`), it will not be added
     69  unless you request it specifically. 
     70
     71*Conda command*:
     72  Here is a typical conda command used to install a GSAS-II compatible
     73  Python interpreter::
     74
     75    conda install wxpython numpy scipy matplotlib pyopengl pillow h5py imageio svn -c conda-forge
     76   
     77  or to use a separate environment (here named ``g2python``), use::
     78
     79    conda install -n g2python wxpython numpy scipy matplotlib pyopengl pillow h5py imageio svn conda -c conda-forge
     80
     81Remember to activate using: ``<path>\Scripts\activate``  (windows);
     82``source <path>/bin/activate`` (Mac/Linux). Note that one should add
     83``g2python`` (etc.) at the end if using a conda environment.
     84
     85Note that svn seems to be unsupported these days by Anaconda. For
     86Linux and MacOS, use subversion in conda-forge rather than svn. No
     87good solution yet for Windows.
    6288
    6389Scripting  Requirements
     
    130156  **fullrmc**
    131157    A modern software toolkit for large-box PDF & S(Q) fitting. Use
    132     version 5.0 or later.
     158    version 5.0 or later. The implementation for this is not
     159    completed.
    133160
    134161  **Dysnomia**
     
    136163    extension of reflection sphere
    137164
     165  **PDFfit2**
     166  Small-box fitting of PDFs. This code is no longer supported, but is
     167  still quite useful. It can be installed from conda into Python
     168  versions up to Python 3.7, but is supplied for Windows within
     169  GSAS-II for Python 3.7, 3.8 and 3.9 and for MacOS only with Python
     170  3.7.
     171
     172  For other platforms/Python versions, it is probably best to use a
     173  separate Python interpreter.
     174   
    138175Supported Platforms
    139176--------------------------------
Note: See TracChangeset for help on using the changeset viewer.