diff --git a/src/diffpy/srxplanar/mask.py b/src/diffpy/srxplanar/mask.py index f7e2b39..32cb31f 100644 --- a/src/diffpy/srxplanar/mask.py +++ b/src/diffpy/srxplanar/mask.py @@ -14,7 +14,6 @@ ############################################################################## import numpy as np -import scipy.sparse as ssp try: import fabio @@ -23,7 +22,7 @@ def openImage(im): rv = fabio.openimage.openimage(im) return rv.data -except: +except ImportError: import tifffile print("Only tiff or .npy mask is support since fabio is not available") @@ -31,7 +30,7 @@ def openImage(im): def openImage(im): try: rv = tifffile.imread(im) - except: + except (ValueError, OSError): rv = 0 return rv @@ -74,12 +73,14 @@ def staticMask(self, maskfile=None): remain unchanged for different images. :param maskfile: string, file name of mask, - mask file supported: .npy, .tif file, ATTN: mask in .npy form should be already flipped, - and 1 (or larger) stands for masked pixels, 0(<0) stands for unmasked pixels + mask file supported: .npy, .tif file, ATTN: mask in .npy form + should be already flipped, + and 1 (or larger) stands for masked pixels, + 0(<0) stands for unmasked pixels :return: 2d array of boolean, 1 stands for masked pixel """ - maskfile = self.maskfile if maskfile == None else maskfile + maskfile = self.maskfile if maskfile is None else maskfile if os.path.exists(maskfile): if maskfile.endswith(".npy"): @@ -117,13 +118,13 @@ def dynamicMask( brightpixelmask = ( self.brightpixelmask - if brightpixelmask == None + if brightpixelmask is None else brightpixelmask ) darkpixelmask = ( - self.darkpixelmask if darkpixelmask == None else darkpixelmask + self.darkpixelmask if darkpixelmask is None else darkpixelmask ) - avgmask = self.avgmask if avgmask == None else avgmask + avgmask = self.avgmask if avgmask is None else avgmask if darkpixelmask or brightpixelmask or avgmask: rv = np.zeros((self.ydimension, self.xdimension)) @@ -145,7 +146,7 @@ def edgeMask(self, cropedges=None): edge (left, right, top, bottom), must larger than 0, if None, use self.corpedges """ - ce = self.cropedges if cropedges == None else cropedges + ce = self.cropedges if cropedges is None else cropedges mask = np.ones((self.ydimension, self.xdimension), dtype=bool) mask[ce[2] : -ce[3], ce[0] : -ce[1]] = 0 return mask @@ -167,10 +168,10 @@ def avgMask(self, image, high=None, low=None, dymask=None, cropedges=None): None, use self.config.corpedges :return 2d bool array, True for masked pixel, edgemake included, dymask not included """ - if dymask == None: + if dymask is None: dymask = self.staticmask - high = self.config.avgmaskhigh if high == None else high - low = self.config.avgmasklow if low == None else low + high = self.config.avgmaskhigh if high is None else high + low = self.config.avgmasklow if low is None else low self.calculate.genIntegrationInds(dymask) chi = self.calculate.intensity(image) @@ -180,7 +181,7 @@ def avgMask(self, image, high=None, low=None, dymask=None, cropedges=None): index[index >= len(chi[1]) - 1] = len(chi[1]) - 1 avgimage = chi[1][index.ravel()].reshape(index.shape) mask = np.ones((self.ydimension, self.xdimension), dtype=bool) - ce = self.cropedges if cropedges == None else cropedges + ce = self.cropedges if cropedges is None else cropedges mask[ce[2] : -ce[3], ce[0] : -ce[1]] = np.logical_or( image[ce[2] : -ce[3], ce[0] : -ce[1]] < avgimage * low, image[ce[2] : -ce[3], ce[0] : -ce[1]] > avgimage * high, @@ -195,7 +196,7 @@ def darkPixelMask(self, pic, r=None): :param r: float, a threshold for masked pixels :return: 2d array of boolean, 1 stands for masked pixel """ - r = self.config.darkpixelr if r == None else r # 0.1 + r = self.config.darkpixelr if r is None else r # 0.1 avgpic = np.average(pic) ks = np.ones((5, 5)) @@ -222,8 +223,8 @@ def brightPixelMask(self, pic, size=None, r=None): :param r: float, a threshold for masked pixels :return: 2d array of boolean, 1 stands for masked pixel """ - size = self.config.brightpixelsize if size == None else size # 5 - r = self.config.brightpixelr if r == None else r # 1.2 + size = self.config.brightpixelsize if size is None else size # 5 + r = self.config.brightpixelr if r is None else r # 1.2 rank = snf.rank_filter(pic, -size, size) ind = snm.binary_dilation(pic > rank * r, np.ones((3, 3))) @@ -266,14 +267,14 @@ def saveMask(self, filename, pic=None, addmask=None): """ if not hasattr(self, "mask"): self.normalMask(addmask) - if (not hasattr(self, "dynamicmask")) and (pic != None): + if (not hasattr(self, "dynamicmask")) and (pic is not None): self.dynamicMask(pic, addmask=addmask) tmask = self.mask if hasattr(self, "dynamicmask"): - if self.dynamicmask != None: + if self.dynamicmask is not None: tmask = ( np.logical_or(self.mask, self.dynamicmask) - if pic != None + if pic is not None else self.mask ) np.save(filename, tmask) diff --git a/src/diffpy/srxplanar/saveresults.py b/src/diffpy/srxplanar/saveresults.py index af22596..dbe99e2 100644 --- a/src/diffpy/srxplanar/saveresults.py +++ b/src/diffpy/srxplanar/saveresults.py @@ -16,7 +16,6 @@ import os import numpy as np -import scipy.io from diffpy.srxplanar.srxplanarconfig import _configPropertyR @@ -48,7 +47,7 @@ def getFilePathWithoutExt(self, filename): :return: string, full normalized path of file without extension """ filebase = os.path.splitext(os.path.split(filename)[1])[0] - if self.filenameplus != "" and self.filenameplus != None: + if self.filenameplus != "" and self.filenameplus is not None: filenamep = "_".join( [filebase, self.filenameplus, self.integrationspace] ) @@ -99,7 +98,7 @@ def saveGSAS(self, xrd, filename): f.write("#### start data\n") if xrd.shape[0] == 3: s = writeGSASStr( - os.path.splitext(path)[0], + os.path.splitext(filepath)[0], self.gsasoutput, xrd[0], xrd[1], @@ -107,7 +106,7 @@ def saveGSAS(self, xrd, filename): ) elif xrd.shape[0] == 2: s = writeGSASStr( - os.path.splitext(path)[0], self.gsasoutput, xrd[0], xrd[1] + os.path.splitext(filepath)[0], self.gsasoutput, xrd[0], xrd[1] ) f.write(s) f.close() @@ -124,7 +123,7 @@ def writeGSASStr(name, mode, tth, iobs, esd=None): :return: string, a string to be saved to file """ maxintensity = 999999 - logscale = numpy.floor(numpy.log10(maxintensity / numpy.max(iobs))) + logscale = np.floor(np.log10(maxintensity / np.max(iobs))) logscale = min(logscale, 0) scale = 10 ** int(logscale) lines = [] @@ -139,10 +138,10 @@ def writeGSASStr(name, mode, tth, iobs, esd=None): # two-theta0 and dtwo-theta in centidegrees tth0_cdg = tth[0] * 100 dtth_cdg = (tth[-1] - tth[0]) / (len(tth) - 1) * 100 - if esd == None: + if esd is None: mode = "std" if mode == "std": - nrec = int(numpy.ceil(nchan / 10.0)) + nrec = int(np.ceil(nchan / 10.0)) lbank = "BANK %5i %8i %8i CONST %9.5f %9.5f %9.5f %9.5f STD" % ( ibank, nchan, @@ -157,7 +156,7 @@ def writeGSASStr(name, mode, tth, iobs, esd=None): for i in range(0, len(lrecs), 10): lines.append("".join(lrecs[i : i + 10])) if mode == "esd": - nrec = int(numpy.ceil(nchan / 5.0)) + nrec = int(np.ceil(nchan / 5.0)) lbank = "BANK %5i %8i %8i CONST %9.5f %9.5f %9.5f %9.5f ESD" % ( ibank, nchan, diff --git a/src/diffpy/srxplanar/selfcalibrate.py b/src/diffpy/srxplanar/selfcalibrate.py index ceef774..f631bf0 100644 --- a/src/diffpy/srxplanar/selfcalibrate.py +++ b/src/diffpy/srxplanar/selfcalibrate.py @@ -1,38 +1,23 @@ -import os from functools import partial import numpy as np -import scipy as sp from matplotlib import rcParams -from scipy.optimize import ( - brent, - fmin_bfgs, - fmin_cg, - fmin_l_bfgs_b, - fmin_powell, - fmin_slsqp, - fmin_tnc, - golden, - leastsq, - minimize, - minimize_scalar, -) +from scipy.optimize import leastsq, minimize rcParams["backend"] = "Qt4Agg" try: - import PySide + import PySide # noqa: F401 rcParams["backend.qt4"] = "PySide" import matplotlib.pyplot as plt mplenabled = True -except: +except ImportError: try: import matplotlib.pyplot as plt - import PyQt4 mplenabled = True - except: + except ImportError: mplenabled = False @@ -233,13 +218,13 @@ def selfCalibrateX( qind = [None, None] qind[0] = ( int(qrange[0] / qstep) - if qrange[0] != None + if qrange[0] is not None else srx.config.xdimension / 20 ) qind[0] = 0 if qind[0] < 0 else qind[0] qind[1] = ( int(qrange[1] / qstep) - if qrange[1] != None + if qrange[1] is not None else srx.config.xdimension / 2 ) qind[1] = ( @@ -316,7 +301,6 @@ def selfCalibrateX( print(p) if mode == "x": srx.updateConfig(xbeamcenter=p[0], **bak) - prv = p[0] elif mode == "y": srx.updateConfig(ybeamcenter=p[0], **bak) elif mode == "tilt": @@ -395,7 +379,7 @@ def selfCalibrate( elif cropedges == "x" or (cropedges == "auto" and mode == "x"): ce = [xd / 100, xd / 100, int(yc - 50), int(yd - yc - 50)] elif cropedges == "box" or ( - cropedges == "auto" and (not mode in ["x", "y"]) + cropedges == "auto" and (mode not in ["x", "y"]) ): ce = [ int(xc - xd / 6), diff --git a/src/diffpy/srxplanar/srxplanar.py b/src/diffpy/srxplanar/srxplanar.py index edebb36..8dffa68 100644 --- a/src/diffpy/srxplanar/srxplanar.py +++ b/src/diffpy/srxplanar/srxplanar.py @@ -18,7 +18,6 @@ import sys import numpy as np -import scipy.sparse as ssp from diffpy.srxplanar.calculate import Calculate from diffpy.srxplanar.loadimage import LoadImage @@ -48,7 +47,7 @@ def __init__( :param kwargs: you can use like 'xbeamcenter=1024' or a dict to update the value of xbeamcenter """ - if srxplanarconfig != None: + if srxplanarconfig is not None: self.config = srxplanarconfig self.config.updateConfig(filename=configfile, args=args, **kwargs) else: @@ -100,16 +99,16 @@ def _picChanged(self, extramask=None): """ dynamicmask = self.mask.dynamicMask(self.pic, dymask=self.staticmask) - if dynamicmask != None: + if dynamicmask is not None: mask = np.logical_or(self.staticmask, dynamicmask) - if extramask != None: + if extramask is not None: mask = np.logical_or(mask, extramask) - elif extramask != None: + elif extramask is not None: mask = np.logical_or(self.staticmask, extramask) else: mask = self.staticmask - if (dynamicmask != None) or (extramask != None): + if (dynamicmask is not None) or (extramask is not None): self.calculate.genIntegrationInds(mask) return @@ -123,11 +122,11 @@ def _getSaveFileName(self, imagename=None, filename=None): :return: string, name of file to be saved """ rv = "output" - if self.config.output != None and self.config.output != "": + if self.config.output is not None and self.config.output != "": rv = self.config.output - elif filename != None: + elif filename is not None: rv = filename - elif imagename != None and isinstance(imagename, (str, unicode)): + elif imagename is not None and isinstance(imagename, str): rv = imagename return rv @@ -136,14 +135,17 @@ def _getPic(self, image, flip=None, correction=None): :param image: could be a string, a list of string or a 2d array, if string, load the image file using the string as the path. - if list of string, load the image files using the string as their path - and sum them togethor + if list of strings, load the image files using the string + as their path and sum them together if 2d array, use that array directly :param flip: flip the image/2d array, - if None: flip on the string/list of string, not flip on the 2d array + if None: flip on the string/list of strings, + not flip on the 2d array Flip behavior is controlled in self.config - :param correction: apply correction to the returned 2d array - if None: correct on the string/list of string, not correct on the 2d array + :param correction: apply correction to + the returned 2d array + if None: correct on the string/list of string, + not correct on the 2d array :return: 2d array of image """ @@ -152,9 +154,9 @@ def _getPic(self, image, flip=None, correction=None): for imagefile in image: rv += self._getPic(imagefile) rv /= len(image) - elif isinstance(image, (str, unicode)): + elif isinstance(image, str): rv = self.loadimage.loadImage(image) - if correction == None or correction == True: + if correction is None or correction is True: ce = self.config.cropedges rv[ce[2] : -ce[3], ce[0] : -ce[1]] = ( rv[ce[2] : -ce[3], ce[0] : -ce[1]] * self.correction @@ -162,9 +164,9 @@ def _getPic(self, image, flip=None, correction=None): # rv *= self.correction else: rv = image - if flip == True: + if flip is True: rv = self.loadimage.flipImage(rv) - if correction == True: + if correction is True: # rv *= self.correction ce = self.config.cropedges rv[ce[2] : -ce[3], ce[0] : -ce[1]] = ( @@ -190,17 +192,21 @@ def integrate( if str, then read image file using it as file name. if 2d array, integrate this 2d array. :param savename: str, name of file to save - :param savefile: boolean, if True, save file to disk, if False, do not save file to disk + :param savefile: boolean, if True, save file to disk, + if False, do not save file to disk :param flip: flip the image/2d array, - if None: flip on the string/list of string, not flip on the 2d array + if None: flip on the string/list of string, + not flip on the 2d array Flip behavior is controlled in self.config :param correction: apply correction to the returned 2d array - if None: correct on the string/list of string, not correct on the 2d array + if None: correct on the string/list of string, + not correct on the 2d array :param extramask: 2d array, extra mask applied in integration - :return: dict, rv['chi'] is a 2d array of integrated intensity, shape is (2, len of intensity) - or (3, len of intensity) in [tth or q, intensity, (uncertainty)]. rv['filename'] is the - name of file to save to disk + :return: dict, rv['chi'] is a 2d array of integrated intensity, + shape is (2, len of intensity) or (3, len of intensity) + in [tth or q, intensity, (uncertainty)]. + rv['filename'] is the name of file to save to disk """ rv = {} self.pic = self._getPic(image, flip, correction) @@ -233,20 +239,23 @@ def integrateFilelist( use self.config.summation :param filename: file name of output file :param flip: flip the image/2d array, - if None: flip on the string/list of string, not flip on the 2d array + if None: flip on the string/list of string, + not flip on the 2d array Flip behavior is controlled in self.config :param correction: apply correction to the returned 2d array - if None: correct on the string/list of string, not correct on the 2d array + if None: correct on the string/list of string, + not correct on the 2d array :param extramask: 2d array, extra mask applied in integration - :return: list of dict, in each dict, rv['chi'] is a 2d array of integrated intensity, shape is (2, len of intensity) - or (3, len of intensity) as [tth or q, intensity, (uncertainty)]. rv['filename'] is the - name of file to save to disk + :return: list of dict, in each dict, rv['chi'] is a + 2d array of integrated intensity, shape is (2, len of intensity) + or (3, len of intensity) as [tth or q, intensity, (uncertainty)]. + rv['filename'] is the name of file to save to disk """ - summation = self.config.summation if summation == None else summation + summation = self.config.summation if summation is None else summation if (summation) and (len(filelist) > 1): image = self._getPic(filelist, flip, correction) - if filename == None: + if filename is None: if isinstance(filelist[-1], str): filename = os.path.splitext(filelist[-1])[0] + "_sum.chi" else: @@ -258,7 +267,7 @@ def integrateFilelist( i = 0 rv = [] for imagefile in filelist: - if filename == None: + if filename is None: rvv = self.integrate( imagefile, flip=flip, @@ -315,17 +324,17 @@ def createMask(self, filename=None, pic=None, addmask=None): Mask module for detail :return: 2d array, 1 stands for masked pixel here """ - filename = self.config.createmask if filename == None else filename + filename = self.config.createmask if filename is None else filename filename = "mask.npy" if filename == "" else filename - addmask = self.config.addmask if addmask == None else addmask + addmask = self.config.addmask if addmask is None else addmask if not hasattr(self, "mask"): self.mask = Mask(self.config) if not hasattr(self, "loadimage"): self.loadimage = LoadImage(self.config) - if pic == None: + if pic is None: filelist = self.loadimage.genFileList() if hasattr(self, "pic"): - if self.pic != None: + if self.pic is not None: pic = self.pic else: pic = (