Exemplo n.º 1
0
    def process(self):
        #print "process called."
        if self.ctrl.disableChk.isChecked():
            return
        if self.data is None:
            return

        #print "calculating Probs"
        fn.bendelsSpatialCorrelationAlgorithm(
            self.data,
            self.ctrl.radiusSpin.value(),
            self.ctrl.spontSpin.value(),
            self.ctrl.deltaTSpin.value(),
            printProcess=False,
            eventsKey=str(self.ctrl.eventCombo.currentText()))
        #print "probs calculated"
        self.data['prob'] = 1 - self.data[
            'prob']  ## give probability that events are not spontaneous

        if self.ctrl.probabilityRadio.isChecked():
            self.emitOutputChanged(self.data)
        elif self.ctrl.thresholdRadio.isChecked():
            arr = self.data['prob']
            arr[1 - arr < self.ctrl.thresholdSpin.value()] = 1
            arr[(1 - arr > self.ctrl.thresholdSpin.value()) * (arr != 1)] = 0
            self.data['prob'] = arr
            self.emitOutputChanged(self.data)
Exemplo n.º 2
0
def randomProbTest(sites, cellDir, n=10, getContours=False, **kwargs):
    #prof = Profiler('randomProbTest', disabled=False)
    keys={
       'x':'modXPosCell',
       'y':'percentDepth',
       'probThreshold': 0.02,
       'timeWindow':0.1,
       'eventKey':'numOfPostEvents',
       'spacing':5e-6,
       'factor':1.11849
    }
    for k in kwargs:
        keys[k] = kwargs[k]
        
        
    tasks = range(n)
    results = np.zeros((n), dtype=[('numOfSpots', int), ('contourInfo', object)])
    
    data = sites[sites['CellDir']==cellDir]
    spontRate = data['numOfPreEvents'].sum()/data['PreRegionLen'].sum()
    #prof.mark('selected data, calculated spontRate')
    
    d = afn.bendelsSpatialCorrelationAlgorithm(data, 90e-6, spontRate, keys['timeWindow'], eventsKey=keys['eventKey'])
    actualNum = len(d[d['prob'] < keys['probThreshold']])
    #prof.mark('calculated actual number of spots')
    if getContours:
        img, xmin, ymin = reserveImageArray(sites)
    
    with Parallelize(tasks, workers=1, results=results)as tasker:
        for task in tasker:
            np.random.seed()
            randomData = randomizeData(data, keys['eventKey'])
            randomData = afn.bendelsSpatialCorrelationAlgorithm(randomData, 90e-6, spontRate, keys['timeWindow'], eventsKey=keys['eventKey'])
            tasker.results[task]['numOfSpots'] = len(randomData[randomData['prob'] < keys['probThreshold']])
            if getContours:
                im = img.copy()
                data = randomData[randomData['prob'] < probThreshold]
                for i, s in enumerate(data):
                    x, y = (int((s[keys['x']]-xmin)/keys['spacing']), int((s[keys['y']]*keys['factor']-ymin)/keys['spacing']))
                    im[x,y] = 1
                ## convolution params are good for 5um grid spacing and 35um spaced samples -- need to generalize for other options
                im = scipy.ndimage.gaussian_filter(im, 2, mode='constant')
                im /= 0.039
                im[im > 0.03] = 1
                im[im <= 0.03] = 0
                stats = getContourStats(im, spacing=keys['spacing'])
                tasker.results[task]['contourInfo'] = stats
                
    #prof.mark('calculated number of spots for %i random trials'%n)
    #prof.finish()
    return actualNum, results
