def getSaveFileName(self, *args, **kwargs): """ analogue to QtWidgets.QFileDialog.getSaveFileNameAndFilter but returns the filename + chosen file ending even if not typed in gui """ if 'directory' not in kwargs: if self.opts['save']: if self.opts['save']: kwargs['directory'] = self.opts['save'] fname = QtWidgets.QFileDialog.getSaveFileName(**kwargs) if fname: if type(fname) == tuple: # only happened since qt5 # getSaveFileName returns (path, ftype) if not fname[0]: return p = PathStr(fname[0]) if not p.filetype(): ftyp = self._extractFtype(fname[1]) p = p.setFiletype(ftyp) else: p = PathStr(fname) self.opts['save'] = p.dirname() if self.opts['open'] is None: self.opts['open'] = self.opts['save'] return p
def _getLayerLabel(fname, label): if fname is None: if label is None: return 'unknown' return label if PathStr(fname).isfile(): fname = PathStr(fname).basename() if label is not None: return '%s - %s' % (label, fname) return fname
def getOpenFileNames(self, **kwargs): kwargs = self._processOpenKwargs(kwargs) fnames = QtWidgets.QFileDialog.getOpenFileNames(**kwargs) # PyQt4 and 5 comp. workaround if isinstance(fnames, tuple) and isinstance(fnames[0], list): fnames = fnames[0] for n, f in enumerate(fnames): fnames[n] = PathStr(f) if fnames: self.opts['open'] = PathStr(f).dirname() return fnames
def pathJoin(pathlist): if len(pathlist) > 5: mod, number, name, current, date = pathlist[:5] date = date.replace(':', '-') dd0 = '__'.join((number, name)) dd1 = '__'.join((current, date)) out = PathStr(mod).join(dd0, dd1) if len(pathlist[5:]): out = out.join(*tuple(pathlist[5:])) return out return PathStr(pathlist[0]).join(*pathlist[1:])
def pathJoin(pathlist): if len(pathlist) > 5: mod, number, name, current, date = pathlist[:5] # date = date.replace(':', '-') date = datetime.strptime(date, '%x %X').strftime("%Y-%m-%dT%H_%M_%S") dd0 = '__'.join((number, name)) dd1 = '__'.join((current, date)) out = PathStr(mod).join(dd0, dd1) if len(pathlist[5:]): out = out.join(*tuple(pathlist[5:])) return out return PathStr(pathlist[0]).join(*pathlist[1:])
def _pathToData(self, rootpath): '''returns 2d array of all files in [rootpath], splitted by folder''' self._root = rootpath f = list(PathStr(rootpath).nestedFiles(includeroot=False)) f = [fi for fi in f if not fi.isHidden()] data = [self.pathSplit(fi) for fi in f] dates = [dateStr(rootpath.join(fi).date()) for fi in f] sizes = [fs.toStr(rootpath.join(fi).size()) for fi in f] # transform list of lists into array: # lmax = max([len(d) for d in data]) data2 = np.zeros(shape=(len(data), self.columnCount()), dtype="<U%i" % self.STRING_LEN) pathindices = np.array([self.col(ni) for ni in range(self._n)]) fileindex = self.col(self._n) sizeindex = self.col(self._n + 3) dateindex = self.col(self._n + 1) for (dd, d2, date, size) in zip(data, data2, dates, sizes): # if 'DATA' in dd: # print(dd) d2[pathindices[:len(dd) - 1]] = dd[:-1] d2[fileindex] = dd[-1] d2[sizeindex] = size d2[dateindex] = date return data2
def _changeRootPath(self): r = self.gui.root # self.fileTableView.rootPath() new_r = QtWidgets.QFileDialog.getExistingDirectory(directory=r) if new_r: self.gui.setProjectFolder(PathStr(new_r)) self.fileTableView.rootPathChanged(False) # (PathStr(new_r)) self._updateFilePathLabel()
def vignettingFromRandomSteps(imgs, bg, inPlane_scale_factor=None, debugFolder=None, **kwargs): ''' important: first image should shown most iof the device because it is used as reference ''' # TODO: inPlane_scale_factor if debugFolder: debugFolder = PathStr(debugFolder) s = ObjectVignettingSeparation(imgs[0], bg, **kwargs) for img in imgs[1:]: fit = s.addImg(img) if debugFolder and fit is not False: imwrite(debugFolder.join('fit_%s.tiff' % len(s.fits)), fit) if debugFolder: imwrite(debugFolder.join('init.tiff'), s.flatField) smoothed_ff, mask, flatField, obj = s.separate() if debugFolder: imwrite(debugFolder.join('object.tiff'), obj) imwrite(debugFolder.join('flatfield.tiff'), flatField, dtype=float) imwrite(debugFolder.join('flatfield_smoothed.tiff'), smoothed_ff, dtype=float) return smoothed_ff, mask
def fillFromState(self, lines, appendRows=False): self._allowModelToModifyCells = False if appendRows: row0 = self.rowCount() else: row0 = 0 self.clearContents() self.paths = [] nrows = row0 + len(lines) self.setRowCount(nrows) self._new_rows = [] for row, line in enumerate(lines): row += row0 for col, txt in enumerate(line): if txt != '': self.setCell(row, col, txt) item = self.item(row, 0) if len(line) and item: self.paths.append(PathStr(item.text())) self._setPathItem(row) self._setRowItem(row) self._new_rows.append(row) else: nrows -= 1 self.drawWidget.setExamplePath(self.paths[0]) self.setRowCount(nrows) self._checkShowOptionsColumn() self._new_paths = self.paths[row0:] self.filled.emit()
def addFilePath(self, filepath): ''' create a new display for one ore more given file paths INPUT: "Path/To/File.txt" ''' if filepath: return self.currentWorkspace().addFiles([PathStr(filepath)])
def getExistingDirectory(self, **kwargs): kwargs = self._processOpenKwargs(kwargs) fname = QtWidgets.QFileDialog.getExistingDirectory(**kwargs) if fname: p = PathStr(fname) self.opts['open'] = p.dirname() return p
def html2data(html): ''' extract either tables or images from html code ''' paths = [] # data = [] doc = lxml.html.fromstring(html) #images in html for img in doc.cssselect("img"): #doc.xpath('img'): # get the scr-path of the image: imgsrc = img.get('src') fname = PathStr(imgsrc).basename() if not hasattr(html2data, 'TMP_IMG_DIR'): html2data.TMP_IMG_DIR = PathStr(tempfile.mkdtemp('tmpImgDir')) fpath = html2data.TMP_IMG_DIR.join(fname) #in case img src is within HTML code: if not fname.filetype(): ftype = imgsrc[imgsrc.index('image/') + 6:imgsrc.index(';')] fpath = fpath.setFiletype(ftype) # download the image in a temporary folder: urllib.request.urlretrieve(imgsrc, fpath) paths.append(fpath) #TODO: doesnt work and table import from internet #...is not really needed # # tables # table = _html2PyTable(doc) # if table: # data.append(table) return paths #, data # def _html2PyTable(doc): # table = [] # try: # rows = doc.cssselect("tr") # # TODO: # except: # lxml.cccselect uses __import__ which doesnt work with pyinstaller # print('dynamic import error in lxml.cccselect') # return [] # for row in rows: # table.append(list()) # for td in row.cssselect("td"): # table[-1].append(str(td.text_content())) # return table
def _copyToNewFolder(self): f = QtWidgets.QFileDialog.getExistingDirectory() if f: m = self.selectionModel() for index in m.selectedRows(): y = index.row() path = self._path2(y) self._root.join(path).copy(PathStr(f).join(path.basename())) QtGui.QDesktopServices.openUrl(QtCore.QUrl(f))
def getOpenFileName(self, **kwargs): kwargs = self._processOpenKwargs(kwargs) fname = QtWidgets.QFileDialog.getOpenFileName(**kwargs) if isinstance(fname, tuple): fname = fname[0] if fname: p = PathStr(fname) self.opts['open'] = p.dirname() return p
def plotFitResult(fit, show_legend=True, show_plots=True, save_to_file=False, foldername='', filename='', filetype='png'): from matplotlib import pyplot xvals = fit.xvals yvals = fit.yvals fit = fit.fitValues(xvals) fig, ax = pyplot.subplots(1) ax.plot(xvals, yvals, label='histogram', linewidth=3) for n, f in enumerate(fit): ax.plot(xvals, f, label='peak %i' % (n + 1), linewidth=6) l2 = ax.legend(loc='upper center', bbox_to_anchor=(0.7, 1.05), ncol=3, fancybox=True, shadow=True) l2.set_visible(show_legend) pyplot.xlabel('pixel value') pyplot.ylabel('number of pixels') if save_to_file: p = PathStr(foldername).join(filename).setFiletype(filetype) pyplot.savefig(p) with open(PathStr(foldername).join('%s_params.csv' % filename), 'w') as f: f.write('#x, #y, #fit\n') for n, (x, y, ys) in enumerate(zip(xvals, yvals)): fstr = ', '.join(str(f[n]) for f in fit) f.write('%s, %s, %s\n' % (x, y, fstr)) if show_plots: pyplot.show()
def start(self): ''' configure the server and check the message-inbox every self.opts['refeshrate'] ''' self._files = PathStr(self.opts['folder']).listdir() self.timer = QtCore.QTimer() self.timer.timeout.connect(self.checkFolder) self.timer.start(self.opts['refreshrate'])
def _docopyToNewFolder(self): self.setEnabled(True) f = self._tempF m = self.selectionModel() for index in m.selectedRows(): y = index.row() path = self._path2(y) self._root.join(path).copy(PathStr(f).join(path.basename())) QtGui.QDesktopServices.openUrl(QtCore.QUrl(f)) self._downloadDone = self._downloadDone_backup del self._tempF del self._downloadDone_backup
def checkFolder(self): ''' Check for new files/folders in self.opts['folder'] and import them into dataArtist ''' fo = PathStr(self.opts['folder']) o = self.opts['files only'] files = fo.listdir() for f in files: if f not in self._files: ff = fo.join(f) if not o or ff.isfile(): self.gui.addFilePath(ff) self._files = files
def _getDockTitle(title, number, names): ''' create a title for this display dock names -> list instances of PathStr ''' docktitle = '[%s] ' % number if title: docktitle += title if len(names) > 1: #if multiple files imported: #FOLDER IN TITLE dirname = PathStr(names[0]).dirname() if len(dirname) > 20: dirname = '~' + dirname[-20:] docktitle += "%s files from %s" % (len(names), dirname) elif names and names != [None]: #FILENAME IN TITLE name = PathStr(names[0]).basename() if len(name) > 20: name = name[:8] + '(...)' + name[-8:] docktitle += name return docktitle
def equalizeImage(img, save_path=None, name_additive='_eqHist'): ''' Equalize the histogram (contrast) of an image works with RGB/multi-channel images and flat-arrays @param img - image_path or np.array @param save_path if given output images will be saved there @param name_additive if given this additive will be appended to output images @return output images if input images are numpy.arrays and no save_path is given @return None elsewise ''' if isinstance(img, string_types): img = PathStr(img) if not img.exists(): raise Exception("image path doesn't exist") img_name = img.basename().replace('.', '%s.' % name_additive) if save_path is None: save_path = img.dirname() img = cv2.imread(img) if img.dtype != np.dtype('uint8'): # openCV cannot work with float arrays or uint > 8bit eqFn = _equalizeHistogram else: eqFn = cv2.equalizeHist if len(img.shape) == 3: # multi channel img like rgb for i in range(img.shape[2]): img[:, :, i] = eqFn(img[:, :, i]) else: # grey scale image img = eqFn(img) if save_path: img_name = PathStr(save_path).join(img_name) cv2.imwrite(img_name, img) return img
def __call__(self, path, prevFn=None, nextFn=None): ftype = PathStr(path).filetype() V = None if ftype != 'svg': if ftype in QtGui.QImageReader.supportedImageFormats(): V = InlineView_image elif ftype == 'csv': V = _CSVviewer elif ftype == 'txt': V = _Txtviewer if V is None: return os.startfile(path) self._prep(V, path, prevFn, nextFn) return V
def valsFromPath(self): colDate = MATRIX_HEADER.index('Date') for row in range(self.rowCount()): if self._allowModelToModifyCells: path = PathStr(self.item(row, 0).text()) _success, entries = self.drawWidget.model(path) for col, e in enumerate(entries): col += 1 # if e == '': # item = self.item(row, col) # if hasattr(item, 'metaText'): # e = self.item(row, col).metaText if e != '': self.setCell(row, col, str(e)) # add date from file date is not already given: if not self.itemText(row, colDate): self.setCell(row, colDate, datetime.fromtimestamp( path.date()).strftime('%Y/%m/%d %H:%M:%S'))
def html2data(html): ''' extract either tables or images from html code ''' paths = [] data = [] doc = lxml.html.fromstring(html) #images in html for img in doc.xpath('img'): #get the scr-path of the image: imgsrc = img.get('src') fname = PathStr(imgsrc).basename() fpath = TMP_IMG_DIR.join(fname) #download the image in a temporary folder: urllib.urlretrieve(imgsrc, fpath) paths.append(fpath) #tables table = _html2PyTable(doc) if table: data.append(table) return paths, data
def _getFilePathsFromUrls(self, urls): ''' return a list of all file paths in event.mimeData.urls() ''' l = [] def _appendRecursive(path): if path.isfile(): #file l.append(path) else: for f in path: #for all files in folder _appendRecursive(path.join(f)) # one or more files/folders are dropped for url in urls: if url.isLocalFile(): path = PathStr(url.toLocalFile().toLocal8Bit().data()) if path.exists(): _appendRecursive(path) return l
def model(self, path): ''' depending on adjusted label positions, split given path into MATRIX row ''' names = path.splitNames()[-self.N_LAST_FOLDERS:] names[-1] = PathStr(names[-1]).rmFileType() out = [''] * (len(MATRIX_HEADER) - 1) success = True for i, n in enumerate(names): item = self._lGrid.itemAtPosition(1, (2 * i) + 1) if item: txt = item.widget().text() try: fn = CAT_FUNCTIONS[txt] fn(out, n) except KeyError: toRow(out, parsePath(n, txt)) except IndexError: pass return success, out
def _getFilePathsFromUrls(self, urls): ''' return a list of all file paths in event.mimeData.urls() ''' limg, lagenda = [], [] def _appendRecursive(path): if path.isfile(): if path.filetype() in IMG_FILETYPES: limg.append(path) elif path.filetype() == 'csv': lagenda.append(path) else: for f in path: # for all files in folder _appendRecursive(path.join(f)) # one or more files/folders are dropped for url in urls: if url.isLocalFile(): path = PathStr(url.toLocalFile()) if path.exists(): _appendRecursive(path) return limg, lagenda
mx = mn img /= mx img = exposure.equalize_hist(img, nbins=nBins) img *= mx if intType: img = img.astype(intType) return img if __name__ == '__main__': import sys import pylab as plt import imgProcessor from imgProcessor.imgIO import imread img = imread(PathStr(imgProcessor.__file__).dirname().join( 'media', 'electroluminescence', 'EL_module_orig.PNG')) eq = equalizeImage(img.copy()) if 'no_window' not in sys.argv: plt.figure('original') plt.imshow(img) plt.figure('equalised histogram') plt.imshow(eq) plt.show()
''' Quantitative ElectroLuminescence Analysis ''' from fancytools.os.PathStr import PathStr name = 'QELA' __version__ = '-' # time stamp, set by server during packaging __author__ = 'Karl Bedrich' __email__ = '*****@*****.**' __license__ = 'GPLv3' # MEDIA_PATH = PathStr.getcwd().join('client', 'media') #only works if # executed from main dir MEDIA_PATH = PathStr(__file__).dirname().join( 'media') # TODO: works in FROZEN? ICON = MEDIA_PATH.join('logo.svg') PATH = PathStr.home().mkdir(".%s" % name)
def getBackgroundLevel(img): #seems to be best one according of no-ref bg comparison #as done for SNR article in BEDRICH2016 JPV return median_filter(img[10:-10:10,10:-10:10],7).min() if __name__ == '__main__': import sys from fancytools.os.PathStr import PathStr import imgProcessor from imgProcessor.imgIO import imread import pylab as plt p = PathStr(imgProcessor.__file__).dirname().join( 'media', 'electroluminescence') img = imread(p.join('EL_cell_cracked.png'), 'gray') snr = SNR(img) if 'no_window' not in sys.argv: plt.figure('image') plt.imshow(img) plt.colorbar() plt.figure('SNR') plt.imshow(snr) plt.colorbar() plt.show()
# self.camOpts.addItem(c['name']) # self.camOpts.setCurrentIndex(self.camOpts.count() - 1) if __name__ == '__main__': import sys from fancytools.os.PathStr import PathStr ####################### # temporary fix: app crack doesnt through exception # https://stackoverflow.com/questions/38020020/pyqt5-app-exits-on-error-where-pyqt4-app-would-not sys.excepthook = lambda t, v, b: sys.__excepthook__(t, v, b) ####################### app = QtWidgets.QApplication([]) w = TabCheck() imgs = PathStr( r'D:\Measurements\TrinaPID_EL\EL\source\02ND ROUND\8A').files() # w.addImgs(imgs) # w.buildTree('''ID1 {"grid":[6,10], "sublines":[[],[]]} # meas1 {"vertices":[[[0, 0], [2, 0], [1, 1], [0, 1]]], "validated":0} # cur1 # cur2 # mea2 {"vertices":[[[0, 0], [2, 0], [1, 1], [0, 1]]], "validated":0} # cur1 # cur2 # ID2 # ID3 # ''') # childItem = QtWidgets.QTreeWidgetItem(w.list.item(0)) # w.list.item(0).insertChild(0, 'ss')