How to use computeNull method in avocado

Best Python code snippet using avocado_python

spark.py

Source:spark.py Github

copy

Full Screen

...66 if self.ruleschanged:67 #68 # XXX - duplicated from parse()69 #70 self.computeNull()71 self.newrules = {}72 self.new2old = {}73 self.makeNewRules()74 self.ruleschanged = 075 self.edges, self.cores = {}, {}76 self.states = { 0: self.makeState0() }77 self.makeState(0, self._BOF)78 #79 # XXX - should find a better way to do this..80 #81 changes = 182 while changes:83 changes = 084 for k, v in self.edges.items():85 if v is None:86 state, sym = k87 if self.states.has_key(state):88 self.goto(state, sym)89 changes = 190 rv = self.__dict__.copy()91 for s in self.states.values():92 del s.items93 del rv['rule2func']94 del rv['nullable']95 del rv['cores']96 return rv97 def __setstate__(self, D):98 self.rules = {}99 self.rule2func = {}100 self.rule2name = {}101 self.collectRules()102 start = D['rules'][self._START][0][1][1] # Blech.103 self.augment(start)104 D['rule2func'] = self.rule2func105 D['makeSet'] = self.makeSet_fast106 self.__dict__ = D107 #108 # A hook for GenericASTBuilder and GenericASTMatcher. Mess109 # thee not with this; nor shall thee toucheth the _preprocess110 # argument to addRule.111 #112 def preprocess(self, rule, func): return rule, func113 def addRule(self, doc, func, _preprocess=1):114 fn = func115 rules = doc.split()116 index = []117 for i in xrange(len(rules)):118 if rules[i] == '::=':119 index.append(i-1)120 index.append(len(rules))121 for i in xrange(len(index)-1):122 lhs = rules[index[i]]123 rhs = rules[index[i]+2:index[i+1]]124 rule = (lhs, tuple(rhs))125 if _preprocess:126 rule, fn = self.preprocess(rule, func)127 if self.rules.has_key(lhs):128 self.rules[lhs].append(rule)129 else:130 self.rules[lhs] = [ rule ]131 self.rule2func[rule] = fn132 self.rule2name[rule] = func.__name__[2:]133 self.ruleschanged = 1134 def collectRules(self):135 for name in _namelist(self):136 if name[:2] == 'p_':137 func = getattr(self, name)138 doc = func.__doc__139 self.addRule(doc, func)140 def augment(self, start):141 rule = '%s ::= %s %s' % (self._START, self._BOF, start)142 self.addRule(rule, lambda args: args[1], 0)143 def computeNull(self):144 self.nullable = {}145 tbd = []146 for rulelist in self.rules.values():147 lhs = rulelist[0][0]148 self.nullable[lhs] = 0149 for rule in rulelist:150 rhs = rule[1]151 if len(rhs) == 0:152 self.nullable[lhs] = 1153 continue154 #155 # We only need to consider rules which156 # consist entirely of nonterminal symbols.157 # This should be a savings on typical158 # grammars.159 #160 for sym in rhs:161 if not self.rules.has_key(sym):162 break163 else:164 tbd.append(rule)165 changes = 1166 while changes:167 changes = 0168 for lhs, rhs in tbd:169 if self.nullable[lhs]:170 continue171 for sym in rhs:172 if not self.nullable[sym]:173 break174 else:175 self.nullable[lhs] = 1176 changes = 1177 def makeState0(self):178 s0 = _State(0, [])179 for rule in self.newrules[self._START]:180 s0.items.append((rule, 0))181 return s0182 def finalState(self, tokens):183 #184 # Yuck.185 #186 if len(self.newrules[self._START]) == 2 and len(tokens) == 0:187 return 1188 start = self.rules[self._START][0][1][1]189 return self.goto(1, start)190 def makeNewRules(self):191 worklist = []192 for rulelist in self.rules.values():193 for rule in rulelist:194 worklist.append((rule, 0, 1, rule))195 for rule, i, candidate, oldrule in worklist:196 lhs, rhs = rule197 n = len(rhs)198 while i < n:199 sym = rhs[i]200 if not self.rules.has_key(sym) or \201 not self.nullable[sym]:202 candidate = 0203 i = i + 1204 continue205 newrhs = list(rhs)206 newrhs[i] = self._NULLABLE+sym207 newrule = (lhs, tuple(newrhs))208 worklist.append((newrule, i+1,209 candidate, oldrule))210 candidate = 0211 i = i + 1212 else:213 if candidate:214 lhs = self._NULLABLE+lhs215 rule = (lhs, rhs)216 if self.newrules.has_key(lhs):217 self.newrules[lhs].append(rule)218 else:219 self.newrules[lhs] = [ rule ]220 self.new2old[rule] = oldrule221 def typestring(self, token):222 return None223 def error(self, token):224 print "Syntax error at or near `%s' token" % token225 raise SystemExit226 def parse(self, tokens):227 sets = [ [(1,0), (2,0)] ]228 self.links = {}229 if self.ruleschanged:230 self.computeNull()231 self.newrules = {}232 self.new2old = {}233 self.makeNewRules()234 self.ruleschanged = 0235 self.edges, self.cores = {}, {}236 self.states = { 0: self.makeState0() }237 self.makeState(0, self._BOF)238 for i in xrange(len(tokens)):239 sets.append([])240 if sets[i] == []:241 break 242 self.makeSet(tokens[i], sets, i)243 else:244 sets.append([])...