Exemplo n.º 3
0
def convolveCells_Xuying(sites, spacing=5e-6, probThreshold=0.02, probRadius=90e-6):
    #avgCellX = np.array(list(set(sites['xPosCell']))).mean()
    #avgCellY = np.array(list(set(sites['yPosCell']))).mean()
    #xmin = (sites['xPos']-sites['xPosCell']).min() ## point furthest left of the cell
    xmin = sites['xPosCell'].min()
    #ymin = (sites['yPos']-sites['yPosCell']).min() ## point furthest above the cell
    ymin = sites['yPosCell'].min()
    #xmax = (sites['xPos']-sites['xPosCell']).max()
    xmax = sites['xPosCell'].max()
    #ymax = (sites['yPos']-sites['yPosCell']).max()
    ymax = sites['yPosCell'].max()
    xdim = int((xmax-xmin)/spacing)+10
    ydim = int((ymax-ymin)/spacing)+10
    #avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])
    cells = set(sites['CellDir'])
    n = len(cells)
    
    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []
    
    #kernel = np.zeros((xdim, ydim))
    #midx = xdim/2
    #midy = ydim/2
    #radius = int((sampleSpacing)/spacing)
    #radius2 = radius**2
    #for x in range(midx-radius, midx+radius):
        #for y in range(midy - radius, midy+radius):
            #r2 = (x-radius)**2 + (y-radius)**2
            #if r2 <= radius2:
                #kernel[x,y] = 1 
    results = []
    for i, c in enumerate(cells):
        data = sites[sites['CellDir']==c]
        spontRate = data['numOfPreEvents'].sum()/data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, probRadius, spontRate, data[0]['PostRegionLen'])
        #data['prob'][data['prob'] < probThreshold] = 2.         
        #data['prob'][(data['prob'] >= probThreshold)*(data[i]['prob']!= 2.)] = 0.
        #data['prob'][data['prob'] == 2.] = 1. 
        probs = np.zeros(len(data))
        probs[data['prob'] < probThreshold] = 1.
        for j, s in enumerate(data):
            #trans1 = (data['xPosCell'][0] - avgCellX, data['yPosCell'][0]-avgCellY)
            #trans2 = (avgCellX+xmin, avgCellY+ymin)
            trans = (xmin, ymin)
            #x, y = (int((s['xPos']-trans1[0]-trans2[0])/spacing), int((s['yPos']-trans1[1]-trans2[1])/spacing))
            x, y = (int((s['xPosCell']-trans[0])/spacing), int((s['yPosCell'] - trans[1])/spacing))
            #print i, x, y, j
            arr[i, x, y] = probs[j] 
              
        results.append(arr[i].copy())
        arr[i] = scipy.ndimage.gaussian_filter(arr[i], 2)
        arr[i] = arr[i]/0.039
        #arr[i][arr[i] > 0.04] = 2
        #arr[i][(arr[i] > 0.02)*(arr[i] <=0.04)] = 1
        arr[i][arr[i] > 0.02] = 1
        arr[i][arr[i] <= 0.02] = 0
        
        #arr[i] = scipy.ndimage.convolve(arr[i], kernel)
        
    return arr, results
Exemplo n.º 4
0
def interpolateCells(sites, spacing=5e-6, method='nearest', probThreshold=0.05):
    avgCellX = np.array(list(set(sites['CellXPos']))).mean()
    avgCellY = np.array(list(set(sites['CellYPos']))).mean()
    xmin = (sites['xPos']-sites['CellXPos']).min() ## point furthest left of the cell
    ymin = (sites['yPos']-sites['CellYPos']).min() ## point furthest above the cell
    xmax = (sites['xPos']-sites['CellXPos']).max()
    ymax = (sites['yPos']-sites['CellXPos']).max()
    xdim = int((xmax-xmin)/spacing)+10
    ydim = int((ymax-ymin)/spacing)+10
    avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])
    cells = set(sites['CellDir'])
    n = len(cells)
    
    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []
    print 'xdim:', xdim, 'ydim:', ydim
    for i, c in enumerate(cells):
        data = sites[sites['CellDir'] == c]
        trans1 = (data['CellXPos'][0] - avgCellX, data['CellYPos'][0]-avgCellY)
        trans2 = (avgCellX+xmin, avgCellY+ymin)
        pts = np.array([data['xPos']-trans1[0]-trans2[0], data['yPos']-trans1[1]-trans2[1]], dtype=float)
        #pts[0] = pts[0]+(avgCellX-xmin)
        #pts[1] = pts[1]+(avgCellY-ymin)
        xlimits = (int((data['xPos'].min()-trans1[0]-trans2[0])/spacing), int((data['xPos'].max()-trans1[0]-trans2[0])/spacing))
        ylimits = (int((data['yPos'].min()-trans1[1]-trans2[1])/spacing), int((data['yPos'].max()-trans1[1]-trans2[1])/spacing)) 
        print 'xlimits:', xlimits, '   ylimits:', ylimits
        pts = pts.transpose()/spacing 
        
        xi = np.indices((xdim, ydim))
        xi = xi.transpose(1,2,0)
        
        spontRate = data['numOfPreEvents'].sum()/data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, 90e-6, spontRate, data[0]['PostRegionLen'])
        
        #print data['prob'].max()
        data['prob'][data['prob'] < probThreshold] = 2.
       # print data['prob'].max()
        data['prob'][(data['prob'] >= probThreshold)*(data['prob']!= 2.)] = 0.
        data['prob'][data['prob'] == 2.] = 1.
        #print data['prob'].max()
        #print "========= ", data['prob'].mean()
    
        
        res = scipy.interpolate.griddata(pts, data['prob'], xi, method=method)
        res[:xlimits[0], :] = 0
        res[xlimits[1]+1:, :] = 0
        res[:, :ylimits[0]] = 0
        res[:, ylimits[1]+1:] = 0
        
        arr[i] = res
        results.append(res)
        #arr[i][np.isnan(arr[i])] = .1
    
    return arr, (xmin, ymin)
