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
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()
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
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
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(iteration=0)
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()
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) self.directionIndex = 0 if self.numImages: self._firstSweepPosition()
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) self.diagdiri = 0 self.diagi = 0 self.position['offset'][0] += self.numSteps self.cent = list(self.position['offset']) self.jitteri = 0
def first(self, center=True): """ 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) self._resetIndex() offsets = self._getCurrentOffsets() # Set the 2 dimensions of the position. for i in (0, 1): self.position['offset'][i] = offsets[self.index][i]
def first(self, center=True): """ 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) self._resetIndex() offsets = self._getCurrentOffsets() # Set the 2 dimensions of the position. for i in (0,1): self.position['offset'][i] = offsets[self.index][i]
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 if not self.replacement \ and len(self.history) == self.getNumIterations(None): # All images have been visited self.history = [] while True: self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) index = self.random.randint(0, 8) historyItem = (self.position['image'], self.position['filters'][:], index) if self.replacement or historyItem not in self.history: # Use this position if not self.replacement: # Add to the history self.history.append(historyItem) # Calculate the offset from the eye movement index if index in (1, 2, 3): self.position['offset'][1] -= self.shift elif index in (5, 6, 7): self.position['offset'][1] += self.shift if index in (1, 7, 8): self.position['offset'][0] -= self.shift elif index in (3, 4, 5): self.position['offset'][0] += self.shift break self.position['reset'] = True
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 # Pick a random dimension (exclude filters without multiple outputs) filters = [] for i in xrange(self.numFilters): if self.numFilterOutputs[i] > 1: filters.append(i) legalDimensions = self.dimensions[:] for d in self.dimensions: if type(d['name']) is int and d['name'] not in filters: legalDimensions.remove(d) # Choice is weighted by probabilities r = self.random.uniform(0, sum([d['probability'] for d in legalDimensions])) for i, d in enumerate(legalDimensions): if r <= d['probability']: self.dimension = d break r -= d['probability'] self.start = None restart = True while restart: self.position['reset'] = False restart = False # Translation sweep if self.dimension['name'] == 'translation': # Pick a random direction, image, and set of filters self.direction = self.random.choice(('left', 'right', 'up', 'down', 'leftdown', 'leftup', 'rightdown', 'rightup')) self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() ebbox = self._getEffectiveBoundingBox(filteredImages[0]) # Align starting position at zero offset position. forceAlignment = self.dimension.get('forceAlignment') if forceAlignment is not None and forceAlignment: self.position['offset'] = [0,0] # Pick a random starting position on the appropriate edge of the image else: self._firstTranslationPosition(ebbox, filteredImages[0]) # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextTranslationPosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) if not forceAlignment: self._nextTranslationPosition(randomShift=True) # Image sweep elif self.dimension['name'] == 'image': # Pick a random direction self.direction = self.random.choice(('up', 'down')) # Pick a random image and find the first or last image in the category image = self.pickRandomImage(self.random) startCategory = self.getImageInfo(image)['categoryIndex'] if self.direction == 'up': while image > 0 and \ self.getImageInfo(image-1)['categoryIndex'] == startCategory: image -= 1 else: while image < self.numImages - 1 and \ self.getImageInfo(image+1)['categoryIndex'] == startCategory: image += 1 self.position['image'] = image # Pick the filters self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() # Pick a random position within the bounding box ebbox = self._getEffectiveBoundingBox(filteredImages[0]) self.position['offset'] = [ self.random.randint(ebbox[0], ebbox[2]-1), self.random.randint(ebbox[1], ebbox[3]-1) ] # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextImagePosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) self._nextImagePosition(randomShift=True) # Parsed dimension sweep elif self.dimension['name'] in self.parsedDimensions: # Pick a random direction self.direction = self.random.choice(('up', 'down')) # Pick a random image image = self.pickRandomImage(self.random) # Create a list of filenames that will be included in this sweep self._createParsedDimension(image) # Find the first or last image if self.direction == 'up': self.parsedIndex = 0 else: self.parsedIndex = len(self.parsedIndices) - 1 self.position['image'] = self.parsedIndices[self.parsedIndex] # Pick the filters self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() # Pick a random position within the bounding box ebbox = self._getEffectiveBoundingBox(filteredImages[0]) self.position['offset'] = [ self.random.randint(ebbox[0], ebbox[2]-1), self.random.randint(ebbox[1], ebbox[3]-1) ] # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextParsedPosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) self._nextParsedPosition(randomShift=True) # Filter sweep else: # Pick a random direction, image, and set of filters self.direction = self.random.choice(('up', 'down')) self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() # Go to one end of the selected filter if self.direction == 'up': self.position['filters'][self.dimension['name']] = 0 else: self.position['filters'][self.dimension['name']] = \ self.numFilterOutputs[self.dimension['name']] - 1 # Pick a random position within the bounding box filteredImages = self.getFilteredImages() ebbox = self._getEffectiveBoundingBox(filteredImages[0]) self.position['offset'] = [ self.random.randint(ebbox[0], ebbox[2]-1), self.random.randint(ebbox[1], ebbox[3]-1) ] self.prevImageSize = filteredImages[0].size # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextFilterPosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) self._nextFilterPosition(randomShift=True) self.start = self._copyPosition(self.position) self.position['reset'] = True self.length = 1
def first(self, seeking=False): """ 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). seeking -- Passed from seek() through next() to avoid loading images unnecessarily when seeking. """ BaseExplorer.first(self, center=False) if not self.numImages: return if not self.replacement \ and len(self.history) == self.getNumIterations(None): # All images have been visited self.history = [] if self.equalizeCategories: # Breakdown the images by category if self.imagesByCat is None: categoryIndex = [] for k in range(self.numImages): categoryIndex += [self.getImageInfo(k)['categoryIndex']] categories = list(set(categoryIndex)) numCats = len(categories) catPopulation = {} imagesByCat = {} for catIndex in categories: #catPopulation[catIndex] = len([c for c in categoryIndex if c == catIndex]) imagesByCat[catIndex] = [k for k, c in enumerate(categoryIndex) if c == catIndex] catPopulation[catIndex] = len(imagesByCat[catIndex]) minNumSamples = min([pop for (cat, pop) in catPopulation.items()]) totalNumSamples = minNumSamples * numCats # Store self.imagesByCat = imagesByCat self.categories = categories self.numCategories = numCats self.nextCatIndex = 0 # Pick random image from next category thisCat = self.imagesByCat[self.nextCatIndex] #randomImageIndex = random.randint(0, len(thisCat)) self.position['image'] = self.random.choice(thisCat) self.position['filters'] = self.pickRandomFilters(self.random) self.nextCatIndex = (self.nextCatIndex + 1) % self.numCategories else: # Pick a random image and set of filters while self.start >= 0: finished = False while not finished: # Pick a position randomly self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) # Pick again if not replacing and this position has been visited if self.replacement or (self.position['image'], self.position['filters']) not in self.history: finished = True if not self.replacement: # Remember this position self.history.append( (self.position['image'], self.position['filters'][:])) self.start -= 1 self.start = 0 if not seeking: self.centerImage()
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 # Pick a random direction and filtered image self.direction = self.random.choice(self.sweepDirections) self.position['image'] = self.random.randint(0, self.numImages - 1) for i in xrange(self.numFilters): self.position['filters'][i] = self.random.randint(0, self.numFilterOutputs[i] - 1) filteredImages = self.getFilteredImages() # Pick a random starting position on the appropriate edge of the image sbbox = self._getSweepBoundingBox(filteredImages[0]) if self.direction == 'left': self.position['offset'][0] = sbbox[2] - 1 self.position['offset'][1] = self.random.randint(sbbox[1], sbbox[3] - 1) elif self.direction == 'right': self.position['offset'][0] = sbbox[0] self.position['offset'][1] = self.random.randint(sbbox[1], sbbox[3] - 1) elif self.direction == 'up': self.position['offset'][0] = self.random.randint(sbbox[0], sbbox[2] - 1) self.position['offset'][1] = sbbox[3] - 1 elif self.direction == 'down': self.position['offset'][0] = self.random.randint(sbbox[0], sbbox[2] - 1) self.position['offset'][1] = sbbox[1] elif self.direction in ('leftup', 'upleft'): if self.random.randint(0,1): self.position['offset'][0] = \ self.random.randint(sbbox[0] + (sbbox[2] - sbbox[0])/2, sbbox[2] - 1) self.position['offset'][1] = sbbox[3] - 1 else: self.position['offset'][0] = sbbox[2] - 1 self.position['offset'][1] = \ self.random.randint(sbbox[1] + (sbbox[3] - sbbox[1])/2, sbbox[3] - 1) elif self.direction in ('leftdown', 'downleft'): if self.random.randint(0,1): self.position['offset'][0] = \ self.random.randint(sbbox[0] + (sbbox[2] - sbbox[0])/2, sbbox[2] - 1) self.position['offset'][1] = sbbox[1] else: self.position['offset'][0] = sbbox[2] - 1 self.position['offset'][1] = \ self.random.randint(sbbox[1], sbbox[3] - 1 - (sbbox[3] - sbbox[1])/2) elif self.direction in ('rightup', 'upright'): if self.random.randint(0,1): self.position['offset'][0] = \ self.random.randint(sbbox[0], sbbox[2] - 1 - (sbbox[2] - sbbox[0])/2) self.position['offset'][1] = sbbox[3] - 1 else: self.position['offset'][0] = sbbox[0] self.position['offset'][1] = \ self.random.randint(sbbox[1] + (sbbox[3] - sbbox[1])/2, sbbox[3] - 1) elif self.direction in ('rightdown', 'downright'): if self.random.randint(0,1): self.position['offset'][0] = \ self.random.randint(sbbox[0], sbbox[2] - 1 - (sbbox[2] - sbbox[0])/2) self.position['offset'][1] = sbbox[1] else: self.position['offset'][0] = sbbox[0] self.position['offset'][1] = \ self.random.randint(sbbox[1], sbbox[3] - 1 - (sbbox[3] - sbbox[1])/2) # Increment the position by a random amount in the range # [0, shiftDuringSweep) if self.shiftDuringSweep > 1: prevShiftDuringSweep = self.shiftDuringSweep self.shiftDuringSweep = self.random.randint(0, self.shiftDuringSweep) self._nextSweepPosition() self.shiftDuringSweep = prevShiftDuringSweep if self.position['reset']: self.first() self.position['reset'] = True
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 # Pick a random direction and filtered image self.direction = self.random.choice(self.sweepDirections) self.position["image"] = self.random.randint(0, self.numImages - 1) for i in xrange(self.numFilters): self.position["filters"][i] = self.random.randint(0, self.numFilterOutputs[i] - 1) filteredImages = self.getFilteredImages() # Pick a random starting position on the appropriate edge of the image sbbox = self._getSweepBoundingBox(filteredImages[0]) if self.direction == "left": self.position["offset"][0] = sbbox[2] - 1 self.position["offset"][1] = self.random.randint(sbbox[1], sbbox[3] - 1) elif self.direction == "right": self.position["offset"][0] = sbbox[0] self.position["offset"][1] = self.random.randint(sbbox[1], sbbox[3] - 1) elif self.direction == "up": self.position["offset"][0] = self.random.randint(sbbox[0], sbbox[2] - 1) self.position["offset"][1] = sbbox[3] - 1 elif self.direction == "down": self.position["offset"][0] = self.random.randint(sbbox[0], sbbox[2] - 1) self.position["offset"][1] = sbbox[1] elif self.direction in ("leftup", "upleft"): if self.random.randint(0, 1): self.position["offset"][0] = self.random.randint(sbbox[0] + (sbbox[2] - sbbox[0]) / 2, sbbox[2] - 1) self.position["offset"][1] = sbbox[3] - 1 else: self.position["offset"][0] = sbbox[2] - 1 self.position["offset"][1] = self.random.randint(sbbox[1] + (sbbox[3] - sbbox[1]) / 2, sbbox[3] - 1) elif self.direction in ("leftdown", "downleft"): if self.random.randint(0, 1): self.position["offset"][0] = self.random.randint(sbbox[0] + (sbbox[2] - sbbox[0]) / 2, sbbox[2] - 1) self.position["offset"][1] = sbbox[1] else: self.position["offset"][0] = sbbox[2] - 1 self.position["offset"][1] = self.random.randint(sbbox[1], sbbox[3] - 1 - (sbbox[3] - sbbox[1]) / 2) elif self.direction in ("rightup", "upright"): if self.random.randint(0, 1): self.position["offset"][0] = self.random.randint(sbbox[0], sbbox[2] - 1 - (sbbox[2] - sbbox[0]) / 2) self.position["offset"][1] = sbbox[3] - 1 else: self.position["offset"][0] = sbbox[0] self.position["offset"][1] = self.random.randint(sbbox[1] + (sbbox[3] - sbbox[1]) / 2, sbbox[3] - 1) elif self.direction in ("rightdown", "downright"): if self.random.randint(0, 1): self.position["offset"][0] = self.random.randint(sbbox[0], sbbox[2] - 1 - (sbbox[2] - sbbox[0]) / 2) self.position["offset"][1] = sbbox[1] else: self.position["offset"][0] = sbbox[0] self.position["offset"][1] = self.random.randint(sbbox[1], sbbox[3] - 1 - (sbbox[3] - sbbox[1]) / 2) # Increment the position by a random amount in the range # [0, shiftDuringSweep) if self.shiftDuringSweep > 1: prevShiftDuringSweep = self.shiftDuringSweep self.shiftDuringSweep = self.random.randint(0, self.shiftDuringSweep) self._nextSweepPosition() self.shiftDuringSweep = prevShiftDuringSweep if self.position["reset"]: self.first() self.position["reset"] = True
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 # Pick a random dimension (exclude filters without multiple outputs) filters = [] for i in xrange(self.numFilters): if self.numFilterOutputs[i] > 1: filters.append(i) legalDimensions = self.dimensions[:] for d in self.dimensions: if type(d['name']) is int and d['name'] not in filters: legalDimensions.remove(d) # Choice is weighted by probabilities r = self.random.uniform( 0, sum([d['probability'] for d in legalDimensions])) for i, d in enumerate(legalDimensions): if r <= d['probability']: self.dimension = d break r -= d['probability'] self.start = None restart = True while restart: self.position['reset'] = False restart = False # Translation sweep if self.dimension['name'] == 'translation': # Pick a random direction, image, and set of filters self.direction = self.random.choice( ('left', 'right', 'up', 'down', 'leftdown', 'leftup', 'rightdown', 'rightup')) self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() ebbox = self._getEffectiveBoundingBox(filteredImages[0]) # Align starting position at zero offset position. forceAlignment = self.dimension.get('forceAlignment') if forceAlignment is not None and forceAlignment: self.position['offset'] = [0, 0] # Pick a random starting position on the appropriate edge of the image else: self._firstTranslationPosition(ebbox, filteredImages[0]) # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextTranslationPosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) if not forceAlignment: self._nextTranslationPosition(randomShift=True) # Image sweep elif self.dimension['name'] == 'image': # Pick a random direction self.direction = self.random.choice(('up', 'down')) # Pick a random image and find the first or last image in the category image = self.pickRandomImage(self.random) startCategory = self.getImageInfo(image)['categoryIndex'] if self.direction == 'up': while image > 0 and \ self.getImageInfo(image-1)['categoryIndex'] == startCategory: image -= 1 else: while image < self.numImages - 1 and \ self.getImageInfo(image+1)['categoryIndex'] == startCategory: image += 1 self.position['image'] = image # Pick the filters self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() # Pick a random position within the bounding box ebbox = self._getEffectiveBoundingBox(filteredImages[0]) self.position['offset'] = [ self.random.randint(ebbox[0], ebbox[2] - 1), self.random.randint(ebbox[1], ebbox[3] - 1) ] # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextImagePosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) self._nextImagePosition(randomShift=True) # Parsed dimension sweep elif self.dimension['name'] in self.parsedDimensions: # Pick a random direction self.direction = self.random.choice(('up', 'down')) # Pick a random image image = self.pickRandomImage(self.random) # Create a list of filenames that will be included in this sweep self._createParsedDimension(image) # Find the first or last image if self.direction == 'up': self.parsedIndex = 0 else: self.parsedIndex = len(self.parsedIndices) - 1 self.position['image'] = self.parsedIndices[self.parsedIndex] # Pick the filters self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() # Pick a random position within the bounding box ebbox = self._getEffectiveBoundingBox(filteredImages[0]) self.position['offset'] = [ self.random.randint(ebbox[0], ebbox[2] - 1), self.random.randint(ebbox[1], ebbox[3] - 1) ] # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextParsedPosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) self._nextParsedPosition(randomShift=True) # Filter sweep else: # Pick a random direction, image, and set of filters self.direction = self.random.choice(('up', 'down')) self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters(self.random) filteredImages = self.getFilteredImages() # Go to one end of the selected filter if self.direction == 'up': self.position['filters'][self.dimension['name']] = 0 else: self.position['filters'][self.dimension['name']] = \ self.numFilterOutputs[self.dimension['name']] - 1 # Pick a random position within the bounding box filteredImages = self.getFilteredImages() ebbox = self._getEffectiveBoundingBox(filteredImages[0]) self.position['offset'] = [ self.random.randint(ebbox[0], ebbox[2] - 1), self.random.randint(ebbox[1], ebbox[3] - 1) ] self.prevImageSize = filteredImages[0].size # Increment the start position until it is not blank while self.isBlank(self.dimension['sweepOffObject']): self._nextFilterPosition(shift=1) if self.position['reset'] or not self.isValid(): restart = True break if restart: continue # Increment the position by a random amount in the range [0, shift) self._nextFilterPosition(randomShift=True) self.start = self._copyPosition(self.position) self.position['reset'] = True self.length = 1
def first(self, seeking=False): """ 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). seeking -- Passed from seek() through next() to avoid loading images unnecessarily when seeking. """ BaseExplorer.first(self, center=False) if not self.numImages: return if not self.replacement \ and len(self.history) == self.getNumIterations(None): # All images have been visited self.history = [] if self.equalizeCategories: # Breakdown the images by category if self.imagesByCat is None: categoryIndex = [] for k in range(self.numImages): categoryIndex += [self.getImageInfo(k)['categoryIndex']] categories = list(set(categoryIndex)) numCats = len(categories) catPopulation = {} imagesByCat = {} for catIndex in categories: #catPopulation[catIndex] = len([c for c in categoryIndex if c == catIndex]) imagesByCat[catIndex] = [ k for k, c in enumerate(categoryIndex) if c == catIndex ] catPopulation[catIndex] = len(imagesByCat[catIndex]) minNumSamples = min( [pop for (cat, pop) in catPopulation.items()]) totalNumSamples = minNumSamples * numCats # Store self.imagesByCat = imagesByCat self.categories = categories self.numCategories = numCats self.nextCatIndex = 0 # Pick random image from next category thisCat = self.imagesByCat[self.nextCatIndex] #randomImageIndex = random.randint(0, len(thisCat)) self.position['image'] = self.random.choice(thisCat) self.position['filters'] = self.pickRandomFilters(self.random) self.nextCatIndex = (self.nextCatIndex + 1) % self.numCategories else: # Pick a random image and set of filters while self.start >= 0: finished = False while not finished: # Pick a position randomly self.position['image'] = self.pickRandomImage(self.random) self.position['filters'] = self.pickRandomFilters( self.random) # Pick again if not replacing and this position has been visited if self.replacement or ( self.position['image'], self.position['filters']) not in self.history: finished = True if not self.replacement: # Remember this position self.history.append( (self.position['image'], self.position['filters'][:])) self.start -= 1 self.start = 0 if not seeking: self.centerImage()