Example #1
0
 def getFreq(self, seconds):
     if self.fake:
         base = 300
         if random.random() < .2:
             freq = base + randint(-50,50)
         else:
             freq = base + randint(-200, 200)
         #freq = (random.random() * 400) + 100.0
         distance = freq * 0.0051 - 0.0472
         return (distance, freq, 1, 1, 1, 1)
     data = self.read(seconds)
     self.timestamp = time.time()
     transform = FFT.real_fft(data).real
     minFreq = 20 
     maxFreq = 700
     minFreqPos = int(minFreq * seconds)
     maxFreqPos = int(maxFreq * seconds)
     minFreqPos = max(0, minFreqPos)
     maxFreqPos = min(int(self.sample_rate * seconds), maxFreqPos)
     if minFreqPos == maxFreqPos:
         self.lastFreq = int(self.sample_rate * sampleTime/ 2.0)
         return
     elif minFreqPos > maxFreqPos:
         minFreqPos, maxFreqPos = maxFreqPos, minFreqPos
     freqPos = Numeric.argmax(transform[1+minFreqPos:maxFreqPos])
     value = transform[1+minFreqPos:maxFreqPos][freqPos]
     freq = int((freqPos + minFreqPos) / seconds)
     distance = freq * 0.0051 - 0.0472
     bestFreqPos = Numeric.argmax(transform[1:])
     bestValue = transform[1:][bestFreqPos]
     bestFreq = int(bestFreqPos / seconds)
     return (distance, freq, value, transform[0], bestFreq, bestValue)
Example #2
0
 def getFreq(self, seconds):
     if self.fake:
         base = 300
         if random.random() < .2:
             freq = base + randint(-50, 50)
         else:
             freq = base + randint(-200, 200)
         #freq = (random.random() * 400) + 100.0
         distance = freq * 0.0051 - 0.0472
         return (distance, freq, 1, 1, 1, 1)
     data = self.read(seconds)
     self.timestamp = time.time()
     transform = FFT.real_fft(data).real
     minFreq = 20
     maxFreq = 700
     minFreqPos = int(minFreq * seconds)
     maxFreqPos = int(maxFreq * seconds)
     minFreqPos = max(0, minFreqPos)
     maxFreqPos = min(int(self.sample_rate * seconds), maxFreqPos)
     if minFreqPos == maxFreqPos:
         self.lastFreq = int(self.sample_rate * sampleTime / 2.0)
         return
     elif minFreqPos > maxFreqPos:
         minFreqPos, maxFreqPos = maxFreqPos, minFreqPos
     freqPos = Numeric.argmax(transform[1 + minFreqPos:maxFreqPos])
     value = transform[1 + minFreqPos:maxFreqPos][freqPos]
     freq = int((freqPos + minFreqPos) / seconds)
     distance = freq * 0.0051 - 0.0472
     bestFreqPos = Numeric.argmax(transform[1:])
     bestValue = transform[1:][bestFreqPos]
     bestFreq = int(bestFreqPos / seconds)
     return (distance, freq, value, transform[0], bestFreq, bestValue)
Example #3
0
    def __find_opt_w_p_node(self):
        """Help function for load_balancing()
        Finds and returns the optimal number of walkers
        per node for a possibly non-uniform set of nodes
        """
        timings = self.pypar.gather(Numeric.array([abs(self.t1-self.t0)]),
                                    self.master_rank)
        tmp_timings = copy.deepcopy(timings)
        timings = self.pypar.broadcast(tmp_timings,
                                       self.master_rank)

        C = self.no_of_walkers/sum(1./timings)

        tmp_loc_walkers = C/timings

        self.loc_walkers = self.NumericFloat2IntList(tmp_loc_walkers)
        remainders = tmp_loc_walkers-self.loc_walkers

        while sum(self.loc_walkers) < self.no_of_walkers:
            maxarg = Numeric.argmax(remainders)
            self.loc_walkers[maxarg] += 1
            remainders[maxarg] = 0

        if self.master and self.first_balance and\
               not hasattr(self,'silent'):
            print timings
            print self.loc_walkers

        return self.loc_walkers
