From 6bb3c682df58971b901429184d674337693fe394 Mon Sep 17 00:00:00 2001 From: Marco Cammarata Date: Fri, 20 Jan 2017 10:55:24 +0100 Subject: [PATCH] updated xray examples and changed path.exists with isfile since this was what I meant --- mcutils.py | 5 ++-- storage.py | 2 +- xray/azav.py | 6 ++--- xray/example_main_salen.py | 49 ++++++++++++++++---------------------- xray/example_main_tiox.py | 10 ++------ xray/id9.py | 2 +- xray/utils.py | 2 +- 7 files changed, 31 insertions(+), 45 deletions(-) diff --git a/mcutils.py b/mcutils.py index 8031598..8a25810 100644 --- a/mcutils.py +++ b/mcutils.py @@ -518,7 +518,7 @@ def downloadPDB(pdbID,outFileName=None): if (outFileName is None): outFileName = pdbID+".pdb" folder = os.path.dirname(outFileName) - if (folder != '' and not os.path.exists(folder)): + if (folder != '' and not os.path.isdir(folder)): os.makedirs(folder) f=open(outFileName,"w") f.write( "".join(lines) ) @@ -1301,9 +1301,8 @@ def fac(n): def define_colors(fname="colorbrewer_all_schemes.json"): - if not os.path.exists(fname): + if not os.path.isfile(fname): fname = os.path.dirname(os.path.realpath(__file__)) + "/" + fname - if not os.path.exists(fname): print("Cannot find colorbrewer_all_schemes.json can't continue") print(fname) return diff --git a/storage.py b/storage.py index 6185a34..1e215c4 100644 --- a/storage.py +++ b/storage.py @@ -167,7 +167,7 @@ class DataStorage(dict): if isinstance(fileOrDict,dict): d = fileOrDict elif isinstance(fileOrDict,str): - if os.path.exists(fileOrDict): + if os.path.isfile(fileOrDict): d = read(fileOrDict) else: self.filename=fileOrDict diff --git a/xray/azav.py b/xray/azav.py index c1c691c..f28d414 100644 --- a/xray/azav.py +++ b/xray/azav.py @@ -107,7 +107,7 @@ def getAI(poni,folder=None): ai = pyFAI.azimuthalIntegrator.AzimuthalIntegrator(**poni) elif isinstance(poni,str): # look is file exists in cwd - if os.path.exists(poni): + if os.path.isfilee(poni): ai = pyFAI.load(poni) # if file does not exist look for one with that name around else: @@ -123,7 +123,7 @@ def getAI(poni,folder=None): # look for file for path in folders: fname = path + "/" + poni - if os.path.exists(fname): + if os.path.isfile(fname): log.info("Found poni file %s",fname) break else: @@ -197,7 +197,7 @@ def doFolder(folder,files='*.edf*',nQ = 1500,force=False,mask=None, """ if storageFile == 'auto': storageFile = folder + "/" + "pyfai_1d.h5" - if os.path.exists(storageFile) and not force: + if os.path.isfile(storageFile) and not force: saved = storage.DataStorage(storageFile) else: saved = None diff --git a/xray/example_main_salen.py b/xray/example_main_salen.py index bbc41df..cd10fad 100644 --- a/xray/example_main_salen.py +++ b/xray/example_main_salen.py @@ -55,18 +55,15 @@ def prepareLog(): prepareLog() def defineCalibrants(dmin=3): - from pyFAI import calibrant - c120 = calibrant.Cell(a=10.297,b=10.716,c=13.955,alpha=72.22,beta=70.45,gamma=74.13) - p120 = np.pi*2/ np.asarray( list(c120.d_spacing(dmin=dmin).keys()) ).astype(float) - p120.sort() - c300 = calibrant.Cell(a=10.485,b=11.015,c=14.280,alpha=74.61,beta=69.29,gamma=75.29) - p300 = np.pi*2/ np.asarray( list(c300.d_spacing(dmin=dmin).keys()) ).astype(float) - p300.sort() - return xray.storage.DataStorage( dict(p120=p120, c120=c120,p300=p300,c300=c300) ) + c120 = xray.cell.Triclinic(a=10.297,b=10.716,c=13.955,alpha=72.22,beta=70.45,gamma=74.13) + p120,_ = c120.reflection_list() + c300 = xray.cell.Triclinic(a=10.485,b=11.015,c=14.280,alpha=74.61,beta=69.29,gamma=75.29) + p300,_ = c300.reflection_list() + return xray.storage.DataStorage( p120=p120, c120=c120,p300=p300,c300=c300) def readCalculations(folder="../fesalen/cif",force=False,plot=False): npz = folder + "/fesalen_calc.npz" - if not os.path.exists(npz) or force: + if not os.path.isfile(npz) or force: ret = dict() for T in ("120LS","200HS","300HS"): # calculated with lambda = 1 Ang @@ -104,41 +101,37 @@ def findCenter(cen=(484,479.5),dist=0.251): def removeBaseline(folder,qlims=(0.5,2.5),max_iter=30,force=False): if isinstance(folder,int): folder = g_default_folder%folder fname = folder + "/pyfai_1d_nobaseline"+id9.default_extension - if not os.path.exists(fname) or force: - data = azav(folder,doRemoveBaseline=False) + if not os.path.isfile(fname) or force: + log.info("Calling azav with default parameters") + data = azav(folder,withBaseline=False) # first peak (around 0.6) is weak and requires special care ... - data.q,data.data = xray.utils.removeBackground(data.q,data.data,xlims=qlims, - max_iter=max_iter,background_regions=[ (0,0.58) ]) + data.q,data.data = xray.utils.removeBackground(data.q,data.data, + xlims=qlims,max_iter=max_iter,background_regions=[ (0,0.58) ]) data.save(fname) else: data = xray.storage.read(fname) return data def azav(folder,nQ=3000,force=False,saveChi=True,mask=g_default_mask, - doRemoveBaseline=True): - if isinstance(folder,int): folder = g_default_folder%folder - if isinstance(mask,int): - files = xray.utils.getFiles(folder,"*.edf*") - img = xray.azav.pyFAIread(files[0]) - temp = np.ones_like(img,dtype=bool) - temp[:mask] = False - mask = temp - azav = id9.doFolder_azav(folder,nQ=nQ,force=force,mask=mask,saveChi=saveChi) - if doRemoveBaseline: removeBaseline(folder) - return azav + withBaseline=True): + if withBaseline: + data=removeBaseline(folder) + else: + data = id9.doFolder_azav(folder,nQ=nQ,force=force,mask=mask, + saveChi=saveChi) + return data def datared(folder,monitor=(0.5,4),showPlot=True,errMask=5,chi2Mask=2, qlims=(0.5,3),withBaseline=False,forceDatared=False, select=slice(None),**kw): if isinstance(folder,int): folder = g_default_folder%folder - def_ext = id9.default_extension + data = azav(folder,withBaseline=withBaseline) extension = def_ext if withBaseline else "_nobaseline" + def_ext # storage files to use diffFile = folder + "/diffs" + extension - azavFile = folder + "/pyfai_1d" + extension - if not os.path.exists(diffFile) or forceDatared: - data,diffs = id9.doFolder_dataRed(azavFile,monitor=monitor, + if not os.path.isfile(diffFile) or forceDatared: + data,diffs = id9.doFolder_dataRed(data,monitor=monitor, errMask=errMask,chi2Mask=chi2Mask,qlims=qlims, outStorageFile=diffFile) else: diff --git a/xray/example_main_tiox.py b/xray/example_main_tiox.py index f7e3cfd..5670542 100644 --- a/xray/example_main_tiox.py +++ b/xray/example_main_tiox.py @@ -17,18 +17,12 @@ def readCalc(): calc = dict() for name,fname in zip(names,fnames): q,i=np.loadtxt(fold + "%s.xye.q"%fname,unpack=True) - calc[name] = xray.storage.DataStorage( dict( q=q, i=i ) ) + calc[name] = dict( q=q, i=i ) return xray.storage.DataStorage(calc) calc = readCalc() -def azav(folder,nQ=1500,force=False,saveChi=True,mask=470): - if isinstance(mask,int): - files = xray.utils.getFiles(folder,"*.edf*") - img = xray.azav.read(files[0]) - temp = np.ones_like(img,dtype=bool) - temp[:mask] = False - mask = temp +def azav(folder,nQ=1500,force=False,saveChi=True,mask='y>470'): return id9.doFolder_azav(folder,nQ=nQ,force=force,mask=mask,saveChi=saveChi) def datared(folder,monitor=(1,5),showPlot=True,**kw): diff --git a/xray/id9.py b/xray/id9.py index 5818f68..683f29e 100644 --- a/xray/id9.py +++ b/xray/id9.py @@ -52,7 +52,7 @@ def doFolder_dataRed(azavStorage,monitor=None,funcForAveraging=np.nanmean, if isinstance(azavStorage,storage.DataStorage): data = azavStorage folder = os.path.dirname(data.filename) if data.filename is not None else "./" - elif os.path.exists(azavStorage): + elif os.path.isfile(azavStorage): folder = os.path.dirname(azavStorage) data = storage.DataStorage(azavStorage) else: diff --git a/xray/utils.py b/xray/utils.py index cb21b1c..46c814e 100644 --- a/xray/utils.py +++ b/xray/utils.py @@ -184,7 +184,7 @@ def saveTxt(fname,q,data,headerv=None,info=None,overwrite=True,columns=''): info = dictionary (saved as '# key : value') or string headerv = vector to be used as header or string """ - if os.path.exists(fname) and not overwrite: + if os.path.isfile(fname) and not overwrite: log.warn("File %s exists, returning",fname) return if isinstance(info,dict):