Exemplo n.º 1
0
def interpolate(points, t):
    """points=(x,y), list of x's and y's. "t" ... list of x, for which we want
    to evaluate the dependence "y". returns a list of "y" evaluated at the grid
    "t".
    Assumes t is sorted in ascending order
    Assumes points x axis are sorted in ascending order
    """
    xx, yy = points
    x = t

    t0 = numarray.compress(x <= min(xx), x)
    #   t1 = numarray.compress( (x>min(xx)) & (x<max(xx)), x )
    t1 = numarray.compress((x > min(xx)) & (x < xx[-1]), x)
    #   t2 = numarray.compress(x>=max(xx), x)
    t2 = numarray.compress(x >= xx[-1], x)
    if (xx[1] - xx[0] == 0):
        slope0 = sign(yy[1] - yy[0]) * 1e20
    else:
        slope0 = (yy[1] - yy[0]) / (xx[1] - xx[0])
    slope2 = (yy[-1] - yy[-2]) / (xx[-1] - xx[-2])
    indices = numarray.searchsorted(xx, t1)
    x0 = xx[indices - 1]
    x1 = xx[indices]
    y0 = yy[indices - 1]
    y1 = yy[indices]
    slope = (y1 - y0) / (x1 - x0)

    y1 = slope * (t1 - x0) + y0
    y0 = slope0 * (t0 - xx[0]) + yy[0]  # extrapolate
    y2 = slope2 * (t2 - xx[-1]) + yy[-1]  # extrapolate

    y = numarray.concatenate((y0, y1, y2))
    return y
Exemplo n.º 2
0
def calculate_spectrogram(input_array,
						  framesize,
						  hopsize,
						  window_function = numarray.linear_algebra.mlab.hanning,
						  keep_bands_until = 0,
						  axis = 0):
	"""Calculate the spectrogram."""

	do_fft = lambda arr: abs(numarray.fft.real_fft(arr, axis = axis))[:keep_bands_until]
	
	keep_bands_until = keep_bands_until or int(framesize / 2)
	input_shape = map(None, numarray.shape(input_array))

	window = window_function(framesize)
	if len(input_shape) > 1:
		window = transpose(array([list(window)] * input_shape[1]))
		
	# print "input_shape:", shape(input_array)
	zeros_shape = input_shape  # this allows for both 1 and 2 dim inputs
	zeros_shape[0] = framesize / 2
	input_array_plus_zeros = numarray.concatenate((numarray.zeros(zeros_shape), input_array, numarray.zeros(zeros_shape)))
	fft_range = range(0, len(input_array) - framesize, hopsize)

	fft_array = numarray.zeros(([len(fft_range)] + 
								map(None, numarray.shape(do_fft(window)))),
							   # do_fft(window) is used here because it gives the right shape
							   numarray.Float32) * 1.0  # this *1.0 is necessary!
	for result_counter, input_counter in enumerate(fft_range):
		frame = window * input_array_plus_zeros[input_counter : input_counter + framesize]
		fft_array[result_counter] = 10 * numarray.log10(0.1 + do_fft(frame))

	return fft_array
Exemplo n.º 3
0
def interpolate(points,t):
    """points=(x,y), list of x's and y's. "t" ... list of x, for which we want
    to evaluate the dependence "y". returns a list of "y" evaluated at the grid
    "t".
    Assumes t is sorted in ascending order
    Assumes points x axis are sorted in ascending order
    """
    xx,yy=points
    x=t

    t0 = numarray.compress(x<=min(xx), x)
#   t1 = numarray.compress( (x>min(xx)) & (x<max(xx)), x )
    t1 = numarray.compress( (x>min(xx)) & (x<xx[-1]), x )
#   t2 = numarray.compress(x>=max(xx), x)
    t2 = numarray.compress(x>=xx[-1], x)
    if (xx[1]-xx[0] == 0):
        slope0 = sign(yy[1]-yy[0])*1e20
    else:
        slope0 = (yy[1]-yy[0])/(xx[1]-xx[0])
    slope2 = (yy[-1]-yy[-2])/(xx[-1]-xx[-2])
    indices = numarray.searchsorted(xx,t1)
    x0 = xx[indices-1]
    x1 = xx[indices]
    y0 = yy[indices-1]
    y1 = yy[indices]
    slope = (y1-y0)/(x1-x0)

    y1 = slope*(t1-x0)+y0
    y0 = slope0*(t0-xx[0])+yy[0]    # extrapolate
    y2 = slope2*(t2-xx[-1])+yy[-1]    # extrapolate

    y = numarray.concatenate((y0,y1,y2))
    return y