Exemplo n.º 5
0
 def process(self):
     #print "process called."
     if self.ctrl.disableChk.isChecked():
         return
     if self.data is None:
         return
     
     #print "calculating Probs"
     fn.bendelsSpatialCorrelationAlgorithm(self.data, self.ctrl.radiusSpin.value(), self.ctrl.spontSpin.value(), self.ctrl.deltaTSpin.value(), printProcess=False, eventsKey=str(self.ctrl.eventCombo.currentText()))
     #print "probs calculated"
     self.data['prob'] = 1-self.data['prob'] ## give probability that events are not spontaneous
     
     if self.ctrl.probabilityRadio.isChecked():
         self.emitOutputChanged(self.data)
     elif self.ctrl.thresholdRadio.isChecked():
         arr = self.data['prob']
         arr[1-arr < self.ctrl.thresholdSpin.value()] = 1
         arr[(1-arr > self.ctrl.thresholdSpin.value())*(arr!=1)] = 0
         self.data['prob'] = arr
         self.emitOutputChanged(self.data)
Exemplo n.º 6
0
def convolveCells(sites,
                  spacing=5e-6,
                  probThreshold=0.02,
                  probRadius=90e-6,
                  timeWindow=0.1,
                  eventKey='numOfPostEvents'):
    #avgCellX = np.array(list(set(sites['xPosCell']))).mean()
    #avgCellY = np.array(list(set(sites['yPosCell']))).mean()
    #xmin = (sites['xPos']-sites['xPosCell']).min() ## point furthest left of the cell
    xmin = sites['xPosCell'].min()
    #ymin = (sites['yPos']-sites['yPosCell']).min() ## point furthest above the cell
    ymin = sites['yPosCell'].min()
    #xmax = (sites['xPos']-sites['xPosCell']).max()
    xmax = sites['xPosCell'].max()
    #ymax = (sites['yPos']-sites['yPosCell']).max()
    ymax = sites['yPosCell'].max()
    xdim = int((xmax - xmin) / spacing) + 10
    ydim = int((ymax - ymin) / spacing) + 10
    #avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])
    cells = set(sites['CellDir'])
    n = len(cells)

    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []

    for i, c in enumerate(cells):
        data = sites[sites['CellDir'] == c]
        spontRate = data['numOfPreEvents'].sum() / data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data,
                                                      probRadius,
                                                      spontRate,
                                                      timeWindow=timeWindow,
                                                      eventKey=eventKey)

        probs = np.zeros(len(data))
        probs[data['prob'] < probThreshold] = 1.
        for j, s in enumerate(data):
            trans1 = (data['CellXPos'][0] - avgCellX,
                      data['CellYPos'][0] - avgCellY)
            trans2 = (avgCellX + xmin, avgCellY + ymin)
            x, y = (int((s['xPos'] - trans1[0] - trans2[0]) / spacing),
                    int((s['yPos'] - trans1[1] - trans2[1]) / spacing))
            arr[i, x, y] = probs[j]

        results.append(arr[i].copy())
        arr[i] = scipy.ndimage.gaussian_filter(arr[i], 2)
        arr[i] = arr[i] / 0.039
        arr[i][arr[i] > 0.02] = 1
        #arr[i][(arr[i] > 0.02)*(arr[i] <=0.04)] = 1
        arr[i][arr[i] <= 0.02] = 0

    return arr, results
Exemplo n.º 7
0
def calculateProb(sites, spacing=5e-6, keys=None):
    cells = set(sites['CellDir'])
    arr = reserveArray(sites, spacing)
    
    for i, c in enumerate(cells):
        sites = data[data['CellDir']==c]
        spontRate = sites['numOfPreEvents'].sum()/sites['PreRegionLen'].sum()
        sites = afn.bendelsSpatialCorrelationAlgorithm(sites, 90e-6, spontRate, sites[0]['PostRegionLen'])
        for s in sites:
            x, y = (int((s['xPos']-xmin)/spacing), int((s['yPos']-ymin)/spacing))
            arr[i, x, y] = s['prob']
            
    return arr
Exemplo n.º 8
0
def calculateProb(sites, spacing=5e-6, keys=None):
    cells = set(sites['CellDir'])
    arr = reserveArray(sites, spacing)

    for i, c in enumerate(cells):
        sites = data[data['CellDir'] == c]
        spontRate = sites['numOfPreEvents'].sum() / sites['PreRegionLen'].sum()
        sites = afn.bendelsSpatialCorrelationAlgorithm(
            sites, 90e-6, spontRate, sites[0]['PostRegionLen'])
        for s in sites:
            x, y = (int(
                (s['xPos'] - xmin) / spacing), int(
                    (s['yPos'] - ymin) / spacing))
            arr[i, x, y] = s['prob']

    return arr
Exemplo n.º 9
0
def interpolateSlice(sites,
                     spacing=5e-6,
                     method='nearest',
                     probThreshold=0.05):
    xmin = sites['xPos'].min()
    ymin = sites['yPos'].min()
    xdim = int((sites['xPos'].max() - xmin) / spacing) + 5
    ydim = int((sites['yPos'].max() - ymin) / spacing) + 5
    cells = set(sites['CellDir'])
    n = len(cells)

    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []

    for i, c in enumerate(cells):
        data = sites[sites['CellDir'] == c]
        pts = np.array([data['xPos'], data['yPos']], dtype=float)
        pts[0] = pts[0] - xmin
        pts[1] = pts[1] - ymin
        pts = pts.transpose() / spacing

        xi = np.indices((xdim, ydim))
        xi = xi.transpose(1, 2, 0)

        spontRate = data['numOfPreEvents'].sum() / data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, 90e-6, spontRate,
                                                      data[0]['PostRegionLen'])

        #print data['prob'].max()
        data['prob'][data['prob'] < probThreshold] = 2.
        # print data['prob'].max()
        data['prob'][(data['prob'] >= probThreshold) *
                     (data['prob'] != 2.)] = 0.
        data['prob'][data['prob'] == 2.] = 1.
        #print data['prob'].max()
        #print "========= ", data['prob'].mean()

        res = scipy.interpolate.griddata(pts, data['prob'], xi, method=method)
        arr[i] = res
        results.append(res)
        #arr[i][np.isnan(arr[i])] = .1

    return arr
Exemplo n.º 10
0
def convolveCells(sites, spacing=5e-6, probThreshold=0.02, probRadius=90e-6, timeWindow=0.1, eventKey='numOfPostEvents'):
    #avgCellX = np.array(list(set(sites['xPosCell']))).mean()
    #avgCellY = np.array(list(set(sites['yPosCell']))).mean()
    #xmin = (sites['xPos']-sites['xPosCell']).min() ## point furthest left of the cell
    xmin = sites['xPosCell'].min()
    #ymin = (sites['yPos']-sites['yPosCell']).min() ## point furthest above the cell
    ymin = sites['yPosCell'].min()
    #xmax = (sites['xPos']-sites['xPosCell']).max()
    xmax = sites['xPosCell'].max()
    #ymax = (sites['yPos']-sites['yPosCell']).max()
    ymax = sites['yPosCell'].max()
    xdim = int((xmax-xmin)/spacing)+10
    ydim = int((ymax-ymin)/spacing)+10
    #avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])
    cells = set(sites['CellDir'])
    n = len(cells)
    
    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []
    
    for i, c in enumerate(cells):
        data = sites[sites['CellDir']==c]
        spontRate = data['numOfPreEvents'].sum()/data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, probRadius, spontRate, timeWindow=timeWindow, eventKey=eventKey)
  
        probs = np.zeros(len(data))
        probs[data['prob'] < probThreshold] = 1.
        for j, s in enumerate(data):
            trans1 = (data['CellXPos'][0] - avgCellX, data['CellYPos'][0]-avgCellY)
            trans2 = (avgCellX+xmin, avgCellY+ymin)            
            x, y = (int((s['xPos']-trans1[0]-trans2[0])/spacing), int((s['yPos']-trans1[1]-trans2[1])/spacing))
            arr[i, x, y] = probs[j]
              
        results.append(arr[i].copy())
        arr[i] = scipy.ndimage.gaussian_filter(arr[i], 2)
        arr[i] = arr[i]/0.039
        arr[i][arr[i] > 0.02] = 1
        #arr[i][(arr[i] > 0.02)*(arr[i] <=0.04)] = 1
        arr[i][arr[i] <= 0.02] = 0
        
    return arr, results
Exemplo n.º 11
0
def interpolateSlice(sites, spacing=5e-6, method='nearest', probThreshold=0.05):
    xmin = sites['xPos'].min()
    ymin = sites['yPos'].min()
    xdim = int((sites['xPos'].max()-xmin)/spacing)+5
    ydim = int((sites['yPos'].max()-ymin)/spacing)+5
    cells = set(sites['CellDir'])
    n = len(cells)
    
    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []
    
    for i, c in enumerate(cells):
        data = sites[sites['CellDir'] == c]
        pts = np.array([data['xPos'], data['yPos']], dtype=float)
        pts[0] = pts[0]-xmin
        pts[1] = pts[1]-ymin
        pts = pts.transpose()/spacing
        
        xi = np.indices((xdim, ydim))
        xi = xi.transpose(1,2,0)
        
        spontRate = data['numOfPreEvents'].sum()/data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, 90e-6, spontRate, data[0]['PostRegionLen'])
        
        #print data['prob'].max()
        data['prob'][data['prob'] < probThreshold] = 2.
       # print data['prob'].max()
        data['prob'][(data['prob'] >= probThreshold)*(data['prob']!= 2.)] = 0.
        data['prob'][data['prob'] == 2.] = 1.
        #print data['prob'].max()
        #print "========= ", data['prob'].mean()
    
        
        res = scipy.interpolate.griddata(pts, data['prob'], xi, method=method)
        arr[i] = res
        results.append(res)
        #arr[i][np.isnan(arr[i])] = .1
    
    return arr
Exemplo n.º 12
0
def randomProbTest(sites, cellDir, n=10, getContours=False, **kwargs):
    #prof = Profiler('randomProbTest', disabled=False)
    keys = {
        'x': 'modXPosCell',
        'y': 'percentDepth',
        'probThreshold': 0.02,
        'timeWindow': 0.1,
        'eventKey': 'numOfPostEvents',
        'spacing': 5e-6,
        'factor': 1.11849
    }
    for k in kwargs:
        keys[k] = kwargs[k]

    tasks = range(n)
    results = np.zeros((n),
                       dtype=[('numOfSpots', int), ('contourInfo', object)])

    data = sites[sites['CellDir'] == cellDir]
    spontRate = data['numOfPreEvents'].sum() / data['PreRegionLen'].sum()
    #prof.mark('selected data, calculated spontRate')

    d = afn.bendelsSpatialCorrelationAlgorithm(data,
                                               90e-6,
                                               spontRate,
                                               keys['timeWindow'],
                                               eventsKey=keys['eventKey'])
    actualNum = len(d[d['prob'] < keys['probThreshold']])
    #prof.mark('calculated actual number of spots')
    if getContours:
        img, xmin, ymin = reserveImageArray(sites)

    with Parallelize(tasks, workers=1, results=results) as tasker:
        for task in tasker:
            np.random.seed()
            randomData = randomizeData(data, keys['eventKey'])
            randomData = afn.bendelsSpatialCorrelationAlgorithm(
                randomData,
                90e-6,
                spontRate,
                keys['timeWindow'],
                eventsKey=keys['eventKey'])
            tasker.results[task]['numOfSpots'] = len(
                randomData[randomData['prob'] < keys['probThreshold']])
            if getContours:
                im = img.copy()
                data = randomData[randomData['prob'] < probThreshold]
                for i, s in enumerate(data):
                    x, y = (int((s[keys['x']] - xmin) / keys['spacing']),
                            int((s[keys['y']] * keys['factor'] - ymin) /
                                keys['spacing']))
                    im[x, y] = 1
                ## convolution params are good for 5um grid spacing and 35um spaced samples -- need to generalize for other options
                im = scipy.ndimage.gaussian_filter(im, 2, mode='constant')
                im /= 0.039
                im[im > 0.03] = 1
                im[im <= 0.03] = 0
                stats = getContourStats(im, spacing=keys['spacing'])
                tasker.results[task]['contourInfo'] = stats

    #prof.mark('calculated number of spots for %i random trials'%n)
    #prof.finish()
    return actualNum, results
