def replot_out_dots( radius, thetaRads, X, Y, params ): # Determine who's out of range if ( params['displayRegion'] == 'ring' ): outField = (radius >= params['outerRadiusUnits']) inField = (radius <= 0) out = outField + inField elif ( params['displayRegion'] == 'rect' ): gtMaxX = (X >= params['maxXunits']) ltMinX = (X <= params['minXunits']) gtMaxY = (Y >= params['maxYunits']) ltMinY = (Y >= params['minYunits']) outX = ( gtMaxX | ltMinX ) outY = ( gtMaxY | gtMaxY ) out = ( outX | outY ) else: print "Invalid displayRegion" if params['debugMode']: print 'Out= ' + str( sum( outField ) ) + '| In= ' + str( sum( inField ) )+ ' | MaxX = ' + str( numpy.max( X ) ) + ' | MinX = ' + str( numpy.min( X ) ) + ' | MinR = ' + str( numpy.min(radius) ) # Replot based on replotMode and moveMode if params['replotMode'] == 'wrap': if params['displayRegion'] == 'ring': if (params['moveMode'] == 'radial') or (params['moveMode'] == 'rotation') or (params['moveMode'] == 'random'): if out.any: radius = numpy.mod( radius, params['outerRadiusUnits'] ) X, Y = pol2cart( thetaRads, radius, units='rads' ) elif (params['moveMode'] == 'laminar'): if out.any: # mirror reverse X, Y coordinates X[ out ] = -1.0*X[out] Y[ out ] = -1.0*Y[out] thetaRads, radius = cart2pol( X, Y, units='rad' ) elif params['replotMode'] == 'replot-scaled-polar': if outField.any: radius[outField], thetaRads[outField], X[outField], Y[outField] = make_dots( sum( outField ), params, min=0., max = params['innerRadiusUnits'], distributionMode = 'scaled-polar' ) if inField.any: radius[inField], thetaRads[inField], X[inField], Y[inField] = make_dots( sum( inField ), params, min=params['outerRadiusUnits'], max=params['outerRadiusUnits']+params['dotSpeedUnits'], distributionMode = 'scaled-polar' ) elif params['replotMode'] == 'replot-radial': if out.any: radius[out], thetaRads[out], X[out], Y[out] = make_dots( sum( out ), params, min=0, max=params['outerRadiusUnits'], distributionMode = 'uniform-polar' ) elif params['replotMode'] == 'replot-rect': if out.any: radius[out], thetaRads[out], X[out], Y[out] = make_dots( sum( out ), params, min=params['xMinUnits'], max=params['xMaxUnits'], distributionMode='uniform-rect' ) # Convert changed X,Y to polar thetaRads, radius = cart2pol( X, Y, units='rad' ) # end if replotMode return radius, thetaRads, X, Y
def dots_update(dotsX, dotsY, frameCount, dotSpeed=dotSpeed, frameDeathAfter=np.inf): # convert to polar coordinates dotsTheta, dotsRadius = cart2pol(dotsX, dotsY) # update radius dotsRadius = (dotsRadius + dotSpeed) # decide which dots die lgcOutFieldDots = np.zeros(len(dotsTheta), dtype='bool') if dotSpeed > 0: # create lgc for elems where radius too large lgcOutFieldDots = (dotsRadius >= FieldSizeRadius) elif dotSpeed < 0: # create lgc for elems where radius too small lgcOutFieldDots = (dotsRadius <= innerBorder) # create logical for where frameCount too high lgcFrameDeath = (frameCount >= frameDeathAfter) # combine logicals lgcDeath = np.logical_or(lgcOutFieldDots, lgcFrameDeath) # replace dead dots dotsRadius[lgcDeath] = np.random.uniform(innerBorder, FieldSizeRadius, size=sum(lgcDeath)) dotsTheta[lgcDeath] = np.random.rand(sum(lgcDeath)) * 360 # convert dotsX, dotsY = pol2cart(dotsTheta, dotsRadius) # increase frameCount for every elements frameCount += 1 # set the counter for newborn dots to zero frameCount[lgcDeath] = 0 return dotsX, dotsY, frameCount
def computeChaosPos(dir=0): if dir == -1: # left directionRange1 = [-180, -20] directionRange2 = [20, 180] elif dir == 1: # right directionRange1 = [-160, 0] directionRange2 = [0, 160] # Initial parameters dotsTheta = np.random.rand(nDots) * 360 # deg dotsRadius = (np.random.rand(nDots) ** 0.5) * fieldRadius # in deg dotsX, dotsY = pol2cart(dotsTheta, dotsRadius) # in deg XYpos = np.empty((nDots, 2, maxFrames)) for iFrame in range(maxFrames): # choose direction dir1 = np.random.rand(int(nDots / 2)) * (directionRange1[1] - directionRange1[0]) + directionRange1[0] dir2 = np.random.rand(int(nDots / 2)) * (directionRange2[1] - directionRange2[0]) + directionRange2[0] dirAll = np.hstack((dir1, dir2)) # update position dotsX, dotsY = dotsX + speedFrame * np.cos(dirAll), dotsY + speedFrame * np.sin(dirAll) # convert catesian to polar dotsTheta, dotsRadius = cart2pol(dotsX, dotsY) outFieldDots = (dotsRadius > fieldRadius) # judge dots outside if outFieldDots.any(): dotsRadius[outFieldDots] = np.random.rand(sum(outFieldDots)) * fieldRadius dotsTheta[outFieldDots] = np.random.rand(sum(outFieldDots)) * 360 # deg dotsX, dotsY = pol2cart(dotsTheta, dotsRadius) XYpos[:, 0, iFrame] = dotsX XYpos[:, 1, iFrame] = dotsY return XYpos
def makeCoherentOris(XYs, coherence, formAngle): nNew = XYs.shape[0] # length along the first dimension newOris = random(nNew) * 180 # create a random array 0:180 # select some elements to be coherent possibleIndices = range(nNew) # create an array of indices... shuffle(possibleIndices) # ...shuffle it'in-place' (without creating a new array)... coherentIndices = possibleIndices[0 : int(nNew * coherence)] # ...and take first nnn elements # set the ori of the coherent elements theta, radius = cart2pol(XYs[:, 0], XYs[:, 1]) # get polar coordinates for elements newOris[coherentIndices] = theta[coherentIndices] + formAngle return newOris
def makeCoherentOris(XYs, coherence, formAngle): nNew = XYs.shape[0]#length along the first dimension newOris = random(nNew)*180 #create a random array 0:180 #select some elements to be coherent possibleIndices = range(nNew)#create an array of indices... shuffle(possibleIndices) #...shuffle it'in-place' (without creating a new array)... coherentIndices = possibleIndices[0:int(nNew*coherence)]#...and take first nnn elements #set the ori of the coherent elements theta, radius = cart2pol(XYs[:,0], XYs[:,1]) #get polar coordinates for elements newOris[coherentIndices] = formAngle-theta[coherentIndices] return newOris
def dots_update_outward(dotsXout, dotsYout): # convert to polar coordinates dotsTheta, dotsRadius = cart2pol(dotsXout, dotsYout) #update radius dotsRadius = (dotsRadius + dotSpeed) #random radius where radius too large outFieldDots = (dotsRadius >= FieldSizeRadius) dotsRadius[outFieldDots] = np.random.rand( sum(outFieldDots)) * FieldSizeRadius # convert dotsXout, dotsYout = pol2cart(dotsTheta, dotsRadius) return dotsXout, dotsYout
def dots_update_inward(dotsXin, dotsYin): # convert to polar coordinates dotsTheta, dotsRadius = cart2pol(dotsXin, dotsYin) # update radius dotsRadius = (dotsRadius - dotSpeed) # random radius where radius too large outFieldDots = (dotsRadius <= 0) dotsRadius[outFieldDots] = (np.random.rand(sum(outFieldDots))** 0.5) * FieldSizeRadius # convert dotsXin, dotsYin = pol2cart(dotsTheta, dotsRadius) return dotsXin, dotsYin
def dots_update_lifetime(dotsX, dotsY, frameCount, dotSpeed=dotSpeed, frameDeathAfter=np.inf): # convert to polar coordinates dotsTheta, dotsRadius = cart2pol(dotsX, dotsY) # update radius dotsRadius = (dotsRadius + dotSpeed) # update frameCount frameCount += 1 # prepare array for dots that will die from falling out lgcOutFieldDots = np.zeros(len(dotsTheta), dtype='bool') # decide which dots fall out during expansion if dotSpeed > 0: # create lgc for elems where radius too large (expansion) lgcOutFieldDots = (dotsRadius >= FieldSizeRadius) # decide which dots fall out during contraction elif dotSpeed < 0: # create lgc for elems where radius too small (contraction) lgcOutFieldDots = (dotsRadius <= innerBorder) # decide which dots will die because they got too old lgcFrameDeath = (frameCount >= frameDeathAfter) # combine logicals from dots that died due to fell out and high age lgcDeath = np.logical_or(lgcOutFieldDots, lgcFrameDeath) radiOffset.append(dotsRadius[lgcDeath]) # calculate new radius for dots that died dotsRadius[lgcDeath] = np.sqrt( (np.square(FieldSizeRadius) - np.square(innerBorder)) * np.random.rand(sum(lgcDeath)) + np.square(innerBorder)) radiOnset.append(dotsRadius[lgcDeath]) # calculate new angle for all dots that died (from age or falling) dotsTheta[lgcDeath] = np.random.uniform(0, 360, sum(lgcDeath)) # reset the counter for newborn dots that died of high age frameCount[lgcFrameDeath] = 0 # reset the counter for newborn dots that died from falling out frameCount[lgcOutFieldDots] = np.random.uniform( 0, dotLife, size=sum(lgcOutFieldDots)).astype(int) radiDensity.append(dotsRadius) # convert from polar to Cartesian dotsX, dotsY = pol2cart(dotsTheta, dotsRadius) return dotsX, dotsY, frameCount
def make_dots( nDots, params, min=-1, max=1, distributionMode='uniform-rect' ): if distributionMode == 'scaled-polar': # Adjusts for density change due to outward radial motion r = numpy.random.rand(nDots)**(0.5)*(max - min) + min th = numpy.random.rand(nDots)*(2*numpy.pi)-numpy.pi X, Y = pol2cart( th, r, units='rad' ) elif distributionMode == 'uniform-polar': r = numpy.random.rand(nDots)*(max - min) + min th = numpy.random.rand(nDots)*(2*numpy.pi)-numpy.pi X, Y = pol2cart( th, r, units='rad' ) elif distributionMode == 'fixed-circle': th = numpy.random.rand(nDots)*(2*numpy.pi)-numpy.pi r = numpy.ones(nDots)*.95 X, Y = pol2cart( th, r, units='rad' ) elif distributionMode == 'hybrid-uniform-rect-polar': # dots of uniform density in circular region X, Y = numpy.random.rand(nDots)*(max-min)+min, numpy.random.rand(nDots)*(max-min)+min th, r = cart2pol( X, Y, units = 'rad' ) outR = ( r >= params['outerRadiusUnits'] ) r[ outR ] = numpy.random.rand(sum(outR))*params['outerRadiusUnits'] X, Y = pol2cart( th, r, units='rad' ) else : # default is random in [-1,1] and [-1,1] in Cartesian coordinates X, Y = numpy.random.rand( nDots )*(max - min) + min, numpy.random.rand( nDots )*(max - min) + min th, r = cart2pol( X, Y, units = 'rad' ) return r, th, X, Y
def makeCoherentOris(XYs, coherence, formAngle): # length along the first dimension: nNew = XYs.shape[0] # random orientations: newOris = random(nNew) * 180 # select some elements to be coherent possibleIndices = list(range(nNew)) # create an array of indices shuffle(possibleIndices) # shuffle it 'in-place' (no new array) coherentIndices = possibleIndices[0:int(nNew * coherence)] # use polar coordinates; set the ori of the coherent elements theta, radius = cart2pol(XYs[:, 0], XYs[:, 1]) newOris[coherentIndices] = formAngle - theta[coherentIndices] return newOris
def makeCoherentOris(XYs, coherence, formAngle): # length along the first dimension: nNew = XYs.shape[0] # random orientations: newOris = random(nNew) * 180 # select some elements to be coherent possibleIndices = list(range(nNew)) # create an array of indices shuffle(possibleIndices) # shuffle it 'in-place' (no new array) coherentIndices = possibleIndices[0: int(nNew * coherence)] # use polar coordinates; set the ori of the coherent elements theta, radius = cart2pol(XYs[: , 0], XYs[: , 1]) newOris[coherentIndices] = formAngle - theta[coherentIndices] return newOris
def move_dots( r, th, X, Y, params ): if params['moveMode'] == 'radial': # Coherent r[ params['cohIndex'] ] += params['dotSpeedUnits'] X[ params['cohIndex'] ], Y[ params['cohIndex'] ] = pol2cart(th[ params['cohIndex'] ], r[ params['cohIndex'] ], units='rad') # Incoherent if params['nDots'] > params['nCoherent']: inCohIndex = numpy.invert( params['cohIndex'] ) dTheta = numpy.random.rand(params['nDots']-params['nCoherent'])*numpy.pi*2.0 X[ inCohIndex ] += params['dotSpeedUnits']*numpy.cos( dTheta ) Y[ inCohIndex ] += params['dotSpeedUnits']*numpy.sin( dTheta ) th[ inCohIndex ], r[ inCohIndex ] = cart2pol( X[ inCohIndex ], Y[ inCohIndex ], units ='rad' ) # Normalize Cartesian and Polar coordinates # X, Y = pol2cart(th, r, units='rad') elif params['moveMode'] == 'rotation-eq-angle': # Coherent th[ params['cohIndex'] ] += params['dotSpeedUnits'] X[ params['cohIndex'] ], Y[ params['cohIndex'] ] = pol2cart(th[ params['cohIndex'] ], r[ params['cohIndex'] ], units='rad') # Incoherent if params['nDots'] > params['nCoherent']: inCohIndex = numpy.invert( params['cohIndex'] ) dTheta = numpy.random.rand(params['nDots']-params['nCoherent'])*numpy.pi*2.0 X[ inCohIndex ] += params['dotSpeedUnits']*numpy.cos( dTheta ) Y[ inCohIndex ] += params['dotSpeedUnits']*numpy.sin( dTheta ) th[ inCohIndex ], r[ inCohIndex ] = cart2pol( X[ inCohIndex ], Y[ inCohIndex ], units ='rad' ) elif params['moveMode'] == 'rotation-eq-spd': # Coherent X[ params['cohIndex'] ] += params['dotSpeedUnits']*numpy.sin( numpy.pi - th[ params['cohIndex'] ] ) Y[ params['cohIndex'] ] += params['dotSpeedUnits']*numpy.cos( numpy.pi - th[ params['cohIndex'] ] ) th[ params['cohIndex'] ], r[ params['cohIndex'] ] = cart2pol( X[ params['cohIndex'] ], Y[ params['cohIndex'] ], units ='rad' ) # Incoherent if params['nDots'] > params['nCoherent']: inCohIndex = numpy.invert( params['cohIndex'] ) dTheta = numpy.random.rand(params['nDots']-params['nCoherent'])*numpy.pi*2.0 X[ inCohIndex ] += params['dotSpeedUnits']*numpy.cos( dTheta ) Y[ inCohIndex ] += params['dotSpeedUnits']*numpy.sin( dTheta ) th[ inCohIndex ], r[ inCohIndex ] = cart2pol( X[ inCohIndex ], Y[ inCohIndex ], units ='rad' ) elif params['moveMode'] == 'laminar': # Coherent X[ params['cohIndex'] ] += params['dotSpeedUnits']*numpy.cos( params['laminarDirRads'] ) Y[ params['cohIndex'] ] += params['dotSpeedUnits']*numpy.sin( params['laminarDirRads'] ) th[ params['cohIndex'] ], r[ params['cohIndex'] ] = cart2pol( X[ params['cohIndex'] ], Y[ params['cohIndex'] ], units ='rad' ) # Incoherent if params['nDots'] > params['nCoherent']: inCohIndex = numpy.invert( params['cohIndex'] ) dTheta = numpy.random.rand(params['nDots']-params['nCoherent'])*numpy.pi*2.0 X[ inCohIndex ] += params['dotSpeedUnits']*numpy.cos( dTheta ) Y[ inCohIndex ] += params['dotSpeedUnits']*numpy.sin( dTheta ) th[ inCohIndex ], r[ inCohIndex ] = cart2pol( X[ inCohIndex ], Y[ inCohIndex ], units ='rad' ) elif params['moveMode'] == 'random': dTheta = numpy.random.rand(params['nDots'])*numpy.pi*2.0 X += params['dotSpeedUnits']*numpy.cos( dTheta ) Y += params['dotSpeedUnits']*numpy.sin( dTheta ) th, r = cart2pol( X, Y, units ='rad' ) # end if moveMode return r, th, X, Y
def dots_update_wrap(dotsX, dotsY, dotSpeed=dotSpeed): # convert to polar coordinates dotsTheta, dotsRadius = cart2pol(dotsX, dotsY) # update radius dotsRadius = (dotsRadius + dotSpeed) # prepare array for dots that will die from falling out lgcOutFieldDots = np.zeros(len(dotsTheta), dtype='bool') # decide which dots fall out during expansion if dotSpeed > 0: # create lgc for elems where radius too large (expansion) lgcOutFieldDots = (dotsRadius >= FieldSizeRadius) # how many dots should go in area A? numDotsAreaA = np.sum( np.random.choice(np.arange(2), p=[1 - pAexp, pAexp], size=np.sum(lgcOutFieldDots))) # get logical for area A lgcAdots = np.zeros(len(dotsTheta), dtype='bool') lgcAdots[np.where(lgcOutFieldDots)[0][:numDotsAreaA]] = True # calculate new radius for dots appearing in region A dotsRadius[lgcAdots] = innerBorder * np.sqrt( (np.square(alphaExp) - 1) * np.random.rand(sum(lgcAdots)) + 1) # get logical for area B lgcBdots = np.zeros(len(dotsTheta), dtype='bool') lgcBdots[np.where(lgcOutFieldDots)[0][numDotsAreaA:]] = True # calculate new radius for dots appearing in region B dotsRadius[lgcBdots] = np.sqrt( (np.square(FieldSizeRadius) - np.square(alphaExp) * np.square(innerBorder)) * np.random.rand(sum(lgcBdots)) + np.square(alphaExp) * np.square(innerBorder)) # decide which dots fall out during contraction elif dotSpeed < 0: # create lgc for elems where radius too small (contraction) lgcOutFieldDots = (dotsRadius <= innerBorder) # how many dots should go in area A? numDotsAreaA = np.sum( np.random.choice(np.arange(2), p=[1 - pAcontr, pAcontr], size=np.sum(lgcOutFieldDots))) # get logical for area A lgcAdots = np.zeros(len(dotsTheta), dtype='bool') lgcAdots[np.where(lgcOutFieldDots)[0][:numDotsAreaA]] = True # calculate new radius for dots appearing in region A dotsRadius[lgcAdots] = Scontr * np.sqrt( (np.square(alphaContr) - 1) * np.random.rand(sum(lgcAdots)) + 1) # get logical for area B lgcBdots = np.zeros(len(dotsTheta), dtype='bool') lgcBdots[np.where(lgcOutFieldDots)[0][numDotsAreaA:]] = True # calculate new radius for dots appearing in region B dotsRadius[lgcBdots] = np.sqrt( (np.square(Scontr) - np.square(innerBorder)) * np.random.rand(sum(lgcBdots)) + np.square(innerBorder)) # calculate new angle for all dots that died (from age or falling) dotsTheta[lgcOutFieldDots] = np.random.uniform(0, 360, sum(lgcOutFieldDots)) # convert from polar to Cartesian dotsX, dotsY = pol2cart(dotsTheta, dotsRadius) return dotsX, dotsY