def create_households_for_estimation(agent_set, dbcon):
    estimation_set = HouseholdSet(in_base=dbcon, in_storage_type="mysql", in_place="households_for_estimation")
    agent_set.unload_nonderived_attributes()
    agent_set.load_dataset(attributes="*")
    estimation_set.load_dataset(agent_set.get_nonderived_attribute_names())
    for attr in agent_set.get_attribute_names():
        agent_set.set[attr].set_data(concatenate((estimation_set.set[attr].get_data(), agent_set.set[attr].get_data())))
    agent_set.update_id_mapping()
    agent_set.update_size()
    return (agent_set, arange(estimation_set.size()))
def create_households_for_estimation(agent_set, dbcon):
        estimation_set = HouseholdSet(in_base = dbcon, in_storage_type="mysql", \
                in_place="households_for_estimation")
        agent_set.unload_nonderived_attributes()
        agent_set.load_dataset(attributes='*')
        estimation_set.load_dataset(agent_set.get_nonderived_attribute_names())
        for attr in agent_set.get_attribute_names():
            agent_set.set[attr].set_data(concatenate((estimation_set.set[attr].get_data(), agent_set.set[attr].get_data())))
        agent_set.update_id_mapping()
        agent_set.update_size()
        return (agent_set, arange(estimation_set.size()))
Exemplo n.º 6
0
    def setParameters(self, mu = None, sigma = None, wi = None, sigma_type = 'full', \
                      tied_sigma = False, isAdjustable = False):
        #============================================================
        # set the mean :
        # self.mean[i] = the mean for dimension i
        # self.mean.shape = (self.nvalues, q1,q2,...,qn)
        #        where qi is the size of discrete parent i
        if mu == None:
            # set all mu to zeros
            mu = na.zeros(shape=([self.nvalues]+self.discrete_parents_shape), \
                          type='Float32')
        try:
            mu = na.array(shape=[self.nvalues]+self.discrete_parents_shape, \
                          type='Float32')
        except:
            raise 'Could not convert mu to numarray of shape : %s, discrete parents = %s' %(str(self.discrete_parents_shape),
                                                                                            str([dp.name for dp in self.discrete_parents]))
        self.mean = mu

        #============================================================
        # set the covariance :
        # self.sigma[i,j] = the covariance between dimension i and j
        # self.sigma.shape = (nvalues,nvalues,q1,q2,...,qn)
        #        where qi is the size of discrete parent i
        if sigma == None:
            eye = na.identity(self.nvalues, type = 'Float32')[...,na.NewAxis]
            if len(self.discrete_parents) > 0:
                q = reduce(lambda a,b:a*b,self.discrete_parents_shape) # number of different configurations for the parents
                sigma = na.concatenate([eye]*q, axis=2)
                sigma = na.array(sigma,shape=[self.nvalues,self.nvalues]+self.discrete_parents_shape) 
        try:
            sigma = na.array(sigma, shape=[self.nvalues,self.nvalues]+self.discrete_parents_shape, type='Float32')
        except:
            raise 'Not a valid covariance matrix'
        self.sigma = sigma

        #============================================================
        # set the weights :
        # self.weights[i,j] = the regression for dimension i and continuous parent j
        # self.weights.shape = (nvalues,x1,x2,...,xn,q1,q2,...,qn)
        #        where xi is the size of continuous parent i)
        #        and qi is the size of discrete parent i
        
        if wi == None:
            wi = na.ones(shape=[self.nvalues]+self.parents_shape, type='Float32') 
        try:
            wi = na.array(wi, shape=[self.nvalues]+self.parents_shape, type='Float32')
        except:
            raise 'Not a valid weight'
        self.weights = wi
Exemplo n.º 7
0
	def __init__(self, parent,
				 framesize = None,
	 			 hopsize = 100,
				 number_of_vectors_used = 15):
		if not framesize:
			lp = len(parent)
			#logfs = min(int(scipy.log2(lp / 32)), 10)
			log2 = lambda x: math.log(x) / math.log(2)
			logfs = min(int(log2(lp / 32)), 10)
			framesize = pow(2, logfs)
			print framesize
		if hopsize > framesize / 4:
			hopsize = framesize / 4

		self.hopsize = hopsize
		self.framesize = framesize

		#if len(parent) < framesize:
		#	self._delete_from_parents()
		#	raise UnderflowError, "Thingy is too small...\nHere I pull the plug in order to avoid a segfaulty thingy."

		input_array = parent
		
		self.feature_vectors = None
		interesting_parts = input_array[:, :number_of_vectors_used]
		interesting_parts = numarray.transpose(interesting_parts)

		for row in interesting_parts:
			specspectrum = calculate_spectrogram(row,
											  framesize = framesize,
											  hopsize = hopsize)
			import cPickle
			z, lambdas, EOFs = svdeofs.svdeofs(specspectrum)
			print ".",
			s = z[:, :15]
			svd_fft_fft_vectors = numarray.transpose(s)
			
			#print "svd_eofs ok!"
			if self.feature_vectors is None:
				self.feature_vectors = numarray.transpose(svd_fft_fft_vectors)
			else:
				self.feature_vectors = numarray.concatenate((self.feature_vectors,
															numarray.transpose(svd_fft_fft_vectors)), 1)
			#print "svd_fft_fft_vectors shapes:", shape(svd_fft_fft_vectors), shape(self.feature_vectors)

		z, lambdas, EOFs = svdeofs.svdeofs(self.feature_vectors)

		self.feature_vectors = z[:, :15]
		self.arr = self.feature_vectors