Exemplo n.º 13
0
def convolveCells_newAtlas(sites,
                           keys=None,
                           factor=1.11849,
                           spacing=5e-6,
                           probThreshold=0.02,
                           sampleSpacing=35e-6,
                           eventKey=None,
                           timeWindow=None):
    if keys == None:
        keys = {
            'x': 'xPosCell',
            'y': 'yPosCell',
            'mappedX': 'modXPosCell',
            'mappedY': 'percentDepth'
        }
    if eventKey == None:
        eventKey = 'numOfPostEvents'
    #if timeWindow == None:
    #    timeWindow = sites[0]['PostRgnLen']

    #avgCellX = np.array(list(set(sites['CellXPos']))).mean()
    #avgCellY = np.array(list(set(sites['CellYPos']))).mean()
    #xmin = (sites['xPos']-sites['CellXPos']).min() ## point furthest left of the cell
    xmin = sites[keys['mappedX']].min()
    #ymin = (sites['yPos']-sites['CellYPos']).min() ## point furthest above the cell
    ymin = (sites[keys['mappedY']].min()
            if sites[keys['mappedY']].min() < 0 else 0.) * factor
    #xmax = (sites['xPos']-sites['CellXPos']).max()
    xmax = sites[keys['mappedX']].max()
    #ymax = (sites['yPos']-sites['CellXPos']).max()
    ymax = sites[keys['mappedY']].max() * factor

    xdim = int((xmax - xmin) / spacing) + 10
    ydim = int((ymax - ymin) / spacing) + 10
    #avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])

    cells = set(sites['CellDir'])
    n = len(cells)

    arr = np.zeros((n, xdim, ydim), dtype=float)
    sampling = np.zeros((n, xdim, ydim), dtype=float)
    #results = []
    counts = []

    for i, c in enumerate(cells):
        print "index:", i, " = cell:", c

        data = sites[sites['CellDir'] == c]
        if timeWindow == None:
            timeWindow = data[0]['PostRegionLen']
        elif timeWindow == 'pre':
            timeWindow = data[0]['PreRegionLen']
        spontRate = data['numOfPreEvents'].sum() / data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data,
                                                      90e-6,
                                                      spontRate,
                                                      timeWindow,
                                                      eventsKey=eventKey)

        probs = np.zeros(len(data))
        probs[data['prob'] < probThreshold] = 1.
        for j, s in enumerate(data):
            #trans1 = (s[keys['mappedX']] - xmin, s[keys['mappedY']]-ymin)
            #trans2 = (avgCellX+xmin, avgCellY+ymin)
            x, y = (int((s[keys['mappedX']] - xmin) / spacing),
                    int((s[keys['mappedY']] * factor - ymin) / spacing))
            arr[i, x, y] = probs[j]
            sampling[i, x, y] = 1

        counts.append(arr[i].sum())
        #results.append(arr[i].copy())
        arr[i] = scipy.ndimage.gaussian_filter(arr[i], 2, mode='constant')
        arr[i] = arr[i] / 0.039
        arr[i][arr[i] > 0.03] = 1
        arr[i][arr[i] <= 0.03] = 0

        sampling[i] = scipy.ndimage.gaussian_filter(sampling[i], 2)
        sampling[i] = sampling[i] / 0.039
        sampling[i][sampling[i] > 0.02] = 1
        sampling[i][sampling[i] <= 0.02] = 0

        ### mark cell position
        #xind = int(-xmin/spacing)
        #yind = int(data['CellYPos'][0]/spacing)
        ##print "yind=", ymin, '*', factor, '/', spacing
        ##print arr.shape, xind, yind
        #arr[i, xind-1:xind+2, yind-1:yind+2] = 2
        ##print arr.max()

    ### mark separation lines
    #arr[:, int((-xmin-150e-6)/spacing), :] = 3
    #arr[:, int((-xmin-450e-6)/spacing), :] = 3
    #arr[:, int((-xmin+150e-6)/spacing), :] = 3
    #arr[:, int((-xmin+450e-6)/spacing), :] = 3
    #arr[:, :, int(130e-6*factor/spacing)] = 3
    #arr[:, :, int(310e-6*factor/spacing)] = 3
    #arr[:, :, int(450e-6*factor/spacing)] = 3
    #arr[:, :, int(720e-6*factor/spacing)] = 3
    return arr, counts
