本文整理汇总了Python中nupic.regions.ImageSensorExplorers.BaseExplorer.BaseExplorer类的典型用法代码示例。如果您正苦于以下问题:Python BaseExplorer类的具体用法?Python BaseExplorer怎么用?Python BaseExplorer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseExplorer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, sweepLength=4,
numRepetitions=1,
sweepOffMode=False,
maxOffset=None,
minVelocity=1,
maxVelocity=3,
seed=42,
*args, **kwargs):
"""
@param sweepLen: number of presentations per sweep sequence.
@param numRepetitions: number of times to present each inward sweep.
"""
BaseExplorer.__init__(self, *args, **kwargs)
# Parameter checking
if type(sweepLength) is not int or sweepLength < 1:
raise RuntimeError("'sweepLength' should be a positive integer")
if type(numRepetitions) is not int or numRepetitions < 1:
raise RuntimeError("'numRepetitions' should be a positive integer")
# Parameters
self._sweepLength = sweepLength
self._numRepetitions = numRepetitions
self._minVelocity = minVelocity
self._maxVelocity = maxVelocity
self._sweepOffMode = sweepOffMode
self._maxOffset = maxOffset
# Internal state
self._seqIndex = 0
self._repIndex = 0
self._state = None
self._repMemory = None
# Prepare PRNG
self._rng = random.Random()
self._rng.seed(seed)
示例2: first
def first(self):
"""
Set up the position.
BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish
to set a different first position should extend this method. Such explorers
may wish to call BaseExplorer.first(center=False), which initializes the
position tuple but does not call centerImage() (which could cause
unnecessary filtering to occur).
"""
BaseExplorer.first(self, center=False)
if not self.numImages:
return
isBlank = True
while isBlank:
# Pick a random position
if (
not self.numJumpsPerImage
or self.lastImageIndex is None
or (self.numJumpsThisImage % self.numJumpsPerImage == 0)
):
# Pick new image
image = self.pickRandomImage(self.random)
self.lastImageIndex = image
self.numJumpsThisImage = 0
else:
image = self.lastImageIndex
self.position["image"] = image
self.position["filters"] = self.pickRandomFilters(self.random)
filteredImages = self.getFilteredImages()
# Pick a random offset
if self.spaceShape is not None:
self.centerImage()
# NOTE: self.position['offset'] is (x, y), whereas our spaceShape is
# (height, width). Also note that the self.position['offset']
# direction is counter-intuitive: negative numbers move us to the RIGHT
# and DOWN instead of LEFT and UP.
xOffset = self.random.randint(-(self.spaceShape[1] // 2), self.spaceShape[1] // 2)
yOffset = self.random.randint(-(self.spaceShape[0] // 2), self.spaceShape[0] // 2)
# print "(yOffset, xOffset) = ", yOffset, xOffset
self.position["offset"][0] += xOffset
self.position["offset"][1] += yOffset
else:
ebbox = self._getEffectiveBoundingBox(filteredImages[0])
self.position["offset"] = [
self.random.randint(ebbox[0], ebbox[2] - 1),
self.random.randint(ebbox[1], ebbox[3] - 1),
]
# Check if the position is blank
isBlank = self.isBlank(self.jumpOffObject)
self.position["reset"] = True
self.numJumpsThisImage += 1
示例3: first
def first(self):
"""
Set up the position.
BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish
to set a different first position should extend this method. Such explorers
may wish to call BaseExplorer.first(center=False), which initializes the
position tuple but does not call centerImage() (which could cause
unnecessary filtering to occur).
"""
BaseExplorer.first(self)
if not self.numImages:
return
# Set up the list of filenames
self.names = []
for i in xrange(self.numImages):
path, filename = os.path.split(self.getImageInfo(i)['imagePath'])
name = os.path.join(os.path.split(path)[1], filename)
self.names.append(name)
# Find the first image with some fixation points
image = 0
while not self.names[image] in self.points:
# No fixation points for this image
image += 1
if image >= self.numImages:
raise RuntimeError("No fixation points for any loaded images")
self.position['image'] = image
self._firstPoint()
self.position['reset'] = True
示例4: first
def first(self):
"""
Set up the position.
BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish
to set a different first position should extend this method. Such explorers
may wish to call BaseExplorer.first(center=False), which initializes the
position tuple but does not call centerImage() (which could cause
unnecessary filtering to occur).
"""
BaseExplorer.first(self, center=True)
# Update the "home" position for the current image
self._getHomePosition()
if self._verbosity >= 1:
print "BlockSpread: first():"
# Set start position
self._centerPosIdx = 0 # Which center point
self._spreadPosIdx = 0 # radial position around the center point
# Convert to X and Y offsets
self._getPosition()
示例5: __init__
def __init__(self, sweepDirections=['left', 'right', 'up', 'down'],
shiftDuringSweep=1, sweepOffObject=False, *args, **kwargs):
"""
sweepDirections -- Directions for sweeping. Must be a list containing
one or more of 'left', 'right', 'up', and 'down' for horizontal and
vertical sweeps, or 'leftup', 'leftdown', 'rightup', and 'rightdown'
for diagonal sweeps (or 'upleft, 'downleft', 'upright', and
'downright'). Can also be the string 'all', for all eight directions.
shiftDuringSweep -- Number of pixels to jump with each step (during
a sweep).
sweepOffObject -- Whether the sensor can only include a part of the
object, as specified by the bounding box. If False, it will only move
to positions that include as much of the object as possible.
"""
BaseExplorer.__init__(self, *args, **kwargs)
if sweepDirections == 'all':
sweepDirections = ['left', 'right', 'up', 'down',
'leftdown', 'leftup', 'rightdown', 'rightup']
else:
for direction in sweepDirections:
if direction not in ('left', 'right', 'up', 'down',
'leftup', 'upleft', 'leftdown', 'downleft',
'rightup', 'upright', 'rightdown', 'downright'):
raise RuntimeError('Unknown sweep direction: %s' % direction)
if type(shiftDuringSweep) is not int:
raise RuntimeError("'shiftDuringSweep' should be an integer")
if type(sweepOffObject) not in (bool, int):
raise RuntimeError("'sweepOffObject' should be a boolean")
self.sweepDirections = sweepDirections
self.shiftDuringSweep = shiftDuringSweep
self.sweepOffObject = sweepOffObject
示例6: next
def next(self, seeking=False):
"""
Go to the next position (next iteration).
seeking -- Boolean that indicates whether the explorer is calling next()
from seek(). If True, the explorer should avoid unnecessary computation
that would not affect the seek command. The last call to next() from
seek() will be with seeking=False.
"""
BaseExplorer.next(self)
if self.pointIndex is None:
self.first()
self.pointIndex += 1
if self.pointIndex < len(self.currentPoints):
# Next fixation point for this image
self._setOffset()
else:
# Ran out of points for this image
image = self.position['image'] + 1
if image >= self.numImages:
self.first()
return
while not self.names[image] in self.points:
image += 1
if image >= self.numImages:
self.first()
return
self.position['image'] = image
self._firstPoint()
示例7: __init__
def __init__(self, numSteps, diagonals=False, jitterSize=0, *args, **kwargs):
"""
numSteps --
diagonals -- Whether to step along the diagonal
jitterSize -- How much to jitter around each step of the onion trajectories.
"""
BaseExplorer.__init__(self, *args, **kwargs)
self.numSteps = numSteps
self.diagonals = diagonals
if self.diagonals:
self.offsetDelta = [[-1,-1],[-1,1],[1,1],[1,-1]]
else:
self.offsetDelta = [[-numSteps,-numSteps],[-numSteps,numSteps], \
[numSteps,numSteps],[numSteps,-numSteps]]
if jitterSize == 0:
self.jitter = [[0,0]]
self.jitterLength = 1
else:
# eg. jitterSize = 2 ->
# self.jitter = [[-2,0],[-1,0],[1,0],[2,0],
# [0,2],[0,1],[0,-1],[0,-2],
# [0,0]]
self.jitter = []
listi = range(-jitterSize,0) + range(1,jitterSize+1)
for i in listi:
self.jitter.append([i,0])
for i in listi:
self.jitter.append([0,i])
self.jitter.append([0,0])
self.jitterLength = len(self.jitter)
assert(self.jitterLength == 4*jitterSize+1)
示例8: update
def update(self, **kwargs):
"""
Update state with new parameters from ImageSensor and call first().
"""
numFilters = kwargs.get('numFilters', None)
if numFilters is not None and self.allDimensions:
# Remove existing filter dimensions
for dimension in self.dimensions[:]:
if type(dimension['name']) is int:
self.dimensions.remove(dimension)
# Reset the probabilities from the existing dimensions
for d in self.dimensions:
d['probability'] = None
# Add the new filter dimensions
self.dimensions += \
[self._newSweepDictionary(name=name) for name in range(numFilters)]
numImages = kwargs.get('numImages', None)
if numImages is not None and self.pattern:
# Parse all the filenames
self._parseFilenames(numImages)
self._calculateProbabilities()
BaseExplorer.update(self, **kwargs)
示例9: __init__
def __init__(
self,
sweepDirections=["right", "down"],
shiftDuringSweep=1,
shiftBetweenSweeps=1,
sweepOffObject=False,
order=None,
*args,
**kwargs
):
"""
sweepDirections -- Directions for sweeping (a list containing one or
more of 'left', 'right', 'up', and 'down').
shiftDuringSweep -- Number of pixels to jump with each step (during a
sweep).
shiftBetweenSweeps -- Number of pixels to jump in between sweeps
(for example, when moving down a line after sweeping across).
sweepOffObject -- Whether the sensor can only include a part of the
object, as specified by the bounding box. If False, it will only move to
positions that include as much of the object as possible. If True, it
will sweep until all of the object moves off the sensor. If set to a floating
point number between 0 and 1, then it will sweep until that fraction of the
object moves off the sensor.
order -- Order in which to iterate (outer to inner). Default progresses
through switching images, filters, and sweeping, where switching images
is the outer loop and sweeping is the inner loop. Should be a list
containing 'image', 'sweep', and 0, 1, ... numFilters-1.
"""
BaseExplorer.__init__(self, *args, **kwargs)
for direction in sweepDirections:
if direction not in ("left", "right", "up", "down"):
raise RuntimeError("Unknown sweep direction: '%s'" % direction)
if type(shiftDuringSweep) is not int:
raise RuntimeError("'shiftDuringSweep' must be an integer")
if type(shiftBetweenSweeps) is not int:
raise RuntimeError("'shiftBetweenSweeps' must be an integer")
if float(sweepOffObject) < 0 or float(sweepOffObject) > 1.0:
raise RuntimeError("'sweepOffObject' should be a boolean, or floating point" " number between 0 and 1")
if order is not None:
if "image" not in order or "sweep" not in order:
raise RuntimeError("'order' must contain both 'image' and 'sweep'")
if len([x for x in order if type(x) == str]) > 2:
raise RuntimeError("'order' must contain no other strings besides " "'image' and 'sweep'")
self.customOrder = True
else:
self.customOrder = False
self.sweepDirections = sweepDirections
self.shiftDuringSweep = shiftDuringSweep
self.shiftBetweenSweeps = shiftBetweenSweeps
self.sweepOffObject = sweepOffObject
self.order = order
示例10: next
def next(self, seeking=False):
"""
Go to the next position (next iteration).
seeking -- Boolean that indicates whether the explorer is calling next()
from seek(). If True, the explorer should avoid unnecessary computation
that would not affect the seek command. The last call to next() from
seek() will be with seeking=False.
"""
BaseExplorer.next(self)
self._computeNextPosn()
示例11: first
def first(self):
"""
Set up the position.
BaseExplorer picks image 0, offset (0,0), etc., but explorers that wish
to set a different first position should extend this method. Such explorers
may wish to call BaseExplorer.first(center=False), which initializes the
position tuple but does not call centerImage() (which could cause
unnecessary filtering to occur).
"""
BaseExplorer.first(self, center=False)
self._computeNextPosn()
示例12: next
def next(self, seeking=False):
"""
Go to the next position (next iteration).
seeking -- Boolean that indicates whether the explorer is calling next()
from seek(). If True, the explorer should avoid unnecessary computation
that would not affect the seek command. The last call to next() from
seek() will be with seeking=False.
"""
BaseExplorer.next(self)
if self._verbosity >= 1:
print "BlockSpread: next():"
# ========================================================================
# Update to next position
self._spreadPosIdx += 1
if self._spreadPosIdx == self._numSpreadOffsets:
self._spreadPosIdx = 0
self._centerPosIdx += 1
# If we've run through all the center positions, advance to the next
# filtered image
if self._centerPosIdx == self._numCenterOffsets:
self._centerPosIdx = 0
# --------------------------------------------------------------------
# Go to next filter for this image, or next image
# Iterate through the filters first
needNewImage = True
for i in xrange(self.numFilters):
self.position['filters'][i] += 1
if self.position['filters'][i] < self.numFilterOutputs[i]:
needNewImage = False
break
else:
self.position['filters'][i] = 0
# Go to the next image if ready
if needNewImage:
self.position['image'] += 1
if self.position['image'] == self.numImages:
self.position['image'] = 0
# -----------------------------------------------------------------
# Get the home position for this new filtered image
self._getHomePosition()
# ========================================================================
# Get the X,Y corrdinates and reset signal
if not seeking:
self._getPosition()
示例13: next
def next(self, seeking=False):
"""
Go to the next position (next iteration).
seeking -- Boolean that indicates whether the explorer is calling next()
from seek(). If True, the explorer should avoid unnecessary computation
that would not affect the seek command. The last call to next() from
seek() will be with seeking=False.
"""
BaseExplorer.next(self)
# If filters were changed, order may be invalid
if self.order is None or len([x for x in self.order if type(x) == int]) != self.numFilters:
# If user did not set a custom order, just create new one automatically
if not self.customOrder:
self.order = ["image"]
self.order.extend(range(self.numFilters))
self.order += ["sweep"]
# Otherwise, user needs to recreate the explorer with a new order
else:
raise RuntimeError(
"'order' is invalid. Must recreate explorer with " "valid order after changing filters."
)
if self.position["reset"] and self.blankWithReset:
# Last iteration was a blank, so don't increment the position
self.position["reset"] = False
else:
self.position["reset"] = False
for x in reversed(self.order):
if x == "image": # Iterate the image
self.position["image"] += 1
if self.position["image"] == self.numImages:
self.position["image"] = 0
self.position["reset"] = True
else:
break
elif x == "sweep": # Iterate the sweep position
nextImage = self._nextSweepPosition()
if not nextImage:
break
else: # Iterate the filter with index x
self.position["filters"][x] += 1
if self.position["filters"][x] == self.numFilterOutputs[x]:
self.position["filters"][x] = 0
self.position["reset"] = True
else:
break
if nextImage:
self._firstSweepPosition()
示例14: __init__
def __init__(self, shift=1, replacement=True, *args, **kwargs):
"""
shift -- Number of pixels to move from the center ("radius" of the eye
movement square).
replacement -- Whether the same image/position can be picked twice.
"""
BaseExplorer.__init__(self, *args, **kwargs)
self.shift = shift
self.replacement = replacement
if not self.replacement:
self.history = []
示例15: __init__
def __init__(self, radius=4, *args, **kwargs):
"""
@param radius: the distance from the center, in pixels, at which the
sweeps start;
@param numRepetitions: number of times to present each inward sweep.
"""
BaseExplorer.__init__(self, *args, **kwargs)
# Parameter checking
if type(radius) is not int or radius < 1:
raise RuntimeError("'radius' should be a positive integer")
# Parameters
self._radius = radius
# Internal state
self._itersDone = 0