Example #4
0
 def cal_selfing_generation_prob(self, data_matrix,
                                 locus_heterozygous_prob_vector,
                                 strain_acc_list, category_list,
                                 locus_heterozygous_prob_matrix,
                                 output_fname):
     sys.stderr.write("Selfing generation probability...")
     writer = csv.writer(open(output_fname, 'w'), delimiter='\t')
     no_of_strains, no_of_snps = data_matrix.shape
     selfing_generation_ls = []
     for i in range(no_of_strains):
         if self.debug:
             import pdb
             pdb.set_trace()
         selfing_generation_prob_vector = self.cal_individual_selfing_generation_prob_vector(
             data_matrix[i], locus_heterozygous_prob_vector,
             locus_heterozygous_prob_matrix)
         mle_selfing_generation = Numeric.argmax(
             selfing_generation_prob_vector)
         mle_selfing_generation_prob = selfing_generation_prob_vector[
             mle_selfing_generation]
         writer.writerow([
             strain_acc_list[i], category_list[i], mle_selfing_generation,
             mle_selfing_generation_prob
         ])
         selfing_generation_ls.append(mle_selfing_generation)
     del writer
     sys.stderr.write("Done.\n")
     return selfing_generation_ls
Example #5
0
    def __find_opt_w_p_node(self):
        """Help function for load_balancing()
        Finds and returns the optimal number of walkers
        per node for a possibly non-uniform set of nodes
        """
        timings = self.pypar.gather(Numeric.array([abs(self.t1 - self.t0)]),
                                    self.master_rank)
        tmp_timings = copy.deepcopy(timings)
        timings = self.pypar.broadcast(tmp_timings, self.master_rank)

        C = self.no_of_walkers / sum(1. / timings)

        tmp_loc_walkers = C / timings

        self.loc_walkers = self.NumericFloat2IntList(tmp_loc_walkers)
        remainders = tmp_loc_walkers - self.loc_walkers

        while sum(self.loc_walkers) < self.no_of_walkers:
            maxarg = Numeric.argmax(remainders)
            self.loc_walkers[maxarg] += 1
            remainders[maxarg] = 0

        if self.master and self.first_balance and\
               not hasattr(self,'silent'):
            print timings
            print self.loc_walkers

        return self.loc_walkers
Example #6
0
 def testSort (self):
     "Test sort, argsort, argmax, argmin, searchsorted"
     s = (3,2,5,1,4,0)
     sm = [s, Numeric.array(s)[::-1]]
     se = Numeric.array(s)[0:0]
     assert_eq(Numeric.sort(s), self.a)
     assert len(Numeric.sort(se)) == 0
     assert_eq(Numeric.argsort(s), [5,3,1,0,4,2])
     assert len(Numeric.argsort(se)) == 0
     assert_eq(Numeric.sort(sm, axis = -1), [[0,1,2,3,4,5],[0,1,2,3,4,5]])
     assert_eq(Numeric.sort(sm, axis = 0), [[0,2,1,1,2,0],[3,4,5,5,4,3]])
     assert_eq(Numeric.searchsorted(Numeric.arange(10), (5,2)), [5,2])
     assert Numeric.argmax(s) == 2
     assert Numeric.argmin(s) == 5
     assert_eq(Numeric.argmax(sm, axis=-1), [2,3])
     assert_eq(Numeric.argmax(sm, axis=1), [2,3])
     assert_eq(Numeric.argmax(sm, axis=0), [0,1,0,1,0,1])
     assert_eq(Numeric.argmin(sm, axis=-1), [5,0])
     assert_eq(Numeric.argmin(sm, axis=1), [5,0])
Example #7
0
	def get_majority_call_number(cls, call_counter_ls):
		"""
		2008-04-04
			make it class method
		2007-09-22
			return the call with maximum vote or NA
		"""
		index_with_max_value = num.argmax(call_counter_ls)
		if index_with_max_value>0:	#if 0(NA) is maximum or shared-maximum (call_counter_ls=0), no need to pursue
			for i in range(1, len(call_counter_ls)):
				if i!=index_with_max_value and call_counter_ls[i]==call_counter_ls[index_with_max_value]:	#a shared-maximum call, can't be resolved. =>NA
					index_with_max_value = 0
					break
		return index_with_max_value
