def show(config, path, stack, timepoint): """Preview MIPS or single Z stack from LLSdir""" from llspy.gui.img_dialog import ImgDialog from PyQt5 import QtWidgets import numpy as np logging.getLogger('llspy.llsdir').setLevel('CRITICAL') APP = QtWidgets.QApplication(sys.argv) # if the path provided is a tiff, just show it if os.path.isfile(path) and path.endswith('.tif'): data = util.imread(path) if os.path.isdir(path) and util.pathHasPattern(path, '*Settings.txt'): try: data = None exp = llsdir.LLSdir(path) # unless z-stack was requested, look for mips if not stack: # look for decon first, then deskewed if exp.path.joinpath('GPUdecon').joinpath('MIPs').exists(): path = exp.path.joinpath('GPUdecon').joinpath('MIPs') zmip = util.find_filepattern(path, '*comboMIP_z.tif') data = util.imread(zmip) nt, ny, nx = data.shape data = np.reshape(data, (nt, 1, 1, ny, nx)) elif exp.path.joinpath('Deskewed').joinpath('MIPs').exists(): path = exp.path.joinpath('Deskewed').joinpath('MIPs') zmip = util.find_filepattern(path, '*comboMIP_z.tif') data = util.imread(zmip) nt, ny, nx = data.shape data = np.reshape(data, (nt, 1, 1, ny, nx)) if data is None: data = exp.preview(tR=timepoint, **config) config = exp.localParams() except exceptions.ParametersError as e: click.secho(str(e), fg='red') click.echo('Try setting missing parameters using "lls config"') return dialog = ImgDialog(data, info=config) dialog.show() sys.exit(APP.exec_())
def processFolder(self): folder = self.camCalibFolderLineEdit.text() darkavg = None if os.path.isfile(self.darkAVGLineEdit.text()): darkavg = tf.imread(self.darkAVGLineEdit.text()) elif os.path.isfile(os.path.join(folder, 'dark_AVG.tif')): darkavg = tf.imread(os.path.join(folder, 'dark_AVG.tif')) darkstd = None if os.path.isfile(self.darkSTDLineEdit.text()): darkstd = tf.imread(self.darkSTDLineEdit.text()) elif os.path.isfile(os.path.join(folder, 'dark_STD.tif')): darkstd = tf.imread(os.path.join(folder, 'dark_STD.tif')) if not all([isinstance(a, np.ndarray) for a in (darkavg, darkstd)]): if not pathHasPattern(folder, '*dark*.tif*'): QtW.QMessageBox.warning( self, "No dark images!", 'Camera calibration requires dark images, but none were provided' ' and none were detected in the specified folder.' ' Read documentation on camera calibration for more info.', QtW.QMessageBox.Ok, QtW.QMessageBox.NoButton) return if sum([isinstance(a, np.ndarray) for a in (darkavg, darkstd)]) == 1: if not pathHasPattern(folder, '*dark*.tif*'): QtW.QMessageBox.warning( self, "No dark images!", 'Camera calibration requires both a dark image average projection, ' ' and a standard deviation projection, but only one of the' ' two was provided, and no *dark*.tif images ' ' were detected in the specified folder.' ' Read documentation on camera calibration for more info.', QtW.QMessageBox.Ok, QtW.QMessageBox.NoButton) return else: reply = QtW.QMessageBox.question( self, "No dark images!", 'Camera calibration requires both a dark image average projection, ' ' and a standard deviation projection, but only one of the' ' two was provided. *dark*.tif images ' ' were detected in the specified folder, and will still be ' ' used to calculate the projection images. Continue?', QtW.QMessageBox.Yes | QtW.QMessageBox.No, QtW.QMessageBox.No) if reply != QtW.QMessageBox.Yes: return self.worker, self.thread = newWorkerThread( CamCalibWorker, folder, darkavg, darkstd, workerConnect={ 'progress': self.incrementProgress, 'setProgMax': self.resetWithMax, 'setStatus': self.statusLabel.setText, 'finished': self.abortButton.hide, }, start=True)
def decon(config, path, **kwargs): """Deskew and deconvolve data in LLSDIR.""" # update config with relevant values from # raw deskewed MIPs imply saving Deskewed Raw files if any(kwargs['rMIP']): kwargs['saveDeskewedRaw'] = True # override config with keyword options for key, value in kwargs.items(): if key in config and value is not None: config[key] = value # batch processing if not kwargs['batch'] and not util.pathHasPattern(path): click.secho( 'not a LLS data folder with *Settings.txt!\n' 'use --batch for batch processing', fg='red') sys.exit(1) # # allow for provided OTF directory # if not default_otfdir and options.otfdir is None: # print('Could not find OTF directory at {}'.format(default_otfdir)) # sys.exit('Please specify where the OTFs are with --otfdir') # elif options.otfdir is None: # options.otfdir = default_otfdir def procfolder(dirpath, options): E = llsdir.LLSdir(dirpath) # check whether folder has already been processed by the presence of a # ProcessingLog.txt file if E.has_been_processed() and not options['reprocess']: print("Folder already appears to be processed: {}".format(E.path)) print( "Skipping ... use the '--reprocess' flag to force reprocessing" ) return 0 if options['reprocess'] and E.is_compressed(): # uncompress the raw files first... E.decompress() # if reprocessing, look for a top level MIPs folder and remove it if E.path.joinpath('MIPs').exists(): shutil.rmtree(E.path.joinpath('MIPs')) click.secho("\n" + "#" * (int(len(str(E.path))) + 24) + "\n## ", fg='cyan', nl=False) click.secho("processing: %s " % str(E.path), fg='yellow', nl=False) click.secho("##\n" + "#" * (int(len(str(E.path))) + 24) + "\n", fg='cyan') if options['correctFlash']: if E.is_corrected(): if kwargs['useAlreadyCorrected'] is None: import select click.echo( "Corrected folder already exists! Use it? [y/N]", nl=False) click.secho(" (6 seconds to answer)", blink=True) i, o, e = select.select([sys.stdin], [], [], 6) if i: useCor = ( sys.stdin.readline().strip()[0].lower() == 'y') else: useCor = False click.echo("timed out...") elif kwargs['useAlreadyCorrected']: useCor = True else: useCor = False if useCor: click.echo("Using already corrected files...") options['correctFlash'] = False E.path = E.path.joinpath('Corrected') else: click.echo("recreating corrected files...") try: # try: E.autoprocess(**options) # logdict = None # except Exception as e: # raise click.ClickException(e) except Exception: raise if kwargs['batch']: try: subfolders = util.get_subfolders_containing_filepattern( path, filepattern='*Settings.txt') click.secho("found the following LLS data folders:", fg='magenta') for folder in subfolders: click.secho(folder.split(path)[1], fg='yellow') for folder in subfolders: try: procfolder(folder, config) except voluptuous.error.MultipleInvalid as e: e = str(e).replace("@ data['", 'for ') e = e.strip("'][0]") click.secho("VALIDATION ERROR: %s" % e, fg='red') except exceptions.LLSpyError as e: click.secho("ERROR: %s" % e, fg='red') click.echo('\n\nDone batch processing!') sys.exit(0) except Exception: raise else: try: procfolder(path, config) click.echo('Done!') sys.exit(0) except voluptuous.error.MultipleInvalid as e: e = str(e).replace("@ data['", 'for ') e = e.strip("'][0]") click.secho("VALIDATION ERROR: %s" % e, fg='red') except exceptions.LLSpyError as e: click.secho("ERROR: %s" % e, fg='red') sys.exit(0)
def compress(paths, freeze, _reduce, decompress, recurse, minage, keepmips, depth, dryrun): """Compression & decompression of LLSdir""" exclusive(click.get_current_context().params, ['freeze', '_reduce', 'decompress'], 'options {freeze, reduce, decompress} are mutually exclusive') logging.getLogger('llspy.llsdir').setLevel('CRITICAL') paths = list(paths) if recurse: depth = 1000 # recurse and restrict to LLSdirs for i, path in enumerate(paths): if not util.pathHasPattern(path, pattern='*Settings.txt'): paths.pop(i) subf = sorted( util.get_subfolders_containing_filepattern(path, level=depth)) [paths.insert(i, s) for s in reversed(subf)] # remove duplicates paths = sorted(list(set(paths))) if dryrun: click.secho("DRY RUN: NOTHING PERFORMED!", fg='red', underline=True) for path in paths: try: E = llsdir.LLSdir(path) if E.age < minage: click.secho(' skip:', nl=False, underline=False, fg='blue') click.secho('{} ({} days old)'.format(path, E.age), fg='blue') continue if decompress: click.secho('decompress:', nl=False, underline=False, fg='yellow') click.echo('{}'.format(path)) if not dryrun: E.decompress() continue elif _reduce: click.secho(' reduce:', nl=False, underline=False, fg='yellow') click.echo('{}'.format(path)) if not dryrun: E.reduce_to_raw(keepmip=keepmips) continue else: if freeze: click.secho(' freeze:', nl=False, underline=False, fg='yellow') click.echo('{}'.format(path)) if not dryrun: E.freeze(keepmip=keepmips) else: click.secho(' compress:', nl=False, underline=False, fg='yellow') click.echo('{}'.format(path)) if not dryrun: E.compress() except exceptions.CompressionError as e: logger.warn(e)
def info(paths, verbose, recurse, depth, showsize): """Get info on an LLSDIR. Change verbosity with -v""" logging.getLogger('llspy.llsdir').setLevel('CRITICAL') paths = list(paths) if recurse: depth = 1000 for i, path in enumerate(paths): if not util.pathHasPattern(path, pattern='*Settings.txt'): paths.pop(i) subf = sorted( util.get_subfolders_containing_filepattern(path, level=depth)) [paths.insert(i, s) for s in reversed(subf)] # remove duplicates paths = sorted(list(set(paths))) if verbose == 0 and len(paths): click.echo() headers = [ 'Path', 'nC', 'nT', 'nZ', 'nY', 'nX', 'Angle', 'dZ', 'dXY', 'compr' ] maxPathLen = max([len(util.shortname(str(p), 3)) for p in paths]) + 2 row_format = "{:<%d}{:<4}{:<5}{:<5}{:<6}{:<6}{:<7}{:<7}{:<7}{:<5}" % maxPathLen if showsize: headers.append('size') row_format = row_format + "{:>7}" click.secho(row_format.format(*[str(i) for i in headers]), underline=True, fg='cyan', bold=True) row_format = "{:<4}{:<5}{:<5}{:<6}{:<6}{:<7}{:<7}{:<7}{:<5}" if showsize: row_format = row_format + "{:>7}" for path in paths: try: E = llsdir.LLSdir(path) infolist = [ E.parameters.nc, E.parameters.nt, E.parameters.nz, E.parameters.ny, E.parameters.nx, "{:2.1f}".format(E.parameters.angle) if E.parameters.samplescan else "0", "{:0.3f}".format(E.parameters.dz), "{:0.3f}".format(E.parameters.dx), "Yes" if E.is_compressed() else "No" ] if showsize: foldersize = util.getfoldersize(path, recurse=True) infolist.append(util.format_size(foldersize)) f = "{:<%d}" % maxPathLen short = f.format(util.shortname(str(E.path), 3)) click.secho(os.path.split(short)[0] + os.path.sep, nl=False, fg='magenta', bold=False) click.secho(os.path.split(short)[1], nl=False, fg='yellow', bold=True) click.echo(row_format.format(*[i for i in infolist])) except exceptions.LLSpyError: pass click.echo()