Exemplo n.º 8
0
def file2matrix(fileName):
    """
        Given a file "fileName", this script converts the contents of the 
        file to a numarray matrix of size (m,) or (m,n).
    """
    # Open the file
    open1 = open(fileName, 'r')
    line = open1.readline()

    # A single line from the file is read. This should be the number
    # of columns. Spliting "line" results in a list, the length of
    # which is the number of columns: cols
    cols = len(line.split())

    # x is the placeholder for the data.
    # The placeholder matrix is made of concatenating
    # every line that is read from the file.
    # The type code is float.
    x = numarray.zeros(cols, type='f')

    while (line):
        temp = []
        list_elements = line.split()
        for e in list_elements:
            temp.append(float(e))
        x = numarray.concatenate((x, numarray.array(temp)))
        line = open1.readline()

    open1.close()

    # concatenate((x,x)) is along the dimension 1,
    # each new addition is an increase in columns,
    # the total length of x gives m*n. We determine
    # the rows as
    rows = len(x) / cols

    # We have added an aritifical row, so if row == 2
    # the file has tuple, hence the data consists of
    # everything after our artificial set of columns
    if rows == 2:
        return x[cols:]
    else:
        # If there are m+1 rows, we resize the data to (m+1)xn
        # and return everything other than the artificial first
        # row of zeros
        x = numarray.resize(x, (rows, cols))
        return x[1:, :]
Exemplo n.º 9
0
def scanSequence(mix, bg, seq,scoring='mix'):
    """
    Scores all positions of a sequence with the given model and background.
    
    @param mix: MixtureModel object
    @param bg: background MixtureModel object
    @param seq: sequence as list of nucleotides
    @param scoring: flag to determine the scoring scheme used for the mixtures. 
      'compmax' means maximum density over the components, 'mix' means true mixture density
    
    @return: list of position-wise log-odd scores
    """
    # convert sequence to internal representation, alphabet of seq must be DNA
    alph = mixture.Alphabet(['A','C','G','T'])
    f = lambda x: alph.internal(x)
    seq=map(f,seq)
    
    dnr = mix.components[0].dist_nr

    # init with dummy value at first position
    s = numarray.array([[-1]+ seq[0:dnr-1]])
    
    
    score = []
    for i in range(dnr-1,len(seq),1):
        # shift query sequence by one position
        s[0] = numarray.concatenate( [s[0][1:],numarray.array([seq[i]])],0)

        if scoring == 'compmax':
            # score as maximum over components 
            c_m_l = numarray.zeros(mix.G,numarray.Float)
            for i in range(mix.G):
                c_m_l[i] = mix.components[i].pdf(s)[0]
            m_l = c_m_l.max()

        elif scoring == 'mix':
            m_l =   mix.pdf(s)[0]          
            
        bg_l = bg.pdf(s)[0]


        score.append(m_l-bg_l)

    return score
Exemplo n.º 10
0
    def __init__(self, v, mu = None, sigma = None, wi = None, \
                   sigma_type = 'full', tied_sigma = False, \
                   isAdjustable = True, ignoreFamily = False):

        Distribution.__init__(self, v, isAdjustable=isAdjustable, \
                              ignoreFamily=ignoreFamily)
        self.distribution_type = 'Gaussian'

        # check that current node is continuous
        if v.discrete:
            raise 'Node must be continuous'

        self.discrete_parents = [parent for parent in self.parents \
                                 if parent.discrete]
        self.continuous_parents = [parent for parent in self.parents \
                                   if not parent.discrete]

        self.discrete_parents_shape = [dp.nvalues for dp \
                                       in self.discrete_parents]
        self.parents_shape = [p.nvalues for p in self.parents]
        if not self.parents_shape:
            self.parents_shape = [0]

        # set defaults
        # set all mu to zeros
        self.mean = na.zeros(shape=([self.nvalues] + \
                             self.discrete_parents_shape), type='Float32')

        # set sigma to ones along the diagonal	
        eye = na.identity(self.nvalues, type = 'Float32')[..., na.NewAxis]
        if len(self.discrete_parents) > 0:            
            q = reduce(lambda a, b:a * b, self.discrete_parents_shape) # number of different configurations for the parents
            sigma = na.concatenate([eye] * q, axis=2)
            self.sigma = na.array(sigma, shape=[self.nvalues, self.nvalues] + \
                                  self.discrete_parents_shape) 

        # set weights to 
        self.weights = na.ones(shape=[self.nvalues] + self.parents_shape, type='Float32')

        # set the parameters : mean, sigma, weights
        self.setParameters(mu=mu, sigma=sigma, wi=wi, sigma_type=sigma_type, \
                           tied_sigma=tied_sigma, isAdjustable=isAdjustable)