Full Screen

Full Screen

SigAvg.py

Source:SigAvg.py Github

copy

Full Screen

1"""File which does the actual analysis for the signature visualization2 Written/modified by Mike Ambrose, mikeambrose@berkeley.edu3Feel free to email with any questions!4"""5import os6from optparse import OptionParser7import math8import iSigDBUtilities as util9#TODO: uncomment this when on server10#os.environ["MPLCONFIGDIR"] = "/UCSC/Pathways-Auxiliary/UCLApathways-Scratch-Space"11import matplotlib12matplotlib.use('Agg')13import nullmodel14from matplotlib.backends.backend_pdf import PdfPages15from collections import OrderedDict16import bisect17import iSigDBConstants1819def getSigGenes(sigFile,selectedSigs,n):20 """Returns dictionary of signature : [top n genes]21 sigFile is the file with the shortened signatures22 n is the number of genes"""23 sigGenes = {}24 with open(sigFile) as sigToGenes:25 for line in sigToGenes:26 splitLine = line.strip("\n").split("\t")27 sig = splitLine[0]28 if sig in selectedSigs:29 sigGenes[sig] = []30 for gene in splitLine[1:n+1]:31 sigGenes[sig].append(gene)32 return sigGenes3334def ranked(sams):35 """for each sample, replaces the gene's value with its rank"""36 rankedSams = OrderedDict()37 for sam in sams:38 samvals = [sams[sam][gene] for gene in sams[sam]]39 ranks = util.getRanks(tuple(samvals))40 rankedSams[sam] = {gene:ranks[sams[sam][gene]] for gene in sams[sam]}41 return rankedSams4243def logall(sams):44 """for each sample, replace the gene's value with the log of its value"""45 logSams = OrderedDict()46 for sam in sams:47 if any(sams[sam][gene] < 0 for gene in sams[sam]):48 util.displayErrorMessage("Log of negative value")49 logSams[sam] = {gene:math.log10(sams[sam][gene]+1) for gene in sams[sam]}50 return logSams5152def delta(sams):53 """for each sample, replaces the gene's value with the difference between its value and the54 mean value across all samples"""55 genes = sams[sams.keys()[0]].keys()56 for gene in genes:57 av = util.average([sams[sam][gene] for sam in sams])58 for sam in sams:59 sams[sam][gene] = sams[sam][gene] - av60 return sams6162def writeValues(sams,sigGenes,compOutput,version,abbrevsDict,av=True,nullVals=None):63 """Writes the values after computation by version to compOutput64 inputFile - user-provided input file65 sigGenes - dictionary of signature : list of genes in signature66 compOutput - where to write output67 version - how to process input68 abbrevsDict - dictionary of abbreviation : full name for each signature in sigGenes"""69 samSigVal = OrderedDict()70 for sam in sams:71 samSigVal[sam] = {}72 for sig in sigGenes:73 geneVals = []74 for gene in sigGenes[sig]:75 if gene not in sams[sam]:76 #the gene does not exist in our input, so it is reported as a N/A77 continue78 geneVals.append(sams[sam][gene])79 if len(geneVals) == 0:80 continue81 if av:82 samSigVal[sam][sig] = util.average(geneVals)83 else:84 samSigVal[sam][sig] = sum(geneVals)85 if 'null' in version:86 #add tooltips with the p-value87 tooltips = {}88 for sam in samSigVal:89 tooltips[sam] = {}90 nullDist = nullVals[sam]91 for sig in samSigVal[sam]:92 currentLoc = bisect.bisect_left(nullDist,samSigVal[sam][sig])93 numGreaterThan = len(nullDist)-currentLoc94 signame = sig if sig not in abbrevsDict else abbrevsDict[sig]95 if 'delta' in version:96 tooltips[sam][signame] = "left-tail <b>{0}</b>; right-tail <b>{1}</b>".format(currentLoc/float(len(nullDist)),numGreaterThan/float(len(nullDist)))97 else:98 tooltips[sam][signame] = "<b>{0}</b>".format(numGreaterThan/float(len(nullDist)))99 if 'sig' in version:100 #replace the value itself101 samSigVal[sam][sig] = numGreaterThan/float(len(nullDist))102 if 'null' not in version or 'sig' in version:103 tooltips = None104 util.writeRegularOutput(samSigVal,compOutput,abbrevsDict)105 util.writeDetailedOutput(sigGenes,sams,compOutput+'.full.txt',abbrevsDict)106 return tooltips107108def writeNullModelHists(filename,sigNames,allValues,n,num_iter=100000,num_buckets=100):109 """Writes each of the histograms to a pdf110 sigNames[i] should correspond with allValues[i]"""111 nullVals = {}112 pdf = PdfPages(filename) #version-dependent, but this seems to work for all versions113 for i in range(len(allValues)):114 sigVals = nullmodel.getStatistics(allValues[i],n,pdf,sigNames[i],num_iter,num_buckets)115 nullVals[sigNames[i]] = sigVals116 pdf.close()117 return nullVals118119def writeInputFileHist(filename,sigNames,allValues,num_buckets=100):120 """Writes the distribution of each input signature121 sigNames is the names of the signatures, allValues is the set of values in the signature122 sigNames[i] corresponds to allValues[i]"""123 pdf = PdfPages(filename)124 for i in range(len(allValues)):125 matplotlib.pyplot.hist(allValues[i],num_buckets)126 matplotlib.pyplot.title(sigNames[i])127 pdf.savefig()128 matplotlib.pyplot.close()129 pdf.close()130131def writeNull(sams,nullFilename,n,numIter,genes=None):132 """Writes the null distribution of inputFile to nullFilename133 n is the number of genes we're averaging over134 numIter is the number of iterations135 genes is the set of genes which we're considering136 returns a dictionary of sample:values simulated137 """138 names = [sam for sam in sorted(sams.keys())]139 if not genes:140 allVals = [[sams[sam][gene] for gene in sams[sam]] for sam in names]141 else:142 allVals = []143 for sam in sams:144 genevals = []145 for gene in genes:146 if gene in sams[sam]:147 genevals.append(sams[sam][gene])148 allVals.append(genevals)149 return writeNullModelHists(nullFilename,names,allVals,n,numIter)150151def writeInDist(sams,inDistFilename):152 """Writes the distribution of the input samples to inDistFilename"""153 names = [sam for sam in sorted(sams.keys())]154 allVals = [[sams[sam][gene] for gene in sams[sam]] for sam in names]155 writeInputFileHist(inDistFilename,names,allVals)156157def generateHeatmap(inputFile,sigfile,abbrevs,n,version,zTransform,jobID,rowMetric,colMetric,invert,\158 computeNull,isClient,nullIterations,mn,mx,av,color,fileName):159 """Main function which generates the heatmap160 inputFile - user-provided input161 sigfile - file with most important genes for each signature162 format: sigName\tgene1\tgene2...\nsigName2\t...163 abbrevs - file with abbreviations\tfull names164 n - number of genes to take from each signature165 version - what computation to do. looks for certain strings166 "avg" - use average values, or "delta" - difference from average167 "log" - log-transform values, or nothing168 "rank" - rank rather than raw values, or nothing169 zTransform - "matrix", "column", "row" if going to be transformed by those metrics, otherwise "none"170 jobID - ID of the current job, used for identifying and writing to file171 rowMetric, colMetric - "euclidean", "pearson", "none", used to cluster columns/rows172 invert - invert the axes of the heatmap173 computeNull - compute the null distribution (only works for rank average, value, log)174 isClient - always false when run on server (debug option)175 nullIterations - number of iterations to run the simulation to find null distribution176 mn - min value in the heatmap color range (or None to automatically scale)177 mx - max value in the heatmap color range (or None)178 av - whether to average the values (if True) or report the sum (if False)179 color - which color axis to use180 'bwr' goes blue->white->red181 'wr' goes white->red182 'rw' goes red->white183 None sets it automatically depending on what computation options were selected184 """185 #checks for errors and corrects whatever errors it can186 util.reformatFile(inputFile)187 util.checkForErrors(inputFile)188189 #set up constants190 C = iSigDBConstants.Constants(jobID)191192 #get sample values193 sams = util.readMatrix(inputFile,ordered=True)194195 if 'rank' in version:196 sams = ranked(sams)197 if 'log' in version:198 sams = logall(sams)199 if 'delta' in version:200 sams = delta(sams)201202 #computing sample distribution203 writeInDist(sams,C.INPUT_DIST_PDF)204205 #get signatures206 abbrevsDict = util.loadAbbrevs(abbrevs)207 sigGenes = getSigGenes(sigfile,abbrevsDict.keys(),n) 208209 #computing null distribution210 if computeNull:211 allSigGenes = set()212 for sig in sigGenes:213 allSigGenes = allSigGenes.union(set(sigGenes[sig]))214 nullVals = writeNull(sams,C.NULL_PDF,n,nullIterations,allSigGenes)215 else:216 nullVals = None217 tooltips = writeValues(sams,sigGenes,C.COMP_OUTPUT,version,abbrevsDict,av,nullVals)218 optionsUsed = util.getOptionsUsed(version,n,zTransform,rowMetric,colMetric,fileName)219 util.createHeatmap(C,version,zTransform,rowMetric,colMetric,invert,isClient,computeNull,True,True,mn,mx,tooltips,color,optionsUsed)220 221#----------------------------------------------------------------------------222# main function call223#----------------------------------------------------------------------------224if __name__ == "__main__":225 parser = OptionParser()226 parser.add_option("--input", dest="input", help="user-provided input")227 parser.add_option("--sigfile",dest="sigfile",help="path to precomputed signatures")228 parser.add_option("--abbrev", dest="abbrev", help="path to abbrevs file")229 parser.add_option("--n",dest='n', help="number of genes to use")230 parser.add_option("--version", dest="version", help="metric version with avg, log, rank, delta ")231 parser.add_option("--zTransform", dest="zTransform", help="how to transform matrix")232 parser.add_option("--row_metric", dest="row_metric", help="metric for clustering rows (samples)")233 parser.add_option("--col_metric", dest="col_metric", help="metric for clustering columns (signatures)")234 parser.add_option("--invert",default=False,dest="invert",action="store_true",help="heatmap columns/rows swtiched")235 parser.add_option("--null",default=False,action="store_true", dest="null", help="compute null model")236 parser.add_option("--nullIter",dest="nullIterations",help="how many iterations to use in null model")237 parser.add_option("--range",dest="range",help="range of output")238 parser.add_option("--color",dest="color",help="color axis")239 (options, args) = parser.parse_args()240 mn,mx = options.range.split(',') if options.range != "None" else (None,None)241 color = options.color if options.color != 'None' else None ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful