def constructFromFile(self, fileName, **args): matrix = ckernel.KernelMatrix() matrix.thisown = 0 patternID = [] delim = None delim = misc.getDelim(fileName) idColumn = 0 if 'idColumn' in args: idColumn = args['idColumn'] if idColumn is None: firstColumn = 0 else: firstColumn = 1 print firstColumn print idColumn matrixFile = myio.myopen(fileName) firstRow = True for line in matrixFile: # skip comments: if line[0] in ["%", "#"]: continue tokens = line.split(delim) # check if the file is in gist format: if firstRow: firstRow = False try: float(tokens[-1]) except: continue if (('headerRow' in args and args['headerRow']) or ('gistFormat' in args and args['gistFormat'])): continue values = arrayWrap.floatVector( [float(token) for token in tokens[firstColumn:]]) matrix.addRow(values) if idColumn is not None: patternID.append(tokens[0]) ckerneldata.KernelData.__init__(self, matrix) if 'labelsFile' in args: self.attachLabels(labels.Labels(args['labelsFile'], **args)) else: self.attachLabels(labels.Labels(None, patternID=patternID))
def constructFromFile(self, fileName, **args): matrix = ckernel.KernelMatrix() matrix.thisown = 0 patternID = [] delim = None delim = misc.getDelim(fileName) idColumn = 0 if "idColumn" in args: idColumn = args["idColumn"] if idColumn is None: firstColumn = 0 else: firstColumn = 1 print firstColumn print idColumn matrixFile = myio.myopen(fileName) firstRow = True for line in matrixFile: # skip comments: if line[0] in ["%", "#"]: continue tokens = line.split(delim) # check if the file is in gist format: if firstRow: firstRow = False try: float(tokens[-1]) except: continue if ("headerRow" in args and args["headerRow"]) or ("gistFormat" in args and args["gistFormat"]): continue values = arrayWrap.floatVector([float(token) for token in tokens[firstColumn:]]) matrix.addRow(values) if idColumn is not None: patternID.append(tokens[0]) ckerneldata.KernelData.__init__(self, matrix) if "labelsFile" in args: self.attachLabels(labels.Labels(args["labelsFile"], **args)) else: self.attachLabels(labels.Labels(None, patternID=patternID))
def combineKernels(ker1file, ker2file, kerOutFile, operation = 'add', **args) : """combine two kernels by either adding or multiplying them. In the case of addition the resulting kernel is of the form: K_out(i,j) = weight * K1(i,j) + (1-weight) * K2(i,j) where the default weight is 0.5 In the case of multiplication the resulting kernel is: K_out(i,j) = (const1 + K1(i,j)) * (const2 + K2(i, j)) where const1 and const2 are 0 by default. Notes: It is assumed that the kernels have the same size and the ids are in the same order (an exception is raised if this is not satisfied). :Parameters: - `operation` - which operation to perform between the kernels; it is a string with supported values 'add' or 'multiply' (add by default) :Keywords: - `weight` - weighting of kernels for kernel addition - `const1,const2` - additive factor in case of kernel multiplication """ weight = 0.5 if 'weight' in args : weight = args['weight'] const1 = 0 if 'const1' in args : const1 = args['const1'] const2 = 0 if 'const2' in args : const2 = args['const2'] import misc delim1 = misc.getDelim(ker1file) delim2 = misc.getDelim(ker2file) ker1 = open(ker1file) ker2 = open(ker2file) kerOut = open(kerOutFile, 'w') # check if kernel is in gist format line1 = ker1.readline() try : float(line1.split(delim1)[-1]) except : line1 = ker1.readline() line2 = ker2.readline() try : float(line2.split(delim2)[-1]) except : line2 = ker2.readline() # check if there's a pattern id: firstToken = 0 try : float(tokens1[0]) except : firstToken = 1 while len(line1) > 0 : tokens1 = line1.split(delim1) tokens2 = line2.split(delim2) if firstToken > 0 : if tokens1[0] != tokens2[0] : print tokens1[0], tokens2[0] raise ValueError, 'kernels do not have the same ids' kerOut.write(tokens1[0] + delim1) if operation == 'add' : outTokens = [str(float(tokens1[i]) * weight + float(tokens2[i]) * (1-weight)) for i in range(firstToken, len(tokens1))] else : outTokens = [str((const1 + float(tokens1[i])) * (const2 + float(tokens2[i]))) for i in range(firstToken, len(tokens1))] kerOut.write(delim1.join(outTokens) + '\n') line1 = ker1.readline() line2 = ker2.readline()