Exemplo n.º 11
0
    def _updateVertexPosition( self ):
        # Draw Colormap Line set
        self.vertexPositions  = numarray.concatenate( ( self.vertexPositions[0:256],
                                                        self.vertexPositions[256:512]),
                                                        axis = 1 ).copy()            
        self.vertexPositions.setshape( 512, 3 )       
       
        scalar      = self.vertexPositions[:,1]
        sclrMinVal  = scalar.max()
        sclrMaxVal  = scalar.min()

        fct = (len(self.cMapColors)-1) / (sclrMaxVal - sclrMinVal)
        sclrIdx	 = fct * (scalar - sclrMinVal) + 0.5
        sclrIdx	 = numarray.clip( sclrIdx, 0, len(self.cMapColors)-1 )
        colorIdx = self.cmapRGBA[sclrIdx]
        colorIdx = colorIdx.tolist()
        colorIdx.reverse()       
     
        self.vertexPropertyObj.vertex.setValues( 0, self.vertexPositions )        
        self.vertexPropertyObj.orderedRGBA.setValues( 0, colorIdx )         
Exemplo n.º 12
0
        plot.hline(0)
        residplot.setTitle("%s: %i MeV, %.1f deg\n ks prob=%.2e" %
                           (self.irfs, energy, inclination, ks_prob[1]))
        return energy, inclination, ks_prob[1]

    def _interpolate(self, x, y, xval):
        if xval > x[-1]:
            return 1
        indx = bisect.bisect(x, xval) - 1
        yval = ((xval - x[indx]) / (x[indx + 1] - x[indx]) *
                (y[indx + 1] - y[indx]) + y[indx])
        return yval


if __name__ == '__main__':
    seps = num.concatenate(
        (num.arange(12, 19), num.arange(19, 21, 0.3), num.arange(21, 25)))

    energies = (30, 100, 1000, 1e4)
    #    energies = (1e3, 3e3, 1e4, 3e4)
    incs = (0, 10, 30)

    nt = plot.newNTuple(([], [], []), ('energy', 'inc', 'ks prob'))
    plot.Scatter(nt, 'energy', 'ks prob')
    plot.Scatter(nt, 'inc', 'ks prob')

    def createPlots(irfs, seps, energies, inclinations):
        my_tests = PsfTests(irfs, seps)
        for energy in energies:
            for inclination in inclinations:
                my_tests.plotResults(energy, inclination, plot_residuals=True)
                nt.addRow(my_tests.plot_rspgenIntegral(energy, inclination))
Exemplo n.º 13
0
    def sample(self,
               varlist=None,
               dt=None,
               tlo=None,
               thi=None,
               eventdata=None,
               precise=False,
               useGlobalTime=True):
        """Uniformly sample the named trajectory over range indicated.
        If optional 'eventdata' argument included, the event points are included
        in the mesh (e.g. for plotting purposes).

        The order of variable names in varlist is ignored.

        precise=True causes the trajectory position to be evaluated precisely
        at the t values specified, which will invoke slow interpolation.
        precise=False (default) causes the nearest underlying mesh positions
        to be used, if available (otherwise the behaviour is the same as
                                  precise=True)

        If dt is not given, the underlying time mesh is used, if available.
        """

        if varlist is None:
            varlist_sorted = self.varnames
        else:
            assert isinstance(varlist, list), 'varlist argument must be a list'
            varlist_sorted = copy.copy(varlist)
            varlist_sorted.sort()
        tlo_base = self.indepdomain.get(0)
        thi_base = self.indepdomain.get(1)
        if tlo is None:
            tlo = tlo_base
        elif useGlobalTime:
            tlo = tlo - self.globalt0
            if tlo < tlo_base:
                tlo = tlo_base
            if tlo >= thi_base:
                raise ValueError, "tlo too large"
        elif tlo < tlo_base:
            tlo = tlo_base
        elif tlo >= thi_base:
            raise ValueError, "tlo too large"
        if thi is None:
            thi = thi_base
        elif useGlobalTime:
            thi = thi - self.globalt0
            if thi > thi_base:
                thi = thi_base
            if thi <= tlo_base:
                raise ValueError, "thi too small"
        elif thi > thi_base:
            thi = thi_base
        elif thi <= tlo_base:
            raise ValueError, "thi too small"
        assert tlo < thi, 't start point must be less than t endpoint'
        if dt is not None and dt >= abs(thi - tlo):
            if precise:
                print "dt = %f for interval [%f,%f]" % (dt, tlo, thi)
                raise ValueError('dt must be smaller than time interval')
            else:
                dt = (thi - tlo) / 10.
##        do_events = eventdata is not None
        if not precise or dt is None:
            # attempt to use underlying mesh for each variable,
            # if available
            meshdict = self.underlyingMesh()
            meshes_ok = False
            if meshdict is not None:
                if not any([meshdict[v] is None for v in varlist_sorted]):
                    # ensure all vars' meshes are identical!
                    firstvar_tmesh = meshdict[varlist_sorted[0]][0]
                    if not type(firstvar_tmesh) in [Array, NArray]:
                        firstvar_tmesh = array(firstvar_tmesh)
                    meshes_ok = True
                    # explicitly checking that the arrays are the same
                    # is very slow -- just catch a resulting error later
#                    try:
#                        for v in varlist_sorted[1:]:
#                            assert all(meshdict[v][0] == firstvar_tmesh)
#                        meshes_ok = True
#                    except AssertionError:
#                        meshes_ok = False
            if meshes_ok:
                loix_a = firstvar_tmesh >= tlo
                loix_a = loix_a.tolist()
                try:
                    loix = loix_a.index(1)
                except ValueError:
                    loix = 0
                hiix_a = firstvar_tmesh > thi
                hiix_a = hiix_a.tolist()
                try:
                    hiix = hiix_a.index(1)
                except ValueError:
                    hiix = len(firstvar_tmesh)
                tmesh = firstvar_tmesh[loix:hiix]
                #do_events = False
        if dt is None:
            if not meshes_ok:
                raise ValueError("Underlying mesh of trajectory is not"
                                 " the same for all variables: dt must "
                                 "be specified in this case")
        else:
            tmesh = concatenate([arange(tlo, thi, dt), [thi]])
            loix = 0
            hiix = len(tmesh)
        if eventdata:  #and do_events:
            if isinstance(eventdata, dict):
                assert type(eventdata.values()[0]) in [Array, NArray, list], \
                   "eventdata must be list of times or dictionary of lists of times"
                evtlist = orderEventData(eventdata, nonames=True)
            elif type(eventdata) in [Array, NArray, list]:
                evtlist = eventdata
            else:
                raise TypeError, \
                   "eventdata must be list of times or dictionary of lists of times"
            if evtlist != []:
                tmesh, ins_ixs = insertInOrder(tmesh, evtlist, True)
            else:
                ins_ixs = []
        if len(tmesh) > 0:
            if dt is None:
                # meshes_ok already checked
                ##                coorddict = {}
                ##                for v in varlist_sorted:
                ##                    coorddict[v] = meshdict[v][1]
                coorddict = dict(
                    zip(varlist_sorted,
                        [m[1][loix:hiix] for m in sortedDictValues(meshdict)]))
                if eventdata:  #and do_events:
                    # insert var values at events (SLOW!)
                    # can only insert to lists, so have to convert coorddict arrays
                    # to lists first.
                    for tpos in xrange(len(ins_ixs)):
                        tix = ins_ixs[tpos]
                        t = evtlist[tpos]
                        x = self(t, varlist_sorted)
                        for v in varlist_sorted:
                            try:
                                coorddict[v].insert(tix, x[v])
                            except AttributeError:
                                coorddict[v] = coorddict[v].tolist()
                                coorddict[v].insert(tix, x[v])
                if useGlobalTime and self.globalt0 != 0:
                    tmesh = tmesh + self.globalt0
                return Pointset({
                    'coorddict': coorddict,
                    'coordtype': Float,
                    'coordnames': varlist_sorted,
                    'indepvararray': tmesh,
                    'indepvarname': self.indepvarname,
                    'indepvartype': Float,
                    'norm': self._normord,
                    'name': self.name + "_sample"
                })
            if not precise:
                # get closest mesh indices corresponding to tmesh
                # times for first var
                try:
                    try:
                        closest = findClosestArray(firstvar_tmesh, tmesh,
                                                   dt / 2)
                        find_ixs = True
                    except UnboundLocalError:
                        # meshes_ok==False and firstvar_tmesh never created
                        # so don't need to find indexes of closest meshpoints,
                        # just use original mesh
                        find_ixs = False
                    coorddict = {}
                    if find_ixs:
                        closest_unique = makeSeqUnique(closest, True, True)
                        if useGlobalTime and self.globalt0 != 0:
                            tmesh = firstvar_tmesh[
                                closest_unique] + self.globalt0
                        else:
                            tmesh = firstvar_tmesh[closest_unique]
                        for v in varlist_sorted:
                            dat = self.variables[v].output.datapoints[1]
                            try:
                                coorddict[v] = dat[closest_unique]
                            except TypeError:
                                dat = array(dat)
                                coorddict[v] = dat[closest_unique]
                    else:
                        for v in varlist_sorted:
                            coorddict[v] = self.variables[v].output.datapoints[
                                1]
                    return Pointset({
                        'coorddict': coorddict,
                        'coordtype': Float,
                        'coordnames': varlist_sorted,
                        'indepvararray': tmesh,
                        'indepvarname': self.indepvarname,
                        'indepvartype': Float,
                        'norm': self._normord,
                        'name': self.name + "_sample"
                    })
                except (AttributeError, IndexError):
                    # meshes of variables did not match up, so
                    # continue to 'else' case, as if precise=True
                    pass
            # else mesh not available for some variables so we have
            # a mixed-source trajectory, and simple solution is to
            # treat as if precise==True
            # Cases: either precise == True or could not extract
            # underlying meshes for some variables, or the meshes were not
            # identical.
            pset = Pointset({
                'coordarray':
                self(tmesh, varlist_sorted, useGlobalTime=False).toarray(),
                'coordnames':
                varlist_sorted,
                'indepvararray':
                array(tmesh),
                'indepvarname':
                self.indepvarname,
                'name':
                self.name + "_sample"
            })
            # if varlist was not sorted, call to self could potentially return
            # an array in a different order to that of varlist.
            if useGlobalTime and self.globalt0 != 0:
                pset.indepvararray += self.globalt0
                pset.makeIxMaps()
            return pset
        else:
            return None
    def sample(self, varlist=None, dt=None, tlo=None, thi=None,
                   eventdata=None, precise=False, useGlobalTime=True):
        """Uniformly sample the named trajectory over range indicated.
        If optional 'eventdata' argument included, the event points are included
        in the mesh (e.g. for plotting purposes).

        The order of variable names in varlist is ignored.

        precise=True causes the trajectory position to be evaluated precisely
        at the t values specified, which will invoke slow interpolation.
        precise=False (default) causes the nearest underlying mesh positions
        to be used, if available (otherwise the behaviour is the same as
                                  precise=True)

        If dt is not given, the underlying time mesh is used, if available.
        """

        if varlist is None:
            varlist_sorted = self.varnames
        else:
            assert isinstance(varlist, list), 'varlist argument must be a list'
            varlist_sorted = copy.copy(varlist)
            varlist_sorted.sort()
        tlo_base = self.indepdomain.get(0)
        thi_base = self.indepdomain.get(1)
        if tlo is None:
            tlo = tlo_base
        elif useGlobalTime:
            tlo = tlo - self.globalt0
            if tlo < tlo_base:
                tlo = tlo_base
            if tlo >= thi_base:
                raise ValueError, "tlo too large"
        elif tlo < tlo_base:
            tlo = tlo_base
        elif tlo >= thi_base:
            raise ValueError, "tlo too large"
        if thi is None:
            thi = thi_base
        elif useGlobalTime:
            thi = thi - self.globalt0
            if thi > thi_base:
                thi = thi_base
            if thi <= tlo_base:
                raise ValueError, "thi too small"
        elif thi > thi_base:
            thi = thi_base
        elif thi <= tlo_base:
            raise ValueError, "thi too small"
        assert tlo < thi, 't start point must be less than t endpoint'
        if dt is not None and dt >= abs(thi-tlo):
            if precise:
                print "dt = %f for interval [%f,%f]"%(dt,tlo,thi)
                raise ValueError('dt must be smaller than time interval')
            else:
                dt = (thi-tlo)/10.
##        do_events = eventdata is not None
        if not precise or dt is None:
            # attempt to use underlying mesh for each variable,
            # if available
            meshdict = self.underlyingMesh()
            meshes_ok = False
            if meshdict is not None:
                if not any([meshdict[v] is None for v in varlist_sorted]):
                    # ensure all vars' meshes are identical!
                    firstvar_tmesh = meshdict[varlist_sorted[0]][0]
                    if not type(firstvar_tmesh) in [Array, NArray]:
                        firstvar_tmesh = array(firstvar_tmesh)
                    meshes_ok = True
                    # explicitly checking that the arrays are the same
                    # is very slow -- just catch a resulting error later
#                    try:
#                        for v in varlist_sorted[1:]:
#                            assert all(meshdict[v][0] == firstvar_tmesh)
#                        meshes_ok = True
#                    except AssertionError:
#                        meshes_ok = False
            if meshes_ok:
                loix_a = firstvar_tmesh>=tlo
                loix_a = loix_a.tolist()
                try:
                    loix = loix_a.index(1)
                except ValueError:
                    loix = 0
                hiix_a = firstvar_tmesh>thi
                hiix_a = hiix_a.tolist()
                try:
                    hiix = hiix_a.index(1)
                except ValueError:
                    hiix = len(firstvar_tmesh)
                tmesh = firstvar_tmesh[loix:hiix]
                #do_events = False
        if dt is None:
            if not meshes_ok:
                raise ValueError("Underlying mesh of trajectory is not"
                                    " the same for all variables: dt must "
                                    "be specified in this case")
        else:
            tmesh = concatenate([arange(tlo, thi, dt),[thi]])
            loix = 0
            hiix = len(tmesh)
        if eventdata: #and do_events:
            if isinstance(eventdata, dict):
                assert type(eventdata.values()[0]) in [Array, NArray, list], \
                   "eventdata must be list of times or dictionary of lists of times"
                evtlist = orderEventData(eventdata, nonames=True)
            elif type(eventdata) in [Array, NArray, list]:
                evtlist = eventdata
            else:
                raise TypeError, \
                   "eventdata must be list of times or dictionary of lists of times"
            if evtlist != []:
                tmesh, ins_ixs = insertInOrder(tmesh, evtlist, True)
            else:
                ins_ixs = []
        if len(tmesh) > 0:
            if dt is None:
                # meshes_ok already checked
##                coorddict = {}
##                for v in varlist_sorted:
##                    coorddict[v] = meshdict[v][1]
                coorddict = dict(zip(varlist_sorted,
                                 [m[1][loix:hiix] for m in sortedDictValues(meshdict)]))
                if eventdata: #and do_events:
                    # insert var values at events (SLOW!)
                    # can only insert to lists, so have to convert coorddict arrays
                    # to lists first.
                    for tpos in xrange(len(ins_ixs)):
                        tix = ins_ixs[tpos]
                        t = evtlist[tpos]
                        x = self(t, varlist_sorted)
                        for v in varlist_sorted:
                            try:
                                coorddict[v].insert(tix, x[v])
                            except AttributeError:
                                coorddict[v] = coorddict[v].tolist()
                                coorddict[v].insert(tix, x[v])
                if useGlobalTime and self.globalt0 != 0:
                    tmesh = tmesh + self.globalt0
                return Pointset({'coorddict': coorddict,
                           'coordtype': Float,
                           'coordnames': varlist_sorted,
                           'indepvararray': tmesh,
                           'indepvarname': self.indepvarname,
                           'indepvartype': Float,
                           'norm': self._normord,
                           'name': self.name + "_sample"})
            if not precise:
                # get closest mesh indices corresponding to tmesh
                # times for first var
                try:
                    try:
                        closest = findClosestArray(firstvar_tmesh,tmesh,dt/2)
                        find_ixs = True
                    except UnboundLocalError:
                        # meshes_ok==False and firstvar_tmesh never created
                        # so don't need to find indexes of closest meshpoints,
                        # just use original mesh
                        find_ixs = False
                    coorddict = {}
                    if find_ixs:
                        closest_unique = makeSeqUnique(closest, True, True)
                        if useGlobalTime and self.globalt0 != 0:
                            tmesh = firstvar_tmesh[closest_unique] + self.globalt0
                        else:
                            tmesh = firstvar_tmesh[closest_unique]
                        for v in varlist_sorted:
                            dat = self.variables[v].output.datapoints[1]
                            try:
                                coorddict[v] = dat[closest_unique]
                            except TypeError:
                                dat = array(dat)
                                coorddict[v] = dat[closest_unique]
                    else:
                        for v in varlist_sorted:
                            coorddict[v] = self.variables[v].output.datapoints[1]
                    return Pointset({'coorddict': coorddict,
                                   'coordtype': Float,
                                   'coordnames': varlist_sorted,
                                   'indepvararray': tmesh,
                                   'indepvarname': self.indepvarname,
                                   'indepvartype': Float,
                                   'norm': self._normord,
                                   'name': self.name + "_sample"})
                except (AttributeError, IndexError):
                    # meshes of variables did not match up, so
                    # continue to 'else' case, as if precise=True
                    pass
            # else mesh not available for some variables so we have
            # a mixed-source trajectory, and simple solution is to
            # treat as if precise==True
            # Cases: either precise == True or could not extract
            # underlying meshes for some variables, or the meshes were not
            # identical.
            pset = Pointset({'coordarray': self(tmesh, varlist_sorted,
                                                useGlobalTime=False).toarray(),
                     'coordnames': varlist_sorted,
                     'indepvararray': array(tmesh),
                     'indepvarname': self.indepvarname,
                     'name': self.name + "_sample"})
            # if varlist was not sorted, call to self could potentially return
            # an array in a different order to that of varlist.
            if useGlobalTime and self.globalt0 != 0:
                pset.indepvararray += self.globalt0
                pset.makeIxMaps()
            return pset
        else:
            return None
Exemplo n.º 15
0
    def getDistribution(self):
        self.distributionList = []
        
        node = self.xbn.childNodes
        
        if self.version == "1.0":
            bnmodel = node[0].childNodes
            statdyndist = bnmodel[1].childNodes
            distribution = statdyndist[8].childNodes
        
        else:
            bnmodel = node[1].childNodes
            statdyndist = bnmodel[1].childNodes               
            distribution = statdyndist[9].childNodes
        
        for dist in distribution:
            d = Distribution()
            d.condelem = []
            d.dpiIndex = []
            d.dpiData = []
            
            if dist.nodeType == Node.ELEMENT_NODE:
                attrs = dist.attributes
                
                for attrName in attrs.keys(): 
                    attrNode = attrs.get(attrName)
                    attrValue = attrNode.nodeValue

                    if attrName == "TYPE":
                        d.type = attrValue
            
            for distInfos in dist.childNodes:
                
                if distInfos.nodeType == Node.ELEMENT_NODE:

                    if distInfos.nodeName == "CONDSET":
                        
                        for elem in distInfos.childNodes:
                            
                            if elem.nodeType == Node.ELEMENT_NODE:
                                attrsb = elem.attributes
                                d.condelem.append(attrsb.get(attrsb.keys()[0]).nodeValue)

                    elif distInfos.nodeName == "PRIVATE":
                        
                        if distInfos.nodeType == Node.ELEMENT_NODE:
                            attrsb = distInfos.attributes
                            d.name = attrsb.get(attrsb.keys()[0]).nodeValue

                    elif distInfos.nodeName == "DPIS":
                        
                        for dpi in distInfos.childNodes:
                            
                            if dpi.nodeName == "DPI":
                                d.dpiData.append(dpi.childNodes[0].nodeValue)
                                attrs = dpi.attributes
                                
                                for attrName in attrs.keys(): 
                                    attrNode = attrs.get(attrName)
                                    attrValue = attrNode.nodeValue

                                    if attrName == "INDEXES":
                                        d.dpiIndex.append(attrValue)

            if dist.nodeType == Node.ELEMENT_NODE:                                   
                self.distributionList.append(d)
                
        for d in self.distributionList:
            dist = self.G.v[d.name].distribution # the distribution class into the BNet
            
            #---TODO: what about gaussians ???
            dist.distribution_type = 'Multinomial'
            
            if d.type == 'ci':
                # conditionally independant values are defined
                # fill the matrix with the conditionally independant term
                new = array([float(da) for da in d.dpiData[0].split()],type='Float32') # transform a string into a numarray
                for pa in dist.family[1:]:
                    new = new[...,NewAxis]
                    n_states = pa.nvalues # number of states for each parent
                    new = concatenate([new]*n_states, axis=-1)
                    
                # replace all values in the distribution with the ci values
                dist[:]=new            

            if len(d.dpiIndex):
                # when multiple elements (nodes with parents)
                for data,index in zip(d.dpiData,d.dpiIndex):
                    # data, index are strings containing the data and index
                    ii = tuple([int(i) for i in index.split()]) # transform the string into a tuple of integers
                    
                    # create a dictionnary with the name of the dimension and the value it takes
                    dictin = {}     # e.g. dictin = {'Alternator':1,'FanBelt':0}
                    for pa,iii in zip(d.condelem,ii):
                        dictin[pa] = iii
                        
                    dd = array([float(da) for da in data.split()],type='Float32') # transform a string into a numarray
                    dist[dictin] = dd
                
            else:
                # for nodes with no parents
                # simply insert the data into the matrix
                dd = array([float(da) for da in d.dpiData[0].split()],type='Float32')
                dist[:] = dd
            
        return self.distributionList