Index: lnt/trunk/lnt/external/stats/pstat.py =================================================================== --- lnt/trunk/lnt/external/stats/pstat.py +++ lnt/trunk/lnt/external/stats/pstat.py @@ -131,10 +131,10 @@ 'left', lists in attached consecutively on the 'right' """ - if type(source) not in [ListType,TupleType]: + if not isinstance(source, (list, tuple)): source = [source] for addon in args: - if type(addon) not in [ListType,TupleType]: + if not isinstance(addon, (list, tuple)): addon = [addon] if len(addon) < len(source): # is source list longer? if len(source) % len(addon) == 0: # are they integer multiples? @@ -177,21 +177,21 @@ Returns: a list of lists as long as source, with source on the 'left' and addon on the 'right' """ - if type(source) not in [ListType,TupleType]: + if not isinstance(source, (list, tuple)): source = [source] - if type(addon) not in [ListType,TupleType]: + if not isinstance(addon, (list, tuple)): addon = [addon] minlen = min(len(source),len(addon)) list = copy.deepcopy(source) # start abut process - if type(source[0]) not in [ListType,TupleType]: - if type(addon[0]) not in [ListType,TupleType]: + if not isinstance(source[0], (list, tuple)): + if not isinstance(addon[0], (list, tuple)): for i in range(minlen): list[i] = [source[i]] + [addon[i]] # source/addon = column else: for i in range(minlen): list[i] = [source[i]] + addon[i] # addon=list-of-lists else: - if type(addon[0]) not in [ListType,TupleType]: + if not isinstance(addon[0], (list, tuple)): for i in range(minlen): list[i] = source[i] + [addon[i]] # source=list-of-lists else: @@ -214,13 +214,13 @@ """ global index column = 0 - if type(cnums) in [ListType,TupleType]: # if multiple columns to get + if isinstance(cnums, (list, tuple)): # if multiple columns to get index = cnums[0] column = map(lambda x: x[index], listoflists) for col in cnums[1:]: index = col column = abut(column,map(lambda x: x[index], listoflists)) - elif type(cnums) == StringType: # if an 'x[3:]' type expr. + elif isinstance(cnums, str): # if an 'x[3:]' type expr. evalstring = 'map(lambda x: x'+cnums+', listoflists)' column = eval(evalstring) else: # else it's just 1 col to get @@ -252,9 +252,9 @@ s = s + item return s/float(len(inlist)) - if type(keepcols) not in [ListType,TupleType]: + if not isinstance(keepcols, (list, tuple)): keepcols = [keepcols] - if type(collapsecols) not in [ListType,TupleType]: + if not isinstance(collapsecols, (list, tuple)): collapsecols = [collapsecols] if cfcn == None: cfcn = collmean @@ -284,9 +284,9 @@ uniques = unique(values) uniques.sort() newlist = [] - if type(keepcols) not in [ListType,TupleType]: keepcols = [keepcols] + if not isinstance(keepcols, (list, tuple)): keepcols = [keepcols] for item in uniques: - if type(item) not in [ListType,TupleType]: item =[item] + if not isinstance(item, (list, tuple)): item =[item] tmprows = linexand(listoflists,keepcols,item) for col in collapsecols: avgcol = colex(tmprows,col) @@ -345,13 +345,13 @@ Usage: linexand (listoflists,columnlist,valuelist) Returns: the rows of listoflists where columnlist[i]=valuelist[i] for ALL i """ - if type(columnlist) not in [ListType,TupleType]: + if not isinstance(columnlist, (list, tuple)): columnlist = [columnlist] - if type(valuelist) not in [ListType,TupleType]: + if not isinstance(valuelist, (list, tuple)): valuelist = [valuelist] criterion = '' for i in range(len(columnlist)): - if type(valuelist[i])==StringType: + if isinstance(valuelist[i], str): critval = '\'' + valuelist[i] + '\'' else: critval = str(valuelist[i]) @@ -373,15 +373,15 @@ Usage: linexor (listoflists,columnlist,valuelist) Returns: the rows of listoflists where columnlist[i]=valuelist[i] for ANY i """ - if type(columnlist) not in [ListType,TupleType]: + if not isinstance(columnlist, (list, tuple)): columnlist = [columnlist] - if type(valuelist) not in [ListType,TupleType]: + if not isinstance(valuelist, (list, tuple)): valuelist = [valuelist] criterion = '' if len(columnlist) == 1 and len(valuelist) > 1: columnlist = columnlist*len(valuelist) for i in range(len(columnlist)): # build an exec string - if type(valuelist[i])==StringType: + if isinstance(valuelist[i], str): critval = '\'' + valuelist[i] + '\'' else: critval = str(valuelist[i]) @@ -402,7 +402,7 @@ """ outstr = '' for item in inlist: - if type(item) <> StringType: + if not isinstance(item, str): item = str(item) outstr = outstr + item + delimiter outstr = outstr[0:-1] @@ -418,7 +418,7 @@ """ outstr = '' for item in inlist: - if type(item) <> StringType: + if not isinstance(item, str): item = str(item) size = len(item) if size <= colsize: @@ -442,7 +442,7 @@ """ outstr = '' for i in range(len(inlist)): - if type(inlist[i]) <> StringType: + if not isinstance(inlist[i], str): item = str(inlist[i]) else: item = inlist[i] @@ -483,7 +483,7 @@ def makestr (x): - if type(x) <> StringType: + if not isinstance(x, str): x = str(x) return x @@ -498,7 +498,7 @@ Usage: printcc (lst,extra=2) Returns: None """ - if type(lst[0]) not in [ListType,TupleType]: + if not isinstance(lst[0], (list, tuple)): lst = [lst] rowstokill = [] list2print = copy.deepcopy(lst) @@ -568,7 +568,7 @@ """ lst = inlst*1 for i in range(len(lst)): - if type(lst[i]) not in [ListType,TupleType]: + if not isinstance(lst[i], (list, tuple)): if lst[i]==oldval: lst[i]=newval else: lst[i] = replace(lst[i],oldval,newval) @@ -586,7 +586,7 @@ """ lst = copy.deepcopy(inlist) if cols != None: - if type(cols) not in [ListType,TupleType]: + if not isinstance(cols, (list, tuple)): cols = [cols] for col in cols: for row in range(len(lst)): @@ -623,17 +623,17 @@ def roundlist (inlist,digits): """ Goes through each element in a 1D or 2D inlist, and applies the following -function to all elements of FloatType ... round(element,digits). +function to all elements of float ... round(element,digits). Usage: roundlist(inlist,digits) Returns: list with rounded floats """ - if type(inlist[0]) in [IntType, FloatType]: + if isinstance(inlist[0], (int, float)): inlist = [inlist] l = inlist*1 for i in range(len(l)): for j in range(len(l[i])): - if type(l[i][j])==FloatType: + if isinstance(l[i][j], float): l[i][j] = round(l[i][j],digits) return l @@ -756,7 +756,7 @@ Usage: acolex (a,indices,axis=1) Returns: the columns of a specified by indices """ - if type(indices) not in [ListType,TupleType,N.ndarray]: + if not isinstance(indices, (list, tuple, N.ndarray)): indices = [indices] if len(N.shape(a)) == 1: cols = N.resize(a,[a.shape[0],1]) @@ -780,9 +780,9 @@ def acollmean (inarray): return N.sum(N.ravel(inarray)) - if type(keepcols) not in [ListType,TupleType,N.ndarray]: + if not isinstance(keepcols, (list, tuple, N.ndarray)): keepcols = [keepcols] - if type(collapsecols) not in [ListType,TupleType,N.ndarray]: + if not isinstance(collapsecols, (list, tuple, N.ndarray)): collapsecols = [collapsecols] if cfcn == None: @@ -804,14 +804,14 @@ means = aabut(means,test) return means else: - if type(keepcols) not in [ListType,TupleType,N.ndarray]: + if not isinstance(keepcols, (list, tuple, N.ndarray)): keepcols = [keepcols] values = colex(a,keepcols) # so that "item" can be appended (below) uniques = unique(values) # get a LIST, so .sort keeps rows intact uniques.sort() newlist = [] for item in uniques: - if type(item) not in [ListType,TupleType,N.ndarray]: + if not isinstance(item, (list, tuple, N.ndarray)): item =[item] tmprows = alinexand(a,keepcols,item) for col in collapsecols: @@ -854,7 +854,7 @@ def isstring(x): - if type(x)==StringType: + if isinstance(x, str): return 1 else: return 0 @@ -868,13 +868,13 @@ Usage: alinexand (a,columnlist,valuelist) Returns: the rows of a where columnlist[i]=valuelist[i] for ALL i """ - if type(columnlist) not in [ListType,TupleType,N.ndarray]: + if not isinstance(columnlist, (list, tuple, N.ndarray)): columnlist = [columnlist] - if type(valuelist) not in [ListType,TupleType,N.ndarray]: + if not isinstance(valuelist, (list, tuple, N.ndarray)): valuelist = [valuelist] criterion = '' for i in range(len(columnlist)): - if type(valuelist[i])==StringType: + if isinstance(valuelist[i], str): critval = '\'' + valuelist[i] + '\'' else: critval = str(valuelist[i]) @@ -894,9 +894,9 @@ Usage: alinexor (a,columnlist,valuelist) Returns: the rows of a where columnlist[i]=valuelist[i] for ANY i """ - if type(columnlist) not in [ListType,TupleType,N.ndarray]: + if not isinstance(columnlist, (list, tuple, N.ndarray)): columnlist = [columnlist] - if type(valuelist) not in [ListType,TupleType,N.ndarray]: + if not isinstance(valuelist, (list, tuple, N.ndarray)): valuelist = [valuelist] criterion = '' if len(columnlist) == 1 and len(valuelist) > 1: @@ -904,7 +904,7 @@ elif len(valuelist) == 1 and len(columnlist) > 1: valuelist = valuelist*len(columnlist) for i in range(len(columnlist)): - if type(valuelist[i])==StringType: + if isinstance(valuelist[i], str): critval = '\'' + valuelist[i] + '\'' else: critval = str(valuelist[i]) @@ -939,7 +939,7 @@ work = acolex(a,col) work = work.ravel() for pair in listmap: - if type(pair[1]) == StringType or work.dtype.char=='O' or a.dtype.char=='O': + if isinstance(pair[1], str) or work.dtype.char == 'O' or a.dtype.char == 'O': work = N.array(work,dtype='O') a = N.array(a,dtype='O') for i in range(len(work)): Index: lnt/trunk/lnt/external/stats/stats.py =================================================================== --- lnt/trunk/lnt/external/stats/stats.py +++ lnt/trunk/lnt/external/stats/stats.py @@ -246,13 +246,12 @@ self._dispatch = {} for func, types in tuples: for t in types: - if t in self._dispatch.keys(): + if t in self._dispatch: raise ValueError, "can't have two dispatches on "+str(t) self._dispatch[t] = func - self._types = self._dispatch.keys() def __call__(self, arg1, *args, **kw): - if type(arg1) not in self._types: + if type(arg1) not in self._dispatch: raise TypeError, "don't know how to dispatch %s arguments" % type(arg1) return apply(self._dispatch[type(arg1)], (arg1,) + args, kw) @@ -513,7 +512,7 @@ Returns: list of bin values, lowerreallimit, binsize, extrapoints """ if (defaultreallimits <> None): - if type(defaultreallimits) not in [ListType,TupleType] or len(defaultreallimits)==1: # only one limit given, assumed to be lower one & upper is calc'd + if not isinstance(defaultreallimits, (list, tuple)) or len(defaultreallimits) == 1: # only one limit given, assumed to be lower one & upper is calc'd lowerreallimit = defaultreallimits upperreallimit = 1.000001 * max(inlist) else: # assume both limits given @@ -1618,7 +1617,7 @@ Usage: writecc (listoflists,file,writetype='w',extra=2) Returns: None """ - if type(listoflists[0]) not in [ListType,TupleType]: + if not isinstance(listoflists[0], (list, tuple)): listoflists = [listoflists] outfile = open(file,writetype) rowstokill = [] @@ -1821,7 +1820,7 @@ title = [['Name','N','Mean','SD','Min','Max']] lofl = title+[[name1,n1,round(m1,3),round(math.sqrt(se1),3),min1,max1], [name2,n2,round(m2,3),round(math.sqrt(se2),3),min2,max2]] - if type(fname)<>StringType or len(fname)==0: + if not isinstance(fname, str) or len(fname) == 0: print() print(statname) print() @@ -1886,89 +1885,89 @@ ######################################################### ## CENTRAL TENDENCY: -geometricmean = Dispatch ( (lgeometricmean, (ListType, TupleType)), ) -harmonicmean = Dispatch ( (lharmonicmean, (ListType, TupleType)), ) -mean = Dispatch ( (lmean, (ListType, TupleType)), ) -median = Dispatch ( (lmedian, (ListType, TupleType)), ) -medianscore = Dispatch ( (lmedianscore, (ListType, TupleType)), ) -mode = Dispatch ( (lmode, (ListType, TupleType)), ) +geometricmean = Dispatch ( (lgeometricmean, (list, tuple)), ) +harmonicmean = Dispatch ( (lharmonicmean, (list, tuple)), ) +mean = Dispatch ( (lmean, (list, tuple)), ) +median = Dispatch ( (lmedian, (list, tuple)), ) +medianscore = Dispatch ( (lmedianscore, (list, tuple)), ) +mode = Dispatch ( (lmode, (list, tuple)), ) ## MOMENTS: -moment = Dispatch ( (lmoment, (ListType, TupleType)), ) -variation = Dispatch ( (lvariation, (ListType, TupleType)), ) -skew = Dispatch ( (lskew, (ListType, TupleType)), ) -kurtosis = Dispatch ( (lkurtosis, (ListType, TupleType)), ) -describe = Dispatch ( (ldescribe, (ListType, TupleType)), ) +moment = Dispatch ( (lmoment, (list, tuple)), ) +variation = Dispatch ( (lvariation, (list, tuple)), ) +skew = Dispatch ( (lskew, (list, tuple)), ) +kurtosis = Dispatch ( (lkurtosis, (list, tuple)), ) +describe = Dispatch ( (ldescribe, (list, tuple)), ) ## FREQUENCY STATISTICS: -itemfreq = Dispatch ( (litemfreq, (ListType, TupleType)), ) -scoreatpercentile = Dispatch ( (lscoreatpercentile, (ListType, TupleType)), ) -percentileofscore = Dispatch ( (lpercentileofscore, (ListType, TupleType)), ) -histogram = Dispatch ( (lhistogram, (ListType, TupleType)), ) -cumfreq = Dispatch ( (lcumfreq, (ListType, TupleType)), ) -relfreq = Dispatch ( (lrelfreq, (ListType, TupleType)), ) +itemfreq = Dispatch ( (litemfreq, (list, tuple)), ) +scoreatpercentile = Dispatch ( (lscoreatpercentile, (list, tuple)), ) +percentileofscore = Dispatch ( (lpercentileofscore, (list, tuple)), ) +histogram = Dispatch ( (lhistogram, (list, tuple)), ) +cumfreq = Dispatch ( (lcumfreq, (list, tuple)), ) +relfreq = Dispatch ( (lrelfreq, (list, tuple)), ) ## VARIABILITY: -obrientransform = Dispatch ( (lobrientransform, (ListType, TupleType)), ) -samplevar = Dispatch ( (lsamplevar, (ListType, TupleType)), ) -samplestdev = Dispatch ( (lsamplestdev, (ListType, TupleType)), ) -var = Dispatch ( (lvar, (ListType, TupleType)), ) -stdev = Dispatch ( (lstdev, (ListType, TupleType)), ) -sterr = Dispatch ( (lsterr, (ListType, TupleType)), ) -sem = Dispatch ( (lsem, (ListType, TupleType)), ) -z = Dispatch ( (lz, (ListType, TupleType)), ) -zs = Dispatch ( (lzs, (ListType, TupleType)), ) +obrientransform = Dispatch ( (lobrientransform, (list, tuple)), ) +samplevar = Dispatch ( (lsamplevar, (list, tuple)), ) +samplestdev = Dispatch ( (lsamplestdev, (list, tuple)), ) +var = Dispatch ( (lvar, (list, tuple)), ) +stdev = Dispatch ( (lstdev, (list, tuple)), ) +sterr = Dispatch ( (lsterr, (list, tuple)), ) +sem = Dispatch ( (lsem, (list, tuple)), ) +z = Dispatch ( (lz, (list, tuple)), ) +zs = Dispatch ( (lzs, (list, tuple)), ) ## TRIMMING FCNS: -trimboth = Dispatch ( (ltrimboth, (ListType, TupleType)), ) -trim1 = Dispatch ( (ltrim1, (ListType, TupleType)), ) +trimboth = Dispatch ( (ltrimboth, (list, tuple)), ) +trim1 = Dispatch ( (ltrim1, (list, tuple)), ) ## CORRELATION FCNS: -paired = Dispatch ( (lpaired, (ListType, TupleType)), ) -pearsonr = Dispatch ( (lpearsonr, (ListType, TupleType)), ) -spearmanr = Dispatch ( (lspearmanr, (ListType, TupleType)), ) -pointbiserialr = Dispatch ( (lpointbiserialr, (ListType, TupleType)), ) -kendalltau = Dispatch ( (lkendalltau, (ListType, TupleType)), ) -linregress = Dispatch ( (llinregress, (ListType, TupleType)), ) +paired = Dispatch ( (lpaired, (list, tuple)), ) +pearsonr = Dispatch ( (lpearsonr, (list, tuple)), ) +spearmanr = Dispatch ( (lspearmanr, (list, tuple)), ) +pointbiserialr = Dispatch ( (lpointbiserialr, (list, tuple)), ) +kendalltau = Dispatch ( (lkendalltau, (list, tuple)), ) +linregress = Dispatch ( (llinregress, (list, tuple)), ) ## INFERENTIAL STATS: -ttest_1samp = Dispatch ( (lttest_1samp, (ListType, TupleType)), ) -ttest_ind = Dispatch ( (lttest_ind, (ListType, TupleType)), ) -ttest_rel = Dispatch ( (lttest_rel, (ListType, TupleType)), ) -chisquare = Dispatch ( (lchisquare, (ListType, TupleType)), ) -ks_2samp = Dispatch ( (lks_2samp, (ListType, TupleType)), ) -mannwhitneyu = Dispatch ( (lmannwhitneyu, (ListType, TupleType)), ) -ranksums = Dispatch ( (lranksums, (ListType, TupleType)), ) -tiecorrect = Dispatch ( (ltiecorrect, (ListType, TupleType)), ) -wilcoxont = Dispatch ( (lwilcoxont, (ListType, TupleType)), ) -kruskalwallish = Dispatch ( (lkruskalwallish, (ListType, TupleType)), ) -friedmanchisquare = Dispatch ( (lfriedmanchisquare, (ListType, TupleType)), ) +ttest_1samp = Dispatch ( (lttest_1samp, (list, tuple)), ) +ttest_ind = Dispatch ( (lttest_ind, (list, tuple)), ) +ttest_rel = Dispatch ( (lttest_rel, (list, tuple)), ) +chisquare = Dispatch ( (lchisquare, (list, tuple)), ) +ks_2samp = Dispatch ( (lks_2samp, (list, tuple)), ) +mannwhitneyu = Dispatch ( (lmannwhitneyu, (list, tuple)), ) +ranksums = Dispatch ( (lranksums, (list, tuple)), ) +tiecorrect = Dispatch ( (ltiecorrect, (list, tuple)), ) +wilcoxont = Dispatch ( (lwilcoxont, (list, tuple)), ) +kruskalwallish = Dispatch ( (lkruskalwallish, (list, tuple)), ) +friedmanchisquare = Dispatch ( (lfriedmanchisquare, (list, tuple)), ) ## PROBABILITY CALCS: -chisqprob = Dispatch ( (lchisqprob, (IntType, FloatType)), ) -zprob = Dispatch ( (lzprob, (IntType, FloatType)), ) -ksprob = Dispatch ( (lksprob, (IntType, FloatType)), ) -fprob = Dispatch ( (lfprob, (IntType, FloatType)), ) -betacf = Dispatch ( (lbetacf, (IntType, FloatType)), ) -betai = Dispatch ( (lbetai, (IntType, FloatType)), ) -erfcc = Dispatch ( (lerfcc, (IntType, FloatType)), ) -gammln = Dispatch ( (lgammln, (IntType, FloatType)), ) +chisqprob = Dispatch ( (lchisqprob, (int, float)), ) +zprob = Dispatch ( (lzprob, (int, float)), ) +ksprob = Dispatch ( (lksprob, (int, float)), ) +fprob = Dispatch ( (lfprob, (int, float)), ) +betacf = Dispatch ( (lbetacf, (int, float)), ) +betai = Dispatch ( (lbetai, (int, float)), ) +erfcc = Dispatch ( (lerfcc, (int, float)), ) +gammln = Dispatch ( (lgammln, (int, float)), ) ## ANOVA FUNCTIONS: -F_oneway = Dispatch ( (lF_oneway, (ListType, TupleType)), ) -F_value = Dispatch ( (lF_value, (ListType, TupleType)), ) +F_oneway = Dispatch ( (lF_oneway, (list, tuple)), ) +F_value = Dispatch ( (lF_value, (list, tuple)), ) ## SUPPORT FUNCTIONS: -incr = Dispatch ( (lincr, (ListType, TupleType)), ) -sum = Dispatch ( (lsum, (ListType, TupleType)), ) -cumsum = Dispatch ( (lcumsum, (ListType, TupleType)), ) -ss = Dispatch ( (lss, (ListType, TupleType)), ) -summult = Dispatch ( (lsummult, (ListType, TupleType)), ) -square_of_sums = Dispatch ( (lsquare_of_sums, (ListType, TupleType)), ) -sumdiffsquared = Dispatch ( (lsumdiffsquared, (ListType, TupleType)), ) -shellsort = Dispatch ( (lshellsort, (ListType, TupleType)), ) -rankdata = Dispatch ( (lrankdata, (ListType, TupleType)), ) -findwithin = Dispatch ( (lfindwithin, (ListType, TupleType)), ) +incr = Dispatch ( (lincr, (list, tuple)), ) +sum = Dispatch ( (lsum, (list, tuple)), ) +cumsum = Dispatch ( (lcumsum, (list, tuple)), ) +ss = Dispatch ( (lss, (list, tuple)), ) +summult = Dispatch ( (lsummult, (list, tuple)), ) +square_of_sums = Dispatch ( (lsquare_of_sums, (list, tuple)), ) +sumdiffsquared = Dispatch ( (lsumdiffsquared, (list, tuple)), ) +shellsort = Dispatch ( (lshellsort, (list, tuple)), ) +rankdata = Dispatch ( (lrankdata, (list, tuple)), ) +findwithin = Dispatch ( (lfindwithin, (list, tuple)), ) #============= THE ARRAY-VERSION OF THE STATS FUNCTIONS =============== @@ -2019,7 +2018,7 @@ size = len(inarray) mult = N.power(inarray,1.0/size) mult = N.multiply.reduce(mult) - elif type(dimension) in [IntType,FloatType]: + elif isinstance(dimension, (int, float)): size = inarray.shape[dimension] mult = N.power(inarray,1.0/size) mult = N.multiply.reduce(mult,dimension) @@ -2061,7 +2060,7 @@ inarray = N.ravel(inarray) size = len(inarray) s = N.add.reduce(1.0 / inarray) - elif type(dimension) in [IntType,FloatType]: + elif isinstance(dimension, (int, float)): size = float(inarray.shape[dimension]) s = N.add.reduce(1.0/inarray, dimension) if keepdims == 1: @@ -2116,7 +2115,7 @@ inarray = N.ravel(inarray) sum = N.add.reduce(inarray) denom = float(len(inarray)) - elif type(dimension) in [IntType,FloatType]: + elif isinstance(dimension, (int, float)): sum = asum(inarray,dimension) denom = float(inarray.shape[dimension]) if keepdims == 1: @@ -2230,7 +2229,7 @@ a = a.astype(N.float_) if limits == None: return mean(a) - assert type(limits) in [ListType,TupleType,N.ndarray], "Wrong type for limits in atmean" + assert isinstance(limits, (list, tuple, N.ndarray)), "Wrong type for limits in atmean" if inclusive[0]: lowerfcn = N.greater_equal else: lowerfcn = N.greater if inclusive[1]: upperfcn = N.less_equal @@ -2262,7 +2261,7 @@ a = a.astype(N.float_) if limits == None or limits == [None,None]: return avar(a) - assert type(limits) in [ListType,TupleType,N.ndarray], "Wrong type for limits in atvar" + assert isinstance(limits, (list, tuple, N.ndarray)), "Wrong type for limits in atvar" if inclusive[0]: lowerfcn = N.greater_equal else: lowerfcn = N.greater if inclusive[1]: upperfcn = N.less_equal @@ -2348,7 +2347,7 @@ if limits == None or limits == [None,None]: n = float(len(N.ravel(a))) limits = [min(a)-1, max(a)+1] - assert type(limits) in [ListType,TupleType,N.ndarray], "Wrong type for limits in atsem" + assert isinstance(limits, (list, tuple, N.ndarray)), "Wrong type for limits in atsem" if inclusive[0]: lowerfcn = N.greater_equal else: lowerfcn = N.greater if inclusive[1]: upperfcn = N.less_equal @@ -2417,7 +2416,7 @@ """ denom = N.power(amoment(a,2,dimension),1.5) zero = N.equal(denom,0) - if type(denom) == N.ndarray and asum(zero) <> 0: + if isinstance(denom, N.ndarray) and asum(zero) <> 0: print("Number of zeros in askew: ", asum(zero)) denom = denom + zero # prevent divide-by-zero return N.where(zero, 0, amoment(a,3,dimension)/denom) @@ -2436,7 +2435,7 @@ """ denom = N.power(amoment(a,2,dimension),2) zero = N.equal(denom,0) - if type(denom) == N.ndarray and asum(zero) <> 0: + if isinstance(denom, N.ndarray) and asum(zero) <> 0: print("Number of zeros in akurtosis: ", asum(zero)) denom = denom + zero # prevent divide-by-zero return N.where(zero,0,amoment(a,4,dimension)/denom) @@ -2722,7 +2721,7 @@ else: mn = amean(inarray,dimension,keepdims=1) deviations = inarray - mn - if type(dimension) == ListType: + if isinstance(dimension, list): n = 1 for d in dimension: n = n*inarray.shape[d] @@ -2778,7 +2777,7 @@ xdeviations = x - xmn ymn = amean(y,dimension,1) # keepdims ydeviations = y - ymn - if type(dimension) == ListType: + if isinstance(dimension, list): n = 1 for d in dimension: n = n*x.shape[d] @@ -2803,7 +2802,7 @@ dimension = 0 mn = amean(inarray,dimension,1) deviations = inarray - mn - if type(dimension) == ListType: + if isinstance(dimension, list): n = 1 for d in dimension: n = n*inarray.shape[d] @@ -2855,7 +2854,7 @@ if dimension == None: inarray = N.ravel(inarray) dimension = 0 - if type(dimension) == ListType: + if isinstance(dimension, list): n = 1 for d in dimension: n = n*inarray.shape[d] @@ -3319,7 +3318,7 @@ Usage: attest_1samp(a,popmean,Name='Sample',printit=0,writemode='a') Returns: t-value, two-tailed prob """ - if type(a) != N.ndarray: + if not isinstance(a, N.ndarray): a = N.array(a) x = amean(a) v = avar(a) @@ -3370,15 +3369,15 @@ t = N.where(zerodivproblem,1.0,t) # replace NaN/wrong t-values with 1.0 probs = abetai(0.5*df,0.5,float(df)/(df+t*t)) - if type(t) == N.ndarray: + if isinstance(t, N.ndarray): probs = N.reshape(probs,t.shape) if probs.shape == (1,): probs = probs[0] if printit <> 0: - if type(t) == N.ndarray: + if isinstance(t, N.ndarray): t = t[0] - if type(probs) == N.ndarray: + if isinstance(probs, N.ndarray): probs = probs[0] statname = 'Independent samples T-test.' outputpairedstats(printit,writemode, @@ -3453,7 +3452,7 @@ t = N.add.reduce(d,dimension) / denom # N-D COMPUTATION HERE!!!!!! t = N.where(zerodivproblem,1.0,t) # replace NaN/wrong t-values with 1.0 probs = abetai(0.5*df,0.5,float(df)/(df+t*t)) - if type(t) == N.ndarray: + if isinstance(t, N.ndarray): probs = N.reshape(probs,t.shape) if probs.shape == (1,): probs = probs[0] @@ -3714,7 +3713,7 @@ exponents = N.where(N.less(x,-BIG),-BIG,x) return N.exp(exponents) - if type(chisq) == N.ndarray: + if isinstance(chisq, N.ndarray): arrayflag = 1 else: arrayflag = 0 @@ -3842,7 +3841,7 @@ Usage: aksprob(alam) """ - if type(alam) == N.ndarray: + if isinstance(alam, N.ndarray): frozen = -1 *N.ones(alam.shape,N.float64) alam = alam.astype(N.float64) arrayflag = 1 @@ -3885,7 +3884,7 @@ Usage: afprob(dfnum, dfden, F) where usually dfnum=dfbn, dfden=dfwn """ - if type(F) == N.ndarray: + if isinstance(F, N.ndarray): return abetai(0.5*dfden, 0.5*dfnum, dfden/(1.0*dfden+dfnum*F)) else: return abetai(0.5*dfden, 0.5*dfnum, dfden/float(dfden+dfnum*F)) @@ -3903,7 +3902,7 @@ EPS = 3.0e-7 arrayflag = 1 - if type(x) == N.ndarray: + if isinstance(x, N.ndarray): frozen = N.ones(x.shape,N.float_) *-1 #start out w/ -1s, should replace all else: arrayflag = 0 @@ -3978,7 +3977,7 @@ Usage: abetai(a,b,x,verbose=1) """ TINY = 1e-15 - if type(a) == N.ndarray: + if isinstance(a, N.ndarray): if asum(N.less(x,0)+N.greater(x,1)) <> 0: raise ValueError, 'Bad x in abetai' x = N.where(N.equal(x,0),TINY,x) @@ -3990,7 +3989,7 @@ # 746 (below) is the MAX POSSIBLE BEFORE OVERFLOW exponents = N.where(N.less(exponents,-740),-740,exponents) bt = N.exp(exponents) - if type(x) == N.ndarray: + if isinstance(x, N.ndarray): ans = N.where(N.less(x,(a+1)/(a+b+2.0)), bt*abetacf(a,b,x,verbose)/float(a), 1.0-bt*abetacf(b,a,1.0-x,verbose)/float(b)) @@ -4116,9 +4115,9 @@ dfF = degrees of freedom associated with the Restricted model where ER and EF are matrices from a multivariate F calculation. """ - if type(ER) in [IntType, FloatType]: + if isinstance(ER, (int, float)): ER = N.array([[ER]]) - if type(EF) in [IntType, FloatType]: + if isinstance(EF, (int, float)): EF = N.array([[EF]]) n_um = (LA.det(ER) - LA.det(EF)) / float(dfnum) d_en = LA.det(EF) / float(dfden) @@ -4135,7 +4134,7 @@ Returns: array shape of a, with -1 where a<0 and +1 where a>=0 """ a = N.asarray(a) - if ((type(a) == type(1.4)) or (type(a) == type(1))): + if isinstance(a, (int, float)): return a-a-N.less(a,0)+N.greater(a,0) else: return N.zeros(N.shape(a))-N.less(a,0)+N.greater(a,0) @@ -4154,11 +4153,11 @@ Usage: asum(a, dimension=None, keepdims=0) Returns: array summed along 'dimension'(s), same _number_ of dims if keepdims=1 """ - if type(a) == N.ndarray and a.dtype in [N.int_, N.short, N.ubyte]: + if isinstance(a, N.ndarray) and a.dtype in [N.int_, N.short, N.ubyte]: a = a.astype(N.float_) if dimension == None: s = N.sum(N.ravel(a)) - elif type(dimension) in [IntType,FloatType]: + elif isinstance(dimension, (int, float)): s = N.add.reduce(a, dimension) if keepdims == 1: shp = list(a.shape) @@ -4191,7 +4190,7 @@ if dimension == None: a = N.ravel(a) dimension = 0 - if type(dimension) in [ListType, TupleType, N.ndarray]: + if isinstance(dimension, (list, tuple, N.ndarray)): dimension = list(dimension) dimension.sort() dimension.reverse() @@ -4252,7 +4251,7 @@ inarray = N.ravel(inarray) dimension = 0 s = asum(inarray,dimension,keepdims) - if type(s) == N.ndarray: + if isinstance(s, N.ndarray): return s.astype(N.float_)*s else: return float(s)*s @@ -4350,17 +4349,17 @@ ######################################################### ## CENTRAL TENDENCY: - geometricmean = Dispatch ( (lgeometricmean, (ListType, TupleType)), + geometricmean = Dispatch ( (lgeometricmean, (list, tuple)), (ageometricmean, (N.ndarray,)) ) - harmonicmean = Dispatch ( (lharmonicmean, (ListType, TupleType)), + harmonicmean = Dispatch ( (lharmonicmean, (list, tuple)), (aharmonicmean, (N.ndarray,)) ) - mean = Dispatch ( (lmean, (ListType, TupleType)), + mean = Dispatch ( (lmean, (list, tuple)), (amean, (N.ndarray,)) ) - median = Dispatch ( (lmedian, (ListType, TupleType)), + median = Dispatch ( (lmedian, (list, tuple)), (amedian, (N.ndarray,)) ) - medianscore = Dispatch ( (lmedianscore, (ListType, TupleType)), + medianscore = Dispatch ( (lmedianscore, (list, tuple)), (amedianscore, (N.ndarray,)) ) - mode = Dispatch ( (lmode, (ListType, TupleType)), + mode = Dispatch ( (lmode, (list, tuple)), (amode, (N.ndarray,)) ) tmean = Dispatch ( (atmean, (N.ndarray,)) ) tvar = Dispatch ( (atvar, (N.ndarray,)) ) @@ -4368,151 +4367,151 @@ tsem = Dispatch ( (atsem, (N.ndarray,)) ) ## VARIATION: - moment = Dispatch ( (lmoment, (ListType, TupleType)), + moment = Dispatch ( (lmoment, (list, tuple)), (amoment, (N.ndarray,)) ) - variation = Dispatch ( (lvariation, (ListType, TupleType)), + variation = Dispatch ( (lvariation, (list, tuple)), (avariation, (N.ndarray,)) ) - skew = Dispatch ( (lskew, (ListType, TupleType)), + skew = Dispatch ( (lskew, (list, tuple)), (askew, (N.ndarray,)) ) - kurtosis = Dispatch ( (lkurtosis, (ListType, TupleType)), + kurtosis = Dispatch ( (lkurtosis, (list, tuple)), (akurtosis, (N.ndarray,)) ) - describe = Dispatch ( (ldescribe, (ListType, TupleType)), + describe = Dispatch ( (ldescribe, (list, tuple)), (adescribe, (N.ndarray,)) ) ## DISTRIBUTION TESTS - skewtest = Dispatch ( (askewtest, (ListType, TupleType)), + skewtest = Dispatch ( (askewtest, (list, tuple)), (askewtest, (N.ndarray,)) ) - kurtosistest = Dispatch ( (akurtosistest, (ListType, TupleType)), + kurtosistest = Dispatch ( (akurtosistest, (list, tuple)), (akurtosistest, (N.ndarray,)) ) - normaltest = Dispatch ( (anormaltest, (ListType, TupleType)), + normaltest = Dispatch ( (anormaltest, (list, tuple)), (anormaltest, (N.ndarray,)) ) ## FREQUENCY STATS: - itemfreq = Dispatch ( (litemfreq, (ListType, TupleType)), + itemfreq = Dispatch ( (litemfreq, (list, tuple)), (aitemfreq, (N.ndarray,)) ) - scoreatpercentile = Dispatch ( (lscoreatpercentile, (ListType, TupleType)), + scoreatpercentile = Dispatch ( (lscoreatpercentile, (list, tuple)), (ascoreatpercentile, (N.ndarray,)) ) - percentileofscore = Dispatch ( (lpercentileofscore, (ListType, TupleType)), + percentileofscore = Dispatch ( (lpercentileofscore, (list, tuple)), (apercentileofscore, (N.ndarray,)) ) - histogram = Dispatch ( (lhistogram, (ListType, TupleType)), + histogram = Dispatch ( (lhistogram, (list, tuple)), (ahistogram, (N.ndarray,)) ) - cumfreq = Dispatch ( (lcumfreq, (ListType, TupleType)), + cumfreq = Dispatch ( (lcumfreq, (list, tuple)), (acumfreq, (N.ndarray,)) ) - relfreq = Dispatch ( (lrelfreq, (ListType, TupleType)), + relfreq = Dispatch ( (lrelfreq, (list, tuple)), (arelfreq, (N.ndarray,)) ) ## VARIABILITY: - obrientransform = Dispatch ( (lobrientransform, (ListType, TupleType)), + obrientransform = Dispatch ( (lobrientransform, (list, tuple)), (aobrientransform, (N.ndarray,)) ) - samplevar = Dispatch ( (lsamplevar, (ListType, TupleType)), + samplevar = Dispatch ( (lsamplevar, (list, tuple)), (asamplevar, (N.ndarray,)) ) - samplestdev = Dispatch ( (lsamplestdev, (ListType, TupleType)), + samplestdev = Dispatch ( (lsamplestdev, (list, tuple)), (asamplestdev, (N.ndarray,)) ) signaltonoise = Dispatch( (asignaltonoise, (N.ndarray,)),) - var = Dispatch ( (lvar, (ListType, TupleType)), + var = Dispatch ( (lvar, (list, tuple)), (avar, (N.ndarray,)) ) - stdev = Dispatch ( (lstdev, (ListType, TupleType)), + stdev = Dispatch ( (lstdev, (list, tuple)), (astdev, (N.ndarray,)) ) - sterr = Dispatch ( (lsterr, (ListType, TupleType)), + sterr = Dispatch ( (lsterr, (list, tuple)), (asterr, (N.ndarray,)) ) - sem = Dispatch ( (lsem, (ListType, TupleType)), + sem = Dispatch ( (lsem, (list, tuple)), (asem, (N.ndarray,)) ) - z = Dispatch ( (lz, (ListType, TupleType)), + z = Dispatch ( (lz, (list, tuple)), (az, (N.ndarray,)) ) - zs = Dispatch ( (lzs, (ListType, TupleType)), + zs = Dispatch ( (lzs, (list, tuple)), (azs, (N.ndarray,)) ) ## TRIMMING FCNS: threshold = Dispatch( (athreshold, (N.ndarray,)),) - trimboth = Dispatch ( (ltrimboth, (ListType, TupleType)), + trimboth = Dispatch ( (ltrimboth, (list, tuple)), (atrimboth, (N.ndarray,)) ) - trim1 = Dispatch ( (ltrim1, (ListType, TupleType)), + trim1 = Dispatch ( (ltrim1, (list, tuple)), (atrim1, (N.ndarray,)) ) ## CORRELATION FCNS: - paired = Dispatch ( (lpaired, (ListType, TupleType)), + paired = Dispatch ( (lpaired, (list, tuple)), (apaired, (N.ndarray,)) ) - lincc = Dispatch ( (llincc, (ListType, TupleType)), + lincc = Dispatch ( (llincc, (list, tuple)), (alincc, (N.ndarray,)) ) - pearsonr = Dispatch ( (lpearsonr, (ListType, TupleType)), + pearsonr = Dispatch ( (lpearsonr, (list, tuple)), (apearsonr, (N.ndarray,)) ) - spearmanr = Dispatch ( (lspearmanr, (ListType, TupleType)), + spearmanr = Dispatch ( (lspearmanr, (list, tuple)), (aspearmanr, (N.ndarray,)) ) - pointbiserialr = Dispatch ( (lpointbiserialr, (ListType, TupleType)), + pointbiserialr = Dispatch ( (lpointbiserialr, (list, tuple)), (apointbiserialr, (N.ndarray,)) ) - kendalltau = Dispatch ( (lkendalltau, (ListType, TupleType)), + kendalltau = Dispatch ( (lkendalltau, (list, tuple)), (akendalltau, (N.ndarray,)) ) - linregress = Dispatch ( (llinregress, (ListType, TupleType)), + linregress = Dispatch ( (llinregress, (list, tuple)), (alinregress, (N.ndarray,)) ) ## INFERENTIAL STATS: - ttest_1samp = Dispatch ( (lttest_1samp, (ListType, TupleType)), + ttest_1samp = Dispatch ( (lttest_1samp, (list, tuple)), (attest_1samp, (N.ndarray,)) ) - ttest_ind = Dispatch ( (lttest_ind, (ListType, TupleType)), + ttest_ind = Dispatch ( (lttest_ind, (list, tuple)), (attest_ind, (N.ndarray,)) ) - ttest_rel = Dispatch ( (lttest_rel, (ListType, TupleType)), + ttest_rel = Dispatch ( (lttest_rel, (list, tuple)), (attest_rel, (N.ndarray,)) ) - chisquare = Dispatch ( (lchisquare, (ListType, TupleType)), + chisquare = Dispatch ( (lchisquare, (list, tuple)), (achisquare, (N.ndarray,)) ) - ks_2samp = Dispatch ( (lks_2samp, (ListType, TupleType)), + ks_2samp = Dispatch ( (lks_2samp, (list, tuple)), (aks_2samp, (N.ndarray,)) ) - mannwhitneyu = Dispatch ( (lmannwhitneyu, (ListType, TupleType)), + mannwhitneyu = Dispatch ( (lmannwhitneyu, (list, tuple)), (amannwhitneyu, (N.ndarray,)) ) - tiecorrect = Dispatch ( (ltiecorrect, (ListType, TupleType)), + tiecorrect = Dispatch ( (ltiecorrect, (list, tuple)), (atiecorrect, (N.ndarray,)) ) - ranksums = Dispatch ( (lranksums, (ListType, TupleType)), + ranksums = Dispatch ( (lranksums, (list, tuple)), (aranksums, (N.ndarray,)) ) - wilcoxont = Dispatch ( (lwilcoxont, (ListType, TupleType)), + wilcoxont = Dispatch ( (lwilcoxont, (list, tuple)), (awilcoxont, (N.ndarray,)) ) - kruskalwallish = Dispatch ( (lkruskalwallish, (ListType, TupleType)), + kruskalwallish = Dispatch ( (lkruskalwallish, (list, tuple)), (akruskalwallish, (N.ndarray,)) ) - friedmanchisquare = Dispatch ( (lfriedmanchisquare, (ListType, TupleType)), + friedmanchisquare = Dispatch ( (lfriedmanchisquare, (list, tuple)), (afriedmanchisquare, (N.ndarray,)) ) ## PROBABILITY CALCS: - chisqprob = Dispatch ( (lchisqprob, (IntType, FloatType)), + chisqprob = Dispatch ( (lchisqprob, (int, float)), (achisqprob, (N.ndarray,)) ) - zprob = Dispatch ( (lzprob, (IntType, FloatType)), + zprob = Dispatch ( (lzprob, (int, float)), (azprob, (N.ndarray,)) ) - ksprob = Dispatch ( (lksprob, (IntType, FloatType)), + ksprob = Dispatch ( (lksprob, (int, float)), (aksprob, (N.ndarray,)) ) - fprob = Dispatch ( (lfprob, (IntType, FloatType)), + fprob = Dispatch ( (lfprob, (int, float)), (afprob, (N.ndarray,)) ) - betacf = Dispatch ( (lbetacf, (IntType, FloatType)), + betacf = Dispatch ( (lbetacf, (int, float)), (abetacf, (N.ndarray,)) ) - betai = Dispatch ( (lbetai, (IntType, FloatType)), + betai = Dispatch ( (lbetai, (int, float)), (abetai, (N.ndarray,)) ) - erfcc = Dispatch ( (lerfcc, (IntType, FloatType)), + erfcc = Dispatch ( (lerfcc, (int, float)), (aerfcc, (N.ndarray,)) ) - gammln = Dispatch ( (lgammln, (IntType, FloatType)), + gammln = Dispatch ( (lgammln, (int, float)), (agammln, (N.ndarray,)) ) ## ANOVA FUNCTIONS: - F_oneway = Dispatch ( (lF_oneway, (ListType, TupleType)), + F_oneway = Dispatch ( (lF_oneway, (list, tuple)), (aF_oneway, (N.ndarray,)) ) - F_value = Dispatch ( (lF_value, (ListType, TupleType)), + F_value = Dispatch ( (lF_value, (list, tuple)), (aF_value, (N.ndarray,)) ) ## SUPPORT FUNCTIONS: - incr = Dispatch ( (lincr, (ListType, TupleType, N.ndarray)), ) - sum = Dispatch ( (lsum, (ListType, TupleType)), + incr = Dispatch ( (lincr, (list, tuple, N.ndarray)), ) + sum = Dispatch ( (lsum, (list, tuple)), (asum, (N.ndarray,)) ) - cumsum = Dispatch ( (lcumsum, (ListType, TupleType)), + cumsum = Dispatch ( (lcumsum, (list, tuple)), (acumsum, (N.ndarray,)) ) - ss = Dispatch ( (lss, (ListType, TupleType)), + ss = Dispatch ( (lss, (list, tuple)), (ass, (N.ndarray,)) ) - summult = Dispatch ( (lsummult, (ListType, TupleType)), + summult = Dispatch ( (lsummult, (list, tuple)), (asummult, (N.ndarray,)) ) - square_of_sums = Dispatch ( (lsquare_of_sums, (ListType, TupleType)), + square_of_sums = Dispatch ( (lsquare_of_sums, (list, tuple)), (asquare_of_sums, (N.ndarray,)) ) - sumdiffsquared = Dispatch ( (lsumdiffsquared, (ListType, TupleType)), + sumdiffsquared = Dispatch ( (lsumdiffsquared, (list, tuple)), (asumdiffsquared, (N.ndarray,)) ) - shellsort = Dispatch ( (lshellsort, (ListType, TupleType)), + shellsort = Dispatch ( (lshellsort, (list, tuple)), (ashellsort, (N.ndarray,)) ) - rankdata = Dispatch ( (lrankdata, (ListType, TupleType)), + rankdata = Dispatch ( (lrankdata, (list, tuple)), (arankdata, (N.ndarray,)) ) - findwithin = Dispatch ( (lfindwithin, (ListType, TupleType)), + findwithin = Dispatch ( (lfindwithin, (list, tuple)), (afindwithin, (N.ndarray,)) ) ###################### END OF NUMERIC FUNCTION BLOCK ##################### Index: lnt/trunk/lnt/server/db/regression.py =================================================================== --- lnt/trunk/lnt/server/db/regression.py +++ lnt/trunk/lnt/server/db/regression.py @@ -47,7 +47,7 @@ session.add(regression) new_ris = [] for fc_id in field_changes: - if type(fc_id) == int: + if isinstance(fc_id, int): fc = get_fieldchange(session, ts, fc_id) else: fc = fc_id Index: lnt/trunk/lnt/server/ui/api.py =================================================================== --- lnt/trunk/lnt/server/ui/api.py +++ lnt/trunk/lnt/server/ui/api.py @@ -30,10 +30,10 @@ def with_ts(obj): """For Url type fields to work, the objects we return must have a test-suite and database attribute set, the function attempts to set them.""" - if type(obj) == list: + if isinstance(obj, list): # For lists, set them on all elements. return [with_ts(x) for x in obj] - if type(obj) == dict: + if isinstance(obj, dict): # If already a dict, just add the fields. new_obj = obj else: Index: lnt/trunk/lnt/server/ui/app.py =================================================================== --- lnt/trunk/lnt/server/ui/app.py +++ lnt/trunk/lnt/server/ui/app.py @@ -57,7 +57,7 @@ def default(self, obj): if hasattr(obj, '__json__'): return obj.__json__() - if type(obj) is datetime.datetime: + if isinstance(obj, datetime.datetime): return obj.isoformat() if isinstance(obj.__class__, DeclarativeMeta): fields = {} Index: lnt/trunk/lnt/tests/nt.py =================================================================== --- lnt/trunk/lnt/tests/nt.py +++ lnt/trunk/lnt/tests/nt.py @@ -100,7 +100,7 @@ opts -- the command line options object start_time -- the time the program was invoked as a string """ - assert type(opts) == dict, "Options must be a dict." + assert isinstance(opts, dict), "Options must be a dict." self.opts = opts self.__dict__.update(opts) self.start_time = start_time