Source code for SimUtils.Readers

from Cell import Cell
import csv
import gzip
import numpy as np

[docs]def readSigmaAndTauFromPIF(filename,size,border=False): if file.endswith('.gz'): f = gzip.open(filename) else: f = open(filename,'r') reader = csv.reader(f, delimiter='\t') # Numpy array to store grid sigma = np.zeros((size[0],size[1],3)) tau = np.zeros_like(sigma) try: for row in reader: s = int(row[0]) t = row[1] # make sure that all described lattice sites are set # pif format: sigma type x1 x2 y1 y2 for x in range(int(row[2]),int(row[3])+1): for y in range(int(row[4]),int(row[5])+1): sigma[x,y] = sigma tau[x,y] = t f.close() except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) if border: sigma = _removeBorder(sigma) tau = _removeBorder(tau) return sigma,tau
[docs]def readCellsFromSaveCells(filename,ignoretypes=None): """ Reader to read cell data from data files containing all pixels for each cell at one MCS. These files are formatted like: id\ttype\t(x0,y0)\t...\t(xn,yn). :param filename: path to data file :param ignoretypes: list of cell types that will not be saved :return: list of :class:`~SimUtils.Cell` instances """ if ignoretypes is None: ignoretypes = [] if filename.endswith('.gz'): f = gzip.open(filename,'r') else: f = open(filename,'r') reader = csv.reader(f, delimiter='\t') celllist = [] try: for row in reader: newcell = Cell(int(row[0]),int(row[1]),[],()) if newcell.type not in ignoretypes: nofpoints = 0 for item in row[2:len(row)]: newcell.addPixel(tuple(int(s) for s in item[1:-1].split(','))) nofpoints = nofpoints + 1 celllist.append(newcell) return celllist except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) f.close()
[docs]def readCellDictFromPif(filename): """ Reader that read data from a CC3D PIF file :param filename: path to data file :return: dictionary with cellid (sigma) as keys and :class:`~SimUtils.Cell` instances as values """ f = open(filename,'r') reader = csv.reader(f, delimiter=' ') dict = {} types = [] try: for row in reader: id = row[0] if not id in dict: if row[1] not in types: types.append(row[1]) dict[id] = Cell(int(id),len(types),[]) for x in range(int(row[2]),int(row[3])+1): for y in range(int(row[4]),int(row[5])+1): dict[id].addPixel((x,y)) f.close() return dict except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
[docs]def readCells2Array(filename,size,ignoretypes=None): """ Reader to read cell data from data files containing all pixels for each cell at one MCS. These files are formatted like: id\ttype\t(x0,y0)\t...\t(xn,yn). The data is returned as numpy array that represent the sigma values on the CPM grid. :param filename: path to data file :param size: size of the CPM grid (nx,ny) :param ignoretypes: list of cell types that will not be saved :return: numpy array representing the CPM grid """ if ignoretypes is None: ignoretypes = [] import numpy as np if filename.endswith('.gz'): f = gzip.open(filename,'r') else: f = open(filename,'r') reader = csv.reader(f, delimiter='\t') a = np.zeros(size) try: for row in reader: id = int(row[0]) newcell = Cell(int(row[0]),int(row[1]),[],()) if int(row[1]) not in ignoretypes: for item in row[2:len(row)]: pix = tuple(int(s) for s in item[1:-1].split(',')) a[pix[1],pix[0]] = id return a except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) f.close()
[docs]def readCellDictFromSigma(sigmafile,typefile,ignore=None): """ Reader to read cell data from CPM grid into a dictionary of :class:`~SimUtils.Cell` instances. :param filename: path to data file :param ignoretypes: list of cell types that will not be saved :return: dictionary with cellid (sigma) as keys and :class:`~SimUtils.Cell` instances as values """ if ignore is None: ignore = [] #~ print 'boeh' import numpy as np sigma = np.loadtxt(sigmafile) type = np.loadtxt(typefile) cells = {} for id in np.unique(sigma): if id == 0: continue celltype = np.unique(type[sigma==id]) if celltype in ignore: continue pixels = np.nonzero(sigma==id) if len(celltype) > 1: print 'this cannot be happening :(' newcell = Cell(id,celltype,[],()) for i in range(len(pixels[0])): newcell.addPixel((pixels[0][i],pixels[1][i])) cells[id] = newcell return cells
[docs]def readCellDictFromSaveCells(filename): """ Reader to read cell data from data files containing all pixels for each cell at one MCS. These files are formatted like: id\ttype\t(x0,y0)\t...\t(xn,yn). :param filename: path to data file :return: dictionary with cellid (sigma) as keys and :class:`~SimUtils.Cell` instances as values """ #~ f = open(filename,'r') if filename.endswith('.gz'): f = gzip.open(filename,'r') else: f = open(filename,'r') reader = csv.reader(f, delimiter='\t') celldict = {} try: for row in reader: newcell = Cell(int(row[0]),int(row[1]),[],()) nofpoints = 0 for item in row[2:len(row)]: newcell.addPixel(tuple(int(s) for s in item[1:-1].split(','))) nofpoints = nofpoints + 1 celldict[newcell.id] = newcell #~ celllist.append(newcell) return celldict except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) f.close() #~ def readFieldFromSaveField(filename): #~ data = np.loadtxt(filename,dtype='float') #~ field = Field({'xmin':int(min(data[:,0])),'xmax':int(max(data[:,0])),'ymin':int(min(data[:,1])),'ymax':int(max(data[:,1]))}) #~ for line in data: #~ field.addPixel(line[0],line[1],line[2]) #~ return field
[docs]def readField2Array(filename,size): """ Read field and save a numpy array. Data is formatted like: x\ty\tfieldvalue :param filename: data file :param size: field size (x,y) :return: numpy array with field values """ import numpy as np data = np.loadtxt(filename,dtype='float') field = np.zeros(size) for line in data: field[int(line[0]),int(line[1])] = float(line[2]) return field ## Read colormap from file # Read color for each cell type from file with format: id\tr\tg\tb # @param filename full path to file
[docs]def readColorMap(filename): """ Create a colormap from input file formatted like: cellid\tr\tg\tb. The colormap is a dictionary with cellid as keys and colors (r,g,b) as values. """ f = open(filename,'r') lines = f.readlines() colormap = {} for line in lines: try: sline = line.split() colormap[int(sline[0])]=(int(sline[1]),int(sline[2]),int(sline[3])) except: print 'bla' f.close() return colormap
def _readGrid(simid,t,suffix,indir,gzipped,border): if gzipped: grid = np.loadtxt(indir+'/'+simid+'/'+simid+suffix+str(t)+'.data.gz') else: grid = np.loadtxt(indir+'/'+simid+suffix+str(t)+'.data') (nx,ny) = grid.shape if border: return _removeBorder(grid) else: return grid def _removeBorder(grid): # cut off border cell (nx,ny) = grid.shape grid[0:nx,0] = 0 grid[0:nx,ny-1] = 0 grid[0,0:ny] = 0 grid[nx-1,0:ny] = 0 return grid
[docs]def readSigma(simid,t,indir,gzipped=True,border=True): """ Load data from cell field (sigma) as an array. :param simid: simulation identifier :param t: time step :param indir: path to data files :param gzipped: data is gzipped (gzipped data is expected to be in indir/simid/) :param border: cut of border pixels :return: CPM grid """ return _readGrid(simid,t,'_CF_',indir,gzipped,border)
[docs]def readTau(simid,t,indir,gzipped=True,border=True): """ Load data from type field (tau) as an array. :param simid: simulation identifier :param t: time step :param indir: path to data files :param gzipped: data is gzipped (gzipped data is expected to be in indir/simid/) :param border: cut of border pixels :return: type grid """ return _readGrid(simid,t,'_TF_',indir,gzipped,border)
[docs]def readChemField(simid,t,indir,fieldname,gzipped=True,border=True): """ Load data from chemical field as an array. :param simid: simulation identifier :param t: time step :param indir: path to data files :param fieldname: name of the field in CC3D :param gzipped: data is gzipped (gzipped data is expected to be in indir/simid/) :param border: cut of border pixels :return: type grid """ return _readGrid(simid,t,'_'+fieldname+'_',indir,gzipped,border)
[docs]def readSimMap(file,col): """ Read file that describes the relation between a simulation id and a given parameter. The data is expected to be formatted like:: #simid par1 par2 sim1 1000 10 ... ... ... simN 10000 50 :param file: file with simulation data :param col: column from which data is read :return: parameter name and dictionary with simid as keys and parameter value as values """ f = open(file,'r') parname = f.readline().split('\t')[col].rstrip('\n') map = {} for line in f.readlines(): if len(line.split('\t')) > col: map[line.split('\t')[0]] = line.split('\t')[col].rstrip('\n') return parname,map
[docs]def readRepSimMap(file,col=1): """ Read file that describes the relation between a simulation id and a given parameter, with an antry for every seed. The data is expected to be formatted like:: #simid par1 par2 sim1 1000 10 ... ... ... simN 10000 50 :param file: file with simulation data :param col: column from which data is read :return: parameter name, dictionary with simid as keys and parameter value as values, and list of repeat numbers """ f = open(file,'r') head = f.readline() name = head.split('\t')[col] idlist = [line.split('\t')[0] for line in f.readlines()] uidlist = np.unique([id.split('-')[0] for id in idlist]) numlist = np.unique([id.split('-')[1] for id in idlist]) par = np.loadtxt(file,skiprows=1,usecols=(col,)) map = dict((id,float(par[idlist.index(id+'-'+numlist[0])])) for id in uidlist) return name,map,numlist