Exemplo n.º 14
0
def convolveCells_Xuying(sites,
                         spacing=5e-6,
                         probThreshold=0.02,
                         probRadius=90e-6):
    #avgCellX = np.array(list(set(sites['xPosCell']))).mean()
    #avgCellY = np.array(list(set(sites['yPosCell']))).mean()
    #xmin = (sites['xPos']-sites['xPosCell']).min() ## point furthest left of the cell
    xmin = sites['xPosCell'].min()
    #ymin = (sites['yPos']-sites['yPosCell']).min() ## point furthest above the cell
    ymin = sites['yPosCell'].min()
    #xmax = (sites['xPos']-sites['xPosCell']).max()
    xmax = sites['xPosCell'].max()
    #ymax = (sites['yPos']-sites['yPosCell']).max()
    ymax = sites['yPosCell'].max()
    xdim = int((xmax - xmin) / spacing) + 10
    ydim = int((ymax - ymin) / spacing) + 10
    #avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])
    cells = set(sites['CellDir'])
    n = len(cells)

    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []

    #kernel = np.zeros((xdim, ydim))
    #midx = xdim/2
    #midy = ydim/2
    #radius = int((sampleSpacing)/spacing)
    #radius2 = radius**2
    #for x in range(midx-radius, midx+radius):
    #for y in range(midy - radius, midy+radius):
    #r2 = (x-radius)**2 + (y-radius)**2
    #if r2 <= radius2:
    #kernel[x,y] = 1
    results = []
    for i, c in enumerate(cells):
        data = sites[sites['CellDir'] == c]
        spontRate = data['numOfPreEvents'].sum() / data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, probRadius,
                                                      spontRate,
                                                      data[0]['PostRegionLen'])
        #data['prob'][data['prob'] < probThreshold] = 2.
        #data['prob'][(data['prob'] >= probThreshold)*(data[i]['prob']!= 2.)] = 0.
        #data['prob'][data['prob'] == 2.] = 1.
        probs = np.zeros(len(data))
        probs[data['prob'] < probThreshold] = 1.
        for j, s in enumerate(data):
            #trans1 = (data['xPosCell'][0] - avgCellX, data['yPosCell'][0]-avgCellY)
            #trans2 = (avgCellX+xmin, avgCellY+ymin)
            trans = (xmin, ymin)
            #x, y = (int((s['xPos']-trans1[0]-trans2[0])/spacing), int((s['yPos']-trans1[1]-trans2[1])/spacing))
            x, y = (int((s['xPosCell'] - trans[0]) / spacing),
                    int((s['yPosCell'] - trans[1]) / spacing))
            #print i, x, y, j
            arr[i, x, y] = probs[j]

        results.append(arr[i].copy())
        arr[i] = scipy.ndimage.gaussian_filter(arr[i], 2)
        arr[i] = arr[i] / 0.039
        #arr[i][arr[i] > 0.04] = 2
        #arr[i][(arr[i] > 0.02)*(arr[i] <=0.04)] = 1
        arr[i][arr[i] > 0.02] = 1
        arr[i][arr[i] <= 0.02] = 0

        #arr[i] = scipy.ndimage.convolve(arr[i], kernel)

    return arr, results
Exemplo n.º 15
0
def interpolateCells(sites,
                     spacing=5e-6,
                     method='nearest',
                     probThreshold=0.05):
    avgCellX = np.array(list(set(sites['CellXPos']))).mean()
    avgCellY = np.array(list(set(sites['CellYPos']))).mean()
    xmin = (sites['xPos'] -
            sites['CellXPos']).min()  ## point furthest left of the cell
    ymin = (sites['yPos'] -
            sites['CellYPos']).min()  ## point furthest above the cell
    xmax = (sites['xPos'] - sites['CellXPos']).max()
    ymax = (sites['yPos'] - sites['CellXPos']).max()
    xdim = int((xmax - xmin) / spacing) + 10
    ydim = int((ymax - ymin) / spacing) + 10
    avgCellIndex = np.array([
        int((avgCellX - xmin) / spacing) + 5,
        int((avgCellY - ymin) / spacing) + 5
    ])
    cells = set(sites['CellDir'])
    n = len(cells)

    arr = np.zeros((n, xdim, ydim), dtype=float)
    results = []
    print 'xdim:', xdim, 'ydim:', ydim
    for i, c in enumerate(cells):
        data = sites[sites['CellDir'] == c]
        trans1 = (data['CellXPos'][0] - avgCellX,
                  data['CellYPos'][0] - avgCellY)
        trans2 = (avgCellX + xmin, avgCellY + ymin)
        pts = np.array([
            data['xPos'] - trans1[0] - trans2[0],
            data['yPos'] - trans1[1] - trans2[1]
        ],
                       dtype=float)
        #pts[0] = pts[0]+(avgCellX-xmin)
        #pts[1] = pts[1]+(avgCellY-ymin)
        xlimits = (int((data['xPos'].min() - trans1[0] - trans2[0]) / spacing),
                   int((data['xPos'].max() - trans1[0] - trans2[0]) / spacing))
        ylimits = (int((data['yPos'].min() - trans1[1] - trans2[1]) / spacing),
                   int((data['yPos'].max() - trans1[1] - trans2[1]) / spacing))
        print 'xlimits:', xlimits, '   ylimits:', ylimits
        pts = pts.transpose() / spacing

        xi = np.indices((xdim, ydim))
        xi = xi.transpose(1, 2, 0)

        spontRate = data['numOfPreEvents'].sum() / data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, 90e-6, spontRate,
                                                      data[0]['PostRegionLen'])

        #print data['prob'].max()
        data['prob'][data['prob'] < probThreshold] = 2.
        # print data['prob'].max()
        data['prob'][(data['prob'] >= probThreshold) *
                     (data['prob'] != 2.)] = 0.
        data['prob'][data['prob'] == 2.] = 1.
        #print data['prob'].max()
        #print "========= ", data['prob'].mean()

        res = scipy.interpolate.griddata(pts, data['prob'], xi, method=method)
        res[:xlimits[0], :] = 0
        res[xlimits[1] + 1:, :] = 0
        res[:, :ylimits[0]] = 0
        res[:, ylimits[1] + 1:] = 0

        arr[i] = res
        results.append(res)
        #arr[i][np.isnan(arr[i])] = .1

    return arr, (xmin, ymin)
Exemplo n.º 16
0
def convolveCells_newAtlas(sites, keys=None, factor=1.11849, spacing=5e-6, probThreshold=0.02, sampleSpacing=35e-6, eventKey=None, timeWindow=None):
    if keys == None:
        keys = {
            'x':'xPosCell',
            'y':'yPosCell',
            'mappedX': 'modXPosCell',
            'mappedY': 'percentDepth'}
    if eventKey == None:
        eventKey = 'numOfPostEvents'
    #if timeWindow == None:
    #    timeWindow = sites[0]['PostRgnLen']
            
    #avgCellX = np.array(list(set(sites['CellXPos']))).mean()
    #avgCellY = np.array(list(set(sites['CellYPos']))).mean()
    #xmin = (sites['xPos']-sites['CellXPos']).min() ## point furthest left of the cell
    xmin = sites[keys['mappedX']].min()
    #ymin = (sites['yPos']-sites['CellYPos']).min() ## point furthest above the cell
    ymin = (sites[keys['mappedY']].min() if sites[keys['mappedY']].min() < 0 else 0.) *factor
    #xmax = (sites['xPos']-sites['CellXPos']).max()
    xmax = sites[keys['mappedX']].max()
    #ymax = (sites['yPos']-sites['CellXPos']).max()
    ymax = sites[keys['mappedY']].max()*factor
                 
    xdim = int((xmax-xmin)/spacing)+10
    ydim = int((ymax-ymin)/spacing)+10
    #avgCellIndex = np.array([int((avgCellX-xmin)/spacing)+5, int((avgCellY-ymin)/spacing)+5])
    
    cells = set(sites['CellDir'])
    n = len(cells)
    
    arr = np.zeros((n, xdim, ydim), dtype=float)
    sampling = np.zeros((n, xdim, ydim), dtype=float)
    #results = []
    counts = []
    
    for i, c in enumerate(cells):
        print "index:", i," = cell:", c
        
        data = sites[sites['CellDir']==c]
        if timeWindow == None:
            timeWindow = data[0]['PostRegionLen']
        elif timeWindow == 'pre':
            timeWindow = data[0]['PreRegionLen']
        spontRate = data['numOfPreEvents'].sum()/data['PreRegionLen'].sum()
        data = afn.bendelsSpatialCorrelationAlgorithm(data, 90e-6, spontRate, timeWindow, eventsKey=eventKey)
  
        probs = np.zeros(len(data))
        probs[data['prob'] < probThreshold] = 1.
        for j, s in enumerate(data):
            #trans1 = (s[keys['mappedX']] - xmin, s[keys['mappedY']]-ymin)
            #trans2 = (avgCellX+xmin, avgCellY+ymin)            
            x, y = (int((s[keys['mappedX']]-xmin)/spacing), int((s[keys['mappedY']]*factor-ymin)/spacing))
            arr[i, x, y] = probs[j]
            sampling[i, x, y] = 1
        
        counts.append(arr[i].sum()) 
        #results.append(arr[i].copy())
        arr[i] = scipy.ndimage.gaussian_filter(arr[i], 2, mode='constant')
        arr[i] = arr[i]/0.039
        arr[i][arr[i] > 0.03] = 1
        arr[i][arr[i] <= 0.03] = 0
        
        sampling[i] = scipy.ndimage.gaussian_filter(sampling[i], 2)
        sampling[i] = sampling[i]/0.039
        sampling[i][sampling[i] > 0.02] = 1
        sampling[i][sampling[i] <= 0.02] = 0    
    
        ### mark cell position
        #xind = int(-xmin/spacing)
        #yind = int(data['CellYPos'][0]/spacing)
        ##print "yind=", ymin, '*', factor, '/', spacing
        ##print arr.shape, xind, yind
        #arr[i, xind-1:xind+2, yind-1:yind+2] = 2
        ##print arr.max()

    ### mark separation lines
    #arr[:, int((-xmin-150e-6)/spacing), :] = 3
    #arr[:, int((-xmin-450e-6)/spacing), :] = 3
    #arr[:, int((-xmin+150e-6)/spacing), :] = 3
    #arr[:, int((-xmin+450e-6)/spacing), :] = 3   
    #arr[:, :, int(130e-6*factor/spacing)] = 3
    #arr[:, :, int(310e-6*factor/spacing)] = 3
    #arr[:, :, int(450e-6*factor/spacing)] = 3
    #arr[:, :, int(720e-6*factor/spacing)] = 3
    return arr, counts