def __call__(self, parameter): field = ctx.field(fieldName(parameter)) # TODO: do the following for all outputs that shall be automatically # saved and loaded (but not for all external types, i.e. not if the field # is a string filename field that is set by the user): if parameter.typ == 'image': # generate filenames if parameter.channel == 'input' and not self.parameterAvailable(parameter): return None # (optional) input image not given filename = self._imageFilenames.get(parameter) if filename is None: fd, filename = self.mkstemp(parameter.defaultExtension()) os.close(fd) self._imageFilenames[parameter] = filename return filename else: if parameter.channel == 'output': if parameter.default is None: return None # nothing to be passed into the CLI module if parameter.isNumericVector(): return ",".join(field.stringValue().split()) elif parameter.typ == 'boolean': if field.value: return True # just set the flag, don't pass an arg else: return None # option not set -> no flag must be passed elif parameter.typ == 'file' and not field.value: return None # don't pass empty filenames else: return str(field.value)
def saveInputImages(self): for p, filename in arg.inputImageFilenames(): if os.path.exists(filename) and os.path.getsize(filename): continue ioModule = ctx.module(fieldName(p)) ioModule.field('unresolvedFileName').value = filename ioModule.field('save').touch()
def compileCommand(self): arguments, options, outputs = cliModule.classifyParameters() command = [cliModule.path] for p in options: value = arg(p) if value is None: # missing optional arg / output arg (without default) / false bool continue if p.longflag is not None: command.append(p.longflag) else: command.append(p.flag) # boolean is special cased, because we need to decide # about passing --longflag without arg: if value != True: command.append(value) if outputs: command.append('--returnparameterfile') fd, self.returnParameterFilename = arg.mkstemp('.params') os.close(fd) command.append(self.returnParameterFilename) for p in arguments: value = arg(p) if value is None: clear() return "%s: Input image %r is not optional!\n" % (cliModule.name, fieldName(p)) command.append(value) return command
def cleanupTemporaryFile(self, touchedFieldName): """Remove a single temporary file for an image which was touch()ed and thus cannot be reused. Does nothing (in particular, throws no error) if no entry for that field name is found. """ for p, fn in self._imageFilenames.items(): if fieldName(p) == touchedFieldName: if os.path.exists(fn): os.unlink(fn) # we need to correctly keep track of _imageFilenames, # since inputImageFilenames() and # outputImageFilenames() must not return old # filenames: del self._imageFilenames[p] return
def parameterAvailable(self, parameter): field = ctx.field(fieldName(parameter)) if parameter.typ == 'image' and field.image(): return True return False
def loadOutputImages(self): for p, filename in arg.outputImageFilenames(): ioModule = ctx.module(fieldName(p)) ioModule.field('unresolvedFileName').value = filename