Example #8
0
    def get_majority_call_number(cls, call_counter_ls):
        """
		2008-04-04
			make it class method
		2007-09-22
			return the call with maximum vote or NA
		"""
        index_with_max_value = num.argmax(call_counter_ls)
        if index_with_max_value > 0:  #if 0(NA) is maximum or shared-maximum (call_counter_ls=0), no need to pursue
            for i in range(1, len(call_counter_ls)):
                if i != index_with_max_value and call_counter_ls[
                        i] == call_counter_ls[
                            index_with_max_value]:  #a shared-maximum call, can't be resolved. =>NA
                    index_with_max_value = 0
                    break
        return index_with_max_value
Example #9
0
	def cal_selfing_generation_prob(self, data_matrix, locus_heterozygous_prob_vector, strain_acc_list, category_list, locus_heterozygous_prob_matrix, output_fname):
		sys.stderr.write("Selfing generation probability...")
		writer = csv.writer(open(output_fname, 'w'), delimiter='\t')
		no_of_strains, no_of_snps = data_matrix.shape
		selfing_generation_ls = []
		for i in range(no_of_strains):
			if self.debug:
				import pdb
				pdb.set_trace()
			selfing_generation_prob_vector = self.cal_individual_selfing_generation_prob_vector(data_matrix[i], locus_heterozygous_prob_vector, locus_heterozygous_prob_matrix)
			mle_selfing_generation = Numeric.argmax(selfing_generation_prob_vector)
			mle_selfing_generation_prob = selfing_generation_prob_vector[mle_selfing_generation]
			writer.writerow([strain_acc_list[i], category_list[i], mle_selfing_generation, mle_selfing_generation_prob])
			selfing_generation_ls.append(mle_selfing_generation)
		del writer
		sys.stderr.write("Done.\n")
		return selfing_generation_ls
Example #10
0
    def axisAndAngle(self):
        """Returns the axis (a normalized vector) and angle (a float,
        in radians)."""
	as = -self.tensor.asymmetricalPart()
	axis = VectorModule.Vector(as[1,2], as[2,0], as[0,1])
	sine = axis.length()
	if abs(sine) > 1.e-10:
	    axis = axis/sine
	    projector = axis.dyadicProduct(axis)
	    cosine = (self.tensor-projector).trace()/(3.-axis*axis)
	    angle = angleFromSineAndCosine(sine, cosine)
	else:
	    t = 0.5*(self.tensor+TensorModule.delta)
	    i = Numeric.argmax(t.diagonal().array)
	    axis = (t[i]/Numeric.sqrt(t[i,i])).asVector()
	    angle = 0.
	    if t.trace() < 2.:
		angle = Numeric.pi
	return axis, angle
Example #11
0
    def axisAndAngle(self):
        """Returns the axis (a normalized vector) and angle (a float,
        in radians). The angle is in the interval (-pi, pi]"""
	as = -self.tensor.asymmetricalPart()
	axis = VectorModule.Vector(as[1,2], as[2,0], as[0,1])
	sine = axis.length()
	if abs(sine) > 1.e-10:
	    axis = axis/sine
	    projector = axis.dyadicProduct(axis)
	    cosine = (self.tensor-projector).trace()/(3.-axis*axis)
	    angle = angleFromSineAndCosine(sine, cosine)
	else:
	    t = 0.5*(self.tensor+TensorModule.delta)
	    i = Numeric.argmax(t.diagonal().array)
	    axis = (t[i]/Numeric.sqrt(t[i,i])).asVector()
	    angle = 0.
	    if t.trace() < 2.:
		angle = Numeric.pi
	return axis, angle
Example #12
0
 def test(self):
     self.max_rel_dev=[]
     if self.ref:
         self._genref()
     else:
         if not self.vars_read:
             self._readref()
         self._compute()
         if type (self.result) != type(()): self.result=self.result,
         for t in range(len(self.out_vars.keys())):
             dev=abs(self.result[t]-self.out_vars[self.out_vars.keys()[t]])
             ref=abs(self.result[t]+self.out_vars[self.out_vars.keys()[t]])/2
             mx_dev_idx=Numeric.argmax(dev,axis=-1)
             if dev[mx_dev_idx] > 0.:
                 if ref[mx_dev_idx] > 0.:
                     self.max_rel_dev.append(dev[mx_dev_idx]/ref[mx_dev_idx])
                 else:
                     self.max_rel_dev.append(1.e+38)
     if len(self.max_rel_dev)>0:
         return max(self.max_rel_dev)
     return 0
Example #13
0
 for j in range(len(evals)):
   if data['equilibr']<11 or data['equilibr']==13:
     if linearx:
       evalsr[j] = evals[j].real-kV_fac*data['k']*data['Vz0']
     else:
       evalsr[j] = cmath.asinh(sfr*(evals[j].real-kV_fac*data['k']*data['Vz0'])).real
   elif data['equilibr']==12:
     kV = data['k']*data['Vp0']/data['Bt0']*data['Bz0']*data['a']+data['m']*data['Vp0']*data['a']
     kV = kV*kV_fac
     if linearx:
       evalsr[j] = evals[j].real-kV
     else:
       evalsr[j] = cmath.asinh(sfr*(evals[j].real-kV)).real
   if not lineary:
     evalsi[j] = cmath.asinh(sfi*(evals[j].imag)).real
 ind = Numeric.argmax(evalsi)
 maxr.append(evalsr[ind])
 maxi.append(evalsi[ind])
 ttl1 = '%s=%2.4g'%(ttl,data[var_key])
 if var_key=='tw':
   if data['tw']==-1:
     ttl1='Ideal Wall'
   elif data['tw']==0:
     ttl1='No Wall'
   else:
     ttl1 = '%s=10^{%g}'%(ttl,(cmath.log10(data[var_key]).real))
 elif var_key=='alpha':
   try:
     shft_grid=d2.grid[1:]-d2.grid[:-1]
     ttl1 = 'N_l: %d; N_h: %d'%(int(data['a']/max(shft_grid)),int(data['a']/min(shft_grid)))
   except:
Example #14
0
def SplitMatrix(nid, matrix, intervall, level, min_length=30, min_distance_border=0, map_row_new2old=None):
    """
    1. calculate objective function for matrix in intervall.


    \           <- xfrom
     \
      \
    c1 \
    ----\       <- x
       | \
    cc |c2\
                <- xto (one past last)

    l1 = x - xfrom
    l2 = xto - x

    chi-squared: 

    I[x] = (i11*i22-i21*i12)**2 * total / row&col-sums


    I[x] = mu[x]/F[x]

    """
    xfrom, xto = intervall
    l = xto - xfrom

    if l < min_length:
        return []

    if param_loglevel >= 3:
        print "# splitting matrix on level %i in intervall" % level, intervall, min_length

    ## 1. build Interfaces
    I = Numeric.zeros(l, Numeric.Float)

    for x in range(xfrom + 1, xto - 1):

        i11 = float(Numeric.sum(Numeric.sum(matrix[xfrom:x, xfrom:x])))
        i22 = float(Numeric.sum(Numeric.sum(matrix[x:xto, x:xto])))
        i12 = float(Numeric.sum(Numeric.sum(matrix[xfrom:x, x:xto])))
        i21 = i12

        row1 = i11 + i12
        row2 = i21 + i22
        col1 = i11 + i21
        col2 = i12 + i22

        l1 = x - xfrom
        l2 = xto - x

        a = i11 * i22 - i21 * i12

        n = row1 * row2 * col1 * col2
        if n > 0.0:
            I[l1] = a * a / n
        else:
            I[l1] = 0.0

        if param_loglevel >= 4:
            print "# %i\t%i\t%i\t%i\t%i\t%i\t%f" % (x, l1, l2, i11, i22, i12, I[l1])

    ## 2. split at maximum
    if min_distance_border and l > 2 * min_distance_border:
        xmax = Numeric.argmax(I[min_distance_border:(-min_distance_border)]) + min_distance_border
    else:
        xmax = Numeric.argmax(I)

    val = I[xmax]
    pos = xmax + xfrom

    if xmax == 0:
        return []

    if param_loglevel >= 2:
        print "# splitting at position %i with value %f" % (pos, val), xmax

    result = []

    if xmax > min_length:
        result += [
            (
                level + 1,
                map_row_new2old[xfrom:pos],
                SplitMatrix(nid, matrix, (xfrom, pos), level + 1, min_length, min_distance_border, map_row_new2old),
            )
        ]

    if (l - xmax) > min_length:
        result += [
            (
                level + 1,
                map_row_new2old[pos:xto],
                SplitMatrix(nid, matrix, (pos, xto), level + 1, min_length, min_distance_border, map_row_new2old),
            )
        ]

    return result