def fileNameToIlastikOuput(filename): """Converts *ClearMap* file name to an argument string for use with Ilastik headless mode Arguments: filename (str): image file name or expression Returns: str: Ilastik headless ouput specifications Note: The output is formated accroding to the Ilastik pixel calssification output specifications """ if not isValidOutputFileName(filename): raise RuntimeError('Ilastik: file format not compatibel with Ilastik output'); if io.isFileExpression(filename): o = '--output_format="' + io.fileExtension(filename) + ' sequence" '+ \ '--output_filename_format="' + filelist.fileExpressionToFileName(filename, '{slice_index}') + '"'; return o; else: # single file extensionToOuput = {'bmp' : 'bmp', 'gif' : 'gif', 'hdr' : 'hrd', 'jpg' : 'jpg', 'jpeg': 'jpeg','pbm' : 'pbm', 'pgm' : 'pgm', 'png' : 'png', 'pnm' : 'pnm', 'ppm' : 'ppm', 'ras' : 'ras', 'tif' : 'tif', 'tiff': 'tiff','xv' : 'xv', 'h5' : 'hdf5' , 'npy' : 'numpy'}; ext = extensionToOuput[io.fileExtension(filename)]; o = '--output_format="' + ext +'" ' + \ '--output_filename_format="' + filename + '"'; return o;
def fileNameToIlastikOuput(filename): """Converts *ClearMap* file name to an argument string for use with Ilastik headless mode Arguments: filename (str): image file name or expression Returns: str: Ilastik headless ouput specifications Note: The output is formated accroding to the Ilastik pixel calssification output specifications """ if not isValidOutputFileName(filename): raise RuntimeError("Ilastik: file format not compatibel with Ilastik output") if io.isFileExpression(filename): o = ( '--output_format="' + io.fileExtension(filename) + ' sequence" ' + '--output_filename_format="' + filelist.fileExpressionToFileName(filename, "{slice_index}") + '"' ) return o else: # single file extensionToOuput = { "bmp": "bmp", "gif": "gif", "hdr": "hrd", "jpg": "jpg", "jpeg": "jpeg", "pbm": "pbm", "pgm": "pgm", "png": "png", "pnm": "pnm", "ppm": "ppm", "ras": "ras", "tif": "tif", "tiff": "tiff", "xv": "xv", "h5": "hdf5", "npy": "numpy", } ext = extensionToOuput[io.fileExtension(filename)] o = '--output_format="' + ext + '" ' + '--output_filename_format="' + filename + '"' return o
def isValidOutputFileName(filename): """Checks if the file is a valid format for use with Ilastik ouput Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be written by Ilastik """ if io.isFileExpression(filename): validExtensions = [ "bmp", "gif", "hdr", "jpg", "jpeg", "pbm", "pgm", "png", "pnm", "ppm", "ras", "tif", "tiff", "xv", ] return io.fileExtension(filename) in validExtensions else: validExtensions = [ "bmp", "gif", "hdr", "jpg", "jpeg", "pbm", "pgm", "png", "pnm", "ppm", "ras", "tif", "tiff", "xv", "h5", "npy", ] return io.fileExtension(filename) in validExtensions
def isValidOutputFileName(filename): """Checks if the file is a valid format for use with Ilastik ouput Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be written by Ilastik """ if io.isFileExpression(filename): validExtensions = ['bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv']; return io.fileExtension(filename) in validExtensions; else: validExtensions = ['bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv', 'h5', 'npy']; return io.fileExtension(filename) in validExtensions;
def isValidInputFileName(filename): """Checks if the file is a valid format for use with Ilastik input Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be read by Ilastik """ validExtensions = [ "bmp", "exr", "gif", "jpg", "jpeg", "tif", "tiff", "ras", "png", "pbm", "pgm", "ppm", "pnm", "hdr", "xv", "npy", ] return io.fileExtension(filename) in validExtensions
def copyData(source, sink): """Copy a raw/mhd file pair from source to sink Arguments: source (str): file name of source sink (str): file name of sink Returns: str: file name of the copy """ sourceExt = io.fileExtension(source) sinkExt = io.fileExtension(sink) sources = [source] sinks = [] if sourceExt == 'raw': sources.append(source[:-3] + 'mhd') if sinkExt == 'raw': sinks.append(sink) sinks.append(sink[:-3] + 'mhd') elif sinkExt == 'mhd': sinks.append(sink[:-3] + 'raw') sinks.append(sink) else: raise RuntimeError('copyData: sink extension %s not raw or mhd' % sinkExt) elif sourceExt == 'mhd': sources.append(source[:-3] + 'raw') if sinkExt == 'raw': sinks.append(sink[:-3] + 'mhd') sinks.append(sink) elif sinkExt == 'mhd': sinks.append(sink) sinks.append(sink[:-3] + 'raw') else: raise RuntimeError('copyData: sink extension %s not raw or mhd' % sinkExt) for i in range(2): io.copyData(sources[i], sinks[i]) return sink
def writeData(filename, data, **args): """ Write data into to raw/mhd file pair Arguments: filename (str): file name as regular expression data (array): data to write to raw file Returns: str: file name of mhd file """ fext = io.fileExtension(filename) if fext == "raw": fname = filename[:-3] + 'mhd' else: fname = filename assert (fname[-4:] == '.mhd') meta_dict = {} meta_dict['ObjectType'] = 'Image' meta_dict['BinaryData'] = 'True' meta_dict['BinaryDataByteOrderMSB'] = 'False' numpy_to_datatype = { numpy.dtype('int8'): "MET_CHAR", numpy.dtype('uint8'): "MET_UCHAR", numpy.dtype('int16'): "MET_SHORT", numpy.dtype('uint16'): "MET_USHORT", numpy.dtype('int32'): "MET_INT", numpy.dtype('uint32'): "MET_UINT", numpy.dtype('int64'): "MET_LONG", numpy.dtype('uint64'): "MET_ULONG", numpy.dtype('float32'): "MET_FLOAT", numpy.dtype('float64'): "MET_DOUBLE", } dtype = data.dtype meta_dict['ElementType'] = numpy_to_datatype[dtype] dsize = list(data.shape) #dsize[0:2] = [dsize[1],dsize[0]]; #fix arrays represented as (y,x,z) meta_dict['NDims'] = str(len(dsize)) meta_dict['DimSize'] = ' '.join([str(i) for i in dsize]) meta_dict['ElementDataFile'] = os.path.split(fname)[1].replace( '.mhd', '.raw') writeHeader(fname, meta_dict) pwd = os.path.split(fname)[0] if pwd: data_file = pwd + '/' + meta_dict['ElementDataFile'] else: data_file = meta_dict['ElementDataFile'] writeRawData(data_file, data) return fname
def fileNameToIlastikOuput(filename): """Converts *ClearMap* file name to an argument string for use with Ilastik headless mode Arguments: filename (str): image file name or expression Returns: str: Ilastik headless ouput specifications Note: The output is formated accroding to the Ilastik pixel calssification output specifications """ if not isValidOutputFileName(filename): raise RuntimeError( 'Ilastik: file format not compatibel with Ilastik output') if io.isFileExpression(filename): o = '--output_format="' + io.fileExtension(filename) + ' sequence" '+ \ '--output_filename_format="' + filelist.fileExpressionToFileName(filename, '{slice_index}') + '"' return o else: # single file extensionToOuput = { 'bmp': 'bmp', 'gif': 'gif', 'hdr': 'hrd', 'jpg': 'jpg', 'jpeg': 'jpeg', 'pbm': 'pbm', 'pgm': 'pgm', 'png': 'png', 'pnm': 'pnm', 'ppm': 'ppm', 'ras': 'ras', 'tif': 'tif', 'tiff': 'tiff', 'xv': 'xv', 'h5': 'hdf5', 'npy': 'numpy' } ext = extensionToOuput[io.fileExtension(filename)] o = '--output_format="' + ext +'" ' + \ '--output_filename_format="' + filename + '"' return o
def copyData(source, sink): """Copy a raw/mhd file pair from source to sink Arguments: source (str): file name of source sink (str): file name of sink Returns: str: file name of the copy """ sourceExt = io.fileExtension(source); sinkExt = io.fileExtension(sink); sources = [source]; sinks = []; if sourceExt == 'raw': sources.append(source[:-3] + 'mhd'); if sinkExt == 'raw': sinks.append(sink); sinks.append(sink[:-3] + 'mhd'); elif sinkExt == 'mhd': sinks.append(sink[:-3] + 'raw'); sinks.append(sink); else: raise RuntimeError('copyData: sink extension %s not raw or mhd' % sinkExt); elif sourceExt == 'mhd': sources.append(source[:-3] + 'raw'); if sinkExt == 'raw': sinks.append(sink[:-3] + 'mhd'); sinks.append(sink); elif sinkExt == 'mhd': sinks.append(sink); sinks.append(sink[:-3] + 'raw'); else: raise RuntimeError('copyData: sink extension %s not raw or mhd' % sinkExt); for i in range(2): io.copyData(sources[i], sinks[i]); return sink;
def writeData(filename, data, **args): """ Write data into to raw/mhd file pair Arguments: filename (str): file name as regular expression data (array): data to write to raw file Returns: str: file name of mhd file """ fext = io.fileExtension(filename); if fext == "raw": fname = filename[:-3] + 'mhd'; else: fname = filename; assert(fname[-4:]=='.mhd') meta_dict = {} meta_dict['ObjectType'] = 'Image' meta_dict['BinaryData'] = 'True' meta_dict['BinaryDataByteOrderMSB'] = 'False' numpy_to_datatype = {numpy.dtype('int8') : "MET_CHAR", numpy.dtype('uint8') : "MET_UCHAR", numpy.dtype('int16') : "MET_SHORT", numpy.dtype('uint16') : "MET_USHORT", numpy.dtype('int32') : "MET_INT", numpy.dtype('uint32') : "MET_UINT", numpy.dtype('int64') : "MET_LONG", numpy.dtype('uint64') : "MET_ULONG", numpy.dtype('float32') : "MET_FLOAT", numpy.dtype('float64') : "MET_DOUBLE", } dtype = data.dtype; meta_dict['ElementType'] = numpy_to_datatype[dtype]; dsize = list(data.shape); #dsize[0:2] = [dsize[1],dsize[0]]; #fix arrays represented as (y,x,z) meta_dict['NDims'] = str(len(dsize)) meta_dict['DimSize'] = ' '.join([str(i) for i in dsize]) meta_dict['ElementDataFile'] = os.path.split(fname)[1].replace('.mhd','.raw') writeHeader(fname, meta_dict) pwd = os.path.split(fname)[0] if pwd: data_file = pwd +'/' + meta_dict['ElementDataFile'] else: data_file = meta_dict['ElementDataFile'] writeRawData(data_file, data) return fname;
def isValidOutputFileName(filename): """Checks if the file is a valid format for use with Ilastik ouput Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be written by Ilastik """ if io.isFileExpression(filename): validExtensions = [ 'bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv' ] return io.fileExtension(filename) in validExtensions else: validExtensions = [ 'bmp', 'gif', 'hdr', 'jpg', 'jpeg', 'pbm', 'pgm', 'png', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xv', 'h5', 'npy' ] return io.fileExtension(filename) in validExtensions
def isValidInputFileName(filename): """Checks if the file is a valid format for use with Ilastik input Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be read by Ilastik """ validExtensions = ['bmp', 'exr', 'gif', 'jpg', 'jpeg', 'tif', 'tiff', 'ras', 'png', 'pbm', 'pgm', 'ppm', 'pnm', 'hdr', 'xv', 'npy']; return io.fileExtension(filename) in validExtensions;
def isValidInputFileName(filename): """Checks if the file is a valid format for use with Ilastik input Arguments: filename (str): image file name or expression Returns: bool: True if the image file can be read by Ilastik """ validExtensions = [ 'bmp', 'exr', 'gif', 'jpg', 'jpeg', 'tif', 'tiff', 'ras', 'png', 'pbm', 'pgm', 'ppm', 'pnm', 'hdr', 'xv', 'npy' ] return io.fileExtension(filename) in validExtensions
def makeColorPalette(filename=None): """Creates a pal file for imaris based on label colors""" global Label maxlabel = max(Label.ids) colarray = numpy.zeros((maxlabel, 3)) for i in Label.ids: colarray[i - 1, :] = Label.color(i) if filename is None: return colarray else: fext = io.fileExtension(filename) if fext == 'pal': writePAL(filename, colarray) elif fext == 'lut': writeLUT(filename, colarray) else: raise RuntimeError('color pallete format: %s not supported' % fext) return colarray
def makeColorPalette(filename=None): """Creates a pal file for imaris based on label colors""" global Label maxlabel = max(Label.ids) colarray = numpy.zeros((maxlabel, 3)) for i in Label.ids: colarray[i - 1, :] = Label.color(i) if filename is None: return colarray else: fext = io.fileExtension(filename) if fext == "pal": writePAL(filename, colarray) elif fext == "lut": writeLUT(filename, colarray) else: raise RuntimeError("color pallete format: %s not supported" % fext) return colarray